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