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" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "components/policy/core/common/external_data_fetcher.h" | 13 #include "components/policy/core/common/external_data_fetcher.h" |
14 #include "components/policy/core/common/policy_map.h" | 14 #include "components/policy/core/common/policy_map.h" |
| 15 #include "components/policy/core/common/policy_types.h" |
15 #include "content/public/test/test_browser_thread.h" | 16 #include "content/public/test/test_browser_thread.h" |
16 #include "extensions/browser/api/storage/settings_observer.h" | 17 #include "extensions/browser/api/storage/settings_observer.h" |
17 #include "extensions/browser/value_store/leveldb_value_store.h" | 18 #include "extensions/browser/value_store/leveldb_value_store.h" |
18 #include "extensions/browser/value_store/value_store_unittest.h" | 19 #include "extensions/browser/value_store/value_store_unittest.h" |
19 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 | 22 |
22 using testing::_; | 23 using testing::_; |
23 using testing::Mock; | 24 using testing::Mock; |
24 | 25 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 content::TestBrowserThread file_thread_; | 114 content::TestBrowserThread file_thread_; |
114 scoped_ptr<PolicyValueStore> store_; | 115 scoped_ptr<PolicyValueStore> store_; |
115 MockSettingsObserver observer_; | 116 MockSettingsObserver observer_; |
116 scoped_refptr<SettingsObserverList> observers_; | 117 scoped_refptr<SettingsObserverList> observers_; |
117 }; | 118 }; |
118 | 119 |
119 TEST_F(PolicyValueStoreTest, DontProvideRecommendedPolicies) { | 120 TEST_F(PolicyValueStoreTest, DontProvideRecommendedPolicies) { |
120 policy::PolicyMap policies; | 121 policy::PolicyMap policies; |
121 base::FundamentalValue expected(123); | 122 base::FundamentalValue expected(123); |
122 policies.Set("must", policy::POLICY_LEVEL_MANDATORY, | 123 policies.Set("must", policy::POLICY_LEVEL_MANDATORY, |
123 policy::POLICY_SCOPE_USER, expected.DeepCopy(), NULL); | 124 policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD, |
| 125 expected.DeepCopy(), nullptr); |
124 policies.Set("may", policy::POLICY_LEVEL_RECOMMENDED, | 126 policies.Set("may", policy::POLICY_LEVEL_RECOMMENDED, |
125 policy::POLICY_SCOPE_USER, | 127 policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD, |
126 new base::FundamentalValue(456), NULL); | 128 new base::FundamentalValue(456), NULL); |
127 store_->SetCurrentPolicy(policies); | 129 store_->SetCurrentPolicy(policies); |
128 ValueStore::ReadResult result = store_->Get(); | 130 ValueStore::ReadResult result = store_->Get(); |
129 ASSERT_FALSE(result->HasError()); | 131 ASSERT_FALSE(result->HasError()); |
130 EXPECT_EQ(1u, result->settings().size()); | 132 EXPECT_EQ(1u, result->settings().size()); |
131 base::Value* value = NULL; | 133 base::Value* value = NULL; |
132 EXPECT_FALSE(result->settings().Get("may", &value)); | 134 EXPECT_FALSE(result->settings().Get("may", &value)); |
133 EXPECT_TRUE(result->settings().Get("must", &value)); | 135 EXPECT_TRUE(result->settings().Get("must", &value)); |
134 EXPECT_TRUE(base::Value::Equals(&expected, value)); | 136 EXPECT_TRUE(base::Value::Equals(&expected, value)); |
135 } | 137 } |
(...skipping 22 matching lines...) Expand all Loading... |
158 ValueStoreChangeList changes; | 160 ValueStoreChangeList changes; |
159 changes.push_back(ValueStoreChange("aaa", NULL, value.DeepCopy())); | 161 changes.push_back(ValueStoreChange("aaa", NULL, value.DeepCopy())); |
160 EXPECT_CALL(observer_, | 162 EXPECT_CALL(observer_, |
161 OnSettingsChanged(kTestExtensionId, | 163 OnSettingsChanged(kTestExtensionId, |
162 settings_namespace::MANAGED, | 164 settings_namespace::MANAGED, |
163 ValueStoreChange::ToJson(changes))); | 165 ValueStoreChange::ToJson(changes))); |
164 } | 166 } |
165 | 167 |
166 policy::PolicyMap policies; | 168 policy::PolicyMap policies; |
167 policies.Set("aaa", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | 169 policies.Set("aaa", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, |
168 value.DeepCopy(), NULL); | 170 policy::POLICY_SOURCE_CLOUD, value.DeepCopy(), nullptr); |
169 store_->SetCurrentPolicy(policies); | 171 store_->SetCurrentPolicy(policies); |
170 loop_.RunUntilIdle(); | 172 loop_.RunUntilIdle(); |
171 Mock::VerifyAndClearExpectations(&observer_); | 173 Mock::VerifyAndClearExpectations(&observer_); |
172 | 174 |
173 // Notify when new policies are added. | 175 // Notify when new policies are added. |
174 { | 176 { |
175 ValueStoreChangeList changes; | 177 ValueStoreChangeList changes; |
176 changes.push_back(ValueStoreChange("bbb", NULL, value.DeepCopy())); | 178 changes.push_back(ValueStoreChange("bbb", NULL, value.DeepCopy())); |
177 EXPECT_CALL(observer_, | 179 EXPECT_CALL(observer_, |
178 OnSettingsChanged(kTestExtensionId, | 180 OnSettingsChanged(kTestExtensionId, |
179 settings_namespace::MANAGED, | 181 settings_namespace::MANAGED, |
180 ValueStoreChange::ToJson(changes))); | 182 ValueStoreChange::ToJson(changes))); |
181 } | 183 } |
182 | 184 |
183 policies.Set("bbb", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | 185 policies.Set("bbb", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, |
184 value.DeepCopy(), NULL); | 186 policy::POLICY_SOURCE_CLOUD, value.DeepCopy(), nullptr); |
185 store_->SetCurrentPolicy(policies); | 187 store_->SetCurrentPolicy(policies); |
186 loop_.RunUntilIdle(); | 188 loop_.RunUntilIdle(); |
187 Mock::VerifyAndClearExpectations(&observer_); | 189 Mock::VerifyAndClearExpectations(&observer_); |
188 | 190 |
189 // Notify when policies change. | 191 // Notify when policies change. |
190 const base::StringValue new_value("222"); | 192 const base::StringValue new_value("222"); |
191 { | 193 { |
192 ValueStoreChangeList changes; | 194 ValueStoreChangeList changes; |
193 changes.push_back( | 195 changes.push_back( |
194 ValueStoreChange("bbb", value.DeepCopy(), new_value.DeepCopy())); | 196 ValueStoreChange("bbb", value.DeepCopy(), new_value.DeepCopy())); |
195 EXPECT_CALL(observer_, | 197 EXPECT_CALL(observer_, |
196 OnSettingsChanged(kTestExtensionId, | 198 OnSettingsChanged(kTestExtensionId, |
197 settings_namespace::MANAGED, | 199 settings_namespace::MANAGED, |
198 ValueStoreChange::ToJson(changes))); | 200 ValueStoreChange::ToJson(changes))); |
199 } | 201 } |
200 | 202 |
201 policies.Set("bbb", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | 203 policies.Set("bbb", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, |
202 new_value.DeepCopy(), NULL); | 204 policy::POLICY_SOURCE_CLOUD, new_value.DeepCopy(), nullptr); |
203 store_->SetCurrentPolicy(policies); | 205 store_->SetCurrentPolicy(policies); |
204 loop_.RunUntilIdle(); | 206 loop_.RunUntilIdle(); |
205 Mock::VerifyAndClearExpectations(&observer_); | 207 Mock::VerifyAndClearExpectations(&observer_); |
206 | 208 |
207 // Notify when policies are removed. | 209 // Notify when policies are removed. |
208 { | 210 { |
209 ValueStoreChangeList changes; | 211 ValueStoreChangeList changes; |
210 changes.push_back(ValueStoreChange("bbb", new_value.DeepCopy(), NULL)); | 212 changes.push_back(ValueStoreChange("bbb", new_value.DeepCopy(), NULL)); |
211 EXPECT_CALL(observer_, | 213 EXPECT_CALL(observer_, |
212 OnSettingsChanged(kTestExtensionId, | 214 OnSettingsChanged(kTestExtensionId, |
213 settings_namespace::MANAGED, | 215 settings_namespace::MANAGED, |
214 ValueStoreChange::ToJson(changes))); | 216 ValueStoreChange::ToJson(changes))); |
215 } | 217 } |
216 | 218 |
217 policies.Erase("bbb"); | 219 policies.Erase("bbb"); |
218 store_->SetCurrentPolicy(policies); | 220 store_->SetCurrentPolicy(policies); |
219 loop_.RunUntilIdle(); | 221 loop_.RunUntilIdle(); |
220 Mock::VerifyAndClearExpectations(&observer_); | 222 Mock::VerifyAndClearExpectations(&observer_); |
221 | 223 |
222 // Don't notify when there aren't any changes. | 224 // Don't notify when there aren't any changes. |
223 EXPECT_CALL(observer_, OnSettingsChanged(_, _, _)).Times(0); | 225 EXPECT_CALL(observer_, OnSettingsChanged(_, _, _)).Times(0); |
224 store_->SetCurrentPolicy(policies); | 226 store_->SetCurrentPolicy(policies); |
225 loop_.RunUntilIdle(); | 227 loop_.RunUntilIdle(); |
226 Mock::VerifyAndClearExpectations(&observer_); | 228 Mock::VerifyAndClearExpectations(&observer_); |
227 } | 229 } |
228 | 230 |
229 } // namespace extensions | 231 } // namespace extensions |
OLD | NEW |