xref: /aosp_15_r20/external/cronet/base/test/metrics/user_action_tester.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_TEST_METRICS_USER_ACTION_TESTER_H_
6 #define BASE_TEST_METRICS_USER_ACTION_TESTER_H_
7 
8 #include <map>
9 #include <string>
10 #include <string_view>
11 #include <vector>
12 
13 #include "base/memory/scoped_refptr.h"
14 #include "base/metrics/user_metrics.h"
15 #include "base/task/single_thread_task_runner.h"
16 #include "base/time/time.h"
17 
18 namespace base {
19 
20 class TimeTicks;
21 
22 // This class observes and collects user action notifications that are sent
23 // by the tests, so that they can be examined afterwards for correctness.
24 // Note: This class is NOT thread-safe.
25 class UserActionTester {
26  public:
27   UserActionTester();
28 
29   UserActionTester(const UserActionTester&) = delete;
30   UserActionTester& operator=(const UserActionTester&) = delete;
31 
32   ~UserActionTester();
33 
34   // Returns the number of times the given |user_action| occurred.
35   int GetActionCount(std::string_view user_action) const;
36 
37   // Returns the time values at which the given |user_action| has occurred.
38   // The order of returned values is unspecified.
39   std::vector<TimeTicks> GetActionTimes(std::string_view user_action) const;
40 
41   // Resets all user action counts to 0.
42   void ResetCounts();
43 
44  private:
45   typedef std::multimap<std::string, TimeTicks, std::less<>> UserActionTimesMap;
46 
47   // The callback that is notified when a user actions occurs.
48   void OnUserAction(const std::string& user_action, TimeTicks action_time);
49 
50   // A map that tracks the times when a user action has occurred.
51   UserActionTimesMap times_map_;
52 
53   // A test task runner used by user metrics.
54   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
55 
56   // The callback that is added to the global action callback list.
57   base::ActionCallback action_callback_;
58 };
59 
60 }  // namespace base
61 
62 #endif  // BASE_TEST_METRICS_USER_ACTION_TESTER_H_
63