Don't preload rarely seen large images
[chromium-blink-merge.git] / components / translate / core / common / translate_metrics_unittest.cc
blob04eac9f003eac053bd987abbb2a3b2d560cc6849
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/common/translate_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::SampleCountIterator;
18 using base::StatisticsRecorder;
19 using base::TimeTicks;
21 namespace translate {
23 namespace {
25 const int kTrue = 1;
26 const int kFalse = 0;
28 class MetricsRecorder {
29 public:
30 explicit MetricsRecorder(const char* key) : key_(key) {
31 StatisticsRecorder::Initialize();
33 HistogramBase* histogram = StatisticsRecorder::FindHistogram(key_);
34 if (histogram)
35 base_samples_ = histogram->SnapshotSamples();
38 void CheckLanguage(translate::MetricsNameIndex index,
39 int expected_not_provided,
40 int expected_valid,
41 int expected_invalid) {
42 ASSERT_EQ(translate::GetMetricsName(index), key_);
44 Snapshot();
46 EXPECT_EQ(expected_not_provided,
47 GetCountWithoutSnapshot(translate::LANGUAGE_NOT_PROVIDED));
48 EXPECT_EQ(expected_valid,
49 GetCountWithoutSnapshot(translate::LANGUAGE_VALID));
50 EXPECT_EQ(expected_invalid,
51 GetCountWithoutSnapshot(translate::LANGUAGE_INVALID));
54 void CheckLanguageVerification(int expected_cld_disabled,
55 int expected_cld_only,
56 int expected_unknown,
57 int expected_cld_agree,
58 int expected_cld_disagree,
59 int expected_trust_cld,
60 int expected_cld_complement_sub_code) {
61 ASSERT_EQ(translate::GetMetricsName(translate::UMA_LANGUAGE_VERIFICATION),
62 key_);
64 Snapshot();
66 EXPECT_EQ(
67 expected_cld_disabled,
68 GetCountWithoutSnapshot(translate::LANGUAGE_VERIFICATION_CLD_DISABLED));
69 EXPECT_EQ(
70 expected_cld_only,
71 GetCountWithoutSnapshot(translate::LANGUAGE_VERIFICATION_CLD_ONLY));
72 EXPECT_EQ(
73 expected_unknown,
74 GetCountWithoutSnapshot(translate::LANGUAGE_VERIFICATION_UNKNOWN));
75 EXPECT_EQ(
76 expected_cld_agree,
77 GetCountWithoutSnapshot(translate::LANGUAGE_VERIFICATION_CLD_AGREE));
78 EXPECT_EQ(
79 expected_cld_disagree,
80 GetCountWithoutSnapshot(translate::LANGUAGE_VERIFICATION_CLD_DISAGREE));
81 EXPECT_EQ(
82 expected_trust_cld,
83 GetCountWithoutSnapshot(translate::LANGUAGE_VERIFICATION_TRUST_CLD));
84 EXPECT_EQ(expected_cld_complement_sub_code,
85 GetCountWithoutSnapshot(
86 translate::LANGUAGE_VERIFICATION_CLD_COMPLEMENT_SUB_CODE));
89 void CheckScheme(int expected_http, int expected_https, int expected_others) {
90 ASSERT_EQ(translate::GetMetricsName(translate::UMA_PAGE_SCHEME), key_);
92 Snapshot();
94 EXPECT_EQ(expected_http, GetCountWithoutSnapshot(translate::SCHEME_HTTP));
95 EXPECT_EQ(expected_https, GetCountWithoutSnapshot(translate::SCHEME_HTTPS));
96 EXPECT_EQ(expected_others,
97 GetCountWithoutSnapshot(translate::SCHEME_OTHERS));
100 void CheckTotalCount(int count) {
101 Snapshot();
102 EXPECT_EQ(count, GetTotalCount());
105 void CheckValueInLogs(double value) {
106 Snapshot();
107 ASSERT_TRUE(samples_.get());
108 for (scoped_ptr<SampleCountIterator> i = samples_->Iterator(); !i->Done();
109 i->Next()) {
110 HistogramBase::Sample min;
111 HistogramBase::Sample max;
112 HistogramBase::Count count;
113 i->Get(&min, &max, &count);
114 if (min <= value && value <= max && count >= 1)
115 return;
117 EXPECT_FALSE(true);
120 HistogramBase::Count GetCount(HistogramBase::Sample value) {
121 Snapshot();
122 return GetCountWithoutSnapshot(value);
125 private:
126 void Snapshot() {
127 HistogramBase* histogram = StatisticsRecorder::FindHistogram(key_);
128 if (!histogram)
129 return;
130 samples_ = histogram->SnapshotSamples();
133 HistogramBase::Count GetCountWithoutSnapshot(HistogramBase::Sample value) {
134 if (!samples_.get())
135 return 0;
136 HistogramBase::Count count = samples_->GetCount(value);
137 if (!base_samples_.get())
138 return count;
139 return count - base_samples_->GetCount(value);
142 HistogramBase::Count GetTotalCount() {
143 if (!samples_.get())
144 return 0;
145 HistogramBase::Count count = samples_->TotalCount();
146 if (!base_samples_.get())
147 return count;
148 return count - base_samples_->TotalCount();
151 std::string key_;
152 scoped_ptr<HistogramSamples> base_samples_;
153 scoped_ptr<HistogramSamples> samples_;
155 DISALLOW_COPY_AND_ASSIGN(MetricsRecorder);
158 } // namespace
160 TEST(TranslateMetricsTest, ReportContentLanguage) {
161 MetricsRecorder recorder(
162 translate::GetMetricsName(translate::UMA_CONTENT_LANGUAGE));
164 recorder.CheckLanguage(translate::UMA_CONTENT_LANGUAGE, 0, 0, 0);
165 translate::ReportContentLanguage(std::string(), std::string());
166 recorder.CheckLanguage(translate::UMA_CONTENT_LANGUAGE, 1, 0, 0);
167 translate::ReportContentLanguage("ja_JP", "ja-JP");
168 recorder.CheckLanguage(translate::UMA_CONTENT_LANGUAGE, 1, 0, 1);
169 translate::ReportContentLanguage("en", "en");
170 recorder.CheckLanguage(translate::UMA_CONTENT_LANGUAGE, 1, 1, 1);
173 TEST(TranslateMetricsTest, ReportHtmlLang) {
174 MetricsRecorder recorder(translate::GetMetricsName(translate::UMA_HTML_LANG));
176 recorder.CheckLanguage(translate::UMA_HTML_LANG, 0, 0, 0);
177 translate::ReportHtmlLang(std::string(), std::string());
178 recorder.CheckLanguage(translate::UMA_HTML_LANG, 1, 0, 0);
179 translate::ReportHtmlLang("ja_JP", "ja-JP");
180 recorder.CheckLanguage(translate::UMA_HTML_LANG, 1, 0, 1);
181 translate::ReportHtmlLang("en", "en");
182 recorder.CheckLanguage(translate::UMA_HTML_LANG, 1, 1, 1);
185 TEST(TranslateMetricsTest, ReportLanguageVerification) {
186 MetricsRecorder recorder(
187 translate::GetMetricsName(translate::UMA_LANGUAGE_VERIFICATION));
189 recorder.CheckLanguageVerification(0, 0, 0, 0, 0, 0, 0);
190 translate::ReportLanguageVerification(
191 translate::LANGUAGE_VERIFICATION_CLD_DISABLED);
192 recorder.CheckLanguageVerification(1, 0, 0, 0, 0, 0, 0);
193 translate::ReportLanguageVerification(
194 translate::LANGUAGE_VERIFICATION_CLD_ONLY);
195 recorder.CheckLanguageVerification(1, 1, 0, 0, 0, 0, 0);
196 translate::ReportLanguageVerification(
197 translate::LANGUAGE_VERIFICATION_UNKNOWN);
198 recorder.CheckLanguageVerification(1, 1, 1, 0, 0, 0, 0);
199 translate::ReportLanguageVerification(
200 translate::LANGUAGE_VERIFICATION_CLD_AGREE);
201 recorder.CheckLanguageVerification(1, 1, 1, 1, 0, 0, 0);
202 translate::ReportLanguageVerification(
203 translate::LANGUAGE_VERIFICATION_CLD_DISAGREE);
204 recorder.CheckLanguageVerification(1, 1, 1, 1, 1, 0, 0);
205 translate::ReportLanguageVerification(
206 translate::LANGUAGE_VERIFICATION_TRUST_CLD);
207 recorder.CheckLanguageVerification(1, 1, 1, 1, 1, 1, 0);
208 translate::ReportLanguageVerification(
209 translate::LANGUAGE_VERIFICATION_CLD_COMPLEMENT_SUB_CODE);
210 recorder.CheckLanguageVerification(1, 1, 1, 1, 1, 1, 1);
213 TEST(TranslateMetricsTest, ReportTimeToBeReady) {
214 MetricsRecorder recorder(
215 translate::GetMetricsName(translate::UMA_TIME_TO_BE_READY));
216 recorder.CheckTotalCount(0);
217 translate::ReportTimeToBeReady(3.14);
218 recorder.CheckValueInLogs(3.14);
219 recorder.CheckTotalCount(1);
222 TEST(TranslateMetricsTest, ReportTimeToLoad) {
223 MetricsRecorder recorder(
224 translate::GetMetricsName(translate::UMA_TIME_TO_LOAD));
225 recorder.CheckTotalCount(0);
226 translate::ReportTimeToLoad(573.0);
227 recorder.CheckValueInLogs(573.0);
228 recorder.CheckTotalCount(1);
231 TEST(TranslateMetricsTest, ReportTimeToTranslate) {
232 MetricsRecorder recorder(
233 translate::GetMetricsName(translate::UMA_TIME_TO_TRANSLATE));
234 recorder.CheckTotalCount(0);
235 translate::ReportTimeToTranslate(4649.0);
236 recorder.CheckValueInLogs(4649.0);
237 recorder.CheckTotalCount(1);
240 TEST(TranslateMetricsTest, ReportUserActionDuration) {
241 MetricsRecorder recorder(
242 translate::GetMetricsName(translate::UMA_USER_ACTION_DURATION));
243 recorder.CheckTotalCount(0);
244 TimeTicks begin = TimeTicks::Now();
245 TimeTicks end = begin + base::TimeDelta::FromSeconds(3776);
246 translate::ReportUserActionDuration(begin, end);
247 recorder.CheckValueInLogs(3776000.0);
248 recorder.CheckTotalCount(1);
251 TEST(TranslateMetricsTest, ReportPageScheme) {
252 MetricsRecorder recorder(
253 translate::GetMetricsName(translate::UMA_PAGE_SCHEME));
254 recorder.CheckScheme(0, 0, 0);
255 translate::ReportPageScheme("http");
256 recorder.CheckScheme(1, 0, 0);
257 translate::ReportPageScheme("https");
258 recorder.CheckScheme(1, 1, 0);
259 translate::ReportPageScheme("ftp");
260 recorder.CheckScheme(1, 1, 1);
263 TEST(TranslateMetricsTest, ReportSimilarLanguageMatch) {
264 MetricsRecorder recorder(
265 translate::GetMetricsName(translate::UMA_SIMILAR_LANGUAGE_MATCH));
266 recorder.CheckTotalCount(0);
267 EXPECT_EQ(0, recorder.GetCount(kTrue));
268 EXPECT_EQ(0, recorder.GetCount(kFalse));
269 translate::ReportSimilarLanguageMatch(true);
270 EXPECT_EQ(1, recorder.GetCount(kTrue));
271 EXPECT_EQ(0, recorder.GetCount(kFalse));
272 translate::ReportSimilarLanguageMatch(false);
273 EXPECT_EQ(1, recorder.GetCount(kTrue));
274 EXPECT_EQ(1, recorder.GetCount(kFalse));
277 TEST(TranslateMetricsTest, ReportLanguageDetectionTime) {
278 MetricsRecorder recorder(
279 translate::GetMetricsName(translate::UMA_LANGUAGE_DETECTION));
280 recorder.CheckTotalCount(0);
281 TimeTicks begin = TimeTicks::Now();
282 TimeTicks end = begin + base::TimeDelta::FromMicroseconds(9009);
283 translate::ReportLanguageDetectionTime(begin, end);
284 recorder.CheckValueInLogs(9.009);
285 recorder.CheckTotalCount(1);
288 } // namespace translate