Index: base/prefs/json_pref_store_unittest.cc |
diff --git a/base/prefs/json_pref_store_unittest.cc b/base/prefs/json_pref_store_unittest.cc |
index 0fab8685e9b21410731955169bad35b5d98ceef2..c78b777c317691518df89c2a1cda90c820f26323 100644 |
--- a/base/prefs/json_pref_store_unittest.cc |
+++ b/base/prefs/json_pref_store_unittest.cc |
@@ -809,4 +809,121 @@ TEST_F(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps) { |
ASSERT_EQ(6, samples->TotalCount()); |
} |
+class JsonPrefStoreLossyWriteTest : public JsonPrefStoreTest { |
+ protected: |
+ void SetUp() override { |
+ JsonPrefStoreTest::SetUp(); |
+ test_file_ = temp_dir_.path().AppendASCII("test.json"); |
+ } |
+ |
+ // Creates a JsonPrefStore with the given |file_writer|. |
+ scoped_refptr<JsonPrefStore> CreatePrefStore() { |
+ return new JsonPrefStore(test_file_, message_loop_.task_runner(), |
+ scoped_ptr<PrefFilter>()); |
+ } |
+ |
+ // Return the ImportantFileWriter for a given JsonPrefStore. |
+ ImportantFileWriter* GetImportantFileWriter( |
+ scoped_refptr<JsonPrefStore> pref_store) { |
+ return &(pref_store->writer_); |
+ } |
+ |
+ // Get the contents of kTestFile. Pumps the message loop before returning the |
+ // result. |
+ std::string GetTestFileContents() { |
+ MessageLoop::current()->RunUntilIdle(); |
+ std::string file_contents; |
+ ReadFileToString(test_file_, &file_contents); |
+ return file_contents; |
+ } |
+ |
+ private: |
+ base::FilePath test_file_; |
+}; |
+ |
+TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteBasic) { |
+ scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
+ ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); |
+ |
+ // Set a normal pref and check that it gets scheduled to be written. |
+ ASSERT_FALSE(file_writer->HasPendingWrite()); |
+ pref_store->SetValue("normal", new base::StringValue("normal"), |
+ WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
+ ASSERT_TRUE(file_writer->HasPendingWrite()); |
+ file_writer->DoScheduledWrite(); |
+ ASSERT_EQ("{\"normal\":\"normal\"}", GetTestFileContents()); |
+ ASSERT_FALSE(file_writer->HasPendingWrite()); |
+ |
+ // Set a lossy pref and check that it is not scheduled to be written. |
+ // SetValue/RemoveValue. |
+ pref_store->SetValue("lossy", new base::StringValue("lossy"), |
+ WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
+ ASSERT_FALSE(file_writer->HasPendingWrite()); |
+ pref_store->RemoveValue("lossy", WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
+ ASSERT_FALSE(file_writer->HasPendingWrite()); |
+ |
+ // SetValueSilently/RemoveValueSilently. |
+ pref_store->SetValueSilently("lossy", new base::StringValue("lossy"), |
+ WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
+ ASSERT_FALSE(file_writer->HasPendingWrite()); |
+ pref_store->RemoveValueSilently("lossy", |
+ WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
+ ASSERT_FALSE(file_writer->HasPendingWrite()); |
+ |
+ // ReportValueChanged. |
+ pref_store->SetValue("lossy", new base::StringValue("lossy"), |
+ WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
+ ASSERT_FALSE(file_writer->HasPendingWrite()); |
+ pref_store->ReportValueChanged("lossy", |
+ WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
+ ASSERT_FALSE(file_writer->HasPendingWrite()); |
+ |
+ // Call CommitPendingWrite and check that the lossy pref and the normal pref |
+ // are there with the last values set above. |
+ pref_store->CommitPendingWrite(); |
+ ASSERT_FALSE(file_writer->HasPendingWrite()); |
+ ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
+ GetTestFileContents()); |
+} |
+ |
+TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossyFirst) { |
+ scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
+ ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); |
+ |
+ // Set a lossy pref and check that it is not scheduled to be written. |
+ ASSERT_FALSE(file_writer->HasPendingWrite()); |
+ pref_store->SetValue("lossy", new base::StringValue("lossy"), |
+ WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
+ ASSERT_FALSE(file_writer->HasPendingWrite()); |
Mattias Nissler (ping if slow)
2015/05/11 07:17:10
suggestion: blank line before comment
raymes
2015/05/11 07:35:07
Done.
|
+ // Set a normal pref and check that it is scheduled to be written. |
+ pref_store->SetValue("normal", new base::StringValue("normal"), |
+ WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
+ ASSERT_TRUE(file_writer->HasPendingWrite()); |
Mattias Nissler (ping if slow)
2015/05/11 07:17:10
ditto
raymes
2015/05/11 07:35:07
Done.
|
+ // Call DoScheduledWrite and check both prefs get written. |
+ file_writer->DoScheduledWrite(); |
+ ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
+ GetTestFileContents()); |
+ ASSERT_FALSE(file_writer->HasPendingWrite()); |
+} |
+ |
+TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossySecond) { |
+ scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
+ ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); |
+ |
+ // Set a normal pref and check that it is scheduled to be written. |
+ ASSERT_FALSE(file_writer->HasPendingWrite()); |
+ pref_store->SetValue("normal", new base::StringValue("normal"), |
+ WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
+ ASSERT_TRUE(file_writer->HasPendingWrite()); |
Mattias Nissler (ping if slow)
2015/05/11 07:17:10
ditto
raymes
2015/05/11 07:35:07
Done.
|
+ // Set a lossy pref and check that the write is still scheduled. |
+ pref_store->SetValue("lossy", new base::StringValue("lossy"), |
+ WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
+ ASSERT_TRUE(file_writer->HasPendingWrite()); |
Mattias Nissler (ping if slow)
2015/05/11 07:17:10
ditto
raymes
2015/05/11 07:35:07
Done.
|
+ // Call DoScheduledWrite and check both prefs get written. |
+ file_writer->DoScheduledWrite(); |
+ ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
+ GetTestFileContents()); |
+ ASSERT_FALSE(file_writer->HasPendingWrite()); |
+} |
+ |
} // namespace base |