Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / spellchecker / spellcheck_host_metrics_unittest.cc
1 // Copyright (c) 2012 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/spellchecker/spellcheck_host_metrics.h"
6
7 #include "base/basictypes.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/metrics/histogram_samples.h"
11 #include "base/metrics/statistics_recorder.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/test/histogram_tester.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 #if defined(OS_WIN)
17 // For version specific disabled tests below (http://crbug.com/230534).
18 #include "base/win/windows_version.h"
19 #endif
20
21 class SpellcheckHostMetricsTest : public testing::Test {
22  public:
23   SpellcheckHostMetricsTest() {
24   }
25
26   static void SetUpTestCase() {
27     base::StatisticsRecorder::Initialize();
28   }
29
30   void SetUp() override { metrics_.reset(new SpellCheckHostMetrics); }
31
32   SpellCheckHostMetrics* metrics() { return metrics_.get(); }
33   void RecordWordCountsForTesting() { metrics_->RecordWordCounts(); }
34
35  private:
36   base::MessageLoop loop_;
37   scoped_ptr<SpellCheckHostMetrics> metrics_;
38 };
39
40 TEST_F(SpellcheckHostMetricsTest, RecordEnabledStats) {
41   const char kMetricName[] = "SpellCheck.Enabled";
42   base::HistogramTester histogram_tester1;
43
44   metrics()->RecordEnabledStats(false);
45
46   histogram_tester1.ExpectBucketCount(kMetricName, 0, 1);
47   histogram_tester1.ExpectBucketCount(kMetricName, 1, 0);
48
49   base::HistogramTester histogram_tester2;
50
51   metrics()->RecordEnabledStats(true);
52
53   histogram_tester2.ExpectBucketCount(kMetricName, 0, 0);
54   histogram_tester2.ExpectBucketCount(kMetricName, 1, 1);
55 }
56
57 TEST_F(SpellcheckHostMetricsTest, CustomWordStats) {
58 #if defined(OS_WIN)
59 // Failing consistently on Win7. See crbug.com/230534.
60   if (base::win::GetVersion() >= base::win::VERSION_VISTA)
61     return;
62 #endif
63   SpellCheckHostMetrics::RecordCustomWordCountStats(123);
64
65   // Determine if test failures are due the statistics recorder not being
66   // available or because the histogram just isn't there: crbug.com/230534.
67   EXPECT_TRUE(base::StatisticsRecorder::IsActive());
68
69   base::HistogramTester histogram_tester;
70
71   SpellCheckHostMetrics::RecordCustomWordCountStats(23);
72   histogram_tester.ExpectBucketCount("SpellCheck.CustomWords", 23, 1);
73 }
74
75 TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) {
76   // This test ensures that RecordWordCounts only records metrics if they
77   // have changed from the last invocation.
78   const char* const histogram_names[] = {
79       "SpellCheck.CheckedWords", "SpellCheck.MisspelledWords",
80       "SpellCheck.ReplacedWords", "SpellCheck.UniqueWords",
81       "SpellCheck.ShownSuggestions"};
82
83   // Ensure all histograms exist.
84   metrics()->RecordCheckedWordStats(base::ASCIIToUTF16("test"), false);
85   RecordWordCountsForTesting();
86
87   // Create the tester, taking a snapshot of current histogram samples.
88   base::HistogramTester histogram_tester;
89
90   // Nothing changed, so this invocation should not affect any histograms.
91   RecordWordCountsForTesting();
92
93   // Get samples for all affected histograms.
94   for (size_t i = 0; i < arraysize(histogram_names); ++i)
95     histogram_tester.ExpectTotalCount(histogram_names[i], 0);
96 }
97
98 TEST_F(SpellcheckHostMetricsTest, RecordSpellingServiceStats) {
99   const char kMetricName[] = "SpellCheck.SpellingService.Enabled";
100   base::HistogramTester histogram_tester1;
101
102   metrics()->RecordSpellingServiceStats(false);
103
104   histogram_tester1.ExpectBucketCount(kMetricName, 0, 1);
105   histogram_tester1.ExpectBucketCount(kMetricName, 1, 0);
106
107   base::HistogramTester histogram_tester2;
108
109   metrics()->RecordSpellingServiceStats(true);
110   histogram_tester2.ExpectBucketCount(kMetricName, 0, 0);
111   histogram_tester2.ExpectBucketCount(kMetricName, 1, 1);
112 }