1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "base/metrics/statistics_recorder.h"
#include "base/test/histogram_tester.h"
#include "chrome/browser/chromeos/external_metrics.h"
#include "components/metrics/serialization/metric_sample.h"
#include "components/metrics/serialization/serialization_utils.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos { // Need this because of the FRIEND_TEST
class ExternalMetricsTest : public testing::Test {
public:
virtual void SetUp() override {
ASSERT_TRUE(dir_.CreateUniqueTempDir());
external_metrics_ = ExternalMetrics::CreateForTesting(
dir_.path().Append("testfile").value());
}
base::ScopedTempDir dir_;
scoped_refptr<ExternalMetrics> external_metrics_;
content::TestBrowserThreadBundle thread_bundle_;
};
TEST_F(ExternalMetricsTest, HandleMissingFile) {
ASSERT_TRUE(base::DeleteFile(
base::FilePath(external_metrics_->uma_events_file_), false));
EXPECT_EQ(0, external_metrics_->CollectEvents());
}
TEST_F(ExternalMetricsTest, CanReceiveHistogram) {
base::HistogramTester histogram_tester;
scoped_ptr<metrics::MetricSample> hist =
metrics::MetricSample::HistogramSample("foo", 2, 1, 100, 10);
EXPECT_TRUE(metrics::SerializationUtils::WriteMetricToFile(
*hist.get(), external_metrics_->uma_events_file_));
EXPECT_EQ(1, external_metrics_->CollectEvents());
histogram_tester.ExpectTotalCount("foo", 1);
}
TEST_F(ExternalMetricsTest, IncorrectHistogramsAreDiscarded) {
base::HistogramTester histogram_tester;
// Malformed histogram (min > max).
scoped_ptr<metrics::MetricSample> hist =
metrics::MetricSample::HistogramSample("bar", 30, 200, 20, 10);
EXPECT_TRUE(metrics::SerializationUtils::WriteMetricToFile(
*hist.get(), external_metrics_->uma_events_file_));
external_metrics_->CollectEvents();
histogram_tester.ExpectTotalCount("bar", 0);
}
} // namespace chromeos
|