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