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 virtual void SetUp() OVERRIDE { 31 metrics_.reset(new SpellCheckHostMetrics); 32 } 33 34 SpellCheckHostMetrics* metrics() { return metrics_.get(); } 35 void RecordWordCountsForTesting() { metrics_->RecordWordCounts(); } 36 37 private: 38 base::MessageLoop loop_; 39 scoped_ptr<SpellCheckHostMetrics> metrics_; 40 }; 41 42 TEST_F(SpellcheckHostMetricsTest, RecordEnabledStats) { 43 const char kMetricName[] = "SpellCheck.Enabled"; 44 base::HistogramTester histogram_tester1; 45 46 metrics()->RecordEnabledStats(false); 47 48 histogram_tester1.ExpectBucketCount(kMetricName, 0, 1); 49 histogram_tester1.ExpectBucketCount(kMetricName, 1, 0); 50 51 base::HistogramTester histogram_tester2; 52 53 metrics()->RecordEnabledStats(true); 54 55 histogram_tester2.ExpectBucketCount(kMetricName, 0, 0); 56 histogram_tester2.ExpectBucketCount(kMetricName, 1, 1); 57 } 58 59 TEST_F(SpellcheckHostMetricsTest, CustomWordStats) { 60 #if defined(OS_WIN) 61 // Failing consistently on Win7. See crbug.com/230534. 62 if (base::win::GetVersion() >= base::win::VERSION_VISTA) 63 return; 64 #endif 65 SpellCheckHostMetrics::RecordCustomWordCountStats(123); 66 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()); 70 71 base::HistogramTester histogram_tester; 72 73 SpellCheckHostMetrics::RecordCustomWordCountStats(23); 74 histogram_tester.ExpectBucketCount("SpellCheck.CustomWords", 23, 1); 75 } 76 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"}; 84 85 // Ensure all histograms exist. 86 metrics()->RecordCheckedWordStats(base::ASCIIToUTF16("test"), false); 87 RecordWordCountsForTesting(); 88 89 // Create the tester, taking a snapshot of current histogram samples. 90 base::HistogramTester histogram_tester; 91 92 // Nothing changed, so this invocation should not affect any histograms. 93 RecordWordCountsForTesting(); 94 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); 98 } 99 100 TEST_F(SpellcheckHostMetricsTest, RecordSpellingServiceStats) { 101 const char kMetricName[] = "SpellCheck.SpellingService.Enabled"; 102 base::HistogramTester histogram_tester1; 103 104 metrics()->RecordSpellingServiceStats(false); 105 106 histogram_tester1.ExpectBucketCount(kMetricName, 0, 1); 107 histogram_tester1.ExpectBucketCount(kMetricName, 1, 0); 108 109 base::HistogramTester histogram_tester2; 110 111 metrics()->RecordSpellingServiceStats(true); 112 histogram_tester2.ExpectBucketCount(kMetricName, 0, 0); 113 histogram_tester2.ExpectBucketCount(kMetricName, 1, 1); 114 } 115