| 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_client_store.h" | 5 #include "services/preferences/public/cpp/pref_client_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 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 TEST_F(PrefClientStoreTest, Initialization) { | 129 TEST_F(PrefClientStoreTest, Initialization) { |
| 130 std::set<std::string> keys; | 130 std::set<std::string> keys; |
| 131 const std::string key("hey"); | 131 const std::string key("hey"); |
| 132 keys.insert(key); | 132 keys.insert(key); |
| 133 store()->Subscribe(keys); | 133 store()->Subscribe(keys); |
| 134 | 134 |
| 135 EXPECT_FALSE(Initialized()); | 135 EXPECT_FALSE(Initialized()); |
| 136 EXPECT_FALSE(observer()->initialized); | 136 EXPECT_FALSE(observer()->initialized); |
| 137 | 137 |
| 138 const int kValue = 42; | 138 const int kValue = 42; |
| 139 base::FundamentalValue pref(kValue); | 139 base::Value pref(kValue); |
| 140 base::DictionaryValue prefs; | 140 base::DictionaryValue prefs; |
| 141 prefs.Set(key, pref.CreateDeepCopy()); | 141 prefs.Set(key, pref.CreateDeepCopy()); |
| 142 | 142 |
| 143 // PreferenceManager notifies of PreferencesChanged, completing | 143 // PreferenceManager notifies of PreferencesChanged, completing |
| 144 // initialization. | 144 // initialization. |
| 145 OnPreferencesChanged(prefs); | 145 OnPreferencesChanged(prefs); |
| 146 EXPECT_TRUE(Initialized()); | 146 EXPECT_TRUE(Initialized()); |
| 147 EXPECT_TRUE(observer()->initialized); | 147 EXPECT_TRUE(observer()->initialized); |
| 148 EXPECT_TRUE(observer()->initialization_success); | 148 EXPECT_TRUE(observer()->initialization_success); |
| 149 observer()->VerifyAndResetChangedKey(key); | 149 observer()->VerifyAndResetChangedKey(key); |
| 150 | 150 |
| 151 const base::Value* value = nullptr; | 151 const base::Value* value = nullptr; |
| 152 int actual_value; | 152 int actual_value; |
| 153 EXPECT_TRUE(store()->GetValue(key, &value)); | 153 EXPECT_TRUE(store()->GetValue(key, &value)); |
| 154 EXPECT_NE(nullptr, value); | 154 EXPECT_NE(nullptr, value); |
| 155 EXPECT_TRUE(value->GetAsInteger(&actual_value)); | 155 EXPECT_TRUE(value->GetAsInteger(&actual_value)); |
| 156 EXPECT_EQ(kValue, actual_value); | 156 EXPECT_EQ(kValue, actual_value); |
| 157 EXPECT_FALSE(service()->set_preferences_called()); | 157 EXPECT_FALSE(service()->set_preferences_called()); |
| 158 } | 158 } |
| 159 | 159 |
| 160 // Tests that values set silently are also set on the preference service, but | 160 // Tests that values set silently are also set on the preference service, but |
| 161 // that no observers are notified. | 161 // that no observers are notified. |
| 162 TEST_F(PrefClientStoreTest, SetValueSilently) { | 162 TEST_F(PrefClientStoreTest, SetValueSilently) { |
| 163 std::set<std::string> keys; | 163 std::set<std::string> keys; |
| 164 const std::string key("hey"); | 164 const std::string key("hey"); |
| 165 keys.insert(key); | 165 keys.insert(key); |
| 166 store()->Subscribe(keys); | 166 store()->Subscribe(keys); |
| 167 | 167 |
| 168 const int kValue = 42; | 168 const int kValue = 42; |
| 169 base::FundamentalValue pref(kValue); | 169 base::Value pref(kValue); |
| 170 store()->SetValueSilently(key, pref.CreateDeepCopy(), 0); | 170 store()->SetValueSilently(key, pref.CreateDeepCopy(), 0); |
| 171 base::RunLoop().RunUntilIdle(); | 171 base::RunLoop().RunUntilIdle(); |
| 172 EXPECT_TRUE(service()->set_preferences_called()); | 172 EXPECT_TRUE(service()->set_preferences_called()); |
| 173 EXPECT_TRUE(observer()->changed_keys.empty()); | 173 EXPECT_TRUE(observer()->changed_keys.empty()); |
| 174 } | 174 } |
| 175 | 175 |
| 176 // Test that reporting values changed notifies observers, and the preference | 176 // Test that reporting values changed notifies observers, and the preference |
| 177 // service. | 177 // service. |
| 178 TEST_F(PrefClientStoreTest, ReportValueChanged) { | 178 TEST_F(PrefClientStoreTest, ReportValueChanged) { |
| 179 std::set<std::string> keys; | 179 std::set<std::string> keys; |
| 180 const std::string key("hey"); | 180 const std::string key("hey"); |
| 181 keys.insert(key); | 181 keys.insert(key); |
| 182 store()->Subscribe(keys); | 182 store()->Subscribe(keys); |
| 183 | 183 |
| 184 const int kValue = 42; | 184 const int kValue = 42; |
| 185 base::FundamentalValue pref(kValue); | 185 base::Value pref(kValue); |
| 186 base::DictionaryValue prefs; | 186 base::DictionaryValue prefs; |
| 187 prefs.Set(key, pref.CreateDeepCopy()); | 187 prefs.Set(key, pref.CreateDeepCopy()); |
| 188 OnPreferencesChanged(prefs); | 188 OnPreferencesChanged(prefs); |
| 189 observer()->changed_keys.clear(); | 189 observer()->changed_keys.clear(); |
| 190 | 190 |
| 191 store()->ReportValueChanged(key, 0); | 191 store()->ReportValueChanged(key, 0); |
| 192 base::RunLoop().RunUntilIdle(); | 192 base::RunLoop().RunUntilIdle(); |
| 193 EXPECT_TRUE(service()->set_preferences_called()); | 193 EXPECT_TRUE(service()->set_preferences_called()); |
| 194 observer()->VerifyAndResetChangedKey(key); | 194 observer()->VerifyAndResetChangedKey(key); |
| 195 } | 195 } |
| 196 | 196 |
| 197 // Test that when initialized with multiple keys, that observers receive a | 197 // Test that when initialized with multiple keys, that observers receive a |
| 198 // notification for each key. | 198 // notification for each key. |
| 199 TEST_F(PrefClientStoreTest, MultipleKeyInitialization) { | 199 TEST_F(PrefClientStoreTest, MultipleKeyInitialization) { |
| 200 std::set<std::string> keys; | 200 std::set<std::string> keys; |
| 201 const std::string key1("hey"); | 201 const std::string key1("hey"); |
| 202 const std::string key2("listen"); | 202 const std::string key2("listen"); |
| 203 keys.insert(key1); | 203 keys.insert(key1); |
| 204 keys.insert(key2); | 204 keys.insert(key2); |
| 205 store()->Subscribe(keys); | 205 store()->Subscribe(keys); |
| 206 | 206 |
| 207 EXPECT_FALSE(Initialized()); | 207 EXPECT_FALSE(Initialized()); |
| 208 EXPECT_FALSE(observer()->initialized); | 208 EXPECT_FALSE(observer()->initialized); |
| 209 | 209 |
| 210 const int kValue = 42; | 210 const int kValue = 42; |
| 211 base::FundamentalValue pref1(kValue); | 211 base::Value pref1(kValue); |
| 212 const std::string kStringValue("look"); | 212 const std::string kStringValue("look"); |
| 213 base::StringValue pref2(kStringValue); | 213 base::StringValue pref2(kStringValue); |
| 214 | 214 |
| 215 base::DictionaryValue prefs; | 215 base::DictionaryValue prefs; |
| 216 prefs.Set(key1, pref1.CreateDeepCopy()); | 216 prefs.Set(key1, pref1.CreateDeepCopy()); |
| 217 prefs.Set(key2, pref2.CreateDeepCopy()); | 217 prefs.Set(key2, pref2.CreateDeepCopy()); |
| 218 | 218 |
| 219 // The observer should be notified of all keys set. | 219 // The observer should be notified of all keys set. |
| 220 OnPreferencesChanged(prefs); | 220 OnPreferencesChanged(prefs); |
| 221 EXPECT_TRUE(Initialized()); | 221 EXPECT_TRUE(Initialized()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 232 // Tests that if OnPreferencesChanged is received with invalid keys, that they | 232 // Tests that if OnPreferencesChanged is received with invalid keys, that they |
| 233 // are ignored. | 233 // are ignored. |
| 234 TEST_F(PrefClientStoreTest, InvalidInitialization) { | 234 TEST_F(PrefClientStoreTest, InvalidInitialization) { |
| 235 std::set<std::string> keys; | 235 std::set<std::string> keys; |
| 236 const std::string key("hey"); | 236 const std::string key("hey"); |
| 237 keys.insert(key); | 237 keys.insert(key); |
| 238 store()->Subscribe(keys); | 238 store()->Subscribe(keys); |
| 239 | 239 |
| 240 const std::string kInvalidKey("look"); | 240 const std::string kInvalidKey("look"); |
| 241 const int kValue = 42; | 241 const int kValue = 42; |
| 242 base::FundamentalValue pref(kValue); | 242 base::Value pref(kValue); |
| 243 base::DictionaryValue prefs; | 243 base::DictionaryValue prefs; |
| 244 prefs.Set(kInvalidKey, pref.CreateDeepCopy()); | 244 prefs.Set(kInvalidKey, pref.CreateDeepCopy()); |
| 245 | 245 |
| 246 OnPreferencesChanged(prefs); | 246 OnPreferencesChanged(prefs); |
| 247 EXPECT_TRUE(observer()->changed_keys.empty()); | 247 EXPECT_TRUE(observer()->changed_keys.empty()); |
| 248 } | 248 } |
| 249 | 249 |
| 250 // Tests that when tracking preferences which nest other DictionaryValues, that | 250 // Tests that when tracking preferences which nest other DictionaryValues, that |
| 251 // modifications to the nested values properly notify the observer. | 251 // modifications to the nested values properly notify the observer. |
| 252 TEST_F(PrefClientStoreTest, WriteToNestedPrefs) { | 252 TEST_F(PrefClientStoreTest, WriteToNestedPrefs) { |
| 253 std::set<std::string> keys; | 253 std::set<std::string> keys; |
| 254 const std::string key1("hey"); | 254 const std::string key1("hey"); |
| 255 const std::string key2("listen"); | 255 const std::string key2("listen"); |
| 256 keys.insert(key1); | 256 keys.insert(key1); |
| 257 keys.insert(key2); | 257 keys.insert(key2); |
| 258 store()->Subscribe(keys); | 258 store()->Subscribe(keys); |
| 259 | 259 |
| 260 EXPECT_FALSE(Initialized()); | 260 EXPECT_FALSE(Initialized()); |
| 261 EXPECT_FALSE(observer()->initialized); | 261 EXPECT_FALSE(observer()->initialized); |
| 262 | 262 |
| 263 const std::string sub_key1("look"); | 263 const std::string sub_key1("look"); |
| 264 const int kValue1 = 42; | 264 const int kValue1 = 42; |
| 265 base::FundamentalValue pref1(kValue1); | 265 base::Value pref1(kValue1); |
| 266 base::DictionaryValue sub_dictionary1; | 266 base::DictionaryValue sub_dictionary1; |
| 267 sub_dictionary1.Set(sub_key1, pref1.CreateDeepCopy()); | 267 sub_dictionary1.Set(sub_key1, pref1.CreateDeepCopy()); |
| 268 | 268 |
| 269 const std::string sub_key2("Watch out!\n"); | 269 const std::string sub_key2("Watch out!\n"); |
| 270 const int kValue2 = 1337; | 270 const int kValue2 = 1337; |
| 271 base::FundamentalValue pref2(kValue2); | 271 base::Value pref2(kValue2); |
| 272 base::DictionaryValue sub_dictionary2; | 272 base::DictionaryValue sub_dictionary2; |
| 273 sub_dictionary2.Set(sub_key2, pref2.CreateDeepCopy()); | 273 sub_dictionary2.Set(sub_key2, pref2.CreateDeepCopy()); |
| 274 | 274 |
| 275 base::DictionaryValue prefs; | 275 base::DictionaryValue prefs; |
| 276 prefs.Set(key1, sub_dictionary1.CreateDeepCopy()); | 276 prefs.Set(key1, sub_dictionary1.CreateDeepCopy()); |
| 277 prefs.Set(key2, sub_dictionary2.CreateDeepCopy()); | 277 prefs.Set(key2, sub_dictionary2.CreateDeepCopy()); |
| 278 | 278 |
| 279 // Initialize with the nested dictionaries | 279 // Initialize with the nested dictionaries |
| 280 OnPreferencesChanged(prefs); | 280 OnPreferencesChanged(prefs); |
| 281 EXPECT_TRUE(Initialized()); | 281 EXPECT_TRUE(Initialized()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 293 store()->GetMutableValue(key1, &result); | 293 store()->GetMutableValue(key1, &result); |
| 294 EXPECT_EQ(base::Value::Type::DICTIONARY, result->GetType()); | 294 EXPECT_EQ(base::Value::Type::DICTIONARY, result->GetType()); |
| 295 EXPECT_TRUE(result->Equals(&sub_dictionary1)); | 295 EXPECT_TRUE(result->Equals(&sub_dictionary1)); |
| 296 | 296 |
| 297 base::DictionaryValue* dictionary_result = nullptr; | 297 base::DictionaryValue* dictionary_result = nullptr; |
| 298 result->GetAsDictionary(&dictionary_result); | 298 result->GetAsDictionary(&dictionary_result); |
| 299 EXPECT_NE(nullptr, dictionary_result); | 299 EXPECT_NE(nullptr, dictionary_result); |
| 300 | 300 |
| 301 const std::string sub_key3("????"); | 301 const std::string sub_key3("????"); |
| 302 const int kValue3 = 9001; | 302 const int kValue3 = 9001; |
| 303 base::FundamentalValue pref3(kValue3); | 303 base::Value pref3(kValue3); |
| 304 dictionary_result->Set(sub_key3, pref3.CreateDeepCopy()); | 304 dictionary_result->Set(sub_key3, pref3.CreateDeepCopy()); |
| 305 | 305 |
| 306 observer()->changed_keys.clear(); | 306 observer()->changed_keys.clear(); |
| 307 store()->ReportValueChanged(key1, 0); | 307 store()->ReportValueChanged(key1, 0); |
| 308 EXPECT_EQ(1u, observer()->changed_keys.size()); | 308 EXPECT_EQ(1u, observer()->changed_keys.size()); |
| 309 } | 309 } |
| 310 | 310 |
| 311 // Tests that when tracking preferences that nest other DictionaryValues, that | 311 // Tests that when tracking preferences that nest other DictionaryValues, that |
| 312 // changes to the tracked keys properly notify the service and observer. | 312 // changes to the tracked keys properly notify the service and observer. |
| 313 TEST_F(PrefClientStoreTest, UpdateOuterNestedPrefs) { | 313 TEST_F(PrefClientStoreTest, UpdateOuterNestedPrefs) { |
| 314 std::set<std::string> keys; | 314 std::set<std::string> keys; |
| 315 const std::string key1("hey"); | 315 const std::string key1("hey"); |
| 316 const std::string key2("listen"); | 316 const std::string key2("listen"); |
| 317 keys.insert(key1); | 317 keys.insert(key1); |
| 318 keys.insert(key2); | 318 keys.insert(key2); |
| 319 store()->Subscribe(keys); | 319 store()->Subscribe(keys); |
| 320 | 320 |
| 321 EXPECT_FALSE(Initialized()); | 321 EXPECT_FALSE(Initialized()); |
| 322 EXPECT_FALSE(observer()->initialized); | 322 EXPECT_FALSE(observer()->initialized); |
| 323 | 323 |
| 324 const std::string sub_key1("look"); | 324 const std::string sub_key1("look"); |
| 325 const int kValue1 = 42; | 325 const int kValue1 = 42; |
| 326 base::FundamentalValue pref1(kValue1); | 326 base::Value pref1(kValue1); |
| 327 base::DictionaryValue sub_dictionary1; | 327 base::DictionaryValue sub_dictionary1; |
| 328 sub_dictionary1.Set(sub_key1, pref1.CreateDeepCopy()); | 328 sub_dictionary1.Set(sub_key1, pref1.CreateDeepCopy()); |
| 329 | 329 |
| 330 const std::string sub_key2("Watch out!\n"); | 330 const std::string sub_key2("Watch out!\n"); |
| 331 const int kValue2 = 1337; | 331 const int kValue2 = 1337; |
| 332 base::FundamentalValue pref2(kValue2); | 332 base::Value pref2(kValue2); |
| 333 base::DictionaryValue sub_dictionary2; | 333 base::DictionaryValue sub_dictionary2; |
| 334 sub_dictionary2.Set(sub_key2, pref2.CreateDeepCopy()); | 334 sub_dictionary2.Set(sub_key2, pref2.CreateDeepCopy()); |
| 335 | 335 |
| 336 base::DictionaryValue prefs; | 336 base::DictionaryValue prefs; |
| 337 prefs.Set(key1, sub_dictionary1.CreateDeepCopy()); | 337 prefs.Set(key1, sub_dictionary1.CreateDeepCopy()); |
| 338 prefs.Set(key2, sub_dictionary2.CreateDeepCopy()); | 338 prefs.Set(key2, sub_dictionary2.CreateDeepCopy()); |
| 339 | 339 |
| 340 // Initialize with the nested dictionaries | 340 // Initialize with the nested dictionaries |
| 341 OnPreferencesChanged(prefs); | 341 OnPreferencesChanged(prefs); |
| 342 EXPECT_TRUE(Initialized()); | 342 EXPECT_TRUE(Initialized()); |
| 343 EXPECT_TRUE(observer()->initialized); | 343 EXPECT_TRUE(observer()->initialized); |
| 344 EXPECT_EQ(2u, observer()->changed_keys.size()); | 344 EXPECT_EQ(2u, observer()->changed_keys.size()); |
| 345 EXPECT_NE(observer()->changed_keys.end(), | 345 EXPECT_NE(observer()->changed_keys.end(), |
| 346 std::find(observer()->changed_keys.begin(), | 346 std::find(observer()->changed_keys.begin(), |
| 347 observer()->changed_keys.end(), key1)); | 347 observer()->changed_keys.end(), key1)); |
| 348 EXPECT_NE(observer()->changed_keys.end(), | 348 EXPECT_NE(observer()->changed_keys.end(), |
| 349 std::find(observer()->changed_keys.begin(), | 349 std::find(observer()->changed_keys.begin(), |
| 350 observer()->changed_keys.end(), key2)); | 350 observer()->changed_keys.end(), key2)); |
| 351 | 351 |
| 352 observer()->changed_keys.clear(); | 352 observer()->changed_keys.clear(); |
| 353 const int kValue3 = 9001; | 353 const int kValue3 = 9001; |
| 354 base::FundamentalValue pref3(kValue3); | 354 base::Value pref3(kValue3); |
| 355 store()->SetValue(key1, pref3.CreateDeepCopy(), 0); | 355 store()->SetValue(key1, pref3.CreateDeepCopy(), 0); |
| 356 base::RunLoop().RunUntilIdle(); | 356 base::RunLoop().RunUntilIdle(); |
| 357 EXPECT_EQ(1u, observer()->changed_keys.size()); | 357 EXPECT_EQ(1u, observer()->changed_keys.size()); |
| 358 EXPECT_TRUE(service()->set_preferences_called()); | 358 EXPECT_TRUE(service()->set_preferences_called()); |
| 359 } | 359 } |
| 360 | 360 |
| 361 // Tests that a PrefClientStore can subscribe multiple times to different | 361 // Tests that a PrefClientStore can subscribe multiple times to different |
| 362 // keys. | 362 // keys. |
| 363 TEST_F(PrefClientStoreTest, MultipleSubscriptions) { | 363 TEST_F(PrefClientStoreTest, MultipleSubscriptions) { |
| 364 std::set<std::string> keys1; | 364 std::set<std::string> keys1; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 383 TEST_F(PrefClientStoreTest, MultipleObservers) { | 383 TEST_F(PrefClientStoreTest, MultipleObservers) { |
| 384 PrefStoreObserverMock observer2; | 384 PrefStoreObserverMock observer2; |
| 385 store()->AddObserver(&observer2); | 385 store()->AddObserver(&observer2); |
| 386 | 386 |
| 387 std::set<std::string> keys; | 387 std::set<std::string> keys; |
| 388 const std::string key("hey"); | 388 const std::string key("hey"); |
| 389 keys.insert(key); | 389 keys.insert(key); |
| 390 store()->Subscribe(keys); | 390 store()->Subscribe(keys); |
| 391 | 391 |
| 392 const int kValue = 42; | 392 const int kValue = 42; |
| 393 base::FundamentalValue pref(kValue); | 393 base::Value pref(kValue); |
| 394 base::DictionaryValue prefs; | 394 base::DictionaryValue prefs; |
| 395 prefs.Set(key, pref.CreateDeepCopy()); | 395 prefs.Set(key, pref.CreateDeepCopy()); |
| 396 | 396 |
| 397 // PreferenceManager notifies of PreferencesChanged, completing | 397 // PreferenceManager notifies of PreferencesChanged, completing |
| 398 // initialization. | 398 // initialization. |
| 399 OnPreferencesChanged(prefs); | 399 OnPreferencesChanged(prefs); |
| 400 EXPECT_TRUE(observer()->initialized); | 400 EXPECT_TRUE(observer()->initialized); |
| 401 EXPECT_TRUE(observer2.initialized); | 401 EXPECT_TRUE(observer2.initialized); |
| 402 EXPECT_TRUE(observer()->initialization_success); | 402 EXPECT_TRUE(observer()->initialization_success); |
| 403 EXPECT_TRUE(observer2.initialization_success); | 403 EXPECT_TRUE(observer2.initialization_success); |
| 404 observer()->VerifyAndResetChangedKey(key); | 404 observer()->VerifyAndResetChangedKey(key); |
| 405 observer2.VerifyAndResetChangedKey(key); | 405 observer2.VerifyAndResetChangedKey(key); |
| 406 | 406 |
| 407 store()->RemoveObserver(&observer2); | 407 store()->RemoveObserver(&observer2); |
| 408 } | 408 } |
| 409 | 409 |
| 410 } // namespace preferences | 410 } // namespace preferences |
| OLD | NEW |