Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: chrome/browser/spellchecker/spellcheck_host_metrics_unittest.cc

Issue 379283002: Rework UMAHistogramHelper and StatisticsDeltaReader into [Chrome]HistogramTester. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_samples.h" 10 #include "base/metrics/histogram_samples.h"
11 #include "base/metrics/statistics_recorder.h" 11 #include "base/metrics/statistics_recorder.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "base/test/statistics_delta_reader.h" 13 #include "base/test/histogram_tester.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 class SpellcheckHostMetricsTest : public testing::Test { 21 class SpellcheckHostMetricsTest : public testing::Test {
22 public: 22 public:
23 SpellcheckHostMetricsTest() { 23 SpellcheckHostMetricsTest() {
(...skipping 10 matching lines...) Expand all
34 SpellCheckHostMetrics* metrics() { return metrics_.get(); } 34 SpellCheckHostMetrics* metrics() { return metrics_.get(); }
35 void RecordWordCountsForTesting() { metrics_->RecordWordCounts(); } 35 void RecordWordCountsForTesting() { metrics_->RecordWordCounts(); }
36 36
37 private: 37 private:
38 base::MessageLoop loop_; 38 base::MessageLoop loop_;
39 scoped_ptr<SpellCheckHostMetrics> metrics_; 39 scoped_ptr<SpellCheckHostMetrics> metrics_;
40 }; 40 };
41 41
42 TEST_F(SpellcheckHostMetricsTest, RecordEnabledStats) { 42 TEST_F(SpellcheckHostMetricsTest, RecordEnabledStats) {
43 const char kMetricName[] = "SpellCheck.Enabled"; 43 const char kMetricName[] = "SpellCheck.Enabled";
44 base::StatisticsDeltaReader statistics_delta_reader1; 44 base::HistogramTester histogram_tester1;
45 45
46 metrics()->RecordEnabledStats(false); 46 metrics()->RecordEnabledStats(false);
47 47
48 scoped_ptr<base::HistogramSamples> samples( 48 histogram_tester1.ExpectBucketCount(kMetricName, 0, 1);
49 statistics_delta_reader1.GetHistogramSamplesSinceCreation(kMetricName)); 49 histogram_tester1.ExpectBucketCount(kMetricName, 1, 0);
50 EXPECT_EQ(1, samples->GetCount(0));
51 EXPECT_EQ(0, samples->GetCount(1));
52 50
53 base::StatisticsDeltaReader statistics_delta_reader2; 51 base::HistogramTester histogram_tester2;
54 52
55 metrics()->RecordEnabledStats(true); 53 metrics()->RecordEnabledStats(true);
56 54
57 samples = 55 histogram_tester2.ExpectBucketCount(kMetricName, 0, 0);
58 statistics_delta_reader2.GetHistogramSamplesSinceCreation(kMetricName); 56 histogram_tester2.ExpectBucketCount(kMetricName, 1, 1);
59 EXPECT_EQ(0, samples->GetCount(0));
60 EXPECT_EQ(1, samples->GetCount(1));
61 } 57 }
62 58
63 TEST_F(SpellcheckHostMetricsTest, CustomWordStats) { 59 TEST_F(SpellcheckHostMetricsTest, CustomWordStats) {
64 #if defined(OS_WIN) 60 #if defined(OS_WIN)
65 // Failing consistently on Win7. See crbug.com/230534. 61 // Failing consistently on Win7. See crbug.com/230534.
66 if (base::win::GetVersion() >= base::win::VERSION_VISTA) 62 if (base::win::GetVersion() >= base::win::VERSION_VISTA)
67 return; 63 return;
68 #endif 64 #endif
69 SpellCheckHostMetrics::RecordCustomWordCountStats(123); 65 SpellCheckHostMetrics::RecordCustomWordCountStats(123);
70 66
71 // Determine if test failures are due the statistics recorder not being 67 // Determine if test failures are due the statistics recorder not being
72 // available or because the histogram just isn't there: crbug.com/230534. 68 // available or because the histogram just isn't there: crbug.com/230534.
73 EXPECT_TRUE(base::StatisticsRecorder::IsActive()); 69 EXPECT_TRUE(base::StatisticsRecorder::IsActive());
74 70
75 base::StatisticsDeltaReader statistics_delta_reader; 71 base::HistogramTester histogram_tester;
76 72
77 SpellCheckHostMetrics::RecordCustomWordCountStats(23); 73 SpellCheckHostMetrics::RecordCustomWordCountStats(23);
78 74 histogram_tester.ExpectBucketCount("SpellCheck.CustomWords", 23, 1);
79 scoped_ptr<base::HistogramSamples> samples(
80 statistics_delta_reader.GetHistogramSamplesSinceCreation(
81 "SpellCheck.CustomWords"));
82 EXPECT_EQ(23, samples->sum());
83 } 75 }
84 76
85 TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) { 77 TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) {
86 // This test ensures that RecordWordCounts only records metrics if they 78 // This test ensures that RecordWordCounts only records metrics if they
87 // have changed from the last invocation. 79 // have changed from the last invocation.
88 const char* histogramName[] = { 80 const char* const histogram_names[] = {
89 "SpellCheck.CheckedWords", 81 "SpellCheck.CheckedWords", "SpellCheck.MisspelledWords",
90 "SpellCheck.MisspelledWords", 82 "SpellCheck.ReplacedWords", "SpellCheck.UniqueWords",
91 "SpellCheck.ReplacedWords", 83 "SpellCheck.ShownSuggestions"};
92 "SpellCheck.UniqueWords",
93 "SpellCheck.ShownSuggestions"
94 };
95 84
96 // Ensure all histograms exist. 85 // Ensure all histograms exist.
97 metrics()->RecordCheckedWordStats(base::ASCIIToUTF16("test"), false); 86 metrics()->RecordCheckedWordStats(base::ASCIIToUTF16("test"), false);
98 RecordWordCountsForTesting(); 87 RecordWordCountsForTesting();
99 88
100 // Start the reader. 89 // Create the tester, taking a snapshot of current histogram samples.
101 base::StatisticsDeltaReader statistics_delta_reader; 90 base::HistogramTester histogram_tester;
102 91
103 // Nothing changed, so this invocation should not affect any histograms. 92 // Nothing changed, so this invocation should not affect any histograms.
104 RecordWordCountsForTesting(); 93 RecordWordCountsForTesting();
105 94
106 // Get samples for all affected histograms. 95 // Get samples for all affected histograms.
107 scoped_ptr<base::HistogramSamples> samples; 96 for (size_t i = 0; i < arraysize(histogram_names); ++i)
108 for (size_t i = 0; i < arraysize(histogramName); ++i) { 97 histogram_tester.ExpectTotalCount(histogram_names[i], 0);
109 samples = statistics_delta_reader.GetHistogramSamplesSinceCreation(
110 histogramName[i]);
111 EXPECT_EQ(0, samples->TotalCount());
112 }
113 } 98 }
114 99
115 TEST_F(SpellcheckHostMetricsTest, RecordSpellingServiceStats) { 100 TEST_F(SpellcheckHostMetricsTest, RecordSpellingServiceStats) {
116 const char kMetricName[] = "SpellCheck.SpellingService.Enabled"; 101 const char kMetricName[] = "SpellCheck.SpellingService.Enabled";
117 base::StatisticsDeltaReader statistics_delta_reader1; 102 base::HistogramTester histogram_tester1;
118 103
119 metrics()->RecordSpellingServiceStats(false); 104 metrics()->RecordSpellingServiceStats(false);
120 105
121 scoped_ptr<base::HistogramSamples> samples( 106 histogram_tester1.ExpectBucketCount(kMetricName, 0, 1);
122 statistics_delta_reader1.GetHistogramSamplesSinceCreation(kMetricName)); 107 histogram_tester1.ExpectBucketCount(kMetricName, 1, 0);
123 EXPECT_EQ(1, samples->GetCount(0));
124 EXPECT_EQ(0, samples->GetCount(1));
125 108
126 base::StatisticsDeltaReader statistics_delta_reader2; 109 base::HistogramTester histogram_tester2;
127 110
128 metrics()->RecordSpellingServiceStats(true); 111 metrics()->RecordSpellingServiceStats(true);
129 112 histogram_tester2.ExpectBucketCount(kMetricName, 0, 0);
130 samples = 113 histogram_tester2.ExpectBucketCount(kMetricName, 1, 1);
131 statistics_delta_reader2.GetHistogramSamplesSinceCreation(kMetricName);
132 EXPECT_EQ(0, samples->GetCount(0));
133 EXPECT_EQ(1, samples->GetCount(1));
134 } 114 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698