Chromium Code Reviews| 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 ValueStoreChangeList changes; | |
| 158 base::StringValue value("111"); | |
| 159 changes.push_back(ValueStoreChange("aaa", NULL, value.DeepCopy())); | |
| 160 EXPECT_CALL(observer_, OnSettingsChanged(kTestExtensionId, | |
| 161 settings_namespace::MANAGED, | |
| 162 ValueStoreChange::ToJson(changes))); | |
| 156 policy::PolicyMap policies; | 163 policy::PolicyMap policies; |
| 157 policies.Set("aaa", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | 164 policies.Set("aaa", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, |
| 158 new base::StringValue("111"), NULL); | 165 value.DeepCopy(), NULL); |
| 159 EXPECT_CALL(observer_, OnSettingsChanged(_, _, _)).Times(0); | 166 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(); | 167 loop_.RunUntilIdle(); |
| 173 Mock::VerifyAndClearExpectations(&observer_); | 168 Mock::VerifyAndClearExpectations(&observer_); |
| 174 | 169 |
| 175 // Notify when new policies are added. | 170 // Notify when new policies are added. |
| 176 ValueStoreChangeList changes; | 171 changes.clear(); |
|
not at google - send to devlin
2013/11/13 00:30:18
I prefer scoping these sorts of thing explicitly u
Joao da Silva
2013/11/13 08:56:47
Done.
| |
| 177 base::StringValue value("333"); | 172 changes.push_back(ValueStoreChange("bbb", NULL, value.DeepCopy())); |
| 178 changes.push_back(ValueStoreChange("ccc", NULL, value.DeepCopy())); | |
| 179 policies.Set("ccc", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | |
| 180 value.DeepCopy(), NULL); | |
| 181 EXPECT_CALL(observer_, OnSettingsChanged(kTestExtensionId, | 173 EXPECT_CALL(observer_, OnSettingsChanged(kTestExtensionId, |
| 182 settings_namespace::MANAGED, | 174 settings_namespace::MANAGED, |
| 183 ValueStoreChange::ToJson(changes))); | 175 ValueStoreChange::ToJson(changes))); |
| 184 store_->SetCurrentPolicy(policies, true); | 176 policies.Set("bbb", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, |
| 177 value.DeepCopy(), NULL); | |
| 178 store_->SetCurrentPolicy(policies); | |
| 185 loop_.RunUntilIdle(); | 179 loop_.RunUntilIdle(); |
| 186 Mock::VerifyAndClearExpectations(&observer_); | 180 Mock::VerifyAndClearExpectations(&observer_); |
| 187 | 181 |
| 188 // Notify when policies change. | 182 // Notify when policies change. |
| 189 changes.clear(); | 183 changes.clear(); |
| 190 base::StringValue new_value("444"); | 184 base::StringValue new_value("222"); |
| 191 changes.push_back( | 185 changes.push_back( |
| 192 ValueStoreChange("ccc", value.DeepCopy(), new_value.DeepCopy())); | 186 ValueStoreChange("bbb", value.DeepCopy(), new_value.DeepCopy())); |
| 193 policies.Set("ccc", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | |
| 194 new_value.DeepCopy(), NULL); | |
| 195 EXPECT_CALL(observer_, OnSettingsChanged(kTestExtensionId, | 187 EXPECT_CALL(observer_, OnSettingsChanged(kTestExtensionId, |
| 196 settings_namespace::MANAGED, | 188 settings_namespace::MANAGED, |
| 197 ValueStoreChange::ToJson(changes))); | 189 ValueStoreChange::ToJson(changes))); |
| 198 store_->SetCurrentPolicy(policies, true); | 190 policies.Set("bbb", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, |
| 191 new_value.DeepCopy(), NULL); | |
| 192 store_->SetCurrentPolicy(policies); | |
| 199 loop_.RunUntilIdle(); | 193 loop_.RunUntilIdle(); |
| 200 Mock::VerifyAndClearExpectations(&observer_); | 194 Mock::VerifyAndClearExpectations(&observer_); |
| 201 | 195 |
| 202 // Notify when policies are removed. | 196 // Notify when policies are removed. |
| 203 changes.clear(); | 197 changes.clear(); |
| 204 changes.push_back(ValueStoreChange("ccc", new_value.DeepCopy(), NULL)); | 198 changes.push_back(ValueStoreChange("bbb", new_value.DeepCopy(), NULL)); |
| 205 policies.Erase("ccc"); | |
| 206 EXPECT_CALL(observer_, OnSettingsChanged(kTestExtensionId, | 199 EXPECT_CALL(observer_, OnSettingsChanged(kTestExtensionId, |
| 207 settings_namespace::MANAGED, | 200 settings_namespace::MANAGED, |
| 208 ValueStoreChange::ToJson(changes))); | 201 ValueStoreChange::ToJson(changes))); |
| 209 store_->SetCurrentPolicy(policies, true); | 202 policies.Erase("bbb"); |
| 203 store_->SetCurrentPolicy(policies); | |
| 210 loop_.RunUntilIdle(); | 204 loop_.RunUntilIdle(); |
| 211 Mock::VerifyAndClearExpectations(&observer_); | 205 Mock::VerifyAndClearExpectations(&observer_); |
| 212 | 206 |
| 213 // Don't notify when there aren't changes. | 207 // Don't notify when there aren't any changes. |
| 214 EXPECT_CALL(observer_, OnSettingsChanged(_, _, _)).Times(0); | 208 EXPECT_CALL(observer_, OnSettingsChanged(_, _, _)).Times(0); |
| 215 store_->SetCurrentPolicy(policies, true); | 209 store_->SetCurrentPolicy(policies); |
| 216 loop_.RunUntilIdle(); | 210 loop_.RunUntilIdle(); |
| 217 Mock::VerifyAndClearExpectations(&observer_); | 211 Mock::VerifyAndClearExpectations(&observer_); |
| 218 } | 212 } |
| 219 | 213 |
| 220 } // namespace extensions | 214 } // namespace extensions |
| OLD | NEW |