Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / metrics_private / metrics_apitest.cc
blobf11d2dcb30b5a5ac8efb84a4eaeb496926047335
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.
5 #include <map>
7 #include "base/metrics/field_trial.h"
8 #include "base/metrics/histogram.h"
9 #include "base/metrics/statistics_recorder.h"
10 #include "base/test/user_action_tester.h"
11 #include "chrome/browser/extensions/extension_apitest.h"
12 #include "components/variations/variations_associated_data.h"
14 namespace {
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 {
20 const char* name;
21 int count; // number of times the metric was recorded.
22 } g_user_actions[] = {
23 {"test.ua.1", 1},
24 {"test.ua.2", 2},
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
29 // updated.
30 struct RecordedHistogram {
31 const char* name;
32 base::HistogramType type;
33 int min;
34 int max;
35 size_t buckets;
36 int count;
37 } g_histograms[] = {
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.
54 struct Bucket {
55 int histogram_value;
56 int count;
59 // We expect the following sparse histograms.
60 struct SparseHistogram {
61 const char* name;
62 int bucket_count;
63 Bucket buckets[10];
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 void ValidateUserActions(const base::UserActionTester& user_action_tester,
70 const RecordedUserAction* recorded,
71 int count) {
72 for (int i = 0; i < count; ++i) {
73 const RecordedUserAction& ua = recorded[i];
74 EXPECT_EQ(ua.count, user_action_tester.GetActionCount(ua.name));
78 void ValidateSparseHistogramSamples(
79 const std::string& name,
80 const base::HistogramSamples& samples) {
81 for (unsigned int i = 0; i < arraysize(g_sparse_histograms); ++i) {
82 const SparseHistogram& sparse_histogram = g_sparse_histograms[i];
83 if (std::string(name) == sparse_histogram.name) {
84 for (int j = 0; j < sparse_histogram.bucket_count; ++j) {
85 const Bucket& bucket = sparse_histogram.buckets[j];
86 EXPECT_EQ(bucket.count, samples.GetCount(bucket.histogram_value));
92 void ValidateHistograms(const RecordedHistogram* recorded,
93 int count) {
94 base::StatisticsRecorder::Histograms histograms;
95 base::StatisticsRecorder::GetHistograms(&histograms);
97 // Code other than the tests tun here will record some histogram values, but
98 // we will ignore those. This function validates that all the histogram we
99 // expect to see are present in the list, and that their basic info is
100 // correct.
101 for (int i = 0; i < count; ++i) {
102 const RecordedHistogram& r = recorded[i];
103 size_t j = 0;
104 for (j = 0; j < histograms.size(); ++j) {
105 base::HistogramBase* histogram(histograms[j]);
106 if (r.name == histogram->histogram_name()) {
107 scoped_ptr<base::HistogramSamples> snapshot =
108 histogram->SnapshotSamples();
109 base::HistogramBase::Count sample_count = snapshot->TotalCount();
110 EXPECT_EQ(r.count, sample_count);
112 EXPECT_EQ(r.type, histogram->GetHistogramType());
113 if (r.type == base::SPARSE_HISTOGRAM) {
114 ValidateSparseHistogramSamples(r.name, *snapshot);
115 } else {
116 EXPECT_TRUE(
117 histogram->HasConstructionArguments(r.min, r.max, r.buckets));
119 break;
122 EXPECT_LT(j, histograms.size());
126 } // anonymous namespace
128 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Metrics) {
129 base::UserActionTester user_action_tester;
131 base::FieldTrialList::CreateFieldTrial("apitestfieldtrial2", "group1");
133 std::map<std::string, std::string> params;
134 params["a"] = "aa";
135 params["b"] = "bb";
136 ASSERT_TRUE(variations::AssociateVariationParams(
137 "apitestfieldtrial2", "group1", params));
139 ASSERT_TRUE(RunComponentExtensionTest("metrics")) << message_;
141 ValidateUserActions(user_action_tester, g_user_actions,
142 arraysize(g_user_actions));
143 ValidateHistograms(g_histograms, arraysize(g_histograms));