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 "base/files/file_util.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/metrics/statistics_recorder.h"
9 #include "base/test/histogram_tester.h"
10 #include "chrome/browser/chromeos/external_metrics.h"
11 #include "components/metrics/serialization/metric_sample.h"
12 #include "components/metrics/serialization/serialization_utils.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace chromeos
{ // Need this because of the FRIEND_TEST
18 class ExternalMetricsTest
: public testing::Test
{
20 void SetUp() override
{
21 ASSERT_TRUE(dir_
.CreateUniqueTempDir());
22 external_metrics_
= ExternalMetrics::CreateForTesting(
23 dir_
.path().Append("testfile").value());
26 base::ScopedTempDir dir_
;
27 scoped_refptr
<ExternalMetrics
> external_metrics_
;
28 content::TestBrowserThreadBundle thread_bundle_
;
31 TEST_F(ExternalMetricsTest
, HandleMissingFile
) {
32 ASSERT_TRUE(base::DeleteFile(
33 base::FilePath(external_metrics_
->uma_events_file_
), false));
35 EXPECT_EQ(0, external_metrics_
->CollectEvents());
38 TEST_F(ExternalMetricsTest
, CanReceiveHistogram
) {
39 base::HistogramTester histogram_tester
;
41 scoped_ptr
<metrics::MetricSample
> hist
=
42 metrics::MetricSample::HistogramSample("foo", 2, 1, 100, 10);
44 EXPECT_TRUE(metrics::SerializationUtils::WriteMetricToFile(
45 *hist
.get(), external_metrics_
->uma_events_file_
));
47 EXPECT_EQ(1, external_metrics_
->CollectEvents());
49 histogram_tester
.ExpectTotalCount("foo", 1);
52 TEST_F(ExternalMetricsTest
, IncorrectHistogramsAreDiscarded
) {
53 base::HistogramTester histogram_tester
;
55 // Malformed histogram (min > max).
56 scoped_ptr
<metrics::MetricSample
> hist
=
57 metrics::MetricSample::HistogramSample("bar", 30, 200, 20, 10);
59 EXPECT_TRUE(metrics::SerializationUtils::WriteMetricToFile(
60 *hist
.get(), external_metrics_
->uma_events_file_
));
62 external_metrics_
->CollectEvents();
64 histogram_tester
.ExpectTotalCount("bar", 0);
67 } // namespace chromeos