Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" | 5 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/metrics/histogram_samples.h" | 10 #include "base/metrics/histogram_samples.h" |
| 11 #include "base/metrics/statistics_recorder.h" | 11 #include "base/metrics/statistics_recorder.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/test/statistics_delta_reader.h" | 13 #include "base/test/histogram_tester.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 #if defined(OS_WIN) | 16 #if defined(OS_WIN) |
| 17 // For version specific disabled tests below (http://crbug.com/230534). | 17 // For version specific disabled tests below (http://crbug.com/230534). |
| 18 #include "base/win/windows_version.h" | 18 #include "base/win/windows_version.h" |
| 19 #endif | 19 #endif |
| 20 | 20 |
| 21 class SpellcheckHostMetricsTest : public testing::Test { | 21 class SpellcheckHostMetricsTest : public testing::Test { |
| 22 public: | 22 public: |
| 23 SpellcheckHostMetricsTest() { | 23 SpellcheckHostMetricsTest() { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 34 SpellCheckHostMetrics* metrics() { return metrics_.get(); } | 34 SpellCheckHostMetrics* metrics() { return metrics_.get(); } |
| 35 void RecordWordCountsForTesting() { metrics_->RecordWordCounts(); } | 35 void RecordWordCountsForTesting() { metrics_->RecordWordCounts(); } |
| 36 | 36 |
| 37 private: | 37 private: |
| 38 base::MessageLoop loop_; | 38 base::MessageLoop loop_; |
| 39 scoped_ptr<SpellCheckHostMetrics> metrics_; | 39 scoped_ptr<SpellCheckHostMetrics> metrics_; |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 TEST_F(SpellcheckHostMetricsTest, RecordEnabledStats) { | 42 TEST_F(SpellcheckHostMetricsTest, RecordEnabledStats) { |
| 43 const char kMetricName[] = "SpellCheck.Enabled"; | 43 const char kMetricName[] = "SpellCheck.Enabled"; |
| 44 base::StatisticsDeltaReader statistics_delta_reader1; | 44 base::HistogramTester histogram_tester1; |
| 45 | 45 |
| 46 metrics()->RecordEnabledStats(false); | 46 metrics()->RecordEnabledStats(false); |
| 47 | 47 |
| 48 scoped_ptr<base::HistogramSamples> samples( | 48 histogram_tester1.ExpectBucketCount(kMetricName, 0, 1); |
| 49 statistics_delta_reader1.GetHistogramSamplesSinceCreation(kMetricName)); | 49 histogram_tester1.ExpectBucketCount(kMetricName, 1, 0); |
| 50 EXPECT_EQ(1, samples->GetCount(0)); | |
| 51 EXPECT_EQ(0, samples->GetCount(1)); | |
| 52 | 50 |
| 53 base::StatisticsDeltaReader statistics_delta_reader2; | 51 base::HistogramTester histogram_tester2; |
| 54 | 52 |
| 55 metrics()->RecordEnabledStats(true); | 53 metrics()->RecordEnabledStats(true); |
| 56 | 54 |
| 57 samples = | 55 histogram_tester2.ExpectBucketCount(kMetricName, 0, 0); |
| 58 statistics_delta_reader2.GetHistogramSamplesSinceCreation(kMetricName); | 56 histogram_tester2.ExpectBucketCount(kMetricName, 1, 1); |
| 59 EXPECT_EQ(0, samples->GetCount(0)); | |
| 60 EXPECT_EQ(1, samples->GetCount(1)); | |
| 61 } | 57 } |
| 62 | 58 |
| 63 TEST_F(SpellcheckHostMetricsTest, CustomWordStats) { | 59 TEST_F(SpellcheckHostMetricsTest, CustomWordStats) { |
| 64 #if defined(OS_WIN) | 60 #if defined(OS_WIN) |
| 65 // Failing consistently on Win7. See crbug.com/230534. | 61 // Failing consistently on Win7. See crbug.com/230534. |
| 66 if (base::win::GetVersion() >= base::win::VERSION_VISTA) | 62 if (base::win::GetVersion() >= base::win::VERSION_VISTA) |
| 67 return; | 63 return; |
| 68 #endif | 64 #endif |
| 69 SpellCheckHostMetrics::RecordCustomWordCountStats(123); | 65 SpellCheckHostMetrics::RecordCustomWordCountStats(123); |
| 70 | 66 |
| 71 // Determine if test failures are due the statistics recorder not being | 67 // Determine if test failures are due the statistics recorder not being |
| 72 // available or because the histogram just isn't there: crbug.com/230534. | 68 // available or because the histogram just isn't there: crbug.com/230534. |
| 73 EXPECT_TRUE(base::StatisticsRecorder::IsActive()); | 69 EXPECT_TRUE(base::StatisticsRecorder::IsActive()); |
| 74 | 70 |
| 75 base::StatisticsDeltaReader statistics_delta_reader; | 71 base::HistogramTester histogram_tester; |
| 76 | 72 |
| 77 SpellCheckHostMetrics::RecordCustomWordCountStats(23); | 73 SpellCheckHostMetrics::RecordCustomWordCountStats(23); |
| 78 | 74 |
| 79 scoped_ptr<base::HistogramSamples> samples( | 75 scoped_ptr<base::HistogramSamples> samples( |
| 80 statistics_delta_reader.GetHistogramSamplesSinceCreation( | 76 histogram_tester.GetBaseHistogramSamples( |
| 81 "SpellCheck.CustomWords")); | 77 "SpellCheck.CustomWords")); |
| 82 EXPECT_EQ(23, samples->sum()); | 78 EXPECT_EQ(23, samples->sum()); |
| 83 } | 79 } |
| 84 | 80 |
| 85 TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) { | 81 TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) { |
| 86 // This test ensures that RecordWordCounts only records metrics if they | 82 // This test ensures that RecordWordCounts only records metrics if they |
| 87 // have changed from the last invocation. | 83 // have changed from the last invocation. |
| 88 const char* histogramName[] = { | 84 const char* histogramName[] = { |
|
Ilya Sherman
2014/07/15 03:56:35
nit: As long as you're here, please update this li
Mike Lerman
2014/07/16 17:29:03
Done.
| |
| 89 "SpellCheck.CheckedWords", | 85 "SpellCheck.CheckedWords", |
| 90 "SpellCheck.MisspelledWords", | 86 "SpellCheck.MisspelledWords", |
| 91 "SpellCheck.ReplacedWords", | 87 "SpellCheck.ReplacedWords", |
| 92 "SpellCheck.UniqueWords", | 88 "SpellCheck.UniqueWords", |
| 93 "SpellCheck.ShownSuggestions" | 89 "SpellCheck.ShownSuggestions" |
| 94 }; | 90 }; |
| 95 | 91 |
| 96 // Ensure all histograms exist. | 92 // Ensure all histograms exist. |
| 97 metrics()->RecordCheckedWordStats(base::ASCIIToUTF16("test"), false); | 93 metrics()->RecordCheckedWordStats(base::ASCIIToUTF16("test"), false); |
| 98 RecordWordCountsForTesting(); | 94 RecordWordCountsForTesting(); |
| 99 | 95 |
| 100 // Start the reader. | 96 // Start the reader. |
|
Ilya Sherman
2014/07/15 03:56:35
nit: Please update (or remove) this comment.
Mike Lerman
2014/07/16 17:29:03
Done.
| |
| 101 base::StatisticsDeltaReader statistics_delta_reader; | 97 base::HistogramTester histogram_tester; |
| 102 | 98 |
| 103 // Nothing changed, so this invocation should not affect any histograms. | 99 // Nothing changed, so this invocation should not affect any histograms. |
| 104 RecordWordCountsForTesting(); | 100 RecordWordCountsForTesting(); |
| 105 | 101 |
| 106 // Get samples for all affected histograms. | 102 // Get samples for all affected histograms. |
| 107 scoped_ptr<base::HistogramSamples> samples; | 103 for (size_t i = 0; i < arraysize(histogramName); ++i) |
| 108 for (size_t i = 0; i < arraysize(histogramName); ++i) { | 104 histogram_tester.ExpectTotalCount(histogramName[i], 0); |
| 109 samples = statistics_delta_reader.GetHistogramSamplesSinceCreation( | |
| 110 histogramName[i]); | |
| 111 EXPECT_EQ(0, samples->TotalCount()); | |
| 112 } | |
| 113 } | 105 } |
| 114 | 106 |
| 115 TEST_F(SpellcheckHostMetricsTest, RecordSpellingServiceStats) { | 107 TEST_F(SpellcheckHostMetricsTest, RecordSpellingServiceStats) { |
| 116 const char kMetricName[] = "SpellCheck.SpellingService.Enabled"; | 108 const char kMetricName[] = "SpellCheck.SpellingService.Enabled"; |
| 117 base::StatisticsDeltaReader statistics_delta_reader1; | 109 base::HistogramTester histogram_tester1; |
| 118 | 110 |
| 119 metrics()->RecordSpellingServiceStats(false); | 111 metrics()->RecordSpellingServiceStats(false); |
| 120 | 112 |
| 121 scoped_ptr<base::HistogramSamples> samples( | 113 histogram_tester1.ExpectBucketCount(kMetricName, 0, 1); |
| 122 statistics_delta_reader1.GetHistogramSamplesSinceCreation(kMetricName)); | 114 histogram_tester1.ExpectBucketCount(kMetricName, 1, 0); |
| 123 EXPECT_EQ(1, samples->GetCount(0)); | |
| 124 EXPECT_EQ(0, samples->GetCount(1)); | |
| 125 | 115 |
| 126 base::StatisticsDeltaReader statistics_delta_reader2; | 116 base::HistogramTester histogram_tester2; |
| 127 | 117 |
| 128 metrics()->RecordSpellingServiceStats(true); | 118 metrics()->RecordSpellingServiceStats(true); |
| 129 | 119 histogram_tester2.ExpectBucketCount(kMetricName, 0, 0); |
| 130 samples = | 120 histogram_tester2.ExpectBucketCount(kMetricName, 1, 1); |
| 131 statistics_delta_reader2.GetHistogramSamplesSinceCreation(kMetricName); | |
| 132 EXPECT_EQ(0, samples->GetCount(0)); | |
| 133 EXPECT_EQ(1, samples->GetCount(1)); | |
| 134 } | 121 } |
| OLD | NEW |