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