| 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 "base/prefs/json_pref_store.h" | 5 #include "base/prefs/json_pref_store.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 7 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 12 #include "base/prefs/pref_filter.h" | 13 #include "base/prefs/pref_filter.h" |
| 13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/threading/sequenced_worker_pool.h" | 18 #include "base/threading/sequenced_worker_pool.h" |
| 18 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
| 19 #include "base/values.h" | 20 #include "base/values.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 23 |
| 23 namespace base { | 24 namespace base { |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 const char kHomePage[] = "homepage"; | 27 const char kHomePage[] = "homepage"; |
| 27 | 28 |
| 29 // A PrefFilter that will intercept all calls to FilterOnLoad() and hold on |
| 30 // to the |prefs| until explicitly asked to release them. |
| 31 class InterceptingPrefFilter : public PrefFilter { |
| 32 public: |
| 33 InterceptingPrefFilter(); |
| 34 virtual ~InterceptingPrefFilter(); |
| 35 |
| 36 // PrefFilter implementation: |
| 37 virtual void FilterOnLoad( |
| 38 const PostFilterOnLoadCallback& post_filter_on_load_callback, |
| 39 scoped_ptr<base::DictionaryValue> pref_store_contents) OVERRIDE; |
| 40 virtual void FilterUpdate(const std::string& path) OVERRIDE {} |
| 41 virtual void FilterSerializeData( |
| 42 const base::DictionaryValue* pref_store_contents) OVERRIDE {} |
| 43 |
| 44 bool has_intercepted_prefs() const { return intercepted_prefs_ != NULL; } |
| 45 |
| 46 // Finalize an intercepted read, handing |intercept_prefs_| back to its |
| 47 // JsonPrefStore. |
| 48 void ReleasePrefs(); |
| 49 |
| 50 private: |
| 51 PostFilterOnLoadCallback post_filter_on_load_callback_; |
| 52 scoped_ptr<base::DictionaryValue> intercepted_prefs_; |
| 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(InterceptingPrefFilter); |
| 55 }; |
| 56 |
| 57 InterceptingPrefFilter::InterceptingPrefFilter() {} |
| 58 InterceptingPrefFilter::~InterceptingPrefFilter() {} |
| 59 |
| 60 void InterceptingPrefFilter::FilterOnLoad( |
| 61 const PostFilterOnLoadCallback& post_filter_on_load_callback, |
| 62 scoped_ptr<base::DictionaryValue> pref_store_contents) { |
| 63 post_filter_on_load_callback_ = post_filter_on_load_callback; |
| 64 intercepted_prefs_ = pref_store_contents.Pass(); |
| 65 } |
| 66 |
| 67 void InterceptingPrefFilter::ReleasePrefs() { |
| 68 EXPECT_FALSE(post_filter_on_load_callback_.is_null()); |
| 69 post_filter_on_load_callback_.Run(intercepted_prefs_.Pass(), false); |
| 70 post_filter_on_load_callback_.Reset(); |
| 71 } |
| 72 |
| 28 class MockPrefStoreObserver : public PrefStore::Observer { | 73 class MockPrefStoreObserver : public PrefStore::Observer { |
| 29 public: | 74 public: |
| 30 MOCK_METHOD1(OnPrefValueChanged, void (const std::string&)); | 75 MOCK_METHOD1(OnPrefValueChanged, void (const std::string&)); |
| 31 MOCK_METHOD1(OnInitializationCompleted, void (bool)); | 76 MOCK_METHOD1(OnInitializationCompleted, void (bool)); |
| 32 }; | 77 }; |
| 33 | 78 |
| 34 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { | 79 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { |
| 35 public: | 80 public: |
| 36 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError)); | 81 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError)); |
| 37 }; | 82 }; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 // Serialize and compare to expected output. | 195 // Serialize and compare to expected output. |
| 151 ASSERT_TRUE(PathExists(golden_output_file)); | 196 ASSERT_TRUE(PathExists(golden_output_file)); |
| 152 pref_store->CommitPendingWrite(); | 197 pref_store->CommitPendingWrite(); |
| 153 RunLoop().RunUntilIdle(); | 198 RunLoop().RunUntilIdle(); |
| 154 EXPECT_TRUE(TextContentsEqual(golden_output_file, output_file)); | 199 EXPECT_TRUE(TextContentsEqual(golden_output_file, output_file)); |
| 155 ASSERT_TRUE(base::DeleteFile(output_file, false)); | 200 ASSERT_TRUE(base::DeleteFile(output_file, false)); |
| 156 } | 201 } |
| 157 | 202 |
| 158 TEST_F(JsonPrefStoreTest, Basic) { | 203 TEST_F(JsonPrefStoreTest, Basic) { |
| 159 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), | 204 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 160 temp_dir_.path().AppendASCII("write.json"))); | 205 temp_dir_.path().AppendASCII("write.json"))); |
| 161 | 206 |
| 162 // Test that the persistent value can be loaded. | 207 // Test that the persistent value can be loaded. |
| 163 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 208 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 164 ASSERT_TRUE(PathExists(input_file)); | 209 ASSERT_TRUE(PathExists(input_file)); |
| 165 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 210 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 166 input_file, | 211 input_file, |
| 167 message_loop_.message_loop_proxy().get(), | 212 message_loop_.message_loop_proxy().get(), |
| 168 scoped_ptr<PrefFilter>()); | 213 scoped_ptr<PrefFilter>()); |
| 169 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 214 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 170 ASSERT_FALSE(pref_store->ReadOnly()); | 215 EXPECT_FALSE(pref_store->ReadOnly()); |
| 216 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
| 171 | 217 |
| 172 // The JSON file looks like this: | 218 // The JSON file looks like this: |
| 173 // { | 219 // { |
| 174 // "homepage": "http://www.cnn.com", | 220 // "homepage": "http://www.cnn.com", |
| 175 // "some_directory": "/usr/local/", | 221 // "some_directory": "/usr/local/", |
| 176 // "tabs": { | 222 // "tabs": { |
| 177 // "new_windows_in_tabs": true, | 223 // "new_windows_in_tabs": true, |
| 178 // "max_tabs": 20 | 224 // "max_tabs": 20 |
| 179 // } | 225 // } |
| 180 // } | 226 // } |
| 181 | 227 |
| 182 RunBasicJsonPrefStoreTest( | 228 RunBasicJsonPrefStoreTest( |
| 183 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 229 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
| 184 } | 230 } |
| 185 | 231 |
| 186 TEST_F(JsonPrefStoreTest, BasicAsync) { | 232 TEST_F(JsonPrefStoreTest, BasicAsync) { |
| 187 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), | 233 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 188 temp_dir_.path().AppendASCII("write.json"))); | 234 temp_dir_.path().AppendASCII("write.json"))); |
| 189 | 235 |
| 190 // Test that the persistent value can be loaded. | 236 // Test that the persistent value can be loaded. |
| 191 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 237 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 192 ASSERT_TRUE(PathExists(input_file)); | 238 ASSERT_TRUE(PathExists(input_file)); |
| 193 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 239 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 194 input_file, | 240 input_file, |
| 195 message_loop_.message_loop_proxy().get(), | 241 message_loop_.message_loop_proxy().get(), |
| 196 scoped_ptr<PrefFilter>()); | 242 scoped_ptr<PrefFilter>()); |
| 197 | 243 |
| 198 { | 244 { |
| 199 MockPrefStoreObserver mock_observer; | 245 MockPrefStoreObserver mock_observer; |
| 200 pref_store->AddObserver(&mock_observer); | 246 pref_store->AddObserver(&mock_observer); |
| 201 | 247 |
| 202 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | 248 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
| 203 pref_store->ReadPrefsAsync(mock_error_delegate); | 249 pref_store->ReadPrefsAsync(mock_error_delegate); |
| 204 | 250 |
| 205 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 251 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 206 EXPECT_CALL(*mock_error_delegate, | 252 EXPECT_CALL(*mock_error_delegate, |
| 207 OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); | 253 OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); |
| 208 RunLoop().RunUntilIdle(); | 254 RunLoop().RunUntilIdle(); |
| 209 pref_store->RemoveObserver(&mock_observer); | 255 pref_store->RemoveObserver(&mock_observer); |
| 210 | 256 |
| 211 ASSERT_FALSE(pref_store->ReadOnly()); | 257 EXPECT_FALSE(pref_store->ReadOnly()); |
| 258 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
| 212 } | 259 } |
| 213 | 260 |
| 214 // The JSON file looks like this: | 261 // The JSON file looks like this: |
| 215 // { | 262 // { |
| 216 // "homepage": "http://www.cnn.com", | 263 // "homepage": "http://www.cnn.com", |
| 217 // "some_directory": "/usr/local/", | 264 // "some_directory": "/usr/local/", |
| 218 // "tabs": { | 265 // "tabs": { |
| 219 // "new_windows_in_tabs": true, | 266 // "new_windows_in_tabs": true, |
| 220 // "max_tabs": 20 | 267 // "max_tabs": 20 |
| 221 // } | 268 // } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 | 341 |
| 295 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 342 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 296 EXPECT_CALL(*mock_error_delegate, | 343 EXPECT_CALL(*mock_error_delegate, |
| 297 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); | 344 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); |
| 298 RunLoop().RunUntilIdle(); | 345 RunLoop().RunUntilIdle(); |
| 299 pref_store->RemoveObserver(&mock_observer); | 346 pref_store->RemoveObserver(&mock_observer); |
| 300 | 347 |
| 301 EXPECT_FALSE(pref_store->ReadOnly()); | 348 EXPECT_FALSE(pref_store->ReadOnly()); |
| 302 } | 349 } |
| 303 | 350 |
| 351 TEST_F(JsonPrefStoreTest, ReadWithInterceptor) { |
| 352 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 353 temp_dir_.path().AppendASCII("write.json"))); |
| 354 |
| 355 // Test that the persistent value can be loaded. |
| 356 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 357 ASSERT_TRUE(PathExists(input_file)); |
| 358 |
| 359 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
| 360 new InterceptingPrefFilter()); |
| 361 InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
| 362 intercepting_pref_filter.get(); |
| 363 scoped_refptr<JsonPrefStore> pref_store = |
| 364 new JsonPrefStore(input_file, |
| 365 message_loop_.message_loop_proxy().get(), |
| 366 intercepting_pref_filter.PassAs<PrefFilter>()); |
| 367 |
| 368 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE, |
| 369 pref_store->ReadPrefs()); |
| 370 EXPECT_FALSE(pref_store->ReadOnly()); |
| 371 |
| 372 // The store shouldn't be considered initialized until the interceptor |
| 373 // returns. |
| 374 EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 375 EXPECT_FALSE(pref_store->IsInitializationComplete()); |
| 376 EXPECT_FALSE(pref_store->GetValue(kHomePage, NULL)); |
| 377 |
| 378 raw_intercepting_pref_filter_->ReleasePrefs(); |
| 379 |
| 380 EXPECT_FALSE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 381 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
| 382 EXPECT_TRUE(pref_store->GetValue(kHomePage, NULL)); |
| 383 |
| 384 // The JSON file looks like this: |
| 385 // { |
| 386 // "homepage": "http://www.cnn.com", |
| 387 // "some_directory": "/usr/local/", |
| 388 // "tabs": { |
| 389 // "new_windows_in_tabs": true, |
| 390 // "max_tabs": 20 |
| 391 // } |
| 392 // } |
| 393 |
| 394 RunBasicJsonPrefStoreTest( |
| 395 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
| 396 } |
| 397 |
| 398 TEST_F(JsonPrefStoreTest, ReadAsyncWithInterceptor) { |
| 399 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 400 temp_dir_.path().AppendASCII("write.json"))); |
| 401 |
| 402 // Test that the persistent value can be loaded. |
| 403 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 404 ASSERT_TRUE(PathExists(input_file)); |
| 405 |
| 406 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
| 407 new InterceptingPrefFilter()); |
| 408 InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
| 409 intercepting_pref_filter.get(); |
| 410 scoped_refptr<JsonPrefStore> pref_store = |
| 411 new JsonPrefStore(input_file, |
| 412 message_loop_.message_loop_proxy().get(), |
| 413 intercepting_pref_filter.PassAs<PrefFilter>()); |
| 414 |
| 415 MockPrefStoreObserver mock_observer; |
| 416 pref_store->AddObserver(&mock_observer); |
| 417 |
| 418 // Ownership of the |mock_error_delegate| is handed to the |pref_store| below. |
| 419 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
| 420 |
| 421 { |
| 422 pref_store->ReadPrefsAsync(mock_error_delegate); |
| 423 |
| 424 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(0); |
| 425 // EXPECT_CALL(*mock_error_delegate, |
| 426 // OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); |
| 427 RunLoop().RunUntilIdle(); |
| 428 |
| 429 EXPECT_FALSE(pref_store->ReadOnly()); |
| 430 EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 431 EXPECT_FALSE(pref_store->IsInitializationComplete()); |
| 432 EXPECT_FALSE(pref_store->GetValue(kHomePage, NULL)); |
| 433 } |
| 434 |
| 435 { |
| 436 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 437 // EXPECT_CALL(*mock_error_delegate, |
| 438 // OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); |
| 439 |
| 440 raw_intercepting_pref_filter_->ReleasePrefs(); |
| 441 |
| 442 EXPECT_FALSE(pref_store->ReadOnly()); |
| 443 EXPECT_FALSE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 444 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
| 445 EXPECT_TRUE(pref_store->GetValue(kHomePage, NULL)); |
| 446 } |
| 447 |
| 448 pref_store->RemoveObserver(&mock_observer); |
| 449 |
| 450 // The JSON file looks like this: |
| 451 // { |
| 452 // "homepage": "http://www.cnn.com", |
| 453 // "some_directory": "/usr/local/", |
| 454 // "tabs": { |
| 455 // "new_windows_in_tabs": true, |
| 456 // "max_tabs": 20 |
| 457 // } |
| 458 // } |
| 459 |
| 460 RunBasicJsonPrefStoreTest( |
| 461 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
| 462 } |
| 463 |
| 304 } // namespace base | 464 } // namespace base |
| OLD | NEW |