| 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.h" | |
| 11 #include "base/metrics/histogram_samples.h" | 10 #include "base/metrics/histogram_samples.h" |
| 12 #include "base/metrics/statistics_recorder.h" | 11 #include "base/metrics/statistics_recorder.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/test/statistics_delta_reader.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 using base::HistogramBase; | |
| 22 using base::HistogramSamples; | |
| 23 using base::StatisticsRecorder; | |
| 24 | |
| 25 class SpellcheckHostMetricsTest : public testing::Test { | 21 class SpellcheckHostMetricsTest : public testing::Test { |
| 26 public: | 22 public: |
| 27 SpellcheckHostMetricsTest() : loop_(base::MessageLoop::TYPE_DEFAULT) { | 23 SpellcheckHostMetricsTest() : loop_(base::MessageLoop::TYPE_DEFAULT) { |
| 28 } | 24 } |
| 29 | 25 |
| 26 static void SetUpTestCase() { |
| 27 base::StatisticsRecorder::Initialize(); |
| 28 } |
| 29 |
| 30 virtual void SetUp() OVERRIDE { | 30 virtual void SetUp() OVERRIDE { |
| 31 base::StatisticsRecorder::Initialize(); | |
| 32 metrics_.reset(new SpellCheckHostMetrics); | 31 metrics_.reset(new SpellCheckHostMetrics); |
| 33 } | 32 } |
| 34 | 33 |
| 35 SpellCheckHostMetrics* metrics() { return metrics_.get(); } | 34 SpellCheckHostMetrics* metrics() { return metrics_.get(); } |
| 36 void RecordWordCountsForTesting() { metrics_->RecordWordCounts(); } | 35 void RecordWordCountsForTesting() { metrics_->RecordWordCounts(); } |
| 37 | 36 |
| 38 private: | 37 private: |
| 39 base::MessageLoop loop_; | 38 base::MessageLoop loop_; |
| 40 scoped_ptr<SpellCheckHostMetrics> metrics_; | 39 scoped_ptr<SpellCheckHostMetrics> metrics_; |
| 41 }; | 40 }; |
| 42 | 41 |
| 43 TEST_F(SpellcheckHostMetricsTest, RecordEnabledStats) { | 42 TEST_F(SpellcheckHostMetricsTest, RecordEnabledStats) { |
| 44 scoped_ptr<HistogramSamples> baseline; | 43 const char kMetricName[] = "SpellCheck.Enabled"; |
| 45 HistogramBase* histogram = | 44 base::StatisticsDeltaReader statistics_delta_reader1; |
| 46 StatisticsRecorder::FindHistogram("SpellCheck.Enabled"); | |
| 47 if (histogram) | |
| 48 baseline = histogram->SnapshotSamples(); | |
| 49 | 45 |
| 50 metrics()->RecordEnabledStats(false); | 46 metrics()->RecordEnabledStats(false); |
| 51 | 47 |
| 52 histogram = | 48 scoped_ptr<base::HistogramSamples> samples( |
| 53 StatisticsRecorder::FindHistogram("SpellCheck.Enabled"); | 49 statistics_delta_reader1.GetHistogramSamplesSinceCreation(kMetricName)); |
| 54 ASSERT_TRUE(histogram != NULL); | |
| 55 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples()); | |
| 56 if (baseline.get()) | |
| 57 samples->Subtract(*baseline); | |
| 58 EXPECT_EQ(1, samples->GetCount(0)); | 50 EXPECT_EQ(1, samples->GetCount(0)); |
| 59 EXPECT_EQ(0, samples->GetCount(1)); | 51 EXPECT_EQ(0, samples->GetCount(1)); |
| 60 | 52 |
| 61 baseline.reset(samples.release()); | 53 base::StatisticsDeltaReader statistics_delta_reader2; |
| 62 | 54 |
| 63 metrics()->RecordEnabledStats(true); | 55 metrics()->RecordEnabledStats(true); |
| 64 | 56 |
| 65 histogram = | 57 samples = |
| 66 StatisticsRecorder::FindHistogram("SpellCheck.Enabled"); | 58 statistics_delta_reader2.GetHistogramSamplesSinceCreation(kMetricName); |
| 67 ASSERT_TRUE(histogram != NULL); | |
| 68 samples = histogram->SnapshotSamples(); | |
| 69 samples->Subtract(*baseline); | |
| 70 EXPECT_EQ(0, samples->GetCount(0)); | 59 EXPECT_EQ(0, samples->GetCount(0)); |
| 71 EXPECT_EQ(1, samples->GetCount(1)); | 60 EXPECT_EQ(1, samples->GetCount(1)); |
| 72 } | 61 } |
| 73 | 62 |
| 74 TEST_F(SpellcheckHostMetricsTest, CustomWordStats) { | 63 TEST_F(SpellcheckHostMetricsTest, CustomWordStats) { |
| 75 #if defined(OS_WIN) | 64 #if defined(OS_WIN) |
| 76 // Failing consistently on Win7. See crbug.com/230534. | 65 // Failing consistently on Win7. See crbug.com/230534. |
| 77 if (base::win::GetVersion() >= base::win::VERSION_VISTA) | 66 if (base::win::GetVersion() >= base::win::VERSION_VISTA) |
| 78 return; | 67 return; |
| 79 #endif | 68 #endif |
| 80 SpellCheckHostMetrics::RecordCustomWordCountStats(123); | 69 SpellCheckHostMetrics::RecordCustomWordCountStats(123); |
| 81 | 70 |
| 82 // Determine if test failures are due the statistics recorder not being | 71 // Determine if test failures are due the statistics recorder not being |
| 83 // available or because the histogram just isn't there: crbug.com/230534. | 72 // available or because the histogram just isn't there: crbug.com/230534. |
| 84 EXPECT_TRUE(StatisticsRecorder::IsActive()); | 73 EXPECT_TRUE(base::StatisticsRecorder::IsActive()); |
| 85 | 74 |
| 86 HistogramBase* histogram = | 75 base::StatisticsDeltaReader statistics_delta_reader; |
| 87 StatisticsRecorder::FindHistogram("SpellCheck.CustomWords"); | |
| 88 ASSERT_TRUE(histogram != NULL); | |
| 89 scoped_ptr<HistogramSamples> baseline = histogram->SnapshotSamples(); | |
| 90 | 76 |
| 91 SpellCheckHostMetrics::RecordCustomWordCountStats(23); | 77 SpellCheckHostMetrics::RecordCustomWordCountStats(23); |
| 92 histogram = | |
| 93 StatisticsRecorder::FindHistogram("SpellCheck.CustomWords"); | |
| 94 ASSERT_TRUE(histogram != NULL); | |
| 95 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); | |
| 96 | 78 |
| 97 samples->Subtract(*baseline); | 79 scoped_ptr<base::HistogramSamples> samples( |
| 98 EXPECT_EQ(23,samples->sum()); | 80 statistics_delta_reader.GetHistogramSamplesSinceCreation( |
| 81 "SpellCheck.CustomWords")); |
| 82 EXPECT_EQ(23, samples->sum()); |
| 99 } | 83 } |
| 100 | 84 |
| 101 TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) { | 85 TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) { |
| 102 // This test ensures that RecordWordCounts only records metrics if they | 86 // This test ensures that RecordWordCounts only records metrics if they |
| 103 // have changed from the last invocation. | 87 // have changed from the last invocation. |
| 104 const char* histogramName[] = { | 88 const char* histogramName[] = { |
| 105 "SpellCheck.CheckedWords", | 89 "SpellCheck.CheckedWords", |
| 106 "SpellCheck.MisspelledWords", | 90 "SpellCheck.MisspelledWords", |
| 107 "SpellCheck.ReplacedWords", | 91 "SpellCheck.ReplacedWords", |
| 108 "SpellCheck.UniqueWords", | 92 "SpellCheck.UniqueWords", |
| 109 "SpellCheck.ShownSuggestions" | 93 "SpellCheck.ShownSuggestions" |
| 110 }; | 94 }; |
| 111 | 95 |
| 112 // Ensure all histograms exist. | 96 // Ensure all histograms exist. |
| 113 metrics()->RecordCheckedWordStats(ASCIIToUTF16("test"), false); | 97 metrics()->RecordCheckedWordStats(ASCIIToUTF16("test"), false); |
| 114 RecordWordCountsForTesting(); | 98 RecordWordCountsForTesting(); |
| 115 | 99 |
| 116 // Get baselines for all affected histograms. | 100 // Start the reader. |
| 117 scoped_ptr<HistogramSamples> baselines[arraysize(histogramName)]; | 101 base::StatisticsDeltaReader statistics_delta_reader; |
| 118 for (size_t i = 0; i < arraysize(histogramName); ++i) { | |
| 119 HistogramBase* histogram = | |
| 120 StatisticsRecorder::FindHistogram(histogramName[i]); | |
| 121 if (histogram) | |
| 122 baselines[i] = histogram->SnapshotSamples(); | |
| 123 } | |
| 124 | 102 |
| 125 // Nothing changed, so this invocation should not affect any histograms. | 103 // Nothing changed, so this invocation should not affect any histograms. |
| 126 RecordWordCountsForTesting(); | 104 RecordWordCountsForTesting(); |
| 127 | 105 |
| 128 // Get samples for all affected histograms. | 106 // Get samples for all affected histograms. |
| 129 scoped_ptr<HistogramSamples> samples[arraysize(histogramName)]; | 107 scoped_ptr<base::HistogramSamples> samples; |
| 130 for (size_t i = 0; i < arraysize(histogramName); ++i) { | 108 for (size_t i = 0; i < arraysize(histogramName); ++i) { |
| 131 HistogramBase* histogram = | 109 samples = statistics_delta_reader.GetHistogramSamplesSinceCreation( |
| 132 StatisticsRecorder::FindHistogram(histogramName[i]); | 110 histogramName[i]); |
| 133 ASSERT_TRUE(histogram != NULL); | 111 EXPECT_EQ(0, samples->TotalCount()); |
| 134 samples[i] = histogram->SnapshotSamples(); | |
| 135 if (baselines[i].get()) | |
| 136 samples[i]->Subtract(*baselines[i]); | |
| 137 | |
| 138 EXPECT_EQ(0, samples[i]->TotalCount()); | |
| 139 } | 112 } |
| 140 } | 113 } |
| 141 | 114 |
| 142 TEST_F(SpellcheckHostMetricsTest, RecordSpellingServiceStats) { | 115 TEST_F(SpellcheckHostMetricsTest, RecordSpellingServiceStats) { |
| 143 const char kMetricName[] = "SpellCheck.SpellingService.Enabled"; | 116 const char kMetricName[] = "SpellCheck.SpellingService.Enabled"; |
| 144 scoped_ptr<HistogramSamples> baseline; | 117 base::StatisticsDeltaReader statistics_delta_reader1; |
| 145 HistogramBase* histogram = StatisticsRecorder::FindHistogram(kMetricName); | |
| 146 if (histogram) | |
| 147 baseline = histogram->SnapshotSamples(); | |
| 148 | 118 |
| 149 metrics()->RecordSpellingServiceStats(false); | 119 metrics()->RecordSpellingServiceStats(false); |
| 150 | 120 |
| 151 histogram = | 121 scoped_ptr<base::HistogramSamples> samples( |
| 152 StatisticsRecorder::FindHistogram(kMetricName); | 122 statistics_delta_reader1.GetHistogramSamplesSinceCreation(kMetricName)); |
| 153 ASSERT_TRUE(histogram != NULL); | |
| 154 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples()); | |
| 155 if (baseline.get()) | |
| 156 samples->Subtract(*baseline); | |
| 157 EXPECT_EQ(1, samples->GetCount(0)); | 123 EXPECT_EQ(1, samples->GetCount(0)); |
| 158 EXPECT_EQ(0, samples->GetCount(1)); | 124 EXPECT_EQ(0, samples->GetCount(1)); |
| 159 | 125 |
| 160 baseline.reset(samples.release()); | 126 base::StatisticsDeltaReader statistics_delta_reader2; |
| 161 | 127 |
| 162 metrics()->RecordSpellingServiceStats(true); | 128 metrics()->RecordSpellingServiceStats(true); |
| 163 | 129 |
| 164 histogram = | 130 samples = |
| 165 StatisticsRecorder::FindHistogram(kMetricName); | 131 statistics_delta_reader2.GetHistogramSamplesSinceCreation(kMetricName); |
| 166 ASSERT_TRUE(histogram != NULL); | |
| 167 samples = histogram->SnapshotSamples(); | |
| 168 samples->Subtract(*baseline); | |
| 169 EXPECT_EQ(0, samples->GetCount(0)); | 132 EXPECT_EQ(0, samples->GetCount(0)); |
| 170 EXPECT_EQ(1, samples->GetCount(1)); | 133 EXPECT_EQ(1, samples->GetCount(1)); |
| 171 } | 134 } |
| OLD | NEW |