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/statistics_delta_reader.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:
SpellcheckHostMetricsTest()23 SpellcheckHostMetricsTest() {
24 }
25
SetUpTestCase()26 static void SetUpTestCase() {
27 base::StatisticsRecorder::Initialize();
28 }
29
SetUp()30 virtual void SetUp() OVERRIDE {
31 metrics_.reset(new SpellCheckHostMetrics);
32 }
33
metrics()34 SpellCheckHostMetrics* metrics() { return metrics_.get(); }
RecordWordCountsForTesting()35 void RecordWordCountsForTesting() { metrics_->RecordWordCounts(); }
36
37 private:
38 base::MessageLoop loop_;
39 scoped_ptr<SpellCheckHostMetrics> metrics_;
40 };
41
TEST_F(SpellcheckHostMetricsTest,RecordEnabledStats)42 TEST_F(SpellcheckHostMetricsTest, RecordEnabledStats) {
43 const char kMetricName[] = "SpellCheck.Enabled";
44 base::StatisticsDeltaReader statistics_delta_reader1;
45
46 metrics()->RecordEnabledStats(false);
47
48 scoped_ptr<base::HistogramSamples> samples(
49 statistics_delta_reader1.GetHistogramSamplesSinceCreation(kMetricName));
50 EXPECT_EQ(1, samples->GetCount(0));
51 EXPECT_EQ(0, samples->GetCount(1));
52
53 base::StatisticsDeltaReader statistics_delta_reader2;
54
55 metrics()->RecordEnabledStats(true);
56
57 samples =
58 statistics_delta_reader2.GetHistogramSamplesSinceCreation(kMetricName);
59 EXPECT_EQ(0, samples->GetCount(0));
60 EXPECT_EQ(1, samples->GetCount(1));
61 }
62
TEST_F(SpellcheckHostMetricsTest,CustomWordStats)63 TEST_F(SpellcheckHostMetricsTest, CustomWordStats) {
64 #if defined(OS_WIN)
65 // Failing consistently on Win7. See crbug.com/230534.
66 if (base::win::GetVersion() >= base::win::VERSION_VISTA)
67 return;
68 #endif
69 SpellCheckHostMetrics::RecordCustomWordCountStats(123);
70
71 // Determine if test failures are due the statistics recorder not being
72 // available or because the histogram just isn't there: crbug.com/230534.
73 EXPECT_TRUE(base::StatisticsRecorder::IsActive());
74
75 base::StatisticsDeltaReader statistics_delta_reader;
76
77 SpellCheckHostMetrics::RecordCustomWordCountStats(23);
78
79 scoped_ptr<base::HistogramSamples> samples(
80 statistics_delta_reader.GetHistogramSamplesSinceCreation(
81 "SpellCheck.CustomWords"));
82 EXPECT_EQ(23, samples->sum());
83 }
84
TEST_F(SpellcheckHostMetricsTest,RecordWordCountsDiscardsDuplicates)85 TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) {
86 // This test ensures that RecordWordCounts only records metrics if they
87 // have changed from the last invocation.
88 const char* histogramName[] = {
89 "SpellCheck.CheckedWords",
90 "SpellCheck.MisspelledWords",
91 "SpellCheck.ReplacedWords",
92 "SpellCheck.UniqueWords",
93 "SpellCheck.ShownSuggestions"
94 };
95
96 // Ensure all histograms exist.
97 metrics()->RecordCheckedWordStats(base::ASCIIToUTF16("test"), false);
98 RecordWordCountsForTesting();
99
100 // Start the reader.
101 base::StatisticsDeltaReader statistics_delta_reader;
102
103 // Nothing changed, so this invocation should not affect any histograms.
104 RecordWordCountsForTesting();
105
106 // Get samples for all affected histograms.
107 scoped_ptr<base::HistogramSamples> samples;
108 for (size_t i = 0; i < arraysize(histogramName); ++i) {
109 samples = statistics_delta_reader.GetHistogramSamplesSinceCreation(
110 histogramName[i]);
111 EXPECT_EQ(0, samples->TotalCount());
112 }
113 }
114
TEST_F(SpellcheckHostMetricsTest,RecordSpellingServiceStats)115 TEST_F(SpellcheckHostMetricsTest, RecordSpellingServiceStats) {
116 const char kMetricName[] = "SpellCheck.SpellingService.Enabled";
117 base::StatisticsDeltaReader statistics_delta_reader1;
118
119 metrics()->RecordSpellingServiceStats(false);
120
121 scoped_ptr<base::HistogramSamples> samples(
122 statistics_delta_reader1.GetHistogramSamplesSinceCreation(kMetricName));
123 EXPECT_EQ(1, samples->GetCount(0));
124 EXPECT_EQ(0, samples->GetCount(1));
125
126 base::StatisticsDeltaReader statistics_delta_reader2;
127
128 metrics()->RecordSpellingServiceStats(true);
129
130 samples =
131 statistics_delta_reader2.GetHistogramSamplesSinceCreation(kMetricName);
132 EXPECT_EQ(0, samples->GetCount(0));
133 EXPECT_EQ(1, samples->GetCount(1));
134 }
135