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 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 11 #include "base/macros.h" 12 #include "base/metrics/user_metrics.h" 13 14 namespace base { 15 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 24 // Returns the number of times the given |user_action| occurred. 25 int GetActionCount(const std::string& user_action) const; 26 27 // Resets all user action counts to 0. 28 void ResetCounts(); 29 30 private: 31 typedef std::map<std::string, int> UserActionCountMap; 32 33 // The callback that is notified when a user actions occurs. 34 void OnUserAction(const std::string& user_action); 35 36 // A map that tracks the number of times a user action has occurred. 37 UserActionCountMap count_map_; 38 39 // A test task runner used by user metrics. 40 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 41 42 // The callback that is added to the global action callback list. 43 base::ActionCallback action_callback_; 44 45 DISALLOW_COPY_AND_ASSIGN(UserActionTester); 46 }; 47 48 } // namespace base 49 50 #endif // BASE_TEST_METRICS_USER_ACTION_TESTER_H_ 51