Chromium Code Reviews| Index: chrome/browser/spellchecker/spellcheck_host_metrics_unittest.cc |
| diff --git a/chrome/browser/spellchecker/spellcheck_host_metrics_unittest.cc b/chrome/browser/spellchecker/spellcheck_host_metrics_unittest.cc |
| index ab7348a3f44938aa8bd0d5c1ba36f34d155ee894..f70272359f2989c26a3d5ab7704bbebe1c53eb7f 100644 |
| --- a/chrome/browser/spellchecker/spellcheck_host_metrics_unittest.cc |
| +++ b/chrome/browser/spellchecker/spellcheck_host_metrics_unittest.cc |
| @@ -7,10 +7,9 @@ |
| #include "base/basictypes.h" |
| #include "base/memory/scoped_ptr.h" |
| #include "base/message_loop/message_loop.h" |
| -#include "base/metrics/histogram.h" |
| #include "base/metrics/histogram_samples.h" |
| -#include "base/metrics/statistics_recorder.h" |
| #include "base/strings/utf_string_conversions.h" |
| +#include "base/test/histogram_recorder.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| #if defined(OS_WIN) |
| @@ -18,55 +17,51 @@ |
| #include "base/win/windows_version.h" |
| #endif |
| -using base::HistogramBase; |
| -using base::HistogramSamples; |
| -using base::StatisticsRecorder; |
| - |
| class SpellcheckHostMetricsTest : public testing::Test { |
| public: |
| SpellcheckHostMetricsTest() : loop_(base::MessageLoop::TYPE_DEFAULT) { |
| } |
| + static void SetUpTestCase() { |
| + base::HistogramRecorder::Initialize(); |
|
ppi
2013/12/23 16:37:48
StatisticsRecorder::Initialize()?
|
| + } |
| + |
| virtual void SetUp() OVERRIDE { |
| - base::StatisticsRecorder::Initialize(); |
| + ResetHistogramRecorder(); |
| metrics_.reset(new SpellCheckHostMetrics); |
| } |
| + void ResetHistogramRecorder() { |
| + histogram_recorder_.reset(new base::HistogramRecorder()); |
| + } |
| + |
| SpellCheckHostMetrics* metrics() { return metrics_.get(); } |
| void RecordWordCountsForTesting() { metrics_->RecordWordCounts(); } |
| + protected: |
| + scoped_ptr<base::HistogramRecorder> histogram_recorder_; |
|
ppi
2013/12/23 16:37:48
We can drop this field along with ResetHistogramRe
lpromero
2013/12/23 17:22:54
Done.
|
| + |
| private: |
| base::MessageLoop loop_; |
| scoped_ptr<SpellCheckHostMetrics> metrics_; |
| }; |
| TEST_F(SpellcheckHostMetricsTest, RecordEnabledStats) { |
| - scoped_ptr<HistogramSamples> baseline; |
| - HistogramBase* histogram = |
| - StatisticsRecorder::FindHistogram("SpellCheck.Enabled"); |
| - if (histogram) |
| - baseline = histogram->SnapshotSamples(); |
| + const char kMetricName[] = "SpellCheck.Enabled"; |
| metrics()->RecordEnabledStats(false); |
| - histogram = |
| - StatisticsRecorder::FindHistogram("SpellCheck.Enabled"); |
| - ASSERT_TRUE(histogram != NULL); |
| - scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples()); |
| - if (baseline.get()) |
| - samples->Subtract(*baseline); |
| + scoped_ptr<base::HistogramSamples> samples( |
| + histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName)); |
| EXPECT_EQ(1, samples->GetCount(0)); |
| EXPECT_EQ(0, samples->GetCount(1)); |
| - baseline.reset(samples.release()); |
| + ResetHistogramRecorder(); |
| metrics()->RecordEnabledStats(true); |
| - histogram = |
| - StatisticsRecorder::FindHistogram("SpellCheck.Enabled"); |
| - ASSERT_TRUE(histogram != NULL); |
| - samples = histogram->SnapshotSamples(); |
| - samples->Subtract(*baseline); |
| + samples = |
| + histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName); |
| EXPECT_EQ(0, samples->GetCount(0)); |
| EXPECT_EQ(1, samples->GetCount(1)); |
| } |
| @@ -81,21 +76,16 @@ TEST_F(SpellcheckHostMetricsTest, CustomWordStats) { |
| // Determine if test failures are due the statistics recorder not being |
| // available or because the histogram just isn't there: crbug.com/230534. |
| - EXPECT_TRUE(StatisticsRecorder::IsActive()); |
| + EXPECT_TRUE(base::HistogramRecorder::IsActive()); |
| - HistogramBase* histogram = |
| - StatisticsRecorder::FindHistogram("SpellCheck.CustomWords"); |
| - ASSERT_TRUE(histogram != NULL); |
| - scoped_ptr<HistogramSamples> baseline = histogram->SnapshotSamples(); |
| + ResetHistogramRecorder(); |
| SpellCheckHostMetrics::RecordCustomWordCountStats(23); |
| - histogram = |
| - StatisticsRecorder::FindHistogram("SpellCheck.CustomWords"); |
| - ASSERT_TRUE(histogram != NULL); |
| - scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
| - samples->Subtract(*baseline); |
| - EXPECT_EQ(23,samples->sum()); |
| + scoped_ptr<base::HistogramSamples> samples( |
| + histogram_recorder_->GetHistogramSamplesSinceCreation( |
| + "SpellCheck.CustomWords")); |
| + EXPECT_EQ(23, samples->sum()); |
| } |
| TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) { |
| @@ -113,59 +103,37 @@ TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) { |
| metrics()->RecordCheckedWordStats(ASCIIToUTF16("test"), false); |
| RecordWordCountsForTesting(); |
| - // Get baselines for all affected histograms. |
| - scoped_ptr<HistogramSamples> baselines[arraysize(histogramName)]; |
| - for (size_t i = 0; i < arraysize(histogramName); ++i) { |
| - HistogramBase* histogram = |
| - StatisticsRecorder::FindHistogram(histogramName[i]); |
| - if (histogram) |
| - baselines[i] = histogram->SnapshotSamples(); |
| - } |
| + // Restart the recorder. |
| + ResetHistogramRecorder(); |
| // Nothing changed, so this invocation should not affect any histograms. |
| RecordWordCountsForTesting(); |
| // Get samples for all affected histograms. |
| - scoped_ptr<HistogramSamples> samples[arraysize(histogramName)]; |
| + scoped_ptr<base::HistogramSamples> samples; |
| for (size_t i = 0; i < arraysize(histogramName); ++i) { |
| - HistogramBase* histogram = |
| - StatisticsRecorder::FindHistogram(histogramName[i]); |
| - ASSERT_TRUE(histogram != NULL); |
| - samples[i] = histogram->SnapshotSamples(); |
| - if (baselines[i].get()) |
| - samples[i]->Subtract(*baselines[i]); |
| - |
| - EXPECT_EQ(0, samples[i]->TotalCount()); |
| + samples = histogram_recorder_->GetHistogramSamplesSinceCreation( |
| + histogramName[i]); |
| + EXPECT_EQ(0, samples->TotalCount()); |
| } |
| } |
| TEST_F(SpellcheckHostMetricsTest, RecordSpellingServiceStats) { |
| const char kMetricName[] = "SpellCheck.SpellingService.Enabled"; |
| - scoped_ptr<HistogramSamples> baseline; |
| - HistogramBase* histogram = StatisticsRecorder::FindHistogram(kMetricName); |
| - if (histogram) |
| - baseline = histogram->SnapshotSamples(); |
| metrics()->RecordSpellingServiceStats(false); |
| - histogram = |
| - StatisticsRecorder::FindHistogram(kMetricName); |
| - ASSERT_TRUE(histogram != NULL); |
| - scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples()); |
| - if (baseline.get()) |
| - samples->Subtract(*baseline); |
| + scoped_ptr<base::HistogramSamples> samples( |
| + histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName)); |
| EXPECT_EQ(1, samples->GetCount(0)); |
| EXPECT_EQ(0, samples->GetCount(1)); |
| - baseline.reset(samples.release()); |
| + ResetHistogramRecorder(); |
| metrics()->RecordSpellingServiceStats(true); |
| - histogram = |
| - StatisticsRecorder::FindHistogram(kMetricName); |
| - ASSERT_TRUE(histogram != NULL); |
| - samples = histogram->SnapshotSamples(); |
| - samples->Subtract(*baseline); |
| + samples = |
| + histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName); |
| EXPECT_EQ(0, samples->GetCount(0)); |
| EXPECT_EQ(1, samples->GetCount(1)); |
| } |