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.
7 #include "base/metrics/field_trial.h"
8 #include "base/metrics/histogram.h"
9 #include "base/metrics/statistics_recorder.h"
10 #include "base/metrics/user_metrics.h"
11 #include "chrome/browser/extensions/extension_apitest.h"
12 #include "components/variations/variations_associated_data.h"
16 // 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
18 // modified, this array may need to be updated.
19 struct RecordedUserAction
{
21 int count
; // number of times the metric was recorded.
22 } g_user_actions
[] = {
27 // 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
30 struct RecordedHistogram
{
32 base::HistogramType type
;
38 {"test.h.1", base::HISTOGRAM
, 1, 100, 50, 1}, // custom
39 {"test.h.2", base::LINEAR_HISTOGRAM
, 1, 200, 50, 1}, // custom
40 {"test.h.3", base::LINEAR_HISTOGRAM
, 1, 101, 102, 2}, // percentage
41 {"test.sparse.1", base::SPARSE_HISTOGRAM
, 0, 0, 0, 1},
42 {"test.sparse.2", base::SPARSE_HISTOGRAM
, 0, 0, 0, 2},
43 {"test.sparse.3", base::SPARSE_HISTOGRAM
, 0, 0, 0, 6},
44 {"test.time", base::HISTOGRAM
, 1, 10000, 50, 1},
45 {"test.medium.time", base::HISTOGRAM
, 1, 180000, 50, 1},
46 {"test.long.time", base::HISTOGRAM
, 1, 3600000, 50, 1},
47 {"test.count", base::HISTOGRAM
, 1, 1000000, 50, 1},
48 {"test.medium.count", base::HISTOGRAM
, 1, 10000, 50, 1},
49 {"test.small.count", base::HISTOGRAM
, 1, 100, 50, 1},
50 {"test.bucketchange.linear", base::LINEAR_HISTOGRAM
, 1, 100, 10, 2},
51 {"test.bucketchange.log", base::HISTOGRAM
, 1, 100, 10, 2}, };
53 // Represents a bucket in a sparse histogram.
59 // We expect the following sparse histograms.
60 struct SparseHistogram
{
64 } g_sparse_histograms
[] = {
65 {"test.sparse.1", 1, {{42, 1}}},
66 {"test.sparse.2", 1, {{24, 2}}},
67 {"test.sparse.3", 3, {{1, 1}, {2, 2}, {3, 3}}}};
69 // This class observes and collects user action notifications that are sent
70 // by the tests, so that they can be examined afterwards for correctness.
71 class UserActionObserver
{
74 ~UserActionObserver();
76 void ValidateUserActions(const RecordedUserAction
* recorded
, int count
);
79 typedef std::map
<std::string
, int> UserActionCountMap
;
81 void OnUserAction(const std::string
& action
);
83 int num_metrics() const {
84 return count_map_
.size();
87 int GetMetricCount(const std::string
& name
) const {
88 UserActionCountMap::const_iterator i
= count_map_
.find(name
);
89 return i
== count_map_
.end() ? -1 : i
->second
;
92 UserActionCountMap count_map_
;
94 base::ActionCallback action_callback_
;
96 DISALLOW_COPY_AND_ASSIGN(UserActionObserver
);
99 UserActionObserver::UserActionObserver()
100 : action_callback_(base::Bind(&UserActionObserver::OnUserAction
,
101 base::Unretained(this))) {
102 base::AddActionCallback(action_callback_
);
105 UserActionObserver::~UserActionObserver() {
106 base::RemoveActionCallback(action_callback_
);
109 void UserActionObserver::OnUserAction(const std::string
& action
) {
110 ++(count_map_
[action
]);
113 void UserActionObserver::ValidateUserActions(const RecordedUserAction
* recorded
,
115 for (int i
= 0; i
< count
; ++i
) {
116 const RecordedUserAction
& ua
= recorded
[i
];
117 EXPECT_EQ(ua
.count
, GetMetricCount(ua
.name
));
121 void ValidateSparseHistogramSamples(
122 const std::string
& name
,
123 const base::HistogramSamples
& samples
) {
124 for (unsigned int i
= 0; i
< arraysize(g_sparse_histograms
); ++i
) {
125 const SparseHistogram
& sparse_histogram
= g_sparse_histograms
[i
];
126 if (std::string(name
) == sparse_histogram
.name
) {
127 for (int j
= 0; j
< sparse_histogram
.bucket_count
; ++j
) {
128 const Bucket
& bucket
= sparse_histogram
.buckets
[j
];
129 EXPECT_EQ(bucket
.count
, samples
.GetCount(bucket
.histogram_value
));
135 void ValidateHistograms(const RecordedHistogram
* recorded
,
137 base::StatisticsRecorder::Histograms histograms
;
138 base::StatisticsRecorder::GetHistograms(&histograms
);
140 // Code other than the tests tun here will record some histogram values, but
141 // we will ignore those. This function validates that all the histogram we
142 // expect to see are present in the list, and that their basic info is
144 for (int i
= 0; i
< count
; ++i
) {
145 const RecordedHistogram
& r
= recorded
[i
];
147 for (j
= 0; j
< histograms
.size(); ++j
) {
148 base::HistogramBase
* histogram(histograms
[j
]);
149 if (r
.name
== histogram
->histogram_name()) {
150 scoped_ptr
<base::HistogramSamples
> snapshot
=
151 histogram
->SnapshotSamples();
152 base::HistogramBase::Count sample_count
= snapshot
->TotalCount();
153 EXPECT_EQ(r
.count
, sample_count
);
155 EXPECT_EQ(r
.type
, histogram
->GetHistogramType());
156 if (r
.type
== base::SPARSE_HISTOGRAM
) {
157 ValidateSparseHistogramSamples(r
.name
, *snapshot
);
160 histogram
->HasConstructionArguments(r
.min
, r
.max
, r
.buckets
));
165 EXPECT_LT(j
, histograms
.size());
169 } // anonymous namespace
171 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, Metrics
) {
172 UserActionObserver observer
;
174 base::FieldTrialList::CreateFieldTrial("apitestfieldtrial2", "group1");
176 std::map
<std::string
, std::string
> params
;
179 ASSERT_TRUE(chrome_variations::AssociateVariationParams(
180 "apitestfieldtrial2", "group1", params
));
182 ASSERT_TRUE(RunComponentExtensionTest("metrics")) << message_
;
184 observer
.ValidateUserActions(g_user_actions
, arraysize(g_user_actions
));
185 ValidateHistograms(g_histograms
, arraysize(g_histograms
));