OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <map> | 5 #include <map> |
6 | 6 |
7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
8 #include "base/metrics/statistics_recorder.h" | 8 #include "base/metrics/statistics_recorder.h" |
9 #include "chrome/browser/extensions/extension_apitest.h" | 9 #include "chrome/browser/extensions/extension_apitest.h" |
10 #include "content/public/browser/notification_registrar.h" | 10 #include "content/public/browser/user_metrics.h" |
11 #include "content/public/browser/notification_service.h" | |
12 | 11 |
13 namespace { | 12 namespace { |
14 | 13 |
15 // The tests that are run by this extension are expected to record the following | 14 // The tests that are run by this extension are expected to record the following |
16 // user actions, with the specified counts. If the tests in test.js are | 15 // user actions, with the specified counts. If the tests in test.js are |
17 // modified, this array may need to be updated. | 16 // modified, this array may need to be updated. |
18 struct RecordedUserAction { | 17 struct RecordedUserAction { |
19 const char* name; | 18 const char* name; |
20 int count; // number of times the metric was recorded. | 19 int count; // number of times the metric was recorded. |
21 } g_user_actions[] = { | 20 } g_user_actions[] = { |
(...skipping 17 matching lines...) Expand all Loading... |
39 {"test.time", base::HISTOGRAM, 1, 10000, 50}, | 38 {"test.time", base::HISTOGRAM, 1, 10000, 50}, |
40 {"test.medium.time", base::HISTOGRAM, 1, 180000, 50}, | 39 {"test.medium.time", base::HISTOGRAM, 1, 180000, 50}, |
41 {"test.long.time", base::HISTOGRAM, 1, 3600000, 50}, | 40 {"test.long.time", base::HISTOGRAM, 1, 3600000, 50}, |
42 {"test.count", base::HISTOGRAM, 1, 1000000, 50}, | 41 {"test.count", base::HISTOGRAM, 1, 1000000, 50}, |
43 {"test.medium.count", base::HISTOGRAM, 1, 10000, 50}, | 42 {"test.medium.count", base::HISTOGRAM, 1, 10000, 50}, |
44 {"test.small.count", base::HISTOGRAM, 1, 100, 50}, | 43 {"test.small.count", base::HISTOGRAM, 1, 100, 50}, |
45 }; | 44 }; |
46 | 45 |
47 // This class observes and collects user action notifications that are sent | 46 // This class observes and collects user action notifications that are sent |
48 // by the tests, so that they can be examined afterwards for correctness. | 47 // by the tests, so that they can be examined afterwards for correctness. |
49 class UserActionObserver : public content::NotificationObserver { | 48 class UserActionObserver { |
50 public: | 49 public: |
51 UserActionObserver(); | 50 UserActionObserver(); |
| 51 ~UserActionObserver(); |
52 | 52 |
53 void ValidateUserActions(const RecordedUserAction* recorded, int count); | 53 void ValidateUserActions(const RecordedUserAction* recorded, int count); |
54 | 54 |
55 virtual void Observe(int type, | |
56 const content::NotificationSource& source, | |
57 const content::NotificationDetails& details); | |
58 | |
59 private: | 55 private: |
60 typedef std::map<std::string, int> UserActionCountMap; | 56 typedef std::map<std::string, int> UserActionCountMap; |
61 | 57 |
| 58 void OnUserAction(const std::string& action); |
| 59 |
62 int num_metrics() const { | 60 int num_metrics() const { |
63 return count_map_.size(); | 61 return count_map_.size(); |
64 } | 62 } |
65 | 63 |
66 int GetMetricCount(const std::string& name) const { | 64 int GetMetricCount(const std::string& name) const { |
67 UserActionCountMap::const_iterator i = count_map_.find(name); | 65 UserActionCountMap::const_iterator i = count_map_.find(name); |
68 return i == count_map_.end() ? -1 : i->second; | 66 return i == count_map_.end() ? -1 : i->second; |
69 } | 67 } |
70 | 68 |
71 content::NotificationRegistrar registrar_; | |
72 UserActionCountMap count_map_; | 69 UserActionCountMap count_map_; |
73 }; | 70 }; |
74 | 71 |
75 UserActionObserver::UserActionObserver() { | 72 UserActionObserver::UserActionObserver() { |
76 registrar_.Add(this, content::NOTIFICATION_USER_ACTION, | 73 content::AddActionCallback(base::Bind(&UserActionObserver::OnUserAction, |
77 content::NotificationService::AllSources()); | 74 base::Unretained(this))); |
78 } | 75 } |
79 | 76 |
80 void UserActionObserver::Observe(int type, | 77 UserActionObserver::~UserActionObserver() { |
81 const content::NotificationSource& source, | 78 content::RemoveActionCallback(base::Bind(&UserActionObserver::OnUserAction, |
82 const content::NotificationDetails& details) { | 79 base::Unretained(this))); |
83 const char* name = *content::Details<const char*>(details).ptr(); | 80 } |
84 ++(count_map_[name]); | 81 |
| 82 void UserActionObserver::OnUserAction(const std::string& action) { |
| 83 ++(count_map_[action]); |
85 } | 84 } |
86 | 85 |
87 void UserActionObserver::ValidateUserActions(const RecordedUserAction* recorded, | 86 void UserActionObserver::ValidateUserActions(const RecordedUserAction* recorded, |
88 int count) { | 87 int count) { |
89 EXPECT_EQ(count, num_metrics()); | 88 EXPECT_EQ(count, num_metrics()); |
90 | 89 |
91 for (int i = 0; i < count; ++i) { | 90 for (int i = 0; i < count; ++i) { |
92 const RecordedUserAction& ua = recorded[i]; | 91 const RecordedUserAction& ua = recorded[i]; |
93 EXPECT_EQ(ua.count, GetMetricCount(ua.name)); | 92 EXPECT_EQ(ua.count, GetMetricCount(ua.name)); |
94 } | 93 } |
(...skipping 29 matching lines...) Expand all Loading... |
124 } // anonymous namespace | 123 } // anonymous namespace |
125 | 124 |
126 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Metrics) { | 125 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Metrics) { |
127 UserActionObserver observer; | 126 UserActionObserver observer; |
128 | 127 |
129 ASSERT_TRUE(RunComponentExtensionTest("metrics")) << message_; | 128 ASSERT_TRUE(RunComponentExtensionTest("metrics")) << message_; |
130 | 129 |
131 observer.ValidateUserActions(g_user_actions, arraysize(g_user_actions)); | 130 observer.ValidateUserActions(g_user_actions, arraysize(g_user_actions)); |
132 ValidateHistograms(g_histograms, arraysize(g_histograms)); | 131 ValidateHistograms(g_histograms, arraysize(g_histograms)); |
133 } | 132 } |
OLD | NEW |