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 "chrome/test/base/uma_histogram_helper.h"
8 #include "base/metrics/statistics_recorder.h"
9 #include "base/test/test_timeouts.h"
10 #include "chrome/test/base/ui_test_utils.h"
11 #include "content/public/browser/histogram_fetcher.h"
13 UMAHistogramHelper::UMAHistogramHelper() {
16 void UMAHistogramHelper::Fetch() {
17 base::Closure callback
= base::Bind(&UMAHistogramHelper::FetchCallback
,
18 base::Unretained(this));
20 content::FetchHistogramsAsynchronously(
21 base::MessageLoop::current(),
23 // If this call times out, it means that a child process is not
24 // responding, which is something we should not ignore. The timeout is
25 // set to be longer than the normal browser test timeout so that it will
26 // be prempted by the normal timeout.
27 TestTimeouts::action_max_timeout() * 2);
28 content::RunMessageLoop();
31 void UMAHistogramHelper::ExpectUniqueSample(
32 const std::string
& name
,
33 base::HistogramBase::Sample sample
,
34 base::HistogramBase::Count expected_count
) {
35 base::HistogramBase
* histogram
=
36 base::StatisticsRecorder::FindHistogram(name
);
37 EXPECT_NE(static_cast<base::HistogramBase
*>(NULL
), histogram
)
38 << "Histogram \"" << name
<< "\" does not exist.";
41 scoped_ptr
<base::HistogramSamples
> samples(histogram
->SnapshotSamples());
42 CheckBucketCount(name
, sample
, expected_count
, *samples
);
43 CheckTotalCount(name
, expected_count
, *samples
);
47 void UMAHistogramHelper::ExpectBucketCount(
48 const std::string
& name
,
49 base::HistogramBase::Sample sample
,
50 base::HistogramBase::Count expected_count
) {
51 base::HistogramBase
* histogram
=
52 base::StatisticsRecorder::FindHistogram(name
);
53 EXPECT_NE(static_cast<base::HistogramBase
*>(NULL
), histogram
)
54 << "Histogram \"" << name
<< "\" does not exist.";
57 scoped_ptr
<base::HistogramSamples
> samples(histogram
->SnapshotSamples());
58 CheckBucketCount(name
, sample
, expected_count
, *samples
);
62 void UMAHistogramHelper::ExpectTotalCount(
63 const std::string
& name
,
64 base::HistogramBase::Count count
) {
65 base::HistogramBase
* histogram
=
66 base::StatisticsRecorder::FindHistogram(name
);
68 scoped_ptr
<base::HistogramSamples
> samples(histogram
->SnapshotSamples());
69 CheckTotalCount(name
, count
, *samples
);
71 // No histogram means there were zero samples.
72 EXPECT_EQ(count
, 0) << "Histogram \"" << name
<< "\" does not exist.";
76 void UMAHistogramHelper::FetchCallback() {
77 base::MessageLoopForUI::current()->Quit();
80 void UMAHistogramHelper::CheckBucketCount(
81 const std::string
& name
,
82 base::HistogramBase::Sample sample
,
83 base::HistogramBase::Count expected_count
,
84 base::HistogramSamples
& samples
) {
85 EXPECT_EQ(expected_count
, samples
.GetCount(sample
))
86 << "Histogram \"" << name
87 << "\" does not have the right number of samples (" << expected_count
88 << ") in the expected bucket (" << sample
<< ").";
91 void UMAHistogramHelper::CheckTotalCount(
92 const std::string
& name
,
93 base::HistogramBase::Count expected_count
,
94 base::HistogramSamples
& samples
) {
95 EXPECT_EQ(expected_count
, samples
.TotalCount())
96 << "Histogram \"" << name
97 << "\" does not have the right total number of samples ("
98 << expected_count
<< ").";