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