| OLD | NEW |
| (Empty) |
| 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 <stddef.h> | |
| 8 | |
| 9 #include <memory> | |
| 10 | |
| 11 #include "base/macros.h" | |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "base/metrics/histogram_samples.h" | |
| 14 #include "base/metrics/statistics_recorder.h" | |
| 15 #include "base/strings/utf_string_conversions.h" | |
| 16 #include "base/test/histogram_tester.h" | |
| 17 #include "build/build_config.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 #if defined(OS_WIN) | |
| 21 // For version specific disabled tests below (http://crbug.com/230534). | |
| 22 #include "base/win/windows_version.h" | |
| 23 #endif | |
| 24 | |
| 25 class SpellcheckHostMetricsTest : public testing::Test { | |
| 26 public: | |
| 27 SpellcheckHostMetricsTest() { | |
| 28 } | |
| 29 | |
| 30 static void SetUpTestCase() { | |
| 31 base::StatisticsRecorder::Initialize(); | |
| 32 } | |
| 33 | |
| 34 void SetUp() override { metrics_.reset(new SpellCheckHostMetrics); } | |
| 35 | |
| 36 SpellCheckHostMetrics* metrics() { return metrics_.get(); } | |
| 37 void RecordWordCountsForTesting() { metrics_->RecordWordCounts(); } | |
| 38 | |
| 39 private: | |
| 40 base::MessageLoop loop_; | |
| 41 std::unique_ptr<SpellCheckHostMetrics> metrics_; | |
| 42 }; | |
| 43 | |
| 44 TEST_F(SpellcheckHostMetricsTest, RecordEnabledStats) { | |
| 45 const char kMetricName[] = "SpellCheck.Enabled"; | |
| 46 base::HistogramTester histogram_tester1; | |
| 47 | |
| 48 metrics()->RecordEnabledStats(false); | |
| 49 | |
| 50 histogram_tester1.ExpectBucketCount(kMetricName, 0, 1); | |
| 51 histogram_tester1.ExpectBucketCount(kMetricName, 1, 0); | |
| 52 | |
| 53 base::HistogramTester histogram_tester2; | |
| 54 | |
| 55 metrics()->RecordEnabledStats(true); | |
| 56 | |
| 57 histogram_tester2.ExpectBucketCount(kMetricName, 0, 0); | |
| 58 histogram_tester2.ExpectBucketCount(kMetricName, 1, 1); | |
| 59 } | |
| 60 | |
| 61 TEST_F(SpellcheckHostMetricsTest, CustomWordStats) { | |
| 62 #if defined(OS_WIN) | |
| 63 // Failing consistently on Win7. See crbug.com/230534. | |
| 64 if (base::win::GetVersion() >= base::win::VERSION_VISTA) | |
| 65 return; | |
| 66 #endif | |
| 67 SpellCheckHostMetrics::RecordCustomWordCountStats(123); | |
| 68 | |
| 69 // Determine if test failures are due the statistics recorder not being | |
| 70 // available or because the histogram just isn't there: crbug.com/230534. | |
| 71 EXPECT_TRUE(base::StatisticsRecorder::IsActive()); | |
| 72 | |
| 73 base::HistogramTester histogram_tester; | |
| 74 | |
| 75 SpellCheckHostMetrics::RecordCustomWordCountStats(23); | |
| 76 histogram_tester.ExpectBucketCount("SpellCheck.CustomWords", 23, 1); | |
| 77 } | |
| 78 | |
| 79 TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) { | |
| 80 // This test ensures that RecordWordCounts only records metrics if they | |
| 81 // have changed from the last invocation. | |
| 82 const char* const histogram_names[] = { | |
| 83 "SpellCheck.CheckedWords", "SpellCheck.MisspelledWords", | |
| 84 "SpellCheck.ReplacedWords", "SpellCheck.UniqueWords", | |
| 85 "SpellCheck.ShownSuggestions"}; | |
| 86 | |
| 87 // Ensure all histograms exist. | |
| 88 metrics()->RecordCheckedWordStats(base::ASCIIToUTF16("test"), false); | |
| 89 RecordWordCountsForTesting(); | |
| 90 | |
| 91 // Create the tester, taking a snapshot of current histogram samples. | |
| 92 base::HistogramTester histogram_tester; | |
| 93 | |
| 94 // Nothing changed, so this invocation should not affect any histograms. | |
| 95 RecordWordCountsForTesting(); | |
| 96 | |
| 97 // Get samples for all affected histograms. | |
| 98 for (size_t i = 0; i < arraysize(histogram_names); ++i) | |
| 99 histogram_tester.ExpectTotalCount(histogram_names[i], 0); | |
| 100 } | |
| 101 | |
| 102 TEST_F(SpellcheckHostMetricsTest, RecordSpellingServiceStats) { | |
| 103 const char kMetricName[] = "SpellCheck.SpellingService.Enabled"; | |
| 104 base::HistogramTester histogram_tester1; | |
| 105 | |
| 106 metrics()->RecordSpellingServiceStats(false); | |
| 107 | |
| 108 histogram_tester1.ExpectBucketCount(kMetricName, 0, 1); | |
| 109 histogram_tester1.ExpectBucketCount(kMetricName, 1, 0); | |
| 110 | |
| 111 base::HistogramTester histogram_tester2; | |
| 112 | |
| 113 metrics()->RecordSpellingServiceStats(true); | |
| 114 histogram_tester2.ExpectBucketCount(kMetricName, 0, 0); | |
| 115 histogram_tester2.ExpectBucketCount(kMetricName, 1, 1); | |
| 116 } | |
| OLD | NEW |