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 // Test that the persistent value can be loaded. | |
Mattias Nissler (ping if slow)
2013/11/22 08:03:38
I think this comment is stale.
gab
2013/11/22 23:24:21
Removed.
| |
223 scoped_refptr<JsonPrefStore> pref_store = | |
224 new JsonPrefStore(pref_file, message_loop_.message_loop_proxy()); | |
Mattias Nissler (ping if slow)
2013/11/22 08:03:38
Looks like there's a merge problem here, the origi
gab
2013/11/22 23:24:21
Oops, merge problem indeed, fixed to be as in http
| |
225 // Reload. | |
226 pref_store = new JsonPrefStore(pref_file, message_loop_.message_loop_proxy()); | |
227 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | |
228 ASSERT_FALSE(pref_store->ReadOnly()); | |
229 | |
230 // Check values. | |
231 const Value* result = NULL; | |
232 EXPECT_TRUE(pref_store->GetValue("list", &result)); | |
233 ListValue().Equals(result); | |
234 EXPECT_TRUE(pref_store->GetValue("dict", &result)); | |
235 DictionaryValue().Equals(result); | |
236 } | |
237 | |
219 // Tests asynchronous reading of the file when there is no file. | 238 // Tests asynchronous reading of the file when there is no file. |
220 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { | 239 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { |
221 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 240 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
222 ASSERT_FALSE(PathExists(bogus_input_file)); | 241 ASSERT_FALSE(PathExists(bogus_input_file)); |
223 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 242 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
224 bogus_input_file, message_loop_.message_loop_proxy().get()); | 243 bogus_input_file, message_loop_.message_loop_proxy().get()); |
225 MockPrefStoreObserver mock_observer; | 244 MockPrefStoreObserver mock_observer; |
226 pref_store->AddObserver(&mock_observer); | 245 pref_store->AddObserver(&mock_observer); |
227 | 246 |
228 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; | 247 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; |
229 pref_store->ReadPrefsAsync(mock_error_delegate); | 248 pref_store->ReadPrefsAsync(mock_error_delegate); |
230 | 249 |
231 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 250 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
232 EXPECT_CALL(*mock_error_delegate, | 251 EXPECT_CALL(*mock_error_delegate, |
233 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); | 252 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); |
234 RunLoop().RunUntilIdle(); | 253 RunLoop().RunUntilIdle(); |
235 pref_store->RemoveObserver(&mock_observer); | 254 pref_store->RemoveObserver(&mock_observer); |
236 | 255 |
237 EXPECT_FALSE(pref_store->ReadOnly()); | 256 EXPECT_FALSE(pref_store->ReadOnly()); |
238 } | 257 } |
239 | 258 |
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 | 259 } // namespace base |
OLD | NEW |