| 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/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 | 242 |
| 243 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 243 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 244 EXPECT_CALL(*mock_error_delegate, | 244 EXPECT_CALL(*mock_error_delegate, |
| 245 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); | 245 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); |
| 246 message_loop_.RunUntilIdle(); | 246 message_loop_.RunUntilIdle(); |
| 247 pref_store->RemoveObserver(&mock_observer); | 247 pref_store->RemoveObserver(&mock_observer); |
| 248 | 248 |
| 249 EXPECT_FALSE(pref_store->ReadOnly()); | 249 EXPECT_FALSE(pref_store->ReadOnly()); |
| 250 } | 250 } |
| 251 | 251 |
| 252 TEST_F(JsonPrefStoreTest, NeedsEmptyValue) { | 252 TEST_F(JsonPrefStoreTest, PreserveEmptyValues) { |
| 253 FilePath pref_file = temp_dir_.path().AppendASCII("write.json"); | 253 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); |
| 254 | |
| 255 ASSERT_TRUE(file_util::CopyFile( | |
| 256 data_dir_.AppendASCII("read.need_empty_value.json"), | |
| 257 pref_file)); | |
| 258 | 254 |
| 259 // Test that the persistent value can be loaded. | 255 // Test that the persistent value can be loaded. |
| 260 ASSERT_TRUE(file_util::PathExists(pref_file)); | |
| 261 scoped_refptr<JsonPrefStore> pref_store = | 256 scoped_refptr<JsonPrefStore> pref_store = |
| 262 new JsonPrefStore( | 257 new JsonPrefStore(pref_file, message_loop_.message_loop_proxy()); |
| 263 pref_file, message_loop_.message_loop_proxy()); | |
| 264 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | |
| 265 ASSERT_FALSE(pref_store->ReadOnly()); | |
| 266 | 258 |
| 267 // The JSON file looks like this: | 259 // Set some keys with empty values. |
| 268 // { | |
| 269 // "list": [ 1 ], | |
| 270 // "list_needs_empty_value": [ 2 ], | |
| 271 // "dict": { | |
| 272 // "dummy": true, | |
| 273 // }, | |
| 274 // "dict_needs_empty_value": { | |
| 275 // "dummy": true, | |
| 276 // }, | |
| 277 // } | |
| 278 | |
| 279 // Set flag to preserve empty values for the following keys. | |
| 280 pref_store->MarkNeedsEmptyValue("list_needs_empty_value"); | |
| 281 pref_store->MarkNeedsEmptyValue("dict_needs_empty_value"); | |
| 282 | |
| 283 // Set all keys to empty values. | |
| 284 pref_store->SetValue("list", new base::ListValue); | 260 pref_store->SetValue("list", new base::ListValue); |
| 285 pref_store->SetValue("list_needs_empty_value", new base::ListValue); | |
| 286 pref_store->SetValue("dict", new base::DictionaryValue); | 261 pref_store->SetValue("dict", new base::DictionaryValue); |
| 287 pref_store->SetValue("dict_needs_empty_value", new base::DictionaryValue); | |
| 288 | 262 |
| 289 // Write to file. | 263 // Write to file. |
| 290 pref_store->CommitPendingWrite(); | 264 pref_store->CommitPendingWrite(); |
| 291 MessageLoop::current()->RunUntilIdle(); | 265 MessageLoop::current()->RunUntilIdle(); |
| 292 | 266 |
| 293 // Compare to expected output. | 267 // Reload. |
| 294 FilePath golden_output_file = | 268 pref_store = new JsonPrefStore(pref_file, message_loop_.message_loop_proxy()); |
| 295 data_dir_.AppendASCII("write.golden.need_empty_value.json"); | 269 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 296 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | 270 ASSERT_FALSE(pref_store->ReadOnly()); |
| 297 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); | 271 |
| 272 // Check values. |
| 273 const Value* result = NULL; |
| 274 EXPECT_TRUE(pref_store->GetValue("list", &result)); |
| 275 ListValue().Equals(result); |
| 276 EXPECT_TRUE(pref_store->GetValue("dict", &result)); |
| 277 DictionaryValue().Equals(result); |
| 298 } | 278 } |
| 299 | 279 |
| 300 } // namespace base | 280 } // namespace base |
| OLD | NEW |