user_action_tester.h 1.8 KB

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