Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(395)

Side by Side Diff: chrome/browser/extensions/extension_metrics_apitest.cc

Issue 8548013: Moving experimental.metrics API to metricsPrivate (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Rebase Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "chrome/browser/extensions/extension_apitest.h" 8 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "chrome/common/chrome_switches.h"
10 #include "chrome/common/extensions/extension.h"
11 #include "content/public/browser/notification_registrar.h" 9 #include "content/public/browser/notification_registrar.h"
12 #include "content/public/browser/notification_service.h" 10 #include "content/public/browser/notification_service.h"
13 11
14 namespace { 12 namespace {
15 13
16 // 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
17 // 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
18 // modified, this array may need to be updated. 16 // modified, this array may need to be updated.
19 struct RecordedUserAction { 17 struct RecordedUserAction {
20 const char* name; // base name of metric without extension id. 18 const char* name;
21 int count; // number of times the metric was recorded. 19 int count; // number of times the metric was recorded.
22 } g_user_actions[] = { 20 } g_user_actions[] = {
23 {"test.ua.1", 1}, 21 {"test.ua.1", 1},
24 {"test.ua.2", 2}, 22 {"test.ua.2", 2},
25 }; 23 };
26 24
27 // The tests that are run by this extension are expected to record the following 25 // The tests that are run by this extension are expected to record the following
28 // histograms. If the tests in test.js are modified, this array may need to be 26 // histograms. If the tests in test.js are modified, this array may need to be
29 // updated. 27 // updated.
30 struct RecordedHistogram { 28 struct RecordedHistogram {
31 const char* name; // base name of metric without extension id. 29 const char* name;
32 base::Histogram::ClassType type; 30 base::Histogram::ClassType type;
33 int min; 31 int min;
34 int max; 32 int max;
35 size_t buckets; 33 size_t buckets;
36 } g_histograms[] = { 34 } g_histograms[] = {
37 {"test.h.1", base::Histogram::HISTOGRAM, 1, 100, 50}, // custom 35 {"test.h.1", base::Histogram::HISTOGRAM, 1, 100, 50}, // custom
38 {"test.h.2", base::Histogram::LINEAR_HISTOGRAM, 1, 200, 50}, // custom 36 {"test.h.2", base::Histogram::LINEAR_HISTOGRAM, 1, 200, 50}, // custom
39 {"test.h.3", base::Histogram::LINEAR_HISTOGRAM, 1, 101, 102}, // percentage 37 {"test.h.3", base::Histogram::LINEAR_HISTOGRAM, 1, 101, 102}, // percentage
40 {"test.time", base::Histogram::HISTOGRAM, 1, 10000, 50}, 38 {"test.time", base::Histogram::HISTOGRAM, 1, 10000, 50},
41 {"test.medium.time", base::Histogram::HISTOGRAM, 1, 180000, 50}, 39 {"test.medium.time", base::Histogram::HISTOGRAM, 1, 180000, 50},
42 {"test.long.time", base::Histogram::HISTOGRAM, 1, 3600000, 50}, 40 {"test.long.time", base::Histogram::HISTOGRAM, 1, 3600000, 50},
43 {"test.count", base::Histogram::HISTOGRAM, 1, 1000000, 50}, 41 {"test.count", base::Histogram::HISTOGRAM, 1, 1000000, 50},
44 {"test.medium.count", base::Histogram::HISTOGRAM, 1, 10000, 50}, 42 {"test.medium.count", base::Histogram::HISTOGRAM, 1, 10000, 50},
45 {"test.small.count", base::Histogram::HISTOGRAM, 1, 100, 50}, 43 {"test.small.count", base::Histogram::HISTOGRAM, 1, 100, 50},
46 }; 44 };
47 45
48 // Build the full name of a metrics for the given extension. Each metric
49 // is made up of the unique name within the extension followed by the
50 // extension's id.
51 std::string BuildFullName(const std::string& name, const Extension* extension) {
52 std::string full_name(name);
53 full_name += extension->id();
54 return full_name;
55 }
56
57 // This class observes and collects user action notifications that are sent 46 // This class observes and collects user action notifications that are sent
58 // 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.
59 class UserActionObserver : public content::NotificationObserver { 48 class UserActionObserver : public content::NotificationObserver {
60 public: 49 public:
61 UserActionObserver(); 50 UserActionObserver();
62 51
63 void ValidateUserActions(const Extension* extension, 52 void ValidateUserActions(const RecordedUserAction* recorded, int count);
64 const RecordedUserAction* recorded,
65 int count);
66 53
67 virtual void Observe(int type, 54 virtual void Observe(int type,
68 const content::NotificationSource& source, 55 const content::NotificationSource& source,
69 const content::NotificationDetails& details); 56 const content::NotificationDetails& details);
70 57
71 private: 58 private:
72 typedef std::map<std::string, int> UserActionCountMap; 59 typedef std::map<std::string, int> UserActionCountMap;
73 60
74 int num_metrics() const { 61 int num_metrics() const {
75 return count_map_.size(); 62 return count_map_.size();
(...skipping 13 matching lines...) Expand all
89 content::NotificationService::AllSources()); 76 content::NotificationService::AllSources());
90 } 77 }
91 78
92 void UserActionObserver::Observe(int type, 79 void UserActionObserver::Observe(int type,
93 const content::NotificationSource& source, 80 const content::NotificationSource& source,
94 const content::NotificationDetails& details) { 81 const content::NotificationDetails& details) {
95 const char* name = *content::Details<const char*>(details).ptr(); 82 const char* name = *content::Details<const char*>(details).ptr();
96 ++(count_map_[name]); 83 ++(count_map_[name]);
97 } 84 }
98 85
99 void UserActionObserver::ValidateUserActions(const Extension* extension, 86 void UserActionObserver::ValidateUserActions(const RecordedUserAction* recorded,
100 const RecordedUserAction* recorded,
101 int count) { 87 int count) {
102 EXPECT_EQ(count, num_metrics()); 88 EXPECT_EQ(count, num_metrics());
103 89
104 for (int i = 0; i < count; ++i) { 90 for (int i = 0; i < count; ++i) {
105 const RecordedUserAction& ua = recorded[i]; 91 const RecordedUserAction& ua = recorded[i];
106 EXPECT_EQ(ua.count, GetMetricCount(BuildFullName(ua.name, extension))); 92 EXPECT_EQ(ua.count, GetMetricCount(ua.name));
107 } 93 }
108 } 94 }
109 95
110 void ValidateHistograms(const Extension* extension, 96 void ValidateHistograms(const RecordedHistogram* recorded,
111 const RecordedHistogram* recorded,
112 int count) { 97 int count) {
113 base::StatisticsRecorder::Histograms histograms; 98 base::StatisticsRecorder::Histograms histograms;
114 base::StatisticsRecorder::GetHistograms(&histograms); 99 base::StatisticsRecorder::GetHistograms(&histograms);
115 100
116 // Code other than the tests tun here will record some histogram values, but 101 // Code other than the tests tun here will record some histogram values, but
117 // we will ignore those. This function validates that all the histogram we 102 // we will ignore those. This function validates that all the histogram we
118 // expect to see are present in the list, and that their basic info is 103 // expect to see are present in the list, and that their basic info is
119 // correct. 104 // correct.
120 for (int i = 0; i < count; ++i) { 105 for (int i = 0; i < count; ++i) {
121 const RecordedHistogram& r = recorded[i]; 106 const RecordedHistogram& r = recorded[i];
122 std::string name(BuildFullName(r.name, extension));
123
124 size_t j = 0; 107 size_t j = 0;
125 for (j = 0; j < histograms.size(); ++j) { 108 for (j = 0; j < histograms.size(); ++j) {
126 base::Histogram* histogram(histograms[j]); 109 base::Histogram* histogram(histograms[j]);
127 110
128 if (name == histogram->histogram_name()) { 111 if (r.name == histogram->histogram_name()) {
129 EXPECT_EQ(r.type, histogram->histogram_type()); 112 EXPECT_EQ(r.type, histogram->histogram_type());
130 EXPECT_EQ(r.min, histogram->declared_min()); 113 EXPECT_EQ(r.min, histogram->declared_min());
131 EXPECT_EQ(r.max, histogram->declared_max()); 114 EXPECT_EQ(r.max, histogram->declared_max());
132 EXPECT_EQ(r.buckets, histogram->bucket_count()); 115 EXPECT_EQ(r.buckets, histogram->bucket_count());
133 break; 116 break;
134 } 117 }
135 } 118 }
136 EXPECT_LT(j, histograms.size()); 119 EXPECT_LT(j, histograms.size());
137 } 120 }
138 } 121 }
139 122
140 } // anonymous namespace 123 } // anonymous namespace
141 124
142 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Metrics) { 125 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Metrics) {
143 CommandLine::ForCurrentProcess()->AppendSwitch(
144 switches::kEnableExperimentalExtensionApis);
145
146 UserActionObserver observer; 126 UserActionObserver observer;
147 127
148 ASSERT_TRUE(RunExtensionTest("metrics")) << message_; 128 ASSERT_TRUE(RunComponentExtensionTest("metrics")) << message_;
149 const Extension* extension = GetSingleLoadedExtension();
150 ASSERT_TRUE(extension);
151 129
152 observer.ValidateUserActions(extension, 130 observer.ValidateUserActions(g_user_actions, arraysize(g_user_actions));
153 g_user_actions, 131 ValidateHistograms(g_histograms, arraysize(g_histograms));
154 arraysize(g_user_actions));
155 ValidateHistograms(extension, g_histograms, arraysize(g_histograms));
156 } 132 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_function_dispatcher.cc ('k') | chrome/browser/extensions/extension_metrics_module.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698