Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: base/prefs/json_pref_store_unittest.cc

Issue 1136983004: Revert of Implement lossy pref behavior for JsonPrefStore. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@prefs-fix-flags
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/prefs/json_pref_store.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 RunLoop().RunUntilIdle(); 111 message_loop_.task_runner()->PostTask(FROM_HERE,
112 MessageLoop::QuitWhenIdleClosure());
113 message_loop_.Run();
112 } 114 }
113 115
114 // The path to temporary directory used to contain the test operations. 116 // The path to temporary directory used to contain the test operations.
115 base::ScopedTempDir temp_dir_; 117 base::ScopedTempDir temp_dir_;
116 // The path to the directory where the test data is stored. 118 // The path to the directory where the test data is stored.
117 base::FilePath data_dir_; 119 base::FilePath data_dir_;
118 // 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.
119 MessageLoop message_loop_; 121 MessageLoop message_loop_;
120 122
121 private: 123 private:
122 // Ensure histograms are reset for each test. 124 // Ensure histograms are reset for each test.
123 StatisticsRecorder statistics_recorder_; 125 StatisticsRecorder statistics_recorder_;
124 }; 126 };
125 127
126 // Test fallback behavior for a nonexistent file. 128 // Test fallback behavior for a nonexistent file.
127 TEST_F(JsonPrefStoreTest, NonExistentFile) { 129 TEST_F(JsonPrefStoreTest, NonExistentFile) {
128 base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); 130 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
129 ASSERT_FALSE(PathExists(bogus_input_file)); 131 ASSERT_FALSE(PathExists(bogus_input_file));
130 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( 132 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore(
131 bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); 133 bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>());
132 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, 134 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
133 pref_store->ReadPrefs()); 135 pref_store->ReadPrefs());
134 EXPECT_FALSE(pref_store->ReadOnly()); 136 EXPECT_FALSE(pref_store->ReadOnly());
135 } 137 }
136 138
137 // Test fallback behavior for a nonexistent file and alternate file. 139 // Test fallback behavior for a nonexistent file and alternate file.
138 TEST_F(JsonPrefStoreTest, NonExistentFileAndAlternateFile) { 140 TEST_F(JsonPrefStoreTest, NonExistentFileAndAlternateFile) {
139 base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); 141 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
140 base::FilePath bogus_alternate_input_file = 142 base::FilePath bogus_alternate_input_file =
141 temp_dir_.path().AppendASCII("read_alternate.txt"); 143 data_dir_.AppendASCII("read_alternate.txt");
142 ASSERT_FALSE(PathExists(bogus_input_file)); 144 ASSERT_FALSE(PathExists(bogus_input_file));
143 ASSERT_FALSE(PathExists(bogus_alternate_input_file)); 145 ASSERT_FALSE(PathExists(bogus_alternate_input_file));
144 scoped_refptr<JsonPrefStore> pref_store = 146 scoped_refptr<JsonPrefStore> pref_store =
145 new JsonPrefStore(bogus_input_file, bogus_alternate_input_file, 147 new JsonPrefStore(bogus_input_file, bogus_alternate_input_file,
146 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); 148 message_loop_.task_runner(), scoped_ptr<PrefFilter>());
147 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, 149 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
148 pref_store->ReadPrefs()); 150 pref_store->ReadPrefs());
149 EXPECT_FALSE(pref_store->ReadOnly()); 151 EXPECT_FALSE(pref_store->ReadOnly());
150 } 152 }
151 153
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); 314 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>());
313 315
314 // Set some keys with empty values. 316 // Set some keys with empty values.
315 pref_store->SetValue("list", new base::ListValue, 317 pref_store->SetValue("list", new base::ListValue,
316 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 318 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
317 pref_store->SetValue("dict", new base::DictionaryValue, 319 pref_store->SetValue("dict", new base::DictionaryValue,
318 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 320 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
319 321
320 // Write to file. 322 // Write to file.
321 pref_store->CommitPendingWrite(); 323 pref_store->CommitPendingWrite();
322 RunLoop().RunUntilIdle(); 324 MessageLoop::current()->RunUntilIdle();
323 325
324 // Reload. 326 // Reload.
325 pref_store = new JsonPrefStore(pref_file, message_loop_.task_runner(), 327 pref_store = new JsonPrefStore(pref_file, message_loop_.task_runner(),
326 scoped_ptr<PrefFilter>()); 328 scoped_ptr<PrefFilter>());
327 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); 329 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
328 ASSERT_FALSE(pref_store->ReadOnly()); 330 ASSERT_FALSE(pref_store->ReadOnly());
329 331
330 // Check values. 332 // Check values.
331 const Value* result = NULL; 333 const Value* result = NULL;
332 EXPECT_TRUE(pref_store->GetValue("list", &result)); 334 EXPECT_TRUE(pref_store->GetValue("list", &result));
(...skipping 18 matching lines...) Expand all
351 pref_store->RemoveValue("dict.key", 353 pref_store->RemoveValue("dict.key",
352 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 354 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
353 355
354 const base::Value* retrieved_dict = NULL; 356 const base::Value* retrieved_dict = NULL;
355 bool has_dict = pref_store->GetValue("dict", &retrieved_dict); 357 bool has_dict = pref_store->GetValue("dict", &retrieved_dict);
356 EXPECT_FALSE(has_dict); 358 EXPECT_FALSE(has_dict);
357 } 359 }
358 360
359 // Tests asynchronous reading of the file when there is no file. 361 // Tests asynchronous reading of the file when there is no file.
360 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { 362 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) {
361 base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); 363 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
362 ASSERT_FALSE(PathExists(bogus_input_file)); 364 ASSERT_FALSE(PathExists(bogus_input_file));
363 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( 365 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore(
364 bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); 366 bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>());
365 MockPrefStoreObserver mock_observer; 367 MockPrefStoreObserver mock_observer;
366 pref_store->AddObserver(&mock_observer); 368 pref_store->AddObserver(&mock_observer);
367 369
368 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; 370 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate;
369 pref_store->ReadPrefsAsync(mock_error_delegate); 371 pref_store->ReadPrefsAsync(mock_error_delegate);
370 372
371 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); 373 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after
800 histogram.ReportOutstandingWrites(); 802 histogram.ReportOutstandingWrites();
801 scoped_ptr<HistogramSamples> samples = 803 scoped_ptr<HistogramSamples> samples =
802 histogram.GetHistogram()->SnapshotSamples(); 804 histogram.GetHistogram()->SnapshotSamples();
803 ASSERT_EQ(3, samples->GetCount(0)); 805 ASSERT_EQ(3, samples->GetCount(0));
804 ASSERT_EQ(1, samples->GetCount(1)); 806 ASSERT_EQ(1, samples->GetCount(1));
805 ASSERT_EQ(1, samples->GetCount(2)); 807 ASSERT_EQ(1, samples->GetCount(2));
806 ASSERT_EQ(1, samples->GetCount(3)); 808 ASSERT_EQ(1, samples->GetCount(3));
807 ASSERT_EQ(6, samples->TotalCount()); 809 ASSERT_EQ(6, samples->TotalCount());
808 } 810 }
809 811
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
931 } // namespace base 812 } // namespace base
OLDNEW
« no previous file with comments | « base/prefs/json_pref_store.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698