| 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 728a57d3417fc4e5893c4226939f12ba1b5cc7e8..67a8adbb4d4f93bca61d964ace97353736345f3f 100644
|
| --- a/base/prefs/json_pref_store_unittest.cc
|
| +++ b/base/prefs/json_pref_store_unittest.cc
|
| @@ -108,9 +108,7 @@ class JsonPrefStoreTest : public testing::Test {
|
| void TearDown() override {
|
| // Make sure all pending tasks have been processed (e.g., deleting the
|
| // JsonPrefStore may post write tasks).
|
| - message_loop_.task_runner()->PostTask(FROM_HERE,
|
| - MessageLoop::QuitWhenIdleClosure());
|
| - message_loop_.Run();
|
| + RunLoop().RunUntilIdle();
|
| }
|
|
|
| // The path to temporary directory used to contain the test operations.
|
| @@ -127,7 +125,7 @@ class JsonPrefStoreTest : public testing::Test {
|
|
|
| // Test fallback behavior for a nonexistent file.
|
| TEST_F(JsonPrefStoreTest, NonExistentFile) {
|
| - base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
|
| + base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt");
|
| ASSERT_FALSE(PathExists(bogus_input_file));
|
| scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore(
|
| bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>());
|
| @@ -138,9 +136,9 @@ TEST_F(JsonPrefStoreTest, NonExistentFile) {
|
|
|
| // Test fallback behavior for a nonexistent file and alternate file.
|
| TEST_F(JsonPrefStoreTest, NonExistentFileAndAlternateFile) {
|
| - base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
|
| + base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt");
|
| base::FilePath bogus_alternate_input_file =
|
| - data_dir_.AppendASCII("read_alternate.txt");
|
| + temp_dir_.path().AppendASCII("read_alternate.txt");
|
| ASSERT_FALSE(PathExists(bogus_input_file));
|
| ASSERT_FALSE(PathExists(bogus_alternate_input_file));
|
| scoped_refptr<JsonPrefStore> pref_store =
|
| @@ -194,7 +192,8 @@ void RunBasicJsonPrefStoreTest(JsonPrefStore* pref_store,
|
| EXPECT_EQ(base::FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path);
|
| base::FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/"));
|
|
|
| - pref_store->SetValue(kSomeDirectory, new StringValue(some_path.value()));
|
| + pref_store->SetValue(kSomeDirectory, new StringValue(some_path.value()),
|
| + WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
|
| EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual));
|
| EXPECT_TRUE(actual->GetAsString(&path));
|
| EXPECT_EQ(some_path.value(), path);
|
| @@ -205,7 +204,8 @@ void RunBasicJsonPrefStoreTest(JsonPrefStore* pref_store,
|
| EXPECT_TRUE(actual->GetAsBoolean(&boolean));
|
| EXPECT_TRUE(boolean);
|
|
|
| - pref_store->SetValue(kNewWindowsInTabs, new FundamentalValue(false));
|
| + pref_store->SetValue(kNewWindowsInTabs, new FundamentalValue(false),
|
| + WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
|
| EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual));
|
| EXPECT_TRUE(actual->GetAsBoolean(&boolean));
|
| EXPECT_FALSE(boolean);
|
| @@ -214,13 +214,15 @@ void RunBasicJsonPrefStoreTest(JsonPrefStore* pref_store,
|
| int integer = 0;
|
| EXPECT_TRUE(actual->GetAsInteger(&integer));
|
| EXPECT_EQ(20, integer);
|
| - pref_store->SetValue(kMaxTabs, new FundamentalValue(10));
|
| + pref_store->SetValue(kMaxTabs, new FundamentalValue(10),
|
| + WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
|
| EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual));
|
| EXPECT_TRUE(actual->GetAsInteger(&integer));
|
| EXPECT_EQ(10, integer);
|
|
|
| pref_store->SetValue(kLongIntPref,
|
| - new StringValue(base::Int64ToString(214748364842LL)));
|
| + new StringValue(base::Int64ToString(214748364842LL)),
|
| + WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
|
| EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual));
|
| EXPECT_TRUE(actual->GetAsString(&string_value));
|
| int64 value;
|
| @@ -310,12 +312,14 @@ TEST_F(JsonPrefStoreTest, PreserveEmptyValues) {
|
| pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>());
|
|
|
| // Set some keys with empty values.
|
| - pref_store->SetValue("list", new base::ListValue);
|
| - pref_store->SetValue("dict", new base::DictionaryValue);
|
| + pref_store->SetValue("list", new base::ListValue,
|
| + WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
|
| + pref_store->SetValue("dict", new base::DictionaryValue,
|
| + WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
|
|
|
| // Write to file.
|
| pref_store->CommitPendingWrite();
|
| - MessageLoop::current()->RunUntilIdle();
|
| + RunLoop().RunUntilIdle();
|
|
|
| // Reload.
|
| pref_store = new JsonPrefStore(pref_file, message_loop_.task_runner(),
|
| @@ -341,9 +345,11 @@ TEST_F(JsonPrefStoreTest, RemoveClearsEmptyParent) {
|
|
|
| base::DictionaryValue* dict = new base::DictionaryValue;
|
| dict->SetString("key", "value");
|
| - pref_store->SetValue("dict", dict);
|
| + pref_store->SetValue("dict", dict,
|
| + WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
|
|
|
| - pref_store->RemoveValue("dict.key");
|
| + pref_store->RemoveValue("dict.key",
|
| + WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
|
|
|
| const base::Value* retrieved_dict = NULL;
|
| bool has_dict = pref_store->GetValue("dict", &retrieved_dict);
|
| @@ -352,7 +358,7 @@ TEST_F(JsonPrefStoreTest, RemoveClearsEmptyParent) {
|
|
|
| // Tests asynchronous reading of the file when there is no file.
|
| TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) {
|
| - base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
|
| + base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt");
|
| ASSERT_FALSE(PathExists(bogus_input_file));
|
| scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore(
|
| bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>());
|
| @@ -801,4 +807,125 @@ 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() {
|
| + RunLoop().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());
|
| +
|
| + // 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());
|
| +
|
| + // 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());
|
| +
|
| + // 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());
|
| +
|
| + // 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
|
|
|