• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "chrome/browser/chromeos/net/network_portal_detector_test_utils.h"
6 
7 #include <algorithm>
8 
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/metrics/histogram_base.h"
12 #include "base/metrics/histogram_samples.h"
13 #include "base/metrics/statistics_recorder.h"
14 
15 namespace chromeos {
16 
EnumHistogramChecker(const std::string & histogram,int count,base::HistogramSamples * base)17 EnumHistogramChecker::EnumHistogramChecker(const std::string& histogram,
18                                            int count,
19                                            base::HistogramSamples* base)
20     : histogram_(histogram), expect_(count), base_(base) {}
21 
~EnumHistogramChecker()22 EnumHistogramChecker::~EnumHistogramChecker() {}
23 
Expect(int key,int value)24 EnumHistogramChecker* EnumHistogramChecker::Expect(int key, int value) {
25   expect_[key] = value;
26   return this;
27 }
28 
Check()29 bool EnumHistogramChecker::Check() {
30   bool empty = false;
31   size_t num_zeroes =
32       static_cast<size_t>(std::count(expect_.begin(), expect_.end(), 0));
33   if (num_zeroes == expect_.size())
34     empty = true;
35   base::HistogramBase* histogram =
36       base::StatisticsRecorder::FindHistogram(histogram_);
37   if (!histogram) {
38     if (!empty) {
39       LOG(ERROR) << "Non-empty expectations for " << histogram_ << " "
40                  << "which does not exists.";
41       return false;
42     }
43     return true;
44   }
45   scoped_ptr<base::HistogramSamples> samples = histogram->SnapshotSamples();
46   if (!samples.get()) {
47     if (!empty) {
48       LOG(ERROR) << "Non-empty expectations for " << histogram_ << " "
49                  << "for which samples do not exist.";
50       return false;
51     }
52     return true;
53   }
54 
55   bool ok = true;
56   for (size_t i = 0; i < expect_.size(); ++i) {
57     const int base = base_ ? base_->GetCount(i) : 0;
58     const int actual = samples->GetCount(i) - base;
59     if (actual != expect_[i]) {
60       LOG(ERROR) << "Histogram: " << histogram_ << ", value #" << i << ", "
61                  << "expected: " << expect_[i] << ", actual: " << actual;
62       ok = false;
63     }
64   }
65   return ok;
66 }
67 
68 }  // namespace chromeos
69