| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 EXPECT_EQ(cnn, string_value); | 185 EXPECT_EQ(cnn, string_value); |
| 188 | 186 |
| 189 const char kSomeDirectory[] = "some_directory"; | 187 const char kSomeDirectory[] = "some_directory"; |
| 190 | 188 |
| 191 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); | 189 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); |
| 192 base::FilePath::StringType path; | 190 base::FilePath::StringType path; |
| 193 EXPECT_TRUE(actual->GetAsString(&path)); | 191 EXPECT_TRUE(actual->GetAsString(&path)); |
| 194 EXPECT_EQ(base::FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path); | 192 EXPECT_EQ(base::FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path); |
| 195 base::FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); | 193 base::FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); |
| 196 | 194 |
| 197 pref_store->SetValue(kSomeDirectory, new StringValue(some_path.value())); | 195 pref_store->SetValue(kSomeDirectory, new StringValue(some_path.value()), |
| 196 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 198 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); | 197 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); |
| 199 EXPECT_TRUE(actual->GetAsString(&path)); | 198 EXPECT_TRUE(actual->GetAsString(&path)); |
| 200 EXPECT_EQ(some_path.value(), path); | 199 EXPECT_EQ(some_path.value(), path); |
| 201 | 200 |
| 202 // Test reading some other data types from sub-dictionaries. | 201 // Test reading some other data types from sub-dictionaries. |
| 203 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); | 202 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); |
| 204 bool boolean = false; | 203 bool boolean = false; |
| 205 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); | 204 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
| 206 EXPECT_TRUE(boolean); | 205 EXPECT_TRUE(boolean); |
| 207 | 206 |
| 208 pref_store->SetValue(kNewWindowsInTabs, new FundamentalValue(false)); | 207 pref_store->SetValue(kNewWindowsInTabs, new FundamentalValue(false), |
| 208 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 209 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); | 209 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); |
| 210 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); | 210 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
| 211 EXPECT_FALSE(boolean); | 211 EXPECT_FALSE(boolean); |
| 212 | 212 |
| 213 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); | 213 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); |
| 214 int integer = 0; | 214 int integer = 0; |
| 215 EXPECT_TRUE(actual->GetAsInteger(&integer)); | 215 EXPECT_TRUE(actual->GetAsInteger(&integer)); |
| 216 EXPECT_EQ(20, integer); | 216 EXPECT_EQ(20, integer); |
| 217 pref_store->SetValue(kMaxTabs, new FundamentalValue(10)); | 217 pref_store->SetValue(kMaxTabs, new FundamentalValue(10), |
| 218 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 218 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); | 219 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); |
| 219 EXPECT_TRUE(actual->GetAsInteger(&integer)); | 220 EXPECT_TRUE(actual->GetAsInteger(&integer)); |
| 220 EXPECT_EQ(10, integer); | 221 EXPECT_EQ(10, integer); |
| 221 | 222 |
| 222 pref_store->SetValue(kLongIntPref, | 223 pref_store->SetValue(kLongIntPref, |
| 223 new StringValue(base::Int64ToString(214748364842LL))); | 224 new StringValue(base::Int64ToString(214748364842LL)), |
| 225 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 224 EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual)); | 226 EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual)); |
| 225 EXPECT_TRUE(actual->GetAsString(&string_value)); | 227 EXPECT_TRUE(actual->GetAsString(&string_value)); |
| 226 int64 value; | 228 int64 value; |
| 227 base::StringToInt64(string_value, &value); | 229 base::StringToInt64(string_value, &value); |
| 228 EXPECT_EQ(214748364842LL, value); | 230 EXPECT_EQ(214748364842LL, value); |
| 229 | 231 |
| 230 // Serialize and compare to expected output. | 232 // Serialize and compare to expected output. |
| 231 ASSERT_TRUE(PathExists(golden_output_file)); | 233 ASSERT_TRUE(PathExists(golden_output_file)); |
| 232 pref_store->CommitPendingWrite(); | 234 pref_store->CommitPendingWrite(); |
| 233 RunLoop().RunUntilIdle(); | 235 RunLoop().RunUntilIdle(); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 305 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
| 304 } | 306 } |
| 305 | 307 |
| 306 TEST_F(JsonPrefStoreTest, PreserveEmptyValues) { | 308 TEST_F(JsonPrefStoreTest, PreserveEmptyValues) { |
| 307 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); | 309 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); |
| 308 | 310 |
| 309 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 311 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 310 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 312 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 311 | 313 |
| 312 // Set some keys with empty values. | 314 // Set some keys with empty values. |
| 313 pref_store->SetValue("list", new base::ListValue); | 315 pref_store->SetValue("list", new base::ListValue, |
| 314 pref_store->SetValue("dict", new base::DictionaryValue); | 316 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 317 pref_store->SetValue("dict", new base::DictionaryValue, |
| 318 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 315 | 319 |
| 316 // Write to file. | 320 // Write to file. |
| 317 pref_store->CommitPendingWrite(); | 321 pref_store->CommitPendingWrite(); |
| 318 MessageLoop::current()->RunUntilIdle(); | 322 RunLoop().RunUntilIdle(); |
| 319 | 323 |
| 320 // Reload. | 324 // Reload. |
| 321 pref_store = new JsonPrefStore(pref_file, message_loop_.task_runner(), | 325 pref_store = new JsonPrefStore(pref_file, message_loop_.task_runner(), |
| 322 scoped_ptr<PrefFilter>()); | 326 scoped_ptr<PrefFilter>()); |
| 323 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 327 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 324 ASSERT_FALSE(pref_store->ReadOnly()); | 328 ASSERT_FALSE(pref_store->ReadOnly()); |
| 325 | 329 |
| 326 // Check values. | 330 // Check values. |
| 327 const Value* result = NULL; | 331 const Value* result = NULL; |
| 328 EXPECT_TRUE(pref_store->GetValue("list", &result)); | 332 EXPECT_TRUE(pref_store->GetValue("list", &result)); |
| 329 EXPECT_TRUE(ListValue().Equals(result)); | 333 EXPECT_TRUE(ListValue().Equals(result)); |
| 330 EXPECT_TRUE(pref_store->GetValue("dict", &result)); | 334 EXPECT_TRUE(pref_store->GetValue("dict", &result)); |
| 331 EXPECT_TRUE(DictionaryValue().Equals(result)); | 335 EXPECT_TRUE(DictionaryValue().Equals(result)); |
| 332 } | 336 } |
| 333 | 337 |
| 334 // This test is just documenting some potentially non-obvious behavior. It | 338 // This test is just documenting some potentially non-obvious behavior. It |
| 335 // shouldn't be taken as normative. | 339 // shouldn't be taken as normative. |
| 336 TEST_F(JsonPrefStoreTest, RemoveClearsEmptyParent) { | 340 TEST_F(JsonPrefStoreTest, RemoveClearsEmptyParent) { |
| 337 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); | 341 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); |
| 338 | 342 |
| 339 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 343 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 340 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 344 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 341 | 345 |
| 342 base::DictionaryValue* dict = new base::DictionaryValue; | 346 base::DictionaryValue* dict = new base::DictionaryValue; |
| 343 dict->SetString("key", "value"); | 347 dict->SetString("key", "value"); |
| 344 pref_store->SetValue("dict", dict); | 348 pref_store->SetValue("dict", dict, |
| 349 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 345 | 350 |
| 346 pref_store->RemoveValue("dict.key"); | 351 pref_store->RemoveValue("dict.key", |
| 352 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 347 | 353 |
| 348 const base::Value* retrieved_dict = NULL; | 354 const base::Value* retrieved_dict = NULL; |
| 349 bool has_dict = pref_store->GetValue("dict", &retrieved_dict); | 355 bool has_dict = pref_store->GetValue("dict", &retrieved_dict); |
| 350 EXPECT_FALSE(has_dict); | 356 EXPECT_FALSE(has_dict); |
| 351 } | 357 } |
| 352 | 358 |
| 353 // Tests asynchronous reading of the file when there is no file. | 359 // Tests asynchronous reading of the file when there is no file. |
| 354 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { | 360 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { |
| 355 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 361 base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); |
| 356 ASSERT_FALSE(PathExists(bogus_input_file)); | 362 ASSERT_FALSE(PathExists(bogus_input_file)); |
| 357 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 363 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 358 bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 364 bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 359 MockPrefStoreObserver mock_observer; | 365 MockPrefStoreObserver mock_observer; |
| 360 pref_store->AddObserver(&mock_observer); | 366 pref_store->AddObserver(&mock_observer); |
| 361 | 367 |
| 362 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; | 368 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; |
| 363 pref_store->ReadPrefsAsync(mock_error_delegate); | 369 pref_store->ReadPrefsAsync(mock_error_delegate); |
| 364 | 370 |
| 365 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... |
| 794 histogram.ReportOutstandingWrites(); | 800 histogram.ReportOutstandingWrites(); |
| 795 scoped_ptr<HistogramSamples> samples = | 801 scoped_ptr<HistogramSamples> samples = |
| 796 histogram.GetHistogram()->SnapshotSamples(); | 802 histogram.GetHistogram()->SnapshotSamples(); |
| 797 ASSERT_EQ(3, samples->GetCount(0)); | 803 ASSERT_EQ(3, samples->GetCount(0)); |
| 798 ASSERT_EQ(1, samples->GetCount(1)); | 804 ASSERT_EQ(1, samples->GetCount(1)); |
| 799 ASSERT_EQ(1, samples->GetCount(2)); | 805 ASSERT_EQ(1, samples->GetCount(2)); |
| 800 ASSERT_EQ(1, samples->GetCount(3)); | 806 ASSERT_EQ(1, samples->GetCount(3)); |
| 801 ASSERT_EQ(6, samples->TotalCount()); | 807 ASSERT_EQ(6, samples->TotalCount()); |
| 802 } | 808 } |
| 803 | 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 |
| 804 } // namespace base | 931 } // namespace base |
| OLD | NEW |