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