Proper positioning of new dialog style print preview.
[chromium-blink-merge.git] / base / metrics / sparse_histogram_unittest.cc
blobd9db5d08ad94d69f4657bdf18c460bd977175806
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.
5 #include <string>
7 #include "base/memory/scoped_ptr.h"
8 #include "base/metrics/histogram_base.h"
9 #include "base/metrics/histogram_samples.h"
10 #include "base/metrics/sample_map.h"
11 #include "base/metrics/sparse_histogram.h"
12 #include "base/metrics/statistics_recorder.h"
13 #include "base/pickle.h"
14 #include "base/stringprintf.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace base {
19 class SparseHistogramTest : public testing::Test {
20 protected:
21 virtual void SetUp() {
22 // Each test will have a clean state (no Histogram / BucketRanges
23 // registered).
24 InitializeStatisticsRecorder();
27 virtual void TearDown() {
28 UninitializeStatisticsRecorder();
31 void InitializeStatisticsRecorder() {
32 statistics_recorder_ = new StatisticsRecorder();
35 void UninitializeStatisticsRecorder() {
36 delete statistics_recorder_;
37 statistics_recorder_ = NULL;
40 scoped_ptr<SparseHistogram> NewSparseHistogram(const std::string& name) {
41 return scoped_ptr<SparseHistogram>(new SparseHistogram(name));
44 StatisticsRecorder* statistics_recorder_;
47 TEST_F(SparseHistogramTest, BasicTest) {
48 scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse"));
49 scoped_ptr<HistogramSamples> snapshot(histogram->SnapshotSamples());
50 EXPECT_EQ(0, snapshot->TotalCount());
51 EXPECT_EQ(0, snapshot->sum());
53 histogram->Add(100);
54 scoped_ptr<HistogramSamples> snapshot1(histogram->SnapshotSamples());
55 EXPECT_EQ(1, snapshot1->TotalCount());
56 EXPECT_EQ(1, snapshot1->GetCount(100));
58 histogram->Add(100);
59 histogram->Add(101);
60 scoped_ptr<HistogramSamples> snapshot2(histogram->SnapshotSamples());
61 EXPECT_EQ(3, snapshot2->TotalCount());
62 EXPECT_EQ(2, snapshot2->GetCount(100));
63 EXPECT_EQ(1, snapshot2->GetCount(101));
66 TEST_F(SparseHistogramTest, MacroBasicTest) {
67 HISTOGRAM_SPARSE_SLOWLY("Sparse", 100);
68 HISTOGRAM_SPARSE_SLOWLY("Sparse", 200);
69 HISTOGRAM_SPARSE_SLOWLY("Sparse", 100);
71 StatisticsRecorder::Histograms histograms;
72 StatisticsRecorder::GetHistograms(&histograms);
74 ASSERT_EQ(1U, histograms.size());
75 HistogramBase* sparse_histogram = histograms[0];
77 EXPECT_EQ(SPARSE_HISTOGRAM, sparse_histogram->GetHistogramType());
78 EXPECT_EQ("Sparse", sparse_histogram->histogram_name());
79 EXPECT_EQ(HistogramBase::kNoFlags, sparse_histogram->flags());
81 scoped_ptr<HistogramSamples> samples = sparse_histogram->SnapshotSamples();
82 EXPECT_EQ(3, samples->TotalCount());
83 EXPECT_EQ(2, samples->GetCount(100));
84 EXPECT_EQ(1, samples->GetCount(200));
87 TEST_F(SparseHistogramTest, MacroUmaTest) {
88 UMA_HISTOGRAM_SPARSE_SLOWLY("Uma", 100);
90 StatisticsRecorder::Histograms histograms;
91 StatisticsRecorder::GetHistograms(&histograms);
93 ASSERT_EQ(1U, histograms.size());
94 HistogramBase* sparse_histogram = histograms[0];
96 EXPECT_EQ("Uma", sparse_histogram->histogram_name());
97 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag,
98 sparse_histogram->flags());
101 TEST_F(SparseHistogramTest, MacroInLoopTest) {
102 // Unlike the macros in histogram.h, SparseHistogram macros can have a
103 // variable as histogram name.
104 for (int i = 0; i < 2; i++) {
105 std::string name = StringPrintf("Sparse%d", i + 1);
106 UMA_HISTOGRAM_SPARSE_SLOWLY(name, 100);
109 StatisticsRecorder::Histograms histograms;
110 StatisticsRecorder::GetHistograms(&histograms);
111 ASSERT_EQ(2U, histograms.size());
113 std::string name1 = histograms[0]->histogram_name();
114 std::string name2 = histograms[1]->histogram_name();
115 EXPECT_TRUE(("Sparse1" == name1 && "Sparse2" == name2) ||
116 ("Sparse2" == name1 && "Sparse1" == name2));
119 TEST_F(SparseHistogramTest, Serialize) {
120 scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse"));
121 histogram->SetFlags(HistogramBase::kIPCSerializationSourceFlag);
123 Pickle pickle;
124 histogram->SerializeInfo(&pickle);
126 PickleIterator iter(pickle);
128 int type;
129 EXPECT_TRUE(iter.ReadInt(&type));
130 EXPECT_EQ(SPARSE_HISTOGRAM, type);
132 std::string name;
133 EXPECT_TRUE(iter.ReadString(&name));
134 EXPECT_EQ("Sparse", name);
136 int flag;
137 EXPECT_TRUE(iter.ReadInt(&flag));
138 EXPECT_EQ(HistogramBase::kIPCSerializationSourceFlag, flag);
140 // No more data in the pickle.
141 EXPECT_FALSE(iter.SkipBytes(1));
144 } // namespace base