| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "services/preferences/public/cpp/pref_observer_store.h" | 5 #include "services/preferences/public/cpp/pref_observer_store.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 TEST_F(PrefObserverStoreTest, Initialization) { | 105 TEST_F(PrefObserverStoreTest, Initialization) { |
| 106 std::set<std::string> keys; | 106 std::set<std::string> keys; |
| 107 const std::string key("hey"); | 107 const std::string key("hey"); |
| 108 keys.insert(key); | 108 keys.insert(key); |
| 109 store()->Init(keys); | 109 store()->Init(keys); |
| 110 | 110 |
| 111 EXPECT_FALSE(Initialized()); | 111 EXPECT_FALSE(Initialized()); |
| 112 EXPECT_FALSE(observer()->initialized); | 112 EXPECT_FALSE(observer()->initialized); |
| 113 | 113 |
| 114 const int kValue = 42; | 114 const int kValue = 42; |
| 115 base::FundamentalValue pref(kValue); | 115 base::Value pref(kValue); |
| 116 base::DictionaryValue prefs; | 116 base::DictionaryValue prefs; |
| 117 prefs.Set(key, pref.CreateDeepCopy()); | 117 prefs.Set(key, pref.CreateDeepCopy()); |
| 118 | 118 |
| 119 // PreferenceManager notifies of PreferencesChanged, completing | 119 // PreferenceManager notifies of PreferencesChanged, completing |
| 120 // initialization. | 120 // initialization. |
| 121 OnPreferencesChanged(prefs); | 121 OnPreferencesChanged(prefs); |
| 122 EXPECT_TRUE(Initialized()); | 122 EXPECT_TRUE(Initialized()); |
| 123 EXPECT_TRUE(observer()->initialized); | 123 EXPECT_TRUE(observer()->initialized); |
| 124 EXPECT_TRUE(observer()->initialization_success); | 124 EXPECT_TRUE(observer()->initialization_success); |
| 125 observer()->VerifyAndResetChangedKey(key); | 125 observer()->VerifyAndResetChangedKey(key); |
| 126 | 126 |
| 127 const base::Value* value = nullptr; | 127 const base::Value* value = nullptr; |
| 128 int actual_value; | 128 int actual_value; |
| 129 EXPECT_TRUE(store()->GetValue(key, &value)); | 129 EXPECT_TRUE(store()->GetValue(key, &value)); |
| 130 EXPECT_NE(nullptr, value); | 130 EXPECT_NE(nullptr, value); |
| 131 EXPECT_TRUE(value->GetAsInteger(&actual_value)); | 131 EXPECT_TRUE(value->GetAsInteger(&actual_value)); |
| 132 EXPECT_EQ(kValue, actual_value); | 132 EXPECT_EQ(kValue, actual_value); |
| 133 EXPECT_FALSE(manager()->set_preferences_called()); | 133 EXPECT_FALSE(manager()->set_preferences_called()); |
| 134 } | 134 } |
| 135 | 135 |
| 136 // Tests that values set silently are also set on the preference manager, but | 136 // Tests that values set silently are also set on the preference manager, but |
| 137 // that no observers are notified. | 137 // that no observers are notified. |
| 138 TEST_F(PrefObserverStoreTest, SetValueSilently) { | 138 TEST_F(PrefObserverStoreTest, SetValueSilently) { |
| 139 std::set<std::string> keys; | 139 std::set<std::string> keys; |
| 140 const std::string key("hey"); | 140 const std::string key("hey"); |
| 141 keys.insert(key); | 141 keys.insert(key); |
| 142 store()->Init(keys); | 142 store()->Init(keys); |
| 143 | 143 |
| 144 const int kValue = 42; | 144 const int kValue = 42; |
| 145 base::FundamentalValue pref(kValue); | 145 base::Value pref(kValue); |
| 146 store()->SetValueSilently(key, pref.CreateDeepCopy(), 0); | 146 store()->SetValueSilently(key, pref.CreateDeepCopy(), 0); |
| 147 base::RunLoop().RunUntilIdle(); | 147 base::RunLoop().RunUntilIdle(); |
| 148 EXPECT_TRUE(manager()->set_preferences_called()); | 148 EXPECT_TRUE(manager()->set_preferences_called()); |
| 149 EXPECT_TRUE(observer()->changed_keys.empty()); | 149 EXPECT_TRUE(observer()->changed_keys.empty()); |
| 150 } | 150 } |
| 151 | 151 |
| 152 // Test that reporting values changed notifies observers, and the preference | 152 // Test that reporting values changed notifies observers, and the preference |
| 153 // manager. | 153 // manager. |
| 154 TEST_F(PrefObserverStoreTest, ReportValueChanged) { | 154 TEST_F(PrefObserverStoreTest, ReportValueChanged) { |
| 155 std::set<std::string> keys; | 155 std::set<std::string> keys; |
| 156 const std::string key("hey"); | 156 const std::string key("hey"); |
| 157 keys.insert(key); | 157 keys.insert(key); |
| 158 store()->Init(keys); | 158 store()->Init(keys); |
| 159 | 159 |
| 160 const int kValue = 42; | 160 const int kValue = 42; |
| 161 base::FundamentalValue pref(kValue); | 161 base::Value pref(kValue); |
| 162 base::DictionaryValue prefs; | 162 base::DictionaryValue prefs; |
| 163 prefs.Set(key, pref.CreateDeepCopy()); | 163 prefs.Set(key, pref.CreateDeepCopy()); |
| 164 OnPreferencesChanged(prefs); | 164 OnPreferencesChanged(prefs); |
| 165 observer()->changed_keys.clear(); | 165 observer()->changed_keys.clear(); |
| 166 | 166 |
| 167 store()->ReportValueChanged(key, 0); | 167 store()->ReportValueChanged(key, 0); |
| 168 base::RunLoop().RunUntilIdle(); | 168 base::RunLoop().RunUntilIdle(); |
| 169 EXPECT_TRUE(manager()->set_preferences_called()); | 169 EXPECT_TRUE(manager()->set_preferences_called()); |
| 170 observer()->VerifyAndResetChangedKey(key); | 170 observer()->VerifyAndResetChangedKey(key); |
| 171 } | 171 } |
| 172 | 172 |
| 173 // Test that when initialized with multiple keys, that observers receive a | 173 // Test that when initialized with multiple keys, that observers receive a |
| 174 // notification for each key. | 174 // notification for each key. |
| 175 TEST_F(PrefObserverStoreTest, MultipleKeyInitialization) { | 175 TEST_F(PrefObserverStoreTest, MultipleKeyInitialization) { |
| 176 std::set<std::string> keys; | 176 std::set<std::string> keys; |
| 177 const std::string key1("hey"); | 177 const std::string key1("hey"); |
| 178 const std::string key2("listen"); | 178 const std::string key2("listen"); |
| 179 keys.insert(key1); | 179 keys.insert(key1); |
| 180 keys.insert(key2); | 180 keys.insert(key2); |
| 181 store()->Init(keys); | 181 store()->Init(keys); |
| 182 | 182 |
| 183 EXPECT_FALSE(Initialized()); | 183 EXPECT_FALSE(Initialized()); |
| 184 EXPECT_FALSE(observer()->initialized); | 184 EXPECT_FALSE(observer()->initialized); |
| 185 | 185 |
| 186 const int kValue = 42; | 186 const int kValue = 42; |
| 187 base::FundamentalValue pref1(kValue); | 187 base::Value pref1(kValue); |
| 188 const std::string kStringValue("look"); | 188 const std::string kStringValue("look"); |
| 189 base::StringValue pref2(kStringValue); | 189 base::StringValue pref2(kStringValue); |
| 190 | 190 |
| 191 base::DictionaryValue prefs; | 191 base::DictionaryValue prefs; |
| 192 prefs.Set(key1, pref1.CreateDeepCopy()); | 192 prefs.Set(key1, pref1.CreateDeepCopy()); |
| 193 prefs.Set(key2, pref2.CreateDeepCopy()); | 193 prefs.Set(key2, pref2.CreateDeepCopy()); |
| 194 | 194 |
| 195 // The observer should be notified of all keys set. | 195 // The observer should be notified of all keys set. |
| 196 OnPreferencesChanged(prefs); | 196 OnPreferencesChanged(prefs); |
| 197 EXPECT_TRUE(Initialized()); | 197 EXPECT_TRUE(Initialized()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 208 // Tests that if OnPreferencesChanged is received with invalid keys, that they | 208 // Tests that if OnPreferencesChanged is received with invalid keys, that they |
| 209 // are ignored. | 209 // are ignored. |
| 210 TEST_F(PrefObserverStoreTest, InvalidInitialization) { | 210 TEST_F(PrefObserverStoreTest, InvalidInitialization) { |
| 211 std::set<std::string> keys; | 211 std::set<std::string> keys; |
| 212 const std::string key("hey"); | 212 const std::string key("hey"); |
| 213 keys.insert(key); | 213 keys.insert(key); |
| 214 store()->Init(keys); | 214 store()->Init(keys); |
| 215 | 215 |
| 216 const std::string kInvalidKey("look"); | 216 const std::string kInvalidKey("look"); |
| 217 const int kValue = 42; | 217 const int kValue = 42; |
| 218 base::FundamentalValue pref(kValue); | 218 base::Value pref(kValue); |
| 219 base::DictionaryValue prefs; | 219 base::DictionaryValue prefs; |
| 220 prefs.Set(kInvalidKey, pref.CreateDeepCopy()); | 220 prefs.Set(kInvalidKey, pref.CreateDeepCopy()); |
| 221 | 221 |
| 222 OnPreferencesChanged(prefs); | 222 OnPreferencesChanged(prefs); |
| 223 EXPECT_TRUE(observer()->changed_keys.empty()); | 223 EXPECT_TRUE(observer()->changed_keys.empty()); |
| 224 } | 224 } |
| 225 | 225 |
| 226 // Tests that when tracking preferences which nest other DictionaryValues, that | 226 // Tests that when tracking preferences which nest other DictionaryValues, that |
| 227 // modifications to the nested values properly notify the observer. | 227 // modifications to the nested values properly notify the observer. |
| 228 TEST_F(PrefObserverStoreTest, WriteToNestedPrefs) { | 228 TEST_F(PrefObserverStoreTest, WriteToNestedPrefs) { |
| 229 std::set<std::string> keys; | 229 std::set<std::string> keys; |
| 230 const std::string key1("hey"); | 230 const std::string key1("hey"); |
| 231 const std::string key2("listen"); | 231 const std::string key2("listen"); |
| 232 keys.insert(key1); | 232 keys.insert(key1); |
| 233 keys.insert(key2); | 233 keys.insert(key2); |
| 234 store()->Init(keys); | 234 store()->Init(keys); |
| 235 | 235 |
| 236 EXPECT_FALSE(Initialized()); | 236 EXPECT_FALSE(Initialized()); |
| 237 EXPECT_FALSE(observer()->initialized); | 237 EXPECT_FALSE(observer()->initialized); |
| 238 | 238 |
| 239 const std::string sub_key1("look"); | 239 const std::string sub_key1("look"); |
| 240 const int kValue1 = 42; | 240 const int kValue1 = 42; |
| 241 base::FundamentalValue pref1(kValue1); | 241 base::Value pref1(kValue1); |
| 242 base::DictionaryValue sub_dictionary1; | 242 base::DictionaryValue sub_dictionary1; |
| 243 sub_dictionary1.Set(sub_key1, pref1.CreateDeepCopy()); | 243 sub_dictionary1.Set(sub_key1, pref1.CreateDeepCopy()); |
| 244 | 244 |
| 245 const std::string sub_key2("Watch out!\n"); | 245 const std::string sub_key2("Watch out!\n"); |
| 246 const int kValue2 = 1337; | 246 const int kValue2 = 1337; |
| 247 base::FundamentalValue pref2(kValue2); | 247 base::Value pref2(kValue2); |
| 248 base::DictionaryValue sub_dictionary2; | 248 base::DictionaryValue sub_dictionary2; |
| 249 sub_dictionary2.Set(sub_key2, pref2.CreateDeepCopy()); | 249 sub_dictionary2.Set(sub_key2, pref2.CreateDeepCopy()); |
| 250 | 250 |
| 251 base::DictionaryValue prefs; | 251 base::DictionaryValue prefs; |
| 252 prefs.Set(key1, sub_dictionary1.CreateDeepCopy()); | 252 prefs.Set(key1, sub_dictionary1.CreateDeepCopy()); |
| 253 prefs.Set(key2, sub_dictionary2.CreateDeepCopy()); | 253 prefs.Set(key2, sub_dictionary2.CreateDeepCopy()); |
| 254 | 254 |
| 255 // Initialize with the nested dictionaries | 255 // Initialize with the nested dictionaries |
| 256 OnPreferencesChanged(prefs); | 256 OnPreferencesChanged(prefs); |
| 257 EXPECT_TRUE(Initialized()); | 257 EXPECT_TRUE(Initialized()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 269 store()->GetMutableValue(key1, &result); | 269 store()->GetMutableValue(key1, &result); |
| 270 EXPECT_EQ(base::Value::TYPE_DICTIONARY, result->GetType()); | 270 EXPECT_EQ(base::Value::TYPE_DICTIONARY, result->GetType()); |
| 271 EXPECT_TRUE(result->Equals(&sub_dictionary1)); | 271 EXPECT_TRUE(result->Equals(&sub_dictionary1)); |
| 272 | 272 |
| 273 base::DictionaryValue* dictionary_result = nullptr; | 273 base::DictionaryValue* dictionary_result = nullptr; |
| 274 result->GetAsDictionary(&dictionary_result); | 274 result->GetAsDictionary(&dictionary_result); |
| 275 EXPECT_NE(nullptr, dictionary_result); | 275 EXPECT_NE(nullptr, dictionary_result); |
| 276 | 276 |
| 277 const std::string sub_key3("????"); | 277 const std::string sub_key3("????"); |
| 278 const int kValue3 = 9001; | 278 const int kValue3 = 9001; |
| 279 base::FundamentalValue pref3(kValue3); | 279 base::Value pref3(kValue3); |
| 280 dictionary_result->Set(sub_key3, pref3.CreateDeepCopy()); | 280 dictionary_result->Set(sub_key3, pref3.CreateDeepCopy()); |
| 281 | 281 |
| 282 observer()->changed_keys.clear(); | 282 observer()->changed_keys.clear(); |
| 283 store()->ReportValueChanged(key1, 0); | 283 store()->ReportValueChanged(key1, 0); |
| 284 EXPECT_EQ(1u, observer()->changed_keys.size()); | 284 EXPECT_EQ(1u, observer()->changed_keys.size()); |
| 285 } | 285 } |
| 286 | 286 |
| 287 // Tests that when tracking preferences that nest other DictionaryValues, that | 287 // Tests that when tracking preferences that nest other DictionaryValues, that |
| 288 // changes to the tracked keys properly notify the manager and observer. | 288 // changes to the tracked keys properly notify the manager and observer. |
| 289 TEST_F(PrefObserverStoreTest, UpdateOuterNestedPrefs) { | 289 TEST_F(PrefObserverStoreTest, UpdateOuterNestedPrefs) { |
| 290 std::set<std::string> keys; | 290 std::set<std::string> keys; |
| 291 const std::string key1("hey"); | 291 const std::string key1("hey"); |
| 292 const std::string key2("listen"); | 292 const std::string key2("listen"); |
| 293 keys.insert(key1); | 293 keys.insert(key1); |
| 294 keys.insert(key2); | 294 keys.insert(key2); |
| 295 store()->Init(keys); | 295 store()->Init(keys); |
| 296 | 296 |
| 297 EXPECT_FALSE(Initialized()); | 297 EXPECT_FALSE(Initialized()); |
| 298 EXPECT_FALSE(observer()->initialized); | 298 EXPECT_FALSE(observer()->initialized); |
| 299 | 299 |
| 300 const std::string sub_key1("look"); | 300 const std::string sub_key1("look"); |
| 301 const int kValue1 = 42; | 301 const int kValue1 = 42; |
| 302 base::FundamentalValue pref1(kValue1); | 302 base::Value pref1(kValue1); |
| 303 base::DictionaryValue sub_dictionary1; | 303 base::DictionaryValue sub_dictionary1; |
| 304 sub_dictionary1.Set(sub_key1, pref1.CreateDeepCopy()); | 304 sub_dictionary1.Set(sub_key1, pref1.CreateDeepCopy()); |
| 305 | 305 |
| 306 const std::string sub_key2("Watch out!\n"); | 306 const std::string sub_key2("Watch out!\n"); |
| 307 const int kValue2 = 1337; | 307 const int kValue2 = 1337; |
| 308 base::FundamentalValue pref2(kValue2); | 308 base::Value pref2(kValue2); |
| 309 base::DictionaryValue sub_dictionary2; | 309 base::DictionaryValue sub_dictionary2; |
| 310 sub_dictionary2.Set(sub_key2, pref2.CreateDeepCopy()); | 310 sub_dictionary2.Set(sub_key2, pref2.CreateDeepCopy()); |
| 311 | 311 |
| 312 base::DictionaryValue prefs; | 312 base::DictionaryValue prefs; |
| 313 prefs.Set(key1, sub_dictionary1.CreateDeepCopy()); | 313 prefs.Set(key1, sub_dictionary1.CreateDeepCopy()); |
| 314 prefs.Set(key2, sub_dictionary2.CreateDeepCopy()); | 314 prefs.Set(key2, sub_dictionary2.CreateDeepCopy()); |
| 315 | 315 |
| 316 // Initialize with the nested dictionaries | 316 // Initialize with the nested dictionaries |
| 317 OnPreferencesChanged(prefs); | 317 OnPreferencesChanged(prefs); |
| 318 EXPECT_TRUE(Initialized()); | 318 EXPECT_TRUE(Initialized()); |
| 319 EXPECT_TRUE(observer()->initialized); | 319 EXPECT_TRUE(observer()->initialized); |
| 320 EXPECT_EQ(2u, observer()->changed_keys.size()); | 320 EXPECT_EQ(2u, observer()->changed_keys.size()); |
| 321 EXPECT_NE(observer()->changed_keys.end(), | 321 EXPECT_NE(observer()->changed_keys.end(), |
| 322 std::find(observer()->changed_keys.begin(), | 322 std::find(observer()->changed_keys.begin(), |
| 323 observer()->changed_keys.end(), key1)); | 323 observer()->changed_keys.end(), key1)); |
| 324 EXPECT_NE(observer()->changed_keys.end(), | 324 EXPECT_NE(observer()->changed_keys.end(), |
| 325 std::find(observer()->changed_keys.begin(), | 325 std::find(observer()->changed_keys.begin(), |
| 326 observer()->changed_keys.end(), key2)); | 326 observer()->changed_keys.end(), key2)); |
| 327 | 327 |
| 328 observer()->changed_keys.clear(); | 328 observer()->changed_keys.clear(); |
| 329 const int kValue3 = 9001; | 329 const int kValue3 = 9001; |
| 330 base::FundamentalValue pref3(kValue3); | 330 base::Value pref3(kValue3); |
| 331 store()->SetValue(key1, pref3.CreateDeepCopy(), 0); | 331 store()->SetValue(key1, pref3.CreateDeepCopy(), 0); |
| 332 base::RunLoop().RunUntilIdle(); | 332 base::RunLoop().RunUntilIdle(); |
| 333 EXPECT_EQ(1u, observer()->changed_keys.size()); | 333 EXPECT_EQ(1u, observer()->changed_keys.size()); |
| 334 EXPECT_TRUE(manager()->set_preferences_called()); | 334 EXPECT_TRUE(manager()->set_preferences_called()); |
| 335 } | 335 } |
| OLD | NEW |