summaryrefslogtreecommitdiffstats
path: root/chrome/browser/spellchecker/spellcheck_host_metrics_unittest.cc
blob: a2b9bb3c2c2a77d2c78ea76f2d490011a5b20365 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/spellchecker/spellcheck_host_metrics.h"

#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/metrics/histogram_samples.h"
#include "base/metrics/statistics_recorder.h"
#include "base/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::Histogram;
using base::HistogramSamples;
using base::StatisticsRecorder;

class SpellcheckHostMetricsTest : public testing::Test {
 public:
  SpellcheckHostMetricsTest() : loop_(MessageLoop::TYPE_DEFAULT) {
  }

  virtual void SetUp() OVERRIDE {
    base::StatisticsRecorder::Initialize();
    metrics_.reset(new SpellCheckHostMetrics);
  }

  SpellCheckHostMetrics* metrics() { return metrics_.get(); }
  void RecordWordCountsForTesting() { metrics_->RecordWordCounts(); }

 private:
   MessageLoop loop_;
   scoped_ptr<SpellCheckHostMetrics> metrics_;
};

TEST_F(SpellcheckHostMetricsTest, RecordEnabledStats) {
  scoped_ptr<HistogramSamples> baseline;
  Histogram* histogram =
      StatisticsRecorder::FindHistogram("SpellCheck.Enabled");
  if (histogram)
    baseline = histogram->SnapshotSamples();

  metrics()->RecordEnabledStats(false);

  histogram =
      StatisticsRecorder::FindHistogram("SpellCheck.Enabled");
  ASSERT_TRUE(histogram != NULL);
  scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples());
  if (baseline.get())
    samples->Subtract(*baseline);
  EXPECT_EQ(1, samples->GetCount(0));
  EXPECT_EQ(0, samples->GetCount(1));

  baseline.reset(samples.release());

  metrics()->RecordEnabledStats(true);

  histogram =
      StatisticsRecorder::FindHistogram("SpellCheck.Enabled");
  ASSERT_TRUE(histogram != NULL);
  samples = histogram->SnapshotSamples();
  samples->Subtract(*baseline);
  EXPECT_EQ(0, samples->GetCount(0));
  EXPECT_EQ(1, samples->GetCount(1));
}

TEST_F(SpellcheckHostMetricsTest, CustomWordStats) {
  metrics()->RecordCustomWordCountStats(123);

  Histogram* histogram =
      StatisticsRecorder::FindHistogram("SpellCheck.CustomWords");
  ASSERT_TRUE(histogram != NULL);
  scoped_ptr<HistogramSamples> baseline = histogram->SnapshotSamples();

  metrics()->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());
}

TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) {
  // This test ensures that RecordWordCounts only records metrics if they
  // have changed from the last invocation.
  const char* histogramName[] = {
    "SpellCheck.CheckedWords",
    "SpellCheck.MisspelledWords",
    "SpellCheck.ReplacedWords",
    "SpellCheck.UniqueWords",
    "SpellCheck.ShownSuggestions"
  };

  // Ensure all histograms exist.
  metrics()->RecordCheckedWordStats(string16(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) {
    Histogram* histogram =
        StatisticsRecorder::FindHistogram(histogramName[i]);
    if (histogram)
      baselines[i] = histogram->SnapshotSamples();
  }

  // Nothing changed, so this invocation should not affect any histograms.
  RecordWordCountsForTesting();

  // Get samples for all affected histograms.
  scoped_ptr<HistogramSamples> samples[arraysize(histogramName)];
  for (size_t i = 0; i < arraysize(histogramName); ++i) {
    Histogram* 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());
  }
}