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/location.h" |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 101 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
102 | 102 |
103 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_)); | 103 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_)); |
104 data_dir_ = data_dir_.AppendASCII("prefs"); | 104 data_dir_ = data_dir_.AppendASCII("prefs"); |
105 ASSERT_TRUE(PathExists(data_dir_)); | 105 ASSERT_TRUE(PathExists(data_dir_)); |
106 } | 106 } |
107 | 107 |
108 void TearDown() override { | 108 void TearDown() override { |
109 // 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 |
110 // JsonPrefStore may post write tasks). | 110 // JsonPrefStore may post write tasks). |
111 message_loop_.task_runner()->PostTask(FROM_HERE, | 111 RunLoop().RunUntilIdle(); |
112 MessageLoop::QuitWhenIdleClosure()); | |
113 message_loop_.Run(); | |
114 } | 112 } |
115 | 113 |
116 // The path to temporary directory used to contain the test operations. | 114 // The path to temporary directory used to contain the test operations. |
117 base::ScopedTempDir temp_dir_; | 115 base::ScopedTempDir temp_dir_; |
118 // The path to the directory where the test data is stored. | 116 // The path to the directory where the test data is stored. |
119 base::FilePath data_dir_; | 117 base::FilePath data_dir_; |
120 // A message loop that we can use as the file thread message loop. | 118 // A message loop that we can use as the file thread message loop. |
121 MessageLoop message_loop_; | 119 MessageLoop message_loop_; |
122 | 120 |
123 private: | 121 private: |
124 // Ensure histograms are reset for each test. | 122 // Ensure histograms are reset for each test. |
125 StatisticsRecorder statistics_recorder_; | 123 StatisticsRecorder statistics_recorder_; |
126 }; | 124 }; |
127 | 125 |
128 // Test fallback behavior for a nonexistent file. | 126 // Test fallback behavior for a nonexistent file. |
129 TEST_F(JsonPrefStoreTest, NonExistentFile) { | 127 TEST_F(JsonPrefStoreTest, NonExistentFile) { |
130 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 128 base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); |
131 ASSERT_FALSE(PathExists(bogus_input_file)); | 129 ASSERT_FALSE(PathExists(bogus_input_file)); |
132 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 130 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
133 bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 131 bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
134 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 132 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
135 pref_store->ReadPrefs()); | 133 pref_store->ReadPrefs()); |
136 EXPECT_FALSE(pref_store->ReadOnly()); | 134 EXPECT_FALSE(pref_store->ReadOnly()); |
137 } | 135 } |
138 | 136 |
139 // Test fallback behavior for a nonexistent file and alternate file. | 137 // Test fallback behavior for a nonexistent file and alternate file. |
140 TEST_F(JsonPrefStoreTest, NonExistentFileAndAlternateFile) { | 138 TEST_F(JsonPrefStoreTest, NonExistentFileAndAlternateFile) { |
141 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 139 base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); |
142 base::FilePath bogus_alternate_input_file = | 140 base::FilePath bogus_alternate_input_file = |
143 data_dir_.AppendASCII("read_alternate.txt"); | 141 temp_dir_.path().AppendASCII("read_alternate.txt"); |
144 ASSERT_FALSE(PathExists(bogus_input_file)); | 142 ASSERT_FALSE(PathExists(bogus_input_file)); |
145 ASSERT_FALSE(PathExists(bogus_alternate_input_file)); | 143 ASSERT_FALSE(PathExists(bogus_alternate_input_file)); |
146 scoped_refptr<JsonPrefStore> pref_store = | 144 scoped_refptr<JsonPrefStore> pref_store = |
147 new JsonPrefStore(bogus_input_file, bogus_alternate_input_file, | 145 new JsonPrefStore(bogus_input_file, bogus_alternate_input_file, |
148 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 146 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
149 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 147 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
150 pref_store->ReadPrefs()); | 148 pref_store->ReadPrefs()); |
151 EXPECT_FALSE(pref_store->ReadOnly()); | 149 EXPECT_FALSE(pref_store->ReadOnly()); |
152 } | 150 } |
153 | 151 |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 312 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
315 | 313 |
316 // Set some keys with empty values. | 314 // Set some keys with empty values. |
317 pref_store->SetValue("list", new base::ListValue, | 315 pref_store->SetValue("list", new base::ListValue, |
318 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 316 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
319 pref_store->SetValue("dict", new base::DictionaryValue, | 317 pref_store->SetValue("dict", new base::DictionaryValue, |
320 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 318 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
321 | 319 |
322 // Write to file. | 320 // Write to file. |
323 pref_store->CommitPendingWrite(); | 321 pref_store->CommitPendingWrite(); |
324 MessageLoop::current()->RunUntilIdle(); | 322 RunLoop().RunUntilIdle(); |
325 | 323 |
326 // Reload. | 324 // Reload. |
327 pref_store = new JsonPrefStore(pref_file, message_loop_.task_runner(), | 325 pref_store = new JsonPrefStore(pref_file, message_loop_.task_runner(), |
328 scoped_ptr<PrefFilter>()); | 326 scoped_ptr<PrefFilter>()); |
329 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 327 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
330 ASSERT_FALSE(pref_store->ReadOnly()); | 328 ASSERT_FALSE(pref_store->ReadOnly()); |
331 | 329 |
332 // Check values. | 330 // Check values. |
333 const Value* result = NULL; | 331 const Value* result = NULL; |
334 EXPECT_TRUE(pref_store->GetValue("list", &result)); | 332 EXPECT_TRUE(pref_store->GetValue("list", &result)); |
(...skipping 18 matching lines...) Expand all Loading... |
353 pref_store->RemoveValue("dict.key", | 351 pref_store->RemoveValue("dict.key", |
354 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 352 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
355 | 353 |
356 const base::Value* retrieved_dict = NULL; | 354 const base::Value* retrieved_dict = NULL; |
357 bool has_dict = pref_store->GetValue("dict", &retrieved_dict); | 355 bool has_dict = pref_store->GetValue("dict", &retrieved_dict); |
358 EXPECT_FALSE(has_dict); | 356 EXPECT_FALSE(has_dict); |
359 } | 357 } |
360 | 358 |
361 // Tests asynchronous reading of the file when there is no file. | 359 // Tests asynchronous reading of the file when there is no file. |
362 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { | 360 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { |
363 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 361 base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); |
364 ASSERT_FALSE(PathExists(bogus_input_file)); | 362 ASSERT_FALSE(PathExists(bogus_input_file)); |
365 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 363 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
366 bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 364 bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
367 MockPrefStoreObserver mock_observer; | 365 MockPrefStoreObserver mock_observer; |
368 pref_store->AddObserver(&mock_observer); | 366 pref_store->AddObserver(&mock_observer); |
369 | 367 |
370 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; | 368 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; |
371 pref_store->ReadPrefsAsync(mock_error_delegate); | 369 pref_store->ReadPrefsAsync(mock_error_delegate); |
372 | 370 |
373 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 371 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
802 histogram.ReportOutstandingWrites(); | 800 histogram.ReportOutstandingWrites(); |
803 scoped_ptr<HistogramSamples> samples = | 801 scoped_ptr<HistogramSamples> samples = |
804 histogram.GetHistogram()->SnapshotSamples(); | 802 histogram.GetHistogram()->SnapshotSamples(); |
805 ASSERT_EQ(3, samples->GetCount(0)); | 803 ASSERT_EQ(3, samples->GetCount(0)); |
806 ASSERT_EQ(1, samples->GetCount(1)); | 804 ASSERT_EQ(1, samples->GetCount(1)); |
807 ASSERT_EQ(1, samples->GetCount(2)); | 805 ASSERT_EQ(1, samples->GetCount(2)); |
808 ASSERT_EQ(1, samples->GetCount(3)); | 806 ASSERT_EQ(1, samples->GetCount(3)); |
809 ASSERT_EQ(6, samples->TotalCount()); | 807 ASSERT_EQ(6, samples->TotalCount()); |
810 } | 808 } |
811 | 809 |
| 810 class JsonPrefStoreLossyWriteTest : public JsonPrefStoreTest { |
| 811 protected: |
| 812 void SetUp() override { |
| 813 JsonPrefStoreTest::SetUp(); |
| 814 test_file_ = temp_dir_.path().AppendASCII("test.json"); |
| 815 } |
| 816 |
| 817 // Creates a JsonPrefStore with the given |file_writer|. |
| 818 scoped_refptr<JsonPrefStore> CreatePrefStore() { |
| 819 return new JsonPrefStore(test_file_, message_loop_.task_runner(), |
| 820 scoped_ptr<PrefFilter>()); |
| 821 } |
| 822 |
| 823 // Return the ImportantFileWriter for a given JsonPrefStore. |
| 824 ImportantFileWriter* GetImportantFileWriter( |
| 825 scoped_refptr<JsonPrefStore> pref_store) { |
| 826 return &(pref_store->writer_); |
| 827 } |
| 828 |
| 829 // Get the contents of kTestFile. Pumps the message loop before returning the |
| 830 // result. |
| 831 std::string GetTestFileContents() { |
| 832 RunLoop().RunUntilIdle(); |
| 833 std::string file_contents; |
| 834 ReadFileToString(test_file_, &file_contents); |
| 835 return file_contents; |
| 836 } |
| 837 |
| 838 private: |
| 839 base::FilePath test_file_; |
| 840 }; |
| 841 |
| 842 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteBasic) { |
| 843 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
| 844 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); |
| 845 |
| 846 // Set a normal pref and check that it gets scheduled to be written. |
| 847 ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 848 pref_store->SetValue("normal", new base::StringValue("normal"), |
| 849 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 850 ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 851 file_writer->DoScheduledWrite(); |
| 852 ASSERT_EQ("{\"normal\":\"normal\"}", GetTestFileContents()); |
| 853 ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 854 |
| 855 // Set a lossy pref and check that it is not scheduled to be written. |
| 856 // SetValue/RemoveValue. |
| 857 pref_store->SetValue("lossy", new base::StringValue("lossy"), |
| 858 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 859 ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 860 pref_store->RemoveValue("lossy", WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 861 ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 862 |
| 863 // SetValueSilently/RemoveValueSilently. |
| 864 pref_store->SetValueSilently("lossy", new base::StringValue("lossy"), |
| 865 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 866 ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 867 pref_store->RemoveValueSilently("lossy", |
| 868 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 869 ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 870 |
| 871 // ReportValueChanged. |
| 872 pref_store->SetValue("lossy", new base::StringValue("lossy"), |
| 873 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 874 ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 875 pref_store->ReportValueChanged("lossy", |
| 876 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 877 ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 878 |
| 879 // Call CommitPendingWrite and check that the lossy pref and the normal pref |
| 880 // are there with the last values set above. |
| 881 pref_store->CommitPendingWrite(); |
| 882 ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 883 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
| 884 GetTestFileContents()); |
| 885 } |
| 886 |
| 887 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossyFirst) { |
| 888 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
| 889 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); |
| 890 |
| 891 // Set a lossy pref and check that it is not scheduled to be written. |
| 892 ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 893 pref_store->SetValue("lossy", new base::StringValue("lossy"), |
| 894 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 895 ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 896 |
| 897 // Set a normal pref and check that it is scheduled to be written. |
| 898 pref_store->SetValue("normal", new base::StringValue("normal"), |
| 899 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 900 ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 901 |
| 902 // Call DoScheduledWrite and check both prefs get written. |
| 903 file_writer->DoScheduledWrite(); |
| 904 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
| 905 GetTestFileContents()); |
| 906 ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 907 } |
| 908 |
| 909 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossySecond) { |
| 910 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
| 911 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); |
| 912 |
| 913 // Set a normal pref and check that it is scheduled to be written. |
| 914 ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 915 pref_store->SetValue("normal", new base::StringValue("normal"), |
| 916 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 917 ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 918 |
| 919 // Set a lossy pref and check that the write is still scheduled. |
| 920 pref_store->SetValue("lossy", new base::StringValue("lossy"), |
| 921 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 922 ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 923 |
| 924 // Call DoScheduledWrite and check both prefs get written. |
| 925 file_writer->DoScheduledWrite(); |
| 926 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
| 927 GetTestFileContents()); |
| 928 ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 929 } |
| 930 |
812 } // namespace base | 931 } // namespace base |
OLD | NEW |