| 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 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 ASSERT_FALSE(pref_store->ReadOnly()); | 250 ASSERT_FALSE(pref_store->ReadOnly()); |
| 251 | 251 |
| 252 // Check values. | 252 // Check values. |
| 253 const Value* result = NULL; | 253 const Value* result = NULL; |
| 254 EXPECT_TRUE(pref_store->GetValue("list", &result)); | 254 EXPECT_TRUE(pref_store->GetValue("list", &result)); |
| 255 EXPECT_TRUE(ListValue().Equals(result)); | 255 EXPECT_TRUE(ListValue().Equals(result)); |
| 256 EXPECT_TRUE(pref_store->GetValue("dict", &result)); | 256 EXPECT_TRUE(pref_store->GetValue("dict", &result)); |
| 257 EXPECT_TRUE(DictionaryValue().Equals(result)); | 257 EXPECT_TRUE(DictionaryValue().Equals(result)); |
| 258 } | 258 } |
| 259 | 259 |
| 260 // This test is just documenting some potentially non-obvious behavior. It |
| 261 // shouldn't be taken as normative. |
| 262 TEST_F(JsonPrefStoreTest, RemoveClearsEmptyParent) { |
| 263 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); |
| 264 |
| 265 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 266 pref_file, |
| 267 message_loop_.message_loop_proxy(), |
| 268 scoped_ptr<PrefFilter>()); |
| 269 |
| 270 base::DictionaryValue* dict = new base::DictionaryValue; |
| 271 dict->SetString("key", "value"); |
| 272 pref_store->SetValue("dict", dict); |
| 273 |
| 274 pref_store->RemoveValue("dict.key"); |
| 275 |
| 276 const base::Value* retrieved_dict = NULL; |
| 277 bool has_dict = pref_store->GetValue("dict", &retrieved_dict); |
| 278 EXPECT_FALSE(has_dict); |
| 279 } |
| 280 |
| 260 // Tests asynchronous reading of the file when there is no file. | 281 // Tests asynchronous reading of the file when there is no file. |
| 261 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { | 282 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { |
| 262 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 283 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 263 ASSERT_FALSE(PathExists(bogus_input_file)); | 284 ASSERT_FALSE(PathExists(bogus_input_file)); |
| 264 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 285 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 265 bogus_input_file, | 286 bogus_input_file, |
| 266 message_loop_.message_loop_proxy().get(), | 287 message_loop_.message_loop_proxy().get(), |
| 267 scoped_ptr<PrefFilter>()); | 288 scoped_ptr<PrefFilter>()); |
| 268 MockPrefStoreObserver mock_observer; | 289 MockPrefStoreObserver mock_observer; |
| 269 pref_store->AddObserver(&mock_observer); | 290 pref_store->AddObserver(&mock_observer); |
| 270 | 291 |
| 271 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; | 292 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; |
| 272 pref_store->ReadPrefsAsync(mock_error_delegate); | 293 pref_store->ReadPrefsAsync(mock_error_delegate); |
| 273 | 294 |
| 274 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 295 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 275 EXPECT_CALL(*mock_error_delegate, | 296 EXPECT_CALL(*mock_error_delegate, |
| 276 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); | 297 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); |
| 277 RunLoop().RunUntilIdle(); | 298 RunLoop().RunUntilIdle(); |
| 278 pref_store->RemoveObserver(&mock_observer); | 299 pref_store->RemoveObserver(&mock_observer); |
| 279 | 300 |
| 280 EXPECT_FALSE(pref_store->ReadOnly()); | 301 EXPECT_FALSE(pref_store->ReadOnly()); |
| 281 } | 302 } |
| 282 | 303 |
| 283 } // namespace base | 304 } // namespace base |
| OLD | NEW |