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

Side by Side Diff: chrome/browser/extensions/api/metrics_private/metrics_apitest.cc

Issue 12089062: Move API functions registrations out of ExtensionFunctionRegistry. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Created 7 years, 10 months 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <map>
6
7 #include "base/metrics/histogram.h"
8 #include "base/metrics/statistics_recorder.h"
9 #include "chrome/browser/extensions/extension_apitest.h"
10 #include "content/public/browser/user_metrics.h"
11
12 namespace {
13
14 // The tests that are run by this extension are expected to record the following
15 // user actions, with the specified counts. If the tests in test.js are
16 // modified, this array may need to be updated.
17 struct RecordedUserAction {
18 const char* name;
19 int count; // number of times the metric was recorded.
20 } g_user_actions[] = {
21 {"test.ua.1", 1},
22 {"test.ua.2", 2},
23 };
24
25 // The tests that are run by this extension are expected to record the following
26 // histograms. If the tests in test.js are modified, this array may need to be
27 // updated.
28 struct RecordedHistogram {
29 const char* name;
30 base::HistogramType type;
31 int min;
32 int max;
33 size_t buckets;
34 } g_histograms[] = {
35 {"test.h.1", base::HISTOGRAM, 1, 100, 50}, // custom
36 {"test.h.2", base::LINEAR_HISTOGRAM, 1, 200, 50}, // custom
37 {"test.h.3", base::LINEAR_HISTOGRAM, 1, 101, 102}, // percentage
38 {"test.time", base::HISTOGRAM, 1, 10000, 50},
39 {"test.medium.time", base::HISTOGRAM, 1, 180000, 50},
40 {"test.long.time", base::HISTOGRAM, 1, 3600000, 50},
41 {"test.count", base::HISTOGRAM, 1, 1000000, 50},
42 {"test.medium.count", base::HISTOGRAM, 1, 10000, 50},
43 {"test.small.count", base::HISTOGRAM, 1, 100, 50},
44 };
45
46 // This class observes and collects user action notifications that are sent
47 // by the tests, so that they can be examined afterwards for correctness.
48 class UserActionObserver {
49 public:
50 UserActionObserver();
51 ~UserActionObserver();
52
53 void ValidateUserActions(const RecordedUserAction* recorded, int count);
54
55 private:
56 typedef std::map<std::string, int> UserActionCountMap;
57
58 void OnUserAction(const std::string& action);
59
60 int num_metrics() const {
61 return count_map_.size();
62 }
63
64 int GetMetricCount(const std::string& name) const {
65 UserActionCountMap::const_iterator i = count_map_.find(name);
66 return i == count_map_.end() ? -1 : i->second;
67 }
68
69 UserActionCountMap count_map_;
70
71 content::ActionCallback action_callback_;
72
73 DISALLOW_COPY_AND_ASSIGN(UserActionObserver);
74 };
75
76 UserActionObserver::UserActionObserver()
77 : action_callback_(base::Bind(&UserActionObserver::OnUserAction,
78 base::Unretained(this))) {
79 content::AddActionCallback(action_callback_);
80 }
81
82 UserActionObserver::~UserActionObserver() {
83 content::RemoveActionCallback(action_callback_);
84 }
85
86 void UserActionObserver::OnUserAction(const std::string& action) {
87 ++(count_map_[action]);
88 }
89
90 void UserActionObserver::ValidateUserActions(const RecordedUserAction* recorded,
91 int count) {
92 EXPECT_EQ(count, num_metrics());
93
94 for (int i = 0; i < count; ++i) {
95 const RecordedUserAction& ua = recorded[i];
96 EXPECT_EQ(ua.count, GetMetricCount(ua.name));
97 }
98 }
99
100 void ValidateHistograms(const RecordedHistogram* recorded,
101 int count) {
102 base::StatisticsRecorder::Histograms histograms;
103 base::StatisticsRecorder::GetHistograms(&histograms);
104
105 // Code other than the tests tun here will record some histogram values, but
106 // we will ignore those. This function validates that all the histogram we
107 // expect to see are present in the list, and that their basic info is
108 // correct.
109 for (int i = 0; i < count; ++i) {
110 const RecordedHistogram& r = recorded[i];
111 size_t j = 0;
112 for (j = 0; j < histograms.size(); ++j) {
113 base::Histogram* histogram(histograms[j]);
114
115 if (r.name == histogram->histogram_name()) {
116 EXPECT_EQ(r.type, histogram->GetHistogramType());
117 EXPECT_EQ(r.min, histogram->declared_min());
118 EXPECT_EQ(r.max, histogram->declared_max());
119 EXPECT_EQ(r.buckets, histogram->bucket_count());
120 break;
121 }
122 }
123 EXPECT_LT(j, histograms.size());
124 }
125 }
126
127 } // anonymous namespace
128
129 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Metrics) {
130 UserActionObserver observer;
131
132 ASSERT_TRUE(RunComponentExtensionTest("metrics")) << message_;
133
134 observer.ValidateUserActions(g_user_actions, arraysize(g_user_actions));
135 ValidateHistograms(g_histograms, arraysize(g_histograms));
136 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698