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.
5 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h"
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"
17 // For version specific disabled tests below (http://crbug.com/230534).
18 #include "base/win/windows_version.h"
21 class SpellcheckHostMetricsTest
: public testing::Test
{
23 SpellcheckHostMetricsTest() {
26 static void SetUpTestCase() {
27 base::StatisticsRecorder::Initialize();
30 virtual void SetUp() OVERRIDE
{
31 metrics_
.reset(new SpellCheckHostMetrics
);
34 SpellCheckHostMetrics
* metrics() { return metrics_
.get(); }
35 void RecordWordCountsForTesting() { metrics_
->RecordWordCounts(); }
38 base::MessageLoop loop_
;
39 scoped_ptr
<SpellCheckHostMetrics
> metrics_
;
42 TEST_F(SpellcheckHostMetricsTest
, RecordEnabledStats
) {
43 const char kMetricName
[] = "SpellCheck.Enabled";
44 base::StatisticsDeltaReader statistics_delta_reader1
;
46 metrics()->RecordEnabledStats(false);
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));
53 base::StatisticsDeltaReader statistics_delta_reader2
;
55 metrics()->RecordEnabledStats(true);
58 statistics_delta_reader2
.GetHistogramSamplesSinceCreation(kMetricName
);
59 EXPECT_EQ(0, samples
->GetCount(0));
60 EXPECT_EQ(1, samples
->GetCount(1));
63 TEST_F(SpellcheckHostMetricsTest
, CustomWordStats
) {
65 // Failing consistently on Win7. See crbug.com/230534.
66 if (base::win::GetVersion() >= base::win::VERSION_VISTA
)
69 SpellCheckHostMetrics::RecordCustomWordCountStats(123);
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());
75 base::StatisticsDeltaReader statistics_delta_reader
;
77 SpellCheckHostMetrics::RecordCustomWordCountStats(23);
79 scoped_ptr
<base::HistogramSamples
> samples(
80 statistics_delta_reader
.GetHistogramSamplesSinceCreation(
81 "SpellCheck.CustomWords"));
82 EXPECT_EQ(23, samples
->sum());
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"
96 // Ensure all histograms exist.
97 metrics()->RecordCheckedWordStats(base::ASCIIToUTF16("test"), false);
98 RecordWordCountsForTesting();
101 base::StatisticsDeltaReader statistics_delta_reader
;
103 // Nothing changed, so this invocation should not affect any histograms.
104 RecordWordCountsForTesting();
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(
111 EXPECT_EQ(0, samples
->TotalCount());
115 TEST_F(SpellcheckHostMetricsTest
, RecordSpellingServiceStats
) {
116 const char kMetricName
[] = "SpellCheck.SpellingService.Enabled";
117 base::StatisticsDeltaReader statistics_delta_reader1
;
119 metrics()->RecordSpellingServiceStats(false);
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));
126 base::StatisticsDeltaReader statistics_delta_reader2
;
128 metrics()->RecordSpellingServiceStats(true);
131 statistics_delta_reader2
.GetHistogramSamplesSinceCreation(kMetricName
);
132 EXPECT_EQ(0, samples
->GetCount(0));
133 EXPECT_EQ(1, samples
->GetCount(1));