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/common/translate/translate_common_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::SampleCountIterator; 18 using base::StatisticsRecorder; 19 using base::TimeTicks; 20 21 namespace { 22 23 const int kTrue = 1; 24 const int kFalse = 0; 25 26 class MetricsRecorder { 27 public: 28 explicit MetricsRecorder(const char* key) : key_(key) { 29 StatisticsRecorder::Initialize(); 30 31 HistogramBase* histogram = StatisticsRecorder::FindHistogram(key_); 32 if (histogram) 33 base_samples_ = histogram->SnapshotSamples(); 34 } 35 36 void CheckLanguage(TranslateCommonMetrics::MetricsNameIndex index, 37 int expected_not_provided, 38 int expected_valid, 39 int expected_invalid) { 40 ASSERT_EQ(TranslateCommonMetrics::GetMetricsName(index), key_); 41 42 Snapshot(); 43 44 EXPECT_EQ(expected_not_provided, 45 GetCountWithoutSnapshot( 46 TranslateCommonMetrics::LANGUAGE_NOT_PROVIDED)); 47 EXPECT_EQ(expected_valid, 48 GetCountWithoutSnapshot( 49 TranslateCommonMetrics::LANGUAGE_VALID)); 50 EXPECT_EQ(expected_invalid, 51 GetCountWithoutSnapshot( 52 TranslateCommonMetrics::LANGUAGE_INVALID)); 53 } 54 55 void CheckLanguageVerification(int expected_cld_disabled, 56 int expected_cld_only, 57 int expected_unknown, 58 int expected_cld_agree, 59 int expected_cld_disagree, 60 int expected_trust_cld, 61 int expected_cld_complement_sub_code) { 62 ASSERT_EQ(TranslateCommonMetrics::GetMetricsName( 63 TranslateCommonMetrics::UMA_LANGUAGE_VERIFICATION), key_); 64 65 Snapshot(); 66 67 EXPECT_EQ( 68 expected_cld_disabled, 69 GetCountWithoutSnapshot( 70 TranslateCommonMetrics::LANGUAGE_VERIFICATION_CLD_DISABLED)); 71 EXPECT_EQ( 72 expected_cld_only, 73 GetCountWithoutSnapshot( 74 TranslateCommonMetrics::LANGUAGE_VERIFICATION_CLD_ONLY)); 75 EXPECT_EQ( 76 expected_unknown, 77 GetCountWithoutSnapshot( 78 TranslateCommonMetrics::LANGUAGE_VERIFICATION_UNKNOWN)); 79 EXPECT_EQ( 80 expected_cld_agree, 81 GetCountWithoutSnapshot( 82 TranslateCommonMetrics::LANGUAGE_VERIFICATION_CLD_AGREE)); 83 EXPECT_EQ( 84 expected_cld_disagree, 85 GetCountWithoutSnapshot( 86 TranslateCommonMetrics::LANGUAGE_VERIFICATION_CLD_DISAGREE)); 87 EXPECT_EQ( 88 expected_trust_cld, 89 GetCountWithoutSnapshot( 90 TranslateCommonMetrics::LANGUAGE_VERIFICATION_TRUST_CLD)); 91 EXPECT_EQ( 92 expected_cld_complement_sub_code, 93 GetCountWithoutSnapshot( 94 TranslateCommonMetrics:: 95 LANGUAGE_VERIFICATION_CLD_COMPLEMENT_SUB_CODE)); 96 } 97 98 void CheckScheme(int expected_http, int expected_https, int expected_others) { 99 ASSERT_EQ(TranslateCommonMetrics::GetMetricsName( 100 TranslateCommonMetrics::UMA_PAGE_SCHEME), key_); 101 102 Snapshot(); 103 104 EXPECT_EQ(expected_http, 105 GetCountWithoutSnapshot(TranslateCommonMetrics::SCHEME_HTTP)); 106 EXPECT_EQ(expected_https, 107 GetCountWithoutSnapshot(TranslateCommonMetrics::SCHEME_HTTPS)); 108 EXPECT_EQ(expected_others, 109 GetCountWithoutSnapshot(TranslateCommonMetrics::SCHEME_OTHERS)); 110 } 111 112 void CheckTotalCount(int count) { 113 Snapshot(); 114 EXPECT_EQ(count, GetTotalCount()); 115 } 116 117 void CheckValueInLogs(double value) { 118 Snapshot(); 119 ASSERT_TRUE(samples_.get()); 120 for (scoped_ptr<SampleCountIterator> i = samples_->Iterator(); 121 !i->Done(); 122 i->Next()) { 123 HistogramBase::Sample min; 124 HistogramBase::Sample max; 125 HistogramBase::Count count; 126 i->Get(&min, &max, &count); 127 if (min <= value && value <= max && count >= 1) 128 return; 129 } 130 EXPECT_FALSE(true); 131 } 132 133 HistogramBase::Count GetCount(HistogramBase::Sample value) { 134 Snapshot(); 135 return GetCountWithoutSnapshot(value); 136 } 137 138 private: 139 void Snapshot() { 140 HistogramBase* histogram = StatisticsRecorder::FindHistogram(key_); 141 if (!histogram) 142 return; 143 samples_ = histogram->SnapshotSamples(); 144 } 145 146 HistogramBase::Count GetCountWithoutSnapshot(HistogramBase::Sample value) { 147 if (!samples_.get()) 148 return 0; 149 HistogramBase::Count count = samples_->GetCount(value); 150 if (!base_samples_.get()) 151 return count; 152 return count - base_samples_->GetCount(value); 153 } 154 155 HistogramBase::Count GetTotalCount() { 156 if (!samples_.get()) 157 return 0; 158 HistogramBase::Count count = samples_->TotalCount(); 159 if (!base_samples_.get()) 160 return count; 161 return count - base_samples_->TotalCount(); 162 } 163 164 std::string key_; 165 scoped_ptr<HistogramSamples> base_samples_; 166 scoped_ptr<HistogramSamples> samples_; 167 168 DISALLOW_COPY_AND_ASSIGN(MetricsRecorder); 169 }; 170 171 } // namespace 172 173 TEST(TranslateCommonMetricsTest, ReportContentLanguage) { 174 MetricsRecorder recorder(TranslateCommonMetrics::GetMetricsName( 175 TranslateCommonMetrics::UMA_CONTENT_LANGUAGE)); 176 177 recorder.CheckLanguage(TranslateCommonMetrics::UMA_CONTENT_LANGUAGE, 0, 0, 0); 178 TranslateCommonMetrics::ReportContentLanguage(std::string(), std::string()); 179 recorder.CheckLanguage(TranslateCommonMetrics::UMA_CONTENT_LANGUAGE, 1, 0, 0); 180 TranslateCommonMetrics::ReportContentLanguage("ja_JP", "ja-JP"); 181 recorder.CheckLanguage(TranslateCommonMetrics::UMA_CONTENT_LANGUAGE, 1, 0, 1); 182 TranslateCommonMetrics::ReportContentLanguage("en", "en"); 183 recorder.CheckLanguage(TranslateCommonMetrics::UMA_CONTENT_LANGUAGE, 1, 1, 1); 184 } 185 186 TEST(TranslateCommonMetricsTest, ReportHtmlLang) { 187 MetricsRecorder recorder(TranslateCommonMetrics::GetMetricsName( 188 TranslateCommonMetrics::UMA_HTML_LANG)); 189 190 recorder.CheckLanguage(TranslateCommonMetrics::UMA_HTML_LANG, 0, 0, 0); 191 TranslateCommonMetrics::ReportHtmlLang(std::string(), std::string()); 192 recorder.CheckLanguage(TranslateCommonMetrics::UMA_HTML_LANG, 1, 0, 0); 193 TranslateCommonMetrics::ReportHtmlLang("ja_JP", "ja-JP"); 194 recorder.CheckLanguage(TranslateCommonMetrics::UMA_HTML_LANG, 1, 0, 1); 195 TranslateCommonMetrics::ReportHtmlLang("en", "en"); 196 recorder.CheckLanguage(TranslateCommonMetrics::UMA_HTML_LANG, 1, 1, 1); 197 } 198 199 TEST(TranslateCommonMetricsTest, ReportLanguageVerification) { 200 MetricsRecorder recorder(TranslateCommonMetrics::GetMetricsName( 201 TranslateCommonMetrics::UMA_LANGUAGE_VERIFICATION)); 202 203 recorder.CheckLanguageVerification(0, 0, 0, 0, 0, 0, 0); 204 TranslateCommonMetrics::ReportLanguageVerification( 205 TranslateCommonMetrics::LANGUAGE_VERIFICATION_CLD_DISABLED); 206 recorder.CheckLanguageVerification(1, 0, 0, 0, 0, 0, 0); 207 TranslateCommonMetrics::ReportLanguageVerification( 208 TranslateCommonMetrics::LANGUAGE_VERIFICATION_CLD_ONLY); 209 recorder.CheckLanguageVerification(1, 1, 0, 0, 0, 0, 0); 210 TranslateCommonMetrics::ReportLanguageVerification( 211 TranslateCommonMetrics::LANGUAGE_VERIFICATION_UNKNOWN); 212 recorder.CheckLanguageVerification(1, 1, 1, 0, 0, 0, 0); 213 TranslateCommonMetrics::ReportLanguageVerification( 214 TranslateCommonMetrics::LANGUAGE_VERIFICATION_CLD_AGREE); 215 recorder.CheckLanguageVerification(1, 1, 1, 1, 0, 0, 0); 216 TranslateCommonMetrics::ReportLanguageVerification( 217 TranslateCommonMetrics::LANGUAGE_VERIFICATION_CLD_DISAGREE); 218 recorder.CheckLanguageVerification(1, 1, 1, 1, 1, 0, 0); 219 TranslateCommonMetrics::ReportLanguageVerification( 220 TranslateCommonMetrics::LANGUAGE_VERIFICATION_TRUST_CLD); 221 recorder.CheckLanguageVerification(1, 1, 1, 1, 1, 1, 0); 222 TranslateCommonMetrics::ReportLanguageVerification( 223 TranslateCommonMetrics::LANGUAGE_VERIFICATION_CLD_COMPLEMENT_SUB_CODE); 224 recorder.CheckLanguageVerification(1, 1, 1, 1, 1, 1, 1); 225 } 226 227 TEST(TranslateCommonMetricsTest, ReportTimeToBeReady) { 228 MetricsRecorder recorder(TranslateCommonMetrics::GetMetricsName( 229 TranslateCommonMetrics::UMA_TIME_TO_BE_READY)); 230 recorder.CheckTotalCount(0); 231 TranslateCommonMetrics::ReportTimeToBeReady(3.14); 232 recorder.CheckValueInLogs(3.14); 233 recorder.CheckTotalCount(1); 234 } 235 236 TEST(TranslateCommonMetricsTest, ReportTimeToLoad) { 237 MetricsRecorder recorder(TranslateCommonMetrics::GetMetricsName( 238 TranslateCommonMetrics::UMA_TIME_TO_LOAD)); 239 recorder.CheckTotalCount(0); 240 TranslateCommonMetrics::ReportTimeToLoad(573.0); 241 recorder.CheckValueInLogs(573.0); 242 recorder.CheckTotalCount(1); 243 } 244 245 TEST(TranslateCommonMetricsTest, ReportTimeToTranslate) { 246 MetricsRecorder recorder(TranslateCommonMetrics::GetMetricsName( 247 TranslateCommonMetrics::UMA_TIME_TO_TRANSLATE)); 248 recorder.CheckTotalCount(0); 249 TranslateCommonMetrics::ReportTimeToTranslate(4649.0); 250 recorder.CheckValueInLogs(4649.0); 251 recorder.CheckTotalCount(1); 252 } 253 254 TEST(TranslateCommonMetricsTest, ReportUserActionDuration) { 255 MetricsRecorder recorder(TranslateCommonMetrics::GetMetricsName( 256 TranslateCommonMetrics::UMA_USER_ACTION_DURATION)); 257 recorder.CheckTotalCount(0); 258 TimeTicks begin = TimeTicks::Now(); 259 TimeTicks end = begin + base::TimeDelta::FromSeconds(3776); 260 TranslateCommonMetrics::ReportUserActionDuration(begin, end); 261 recorder.CheckValueInLogs(3776000.0); 262 recorder.CheckTotalCount(1); 263 } 264 265 TEST(TranslateCommonMetricsTest, ReportPageScheme) { 266 MetricsRecorder recorder(TranslateCommonMetrics::GetMetricsName( 267 TranslateCommonMetrics::UMA_PAGE_SCHEME)); 268 recorder.CheckScheme(0, 0, 0); 269 TranslateCommonMetrics::ReportPageScheme("http"); 270 recorder.CheckScheme(1, 0, 0); 271 TranslateCommonMetrics::ReportPageScheme("https"); 272 recorder.CheckScheme(1, 1, 0); 273 TranslateCommonMetrics::ReportPageScheme("ftp"); 274 recorder.CheckScheme(1, 1, 1); 275 } 276 277 TEST(TranslateCommonMetricsTest, ReportSimilarLanguageMatch) { 278 MetricsRecorder recorder(TranslateCommonMetrics::GetMetricsName( 279 TranslateCommonMetrics::UMA_SIMILAR_LANGUAGE_MATCH)); 280 recorder.CheckTotalCount(0); 281 EXPECT_EQ(0, recorder.GetCount(kTrue)); 282 EXPECT_EQ(0, recorder.GetCount(kFalse)); 283 TranslateCommonMetrics::ReportSimilarLanguageMatch(true); 284 EXPECT_EQ(1, recorder.GetCount(kTrue)); 285 EXPECT_EQ(0, recorder.GetCount(kFalse)); 286 TranslateCommonMetrics::ReportSimilarLanguageMatch(false); 287 EXPECT_EQ(1, recorder.GetCount(kTrue)); 288 EXPECT_EQ(1, recorder.GetCount(kFalse)); 289 } 290 291 TEST(TranslateCommonMetricsTest, ReportLanguageDetectionTime) { 292 MetricsRecorder recorder(TranslateCommonMetrics::GetMetricsName( 293 TranslateCommonMetrics::UMA_LANGUAGE_DETECTION)); 294 recorder.CheckTotalCount(0); 295 TimeTicks begin = TimeTicks::Now(); 296 TimeTicks end = begin + base::TimeDelta::FromMicroseconds(9009); 297 TranslateCommonMetrics::ReportLanguageDetectionTime(begin, end); 298 recorder.CheckValueInLogs(9.009); 299 recorder.CheckTotalCount(1); 300 } 301