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 "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/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/prefs/pref_filter.h" | 14 #include "base/prefs/pref_filter.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/single_thread_task_runner.h" | |
| 16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/threading/sequenced_worker_pool.h" | 20 #include "base/threading/sequenced_worker_pool.h" |
| 20 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
| 21 #include "base/values.h" | 22 #include "base/values.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 25 |
| 25 namespace base { | 26 namespace base { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 91 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 91 | 92 |
| 92 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_)); | 93 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_)); |
| 93 data_dir_ = data_dir_.AppendASCII("prefs"); | 94 data_dir_ = data_dir_.AppendASCII("prefs"); |
| 94 ASSERT_TRUE(PathExists(data_dir_)); | 95 ASSERT_TRUE(PathExists(data_dir_)); |
| 95 } | 96 } |
| 96 | 97 |
| 97 void TearDown() override { | 98 void TearDown() override { |
| 98 // Make sure all pending tasks have been processed (e.g., deleting the | 99 // Make sure all pending tasks have been processed (e.g., deleting the |
| 99 // JsonPrefStore may post write tasks). | 100 // JsonPrefStore may post write tasks). |
| 100 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure()); | 101 message_loop_.task_runner()->PostTask(FROM_HERE, |
| 102 MessageLoop::QuitWhenIdleClosure()); | |
| 101 message_loop_.Run(); | 103 message_loop_.Run(); |
| 102 } | 104 } |
| 103 | 105 |
| 104 // The path to temporary directory used to contain the test operations. | 106 // The path to temporary directory used to contain the test operations. |
| 105 base::ScopedTempDir temp_dir_; | 107 base::ScopedTempDir temp_dir_; |
| 106 // The path to the directory where the test data is stored. | 108 // The path to the directory where the test data is stored. |
| 107 base::FilePath data_dir_; | 109 base::FilePath data_dir_; |
| 108 // A message loop that we can use as the file thread message loop. | 110 // A message loop that we can use as the file thread message loop. |
| 109 MessageLoop message_loop_; | 111 MessageLoop message_loop_; |
| 110 }; | 112 }; |
| 111 | 113 |
| 112 // Test fallback behavior for a nonexistent file. | 114 // Test fallback behavior for a nonexistent file. |
| 113 TEST_F(JsonPrefStoreTest, NonExistentFile) { | 115 TEST_F(JsonPrefStoreTest, NonExistentFile) { |
| 114 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 116 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 115 ASSERT_FALSE(PathExists(bogus_input_file)); | 117 ASSERT_FALSE(PathExists(bogus_input_file)); |
| 116 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 118 scoped_refptr<JsonPrefStore> pref_store = |
| 117 bogus_input_file, | 119 new JsonPrefStore(bogus_input_file, message_loop_.task_runner().get(), |
|
danakj
2015/04/21 20:16:30
nit: no get, it's taking a refptr
same for all ot
Sami
2015/04/23 17:48:24
Done.
| |
| 118 message_loop_.message_loop_proxy().get(), | 120 scoped_ptr<PrefFilter>()); |
| 119 scoped_ptr<PrefFilter>()); | |
| 120 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 121 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
| 121 pref_store->ReadPrefs()); | 122 pref_store->ReadPrefs()); |
| 122 EXPECT_FALSE(pref_store->ReadOnly()); | 123 EXPECT_FALSE(pref_store->ReadOnly()); |
| 123 } | 124 } |
| 124 | 125 |
| 125 // Test fallback behavior for a nonexistent file and alternate file. | 126 // Test fallback behavior for a nonexistent file and alternate file. |
| 126 TEST_F(JsonPrefStoreTest, NonExistentFileAndAlternateFile) { | 127 TEST_F(JsonPrefStoreTest, NonExistentFileAndAlternateFile) { |
| 127 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 128 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 128 base::FilePath bogus_alternate_input_file = | 129 base::FilePath bogus_alternate_input_file = |
| 129 data_dir_.AppendASCII("read_alternate.txt"); | 130 data_dir_.AppendASCII("read_alternate.txt"); |
| 130 ASSERT_FALSE(PathExists(bogus_input_file)); | 131 ASSERT_FALSE(PathExists(bogus_input_file)); |
| 131 ASSERT_FALSE(PathExists(bogus_alternate_input_file)); | 132 ASSERT_FALSE(PathExists(bogus_alternate_input_file)); |
| 132 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 133 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 133 bogus_input_file, | 134 bogus_input_file, bogus_alternate_input_file, |
| 134 bogus_alternate_input_file, | 135 message_loop_.task_runner().get(), scoped_ptr<PrefFilter>()); |
| 135 message_loop_.message_loop_proxy().get(), | |
| 136 scoped_ptr<PrefFilter>()); | |
| 137 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 136 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
| 138 pref_store->ReadPrefs()); | 137 pref_store->ReadPrefs()); |
| 139 EXPECT_FALSE(pref_store->ReadOnly()); | 138 EXPECT_FALSE(pref_store->ReadOnly()); |
| 140 } | 139 } |
| 141 | 140 |
| 142 // Test fallback behavior for an invalid file. | 141 // Test fallback behavior for an invalid file. |
| 143 TEST_F(JsonPrefStoreTest, InvalidFile) { | 142 TEST_F(JsonPrefStoreTest, InvalidFile) { |
| 144 base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); | 143 base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); |
| 145 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); | 144 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); |
| 146 ASSERT_TRUE(base::CopyFile(invalid_file_original, invalid_file)); | 145 ASSERT_TRUE(base::CopyFile(invalid_file_original, invalid_file)); |
| 147 scoped_refptr<JsonPrefStore> pref_store = | 146 scoped_refptr<JsonPrefStore> pref_store = |
| 148 new JsonPrefStore(invalid_file, | 147 new JsonPrefStore(invalid_file, message_loop_.task_runner().get(), |
| 149 message_loop_.message_loop_proxy().get(), | |
| 150 scoped_ptr<PrefFilter>()); | 148 scoped_ptr<PrefFilter>()); |
| 151 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, | 149 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, |
| 152 pref_store->ReadPrefs()); | 150 pref_store->ReadPrefs()); |
| 153 EXPECT_FALSE(pref_store->ReadOnly()); | 151 EXPECT_FALSE(pref_store->ReadOnly()); |
| 154 | 152 |
| 155 // The file should have been moved aside. | 153 // The file should have been moved aside. |
| 156 EXPECT_FALSE(PathExists(invalid_file)); | 154 EXPECT_FALSE(PathExists(invalid_file)); |
| 157 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); | 155 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); |
| 158 EXPECT_TRUE(PathExists(moved_aside)); | 156 EXPECT_TRUE(PathExists(moved_aside)); |
| 159 EXPECT_TRUE(TextContentsEqual(invalid_file_original, moved_aside)); | 157 EXPECT_TRUE(TextContentsEqual(invalid_file_original, moved_aside)); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 226 } | 224 } |
| 227 | 225 |
| 228 TEST_F(JsonPrefStoreTest, Basic) { | 226 TEST_F(JsonPrefStoreTest, Basic) { |
| 229 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), | 227 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 230 temp_dir_.path().AppendASCII("write.json"))); | 228 temp_dir_.path().AppendASCII("write.json"))); |
| 231 | 229 |
| 232 // Test that the persistent value can be loaded. | 230 // Test that the persistent value can be loaded. |
| 233 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 231 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 234 ASSERT_TRUE(PathExists(input_file)); | 232 ASSERT_TRUE(PathExists(input_file)); |
| 235 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 233 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 236 input_file, | 234 input_file, message_loop_.task_runner().get(), scoped_ptr<PrefFilter>()); |
| 237 message_loop_.message_loop_proxy().get(), | |
| 238 scoped_ptr<PrefFilter>()); | |
| 239 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 235 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 240 EXPECT_FALSE(pref_store->ReadOnly()); | 236 EXPECT_FALSE(pref_store->ReadOnly()); |
| 241 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 237 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
| 242 | 238 |
| 243 // The JSON file looks like this: | 239 // The JSON file looks like this: |
| 244 // { | 240 // { |
| 245 // "homepage": "http://www.cnn.com", | 241 // "homepage": "http://www.cnn.com", |
| 246 // "some_directory": "/usr/local/", | 242 // "some_directory": "/usr/local/", |
| 247 // "tabs": { | 243 // "tabs": { |
| 248 // "new_windows_in_tabs": true, | 244 // "new_windows_in_tabs": true, |
| 249 // "max_tabs": 20 | 245 // "max_tabs": 20 |
| 250 // } | 246 // } |
| 251 // } | 247 // } |
| 252 | 248 |
| 253 RunBasicJsonPrefStoreTest( | 249 RunBasicJsonPrefStoreTest( |
| 254 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 250 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
| 255 } | 251 } |
| 256 | 252 |
| 257 TEST_F(JsonPrefStoreTest, BasicAsync) { | 253 TEST_F(JsonPrefStoreTest, BasicAsync) { |
| 258 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), | 254 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 259 temp_dir_.path().AppendASCII("write.json"))); | 255 temp_dir_.path().AppendASCII("write.json"))); |
| 260 | 256 |
| 261 // Test that the persistent value can be loaded. | 257 // Test that the persistent value can be loaded. |
| 262 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 258 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 263 ASSERT_TRUE(PathExists(input_file)); | 259 ASSERT_TRUE(PathExists(input_file)); |
| 264 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 260 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 265 input_file, | 261 input_file, message_loop_.task_runner().get(), scoped_ptr<PrefFilter>()); |
| 266 message_loop_.message_loop_proxy().get(), | |
| 267 scoped_ptr<PrefFilter>()); | |
| 268 | 262 |
| 269 { | 263 { |
| 270 MockPrefStoreObserver mock_observer; | 264 MockPrefStoreObserver mock_observer; |
| 271 pref_store->AddObserver(&mock_observer); | 265 pref_store->AddObserver(&mock_observer); |
| 272 | 266 |
| 273 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | 267 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
| 274 pref_store->ReadPrefsAsync(mock_error_delegate); | 268 pref_store->ReadPrefsAsync(mock_error_delegate); |
| 275 | 269 |
| 276 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 270 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 277 EXPECT_CALL(*mock_error_delegate, | 271 EXPECT_CALL(*mock_error_delegate, |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 294 // } | 288 // } |
| 295 | 289 |
| 296 RunBasicJsonPrefStoreTest( | 290 RunBasicJsonPrefStoreTest( |
| 297 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 291 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
| 298 } | 292 } |
| 299 | 293 |
| 300 TEST_F(JsonPrefStoreTest, PreserveEmptyValues) { | 294 TEST_F(JsonPrefStoreTest, PreserveEmptyValues) { |
| 301 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); | 295 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); |
| 302 | 296 |
| 303 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 297 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 304 pref_file, | 298 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 305 message_loop_.message_loop_proxy(), | |
| 306 scoped_ptr<PrefFilter>()); | |
| 307 | 299 |
| 308 // Set some keys with empty values. | 300 // Set some keys with empty values. |
| 309 pref_store->SetValue("list", new base::ListValue); | 301 pref_store->SetValue("list", new base::ListValue); |
| 310 pref_store->SetValue("dict", new base::DictionaryValue); | 302 pref_store->SetValue("dict", new base::DictionaryValue); |
| 311 | 303 |
| 312 // Write to file. | 304 // Write to file. |
| 313 pref_store->CommitPendingWrite(); | 305 pref_store->CommitPendingWrite(); |
| 314 MessageLoop::current()->RunUntilIdle(); | 306 MessageLoop::current()->RunUntilIdle(); |
| 315 | 307 |
| 316 // Reload. | 308 // Reload. |
| 317 pref_store = new JsonPrefStore( | 309 pref_store = new JsonPrefStore(pref_file, message_loop_.task_runner(), |
| 318 pref_file, | 310 scoped_ptr<PrefFilter>()); |
| 319 message_loop_.message_loop_proxy(), | |
| 320 scoped_ptr<PrefFilter>()); | |
| 321 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 311 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 322 ASSERT_FALSE(pref_store->ReadOnly()); | 312 ASSERT_FALSE(pref_store->ReadOnly()); |
| 323 | 313 |
| 324 // Check values. | 314 // Check values. |
| 325 const Value* result = NULL; | 315 const Value* result = NULL; |
| 326 EXPECT_TRUE(pref_store->GetValue("list", &result)); | 316 EXPECT_TRUE(pref_store->GetValue("list", &result)); |
| 327 EXPECT_TRUE(ListValue().Equals(result)); | 317 EXPECT_TRUE(ListValue().Equals(result)); |
| 328 EXPECT_TRUE(pref_store->GetValue("dict", &result)); | 318 EXPECT_TRUE(pref_store->GetValue("dict", &result)); |
| 329 EXPECT_TRUE(DictionaryValue().Equals(result)); | 319 EXPECT_TRUE(DictionaryValue().Equals(result)); |
| 330 } | 320 } |
| 331 | 321 |
| 332 // This test is just documenting some potentially non-obvious behavior. It | 322 // This test is just documenting some potentially non-obvious behavior. It |
| 333 // shouldn't be taken as normative. | 323 // shouldn't be taken as normative. |
| 334 TEST_F(JsonPrefStoreTest, RemoveClearsEmptyParent) { | 324 TEST_F(JsonPrefStoreTest, RemoveClearsEmptyParent) { |
| 335 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); | 325 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); |
| 336 | 326 |
| 337 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 327 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 338 pref_file, | 328 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 339 message_loop_.message_loop_proxy(), | |
| 340 scoped_ptr<PrefFilter>()); | |
| 341 | 329 |
| 342 base::DictionaryValue* dict = new base::DictionaryValue; | 330 base::DictionaryValue* dict = new base::DictionaryValue; |
| 343 dict->SetString("key", "value"); | 331 dict->SetString("key", "value"); |
| 344 pref_store->SetValue("dict", dict); | 332 pref_store->SetValue("dict", dict); |
| 345 | 333 |
| 346 pref_store->RemoveValue("dict.key"); | 334 pref_store->RemoveValue("dict.key"); |
| 347 | 335 |
| 348 const base::Value* retrieved_dict = NULL; | 336 const base::Value* retrieved_dict = NULL; |
| 349 bool has_dict = pref_store->GetValue("dict", &retrieved_dict); | 337 bool has_dict = pref_store->GetValue("dict", &retrieved_dict); |
| 350 EXPECT_FALSE(has_dict); | 338 EXPECT_FALSE(has_dict); |
| 351 } | 339 } |
| 352 | 340 |
| 353 // Tests asynchronous reading of the file when there is no file. | 341 // Tests asynchronous reading of the file when there is no file. |
| 354 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { | 342 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { |
| 355 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 343 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 356 ASSERT_FALSE(PathExists(bogus_input_file)); | 344 ASSERT_FALSE(PathExists(bogus_input_file)); |
| 357 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 345 scoped_refptr<JsonPrefStore> pref_store = |
| 358 bogus_input_file, | 346 new JsonPrefStore(bogus_input_file, message_loop_.task_runner().get(), |
| 359 message_loop_.message_loop_proxy().get(), | 347 scoped_ptr<PrefFilter>()); |
| 360 scoped_ptr<PrefFilter>()); | |
| 361 MockPrefStoreObserver mock_observer; | 348 MockPrefStoreObserver mock_observer; |
| 362 pref_store->AddObserver(&mock_observer); | 349 pref_store->AddObserver(&mock_observer); |
| 363 | 350 |
| 364 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; | 351 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; |
| 365 pref_store->ReadPrefsAsync(mock_error_delegate); | 352 pref_store->ReadPrefsAsync(mock_error_delegate); |
| 366 | 353 |
| 367 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 354 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 368 EXPECT_CALL(*mock_error_delegate, | 355 EXPECT_CALL(*mock_error_delegate, |
| 369 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); | 356 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); |
| 370 RunLoop().RunUntilIdle(); | 357 RunLoop().RunUntilIdle(); |
| 371 pref_store->RemoveObserver(&mock_observer); | 358 pref_store->RemoveObserver(&mock_observer); |
| 372 | 359 |
| 373 EXPECT_FALSE(pref_store->ReadOnly()); | 360 EXPECT_FALSE(pref_store->ReadOnly()); |
| 374 } | 361 } |
| 375 | 362 |
| 376 TEST_F(JsonPrefStoreTest, ReadWithInterceptor) { | 363 TEST_F(JsonPrefStoreTest, ReadWithInterceptor) { |
| 377 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), | 364 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 378 temp_dir_.path().AppendASCII("write.json"))); | 365 temp_dir_.path().AppendASCII("write.json"))); |
| 379 | 366 |
| 380 // Test that the persistent value can be loaded. | 367 // Test that the persistent value can be loaded. |
| 381 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 368 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 382 ASSERT_TRUE(PathExists(input_file)); | 369 ASSERT_TRUE(PathExists(input_file)); |
| 383 | 370 |
| 384 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( | 371 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
| 385 new InterceptingPrefFilter()); | 372 new InterceptingPrefFilter()); |
| 386 InterceptingPrefFilter* raw_intercepting_pref_filter_ = | 373 InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
| 387 intercepting_pref_filter.get(); | 374 intercepting_pref_filter.get(); |
| 388 scoped_refptr<JsonPrefStore> pref_store = | 375 scoped_refptr<JsonPrefStore> pref_store = |
| 389 new JsonPrefStore(input_file, | 376 new JsonPrefStore(input_file, message_loop_.task_runner().get(), |
| 390 message_loop_.message_loop_proxy().get(), | |
| 391 intercepting_pref_filter.Pass()); | 377 intercepting_pref_filter.Pass()); |
| 392 | 378 |
| 393 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE, | 379 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE, |
| 394 pref_store->ReadPrefs()); | 380 pref_store->ReadPrefs()); |
| 395 EXPECT_FALSE(pref_store->ReadOnly()); | 381 EXPECT_FALSE(pref_store->ReadOnly()); |
| 396 | 382 |
| 397 // The store shouldn't be considered initialized until the interceptor | 383 // The store shouldn't be considered initialized until the interceptor |
| 398 // returns. | 384 // returns. |
| 399 EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); | 385 EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 400 EXPECT_FALSE(pref_store->IsInitializationComplete()); | 386 EXPECT_FALSE(pref_store->IsInitializationComplete()); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 426 | 412 |
| 427 // Test that the persistent value can be loaded. | 413 // Test that the persistent value can be loaded. |
| 428 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 414 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 429 ASSERT_TRUE(PathExists(input_file)); | 415 ASSERT_TRUE(PathExists(input_file)); |
| 430 | 416 |
| 431 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( | 417 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
| 432 new InterceptingPrefFilter()); | 418 new InterceptingPrefFilter()); |
| 433 InterceptingPrefFilter* raw_intercepting_pref_filter_ = | 419 InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
| 434 intercepting_pref_filter.get(); | 420 intercepting_pref_filter.get(); |
| 435 scoped_refptr<JsonPrefStore> pref_store = | 421 scoped_refptr<JsonPrefStore> pref_store = |
| 436 new JsonPrefStore(input_file, | 422 new JsonPrefStore(input_file, message_loop_.task_runner().get(), |
| 437 message_loop_.message_loop_proxy().get(), | |
| 438 intercepting_pref_filter.Pass()); | 423 intercepting_pref_filter.Pass()); |
| 439 | 424 |
| 440 MockPrefStoreObserver mock_observer; | 425 MockPrefStoreObserver mock_observer; |
| 441 pref_store->AddObserver(&mock_observer); | 426 pref_store->AddObserver(&mock_observer); |
| 442 | 427 |
| 443 // Ownership of the |mock_error_delegate| is handed to the |pref_store| below. | 428 // Ownership of the |mock_error_delegate| is handed to the |pref_store| below. |
| 444 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | 429 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
| 445 | 430 |
| 446 { | 431 { |
| 447 pref_store->ReadPrefsAsync(mock_error_delegate); | 432 pref_store->ReadPrefsAsync(mock_error_delegate); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 492 temp_dir_.path().AppendASCII("alternate.json"))); | 477 temp_dir_.path().AppendASCII("alternate.json"))); |
| 493 | 478 |
| 494 // Test that the alternate file is moved to the main file and read as-is from | 479 // Test that the alternate file is moved to the main file and read as-is from |
| 495 // there. | 480 // there. |
| 496 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 481 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 497 base::FilePath alternate_input_file = | 482 base::FilePath alternate_input_file = |
| 498 temp_dir_.path().AppendASCII("alternate.json"); | 483 temp_dir_.path().AppendASCII("alternate.json"); |
| 499 ASSERT_FALSE(PathExists(input_file)); | 484 ASSERT_FALSE(PathExists(input_file)); |
| 500 ASSERT_TRUE(PathExists(alternate_input_file)); | 485 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 501 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 486 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 502 input_file, | 487 input_file, alternate_input_file, message_loop_.task_runner().get(), |
| 503 alternate_input_file, | |
| 504 message_loop_.message_loop_proxy().get(), | |
| 505 scoped_ptr<PrefFilter>()); | 488 scoped_ptr<PrefFilter>()); |
| 506 | 489 |
| 507 ASSERT_FALSE(PathExists(input_file)); | 490 ASSERT_FALSE(PathExists(input_file)); |
| 508 ASSERT_TRUE(PathExists(alternate_input_file)); | 491 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 509 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 492 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 510 | 493 |
| 511 ASSERT_TRUE(PathExists(input_file)); | 494 ASSERT_TRUE(PathExists(input_file)); |
| 512 ASSERT_FALSE(PathExists(alternate_input_file)); | 495 ASSERT_FALSE(PathExists(alternate_input_file)); |
| 513 | 496 |
| 514 EXPECT_FALSE(pref_store->ReadOnly()); | 497 EXPECT_FALSE(pref_store->ReadOnly()); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 538 | 521 |
| 539 // Test that the alternate file is ignored and that the read occurs from the | 522 // Test that the alternate file is ignored and that the read occurs from the |
| 540 // existing main file. There is no attempt at even deleting the alternate | 523 // existing main file. There is no attempt at even deleting the alternate |
| 541 // file as this scenario should never happen in normal user-data-dirs. | 524 // file as this scenario should never happen in normal user-data-dirs. |
| 542 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 525 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 543 base::FilePath alternate_input_file = | 526 base::FilePath alternate_input_file = |
| 544 temp_dir_.path().AppendASCII("alternate.json"); | 527 temp_dir_.path().AppendASCII("alternate.json"); |
| 545 ASSERT_TRUE(PathExists(input_file)); | 528 ASSERT_TRUE(PathExists(input_file)); |
| 546 ASSERT_TRUE(PathExists(alternate_input_file)); | 529 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 547 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 530 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 548 input_file, | 531 input_file, alternate_input_file, message_loop_.task_runner().get(), |
| 549 alternate_input_file, | |
| 550 message_loop_.message_loop_proxy().get(), | |
| 551 scoped_ptr<PrefFilter>()); | 532 scoped_ptr<PrefFilter>()); |
| 552 | 533 |
| 553 ASSERT_TRUE(PathExists(input_file)); | 534 ASSERT_TRUE(PathExists(input_file)); |
| 554 ASSERT_TRUE(PathExists(alternate_input_file)); | 535 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 555 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 536 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 556 | 537 |
| 557 ASSERT_TRUE(PathExists(input_file)); | 538 ASSERT_TRUE(PathExists(input_file)); |
| 558 ASSERT_TRUE(PathExists(alternate_input_file)); | 539 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 559 | 540 |
| 560 EXPECT_FALSE(pref_store->ReadOnly()); | 541 EXPECT_FALSE(pref_store->ReadOnly()); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 580 temp_dir_.path().AppendASCII("write.json"))); | 561 temp_dir_.path().AppendASCII("write.json"))); |
| 581 | 562 |
| 582 // Test that the basic read works fine when an alternate file is specified but | 563 // Test that the basic read works fine when an alternate file is specified but |
| 583 // does not exist. | 564 // does not exist. |
| 584 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 565 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 585 base::FilePath alternate_input_file = | 566 base::FilePath alternate_input_file = |
| 586 temp_dir_.path().AppendASCII("alternate.json"); | 567 temp_dir_.path().AppendASCII("alternate.json"); |
| 587 ASSERT_TRUE(PathExists(input_file)); | 568 ASSERT_TRUE(PathExists(input_file)); |
| 588 ASSERT_FALSE(PathExists(alternate_input_file)); | 569 ASSERT_FALSE(PathExists(alternate_input_file)); |
| 589 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 570 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 590 input_file, | 571 input_file, alternate_input_file, message_loop_.task_runner().get(), |
| 591 alternate_input_file, | |
| 592 message_loop_.message_loop_proxy().get(), | |
| 593 scoped_ptr<PrefFilter>()); | 572 scoped_ptr<PrefFilter>()); |
| 594 | 573 |
| 595 ASSERT_TRUE(PathExists(input_file)); | 574 ASSERT_TRUE(PathExists(input_file)); |
| 596 ASSERT_FALSE(PathExists(alternate_input_file)); | 575 ASSERT_FALSE(PathExists(alternate_input_file)); |
| 597 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 576 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 598 | 577 |
| 599 ASSERT_TRUE(PathExists(input_file)); | 578 ASSERT_TRUE(PathExists(input_file)); |
| 600 ASSERT_FALSE(PathExists(alternate_input_file)); | 579 ASSERT_FALSE(PathExists(alternate_input_file)); |
| 601 | 580 |
| 602 EXPECT_FALSE(pref_store->ReadOnly()); | 581 EXPECT_FALSE(pref_store->ReadOnly()); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 622 temp_dir_.path().AppendASCII("alternate.json"))); | 601 temp_dir_.path().AppendASCII("alternate.json"))); |
| 623 | 602 |
| 624 // Test that the alternate file is moved to the main file and read as-is from | 603 // Test that the alternate file is moved to the main file and read as-is from |
| 625 // there even when the read is made asynchronously. | 604 // there even when the read is made asynchronously. |
| 626 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 605 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 627 base::FilePath alternate_input_file = | 606 base::FilePath alternate_input_file = |
| 628 temp_dir_.path().AppendASCII("alternate.json"); | 607 temp_dir_.path().AppendASCII("alternate.json"); |
| 629 ASSERT_FALSE(PathExists(input_file)); | 608 ASSERT_FALSE(PathExists(input_file)); |
| 630 ASSERT_TRUE(PathExists(alternate_input_file)); | 609 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 631 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 610 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 632 input_file, | 611 input_file, alternate_input_file, message_loop_.task_runner().get(), |
| 633 alternate_input_file, | |
| 634 message_loop_.message_loop_proxy().get(), | |
| 635 scoped_ptr<PrefFilter>()); | 612 scoped_ptr<PrefFilter>()); |
| 636 | 613 |
| 637 ASSERT_FALSE(PathExists(input_file)); | 614 ASSERT_FALSE(PathExists(input_file)); |
| 638 ASSERT_TRUE(PathExists(alternate_input_file)); | 615 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 639 | 616 |
| 640 { | 617 { |
| 641 MockPrefStoreObserver mock_observer; | 618 MockPrefStoreObserver mock_observer; |
| 642 pref_store->AddObserver(&mock_observer); | 619 pref_store->AddObserver(&mock_observer); |
| 643 | 620 |
| 644 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | 621 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 665 // "new_windows_in_tabs": true, | 642 // "new_windows_in_tabs": true, |
| 666 // "max_tabs": 20 | 643 // "max_tabs": 20 |
| 667 // } | 644 // } |
| 668 // } | 645 // } |
| 669 | 646 |
| 670 RunBasicJsonPrefStoreTest( | 647 RunBasicJsonPrefStoreTest( |
| 671 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 648 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
| 672 } | 649 } |
| 673 | 650 |
| 674 } // namespace base | 651 } // namespace base |
| OLD | NEW |