OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/metrics/metrics_service.h" | 5 #include "components/metrics/metrics_service.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "base/metrics/metrics_hashes.h" | 15 #include "base/metrics/metrics_hashes.h" |
16 #include "base/metrics/statistics_recorder.h" | 16 #include "base/metrics/statistics_recorder.h" |
17 #include "base/metrics/user_metrics.h" | 17 #include "base/metrics/user_metrics.h" |
18 #include "base/threading/platform_thread.h" | 18 #include "base/threading/platform_thread.h" |
19 #include "components/metrics/client_info.h" | 19 #include "components/metrics/client_info.h" |
20 #include "components/metrics/metrics_log.h" | 20 #include "components/metrics/metrics_log.h" |
21 #include "components/metrics/metrics_pref_names.h" | 21 #include "components/metrics/metrics_pref_names.h" |
22 #include "components/metrics/metrics_state_manager.h" | 22 #include "components/metrics/metrics_state_manager.h" |
| 23 #include "components/metrics/test_enabled_state_provider.h" |
23 #include "components/metrics/test_metrics_provider.h" | 24 #include "components/metrics/test_metrics_provider.h" |
24 #include "components/metrics/test_metrics_service_client.h" | 25 #include "components/metrics/test_metrics_service_client.h" |
25 #include "components/prefs/testing_pref_service.h" | 26 #include "components/prefs/testing_pref_service.h" |
26 #include "components/variations/metrics_util.h" | 27 #include "components/variations/metrics_util.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
28 #include "third_party/zlib/google/compression_utils.h" | 29 #include "third_party/zlib/google/compression_utils.h" |
29 | 30 |
30 namespace metrics { | 31 namespace metrics { |
31 | 32 |
32 namespace { | 33 namespace { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 local_state) {} | 66 local_state) {} |
66 | 67 |
67 ~TestMetricsLog() override {} | 68 ~TestMetricsLog() override {} |
68 | 69 |
69 private: | 70 private: |
70 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog); | 71 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog); |
71 }; | 72 }; |
72 | 73 |
73 class MetricsServiceTest : public testing::Test { | 74 class MetricsServiceTest : public testing::Test { |
74 public: | 75 public: |
75 MetricsServiceTest() : is_metrics_reporting_enabled_(false) { | 76 MetricsServiceTest() |
| 77 : enabled_state_provider_(new TestEnabledStateProvider(false, false)) { |
76 base::SetRecordActionTaskRunner(message_loop.task_runner()); | 78 base::SetRecordActionTaskRunner(message_loop.task_runner()); |
77 MetricsService::RegisterPrefs(testing_local_state_.registry()); | 79 MetricsService::RegisterPrefs(testing_local_state_.registry()); |
78 metrics_state_manager_ = MetricsStateManager::Create( | 80 metrics_state_manager_ = MetricsStateManager::Create( |
79 GetLocalState(), | 81 GetLocalState(), enabled_state_provider_.get(), |
80 base::Bind(&MetricsServiceTest::is_metrics_reporting_enabled, | 82 base::Bind(&StoreNoClientInfoBackup), base::Bind(&ReturnNoBackup)); |
81 base::Unretained(this)), | |
82 base::Bind(&StoreNoClientInfoBackup), | |
83 base::Bind(&ReturnNoBackup)); | |
84 } | 83 } |
85 | 84 |
86 ~MetricsServiceTest() override { | 85 ~MetricsServiceTest() override { |
87 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE, | 86 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE, |
88 GetLocalState()); | 87 GetLocalState()); |
89 } | 88 } |
90 | 89 |
91 MetricsStateManager* GetMetricsStateManager() { | 90 MetricsStateManager* GetMetricsStateManager() { |
92 return metrics_state_manager_.get(); | 91 return metrics_state_manager_.get(); |
93 } | 92 } |
94 | 93 |
95 PrefService* GetLocalState() { return &testing_local_state_; } | 94 PrefService* GetLocalState() { return &testing_local_state_; } |
96 | 95 |
97 // Sets metrics reporting as enabled for testing. | 96 // Sets metrics reporting as enabled for testing. |
98 void EnableMetricsReporting() { | 97 void EnableMetricsReporting() { |
99 is_metrics_reporting_enabled_ = true; | 98 enabled_state_provider_->set_consent(true); |
| 99 enabled_state_provider_->set_enabled(true); |
100 } | 100 } |
101 | 101 |
102 // Waits until base::TimeTicks::Now() no longer equals |value|. This should | 102 // Waits until base::TimeTicks::Now() no longer equals |value|. This should |
103 // take between 1-15ms per the documented resolution of base::TimeTicks. | 103 // take between 1-15ms per the documented resolution of base::TimeTicks. |
104 void WaitUntilTimeChanges(const base::TimeTicks& value) { | 104 void WaitUntilTimeChanges(const base::TimeTicks& value) { |
105 while (base::TimeTicks::Now() == value) { | 105 while (base::TimeTicks::Now() == value) { |
106 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); | 106 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); |
107 } | 107 } |
108 } | 108 } |
109 | 109 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 const uint64_t hash = uma_log.histogram_event(i).name_hash(); | 145 const uint64_t hash = uma_log.histogram_event(i).name_hash(); |
146 | 146 |
147 const base::HistogramBase* histogram = FindHistogram(histograms, hash); | 147 const base::HistogramBase* histogram = FindHistogram(histograms, hash); |
148 EXPECT_TRUE(histogram) << hash; | 148 EXPECT_TRUE(histogram) << hash; |
149 | 149 |
150 EXPECT_EQ(kStabilityFlags, histogram->flags() & kStabilityFlags) << hash; | 150 EXPECT_EQ(kStabilityFlags, histogram->flags() & kStabilityFlags) << hash; |
151 } | 151 } |
152 } | 152 } |
153 | 153 |
154 private: | 154 private: |
155 bool is_metrics_reporting_enabled() const { | 155 std::unique_ptr<TestEnabledStateProvider> enabled_state_provider_; |
156 return is_metrics_reporting_enabled_; | |
157 } | |
158 | |
159 bool is_metrics_reporting_enabled_; | |
160 TestingPrefServiceSimple testing_local_state_; | 156 TestingPrefServiceSimple testing_local_state_; |
161 std::unique_ptr<MetricsStateManager> metrics_state_manager_; | 157 std::unique_ptr<MetricsStateManager> metrics_state_manager_; |
162 base::MessageLoop message_loop; | 158 base::MessageLoop message_loop; |
163 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 159 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
164 | 160 |
165 DISALLOW_COPY_AND_ASSIGN(MetricsServiceTest); | 161 DISALLOW_COPY_AND_ASSIGN(MetricsServiceTest); |
166 }; | 162 }; |
167 | 163 |
168 } // namespace | 164 } // namespace |
169 | 165 |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 TestMetricsProvider* test_provider = new TestMetricsProvider(); | 404 TestMetricsProvider* test_provider = new TestMetricsProvider(); |
409 service.RegisterMetricsProvider( | 405 service.RegisterMetricsProvider( |
410 std::unique_ptr<MetricsProvider>(test_provider)); | 406 std::unique_ptr<MetricsProvider>(test_provider)); |
411 | 407 |
412 service.InitializeMetricsRecordingState(); | 408 service.InitializeMetricsRecordingState(); |
413 | 409 |
414 EXPECT_TRUE(test_provider->init_called()); | 410 EXPECT_TRUE(test_provider->init_called()); |
415 } | 411 } |
416 | 412 |
417 } // namespace metrics | 413 } // namespace metrics |
OLD | NEW |