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/histogram_tester.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::HistogramTester histogram_tester1
;
46 metrics()->RecordEnabledStats(false);
48 histogram_tester1
.ExpectBucketCount(kMetricName
, 0, 1);
49 histogram_tester1
.ExpectBucketCount(kMetricName
, 1, 0);
51 base::HistogramTester histogram_tester2
;
53 metrics()->RecordEnabledStats(true);
55 histogram_tester2
.ExpectBucketCount(kMetricName
, 0, 0);
56 histogram_tester2
.ExpectBucketCount(kMetricName
, 1, 1);
59 TEST_F(SpellcheckHostMetricsTest
, CustomWordStats
) {
61 // Failing consistently on Win7. See crbug.com/230534.
62 if (base::win::GetVersion() >= base::win::VERSION_VISTA
)
65 SpellCheckHostMetrics::RecordCustomWordCountStats(123);
67 // Determine if test failures are due the statistics recorder not being
68 // available or because the histogram just isn't there: crbug.com/230534.
69 EXPECT_TRUE(base::StatisticsRecorder::IsActive());
71 base::HistogramTester histogram_tester
;
73 SpellCheckHostMetrics::RecordCustomWordCountStats(23);
74 histogram_tester
.ExpectBucketCount("SpellCheck.CustomWords", 23, 1);
77 TEST_F(SpellcheckHostMetricsTest
, RecordWordCountsDiscardsDuplicates
) {
78 // This test ensures that RecordWordCounts only records metrics if they
79 // have changed from the last invocation.
80 const char* const histogram_names
[] = {
81 "SpellCheck.CheckedWords", "SpellCheck.MisspelledWords",
82 "SpellCheck.ReplacedWords", "SpellCheck.UniqueWords",
83 "SpellCheck.ShownSuggestions"};
85 // Ensure all histograms exist.
86 metrics()->RecordCheckedWordStats(base::ASCIIToUTF16("test"), false);
87 RecordWordCountsForTesting();
89 // Create the tester, taking a snapshot of current histogram samples.
90 base::HistogramTester histogram_tester
;
92 // Nothing changed, so this invocation should not affect any histograms.
93 RecordWordCountsForTesting();
95 // Get samples for all affected histograms.
96 for (size_t i
= 0; i
< arraysize(histogram_names
); ++i
)
97 histogram_tester
.ExpectTotalCount(histogram_names
[i
], 0);
100 TEST_F(SpellcheckHostMetricsTest
, RecordSpellingServiceStats
) {
101 const char kMetricName
[] = "SpellCheck.SpellingService.Enabled";
102 base::HistogramTester histogram_tester1
;
104 metrics()->RecordSpellingServiceStats(false);
106 histogram_tester1
.ExpectBucketCount(kMetricName
, 0, 1);
107 histogram_tester1
.ExpectBucketCount(kMetricName
, 1, 0);
109 base::HistogramTester histogram_tester2
;
111 metrics()->RecordSpellingServiceStats(true);
112 histogram_tester2
.ExpectBucketCount(kMetricName
, 0, 0);
113 histogram_tester2
.ExpectBucketCount(kMetricName
, 1, 1);