| 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 bogus_input_file, message_loop_.message_loop_proxy().get()); | 63 bogus_input_file, message_loop_.message_loop_proxy().get()); |
| 64 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 64 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
| 65 pref_store->ReadPrefs()); | 65 pref_store->ReadPrefs()); |
| 66 EXPECT_FALSE(pref_store->ReadOnly()); | 66 EXPECT_FALSE(pref_store->ReadOnly()); |
| 67 } | 67 } |
| 68 | 68 |
| 69 // Test fallback behavior for an invalid file. | 69 // Test fallback behavior for an invalid file. |
| 70 TEST_F(JsonPrefStoreTest, InvalidFile) { | 70 TEST_F(JsonPrefStoreTest, InvalidFile) { |
| 71 base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); | 71 base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); |
| 72 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); | 72 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); |
| 73 ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file)); | 73 ASSERT_TRUE(base::CopyFile(invalid_file_original, invalid_file)); |
| 74 scoped_refptr<JsonPrefStore> pref_store = | 74 scoped_refptr<JsonPrefStore> pref_store = |
| 75 new JsonPrefStore(invalid_file, message_loop_.message_loop_proxy().get()); | 75 new JsonPrefStore(invalid_file, message_loop_.message_loop_proxy().get()); |
| 76 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, | 76 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, |
| 77 pref_store->ReadPrefs()); | 77 pref_store->ReadPrefs()); |
| 78 EXPECT_FALSE(pref_store->ReadOnly()); | 78 EXPECT_FALSE(pref_store->ReadOnly()); |
| 79 | 79 |
| 80 // The file should have been moved aside. | 80 // The file should have been moved aside. |
| 81 EXPECT_FALSE(file_util::PathExists(invalid_file)); | 81 EXPECT_FALSE(file_util::PathExists(invalid_file)); |
| 82 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); | 82 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); |
| 83 EXPECT_TRUE(file_util::PathExists(moved_aside)); | 83 EXPECT_TRUE(file_util::PathExists(moved_aside)); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 | 145 |
| 146 // Serialize and compare to expected output. | 146 // Serialize and compare to expected output. |
| 147 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | 147 ASSERT_TRUE(file_util::PathExists(golden_output_file)); |
| 148 pref_store->CommitPendingWrite(); | 148 pref_store->CommitPendingWrite(); |
| 149 RunLoop().RunUntilIdle(); | 149 RunLoop().RunUntilIdle(); |
| 150 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); | 150 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); |
| 151 ASSERT_TRUE(base::Delete(output_file, false)); | 151 ASSERT_TRUE(base::Delete(output_file, false)); |
| 152 } | 152 } |
| 153 | 153 |
| 154 TEST_F(JsonPrefStoreTest, Basic) { | 154 TEST_F(JsonPrefStoreTest, Basic) { |
| 155 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), | 155 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 156 temp_dir_.path().AppendASCII("write.json"))); | 156 temp_dir_.path().AppendASCII("write.json"))); |
| 157 | 157 |
| 158 // Test that the persistent value can be loaded. | 158 // Test that the persistent value can be loaded. |
| 159 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 159 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 160 ASSERT_TRUE(file_util::PathExists(input_file)); | 160 ASSERT_TRUE(file_util::PathExists(input_file)); |
| 161 scoped_refptr<JsonPrefStore> pref_store = | 161 scoped_refptr<JsonPrefStore> pref_store = |
| 162 new JsonPrefStore(input_file, message_loop_.message_loop_proxy().get()); | 162 new JsonPrefStore(input_file, message_loop_.message_loop_proxy().get()); |
| 163 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 163 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 164 ASSERT_FALSE(pref_store->ReadOnly()); | 164 ASSERT_FALSE(pref_store->ReadOnly()); |
| 165 | 165 |
| 166 // The JSON file looks like this: | 166 // The JSON file looks like this: |
| 167 // { | 167 // { |
| 168 // "homepage": "http://www.cnn.com", | 168 // "homepage": "http://www.cnn.com", |
| 169 // "some_directory": "/usr/local/", | 169 // "some_directory": "/usr/local/", |
| 170 // "tabs": { | 170 // "tabs": { |
| 171 // "new_windows_in_tabs": true, | 171 // "new_windows_in_tabs": true, |
| 172 // "max_tabs": 20 | 172 // "max_tabs": 20 |
| 173 // } | 173 // } |
| 174 // } | 174 // } |
| 175 | 175 |
| 176 RunBasicJsonPrefStoreTest( | 176 RunBasicJsonPrefStoreTest( |
| 177 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 177 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
| 178 } | 178 } |
| 179 | 179 |
| 180 TEST_F(JsonPrefStoreTest, BasicAsync) { | 180 TEST_F(JsonPrefStoreTest, BasicAsync) { |
| 181 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), | 181 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 182 temp_dir_.path().AppendASCII("write.json"))); | 182 temp_dir_.path().AppendASCII("write.json"))); |
| 183 | 183 |
| 184 // Test that the persistent value can be loaded. | 184 // Test that the persistent value can be loaded. |
| 185 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 185 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 186 ASSERT_TRUE(file_util::PathExists(input_file)); | 186 ASSERT_TRUE(file_util::PathExists(input_file)); |
| 187 scoped_refptr<JsonPrefStore> pref_store = | 187 scoped_refptr<JsonPrefStore> pref_store = |
| 188 new JsonPrefStore(input_file, message_loop_.message_loop_proxy().get()); | 188 new JsonPrefStore(input_file, message_loop_.message_loop_proxy().get()); |
| 189 | 189 |
| 190 { | 190 { |
| 191 MockPrefStoreObserver mock_observer; | 191 MockPrefStoreObserver mock_observer; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); | 234 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); |
| 235 RunLoop().RunUntilIdle(); | 235 RunLoop().RunUntilIdle(); |
| 236 pref_store->RemoveObserver(&mock_observer); | 236 pref_store->RemoveObserver(&mock_observer); |
| 237 | 237 |
| 238 EXPECT_FALSE(pref_store->ReadOnly()); | 238 EXPECT_FALSE(pref_store->ReadOnly()); |
| 239 } | 239 } |
| 240 | 240 |
| 241 TEST_F(JsonPrefStoreTest, NeedsEmptyValue) { | 241 TEST_F(JsonPrefStoreTest, NeedsEmptyValue) { |
| 242 base::FilePath pref_file = temp_dir_.path().AppendASCII("write.json"); | 242 base::FilePath pref_file = temp_dir_.path().AppendASCII("write.json"); |
| 243 | 243 |
| 244 ASSERT_TRUE(file_util::CopyFile( | 244 ASSERT_TRUE(base::CopyFile( |
| 245 data_dir_.AppendASCII("read.need_empty_value.json"), | 245 data_dir_.AppendASCII("read.need_empty_value.json"), |
| 246 pref_file)); | 246 pref_file)); |
| 247 | 247 |
| 248 // Test that the persistent value can be loaded. | 248 // Test that the persistent value can be loaded. |
| 249 ASSERT_TRUE(file_util::PathExists(pref_file)); | 249 ASSERT_TRUE(file_util::PathExists(pref_file)); |
| 250 scoped_refptr<JsonPrefStore> pref_store = | 250 scoped_refptr<JsonPrefStore> pref_store = |
| 251 new JsonPrefStore(pref_file, message_loop_.message_loop_proxy().get()); | 251 new JsonPrefStore(pref_file, message_loop_.message_loop_proxy().get()); |
| 252 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 252 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 253 ASSERT_FALSE(pref_store->ReadOnly()); | 253 ASSERT_FALSE(pref_store->ReadOnly()); |
| 254 | 254 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 279 RunLoop().RunUntilIdle(); | 279 RunLoop().RunUntilIdle(); |
| 280 | 280 |
| 281 // Compare to expected output. | 281 // Compare to expected output. |
| 282 base::FilePath golden_output_file = | 282 base::FilePath golden_output_file = |
| 283 data_dir_.AppendASCII("write.golden.need_empty_value.json"); | 283 data_dir_.AppendASCII("write.golden.need_empty_value.json"); |
| 284 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | 284 ASSERT_TRUE(file_util::PathExists(golden_output_file)); |
| 285 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); | 285 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); |
| 286 } | 286 } |
| 287 | 287 |
| 288 } // namespace base | 288 } // namespace base |
| OLD | NEW |