OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include <string> | |
6 | |
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" | |
20 | |
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 | |
24 // PolicyErrorMap. | |
25 | |
26 using testing::Mock; | |
27 using testing::Return; | |
28 using testing::_; | |
29 | |
30 namespace { | |
31 | |
32 const char kTestPolicy[] = "test.policy"; | |
33 const char kTestPref[] = "test.pref"; | |
34 | |
35 } // namespace | |
36 | |
37 namespace policy { | |
38 | |
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))); | |
46 } | |
47 }; | |
48 | |
49 TEST_F(ConfigurationPolicyPrefStoreListTest, GetDefault) { | |
50 EXPECT_FALSE(store_->GetValue(kTestPref, NULL)); | |
51 } | |
52 | |
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,")); | |
57 PolicyMap policy; | |
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)); | |
63 ASSERT_TRUE(value); | |
64 EXPECT_TRUE(in_value->Equals(value)); | |
65 } | |
66 | |
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))); | |
74 } | |
75 }; | |
76 | |
77 TEST_F(ConfigurationPolicyPrefStoreStringTest, GetDefault) { | |
78 EXPECT_FALSE(store_->GetValue(kTestPref, NULL)); | |
79 } | |
80 | |
81 TEST_F(ConfigurationPolicyPrefStoreStringTest, SetValue) { | |
82 PolicyMap policy; | |
83 policy.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, | |
84 POLICY_SCOPE_USER, | |
85 base::Value::CreateStringValue("http://chromium.org"), NULL); | |
86 UpdateProviderPolicy(policy); | |
87 const base::Value* value = NULL; | |
88 EXPECT_TRUE(store_->GetValue(kTestPref, &value)); | |
89 ASSERT_TRUE(value); | |
90 EXPECT_TRUE(base::StringValue("http://chromium.org").Equals(value)); | |
91 } | |
92 | |
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))); | |
100 } | |
101 }; | |
102 | |
103 TEST_F(ConfigurationPolicyPrefStoreBooleanTest, GetDefault) { | |
104 EXPECT_FALSE(store_->GetValue(kTestPref, NULL)); | |
105 } | |
106 | |
107 TEST_F(ConfigurationPolicyPrefStoreBooleanTest, SetValue) { | |
108 PolicyMap policy; | |
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)); | |
114 ASSERT_TRUE(value); | |
115 bool boolean_value = true; | |
116 bool result = value->GetAsBoolean(&boolean_value); | |
117 ASSERT_TRUE(result); | |
118 EXPECT_FALSE(boolean_value); | |
119 | |
120 policy.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, | |
121 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL); | |
122 UpdateProviderPolicy(policy); | |
123 value = NULL; | |
124 EXPECT_TRUE(store_->GetValue(kTestPref, &value)); | |
125 boolean_value = false; | |
126 result = value->GetAsBoolean(&boolean_value); | |
127 ASSERT_TRUE(result); | |
128 EXPECT_TRUE(boolean_value); | |
129 } | |
130 | |
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))); | |
138 } | |
139 }; | |
140 | |
141 TEST_F(ConfigurationPolicyPrefStoreIntegerTest, GetDefault) { | |
142 EXPECT_FALSE(store_->GetValue(kTestPref, NULL)); | |
143 } | |
144 | |
145 TEST_F(ConfigurationPolicyPrefStoreIntegerTest, SetValue) { | |
146 PolicyMap policy; | |
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)); | |
153 } | |
154 | |
155 // Exercises the policy refresh mechanism. | |
156 class ConfigurationPolicyPrefStoreRefreshTest | |
157 : public ConfigurationPolicyPrefStoreTest { | |
158 protected: | |
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))); | |
165 } | |
166 | |
167 virtual void TearDown() OVERRIDE { | |
168 store_->RemoveObserver(&observer_); | |
169 ConfigurationPolicyPrefStoreTest::TearDown(); | |
170 } | |
171 | |
172 PrefStoreObserverMock observer_; | |
173 }; | |
174 | |
175 TEST_F(ConfigurationPolicyPrefStoreRefreshTest, Refresh) { | |
176 const base::Value* value = NULL; | |
177 EXPECT_FALSE(store_->GetValue(kTestPolicy, NULL)); | |
178 | |
179 EXPECT_CALL(observer_, OnPrefValueChanged(kTestPref)).Times(1); | |
180 PolicyMap policy; | |
181 policy.Set(kTestPolicy, | |
182 POLICY_LEVEL_MANDATORY, | |
183 POLICY_SCOPE_USER, | |
184 base::Value::CreateStringValue("http://www.chromium.org"), | |
185 NULL); | |
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)); | |
190 | |
191 EXPECT_CALL(observer_, OnPrefValueChanged(_)).Times(0); | |
192 UpdateProviderPolicy(policy); | |
193 Mock::VerifyAndClearExpectations(&observer_); | |
194 | |
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)); | |
200 } | |
201 | |
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); | |
207 PolicyMap policy; | |
208 UpdateProviderPolicy(policy); | |
209 Mock::VerifyAndClearExpectations(&observer_); | |
210 EXPECT_TRUE(store_->IsInitializationComplete()); | |
211 } | |
212 | |
213 } // namespace policy | |
OLD | NEW |