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/extensions/api/storage/policy_value_store.h" | 5 #include "chrome/browser/extensions/api/storage/policy_value_store.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 }; | 118 }; |
119 | 119 |
120 TEST_F(PolicyValueStoreTest, DontProvideRecommendedPolicies) { | 120 TEST_F(PolicyValueStoreTest, DontProvideRecommendedPolicies) { |
121 policy::PolicyMap policies; | 121 policy::PolicyMap policies; |
122 base::FundamentalValue expected(123); | 122 base::FundamentalValue expected(123); |
123 policies.Set("must", policy::POLICY_LEVEL_MANDATORY, | 123 policies.Set("must", policy::POLICY_LEVEL_MANDATORY, |
124 policy::POLICY_SCOPE_USER, expected.DeepCopy(), NULL); | 124 policy::POLICY_SCOPE_USER, expected.DeepCopy(), NULL); |
125 policies.Set("may", policy::POLICY_LEVEL_RECOMMENDED, | 125 policies.Set("may", policy::POLICY_LEVEL_RECOMMENDED, |
126 policy::POLICY_SCOPE_USER, | 126 policy::POLICY_SCOPE_USER, |
127 new base::FundamentalValue(456), NULL); | 127 new base::FundamentalValue(456), NULL); |
128 store_->SetCurrentPolicy(policies, false); | 128 store_->SetCurrentPolicy(policies); |
129 ValueStore::ReadResult result = store_->Get(); | 129 ValueStore::ReadResult result = store_->Get(); |
130 ASSERT_FALSE(result->HasError()); | 130 ASSERT_FALSE(result->HasError()); |
131 EXPECT_EQ(1u, result->settings().size()); | 131 EXPECT_EQ(1u, result->settings().size()); |
132 base::Value* value = NULL; | 132 base::Value* value = NULL; |
133 EXPECT_FALSE(result->settings().Get("may", &value)); | 133 EXPECT_FALSE(result->settings().Get("may", &value)); |
134 EXPECT_TRUE(result->settings().Get("must", &value)); | 134 EXPECT_TRUE(result->settings().Get("must", &value)); |
135 EXPECT_TRUE(base::Value::Equals(&expected, value)); | 135 EXPECT_TRUE(base::Value::Equals(&expected, value)); |
136 } | 136 } |
137 | 137 |
138 TEST_F(PolicyValueStoreTest, ReadOnly) { | 138 TEST_F(PolicyValueStoreTest, ReadOnly) { |
139 ValueStore::WriteOptions options = ValueStore::DEFAULTS; | 139 ValueStore::WriteOptions options = ValueStore::DEFAULTS; |
140 | 140 |
141 base::StringValue string_value("value"); | 141 base::StringValue string_value("value"); |
142 EXPECT_TRUE(store_->Set(options, "key", string_value)->HasError()); | 142 EXPECT_TRUE(store_->Set(options, "key", string_value)->HasError()); |
143 | 143 |
144 base::DictionaryValue dict; | 144 base::DictionaryValue dict; |
145 dict.SetString("key", "value"); | 145 dict.SetString("key", "value"); |
146 EXPECT_TRUE(store_->Set(options, dict)->HasError()); | 146 EXPECT_TRUE(store_->Set(options, dict)->HasError()); |
147 | 147 |
148 EXPECT_TRUE(store_->Remove("key")->HasError()); | 148 EXPECT_TRUE(store_->Remove("key")->HasError()); |
149 std::vector<std::string> keys; | 149 std::vector<std::string> keys; |
150 keys.push_back("key"); | 150 keys.push_back("key"); |
151 EXPECT_TRUE(store_->Remove(keys)->HasError()); | 151 EXPECT_TRUE(store_->Remove(keys)->HasError()); |
152 EXPECT_TRUE(store_->Clear()->HasError()); | 152 EXPECT_TRUE(store_->Clear()->HasError()); |
153 } | 153 } |
154 | 154 |
155 TEST_F(PolicyValueStoreTest, NotifyOnChanges) { | 155 TEST_F(PolicyValueStoreTest, NotifyOnChanges) { |
| 156 // Notify when setting the initial policy. |
| 157 const base::StringValue value("111"); |
| 158 { |
| 159 ValueStoreChangeList changes; |
| 160 changes.push_back(ValueStoreChange("aaa", NULL, value.DeepCopy())); |
| 161 EXPECT_CALL(observer_, |
| 162 OnSettingsChanged(kTestExtensionId, |
| 163 settings_namespace::MANAGED, |
| 164 ValueStoreChange::ToJson(changes))); |
| 165 } |
| 166 |
156 policy::PolicyMap policies; | 167 policy::PolicyMap policies; |
157 policies.Set("aaa", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | 168 policies.Set("aaa", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, |
158 new base::StringValue("111"), NULL); | 169 value.DeepCopy(), NULL); |
159 EXPECT_CALL(observer_, OnSettingsChanged(_, _, _)).Times(0); | 170 store_->SetCurrentPolicy(policies); |
160 // No notification when setting the initial policy. | |
161 store_->SetCurrentPolicy(policies, false); | |
162 loop_.RunUntilIdle(); | |
163 Mock::VerifyAndClearExpectations(&observer_); | |
164 | |
165 // And no notifications on changes when not asked for. | |
166 policies.Set("aaa", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | |
167 new base::StringValue("222"), NULL); | |
168 policies.Set("bbb", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | |
169 new base::StringValue("223"), NULL); | |
170 EXPECT_CALL(observer_, OnSettingsChanged(_, _, _)).Times(0); | |
171 store_->SetCurrentPolicy(policies, false); | |
172 loop_.RunUntilIdle(); | 171 loop_.RunUntilIdle(); |
173 Mock::VerifyAndClearExpectations(&observer_); | 172 Mock::VerifyAndClearExpectations(&observer_); |
174 | 173 |
175 // Notify when new policies are added. | 174 // Notify when new policies are added. |
176 ValueStoreChangeList changes; | 175 { |
177 base::StringValue value("333"); | 176 ValueStoreChangeList changes; |
178 changes.push_back(ValueStoreChange("ccc", NULL, value.DeepCopy())); | 177 changes.push_back(ValueStoreChange("bbb", NULL, value.DeepCopy())); |
179 policies.Set("ccc", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | 178 EXPECT_CALL(observer_, |
| 179 OnSettingsChanged(kTestExtensionId, |
| 180 settings_namespace::MANAGED, |
| 181 ValueStoreChange::ToJson(changes))); |
| 182 } |
| 183 |
| 184 policies.Set("bbb", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, |
180 value.DeepCopy(), NULL); | 185 value.DeepCopy(), NULL); |
181 EXPECT_CALL(observer_, OnSettingsChanged(kTestExtensionId, | 186 store_->SetCurrentPolicy(policies); |
182 settings_namespace::MANAGED, | |
183 ValueStoreChange::ToJson(changes))); | |
184 store_->SetCurrentPolicy(policies, true); | |
185 loop_.RunUntilIdle(); | 187 loop_.RunUntilIdle(); |
186 Mock::VerifyAndClearExpectations(&observer_); | 188 Mock::VerifyAndClearExpectations(&observer_); |
187 | 189 |
188 // Notify when policies change. | 190 // Notify when policies change. |
189 changes.clear(); | 191 const base::StringValue new_value("222"); |
190 base::StringValue new_value("444"); | 192 { |
191 changes.push_back( | 193 ValueStoreChangeList changes; |
192 ValueStoreChange("ccc", value.DeepCopy(), new_value.DeepCopy())); | 194 changes.push_back( |
193 policies.Set("ccc", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | 195 ValueStoreChange("bbb", value.DeepCopy(), new_value.DeepCopy())); |
| 196 EXPECT_CALL(observer_, |
| 197 OnSettingsChanged(kTestExtensionId, |
| 198 settings_namespace::MANAGED, |
| 199 ValueStoreChange::ToJson(changes))); |
| 200 } |
| 201 |
| 202 policies.Set("bbb", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, |
194 new_value.DeepCopy(), NULL); | 203 new_value.DeepCopy(), NULL); |
195 EXPECT_CALL(observer_, OnSettingsChanged(kTestExtensionId, | 204 store_->SetCurrentPolicy(policies); |
196 settings_namespace::MANAGED, | |
197 ValueStoreChange::ToJson(changes))); | |
198 store_->SetCurrentPolicy(policies, true); | |
199 loop_.RunUntilIdle(); | 205 loop_.RunUntilIdle(); |
200 Mock::VerifyAndClearExpectations(&observer_); | 206 Mock::VerifyAndClearExpectations(&observer_); |
201 | 207 |
202 // Notify when policies are removed. | 208 // Notify when policies are removed. |
203 changes.clear(); | 209 { |
204 changes.push_back(ValueStoreChange("ccc", new_value.DeepCopy(), NULL)); | 210 ValueStoreChangeList changes; |
205 policies.Erase("ccc"); | 211 changes.push_back(ValueStoreChange("bbb", new_value.DeepCopy(), NULL)); |
206 EXPECT_CALL(observer_, OnSettingsChanged(kTestExtensionId, | 212 EXPECT_CALL(observer_, |
207 settings_namespace::MANAGED, | 213 OnSettingsChanged(kTestExtensionId, |
208 ValueStoreChange::ToJson(changes))); | 214 settings_namespace::MANAGED, |
209 store_->SetCurrentPolicy(policies, true); | 215 ValueStoreChange::ToJson(changes))); |
| 216 } |
| 217 |
| 218 policies.Erase("bbb"); |
| 219 store_->SetCurrentPolicy(policies); |
210 loop_.RunUntilIdle(); | 220 loop_.RunUntilIdle(); |
211 Mock::VerifyAndClearExpectations(&observer_); | 221 Mock::VerifyAndClearExpectations(&observer_); |
212 | 222 |
213 // Don't notify when there aren't changes. | 223 // Don't notify when there aren't any changes. |
214 EXPECT_CALL(observer_, OnSettingsChanged(_, _, _)).Times(0); | 224 EXPECT_CALL(observer_, OnSettingsChanged(_, _, _)).Times(0); |
215 store_->SetCurrentPolicy(policies, true); | 225 store_->SetCurrentPolicy(policies); |
216 loop_.RunUntilIdle(); | 226 loop_.RunUntilIdle(); |
217 Mock::VerifyAndClearExpectations(&observer_); | 227 Mock::VerifyAndClearExpectations(&observer_); |
218 } | 228 } |
219 | 229 |
220 } // namespace extensions | 230 } // namespace extensions |
OLD | NEW |