| 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 "components/prefs/json_pref_store.h" | 5 #include "components/prefs/json_pref_store.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 base::FilePath bogus_input_file = temp_dir_.GetPath().AppendASCII("read.txt"); | 159 base::FilePath bogus_input_file = temp_dir_.GetPath().AppendASCII("read.txt"); |
| 160 ASSERT_FALSE(PathExists(bogus_input_file)); | 160 ASSERT_FALSE(PathExists(bogus_input_file)); |
| 161 scoped_refptr<JsonPrefStore> pref_store = | 161 scoped_refptr<JsonPrefStore> pref_store = |
| 162 new JsonPrefStore(bogus_input_file, message_loop_.task_runner(), | 162 new JsonPrefStore(bogus_input_file, message_loop_.task_runner(), |
| 163 std::unique_ptr<PrefFilter>()); | 163 std::unique_ptr<PrefFilter>()); |
| 164 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 164 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
| 165 pref_store->ReadPrefs()); | 165 pref_store->ReadPrefs()); |
| 166 EXPECT_FALSE(pref_store->ReadOnly()); | 166 EXPECT_FALSE(pref_store->ReadOnly()); |
| 167 } | 167 } |
| 168 | 168 |
| 169 // Test fallback behavior for a nonexistent file and alternate file. | |
| 170 TEST_F(JsonPrefStoreTest, NonExistentFileAndAlternateFile) { | |
| 171 base::FilePath bogus_input_file = temp_dir_.GetPath().AppendASCII("read.txt"); | |
| 172 base::FilePath bogus_alternate_input_file = | |
| 173 temp_dir_.GetPath().AppendASCII("read_alternate.txt"); | |
| 174 ASSERT_FALSE(PathExists(bogus_input_file)); | |
| 175 ASSERT_FALSE(PathExists(bogus_alternate_input_file)); | |
| 176 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | |
| 177 bogus_input_file, bogus_alternate_input_file, message_loop_.task_runner(), | |
| 178 std::unique_ptr<PrefFilter>()); | |
| 179 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | |
| 180 pref_store->ReadPrefs()); | |
| 181 EXPECT_FALSE(pref_store->ReadOnly()); | |
| 182 } | |
| 183 | |
| 184 // Test fallback behavior for an invalid file. | 169 // Test fallback behavior for an invalid file. |
| 185 TEST_F(JsonPrefStoreTest, InvalidFile) { | 170 TEST_F(JsonPrefStoreTest, InvalidFile) { |
| 186 base::FilePath invalid_file = temp_dir_.GetPath().AppendASCII("invalid.json"); | 171 base::FilePath invalid_file = temp_dir_.GetPath().AppendASCII("invalid.json"); |
| 187 ASSERT_LT(0, base::WriteFile(invalid_file, | 172 ASSERT_LT(0, base::WriteFile(invalid_file, |
| 188 kInvalidJson, arraysize(kInvalidJson) - 1)); | 173 kInvalidJson, arraysize(kInvalidJson) - 1)); |
| 189 | 174 |
| 190 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 175 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 191 invalid_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); | 176 invalid_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); |
| 192 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, | 177 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, |
| 193 pref_store->ReadPrefs()); | 178 pref_store->ReadPrefs()); |
| (...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 // "some_directory": "/usr/local/", | 496 // "some_directory": "/usr/local/", |
| 512 // "tabs": { | 497 // "tabs": { |
| 513 // "new_windows_in_tabs": true, | 498 // "new_windows_in_tabs": true, |
| 514 // "max_tabs": 20 | 499 // "max_tabs": 20 |
| 515 // } | 500 // } |
| 516 // } | 501 // } |
| 517 | 502 |
| 518 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); | 503 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
| 519 } | 504 } |
| 520 | 505 |
| 521 TEST_F(JsonPrefStoreTest, AlternateFile) { | |
| 522 base::FilePath alternate_input_file = | |
| 523 temp_dir_.GetPath().AppendASCII("alternate.json"); | |
| 524 ASSERT_LT(0, base::WriteFile(alternate_input_file, | |
| 525 kReadJson, arraysize(kReadJson) - 1)); | |
| 526 | |
| 527 // Test that the alternate file is moved to the main file and read as-is from | |
| 528 // there. | |
| 529 base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json"); | |
| 530 ASSERT_FALSE(PathExists(input_file)); | |
| 531 ASSERT_TRUE(PathExists(alternate_input_file)); | |
| 532 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | |
| 533 input_file, alternate_input_file, message_loop_.task_runner(), | |
| 534 std::unique_ptr<PrefFilter>()); | |
| 535 | |
| 536 ASSERT_FALSE(PathExists(input_file)); | |
| 537 ASSERT_TRUE(PathExists(alternate_input_file)); | |
| 538 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | |
| 539 | |
| 540 ASSERT_TRUE(PathExists(input_file)); | |
| 541 ASSERT_FALSE(PathExists(alternate_input_file)); | |
| 542 | |
| 543 EXPECT_FALSE(pref_store->ReadOnly()); | |
| 544 EXPECT_TRUE(pref_store->IsInitializationComplete()); | |
| 545 | |
| 546 // The JSON file looks like this: | |
| 547 // { | |
| 548 // "homepage": "http://www.cnn.com", | |
| 549 // "some_directory": "/usr/local/", | |
| 550 // "tabs": { | |
| 551 // "new_windows_in_tabs": true, | |
| 552 // "max_tabs": 20 | |
| 553 // } | |
| 554 // } | |
| 555 | |
| 556 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); | |
| 557 } | |
| 558 | |
| 559 TEST_F(JsonPrefStoreTest, AlternateFileIgnoredWhenMainFileExists) { | |
| 560 base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json"); | |
| 561 ASSERT_LT(0, base::WriteFile(input_file, | |
| 562 kReadJson, arraysize(kReadJson) - 1)); | |
| 563 | |
| 564 base::FilePath alternate_input_file = | |
| 565 temp_dir_.GetPath().AppendASCII("alternate.json"); | |
| 566 ASSERT_LT(0, base::WriteFile(alternate_input_file, | |
| 567 kInvalidJson, arraysize(kInvalidJson) - 1)); | |
| 568 | |
| 569 // Test that the alternate file is ignored and that the read occurs from the | |
| 570 // existing main file. There is no attempt at even deleting the alternate | |
| 571 // file as this scenario should never happen in normal user-data-dirs. | |
| 572 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | |
| 573 input_file, alternate_input_file, message_loop_.task_runner(), | |
| 574 std::unique_ptr<PrefFilter>()); | |
| 575 | |
| 576 ASSERT_TRUE(PathExists(input_file)); | |
| 577 ASSERT_TRUE(PathExists(alternate_input_file)); | |
| 578 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | |
| 579 | |
| 580 ASSERT_TRUE(PathExists(input_file)); | |
| 581 ASSERT_TRUE(PathExists(alternate_input_file)); | |
| 582 | |
| 583 EXPECT_FALSE(pref_store->ReadOnly()); | |
| 584 EXPECT_TRUE(pref_store->IsInitializationComplete()); | |
| 585 | |
| 586 // The JSON file looks like this: | |
| 587 // { | |
| 588 // "homepage": "http://www.cnn.com", | |
| 589 // "some_directory": "/usr/local/", | |
| 590 // "tabs": { | |
| 591 // "new_windows_in_tabs": true, | |
| 592 // "max_tabs": 20 | |
| 593 // } | |
| 594 // } | |
| 595 | |
| 596 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); | |
| 597 } | |
| 598 | |
| 599 TEST_F(JsonPrefStoreTest, AlternateFileDNE) { | |
| 600 base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json"); | |
| 601 ASSERT_LT(0, base::WriteFile(input_file, | |
| 602 kReadJson, arraysize(kReadJson) - 1)); | |
| 603 | |
| 604 // Test that the basic read works fine when an alternate file is specified but | |
| 605 // does not exist. | |
| 606 base::FilePath alternate_input_file = | |
| 607 temp_dir_.GetPath().AppendASCII("alternate.json"); | |
| 608 ASSERT_TRUE(PathExists(input_file)); | |
| 609 ASSERT_FALSE(PathExists(alternate_input_file)); | |
| 610 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | |
| 611 input_file, alternate_input_file, message_loop_.task_runner(), | |
| 612 std::unique_ptr<PrefFilter>()); | |
| 613 | |
| 614 ASSERT_TRUE(PathExists(input_file)); | |
| 615 ASSERT_FALSE(PathExists(alternate_input_file)); | |
| 616 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | |
| 617 | |
| 618 ASSERT_TRUE(PathExists(input_file)); | |
| 619 ASSERT_FALSE(PathExists(alternate_input_file)); | |
| 620 | |
| 621 EXPECT_FALSE(pref_store->ReadOnly()); | |
| 622 EXPECT_TRUE(pref_store->IsInitializationComplete()); | |
| 623 | |
| 624 // The JSON file looks like this: | |
| 625 // { | |
| 626 // "homepage": "http://www.cnn.com", | |
| 627 // "some_directory": "/usr/local/", | |
| 628 // "tabs": { | |
| 629 // "new_windows_in_tabs": true, | |
| 630 // "max_tabs": 20 | |
| 631 // } | |
| 632 // } | |
| 633 | |
| 634 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); | |
| 635 } | |
| 636 | |
| 637 TEST_F(JsonPrefStoreTest, BasicAsyncWithAlternateFile) { | |
| 638 base::FilePath alternate_input_file = | |
| 639 temp_dir_.GetPath().AppendASCII("alternate.json"); | |
| 640 ASSERT_LT(0, base::WriteFile(alternate_input_file, | |
| 641 kReadJson, arraysize(kReadJson) - 1)); | |
| 642 | |
| 643 // Test that the alternate file is moved to the main file and read as-is from | |
| 644 // there even when the read is made asynchronously. | |
| 645 base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json"); | |
| 646 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | |
| 647 input_file, alternate_input_file, message_loop_.task_runner(), | |
| 648 std::unique_ptr<PrefFilter>()); | |
| 649 | |
| 650 ASSERT_FALSE(PathExists(input_file)); | |
| 651 ASSERT_TRUE(PathExists(alternate_input_file)); | |
| 652 | |
| 653 { | |
| 654 MockPrefStoreObserver mock_observer; | |
| 655 pref_store->AddObserver(&mock_observer); | |
| 656 | |
| 657 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | |
| 658 pref_store->ReadPrefsAsync(mock_error_delegate); | |
| 659 | |
| 660 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | |
| 661 EXPECT_CALL(*mock_error_delegate, | |
| 662 OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); | |
| 663 RunLoop().RunUntilIdle(); | |
| 664 pref_store->RemoveObserver(&mock_observer); | |
| 665 | |
| 666 EXPECT_FALSE(pref_store->ReadOnly()); | |
| 667 EXPECT_TRUE(pref_store->IsInitializationComplete()); | |
| 668 } | |
| 669 | |
| 670 ASSERT_TRUE(PathExists(input_file)); | |
| 671 ASSERT_FALSE(PathExists(alternate_input_file)); | |
| 672 | |
| 673 // The JSON file looks like this: | |
| 674 // { | |
| 675 // "homepage": "http://www.cnn.com", | |
| 676 // "some_directory": "/usr/local/", | |
| 677 // "tabs": { | |
| 678 // "new_windows_in_tabs": true, | |
| 679 // "max_tabs": 20 | |
| 680 // } | |
| 681 // } | |
| 682 | |
| 683 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); | |
| 684 } | |
| 685 | |
| 686 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestBasic) { | 506 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestBasic) { |
| 687 base::HistogramTester histogram_tester; | 507 base::HistogramTester histogram_tester; |
| 688 | 508 |
| 689 SimpleTestClock* test_clock = new SimpleTestClock; | 509 SimpleTestClock* test_clock = new SimpleTestClock; |
| 690 SetCurrentTimeInMinutes(0, test_clock); | 510 SetCurrentTimeInMinutes(0, test_clock); |
| 691 JsonPrefStore::WriteCountHistogram histogram( | 511 JsonPrefStore::WriteCountHistogram histogram( |
| 692 base::TimeDelta::FromSeconds(10), | 512 base::TimeDelta::FromSeconds(10), |
| 693 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), | 513 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
| 694 std::unique_ptr<base::Clock>(test_clock)); | 514 std::unique_ptr<base::Clock>(test_clock)); |
| 695 int32_t report_interval = | 515 int32_t report_interval = |
| (...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1244 file_writer->WriteNow(MakeUnique<std::string>("foo")); | 1064 file_writer->WriteNow(MakeUnique<std::string>("foo")); |
| 1245 } | 1065 } |
| 1246 RunLoop().RunUntilIdle(); | 1066 RunLoop().RunUntilIdle(); |
| 1247 EXPECT_FALSE(successful_write_reply_observer_.GetAndResetObservationState()); | 1067 EXPECT_FALSE(successful_write_reply_observer_.GetAndResetObservationState()); |
| 1248 EXPECT_TRUE(write_callback_observer_.GetAndResetPreWriteObservationState()); | 1068 EXPECT_TRUE(write_callback_observer_.GetAndResetPreWriteObservationState()); |
| 1249 EXPECT_EQ(CALLED_WITH_SUCCESS, | 1069 EXPECT_EQ(CALLED_WITH_SUCCESS, |
| 1250 write_callback_observer_.GetAndResetPostWriteObservationState()); | 1070 write_callback_observer_.GetAndResetPostWriteObservationState()); |
| 1251 } | 1071 } |
| 1252 | 1072 |
| 1253 } // namespace base | 1073 } // namespace base |
| OLD | NEW |