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