OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 "chrome/browser/translate/translate_manager_metrics.h" | |
6 | |
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" | |
14 | |
15 using base::HistogramBase; | |
16 using base::HistogramSamples; | |
17 using base::StatisticsRecorder; | |
18 | |
19 namespace { | |
20 | |
21 class MetricsRecorder { | |
22 public: | |
23 explicit MetricsRecorder(const char* key) : key_(key) { | |
24 StatisticsRecorder::Initialize(); | |
25 | |
26 HistogramBase* histogram = StatisticsRecorder::FindHistogram(key_); | |
27 if (histogram) | |
28 base_samples_ = histogram->SnapshotSamples(); | |
29 } | |
30 | |
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_url_is_not_supported, | |
36 int expected_similar_languages, | |
37 int expected_accept_languages, | |
38 int expected_auto_by_config, | |
39 int expected_auto_by_link, | |
40 int expected_show_infobar) { | |
41 Snapshot(); | |
42 | |
43 EXPECT_EQ(expected_disabled_by_prefs, GetCountWithoutSnapshot( | |
44 TranslateManagerMetrics::INITIATION_STATUS_DISABLED_BY_PREFS)); | |
45 EXPECT_EQ(expected_disabled_by_switch, GetCountWithoutSnapshot( | |
46 TranslateManagerMetrics::INITIATION_STATUS_DISABLED_BY_SWITCH)); | |
47 EXPECT_EQ(expected_disabled_by_config, GetCountWithoutSnapshot( | |
48 TranslateManagerMetrics::INITIATION_STATUS_DISABLED_BY_CONFIG)); | |
49 EXPECT_EQ(expected_language_is_not_supported, GetCountWithoutSnapshot( | |
50 TranslateManagerMetrics::INITIATION_STATUS_LANGUAGE_IS_NOT_SUPPORTED)); | |
51 EXPECT_EQ(expected_url_is_not_supported, GetCountWithoutSnapshot( | |
52 TranslateManagerMetrics::INITIATION_STATUS_URL_IS_NOT_SUPPORTED)); | |
53 EXPECT_EQ(expected_similar_languages, GetCountWithoutSnapshot( | |
54 TranslateManagerMetrics::INITIATION_STATUS_SIMILAR_LANGUAGES)); | |
55 EXPECT_EQ(expected_accept_languages, GetCountWithoutSnapshot( | |
56 TranslateManagerMetrics::INITIATION_STATUS_ACCEPT_LANGUAGES)); | |
57 EXPECT_EQ(expected_auto_by_config, GetCountWithoutSnapshot( | |
58 TranslateManagerMetrics::INITIATION_STATUS_AUTO_BY_CONFIG)); | |
59 EXPECT_EQ(expected_auto_by_link, GetCountWithoutSnapshot( | |
60 TranslateManagerMetrics::INITIATION_STATUS_AUTO_BY_LINK)); | |
61 EXPECT_EQ(expected_show_infobar, GetCountWithoutSnapshot( | |
62 TranslateManagerMetrics::INITIATION_STATUS_SHOW_INFOBAR)); | |
63 } | |
64 | |
65 HistogramBase::Count GetTotalCount() { | |
66 Snapshot(); | |
67 if (!samples_.get()) | |
68 return 0; | |
69 HistogramBase::Count count = samples_->TotalCount(); | |
70 if (!base_samples_.get()) | |
71 return count; | |
72 return count - base_samples_->TotalCount(); | |
73 } | |
74 | |
75 HistogramBase::Count GetCount(HistogramBase::Sample value) { | |
76 Snapshot(); | |
77 return GetCountWithoutSnapshot(value); | |
78 } | |
79 | |
80 private: | |
81 void Snapshot() { | |
82 HistogramBase* histogram = StatisticsRecorder::FindHistogram(key_); | |
83 if (!histogram) | |
84 return; | |
85 samples_ = histogram->SnapshotSamples(); | |
86 } | |
87 | |
88 HistogramBase::Count GetCountWithoutSnapshot(HistogramBase::Sample value) { | |
89 if (!samples_.get()) | |
90 return 0; | |
91 HistogramBase::Count count = samples_->GetCount(value); | |
92 if (!base_samples_.get()) | |
93 return count; | |
94 return count - base_samples_->GetCount(value); | |
95 } | |
96 | |
97 std::string key_; | |
98 scoped_ptr<HistogramSamples> base_samples_; | |
99 scoped_ptr<HistogramSamples> samples_; | |
100 | |
101 DISALLOW_COPY_AND_ASSIGN(MetricsRecorder); | |
102 }; | |
103 | |
104 } // namespace | |
105 | |
106 TEST(TranslateManagerMetricsTest, ReportInitiationStatus) { | |
107 MetricsRecorder recorder(TranslateManagerMetrics::GetMetricsName( | |
108 TranslateManagerMetrics::UMA_INITIATION_STATUS)); | |
109 | |
110 recorder.CheckInitiationStatus(0, 0, 0, 0, 0, 0, 0, 0, 0, 0); | |
111 TranslateManagerMetrics::ReportInitiationStatus( | |
112 TranslateManagerMetrics::INITIATION_STATUS_DISABLED_BY_PREFS); | |
113 recorder.CheckInitiationStatus(1, 0, 0, 0, 0, 0, 0, 0, 0, 0); | |
114 TranslateManagerMetrics::ReportInitiationStatus( | |
115 TranslateManagerMetrics::INITIATION_STATUS_DISABLED_BY_SWITCH); | |
116 recorder.CheckInitiationStatus(1, 1, 0, 0, 0, 0, 0, 0, 0, 0); | |
117 TranslateManagerMetrics::ReportInitiationStatus( | |
118 TranslateManagerMetrics::INITIATION_STATUS_DISABLED_BY_CONFIG); | |
119 recorder.CheckInitiationStatus(1, 1, 1, 0, 0, 0, 0, 0, 0, 0); | |
120 TranslateManagerMetrics::ReportInitiationStatus( | |
121 TranslateManagerMetrics::INITIATION_STATUS_LANGUAGE_IS_NOT_SUPPORTED); | |
122 recorder.CheckInitiationStatus(1, 1, 1, 1, 0, 0, 0, 0, 0, 0); | |
123 TranslateManagerMetrics::ReportInitiationStatus( | |
124 TranslateManagerMetrics::INITIATION_STATUS_URL_IS_NOT_SUPPORTED); | |
125 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 0, 0, 0, 0, 0); | |
126 TranslateManagerMetrics::ReportInitiationStatus( | |
127 TranslateManagerMetrics::INITIATION_STATUS_SIMILAR_LANGUAGES); | |
128 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 0, 0, 0, 0); | |
129 TranslateManagerMetrics::ReportInitiationStatus( | |
130 TranslateManagerMetrics::INITIATION_STATUS_ACCEPT_LANGUAGES); | |
131 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 1, 0, 0, 0); | |
132 TranslateManagerMetrics::ReportInitiationStatus( | |
133 TranslateManagerMetrics::INITIATION_STATUS_AUTO_BY_CONFIG); | |
134 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 1, 1, 0, 0); | |
135 TranslateManagerMetrics::ReportInitiationStatus( | |
136 TranslateManagerMetrics::INITIATION_STATUS_AUTO_BY_LINK); | |
137 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 1, 1, 1, 0); | |
138 TranslateManagerMetrics::ReportInitiationStatus( | |
139 TranslateManagerMetrics::INITIATION_STATUS_SHOW_INFOBAR); | |
140 recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 1, 1, 1, 1); | |
141 } | |
142 | |
143 TEST(TranslateManagerMetricsTest, ReportLanguageDetectionError) { | |
144 MetricsRecorder recorder(TranslateManagerMetrics::GetMetricsName( | |
145 TranslateManagerMetrics::UMA_LANGUAGE_DETECTION_ERROR)); | |
146 EXPECT_EQ(0, recorder.GetTotalCount()); | |
147 TranslateManagerMetrics::ReportLanguageDetectionError(); | |
148 EXPECT_EQ(1, recorder.GetTotalCount()); | |
149 | |
150 } | |
151 | |
152 TEST(TranslateManagerMetricsTest, ReportedUnsupportedLanguage) { | |
153 MetricsRecorder recorder(TranslateManagerMetrics::GetMetricsName( | |
154 TranslateManagerMetrics::UMA_SERVER_REPORTED_UNSUPPORTED_LANGUAGE)); | |
155 EXPECT_EQ(0, recorder.GetTotalCount()); | |
156 TranslateManagerMetrics::ReportUnsupportedLanguage(); | |
157 EXPECT_EQ(1, recorder.GetTotalCount()); | |
158 } | |
159 | |
160 TEST(TranslateManagerMetricsTest, ReportedUnsupportedLanguageAtInitiation) { | |
161 const int ENGLISH = 25966; | |
162 | |
163 MetricsRecorder recorder(TranslateManagerMetrics::GetMetricsName( | |
164 TranslateManagerMetrics::UMA_UNSUPPORTED_LANGUAGE_AT_INITIATION)); | |
165 EXPECT_EQ(0, recorder.GetTotalCount()); | |
166 TranslateManagerMetrics::ReportUnsupportedLanguageAtInitiation("en"); | |
167 EXPECT_EQ(1, recorder.GetCount(ENGLISH)); | |
168 } | |
169 | |
170 TEST(TranslateManagerMetricsTest, ReportedLocalesOnDisabledByPrefs) { | |
171 const int ENGLISH = 25966; | |
172 | |
173 MetricsRecorder recorder(TranslateManagerMetrics::GetMetricsName( | |
174 TranslateManagerMetrics::UMA_LOCALES_ON_DISABLED_BY_PREFS)); | |
175 EXPECT_EQ(0, recorder.GetTotalCount()); | |
176 TranslateManagerMetrics::ReportLocalesOnDisabledByPrefs("en"); | |
177 EXPECT_EQ(1, recorder.GetCount(ENGLISH)); | |
178 } | |
OLD | NEW |