| 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_state_manager.h" | 5 #include "components/metrics/metrics_state_manager.h" |
| 6 | 6 |
| 7 #include <ctype.h> | 7 #include <ctype.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "components/metrics/client_info.h" | 16 #include "components/metrics/client_info.h" |
| 17 #include "components/metrics/metrics_pref_names.h" | 17 #include "components/metrics/metrics_pref_names.h" |
| 18 #include "components/metrics/metrics_service.h" | 18 #include "components/metrics/metrics_service.h" |
| 19 #include "components/metrics/metrics_switches.h" | 19 #include "components/metrics/metrics_switches.h" |
| 20 #include "components/metrics/test_enabled_state_provider.h" |
| 20 #include "components/prefs/testing_pref_service.h" | 21 #include "components/prefs/testing_pref_service.h" |
| 21 #include "components/variations/caching_permuted_entropy_provider.h" | 22 #include "components/variations/caching_permuted_entropy_provider.h" |
| 22 #include "components/variations/pref_names.h" | 23 #include "components/variations/pref_names.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 25 |
| 25 namespace metrics { | 26 namespace metrics { |
| 26 | 27 |
| 27 class MetricsStateManagerTest : public testing::Test { | 28 class MetricsStateManagerTest : public testing::Test { |
| 28 public: | 29 public: |
| 29 MetricsStateManagerTest() : is_metrics_reporting_enabled_(false) { | 30 MetricsStateManagerTest() |
| 31 : enabled_state_provider_(new TestEnabledStateProvider(false, false)) { |
| 30 MetricsService::RegisterPrefs(prefs_.registry()); | 32 MetricsService::RegisterPrefs(prefs_.registry()); |
| 31 } | 33 } |
| 32 | 34 |
| 33 std::unique_ptr<MetricsStateManager> CreateStateManager() { | 35 std::unique_ptr<MetricsStateManager> CreateStateManager() { |
| 34 return MetricsStateManager::Create( | 36 return MetricsStateManager::Create( |
| 35 &prefs_, | 37 &prefs_, enabled_state_provider_.get(), |
| 36 base::Bind(&MetricsStateManagerTest::is_metrics_reporting_enabled, | |
| 37 base::Unretained(this)), | |
| 38 base::Bind(&MetricsStateManagerTest::MockStoreClientInfoBackup, | 38 base::Bind(&MetricsStateManagerTest::MockStoreClientInfoBackup, |
| 39 base::Unretained(this)), | 39 base::Unretained(this)), |
| 40 base::Bind(&MetricsStateManagerTest::LoadFakeClientInfoBackup, | 40 base::Bind(&MetricsStateManagerTest::LoadFakeClientInfoBackup, |
| 41 base::Unretained(this))); | 41 base::Unretained(this))); |
| 42 } | 42 } |
| 43 | 43 |
| 44 // Sets metrics reporting as enabled for testing. | 44 // Sets metrics reporting as enabled for testing. |
| 45 void EnableMetricsReporting() { | 45 void EnableMetricsReporting() { |
| 46 is_metrics_reporting_enabled_ = true; | 46 enabled_state_provider_->set_consent(true); |
| 47 enabled_state_provider_->set_enabled(true); |
| 47 } | 48 } |
| 48 | 49 |
| 49 protected: | 50 protected: |
| 50 TestingPrefServiceSimple prefs_; | 51 TestingPrefServiceSimple prefs_; |
| 51 | 52 |
| 52 // Last ClientInfo stored by the MetricsStateManager via | 53 // Last ClientInfo stored by the MetricsStateManager via |
| 53 // MockStoreClientInfoBackup. | 54 // MockStoreClientInfoBackup. |
| 54 std::unique_ptr<ClientInfo> stored_client_info_backup_; | 55 std::unique_ptr<ClientInfo> stored_client_info_backup_; |
| 55 | 56 |
| 56 // If set, will be returned via LoadFakeClientInfoBackup if requested by the | 57 // If set, will be returned via LoadFakeClientInfoBackup if requested by the |
| 57 // MetricsStateManager. | 58 // MetricsStateManager. |
| 58 std::unique_ptr<ClientInfo> fake_client_info_backup_; | 59 std::unique_ptr<ClientInfo> fake_client_info_backup_; |
| 59 | 60 |
| 60 private: | 61 private: |
| 61 bool is_metrics_reporting_enabled() const { | |
| 62 return is_metrics_reporting_enabled_; | |
| 63 } | |
| 64 | |
| 65 // Stores the |client_info| in |stored_client_info_backup_| for verification | 62 // Stores the |client_info| in |stored_client_info_backup_| for verification |
| 66 // by the tests later. | 63 // by the tests later. |
| 67 void MockStoreClientInfoBackup(const ClientInfo& client_info) { | 64 void MockStoreClientInfoBackup(const ClientInfo& client_info) { |
| 68 stored_client_info_backup_.reset(new ClientInfo); | 65 stored_client_info_backup_.reset(new ClientInfo); |
| 69 stored_client_info_backup_->client_id = client_info.client_id; | 66 stored_client_info_backup_->client_id = client_info.client_id; |
| 70 stored_client_info_backup_->installation_date = | 67 stored_client_info_backup_->installation_date = |
| 71 client_info.installation_date; | 68 client_info.installation_date; |
| 72 stored_client_info_backup_->reporting_enabled_date = | 69 stored_client_info_backup_->reporting_enabled_date = |
| 73 client_info.reporting_enabled_date; | 70 client_info.reporting_enabled_date; |
| 74 | 71 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 86 | 83 |
| 87 std::unique_ptr<ClientInfo> backup_copy(new ClientInfo); | 84 std::unique_ptr<ClientInfo> backup_copy(new ClientInfo); |
| 88 backup_copy->client_id = fake_client_info_backup_->client_id; | 85 backup_copy->client_id = fake_client_info_backup_->client_id; |
| 89 backup_copy->installation_date = | 86 backup_copy->installation_date = |
| 90 fake_client_info_backup_->installation_date; | 87 fake_client_info_backup_->installation_date; |
| 91 backup_copy->reporting_enabled_date = | 88 backup_copy->reporting_enabled_date = |
| 92 fake_client_info_backup_->reporting_enabled_date; | 89 fake_client_info_backup_->reporting_enabled_date; |
| 93 return backup_copy; | 90 return backup_copy; |
| 94 } | 91 } |
| 95 | 92 |
| 96 bool is_metrics_reporting_enabled_; | 93 std::unique_ptr<TestEnabledStateProvider> enabled_state_provider_; |
| 97 | 94 |
| 98 DISALLOW_COPY_AND_ASSIGN(MetricsStateManagerTest); | 95 DISALLOW_COPY_AND_ASSIGN(MetricsStateManagerTest); |
| 99 }; | 96 }; |
| 100 | 97 |
| 101 // Ensure the ClientId is formatted as expected. | 98 // Ensure the ClientId is formatted as expected. |
| 102 TEST_F(MetricsStateManagerTest, ClientIdCorrectlyFormatted) { | 99 TEST_F(MetricsStateManagerTest, ClientIdCorrectlyFormatted) { |
| 103 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); | 100 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
| 104 state_manager->ForceClientIdCreation(); | 101 state_manager->ForceClientIdCreation(); |
| 105 | 102 |
| 106 const std::string client_id = state_manager->client_id(); | 103 const std::string client_id = state_manager->client_id(); |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 | 374 |
| 378 // The metrics-reporting-enabled date will be reset to Now(). | 375 // The metrics-reporting-enabled date will be reset to Now(). |
| 379 EXPECT_GE(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), | 376 EXPECT_GE(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), |
| 380 previous_client_info->reporting_enabled_date); | 377 previous_client_info->reporting_enabled_date); |
| 381 | 378 |
| 382 stored_client_info_backup_.reset(); | 379 stored_client_info_backup_.reset(); |
| 383 } | 380 } |
| 384 } | 381 } |
| 385 | 382 |
| 386 } // namespace metrics | 383 } // namespace metrics |
| OLD | NEW |