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 "chrome/browser/policy/configuration_policy_provider_test.h" | 5 #include "chrome/browser/policy/configuration_policy_provider_test.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "chrome/browser/policy/configuration_policy_provider.h" | 10 #include "chrome/browser/policy/configuration_policy_provider.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 | 42 |
43 } // namespace test_policy_definitions | 43 } // namespace test_policy_definitions |
44 | 44 |
45 PolicyTestBase::PolicyTestBase() | 45 PolicyTestBase::PolicyTestBase() |
46 : ui_thread_(BrowserThread::UI, &loop_), | 46 : ui_thread_(BrowserThread::UI, &loop_), |
47 file_thread_(BrowserThread::FILE, &loop_) {} | 47 file_thread_(BrowserThread::FILE, &loop_) {} |
48 | 48 |
49 PolicyTestBase::~PolicyTestBase() {} | 49 PolicyTestBase::~PolicyTestBase() {} |
50 | 50 |
51 void PolicyTestBase::TearDown() { | 51 void PolicyTestBase::TearDown() { |
52 loop_.RunAllPending(); | 52 loop_.RunUntilIdle(); |
53 } | 53 } |
54 | 54 |
55 PolicyProviderTestHarness::PolicyProviderTestHarness(PolicyLevel level, | 55 PolicyProviderTestHarness::PolicyProviderTestHarness(PolicyLevel level, |
56 PolicyScope scope) | 56 PolicyScope scope) |
57 : level_(level), scope_(scope) {} | 57 : level_(level), scope_(scope) {} |
58 | 58 |
59 PolicyProviderTestHarness::~PolicyProviderTestHarness() {} | 59 PolicyProviderTestHarness::~PolicyProviderTestHarness() {} |
60 | 60 |
61 PolicyLevel PolicyProviderTestHarness::policy_level() const { | 61 PolicyLevel PolicyProviderTestHarness::policy_level() const { |
62 return level_; | 62 return level_; |
(...skipping 16 matching lines...) Expand all Loading... |
79 PolicyTestBase::SetUp(); | 79 PolicyTestBase::SetUp(); |
80 | 80 |
81 test_harness_.reset((*GetParam())()); | 81 test_harness_.reset((*GetParam())()); |
82 test_harness_->SetUp(); | 82 test_harness_->SetUp(); |
83 | 83 |
84 provider_.reset( | 84 provider_.reset( |
85 test_harness_->CreateProvider(&test_policy_definitions::kList)); | 85 test_harness_->CreateProvider(&test_policy_definitions::kList)); |
86 provider_->Init(); | 86 provider_->Init(); |
87 // Some providers do a reload on init. Make sure any notifications generated | 87 // Some providers do a reload on init. Make sure any notifications generated |
88 // are fired now. | 88 // are fired now. |
89 loop_.RunAllPending(); | 89 loop_.RunUntilIdle(); |
90 | 90 |
91 const PolicyBundle kEmptyBundle; | 91 const PolicyBundle kEmptyBundle; |
92 EXPECT_TRUE(provider_->policies().Equals(kEmptyBundle)); | 92 EXPECT_TRUE(provider_->policies().Equals(kEmptyBundle)); |
93 } | 93 } |
94 | 94 |
95 void ConfigurationPolicyProviderTest::TearDown() { | 95 void ConfigurationPolicyProviderTest::TearDown() { |
96 // Give providers the chance to clean up after themselves on the file thread. | 96 // Give providers the chance to clean up after themselves on the file thread. |
97 provider_->Shutdown(); | 97 provider_->Shutdown(); |
98 provider_.reset(); | 98 provider_.reset(); |
99 | 99 |
100 PolicyTestBase::TearDown(); | 100 PolicyTestBase::TearDown(); |
101 } | 101 } |
102 | 102 |
103 void ConfigurationPolicyProviderTest::CheckValue( | 103 void ConfigurationPolicyProviderTest::CheckValue( |
104 const char* policy_name, | 104 const char* policy_name, |
105 const base::Value& expected_value, | 105 const base::Value& expected_value, |
106 base::Closure install_value) { | 106 base::Closure install_value) { |
107 // Install the value, reload policy and check the provider for the value. | 107 // Install the value, reload policy and check the provider for the value. |
108 install_value.Run(); | 108 install_value.Run(); |
109 provider_->RefreshPolicies(); | 109 provider_->RefreshPolicies(); |
110 loop_.RunAllPending(); | 110 loop_.RunUntilIdle(); |
111 PolicyBundle expected_bundle; | 111 PolicyBundle expected_bundle; |
112 expected_bundle.Get(POLICY_DOMAIN_CHROME, "") | 112 expected_bundle.Get(POLICY_DOMAIN_CHROME, "") |
113 .Set(policy_name, | 113 .Set(policy_name, |
114 test_harness_->policy_level(), | 114 test_harness_->policy_level(), |
115 test_harness_->policy_scope(), | 115 test_harness_->policy_scope(), |
116 expected_value.DeepCopy()); | 116 expected_value.DeepCopy()); |
117 EXPECT_TRUE(provider_->policies().Equals(expected_bundle)); | 117 EXPECT_TRUE(provider_->policies().Equals(expected_bundle)); |
118 // TODO(joaodasilva): set the policy in the POLICY_DOMAIN_EXTENSIONS too, | 118 // TODO(joaodasilva): set the policy in the POLICY_DOMAIN_EXTENSIONS too, |
119 // and extend the |expected_bundle|, once all providers are ready. | 119 // and extend the |expected_bundle|, once all providers are ready. |
120 } | 120 } |
121 | 121 |
122 TEST_P(ConfigurationPolicyProviderTest, Empty) { | 122 TEST_P(ConfigurationPolicyProviderTest, Empty) { |
123 provider_->RefreshPolicies(); | 123 provider_->RefreshPolicies(); |
124 loop_.RunAllPending(); | 124 loop_.RunUntilIdle(); |
125 const PolicyBundle kEmptyBundle; | 125 const PolicyBundle kEmptyBundle; |
126 EXPECT_TRUE(provider_->policies().Equals(kEmptyBundle)); | 126 EXPECT_TRUE(provider_->policies().Equals(kEmptyBundle)); |
127 } | 127 } |
128 | 128 |
129 TEST_P(ConfigurationPolicyProviderTest, StringValue) { | 129 TEST_P(ConfigurationPolicyProviderTest, StringValue) { |
130 const char kTestString[] = "string_value"; | 130 const char kTestString[] = "string_value"; |
131 base::StringValue expected_value(kTestString); | 131 base::StringValue expected_value(kTestString); |
132 CheckValue(test_policy_definitions::kKeyString, | 132 CheckValue(test_policy_definitions::kKeyString, |
133 expected_value, | 133 expected_value, |
134 base::Bind(&PolicyProviderTestHarness::InstallStringPolicy, | 134 base::Bind(&PolicyProviderTestHarness::InstallStringPolicy, |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 | 204 |
205 TEST_P(ConfigurationPolicyProviderTest, RefreshPolicies) { | 205 TEST_P(ConfigurationPolicyProviderTest, RefreshPolicies) { |
206 PolicyBundle bundle; | 206 PolicyBundle bundle; |
207 EXPECT_TRUE(provider_->policies().Equals(bundle)); | 207 EXPECT_TRUE(provider_->policies().Equals(bundle)); |
208 | 208 |
209 // OnUpdatePolicy is called even when there are no changes. | 209 // OnUpdatePolicy is called even when there are no changes. |
210 MockConfigurationPolicyObserver observer; | 210 MockConfigurationPolicyObserver observer; |
211 provider_->AddObserver(&observer); | 211 provider_->AddObserver(&observer); |
212 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); | 212 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); |
213 provider_->RefreshPolicies(); | 213 provider_->RefreshPolicies(); |
214 loop_.RunAllPending(); | 214 loop_.RunUntilIdle(); |
215 Mock::VerifyAndClearExpectations(&observer); | 215 Mock::VerifyAndClearExpectations(&observer); |
216 | 216 |
217 EXPECT_TRUE(provider_->policies().Equals(bundle)); | 217 EXPECT_TRUE(provider_->policies().Equals(bundle)); |
218 | 218 |
219 // OnUpdatePolicy is called when there are changes. | 219 // OnUpdatePolicy is called when there are changes. |
220 test_harness_->InstallStringPolicy(test_policy_definitions::kKeyString, | 220 test_harness_->InstallStringPolicy(test_policy_definitions::kKeyString, |
221 "value"); | 221 "value"); |
222 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); | 222 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); |
223 provider_->RefreshPolicies(); | 223 provider_->RefreshPolicies(); |
224 loop_.RunAllPending(); | 224 loop_.RunUntilIdle(); |
225 Mock::VerifyAndClearExpectations(&observer); | 225 Mock::VerifyAndClearExpectations(&observer); |
226 | 226 |
227 bundle.Get(POLICY_DOMAIN_CHROME, "") | 227 bundle.Get(POLICY_DOMAIN_CHROME, "") |
228 .Set(test_policy_definitions::kKeyString, | 228 .Set(test_policy_definitions::kKeyString, |
229 test_harness_->policy_level(), | 229 test_harness_->policy_level(), |
230 test_harness_->policy_scope(), | 230 test_harness_->policy_scope(), |
231 base::Value::CreateStringValue("value")); | 231 base::Value::CreateStringValue("value")); |
232 EXPECT_TRUE(provider_->policies().Equals(bundle)); | 232 EXPECT_TRUE(provider_->policies().Equals(bundle)); |
233 provider_->RemoveObserver(&observer); | 233 provider_->RemoveObserver(&observer); |
234 } | 234 } |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 policy_3rdparty.Set("extensions.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", | 298 policy_3rdparty.Set("extensions.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", |
299 policy_dict.DeepCopy()); | 299 policy_dict.DeepCopy()); |
300 // Install invalid 3rd party policies that shouldn't be loaded. These also | 300 // Install invalid 3rd party policies that shouldn't be loaded. These also |
301 // help detecting memory leaks in the code paths that detect invalid input. | 301 // help detecting memory leaks in the code paths that detect invalid input. |
302 policy_3rdparty.Set("invalid-domain.component", policy_dict.DeepCopy()); | 302 policy_3rdparty.Set("invalid-domain.component", policy_dict.DeepCopy()); |
303 policy_3rdparty.Set("extensions.cccccccccccccccccccccccccccccccc", | 303 policy_3rdparty.Set("extensions.cccccccccccccccccccccccccccccccc", |
304 base::Value::CreateStringValue("invalid-value")); | 304 base::Value::CreateStringValue("invalid-value")); |
305 test_harness_->Install3rdPartyPolicy(&policy_3rdparty); | 305 test_harness_->Install3rdPartyPolicy(&policy_3rdparty); |
306 | 306 |
307 provider_->RefreshPolicies(); | 307 provider_->RefreshPolicies(); |
308 loop_.RunAllPending(); | 308 loop_.RunUntilIdle(); |
309 | 309 |
310 PolicyMap expected_policy; | 310 PolicyMap expected_policy; |
311 expected_policy.Set(test_policy_definitions::kKeyDictionary, | 311 expected_policy.Set(test_policy_definitions::kKeyDictionary, |
312 test_harness_->policy_level(), | 312 test_harness_->policy_level(), |
313 test_harness_->policy_scope(), | 313 test_harness_->policy_scope(), |
314 policy_dict.DeepCopy()); | 314 policy_dict.DeepCopy()); |
315 PolicyBundle expected_bundle; | 315 PolicyBundle expected_bundle; |
316 expected_bundle.Get(POLICY_DOMAIN_CHROME, "").CopyFrom(expected_policy); | 316 expected_bundle.Get(POLICY_DOMAIN_CHROME, "").CopyFrom(expected_policy); |
317 expected_policy.Clear(); | 317 expected_policy.Clear(); |
318 expected_policy.LoadFrom(&policy_dict, | 318 expected_policy.LoadFrom(&policy_dict, |
319 test_harness_->policy_level(), | 319 test_harness_->policy_level(), |
320 test_harness_->policy_scope()); | 320 test_harness_->policy_scope()); |
321 expected_bundle.Get(POLICY_DOMAIN_EXTENSIONS, | 321 expected_bundle.Get(POLICY_DOMAIN_EXTENSIONS, |
322 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") | 322 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") |
323 .CopyFrom(expected_policy); | 323 .CopyFrom(expected_policy); |
324 expected_bundle.Get(POLICY_DOMAIN_EXTENSIONS, | 324 expected_bundle.Get(POLICY_DOMAIN_EXTENSIONS, |
325 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb") | 325 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb") |
326 .CopyFrom(expected_policy); | 326 .CopyFrom(expected_policy); |
327 EXPECT_TRUE(provider_->policies().Equals(expected_bundle)); | 327 EXPECT_TRUE(provider_->policies().Equals(expected_bundle)); |
328 } | 328 } |
329 | 329 |
330 } // namespace policy | 330 } // namespace policy |
OLD | NEW |