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

Unified Diff: chrome/common/json_pref_store_unittest.cc

Issue 10055003: Keep emtpy List/Dictionary pref value with non-empty default. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: chrome/common/json_pref_store_unittest.cc
diff --git a/chrome/common/json_pref_store_unittest.cc b/chrome/common/json_pref_store_unittest.cc
index 05d24939811231b88c7bd1ce007ca22f6f6b39be..73b15685adc31702f62a6256ac5e94f9fd9b73b2 100644
--- a/chrome/common/json_pref_store_unittest.cc
+++ b/chrome/common/json_pref_store_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -244,3 +244,51 @@ TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) {
EXPECT_FALSE(pref_store->ReadOnly());
}
+
+TEST_F(JsonPrefStoreTest, NeedsEmptyValue) {
+ FilePath pref_file = temp_dir_.path().AppendASCII("write.json");
+
+ ASSERT_TRUE(file_util::CopyFile(
+ data_dir_.AppendASCII("read.need_empty_value.json"),
+ pref_file));
battre 2012/04/11 21:55:51 nit: indentation
xiyuan 2012/04/11 22:36:46 Done.
+
+ // Test that the persistent value can be loaded.
+ ASSERT_TRUE(file_util::PathExists(pref_file));
+ scoped_refptr<JsonPrefStore> pref_store =
+ new JsonPrefStore(pref_file, message_loop_proxy_.get());
+ ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
+ ASSERT_FALSE(pref_store->ReadOnly());
+
+ // The JSON file looks like this:
+ // {
+ // "list": [ 1 ],
+ // "list_needs_empty_value": [ 2 ],
+ // "dict": {
+ // "dummy": true,
+ // },
+ // "dict_needs_empty_value": {
+ // "dummy": true,
+ // },
+ // }
+
+ // Set flag to preserve empty values for the following keys.
+ pref_store->MarkNeedsEmptyValue("list_needs_empty_value");
+ pref_store->MarkNeedsEmptyValue("dict_needs_empty_value");
+
+ // Set all keys to empty values.
+ pref_store->SetValue("list", new base::ListValue);
+ pref_store->SetValue("list_needs_empty_value", new base::ListValue);
+ pref_store->SetValue("dict", new base::DictionaryValue);
+ pref_store->SetValue("dict_needs_empty_value", new base::DictionaryValue);
+
+ // Write to file.
+ pref_store->CommitPendingWrite();
+ MessageLoop::current()->RunAllPending();
+
+ // Compare to expected output.
+ FilePath golden_output_file =
+ data_dir_.AppendASCII("write.golden.need_empty_value.json");
+ ASSERT_TRUE(file_util::PathExists(golden_output_file));
+ EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file));
+ ASSERT_TRUE(file_util::Delete(pref_file, false));
+}

Powered by Google App Engine
This is Rietveld 408576698