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
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 "chrome/browser/policy/configuration_policy_pref_store_test.h"
12 #include "components/policy/core/browser/configuration_policy_handler.h"
13 #include "components/policy/core/browser/configuration_policy_pref_store.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 virtual 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(base::Value::CreateStringValue("test1"));
56 in_value
->Append(base::Value::CreateStringValue("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 virtual 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
, POLICY_LEVEL_MANDATORY
,
85 base::Value::CreateStringValue("http://chromium.org"), NULL
);
86 UpdateProviderPolicy(policy
);
87 const base::Value
* value
= NULL
;
88 EXPECT_TRUE(store_
->GetValue(kTestPref
, &value
));
90 EXPECT_TRUE(base::StringValue("http://chromium.org").Equals(value
));
93 // Test cases for boolean-valued policy settings.
94 class ConfigurationPolicyPrefStoreBooleanTest
95 : public ConfigurationPolicyPrefStoreTest
{
96 virtual void SetUp() OVERRIDE
{
97 handler_list_
.AddHandler(
98 make_scoped_ptr
<ConfigurationPolicyHandler
>(new SimplePolicyHandler(
99 kTestPolicy
, kTestPref
, base::Value::TYPE_BOOLEAN
)));
103 TEST_F(ConfigurationPolicyPrefStoreBooleanTest
, GetDefault
) {
104 EXPECT_FALSE(store_
->GetValue(kTestPref
, NULL
));
107 TEST_F(ConfigurationPolicyPrefStoreBooleanTest
, SetValue
) {
109 policy
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
110 POLICY_SCOPE_USER
, base::Value::CreateBooleanValue(false), NULL
);
111 UpdateProviderPolicy(policy
);
112 const base::Value
* value
= NULL
;
113 EXPECT_TRUE(store_
->GetValue(kTestPref
, &value
));
115 bool boolean_value
= true;
116 bool result
= value
->GetAsBoolean(&boolean_value
);
118 EXPECT_FALSE(boolean_value
);
120 policy
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
121 POLICY_SCOPE_USER
, base::Value::CreateBooleanValue(true), NULL
);
122 UpdateProviderPolicy(policy
);
124 EXPECT_TRUE(store_
->GetValue(kTestPref
, &value
));
125 boolean_value
= false;
126 result
= value
->GetAsBoolean(&boolean_value
);
128 EXPECT_TRUE(boolean_value
);
131 // Test cases for integer-valued policy settings.
132 class ConfigurationPolicyPrefStoreIntegerTest
133 : public ConfigurationPolicyPrefStoreTest
{
134 virtual void SetUp() OVERRIDE
{
135 handler_list_
.AddHandler(
136 make_scoped_ptr
<ConfigurationPolicyHandler
>(new SimplePolicyHandler(
137 kTestPolicy
, kTestPref
, base::Value::TYPE_INTEGER
)));
141 TEST_F(ConfigurationPolicyPrefStoreIntegerTest
, GetDefault
) {
142 EXPECT_FALSE(store_
->GetValue(kTestPref
, NULL
));
145 TEST_F(ConfigurationPolicyPrefStoreIntegerTest
, SetValue
) {
147 policy
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
148 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(2), NULL
);
149 UpdateProviderPolicy(policy
);
150 const base::Value
* value
= NULL
;
151 EXPECT_TRUE(store_
->GetValue(kTestPref
, &value
));
152 EXPECT_TRUE(base::FundamentalValue(2).Equals(value
));
155 // Exercises the policy refresh mechanism.
156 class ConfigurationPolicyPrefStoreRefreshTest
157 : public ConfigurationPolicyPrefStoreTest
{
159 virtual void SetUp() OVERRIDE
{
160 ConfigurationPolicyPrefStoreTest::SetUp();
161 store_
->AddObserver(&observer_
);
162 handler_list_
.AddHandler(
163 make_scoped_ptr
<ConfigurationPolicyHandler
>(new SimplePolicyHandler(
164 kTestPolicy
, kTestPref
, base::Value::TYPE_STRING
)));
167 virtual void TearDown() OVERRIDE
{
168 store_
->RemoveObserver(&observer_
);
169 ConfigurationPolicyPrefStoreTest::TearDown();
172 PrefStoreObserverMock observer_
;
175 TEST_F(ConfigurationPolicyPrefStoreRefreshTest
, Refresh
) {
176 const base::Value
* value
= NULL
;
177 EXPECT_FALSE(store_
->GetValue(kTestPolicy
, NULL
));
179 EXPECT_CALL(observer_
, OnPrefValueChanged(kTestPref
)).Times(1);
181 policy
.Set(kTestPolicy
,
182 POLICY_LEVEL_MANDATORY
,
184 base::Value::CreateStringValue("http://www.chromium.org"),
186 UpdateProviderPolicy(policy
);
187 Mock::VerifyAndClearExpectations(&observer_
);
188 EXPECT_TRUE(store_
->GetValue(kTestPref
, &value
));
189 EXPECT_TRUE(base::StringValue("http://www.chromium.org").Equals(value
));
191 EXPECT_CALL(observer_
, OnPrefValueChanged(_
)).Times(0);
192 UpdateProviderPolicy(policy
);
193 Mock::VerifyAndClearExpectations(&observer_
);
195 EXPECT_CALL(observer_
, OnPrefValueChanged(kTestPref
)).Times(1);
196 policy
.Erase(kTestPolicy
);
197 UpdateProviderPolicy(policy
);
198 Mock::VerifyAndClearExpectations(&observer_
);
199 EXPECT_FALSE(store_
->GetValue(kTestPref
, NULL
));
202 TEST_F(ConfigurationPolicyPrefStoreRefreshTest
, Initialization
) {
203 EXPECT_FALSE(store_
->IsInitializationComplete());
204 EXPECT_CALL(provider_
, IsInitializationComplete(POLICY_DOMAIN_CHROME
))
205 .WillRepeatedly(Return(true));
206 EXPECT_CALL(observer_
, OnInitializationCompleted(true)).Times(1);
208 UpdateProviderPolicy(policy
);
209 Mock::VerifyAndClearExpectations(&observer_
);
210 EXPECT_TRUE(store_
->IsInitializationComplete());
213 } // namespace policy