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