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 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 // "tabs": { | 209 // "tabs": { |
210 // "new_windows_in_tabs": true, | 210 // "new_windows_in_tabs": true, |
211 // "max_tabs": 20 | 211 // "max_tabs": 20 |
212 // } | 212 // } |
213 // } | 213 // } |
214 | 214 |
215 RunBasicJsonPrefStoreTest( | 215 RunBasicJsonPrefStoreTest( |
216 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 216 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
217 } | 217 } |
218 | 218 |
| 219 TEST_F(JsonPrefStoreTest, PreserveEmptyValues) { |
| 220 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); |
| 221 |
| 222 scoped_refptr<JsonPrefStore> pref_store = |
| 223 new JsonPrefStore(pref_file, message_loop_.message_loop_proxy()); |
| 224 |
| 225 // Set some keys with empty values. |
| 226 pref_store->SetValue("list", new base::ListValue); |
| 227 pref_store->SetValue("dict", new base::DictionaryValue); |
| 228 |
| 229 // Write to file. |
| 230 pref_store->CommitPendingWrite(); |
| 231 MessageLoop::current()->RunUntilIdle(); |
| 232 |
| 233 // Reload. |
| 234 pref_store = new JsonPrefStore(pref_file, message_loop_.message_loop_proxy()); |
| 235 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 236 ASSERT_FALSE(pref_store->ReadOnly()); |
| 237 |
| 238 // Check values. |
| 239 const Value* result = NULL; |
| 240 EXPECT_TRUE(pref_store->GetValue("list", &result)); |
| 241 EXPECT_TRUE(ListValue().Equals(result)); |
| 242 EXPECT_TRUE(pref_store->GetValue("dict", &result)); |
| 243 EXPECT_TRUE(DictionaryValue().Equals(result)); |
| 244 } |
| 245 |
219 // Tests asynchronous reading of the file when there is no file. | 246 // Tests asynchronous reading of the file when there is no file. |
220 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { | 247 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { |
221 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 248 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
222 ASSERT_FALSE(PathExists(bogus_input_file)); | 249 ASSERT_FALSE(PathExists(bogus_input_file)); |
223 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 250 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
224 bogus_input_file, message_loop_.message_loop_proxy().get()); | 251 bogus_input_file, message_loop_.message_loop_proxy().get()); |
225 MockPrefStoreObserver mock_observer; | 252 MockPrefStoreObserver mock_observer; |
226 pref_store->AddObserver(&mock_observer); | 253 pref_store->AddObserver(&mock_observer); |
227 | 254 |
228 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; | 255 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; |
229 pref_store->ReadPrefsAsync(mock_error_delegate); | 256 pref_store->ReadPrefsAsync(mock_error_delegate); |
230 | 257 |
231 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 258 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
232 EXPECT_CALL(*mock_error_delegate, | 259 EXPECT_CALL(*mock_error_delegate, |
233 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); | 260 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); |
234 RunLoop().RunUntilIdle(); | 261 RunLoop().RunUntilIdle(); |
235 pref_store->RemoveObserver(&mock_observer); | 262 pref_store->RemoveObserver(&mock_observer); |
236 | 263 |
237 EXPECT_FALSE(pref_store->ReadOnly()); | 264 EXPECT_FALSE(pref_store->ReadOnly()); |
238 } | 265 } |
239 | 266 |
240 TEST_F(JsonPrefStoreTest, NeedsEmptyValue) { | |
241 base::FilePath pref_file = temp_dir_.path().AppendASCII("write.json"); | |
242 | |
243 ASSERT_TRUE(base::CopyFile( | |
244 data_dir_.AppendASCII("read.need_empty_value.json"), | |
245 pref_file)); | |
246 | |
247 // Test that the persistent value can be loaded. | |
248 ASSERT_TRUE(PathExists(pref_file)); | |
249 scoped_refptr<JsonPrefStore> pref_store = | |
250 new JsonPrefStore(pref_file, message_loop_.message_loop_proxy().get()); | |
251 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | |
252 ASSERT_FALSE(pref_store->ReadOnly()); | |
253 | |
254 // The JSON file looks like this: | |
255 // { | |
256 // "list": [ 1 ], | |
257 // "list_needs_empty_value": [ 2 ], | |
258 // "dict": { | |
259 // "dummy": true, | |
260 // }, | |
261 // "dict_needs_empty_value": { | |
262 // "dummy": true, | |
263 // }, | |
264 // } | |
265 | |
266 // Set flag to preserve empty values for the following keys. | |
267 pref_store->MarkNeedsEmptyValue("list_needs_empty_value"); | |
268 pref_store->MarkNeedsEmptyValue("dict_needs_empty_value"); | |
269 | |
270 // Set all keys to empty values. | |
271 pref_store->SetValue("list", new base::ListValue); | |
272 pref_store->SetValue("list_needs_empty_value", new base::ListValue); | |
273 pref_store->SetValue("dict", new base::DictionaryValue); | |
274 pref_store->SetValue("dict_needs_empty_value", new base::DictionaryValue); | |
275 | |
276 // Write to file. | |
277 pref_store->CommitPendingWrite(); | |
278 RunLoop().RunUntilIdle(); | |
279 | |
280 // Compare to expected output. | |
281 base::FilePath golden_output_file = | |
282 data_dir_.AppendASCII("write.golden.need_empty_value.json"); | |
283 ASSERT_TRUE(PathExists(golden_output_file)); | |
284 EXPECT_TRUE(TextContentsEqual(golden_output_file, pref_file)); | |
285 } | |
286 | |
287 } // namespace base | 267 } // namespace base |
OLD | NEW |