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 void SetUp() override
{ metrics_
.reset(new SpellCheckHostMetrics
); }
32 SpellCheckHostMetrics
* metrics() { return metrics_
.get(); }
33 void RecordWordCountsForTesting() { metrics_
->RecordWordCounts(); }
36 base::MessageLoop loop_
;
37 scoped_ptr
<SpellCheckHostMetrics
> metrics_
;
40 TEST_F(SpellcheckHostMetricsTest
, RecordEnabledStats
) {
41 const char kMetricName
[] = "SpellCheck.Enabled";
42 base::HistogramTester histogram_tester1
;
44 metrics()->RecordEnabledStats(false);
46 histogram_tester1
.ExpectBucketCount(kMetricName
, 0, 1);
47 histogram_tester1
.ExpectBucketCount(kMetricName
, 1, 0);
49 base::HistogramTester histogram_tester2
;
51 metrics()->RecordEnabledStats(true);
53 histogram_tester2
.ExpectBucketCount(kMetricName
, 0, 0);
54 histogram_tester2
.ExpectBucketCount(kMetricName
, 1, 1);
57 TEST_F(SpellcheckHostMetricsTest
, CustomWordStats
) {
59 // Failing consistently on Win7. See crbug.com/230534.
60 if (base::win::GetVersion() >= base::win::VERSION_VISTA
)
63 SpellCheckHostMetrics::RecordCustomWordCountStats(123);
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());
69 base::HistogramTester histogram_tester
;
71 SpellCheckHostMetrics::RecordCustomWordCountStats(23);
72 histogram_tester
.ExpectBucketCount("SpellCheck.CustomWords", 23, 1);
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"};
83 // Ensure all histograms exist.
84 metrics()->RecordCheckedWordStats(base::ASCIIToUTF16("test"), false);
85 RecordWordCountsForTesting();
87 // Create the tester, taking a snapshot of current histogram samples.
88 base::HistogramTester histogram_tester
;
90 // Nothing changed, so this invocation should not affect any histograms.
91 RecordWordCountsForTesting();
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);
98 TEST_F(SpellcheckHostMetricsTest
, RecordSpellingServiceStats
) {
99 const char kMetricName
[] = "SpellCheck.SpellingService.Enabled";
100 base::HistogramTester histogram_tester1
;
102 metrics()->RecordSpellingServiceStats(false);
104 histogram_tester1
.ExpectBucketCount(kMetricName
, 0, 1);
105 histogram_tester1
.ExpectBucketCount(kMetricName
, 1, 0);
107 base::HistogramTester histogram_tester2
;
109 metrics()->RecordSpellingServiceStats(true);
110 histogram_tester2
.ExpectBucketCount(kMetricName
, 0, 0);
111 histogram_tester2
.ExpectBucketCount(kMetricName
, 1, 1);