| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/values.h" | 8 #include "base/values.h" |
| 9 #include "chrome/browser/policy/asynchronous_policy_loader.h" | 9 #include "chrome/browser/policy/asynchronous_policy_loader.h" |
| 10 #include "chrome/browser/policy/asynchronous_policy_provider.h" | 10 #include "chrome/browser/policy/asynchronous_policy_provider.h" |
| 11 #include "chrome/browser/policy/configuration_policy_provider.h" | 11 #include "chrome/browser/policy/configuration_policy_provider.h" |
| 12 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 12 #include "chrome/browser/policy/policy_map.h" | 13 #include "chrome/browser/policy/policy_map.h" |
| 13 #include "policy/policy_constants.h" | 14 #include "policy/policy_constants.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 |
| 17 using ::testing::Mock; |
| 18 using ::testing::_; |
| 14 | 19 |
| 15 namespace policy { | 20 namespace policy { |
| 16 | 21 |
| 17 namespace test_policy_definitions { | 22 namespace test_policy_definitions { |
| 18 | 23 |
| 19 const char kKeyString[] = "StringPolicy"; | 24 const char kKeyString[] = "StringPolicy"; |
| 20 const char kKeyBoolean[] = "BooleanPolicy"; | 25 const char kKeyBoolean[] = "BooleanPolicy"; |
| 21 const char kKeyInteger[] = "IntegerPolicy"; | 26 const char kKeyInteger[] = "IntegerPolicy"; |
| 22 const char kKeyStringList[] = "StringListPolicy"; | 27 const char kKeyStringList[] = "StringListPolicy"; |
| 23 | 28 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 49 ConfigurationPolicyProviderTest::~ConfigurationPolicyProviderTest() {} | 54 ConfigurationPolicyProviderTest::~ConfigurationPolicyProviderTest() {} |
| 50 | 55 |
| 51 void ConfigurationPolicyProviderTest::SetUp() { | 56 void ConfigurationPolicyProviderTest::SetUp() { |
| 52 AsynchronousPolicyTestBase::SetUp(); | 57 AsynchronousPolicyTestBase::SetUp(); |
| 53 | 58 |
| 54 test_harness_.reset((*GetParam())()); | 59 test_harness_.reset((*GetParam())()); |
| 55 test_harness_->SetUp(); | 60 test_harness_->SetUp(); |
| 56 | 61 |
| 57 provider_.reset( | 62 provider_.reset( |
| 58 test_harness_->CreateProvider(&test_policy_definitions::kList)); | 63 test_harness_->CreateProvider(&test_policy_definitions::kList)); |
| 64 // Some providers do a reload on init. Make sure any notifications generated |
| 65 // are fired now. |
| 66 loop_.RunAllPending(); |
| 59 | 67 |
| 60 PolicyMap policy_map; | 68 PolicyMap policy_map; |
| 61 EXPECT_TRUE(provider_->Provide(&policy_map)); | 69 EXPECT_TRUE(provider_->Provide(&policy_map)); |
| 62 EXPECT_TRUE(policy_map.empty()); | 70 EXPECT_TRUE(policy_map.empty()); |
| 63 } | 71 } |
| 64 | 72 |
| 65 void ConfigurationPolicyProviderTest::TearDown() { | 73 void ConfigurationPolicyProviderTest::TearDown() { |
| 66 // Give providers the chance to clean up after themselves on the file thread. | 74 // Give providers the chance to clean up after themselves on the file thread. |
| 67 provider_.reset(); | 75 provider_.reset(); |
| 68 | 76 |
| 69 AsynchronousPolicyTestBase::TearDown(); | 77 AsynchronousPolicyTestBase::TearDown(); |
| 70 } | 78 } |
| 71 | 79 |
| 72 void ConfigurationPolicyProviderTest::CheckValue( | 80 void ConfigurationPolicyProviderTest::CheckValue( |
| 73 const char* policy_name, | 81 const char* policy_name, |
| 74 ConfigurationPolicyType policy_type, | 82 ConfigurationPolicyType policy_type, |
| 75 const base::Value& expected_value, | 83 const base::Value& expected_value, |
| 76 base::Closure install_value) { | 84 base::Closure install_value) { |
| 77 // Install the value, reload policy and check the provider for the value. | 85 // Install the value, reload policy and check the provider for the value. |
| 78 install_value.Run(); | 86 install_value.Run(); |
| 79 provider_->ForceReload(); | 87 provider_->RefreshPolicies(); |
| 80 loop_.RunAllPending(); | 88 loop_.RunAllPending(); |
| 81 PolicyMap policy_map; | 89 PolicyMap policy_map; |
| 82 EXPECT_TRUE(provider_->Provide(&policy_map)); | 90 EXPECT_TRUE(provider_->Provide(&policy_map)); |
| 83 EXPECT_EQ(1U, policy_map.size()); | 91 EXPECT_EQ(1U, policy_map.size()); |
| 84 EXPECT_TRUE(base::Value::Equals(&expected_value, | 92 EXPECT_TRUE(base::Value::Equals(&expected_value, |
| 85 policy_map.Get(policy_type))); | 93 policy_map.Get(policy_type))); |
| 86 } | 94 } |
| 87 | 95 |
| 88 TEST_P(ConfigurationPolicyProviderTest, Empty) { | 96 TEST_P(ConfigurationPolicyProviderTest, Empty) { |
| 89 provider_->ForceReload(); | 97 provider_->RefreshPolicies(); |
| 90 loop_.RunAllPending(); | 98 loop_.RunAllPending(); |
| 91 PolicyMap policy_map; | 99 PolicyMap policy_map; |
| 92 EXPECT_TRUE(provider_->Provide(&policy_map)); | 100 EXPECT_TRUE(provider_->Provide(&policy_map)); |
| 93 EXPECT_TRUE(policy_map.empty()); | 101 EXPECT_TRUE(policy_map.empty()); |
| 94 } | 102 } |
| 95 | 103 |
| 96 TEST_P(ConfigurationPolicyProviderTest, StringValue) { | 104 TEST_P(ConfigurationPolicyProviderTest, StringValue) { |
| 97 const char kTestString[] = "string_value"; | 105 const char kTestString[] = "string_value"; |
| 98 StringValue expected_value(kTestString); | 106 StringValue expected_value(kTestString); |
| 99 CheckValue(test_policy_definitions::kKeyString, | 107 CheckValue(test_policy_definitions::kKeyString, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 expected_value.Set(1U, base::Value::CreateStringValue("second")); | 141 expected_value.Set(1U, base::Value::CreateStringValue("second")); |
| 134 CheckValue(test_policy_definitions::kKeyStringList, | 142 CheckValue(test_policy_definitions::kKeyStringList, |
| 135 test_policy_definitions::kPolicyStringList, | 143 test_policy_definitions::kPolicyStringList, |
| 136 expected_value, | 144 expected_value, |
| 137 base::Bind(&PolicyProviderTestHarness::InstallStringListPolicy, | 145 base::Bind(&PolicyProviderTestHarness::InstallStringListPolicy, |
| 138 base::Unretained(test_harness_.get()), | 146 base::Unretained(test_harness_.get()), |
| 139 test_policy_definitions::kKeyStringList, | 147 test_policy_definitions::kKeyStringList, |
| 140 &expected_value)); | 148 &expected_value)); |
| 141 } | 149 } |
| 142 | 150 |
| 151 TEST_P(ConfigurationPolicyProviderTest, RefreshPolicies) { |
| 152 PolicyMap policy_map; |
| 153 EXPECT_TRUE(provider_->Provide(&policy_map)); |
| 154 EXPECT_EQ(0U, policy_map.size()); |
| 155 |
| 156 // OnUpdatePolicy is called even when there are no changes. |
| 157 MockConfigurationPolicyObserver observer; |
| 158 ConfigurationPolicyObserverRegistrar registrar; |
| 159 registrar.Init(provider_.get(), &observer); |
| 160 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); |
| 161 provider_->RefreshPolicies(); |
| 162 loop_.RunAllPending(); |
| 163 Mock::VerifyAndClearExpectations(&observer); |
| 164 |
| 165 EXPECT_TRUE(provider_->Provide(&policy_map)); |
| 166 EXPECT_EQ(0U, policy_map.size()); |
| 167 |
| 168 // OnUpdatePolicy is called when there are changes. |
| 169 test_harness_->InstallStringPolicy(test_policy_definitions::kKeyString, |
| 170 "value"); |
| 171 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); |
| 172 provider_->RefreshPolicies(); |
| 173 loop_.RunAllPending(); |
| 174 Mock::VerifyAndClearExpectations(&observer); |
| 175 |
| 176 policy_map.Clear(); |
| 177 EXPECT_TRUE(provider_->Provide(&policy_map)); |
| 178 EXPECT_EQ(1U, policy_map.size()); |
| 179 } |
| 180 |
| 143 } // namespace policy | 181 } // namespace policy |
| OLD | NEW |