Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / translate / core / browser / translate_browser_metrics_unittest.cc
blob17c53c35e95094bf8a24064686ca0f029304d841
1 // Copyright 2014 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 "components/translate/core/browser/translate_browser_metrics.h"
7 #include "base/basictypes.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/metrics/histogram.h"
10 #include "base/metrics/histogram_samples.h"
11 #include "base/metrics/statistics_recorder.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "testing/platform_test.h"
15 using base::HistogramBase;
16 using base::HistogramSamples;
17 using base::StatisticsRecorder;
19 namespace {
21 class MetricsRecorder {
22 public:
23 explicit MetricsRecorder(const char* key) : key_(key) {
24 StatisticsRecorder::Initialize();
26 HistogramBase* histogram = StatisticsRecorder::FindHistogram(key_);
27 if (histogram)
28 base_samples_ = histogram->SnapshotSamples();
31 void CheckInitiationStatus(int expected_disabled_by_prefs,
32 int expected_disabled_by_switch,
33 int expected_disabled_by_config,
34 int expected_language_is_not_supported,
35 int expected_mime_type_is_not_supported,
36 int expected_url_is_not_supported,
37 int expected_similar_languages,
38 int expected_accept_languages,
39 int expected_auto_by_config,
40 int expected_auto_by_link,
41 int expected_show_infobar) {
42 Snapshot();
44 EXPECT_EQ(expected_disabled_by_prefs,
45 GetCountWithoutSnapshot(translate::TranslateBrowserMetrics::
46 INITIATION_STATUS_DISABLED_BY_PREFS));
47 EXPECT_EQ(
48 expected_disabled_by_switch,
49 GetCountWithoutSnapshot(translate::TranslateBrowserMetrics::
50 INITIATION_STATUS_DISABLED_BY_SWITCH));
51 EXPECT_EQ(
52 expected_disabled_by_config,
53 GetCountWithoutSnapshot(translate::TranslateBrowserMetrics::
54 INITIATION_STATUS_DISABLED_BY_CONFIG));
55 EXPECT_EQ(expected_language_is_not_supported,
56 GetCountWithoutSnapshot(
57 translate::TranslateBrowserMetrics::
58 INITIATION_STATUS_LANGUAGE_IS_NOT_SUPPORTED));
59 EXPECT_EQ(expected_mime_type_is_not_supported,
60 GetCountWithoutSnapshot(
61 translate::TranslateBrowserMetrics::
62 INITIATION_STATUS_MIME_TYPE_IS_NOT_SUPPORTED));
63 EXPECT_EQ(
64 expected_url_is_not_supported,
65 GetCountWithoutSnapshot(translate::TranslateBrowserMetrics::
66 INITIATION_STATUS_URL_IS_NOT_SUPPORTED));
67 EXPECT_EQ(expected_similar_languages,
68 GetCountWithoutSnapshot(translate::TranslateBrowserMetrics::
69 INITIATION_STATUS_SIMILAR_LANGUAGES));
70 EXPECT_EQ(expected_accept_languages,
71 GetCountWithoutSnapshot(translate::TranslateBrowserMetrics::
72 INITIATION_STATUS_ACCEPT_LANGUAGES));
73 EXPECT_EQ(expected_auto_by_config,
74 GetCountWithoutSnapshot(translate::TranslateBrowserMetrics::
75 INITIATION_STATUS_AUTO_BY_CONFIG));
76 EXPECT_EQ(expected_auto_by_link,
77 GetCountWithoutSnapshot(translate::TranslateBrowserMetrics::
78 INITIATION_STATUS_AUTO_BY_LINK));
79 EXPECT_EQ(expected_show_infobar,
80 GetCountWithoutSnapshot(translate::TranslateBrowserMetrics::
81 INITIATION_STATUS_SHOW_INFOBAR));
84 HistogramBase::Count GetTotalCount() {
85 Snapshot();
86 if (!samples_.get())
87 return 0;
88 HistogramBase::Count count = samples_->TotalCount();
89 if (!base_samples_.get())
90 return count;
91 return count - base_samples_->TotalCount();
94 HistogramBase::Count GetCount(HistogramBase::Sample value) {
95 Snapshot();
96 return GetCountWithoutSnapshot(value);
99 private:
100 void Snapshot() {
101 HistogramBase* histogram = StatisticsRecorder::FindHistogram(key_);
102 if (!histogram)
103 return;
104 samples_ = histogram->SnapshotSamples();
107 HistogramBase::Count GetCountWithoutSnapshot(HistogramBase::Sample value) {
108 if (!samples_.get())
109 return 0;
110 HistogramBase::Count count = samples_->GetCount(value);
111 if (!base_samples_.get())
112 return count;
113 return count - base_samples_->GetCount(value);
116 std::string key_;
117 scoped_ptr<HistogramSamples> base_samples_;
118 scoped_ptr<HistogramSamples> samples_;
120 DISALLOW_COPY_AND_ASSIGN(MetricsRecorder);
123 } // namespace
125 TEST(TranslateBrowserMetricsTest, ReportInitiationStatus) {
126 MetricsRecorder recorder(translate::TranslateBrowserMetrics::GetMetricsName(
127 translate::TranslateBrowserMetrics::UMA_INITIATION_STATUS));
129 recorder.CheckInitiationStatus(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
130 translate::TranslateBrowserMetrics::ReportInitiationStatus(
131 translate::TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_PREFS);
132 recorder.CheckInitiationStatus(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
133 translate::TranslateBrowserMetrics::ReportInitiationStatus(
134 translate::TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_SWITCH);
135 recorder.CheckInitiationStatus(1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
136 translate::TranslateBrowserMetrics::ReportInitiationStatus(
137 translate::TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_CONFIG);
138 recorder.CheckInitiationStatus(1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0);
139 translate::TranslateBrowserMetrics::ReportInitiationStatus(
140 translate::TranslateBrowserMetrics::
141 INITIATION_STATUS_LANGUAGE_IS_NOT_SUPPORTED);
142 recorder.CheckInitiationStatus(1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0);
143 translate::TranslateBrowserMetrics::ReportInitiationStatus(
144 translate::TranslateBrowserMetrics::
145 INITIATION_STATUS_MIME_TYPE_IS_NOT_SUPPORTED);
146 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0);
147 translate::TranslateBrowserMetrics::ReportInitiationStatus(
148 translate::TranslateBrowserMetrics::
149 INITIATION_STATUS_URL_IS_NOT_SUPPORTED);
150 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0);
151 translate::TranslateBrowserMetrics::ReportInitiationStatus(
152 translate::TranslateBrowserMetrics::INITIATION_STATUS_SIMILAR_LANGUAGES);
153 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0);
154 translate::TranslateBrowserMetrics::ReportInitiationStatus(
155 translate::TranslateBrowserMetrics::INITIATION_STATUS_ACCEPT_LANGUAGES);
156 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0);
157 translate::TranslateBrowserMetrics::ReportInitiationStatus(
158 translate::TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_CONFIG);
159 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0);
160 translate::TranslateBrowserMetrics::ReportInitiationStatus(
161 translate::TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_LINK);
162 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0);
163 translate::TranslateBrowserMetrics::ReportInitiationStatus(
164 translate::TranslateBrowserMetrics::INITIATION_STATUS_SHOW_INFOBAR);
165 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
168 TEST(TranslateBrowserMetricsTest, ReportLanguageDetectionError) {
169 MetricsRecorder recorder(translate::TranslateBrowserMetrics::GetMetricsName(
170 translate::TranslateBrowserMetrics::UMA_LANGUAGE_DETECTION_ERROR));
171 EXPECT_EQ(0, recorder.GetTotalCount());
172 translate::TranslateBrowserMetrics::ReportLanguageDetectionError();
173 EXPECT_EQ(1, recorder.GetTotalCount());
178 TEST(TranslateBrowserMetricsTest, ReportedLocalesOnDisabledByPrefs) {
179 const int ENGLISH = 25966;
181 MetricsRecorder recorder(translate::TranslateBrowserMetrics::GetMetricsName(
182 translate::TranslateBrowserMetrics::UMA_LOCALES_ON_DISABLED_BY_PREFS));
183 EXPECT_EQ(0, recorder.GetTotalCount());
184 translate::TranslateBrowserMetrics::ReportLocalesOnDisabledByPrefs("en");
185 EXPECT_EQ(1, recorder.GetCount(ENGLISH));
188 TEST(TranslateBrowserMetricsTest, ReportedUndisplayableLanguage) {
189 const int ENGLISH = 25966;
191 MetricsRecorder recorder(translate::TranslateBrowserMetrics::GetMetricsName(
192 translate::TranslateBrowserMetrics::UMA_UNDISPLAYABLE_LANGUAGE));
193 EXPECT_EQ(0, recorder.GetTotalCount());
194 translate::TranslateBrowserMetrics::ReportUndisplayableLanguage("en");
195 EXPECT_EQ(1, recorder.GetCount(ENGLISH));
198 TEST(TranslateBrowserMetricsTest, ReportedUnsupportedLanguageAtInitiation) {
199 const int ENGLISH = 25966;
201 MetricsRecorder recorder(translate::TranslateBrowserMetrics::GetMetricsName(
202 translate::TranslateBrowserMetrics::
203 UMA_UNSUPPORTED_LANGUAGE_AT_INITIATION));
204 EXPECT_EQ(0, recorder.GetTotalCount());
205 translate::TranslateBrowserMetrics::ReportUnsupportedLanguageAtInitiation(
206 "en");
207 EXPECT_EQ(1, recorder.GetCount(ENGLISH));