- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / translate / translate_browser_metrics_unittest.cc
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_browser_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_mime_type_is_not_supported,
36                              int expected_url_is_not_supported,
37                              int expected_similar_languages,
38                              int expected_accept_languages,
39                              int expected_auto_by_config,
40                              int expected_auto_by_link,
41                              int expected_show_infobar) {
42     Snapshot();
43
44     EXPECT_EQ(expected_disabled_by_prefs, GetCountWithoutSnapshot(
45         TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_PREFS));
46     EXPECT_EQ(expected_disabled_by_switch, GetCountWithoutSnapshot(
47         TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_SWITCH));
48     EXPECT_EQ(expected_disabled_by_config, GetCountWithoutSnapshot(
49         TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_CONFIG));
50     EXPECT_EQ(expected_language_is_not_supported, GetCountWithoutSnapshot(
51         TranslateBrowserMetrics::INITIATION_STATUS_LANGUAGE_IS_NOT_SUPPORTED));
52     EXPECT_EQ(expected_mime_type_is_not_supported, GetCountWithoutSnapshot(
53         TranslateBrowserMetrics::INITIATION_STATUS_MIME_TYPE_IS_NOT_SUPPORTED));
54     EXPECT_EQ(expected_url_is_not_supported, GetCountWithoutSnapshot(
55         TranslateBrowserMetrics::INITIATION_STATUS_URL_IS_NOT_SUPPORTED));
56     EXPECT_EQ(expected_similar_languages, GetCountWithoutSnapshot(
57         TranslateBrowserMetrics::INITIATION_STATUS_SIMILAR_LANGUAGES));
58     EXPECT_EQ(expected_accept_languages, GetCountWithoutSnapshot(
59         TranslateBrowserMetrics::INITIATION_STATUS_ACCEPT_LANGUAGES));
60     EXPECT_EQ(expected_auto_by_config, GetCountWithoutSnapshot(
61         TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_CONFIG));
62     EXPECT_EQ(expected_auto_by_link, GetCountWithoutSnapshot(
63         TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_LINK));
64     EXPECT_EQ(expected_show_infobar, GetCountWithoutSnapshot(
65         TranslateBrowserMetrics::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(TranslateBrowserMetricsTest, ReportInitiationStatus) {
110   MetricsRecorder recorder(TranslateBrowserMetrics::GetMetricsName(
111       TranslateBrowserMetrics::UMA_INITIATION_STATUS));
112
113   recorder.CheckInitiationStatus(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
114   TranslateBrowserMetrics::ReportInitiationStatus(
115       TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_PREFS);
116   recorder.CheckInitiationStatus(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
117   TranslateBrowserMetrics::ReportInitiationStatus(
118       TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_SWITCH);
119   recorder.CheckInitiationStatus(1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
120   TranslateBrowserMetrics::ReportInitiationStatus(
121       TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_CONFIG);
122   recorder.CheckInitiationStatus(1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0);
123   TranslateBrowserMetrics::ReportInitiationStatus(
124       TranslateBrowserMetrics::INITIATION_STATUS_LANGUAGE_IS_NOT_SUPPORTED);
125   recorder.CheckInitiationStatus(1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0);
126   TranslateBrowserMetrics::ReportInitiationStatus(
127       TranslateBrowserMetrics::INITIATION_STATUS_MIME_TYPE_IS_NOT_SUPPORTED);
128   recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0);
129   TranslateBrowserMetrics::ReportInitiationStatus(
130       TranslateBrowserMetrics::INITIATION_STATUS_URL_IS_NOT_SUPPORTED);
131   recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0);
132   TranslateBrowserMetrics::ReportInitiationStatus(
133       TranslateBrowserMetrics::INITIATION_STATUS_SIMILAR_LANGUAGES);
134   recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0);
135   TranslateBrowserMetrics::ReportInitiationStatus(
136       TranslateBrowserMetrics::INITIATION_STATUS_ACCEPT_LANGUAGES);
137   recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0);
138   TranslateBrowserMetrics::ReportInitiationStatus(
139       TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_CONFIG);
140   recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0);
141   TranslateBrowserMetrics::ReportInitiationStatus(
142       TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_LINK);
143   recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0);
144   TranslateBrowserMetrics::ReportInitiationStatus(
145       TranslateBrowserMetrics::INITIATION_STATUS_SHOW_INFOBAR);
146   recorder.CheckInitiationStatus(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
147 }
148
149 TEST(TranslateBrowserMetricsTest, ReportLanguageDetectionError) {
150   MetricsRecorder recorder(TranslateBrowserMetrics::GetMetricsName(
151       TranslateBrowserMetrics::UMA_LANGUAGE_DETECTION_ERROR));
152   EXPECT_EQ(0, recorder.GetTotalCount());
153   TranslateBrowserMetrics::ReportLanguageDetectionError();
154   EXPECT_EQ(1, recorder.GetTotalCount());
155
156 }
157
158
159 TEST(TranslateBrowserMetricsTest, ReportedLocalesOnDisabledByPrefs) {
160   const int ENGLISH = 25966;
161
162   MetricsRecorder recorder(TranslateBrowserMetrics::GetMetricsName(
163       TranslateBrowserMetrics::UMA_LOCALES_ON_DISABLED_BY_PREFS));
164   EXPECT_EQ(0, recorder.GetTotalCount());
165   TranslateBrowserMetrics::ReportLocalesOnDisabledByPrefs("en");
166   EXPECT_EQ(1, recorder.GetCount(ENGLISH));
167 }
168
169 TEST(TranslateBrowserMetricsTest, ReportedUndisplayableLanguage) {
170   const int ENGLISH = 25966;
171
172   MetricsRecorder recorder(TranslateBrowserMetrics::GetMetricsName(
173       TranslateBrowserMetrics::UMA_UNDISPLAYABLE_LANGUAGE));
174   EXPECT_EQ(0, recorder.GetTotalCount());
175   TranslateBrowserMetrics::ReportUndisplayableLanguage("en");
176   EXPECT_EQ(1, recorder.GetCount(ENGLISH));
177 }
178
179 TEST(TranslateBrowserMetricsTest, ReportedUnsupportedLanguageAtInitiation) {
180   const int ENGLISH = 25966;
181
182   MetricsRecorder recorder(TranslateBrowserMetrics::GetMetricsName(
183       TranslateBrowserMetrics::UMA_UNSUPPORTED_LANGUAGE_AT_INITIATION));
184   EXPECT_EQ(0, recorder.GetTotalCount());
185   TranslateBrowserMetrics::ReportUnsupportedLanguageAtInitiation("en");
186   EXPECT_EQ(1, recorder.GetCount(ENGLISH));
187 }