1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50

base / test / metrics / user_action_tester.cc [blame]

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/test/metrics/user_action_tester.h"

#include <string_view>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/test/test_simple_task_runner.h"

namespace base {

UserActionTester::UserActionTester()
    : task_runner_(new base::TestSimpleTaskRunner),
      action_callback_(base::BindRepeating(&UserActionTester::OnUserAction,
                                           base::Unretained(this))) {
  base::SetRecordActionTaskRunner(task_runner_);
  base::AddActionCallback(action_callback_);
}

UserActionTester::~UserActionTester() {
  base::RemoveActionCallback(action_callback_);
}

int UserActionTester::GetActionCount(std::string_view user_action) const {
  return times_map_.count(user_action);
}

std::vector<TimeTicks> UserActionTester::GetActionTimes(
    std::string_view user_action) const {
  std::vector<TimeTicks> result;
  auto range = times_map_.equal_range(user_action);
  for (auto it = range.first; it != range.second; it++) {
    result.push_back(it->second);
  }
  return result;
}

void UserActionTester::ResetCounts() {
  times_map_.clear();
}

void UserActionTester::OnUserAction(const std::string& user_action,
                                    TimeTicks action_time) {
  times_map_.insert({user_action, action_time});
}

}  // namespace base