Home | History | Annotate | Download | only in test
      1 // Copyright 2014 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 "base/test/histogram_tester.h"
      6 
      7 #include "base/memory/scoped_ptr.h"
      8 #include "base/metrics/histogram.h"
      9 #include "base/metrics/histogram_samples.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 namespace base {
     13 
     14 const std::string kHistogram1 = "Test1";
     15 const std::string kHistogram2 = "Test2";
     16 const std::string kHistogram3 = "Test3";
     17 const std::string kHistogram4 = "Test4";
     18 
     19 typedef testing::Test HistogramTesterTest;
     20 
     21 TEST_F(HistogramTesterTest, Scope) {
     22   // Record a histogram before the creation of the recorder.
     23   UMA_HISTOGRAM_BOOLEAN(kHistogram1, true);
     24 
     25   HistogramTester tester;
     26 
     27   // Verify that no histogram is recorded.
     28   scoped_ptr<HistogramSamples> samples(
     29       tester.GetHistogramSamplesSinceCreation(kHistogram1));
     30   EXPECT_FALSE(samples);
     31 
     32   // Record a histogram after the creation of the recorder.
     33   UMA_HISTOGRAM_BOOLEAN(kHistogram1, true);
     34 
     35   // Verify that one histogram is recorded.
     36   samples = tester.GetHistogramSamplesSinceCreation(kHistogram1);
     37   EXPECT_TRUE(samples);
     38   EXPECT_EQ(1, samples->TotalCount());
     39 }
     40 
     41 TEST_F(HistogramTesterTest, TestUniqueSample) {
     42   HistogramTester tester;
     43 
     44   // Record into a sample thrice
     45   UMA_HISTOGRAM_COUNTS_100(kHistogram2, 2);
     46   UMA_HISTOGRAM_COUNTS_100(kHistogram2, 2);
     47   UMA_HISTOGRAM_COUNTS_100(kHistogram2, 2);
     48 
     49   tester.ExpectUniqueSample(kHistogram2, 2, 3);
     50 }
     51 
     52 TEST_F(HistogramTesterTest, TestBucketsSample) {
     53   HistogramTester tester;
     54 
     55   // Record into a sample twice
     56   UMA_HISTOGRAM_COUNTS_100(kHistogram3, 2);
     57   UMA_HISTOGRAM_COUNTS_100(kHistogram3, 2);
     58   UMA_HISTOGRAM_COUNTS_100(kHistogram3, 2);
     59   UMA_HISTOGRAM_COUNTS_100(kHistogram3, 2);
     60   UMA_HISTOGRAM_COUNTS_100(kHistogram3, 3);
     61 
     62   tester.ExpectBucketCount(kHistogram3, 2, 4);
     63   tester.ExpectBucketCount(kHistogram3, 3, 1);
     64 
     65   tester.ExpectTotalCount(kHistogram3, 5);
     66 }
     67 
     68 TEST_F(HistogramTesterTest, TestBucketsSampleWithScope) {
     69   // Record into a sample twice, once before the tester creation and once after.
     70   UMA_HISTOGRAM_COUNTS_100(kHistogram4, 2);
     71 
     72   HistogramTester tester;
     73   UMA_HISTOGRAM_COUNTS_100(kHistogram4, 3);
     74 
     75   tester.ExpectBucketCount(kHistogram4, 2, 0);
     76   tester.ExpectBucketCount(kHistogram4, 3, 1);
     77 
     78   tester.ExpectTotalCount(kHistogram4, 1);
     79 }
     80 
     81 }  // namespace base
     82