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
3 // found in the LICENSE file.
7 #include "base/callback.h"
8 #include "base/files/file_path.h"
9 #include "base/prefs/pref_store_observer_mock.h"
10 #include "base/run_loop.h"
11 #include "components/policy/core/browser/configuration_policy_handler.h"
12 #include "components/policy/core/browser/configuration_policy_pref_store.h"
13 #include "components/policy/core/browser/configuration_policy_pref_store_test.h"
14 #include "components/policy/core/common/external_data_fetcher.h"
15 #include "components/policy/core/common/policy_details.h"
16 #include "components/policy/core/common/policy_map.h"
17 #include "components/policy/core/common/policy_pref_names.h"
18 #include "components/policy/core/common/policy_service_impl.h"
19 #include "testing/gmock/include/gmock/gmock.h"
21 // Note: this file should move to components/policy/core/browser, but the
22 // components_unittests runner does not load the ResourceBundle as
23 // ChromeTestSuite::Initialize does, which leads to failures using
27 using testing::Return;
32 const char kTestPolicy[] = "test.policy";
33 const char kTestPref[] = "test.pref";
39 // Test cases for list-valued policy settings.
40 class ConfigurationPolicyPrefStoreListTest
41 : public ConfigurationPolicyPrefStoreTest {
42 void SetUp() override {
43 handler_list_.AddHandler(
44 make_scoped_ptr<ConfigurationPolicyHandler>(new SimplePolicyHandler(
45 kTestPolicy, kTestPref, base::Value::TYPE_LIST)));
49 TEST_F(ConfigurationPolicyPrefStoreListTest, GetDefault) {
50 EXPECT_FALSE(store_->GetValue(kTestPref, NULL));
53 TEST_F(ConfigurationPolicyPrefStoreListTest, SetValue) {
54 base::ListValue* in_value = new base::ListValue();
55 in_value->Append(new base::StringValue("test1"));
56 in_value->Append(new base::StringValue("test2,"));
58 policy.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
59 POLICY_SCOPE_USER, in_value, NULL);
60 UpdateProviderPolicy(policy);
61 const base::Value* value = NULL;
62 EXPECT_TRUE(store_->GetValue(kTestPref, &value));
64 EXPECT_TRUE(in_value->Equals(value));
67 // Test cases for string-valued policy settings.
68 class ConfigurationPolicyPrefStoreStringTest
69 : public ConfigurationPolicyPrefStoreTest {
70 void SetUp() override {
71 handler_list_.AddHandler(
72 make_scoped_ptr<ConfigurationPolicyHandler>(new SimplePolicyHandler(
73 kTestPolicy, kTestPref, base::Value::TYPE_STRING)));
77 TEST_F(ConfigurationPolicyPrefStoreStringTest, GetDefault) {
78 EXPECT_FALSE(store_->GetValue(kTestPref, NULL));
81 TEST_F(ConfigurationPolicyPrefStoreStringTest, SetValue) {
83 policy.Set(kTestPolicy,
84 POLICY_LEVEL_MANDATORY,
86 new base::StringValue("http://chromium.org"),
88 UpdateProviderPolicy(policy);
89 const base::Value* value = NULL;
90 EXPECT_TRUE(store_->GetValue(kTestPref, &value));
92 EXPECT_TRUE(base::StringValue("http://chromium.org").Equals(value));
95 // Test cases for boolean-valued policy settings.
96 class ConfigurationPolicyPrefStoreBooleanTest
97 : public ConfigurationPolicyPrefStoreTest {
98 void SetUp() override {
99 handler_list_.AddHandler(
100 make_scoped_ptr<ConfigurationPolicyHandler>(new SimplePolicyHandler(
101 kTestPolicy, kTestPref, base::Value::TYPE_BOOLEAN)));
105 TEST_F(ConfigurationPolicyPrefStoreBooleanTest, GetDefault) {
106 EXPECT_FALSE(store_->GetValue(kTestPref, NULL));
109 TEST_F(ConfigurationPolicyPrefStoreBooleanTest, SetValue) {
111 policy.Set(kTestPolicy,
112 POLICY_LEVEL_MANDATORY,
114 new base::FundamentalValue(false),
116 UpdateProviderPolicy(policy);
117 const base::Value* value = NULL;
118 EXPECT_TRUE(store_->GetValue(kTestPref, &value));
120 bool boolean_value = true;
121 bool result = value->GetAsBoolean(&boolean_value);
123 EXPECT_FALSE(boolean_value);
125 policy.Set(kTestPolicy,
126 POLICY_LEVEL_MANDATORY,
128 new base::FundamentalValue(true),
130 UpdateProviderPolicy(policy);
132 EXPECT_TRUE(store_->GetValue(kTestPref, &value));
133 boolean_value = false;
134 result = value->GetAsBoolean(&boolean_value);
136 EXPECT_TRUE(boolean_value);
139 // Test cases for integer-valued policy settings.
140 class ConfigurationPolicyPrefStoreIntegerTest
141 : public ConfigurationPolicyPrefStoreTest {
142 void SetUp() override {
143 handler_list_.AddHandler(
144 make_scoped_ptr<ConfigurationPolicyHandler>(new SimplePolicyHandler(
145 kTestPolicy, kTestPref, base::Value::TYPE_INTEGER)));
149 TEST_F(ConfigurationPolicyPrefStoreIntegerTest, GetDefault) {
150 EXPECT_FALSE(store_->GetValue(kTestPref, NULL));
153 TEST_F(ConfigurationPolicyPrefStoreIntegerTest, SetValue) {
155 policy.Set(kTestPolicy,
156 POLICY_LEVEL_MANDATORY,
158 new base::FundamentalValue(2),
160 UpdateProviderPolicy(policy);
161 const base::Value* value = NULL;
162 EXPECT_TRUE(store_->GetValue(kTestPref, &value));
163 EXPECT_TRUE(base::FundamentalValue(2).Equals(value));
166 // Exercises the policy refresh mechanism.
167 class ConfigurationPolicyPrefStoreRefreshTest
168 : public ConfigurationPolicyPrefStoreTest {
170 void SetUp() override {
171 ConfigurationPolicyPrefStoreTest::SetUp();
172 store_->AddObserver(&observer_);
173 handler_list_.AddHandler(
174 make_scoped_ptr<ConfigurationPolicyHandler>(new SimplePolicyHandler(
175 kTestPolicy, kTestPref, base::Value::TYPE_STRING)));
178 void TearDown() override {
179 store_->RemoveObserver(&observer_);
180 ConfigurationPolicyPrefStoreTest::TearDown();
183 PrefStoreObserverMock observer_;
186 TEST_F(ConfigurationPolicyPrefStoreRefreshTest, Refresh) {
187 const base::Value* value = NULL;
188 EXPECT_FALSE(store_->GetValue(kTestPolicy, NULL));
191 policy.Set(kTestPolicy,
192 POLICY_LEVEL_MANDATORY,
194 new base::StringValue("http://www.chromium.org"),
196 UpdateProviderPolicy(policy);
197 observer_.VerifyAndResetChangedKey(kTestPref);
198 EXPECT_TRUE(store_->GetValue(kTestPref, &value));
199 EXPECT_TRUE(base::StringValue("http://www.chromium.org").Equals(value));
201 UpdateProviderPolicy(policy);
202 EXPECT_TRUE(observer_.changed_keys.empty());
204 policy.Erase(kTestPolicy);
205 UpdateProviderPolicy(policy);
206 observer_.VerifyAndResetChangedKey(kTestPref);
207 EXPECT_FALSE(store_->GetValue(kTestPref, NULL));
210 TEST_F(ConfigurationPolicyPrefStoreRefreshTest, Initialization) {
211 EXPECT_FALSE(store_->IsInitializationComplete());
212 EXPECT_CALL(provider_, IsInitializationComplete(POLICY_DOMAIN_CHROME))
213 .WillRepeatedly(Return(true));
215 UpdateProviderPolicy(policy);
216 EXPECT_TRUE(observer_.initialized);
217 EXPECT_TRUE(observer_.initialization_success);
218 Mock::VerifyAndClearExpectations(&observer_);
219 EXPECT_TRUE(store_->IsInitializationComplete());
222 } // namespace policy