Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / components / translate / core / browser / translate_browser_metrics_unittest.cc
blobc9a24c2b052dc566eda1704f3fae17040dc139f9
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_disabled_by_build,
35 int expected_language_is_not_supported,
36 int expected_mime_type_is_not_supported,
37 int expected_url_is_not_supported,
38 int expected_similar_languages,
39 int expected_accept_languages,
40 int expected_auto_by_config,
41 int expected_auto_by_link,
42 int expected_show_infobar) {
43 Snapshot();
45 EXPECT_EQ(expected_disabled_by_prefs,
46 GetCountWithoutSnapshot(translate::TranslateBrowserMetrics::
47 INITIATION_STATUS_DISABLED_BY_PREFS));
48 EXPECT_EQ(
49 expected_disabled_by_switch,
50 GetCountWithoutSnapshot(translate::TranslateBrowserMetrics::
51 INITIATION_STATUS_DISABLED_BY_SWITCH));
52 EXPECT_EQ(
53 expected_disabled_by_config,
54 GetCountWithoutSnapshot(translate::TranslateBrowserMetrics::
55 INITIATION_STATUS_DISABLED_BY_CONFIG));
56 EXPECT_EQ(
57 expected_disabled_by_build,
58 GetCountWithoutSnapshot(translate::TranslateBrowserMetrics::
59 INITIATION_STATUS_DISABLED_BY_KEY));
60 EXPECT_EQ(expected_language_is_not_supported,
61 GetCountWithoutSnapshot(
62 translate::TranslateBrowserMetrics::
63 INITIATION_STATUS_LANGUAGE_IS_NOT_SUPPORTED));
64 EXPECT_EQ(expected_mime_type_is_not_supported,
65 GetCountWithoutSnapshot(
66 translate::TranslateBrowserMetrics::
67 INITIATION_STATUS_MIME_TYPE_IS_NOT_SUPPORTED));
68 EXPECT_EQ(
69 expected_url_is_not_supported,
70 GetCountWithoutSnapshot(translate::TranslateBrowserMetrics::
71 INITIATION_STATUS_URL_IS_NOT_SUPPORTED));
72 EXPECT_EQ(expected_similar_languages,
73 GetCountWithoutSnapshot(translate::TranslateBrowserMetrics::
74 INITIATION_STATUS_SIMILAR_LANGUAGES));
75 EXPECT_EQ(expected_accept_languages,
76 GetCountWithoutSnapshot(translate::TranslateBrowserMetrics::
77 INITIATION_STATUS_ACCEPT_LANGUAGES));
78 EXPECT_EQ(expected_auto_by_config,
79 GetCountWithoutSnapshot(translate::TranslateBrowserMetrics::
80 INITIATION_STATUS_AUTO_BY_CONFIG));
81 EXPECT_EQ(expected_auto_by_link,
82 GetCountWithoutSnapshot(translate::TranslateBrowserMetrics::
83 INITIATION_STATUS_AUTO_BY_LINK));
84 EXPECT_EQ(expected_show_infobar,
85 GetCountWithoutSnapshot(translate::TranslateBrowserMetrics::
86 INITIATION_STATUS_SHOW_INFOBAR));
89 HistogramBase::Count GetTotalCount() {
90 Snapshot();
91 if (!samples_.get())
92 return 0;
93 HistogramBase::Count count = samples_->TotalCount();
94 if (!base_samples_.get())
95 return count;
96 return count - base_samples_->TotalCount();
99 HistogramBase::Count GetCount(HistogramBase::Sample value) {
100 Snapshot();
101 return GetCountWithoutSnapshot(value);
104 private:
105 void Snapshot() {
106 HistogramBase* histogram = StatisticsRecorder::FindHistogram(key_);
107 if (!histogram)
108 return;
109 samples_ = histogram->SnapshotSamples();
112 HistogramBase::Count GetCountWithoutSnapshot(HistogramBase::Sample value) {
113 if (!samples_.get())
114 return 0;
115 HistogramBase::Count count = samples_->GetCount(value);
116 if (!base_samples_.get())
117 return count;
118 return count - base_samples_->GetCount(value);
121 std::string key_;
122 scoped_ptr<HistogramSamples> base_samples_;
123 scoped_ptr<HistogramSamples> samples_;
125 DISALLOW_COPY_AND_ASSIGN(MetricsRecorder);
128 } // namespace
130 TEST(TranslateBrowserMetricsTest, ReportInitiationStatus) {
131 MetricsRecorder recorder(translate::TranslateBrowserMetrics::GetMetricsName(
132 translate::TranslateBrowserMetrics::UMA_INITIATION_STATUS));
134 recorder.CheckInitiationStatus(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
135 translate::TranslateBrowserMetrics::ReportInitiationStatus(
136 translate::TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_PREFS);
137 recorder.CheckInitiationStatus(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
138 translate::TranslateBrowserMetrics::ReportInitiationStatus(
139 translate::TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_SWITCH);
140 recorder.CheckInitiationStatus(1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
141 translate::TranslateBrowserMetrics::ReportInitiationStatus(
142 translate::TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_CONFIG);
143 recorder.CheckInitiationStatus(1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
144 translate::TranslateBrowserMetrics::ReportInitiationStatus(
145 translate::TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_KEY);
146 recorder.CheckInitiationStatus(1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0);
147 translate::TranslateBrowserMetrics::ReportInitiationStatus(
148 translate::TranslateBrowserMetrics::
149 INITIATION_STATUS_LANGUAGE_IS_NOT_SUPPORTED);
150 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0);
151 translate::TranslateBrowserMetrics::ReportInitiationStatus(
152 translate::TranslateBrowserMetrics::
153 INITIATION_STATUS_MIME_TYPE_IS_NOT_SUPPORTED);
154 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0);
155 translate::TranslateBrowserMetrics::ReportInitiationStatus(
156 translate::TranslateBrowserMetrics::
157 INITIATION_STATUS_URL_IS_NOT_SUPPORTED);
158 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0);
159 translate::TranslateBrowserMetrics::ReportInitiationStatus(
160 translate::TranslateBrowserMetrics::INITIATION_STATUS_SIMILAR_LANGUAGES);
161 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0);
162 translate::TranslateBrowserMetrics::ReportInitiationStatus(
163 translate::TranslateBrowserMetrics::INITIATION_STATUS_ACCEPT_LANGUAGES);
164 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0);
165 translate::TranslateBrowserMetrics::ReportInitiationStatus(
166 translate::TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_CONFIG);
167 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0);
168 translate::TranslateBrowserMetrics::ReportInitiationStatus(
169 translate::TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_LINK);
170 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0);
171 translate::TranslateBrowserMetrics::ReportInitiationStatus(
172 translate::TranslateBrowserMetrics::INITIATION_STATUS_SHOW_INFOBAR);
173 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
176 TEST(TranslateBrowserMetricsTest, ReportLanguageDetectionError) {
177 MetricsRecorder recorder(translate::TranslateBrowserMetrics::GetMetricsName(
178 translate::TranslateBrowserMetrics::UMA_LANGUAGE_DETECTION_ERROR));
179 EXPECT_EQ(0, recorder.GetTotalCount());
180 translate::TranslateBrowserMetrics::ReportLanguageDetectionError();
181 EXPECT_EQ(1, recorder.GetTotalCount());
186 TEST(TranslateBrowserMetricsTest, ReportedLocalesOnDisabledByPrefs) {
187 const int ENGLISH = 25966;
189 MetricsRecorder recorder(translate::TranslateBrowserMetrics::GetMetricsName(
190 translate::TranslateBrowserMetrics::UMA_LOCALES_ON_DISABLED_BY_PREFS));
191 EXPECT_EQ(0, recorder.GetTotalCount());
192 translate::TranslateBrowserMetrics::ReportLocalesOnDisabledByPrefs("en");
193 EXPECT_EQ(1, recorder.GetCount(ENGLISH));
196 TEST(TranslateBrowserMetricsTest, ReportedUndisplayableLanguage) {
197 const int ENGLISH = 25966;
199 MetricsRecorder recorder(translate::TranslateBrowserMetrics::GetMetricsName(
200 translate::TranslateBrowserMetrics::UMA_UNDISPLAYABLE_LANGUAGE));
201 EXPECT_EQ(0, recorder.GetTotalCount());
202 translate::TranslateBrowserMetrics::ReportUndisplayableLanguage("en");
203 EXPECT_EQ(1, recorder.GetCount(ENGLISH));
206 TEST(TranslateBrowserMetricsTest, ReportedUnsupportedLanguageAtInitiation) {
207 const int ENGLISH = 25966;
209 MetricsRecorder recorder(translate::TranslateBrowserMetrics::GetMetricsName(
210 translate::TranslateBrowserMetrics::
211 UMA_UNSUPPORTED_LANGUAGE_AT_INITIATION));
212 EXPECT_EQ(0, recorder.GetTotalCount());
213 translate::TranslateBrowserMetrics::ReportUnsupportedLanguageAtInitiation(
214 "en");
215 EXPECT_EQ(1, recorder.GetCount(ENGLISH));