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