| 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/bind.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/location.h" |
| 10 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 13 #include "base/metrics/histogram_samples.h" | 14 #include "base/metrics/histogram_samples.h" |
| 14 #include "base/metrics/statistics_recorder.h" | 15 #include "base/metrics/statistics_recorder.h" |
| 15 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 16 #include "base/prefs/pref_filter.h" | 17 #include "base/prefs/pref_filter.h" |
| 17 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 19 #include "base/single_thread_task_runner.h" |
| 18 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
| 20 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
| 21 #include "base/test/simple_test_clock.h" | 23 #include "base/test/simple_test_clock.h" |
| 22 #include "base/threading/sequenced_worker_pool.h" | 24 #include "base/threading/sequenced_worker_pool.h" |
| 23 #include "base/threading/thread.h" | 25 #include "base/threading/thread.h" |
| 24 #include "base/values.h" | 26 #include "base/values.h" |
| 25 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 29 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 101 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 100 | 102 |
| 101 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_)); | 103 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_)); |
| 102 data_dir_ = data_dir_.AppendASCII("prefs"); | 104 data_dir_ = data_dir_.AppendASCII("prefs"); |
| 103 ASSERT_TRUE(PathExists(data_dir_)); | 105 ASSERT_TRUE(PathExists(data_dir_)); |
| 104 } | 106 } |
| 105 | 107 |
| 106 void TearDown() override { | 108 void TearDown() override { |
| 107 // Make sure all pending tasks have been processed (e.g., deleting the | 109 // Make sure all pending tasks have been processed (e.g., deleting the |
| 108 // JsonPrefStore may post write tasks). | 110 // JsonPrefStore may post write tasks). |
| 109 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure()); | 111 message_loop_.task_runner()->PostTask(FROM_HERE, |
| 112 MessageLoop::QuitWhenIdleClosure()); |
| 110 message_loop_.Run(); | 113 message_loop_.Run(); |
| 111 } | 114 } |
| 112 | 115 |
| 113 // The path to temporary directory used to contain the test operations. | 116 // The path to temporary directory used to contain the test operations. |
| 114 base::ScopedTempDir temp_dir_; | 117 base::ScopedTempDir temp_dir_; |
| 115 // The path to the directory where the test data is stored. | 118 // The path to the directory where the test data is stored. |
| 116 base::FilePath data_dir_; | 119 base::FilePath data_dir_; |
| 117 // A message loop that we can use as the file thread message loop. | 120 // A message loop that we can use as the file thread message loop. |
| 118 MessageLoop message_loop_; | 121 MessageLoop message_loop_; |
| 119 | 122 |
| 120 private: | 123 private: |
| 121 // Ensure histograms are reset for each test. | 124 // Ensure histograms are reset for each test. |
| 122 StatisticsRecorder statistics_recorder_; | 125 StatisticsRecorder statistics_recorder_; |
| 123 }; | 126 }; |
| 124 | 127 |
| 125 // Test fallback behavior for a nonexistent file. | 128 // Test fallback behavior for a nonexistent file. |
| 126 TEST_F(JsonPrefStoreTest, NonExistentFile) { | 129 TEST_F(JsonPrefStoreTest, NonExistentFile) { |
| 127 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 130 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 128 ASSERT_FALSE(PathExists(bogus_input_file)); | 131 ASSERT_FALSE(PathExists(bogus_input_file)); |
| 129 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 132 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 130 bogus_input_file, | 133 bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 131 message_loop_.message_loop_proxy().get(), | |
| 132 scoped_ptr<PrefFilter>()); | |
| 133 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 134 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
| 134 pref_store->ReadPrefs()); | 135 pref_store->ReadPrefs()); |
| 135 EXPECT_FALSE(pref_store->ReadOnly()); | 136 EXPECT_FALSE(pref_store->ReadOnly()); |
| 136 } | 137 } |
| 137 | 138 |
| 138 // Test fallback behavior for a nonexistent file and alternate file. | 139 // Test fallback behavior for a nonexistent file and alternate file. |
| 139 TEST_F(JsonPrefStoreTest, NonExistentFileAndAlternateFile) { | 140 TEST_F(JsonPrefStoreTest, NonExistentFileAndAlternateFile) { |
| 140 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 141 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 141 base::FilePath bogus_alternate_input_file = | 142 base::FilePath bogus_alternate_input_file = |
| 142 data_dir_.AppendASCII("read_alternate.txt"); | 143 data_dir_.AppendASCII("read_alternate.txt"); |
| 143 ASSERT_FALSE(PathExists(bogus_input_file)); | 144 ASSERT_FALSE(PathExists(bogus_input_file)); |
| 144 ASSERT_FALSE(PathExists(bogus_alternate_input_file)); | 145 ASSERT_FALSE(PathExists(bogus_alternate_input_file)); |
| 145 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 146 scoped_refptr<JsonPrefStore> pref_store = |
| 146 bogus_input_file, | 147 new JsonPrefStore(bogus_input_file, bogus_alternate_input_file, |
| 147 bogus_alternate_input_file, | 148 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 148 message_loop_.message_loop_proxy().get(), | |
| 149 scoped_ptr<PrefFilter>()); | |
| 150 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 149 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
| 151 pref_store->ReadPrefs()); | 150 pref_store->ReadPrefs()); |
| 152 EXPECT_FALSE(pref_store->ReadOnly()); | 151 EXPECT_FALSE(pref_store->ReadOnly()); |
| 153 } | 152 } |
| 154 | 153 |
| 155 // Test fallback behavior for an invalid file. | 154 // Test fallback behavior for an invalid file. |
| 156 TEST_F(JsonPrefStoreTest, InvalidFile) { | 155 TEST_F(JsonPrefStoreTest, InvalidFile) { |
| 157 base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); | 156 base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); |
| 158 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); | 157 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); |
| 159 ASSERT_TRUE(base::CopyFile(invalid_file_original, invalid_file)); | 158 ASSERT_TRUE(base::CopyFile(invalid_file_original, invalid_file)); |
| 160 scoped_refptr<JsonPrefStore> pref_store = | 159 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 161 new JsonPrefStore(invalid_file, | 160 invalid_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 162 message_loop_.message_loop_proxy().get(), | |
| 163 scoped_ptr<PrefFilter>()); | |
| 164 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, | 161 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, |
| 165 pref_store->ReadPrefs()); | 162 pref_store->ReadPrefs()); |
| 166 EXPECT_FALSE(pref_store->ReadOnly()); | 163 EXPECT_FALSE(pref_store->ReadOnly()); |
| 167 | 164 |
| 168 // The file should have been moved aside. | 165 // The file should have been moved aside. |
| 169 EXPECT_FALSE(PathExists(invalid_file)); | 166 EXPECT_FALSE(PathExists(invalid_file)); |
| 170 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); | 167 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); |
| 171 EXPECT_TRUE(PathExists(moved_aside)); | 168 EXPECT_TRUE(PathExists(moved_aside)); |
| 172 EXPECT_TRUE(TextContentsEqual(invalid_file_original, moved_aside)); | 169 EXPECT_TRUE(TextContentsEqual(invalid_file_original, moved_aside)); |
| 173 } | 170 } |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 } | 236 } |
| 240 | 237 |
| 241 TEST_F(JsonPrefStoreTest, Basic) { | 238 TEST_F(JsonPrefStoreTest, Basic) { |
| 242 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), | 239 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 243 temp_dir_.path().AppendASCII("write.json"))); | 240 temp_dir_.path().AppendASCII("write.json"))); |
| 244 | 241 |
| 245 // Test that the persistent value can be loaded. | 242 // Test that the persistent value can be loaded. |
| 246 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 243 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 247 ASSERT_TRUE(PathExists(input_file)); | 244 ASSERT_TRUE(PathExists(input_file)); |
| 248 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 245 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 249 input_file, | 246 input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 250 message_loop_.message_loop_proxy().get(), | |
| 251 scoped_ptr<PrefFilter>()); | |
| 252 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 247 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 253 EXPECT_FALSE(pref_store->ReadOnly()); | 248 EXPECT_FALSE(pref_store->ReadOnly()); |
| 254 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 249 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
| 255 | 250 |
| 256 // The JSON file looks like this: | 251 // The JSON file looks like this: |
| 257 // { | 252 // { |
| 258 // "homepage": "http://www.cnn.com", | 253 // "homepage": "http://www.cnn.com", |
| 259 // "some_directory": "/usr/local/", | 254 // "some_directory": "/usr/local/", |
| 260 // "tabs": { | 255 // "tabs": { |
| 261 // "new_windows_in_tabs": true, | 256 // "new_windows_in_tabs": true, |
| 262 // "max_tabs": 20 | 257 // "max_tabs": 20 |
| 263 // } | 258 // } |
| 264 // } | 259 // } |
| 265 | 260 |
| 266 RunBasicJsonPrefStoreTest( | 261 RunBasicJsonPrefStoreTest( |
| 267 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 262 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
| 268 } | 263 } |
| 269 | 264 |
| 270 TEST_F(JsonPrefStoreTest, BasicAsync) { | 265 TEST_F(JsonPrefStoreTest, BasicAsync) { |
| 271 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), | 266 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 272 temp_dir_.path().AppendASCII("write.json"))); | 267 temp_dir_.path().AppendASCII("write.json"))); |
| 273 | 268 |
| 274 // Test that the persistent value can be loaded. | 269 // Test that the persistent value can be loaded. |
| 275 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 270 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 276 ASSERT_TRUE(PathExists(input_file)); | 271 ASSERT_TRUE(PathExists(input_file)); |
| 277 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 272 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 278 input_file, | 273 input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 279 message_loop_.message_loop_proxy().get(), | |
| 280 scoped_ptr<PrefFilter>()); | |
| 281 | 274 |
| 282 { | 275 { |
| 283 MockPrefStoreObserver mock_observer; | 276 MockPrefStoreObserver mock_observer; |
| 284 pref_store->AddObserver(&mock_observer); | 277 pref_store->AddObserver(&mock_observer); |
| 285 | 278 |
| 286 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | 279 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
| 287 pref_store->ReadPrefsAsync(mock_error_delegate); | 280 pref_store->ReadPrefsAsync(mock_error_delegate); |
| 288 | 281 |
| 289 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 282 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 290 EXPECT_CALL(*mock_error_delegate, | 283 EXPECT_CALL(*mock_error_delegate, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 307 // } | 300 // } |
| 308 | 301 |
| 309 RunBasicJsonPrefStoreTest( | 302 RunBasicJsonPrefStoreTest( |
| 310 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 303 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
| 311 } | 304 } |
| 312 | 305 |
| 313 TEST_F(JsonPrefStoreTest, PreserveEmptyValues) { | 306 TEST_F(JsonPrefStoreTest, PreserveEmptyValues) { |
| 314 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); | 307 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); |
| 315 | 308 |
| 316 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 309 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 317 pref_file, | 310 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 318 message_loop_.message_loop_proxy(), | |
| 319 scoped_ptr<PrefFilter>()); | |
| 320 | 311 |
| 321 // Set some keys with empty values. | 312 // Set some keys with empty values. |
| 322 pref_store->SetValue("list", new base::ListValue); | 313 pref_store->SetValue("list", new base::ListValue); |
| 323 pref_store->SetValue("dict", new base::DictionaryValue); | 314 pref_store->SetValue("dict", new base::DictionaryValue); |
| 324 | 315 |
| 325 // Write to file. | 316 // Write to file. |
| 326 pref_store->CommitPendingWrite(); | 317 pref_store->CommitPendingWrite(); |
| 327 MessageLoop::current()->RunUntilIdle(); | 318 MessageLoop::current()->RunUntilIdle(); |
| 328 | 319 |
| 329 // Reload. | 320 // Reload. |
| 330 pref_store = new JsonPrefStore( | 321 pref_store = new JsonPrefStore(pref_file, message_loop_.task_runner(), |
| 331 pref_file, | 322 scoped_ptr<PrefFilter>()); |
| 332 message_loop_.message_loop_proxy(), | |
| 333 scoped_ptr<PrefFilter>()); | |
| 334 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 323 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 335 ASSERT_FALSE(pref_store->ReadOnly()); | 324 ASSERT_FALSE(pref_store->ReadOnly()); |
| 336 | 325 |
| 337 // Check values. | 326 // Check values. |
| 338 const Value* result = NULL; | 327 const Value* result = NULL; |
| 339 EXPECT_TRUE(pref_store->GetValue("list", &result)); | 328 EXPECT_TRUE(pref_store->GetValue("list", &result)); |
| 340 EXPECT_TRUE(ListValue().Equals(result)); | 329 EXPECT_TRUE(ListValue().Equals(result)); |
| 341 EXPECT_TRUE(pref_store->GetValue("dict", &result)); | 330 EXPECT_TRUE(pref_store->GetValue("dict", &result)); |
| 342 EXPECT_TRUE(DictionaryValue().Equals(result)); | 331 EXPECT_TRUE(DictionaryValue().Equals(result)); |
| 343 } | 332 } |
| 344 | 333 |
| 345 // This test is just documenting some potentially non-obvious behavior. It | 334 // This test is just documenting some potentially non-obvious behavior. It |
| 346 // shouldn't be taken as normative. | 335 // shouldn't be taken as normative. |
| 347 TEST_F(JsonPrefStoreTest, RemoveClearsEmptyParent) { | 336 TEST_F(JsonPrefStoreTest, RemoveClearsEmptyParent) { |
| 348 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); | 337 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); |
| 349 | 338 |
| 350 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 339 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 351 pref_file, | 340 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 352 message_loop_.message_loop_proxy(), | |
| 353 scoped_ptr<PrefFilter>()); | |
| 354 | 341 |
| 355 base::DictionaryValue* dict = new base::DictionaryValue; | 342 base::DictionaryValue* dict = new base::DictionaryValue; |
| 356 dict->SetString("key", "value"); | 343 dict->SetString("key", "value"); |
| 357 pref_store->SetValue("dict", dict); | 344 pref_store->SetValue("dict", dict); |
| 358 | 345 |
| 359 pref_store->RemoveValue("dict.key"); | 346 pref_store->RemoveValue("dict.key"); |
| 360 | 347 |
| 361 const base::Value* retrieved_dict = NULL; | 348 const base::Value* retrieved_dict = NULL; |
| 362 bool has_dict = pref_store->GetValue("dict", &retrieved_dict); | 349 bool has_dict = pref_store->GetValue("dict", &retrieved_dict); |
| 363 EXPECT_FALSE(has_dict); | 350 EXPECT_FALSE(has_dict); |
| 364 } | 351 } |
| 365 | 352 |
| 366 // Tests asynchronous reading of the file when there is no file. | 353 // Tests asynchronous reading of the file when there is no file. |
| 367 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { | 354 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { |
| 368 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 355 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 369 ASSERT_FALSE(PathExists(bogus_input_file)); | 356 ASSERT_FALSE(PathExists(bogus_input_file)); |
| 370 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 357 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 371 bogus_input_file, | 358 bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 372 message_loop_.message_loop_proxy().get(), | |
| 373 scoped_ptr<PrefFilter>()); | |
| 374 MockPrefStoreObserver mock_observer; | 359 MockPrefStoreObserver mock_observer; |
| 375 pref_store->AddObserver(&mock_observer); | 360 pref_store->AddObserver(&mock_observer); |
| 376 | 361 |
| 377 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; | 362 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; |
| 378 pref_store->ReadPrefsAsync(mock_error_delegate); | 363 pref_store->ReadPrefsAsync(mock_error_delegate); |
| 379 | 364 |
| 380 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 365 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 381 EXPECT_CALL(*mock_error_delegate, | 366 EXPECT_CALL(*mock_error_delegate, |
| 382 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); | 367 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); |
| 383 RunLoop().RunUntilIdle(); | 368 RunLoop().RunUntilIdle(); |
| 384 pref_store->RemoveObserver(&mock_observer); | 369 pref_store->RemoveObserver(&mock_observer); |
| 385 | 370 |
| 386 EXPECT_FALSE(pref_store->ReadOnly()); | 371 EXPECT_FALSE(pref_store->ReadOnly()); |
| 387 } | 372 } |
| 388 | 373 |
| 389 TEST_F(JsonPrefStoreTest, ReadWithInterceptor) { | 374 TEST_F(JsonPrefStoreTest, ReadWithInterceptor) { |
| 390 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), | 375 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 391 temp_dir_.path().AppendASCII("write.json"))); | 376 temp_dir_.path().AppendASCII("write.json"))); |
| 392 | 377 |
| 393 // Test that the persistent value can be loaded. | 378 // Test that the persistent value can be loaded. |
| 394 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 379 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 395 ASSERT_TRUE(PathExists(input_file)); | 380 ASSERT_TRUE(PathExists(input_file)); |
| 396 | 381 |
| 397 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( | 382 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
| 398 new InterceptingPrefFilter()); | 383 new InterceptingPrefFilter()); |
| 399 InterceptingPrefFilter* raw_intercepting_pref_filter_ = | 384 InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
| 400 intercepting_pref_filter.get(); | 385 intercepting_pref_filter.get(); |
| 401 scoped_refptr<JsonPrefStore> pref_store = | 386 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 402 new JsonPrefStore(input_file, | 387 input_file, message_loop_.task_runner(), intercepting_pref_filter.Pass()); |
| 403 message_loop_.message_loop_proxy().get(), | |
| 404 intercepting_pref_filter.Pass()); | |
| 405 | 388 |
| 406 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE, | 389 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE, |
| 407 pref_store->ReadPrefs()); | 390 pref_store->ReadPrefs()); |
| 408 EXPECT_FALSE(pref_store->ReadOnly()); | 391 EXPECT_FALSE(pref_store->ReadOnly()); |
| 409 | 392 |
| 410 // The store shouldn't be considered initialized until the interceptor | 393 // The store shouldn't be considered initialized until the interceptor |
| 411 // returns. | 394 // returns. |
| 412 EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); | 395 EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 413 EXPECT_FALSE(pref_store->IsInitializationComplete()); | 396 EXPECT_FALSE(pref_store->IsInitializationComplete()); |
| 414 EXPECT_FALSE(pref_store->GetValue(kHomePage, NULL)); | 397 EXPECT_FALSE(pref_store->GetValue(kHomePage, NULL)); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 438 temp_dir_.path().AppendASCII("write.json"))); | 421 temp_dir_.path().AppendASCII("write.json"))); |
| 439 | 422 |
| 440 // Test that the persistent value can be loaded. | 423 // Test that the persistent value can be loaded. |
| 441 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 424 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 442 ASSERT_TRUE(PathExists(input_file)); | 425 ASSERT_TRUE(PathExists(input_file)); |
| 443 | 426 |
| 444 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( | 427 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
| 445 new InterceptingPrefFilter()); | 428 new InterceptingPrefFilter()); |
| 446 InterceptingPrefFilter* raw_intercepting_pref_filter_ = | 429 InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
| 447 intercepting_pref_filter.get(); | 430 intercepting_pref_filter.get(); |
| 448 scoped_refptr<JsonPrefStore> pref_store = | 431 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 449 new JsonPrefStore(input_file, | 432 input_file, message_loop_.task_runner(), intercepting_pref_filter.Pass()); |
| 450 message_loop_.message_loop_proxy().get(), | |
| 451 intercepting_pref_filter.Pass()); | |
| 452 | 433 |
| 453 MockPrefStoreObserver mock_observer; | 434 MockPrefStoreObserver mock_observer; |
| 454 pref_store->AddObserver(&mock_observer); | 435 pref_store->AddObserver(&mock_observer); |
| 455 | 436 |
| 456 // Ownership of the |mock_error_delegate| is handed to the |pref_store| below. | 437 // Ownership of the |mock_error_delegate| is handed to the |pref_store| below. |
| 457 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | 438 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
| 458 | 439 |
| 459 { | 440 { |
| 460 pref_store->ReadPrefsAsync(mock_error_delegate); | 441 pref_store->ReadPrefsAsync(mock_error_delegate); |
| 461 | 442 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 base::CopyFile(data_dir_.AppendASCII("read.json"), | 485 base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 505 temp_dir_.path().AppendASCII("alternate.json"))); | 486 temp_dir_.path().AppendASCII("alternate.json"))); |
| 506 | 487 |
| 507 // Test that the alternate file is moved to the main file and read as-is from | 488 // Test that the alternate file is moved to the main file and read as-is from |
| 508 // there. | 489 // there. |
| 509 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 490 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 510 base::FilePath alternate_input_file = | 491 base::FilePath alternate_input_file = |
| 511 temp_dir_.path().AppendASCII("alternate.json"); | 492 temp_dir_.path().AppendASCII("alternate.json"); |
| 512 ASSERT_FALSE(PathExists(input_file)); | 493 ASSERT_FALSE(PathExists(input_file)); |
| 513 ASSERT_TRUE(PathExists(alternate_input_file)); | 494 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 514 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 495 scoped_refptr<JsonPrefStore> pref_store = |
| 515 input_file, | 496 new JsonPrefStore(input_file, alternate_input_file, |
| 516 alternate_input_file, | 497 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 517 message_loop_.message_loop_proxy().get(), | |
| 518 scoped_ptr<PrefFilter>()); | |
| 519 | 498 |
| 520 ASSERT_FALSE(PathExists(input_file)); | 499 ASSERT_FALSE(PathExists(input_file)); |
| 521 ASSERT_TRUE(PathExists(alternate_input_file)); | 500 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 522 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 501 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 523 | 502 |
| 524 ASSERT_TRUE(PathExists(input_file)); | 503 ASSERT_TRUE(PathExists(input_file)); |
| 525 ASSERT_FALSE(PathExists(alternate_input_file)); | 504 ASSERT_FALSE(PathExists(alternate_input_file)); |
| 526 | 505 |
| 527 EXPECT_FALSE(pref_store->ReadOnly()); | 506 EXPECT_FALSE(pref_store->ReadOnly()); |
| 528 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 507 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 550 temp_dir_.path().AppendASCII("alternate.json"))); | 529 temp_dir_.path().AppendASCII("alternate.json"))); |
| 551 | 530 |
| 552 // Test that the alternate file is ignored and that the read occurs from the | 531 // Test that the alternate file is ignored and that the read occurs from the |
| 553 // existing main file. There is no attempt at even deleting the alternate | 532 // existing main file. There is no attempt at even deleting the alternate |
| 554 // file as this scenario should never happen in normal user-data-dirs. | 533 // file as this scenario should never happen in normal user-data-dirs. |
| 555 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 534 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 556 base::FilePath alternate_input_file = | 535 base::FilePath alternate_input_file = |
| 557 temp_dir_.path().AppendASCII("alternate.json"); | 536 temp_dir_.path().AppendASCII("alternate.json"); |
| 558 ASSERT_TRUE(PathExists(input_file)); | 537 ASSERT_TRUE(PathExists(input_file)); |
| 559 ASSERT_TRUE(PathExists(alternate_input_file)); | 538 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 560 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 539 scoped_refptr<JsonPrefStore> pref_store = |
| 561 input_file, | 540 new JsonPrefStore(input_file, alternate_input_file, |
| 562 alternate_input_file, | 541 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 563 message_loop_.message_loop_proxy().get(), | |
| 564 scoped_ptr<PrefFilter>()); | |
| 565 | 542 |
| 566 ASSERT_TRUE(PathExists(input_file)); | 543 ASSERT_TRUE(PathExists(input_file)); |
| 567 ASSERT_TRUE(PathExists(alternate_input_file)); | 544 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 568 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 545 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 569 | 546 |
| 570 ASSERT_TRUE(PathExists(input_file)); | 547 ASSERT_TRUE(PathExists(input_file)); |
| 571 ASSERT_TRUE(PathExists(alternate_input_file)); | 548 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 572 | 549 |
| 573 EXPECT_FALSE(pref_store->ReadOnly()); | 550 EXPECT_FALSE(pref_store->ReadOnly()); |
| 574 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 551 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 592 base::CopyFile(data_dir_.AppendASCII("read.json"), | 569 base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 593 temp_dir_.path().AppendASCII("write.json"))); | 570 temp_dir_.path().AppendASCII("write.json"))); |
| 594 | 571 |
| 595 // Test that the basic read works fine when an alternate file is specified but | 572 // Test that the basic read works fine when an alternate file is specified but |
| 596 // does not exist. | 573 // does not exist. |
| 597 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 574 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 598 base::FilePath alternate_input_file = | 575 base::FilePath alternate_input_file = |
| 599 temp_dir_.path().AppendASCII("alternate.json"); | 576 temp_dir_.path().AppendASCII("alternate.json"); |
| 600 ASSERT_TRUE(PathExists(input_file)); | 577 ASSERT_TRUE(PathExists(input_file)); |
| 601 ASSERT_FALSE(PathExists(alternate_input_file)); | 578 ASSERT_FALSE(PathExists(alternate_input_file)); |
| 602 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 579 scoped_refptr<JsonPrefStore> pref_store = |
| 603 input_file, | 580 new JsonPrefStore(input_file, alternate_input_file, |
| 604 alternate_input_file, | 581 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 605 message_loop_.message_loop_proxy().get(), | |
| 606 scoped_ptr<PrefFilter>()); | |
| 607 | 582 |
| 608 ASSERT_TRUE(PathExists(input_file)); | 583 ASSERT_TRUE(PathExists(input_file)); |
| 609 ASSERT_FALSE(PathExists(alternate_input_file)); | 584 ASSERT_FALSE(PathExists(alternate_input_file)); |
| 610 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 585 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 611 | 586 |
| 612 ASSERT_TRUE(PathExists(input_file)); | 587 ASSERT_TRUE(PathExists(input_file)); |
| 613 ASSERT_FALSE(PathExists(alternate_input_file)); | 588 ASSERT_FALSE(PathExists(alternate_input_file)); |
| 614 | 589 |
| 615 EXPECT_FALSE(pref_store->ReadOnly()); | 590 EXPECT_FALSE(pref_store->ReadOnly()); |
| 616 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 591 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 634 base::CopyFile(data_dir_.AppendASCII("read.json"), | 609 base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 635 temp_dir_.path().AppendASCII("alternate.json"))); | 610 temp_dir_.path().AppendASCII("alternate.json"))); |
| 636 | 611 |
| 637 // Test that the alternate file is moved to the main file and read as-is from | 612 // Test that the alternate file is moved to the main file and read as-is from |
| 638 // there even when the read is made asynchronously. | 613 // there even when the read is made asynchronously. |
| 639 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 614 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 640 base::FilePath alternate_input_file = | 615 base::FilePath alternate_input_file = |
| 641 temp_dir_.path().AppendASCII("alternate.json"); | 616 temp_dir_.path().AppendASCII("alternate.json"); |
| 642 ASSERT_FALSE(PathExists(input_file)); | 617 ASSERT_FALSE(PathExists(input_file)); |
| 643 ASSERT_TRUE(PathExists(alternate_input_file)); | 618 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 644 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 619 scoped_refptr<JsonPrefStore> pref_store = |
| 645 input_file, | 620 new JsonPrefStore(input_file, alternate_input_file, |
| 646 alternate_input_file, | 621 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 647 message_loop_.message_loop_proxy().get(), | |
| 648 scoped_ptr<PrefFilter>()); | |
| 649 | 622 |
| 650 ASSERT_FALSE(PathExists(input_file)); | 623 ASSERT_FALSE(PathExists(input_file)); |
| 651 ASSERT_TRUE(PathExists(alternate_input_file)); | 624 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 652 | 625 |
| 653 { | 626 { |
| 654 MockPrefStoreObserver mock_observer; | 627 MockPrefStoreObserver mock_observer; |
| 655 pref_store->AddObserver(&mock_observer); | 628 pref_store->AddObserver(&mock_observer); |
| 656 | 629 |
| 657 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | 630 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
| 658 pref_store->ReadPrefsAsync(mock_error_delegate); | 631 pref_store->ReadPrefsAsync(mock_error_delegate); |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 822 scoped_ptr<HistogramSamples> samples = | 795 scoped_ptr<HistogramSamples> samples = |
| 823 histogram.GetHistogram()->SnapshotSamples(); | 796 histogram.GetHistogram()->SnapshotSamples(); |
| 824 ASSERT_EQ(3, samples->GetCount(0)); | 797 ASSERT_EQ(3, samples->GetCount(0)); |
| 825 ASSERT_EQ(1, samples->GetCount(1)); | 798 ASSERT_EQ(1, samples->GetCount(1)); |
| 826 ASSERT_EQ(1, samples->GetCount(2)); | 799 ASSERT_EQ(1, samples->GetCount(2)); |
| 827 ASSERT_EQ(1, samples->GetCount(3)); | 800 ASSERT_EQ(1, samples->GetCount(3)); |
| 828 ASSERT_EQ(6, samples->TotalCount()); | 801 ASSERT_EQ(6, samples->TotalCount()); |
| 829 } | 802 } |
| 830 | 803 |
| 831 } // namespace base | 804 } // namespace base |
| OLD | NEW |