This source file includes following definitions.
- Fetch
- ExpectUniqueSample
- ExpectTotalCount
- FetchCallback
- CheckBucketCount
- CheckTotalCount
#include "chrome/test/base/uma_histogram_helper.h"
#include "base/bind.h"
#include "base/metrics/statistics_recorder.h"
#include "base/test/test_timeouts.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/histogram_fetcher.h"
UMAHistogramHelper::UMAHistogramHelper() {
}
void UMAHistogramHelper::Fetch() {
base::Closure callback = base::Bind(&UMAHistogramHelper::FetchCallback,
base::Unretained(this));
content::FetchHistogramsAsynchronously(
base::MessageLoop::current(),
callback,
TestTimeouts::action_max_timeout() * 2);
content::RunMessageLoop();
}
void UMAHistogramHelper::ExpectUniqueSample(
const std::string& name,
base::HistogramBase::Sample sample,
base::HistogramBase::Count expected_count) {
base::HistogramBase* histogram =
base::StatisticsRecorder::FindHistogram(name);
EXPECT_NE(static_cast<base::HistogramBase*>(NULL), histogram)
<< "Histogram \"" << name << "\" does not exist.";
if (histogram) {
scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
CheckBucketCount(name, sample, expected_count, *samples);
CheckTotalCount(name, expected_count, *samples);
}
}
void UMAHistogramHelper::ExpectTotalCount(
const std::string& name,
base::HistogramBase::Count count) {
base::HistogramBase* histogram =
base::StatisticsRecorder::FindHistogram(name);
if (histogram) {
scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
CheckTotalCount(name, count, *samples);
} else {
EXPECT_EQ(count, 0) << "Histogram \"" << name << "\" does not exist.";
}
}
void UMAHistogramHelper::FetchCallback() {
base::MessageLoopForUI::current()->Quit();
}
void UMAHistogramHelper::CheckBucketCount(
const std::string& name,
base::HistogramBase::Sample sample,
base::HistogramBase::Count expected_count,
base::HistogramSamples& samples) {
EXPECT_EQ(expected_count, samples.GetCount(sample))
<< "Histogram \"" << name
<< "\" does not have the right number of samples (" << expected_count
<< ") in the expected bucket (" << sample << ").";
}
void UMAHistogramHelper::CheckTotalCount(
const std::string& name,
base::HistogramBase::Count expected_count,
base::HistogramSamples& samples) {
EXPECT_EQ(expected_count, samples.TotalCount())
<< "Histogram \"" << name
<< "\" does not have the right total number of samples ("
<< expected_count << ").";
}