Chromium Code Reviews| 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 <cstring> | 5 #include <cstring> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/prefs/pref_registry_simple.h" | 12 #include "base/prefs/pref_registry_simple.h" |
| 13 #include "base/prefs/testing_pref_service.h" | 13 #include "base/prefs/testing_pref_service.h" |
| 14 #include "base/test/test_simple_task_runner.h" | 14 #include "base/test/test_simple_task_runner.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "base/values.h" | 16 #include "base/values.h" |
| 17 #include "chrome/browser/policy/external_data_fetcher.h" | 17 #include "chrome/browser/policy/external_data_fetcher.h" |
| 18 #include "chrome/browser/policy/mock_policy_service.h" | 18 #include "chrome/browser/policy/mock_policy_service.h" |
| 19 #include "chrome/browser/policy/policy_map.h" | 19 #include "chrome/browser/policy/policy_map.h" |
| 20 #include "chrome/browser/policy/policy_statistics_collector.h" | 20 #include "chrome/browser/policy/policy_statistics_collector.h" |
| 21 #include "chrome/browser/policy/policy_types.h" | 21 #include "chrome/browser/policy/policy_types.h" |
| 22 #include "chrome/browser/policy/test/policy_test_utils.h" | |
| 23 #include "components/policy/core/common/policy_details.h" | |
| 22 #include "components/policy/core/common/policy_pref_names.h" | 24 #include "components/policy/core/common/policy_pref_names.h" |
| 23 #include "policy/policy_constants.h" | 25 #include "components/policy/core/common/schema.h" |
|
bartfab (slow)
2013/11/05 18:18:33
Nit: Already included by header file.
Joao da Silva
2013/11/07 20:27:27
Done.
| |
| 24 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 28 |
| 27 namespace policy { | 29 namespace policy { |
| 28 | 30 |
| 29 namespace { | 31 namespace { |
| 30 | 32 |
| 31 using testing::_; | |
| 32 using testing::Lt; | |
| 33 using testing::Return; | |
| 34 using testing::ReturnRef; | 33 using testing::ReturnRef; |
| 35 | 34 |
| 36 // Arbitrary policy names used for testing. | 35 // Arbitrary policy names used for testing. |
| 37 const char* const kTestPolicy1 = key::kAlternateErrorPagesEnabled; | 36 const char kTestPolicy1[] = "Test Policy 1"; |
| 38 const char* const kTestPolicy2 = key::kSearchSuggestEnabled; | 37 const char kTestPolicy2[] = "Test Policy 2"; |
| 38 | |
| 39 const int kTestPolicy1Id = 42; | |
| 40 const int kTestPolicy2Id = 123; | |
| 41 | |
| 42 const char kTestChromeSchema[] = | |
| 43 "{" | |
| 44 " \"type\": \"object\"," | |
| 45 " \"properties\": {" | |
| 46 " \"Test Policy 1\": { \"type\": \"string\" }," | |
| 47 " \"Test Policy 2\": { \"type\": \"string\" }" | |
| 48 " }" | |
| 49 "}"; | |
| 50 | |
| 51 const PolicyDetails kTestPolicyDetails[] = { | |
| 52 // is_deprecated is_device_policy id max_external_data_size | |
| 53 { false, false, kTestPolicy1Id, 0 }, | |
| 54 { false, false, kTestPolicy2Id, 0 }, | |
| 55 }; | |
| 39 | 56 |
| 40 class TestPolicyStatisticsCollector : public PolicyStatisticsCollector { | 57 class TestPolicyStatisticsCollector : public PolicyStatisticsCollector { |
| 41 public: | 58 public: |
| 42 TestPolicyStatisticsCollector( | 59 TestPolicyStatisticsCollector( |
| 60 const GetChromePolicyDetailsCallback& get_details, | |
| 61 const Schema& chrome_schema, | |
| 43 PolicyService* policy_service, | 62 PolicyService* policy_service, |
| 44 PrefService* prefs, | 63 PrefService* prefs, |
| 45 const scoped_refptr<base::TaskRunner>& task_runner) | 64 const scoped_refptr<base::TaskRunner>& task_runner) |
| 46 : PolicyStatisticsCollector(policy_service, prefs, task_runner) { | 65 : PolicyStatisticsCollector(get_details, |
| 47 } | 66 chrome_schema, |
| 67 policy_service, | |
| 68 prefs, | |
| 69 task_runner) {} | |
| 48 | 70 |
| 49 MOCK_METHOD1(RecordPolicyUse, void(int)); | 71 MOCK_METHOD1(RecordPolicyUse, void(int)); |
| 50 }; | 72 }; |
| 51 | 73 |
| 52 } // namespace | 74 } // namespace |
| 53 | 75 |
| 54 class PolicyStatisticsCollectorTest : public testing::Test { | 76 class PolicyStatisticsCollectorTest : public testing::Test { |
| 55 protected: | 77 protected: |
| 56 PolicyStatisticsCollectorTest() | 78 PolicyStatisticsCollectorTest() |
| 57 : update_delay_(base::TimeDelta::FromMilliseconds( | 79 : update_delay_(base::TimeDelta::FromMilliseconds( |
| 58 PolicyStatisticsCollector::kStatisticsUpdateRate)), | 80 PolicyStatisticsCollector::kStatisticsUpdateRate)), |
| 59 test_policy_id1_(-1), | |
| 60 test_policy_id2_(-1), | |
| 61 task_runner_(new base::TestSimpleTaskRunner()) { | 81 task_runner_(new base::TestSimpleTaskRunner()) { |
| 62 } | 82 } |
| 63 | 83 |
| 64 virtual void SetUp() OVERRIDE { | 84 virtual void SetUp() OVERRIDE { |
| 85 std::string error; | |
| 86 chrome_schema_ = Schema::Parse(kTestChromeSchema, &error); | |
| 87 ASSERT_TRUE(chrome_schema_.valid()) << error; | |
| 88 | |
| 89 policy_details_.SetDetails(kTestPolicy1, &kTestPolicyDetails[0]); | |
| 90 policy_details_.SetDetails(kTestPolicy2, &kTestPolicyDetails[1]); | |
| 91 | |
| 65 prefs_.registry()->RegisterInt64Pref( | 92 prefs_.registry()->RegisterInt64Pref( |
| 66 policy_prefs::kLastPolicyStatisticsUpdate, 0); | 93 policy_prefs::kLastPolicyStatisticsUpdate, 0); |
| 67 | 94 |
| 68 // Find ids for kTestPolicy1 and kTestPolicy2. | |
| 69 const policy::PolicyDefinitionList* policy_list = | |
| 70 policy::GetChromePolicyDefinitionList(); | |
| 71 for (const policy::PolicyDefinitionList::Entry* policy = policy_list->begin; | |
| 72 policy != policy_list->end; ++policy) { | |
| 73 if (strcmp(policy->name, kTestPolicy1) == 0) | |
| 74 test_policy_id1_ = policy->id; | |
| 75 else if (strcmp(policy->name, kTestPolicy2) == 0) | |
| 76 test_policy_id2_ = policy->id; | |
| 77 } | |
| 78 ASSERT_TRUE(test_policy_id1_ != -1); | |
| 79 ASSERT_TRUE(test_policy_id2_ != -1); | |
| 80 | |
| 81 // Set up default function behaviour. | 95 // Set up default function behaviour. |
| 82 EXPECT_CALL(policy_service_, | 96 EXPECT_CALL(policy_service_, |
| 83 GetPolicies(PolicyNamespace(POLICY_DOMAIN_CHROME, | 97 GetPolicies(PolicyNamespace(POLICY_DOMAIN_CHROME, |
| 84 std::string()))) | 98 std::string()))) |
| 85 .WillRepeatedly(ReturnRef(policy_map_)); | 99 .WillRepeatedly(ReturnRef(policy_map_)); |
| 86 | 100 |
| 87 // Arbitrary negative value (so it'll be different from |update_delay_|). | 101 // Arbitrary negative value (so it'll be different from |update_delay_|). |
| 88 last_delay_ = base::TimeDelta::FromDays(-1); | 102 last_delay_ = base::TimeDelta::FromDays(-1); |
| 89 policy_map_.Clear(); | 103 policy_map_.Clear(); |
| 90 policy_statistics_collector_.reset(new TestPolicyStatisticsCollector( | 104 policy_statistics_collector_.reset(new TestPolicyStatisticsCollector( |
| 105 policy_details_.GetCallback(), | |
| 106 chrome_schema_, | |
| 91 &policy_service_, | 107 &policy_service_, |
| 92 &prefs_, | 108 &prefs_, |
| 93 task_runner_)); | 109 task_runner_)); |
| 94 } | 110 } |
| 95 | 111 |
| 96 void SetPolicy(const std::string& name) { | 112 void SetPolicy(const std::string& name) { |
| 97 policy_map_.Set(name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 113 policy_map_.Set(name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 98 base::Value::CreateBooleanValue(true), NULL); | 114 base::Value::CreateBooleanValue(true), NULL); |
| 99 } | 115 } |
| 100 | 116 |
| 101 base::TimeDelta GetFirstDelay() const { | 117 base::TimeDelta GetFirstDelay() const { |
| 102 if (task_runner_->GetPendingTasks().empty()) { | 118 if (task_runner_->GetPendingTasks().empty()) { |
| 103 ADD_FAILURE(); | 119 ADD_FAILURE(); |
| 104 return base::TimeDelta(); | 120 return base::TimeDelta(); |
| 105 } | 121 } |
| 106 return task_runner_->GetPendingTasks().front().delay; | 122 return task_runner_->GetPendingTasks().front().delay; |
| 107 } | 123 } |
| 108 | 124 |
| 109 const base::TimeDelta update_delay_; | 125 const base::TimeDelta update_delay_; |
| 110 | 126 |
| 111 int test_policy_id1_; | |
| 112 int test_policy_id2_; | |
| 113 | |
| 114 base::TimeDelta last_delay_; | 127 base::TimeDelta last_delay_; |
| 115 | 128 |
| 129 PolicyDetailsMap policy_details_; | |
| 130 Schema chrome_schema_; | |
| 116 TestingPrefServiceSimple prefs_; | 131 TestingPrefServiceSimple prefs_; |
| 117 MockPolicyService policy_service_; | 132 MockPolicyService policy_service_; |
| 118 PolicyMap policy_map_; | 133 PolicyMap policy_map_; |
| 119 | 134 |
| 120 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 135 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 121 scoped_ptr<TestPolicyStatisticsCollector> policy_statistics_collector_; | 136 scoped_ptr<TestPolicyStatisticsCollector> policy_statistics_collector_; |
| 122 }; | 137 }; |
| 123 | 138 |
| 124 TEST_F(PolicyStatisticsCollectorTest, CollectPending) { | 139 TEST_F(PolicyStatisticsCollectorTest, CollectPending) { |
| 125 SetPolicy(kTestPolicy1); | 140 SetPolicy(kTestPolicy1); |
| 126 | 141 |
| 127 prefs_.SetInt64(policy_prefs::kLastPolicyStatisticsUpdate, | 142 prefs_.SetInt64(policy_prefs::kLastPolicyStatisticsUpdate, |
| 128 (base::Time::Now() - update_delay_).ToInternalValue()); | 143 (base::Time::Now() - update_delay_).ToInternalValue()); |
| 129 | 144 |
| 130 EXPECT_CALL(*policy_statistics_collector_.get(), | 145 EXPECT_CALL(*policy_statistics_collector_.get(), |
| 131 RecordPolicyUse(test_policy_id1_)); | 146 RecordPolicyUse(kTestPolicy1Id)); |
| 132 | 147 |
| 133 policy_statistics_collector_->Initialize(); | 148 policy_statistics_collector_->Initialize(); |
| 134 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size()); | 149 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size()); |
| 135 EXPECT_EQ(update_delay_, GetFirstDelay()); | 150 EXPECT_EQ(update_delay_, GetFirstDelay()); |
| 136 } | 151 } |
| 137 | 152 |
| 138 TEST_F(PolicyStatisticsCollectorTest, CollectPendingVeryOld) { | 153 TEST_F(PolicyStatisticsCollectorTest, CollectPendingVeryOld) { |
| 139 SetPolicy(kTestPolicy1); | 154 SetPolicy(kTestPolicy1); |
| 140 | 155 |
| 141 // Must not be 0.0 (read comment for Time::FromDoubleT). | 156 // Must not be 0.0 (read comment for Time::FromDoubleT). |
| 142 prefs_.SetInt64(policy_prefs::kLastPolicyStatisticsUpdate, | 157 prefs_.SetInt64(policy_prefs::kLastPolicyStatisticsUpdate, |
| 143 base::Time::FromDoubleT(1.0).ToInternalValue()); | 158 base::Time::FromDoubleT(1.0).ToInternalValue()); |
| 144 | 159 |
| 145 EXPECT_CALL(*policy_statistics_collector_.get(), | 160 EXPECT_CALL(*policy_statistics_collector_.get(), |
| 146 RecordPolicyUse(test_policy_id1_)); | 161 RecordPolicyUse(kTestPolicy1Id)); |
| 147 | 162 |
| 148 policy_statistics_collector_->Initialize(); | 163 policy_statistics_collector_->Initialize(); |
| 149 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size()); | 164 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size()); |
| 150 EXPECT_EQ(update_delay_, GetFirstDelay()); | 165 EXPECT_EQ(update_delay_, GetFirstDelay()); |
| 151 } | 166 } |
| 152 | 167 |
| 153 TEST_F(PolicyStatisticsCollectorTest, CollectLater) { | 168 TEST_F(PolicyStatisticsCollectorTest, CollectLater) { |
| 154 SetPolicy(kTestPolicy1); | 169 SetPolicy(kTestPolicy1); |
| 155 | 170 |
| 156 prefs_.SetInt64(policy_prefs::kLastPolicyStatisticsUpdate, | 171 prefs_.SetInt64(policy_prefs::kLastPolicyStatisticsUpdate, |
| 157 (base::Time::Now() - update_delay_ / 2).ToInternalValue()); | 172 (base::Time::Now() - update_delay_ / 2).ToInternalValue()); |
| 158 | 173 |
| 159 policy_statistics_collector_->Initialize(); | 174 policy_statistics_collector_->Initialize(); |
| 160 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size()); | 175 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size()); |
| 161 EXPECT_LT(GetFirstDelay(), update_delay_); | 176 EXPECT_LT(GetFirstDelay(), update_delay_); |
| 162 } | 177 } |
| 163 | 178 |
| 164 TEST_F(PolicyStatisticsCollectorTest, MultiplePolicies) { | 179 TEST_F(PolicyStatisticsCollectorTest, MultiplePolicies) { |
| 165 SetPolicy(kTestPolicy1); | 180 SetPolicy(kTestPolicy1); |
| 166 SetPolicy(kTestPolicy2); | 181 SetPolicy(kTestPolicy2); |
| 167 | 182 |
| 168 prefs_.SetInt64(policy_prefs::kLastPolicyStatisticsUpdate, | 183 prefs_.SetInt64(policy_prefs::kLastPolicyStatisticsUpdate, |
| 169 (base::Time::Now() - update_delay_).ToInternalValue()); | 184 (base::Time::Now() - update_delay_).ToInternalValue()); |
| 170 | 185 |
| 171 EXPECT_CALL(*policy_statistics_collector_.get(), | 186 EXPECT_CALL(*policy_statistics_collector_.get(), |
| 172 RecordPolicyUse(test_policy_id1_)); | 187 RecordPolicyUse(kTestPolicy1Id)); |
| 173 EXPECT_CALL(*policy_statistics_collector_.get(), | 188 EXPECT_CALL(*policy_statistics_collector_.get(), |
| 174 RecordPolicyUse(test_policy_id2_)); | 189 RecordPolicyUse(kTestPolicy2Id)); |
| 175 | 190 |
| 176 policy_statistics_collector_->Initialize(); | 191 policy_statistics_collector_->Initialize(); |
| 177 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size()); | 192 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size()); |
| 178 } | 193 } |
| 179 | 194 |
| 180 } // namespace policy | 195 } // namespace policy |
| OLD | NEW |