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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 // The path to the directory where the test data is stored. | 52 // The path to the directory where the test data is stored. |
53 base::FilePath data_dir_; | 53 base::FilePath data_dir_; |
54 // A message loop that we can use as the file thread message loop. | 54 // A message loop that we can use as the file thread message loop. |
55 MessageLoop message_loop_; | 55 MessageLoop message_loop_; |
56 }; | 56 }; |
57 | 57 |
58 // Test fallback behavior for a nonexistent file. | 58 // Test fallback behavior for a nonexistent file. |
59 TEST_F(JsonPrefStoreTest, NonExistentFile) { | 59 TEST_F(JsonPrefStoreTest, NonExistentFile) { |
60 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 60 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
61 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); | 61 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); |
62 scoped_refptr<JsonPrefStore> pref_store = | 62 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
63 new JsonPrefStore( | 63 bogus_input_file, message_loop_.message_loop_proxy().get()); |
64 bogus_input_file, message_loop_.message_loop_proxy()); | |
65 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 64 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
66 pref_store->ReadPrefs()); | 65 pref_store->ReadPrefs()); |
67 EXPECT_FALSE(pref_store->ReadOnly()); | 66 EXPECT_FALSE(pref_store->ReadOnly()); |
68 } | 67 } |
69 | 68 |
70 // Test fallback behavior for an invalid file. | 69 // Test fallback behavior for an invalid file. |
71 TEST_F(JsonPrefStoreTest, InvalidFile) { | 70 TEST_F(JsonPrefStoreTest, InvalidFile) { |
72 base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); | 71 base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); |
73 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); | 72 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); |
74 ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file)); | 73 ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file)); |
75 scoped_refptr<JsonPrefStore> pref_store = | 74 scoped_refptr<JsonPrefStore> pref_store = |
76 new JsonPrefStore( | 75 new JsonPrefStore(invalid_file, message_loop_.message_loop_proxy().get()); |
77 invalid_file, message_loop_.message_loop_proxy()); | |
78 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, | 76 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, |
79 pref_store->ReadPrefs()); | 77 pref_store->ReadPrefs()); |
80 EXPECT_FALSE(pref_store->ReadOnly()); | 78 EXPECT_FALSE(pref_store->ReadOnly()); |
81 | 79 |
82 // The file should have been moved aside. | 80 // The file should have been moved aside. |
83 EXPECT_FALSE(file_util::PathExists(invalid_file)); | 81 EXPECT_FALSE(file_util::PathExists(invalid_file)); |
84 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); | 82 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); |
85 EXPECT_TRUE(file_util::PathExists(moved_aside)); | 83 EXPECT_TRUE(file_util::PathExists(moved_aside)); |
86 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original, | 84 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original, |
87 moved_aside)); | 85 moved_aside)); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 } | 152 } |
155 | 153 |
156 TEST_F(JsonPrefStoreTest, Basic) { | 154 TEST_F(JsonPrefStoreTest, Basic) { |
157 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), | 155 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), |
158 temp_dir_.path().AppendASCII("write.json"))); | 156 temp_dir_.path().AppendASCII("write.json"))); |
159 | 157 |
160 // Test that the persistent value can be loaded. | 158 // Test that the persistent value can be loaded. |
161 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 159 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
162 ASSERT_TRUE(file_util::PathExists(input_file)); | 160 ASSERT_TRUE(file_util::PathExists(input_file)); |
163 scoped_refptr<JsonPrefStore> pref_store = | 161 scoped_refptr<JsonPrefStore> pref_store = |
164 new JsonPrefStore( | 162 new JsonPrefStore(input_file, message_loop_.message_loop_proxy().get()); |
165 input_file, message_loop_.message_loop_proxy()); | |
166 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 163 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
167 ASSERT_FALSE(pref_store->ReadOnly()); | 164 ASSERT_FALSE(pref_store->ReadOnly()); |
168 | 165 |
169 // The JSON file looks like this: | 166 // The JSON file looks like this: |
170 // { | 167 // { |
171 // "homepage": "http://www.cnn.com", | 168 // "homepage": "http://www.cnn.com", |
172 // "some_directory": "/usr/local/", | 169 // "some_directory": "/usr/local/", |
173 // "tabs": { | 170 // "tabs": { |
174 // "new_windows_in_tabs": true, | 171 // "new_windows_in_tabs": true, |
175 // "max_tabs": 20 | 172 // "max_tabs": 20 |
176 // } | 173 // } |
177 // } | 174 // } |
178 | 175 |
179 RunBasicJsonPrefStoreTest( | 176 RunBasicJsonPrefStoreTest( |
180 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 177 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
181 } | 178 } |
182 | 179 |
183 TEST_F(JsonPrefStoreTest, BasicAsync) { | 180 TEST_F(JsonPrefStoreTest, BasicAsync) { |
184 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), | 181 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), |
185 temp_dir_.path().AppendASCII("write.json"))); | 182 temp_dir_.path().AppendASCII("write.json"))); |
186 | 183 |
187 // Test that the persistent value can be loaded. | 184 // Test that the persistent value can be loaded. |
188 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 185 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
189 ASSERT_TRUE(file_util::PathExists(input_file)); | 186 ASSERT_TRUE(file_util::PathExists(input_file)); |
190 scoped_refptr<JsonPrefStore> pref_store = | 187 scoped_refptr<JsonPrefStore> pref_store = |
191 new JsonPrefStore( | 188 new JsonPrefStore(input_file, message_loop_.message_loop_proxy().get()); |
192 input_file, message_loop_.message_loop_proxy()); | |
193 | 189 |
194 { | 190 { |
195 MockPrefStoreObserver mock_observer; | 191 MockPrefStoreObserver mock_observer; |
196 pref_store->AddObserver(&mock_observer); | 192 pref_store->AddObserver(&mock_observer); |
197 | 193 |
198 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | 194 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
199 pref_store->ReadPrefsAsync(mock_error_delegate); | 195 pref_store->ReadPrefsAsync(mock_error_delegate); |
200 | 196 |
201 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 197 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
202 EXPECT_CALL(*mock_error_delegate, | 198 EXPECT_CALL(*mock_error_delegate, |
(...skipping 15 matching lines...) Expand all Loading... |
218 // } | 214 // } |
219 | 215 |
220 RunBasicJsonPrefStoreTest( | 216 RunBasicJsonPrefStoreTest( |
221 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 217 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
222 } | 218 } |
223 | 219 |
224 // Tests asynchronous reading of the file when there is no file. | 220 // Tests asynchronous reading of the file when there is no file. |
225 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { | 221 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { |
226 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 222 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
227 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); | 223 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); |
228 scoped_refptr<JsonPrefStore> pref_store = | 224 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
229 new JsonPrefStore( | 225 bogus_input_file, message_loop_.message_loop_proxy().get()); |
230 bogus_input_file, message_loop_.message_loop_proxy()); | |
231 MockPrefStoreObserver mock_observer; | 226 MockPrefStoreObserver mock_observer; |
232 pref_store->AddObserver(&mock_observer); | 227 pref_store->AddObserver(&mock_observer); |
233 | 228 |
234 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; | 229 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; |
235 pref_store->ReadPrefsAsync(mock_error_delegate); | 230 pref_store->ReadPrefsAsync(mock_error_delegate); |
236 | 231 |
237 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 232 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
238 EXPECT_CALL(*mock_error_delegate, | 233 EXPECT_CALL(*mock_error_delegate, |
239 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); | 234 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); |
240 RunLoop().RunUntilIdle(); | 235 RunLoop().RunUntilIdle(); |
241 pref_store->RemoveObserver(&mock_observer); | 236 pref_store->RemoveObserver(&mock_observer); |
242 | 237 |
243 EXPECT_FALSE(pref_store->ReadOnly()); | 238 EXPECT_FALSE(pref_store->ReadOnly()); |
244 } | 239 } |
245 | 240 |
246 TEST_F(JsonPrefStoreTest, NeedsEmptyValue) { | 241 TEST_F(JsonPrefStoreTest, NeedsEmptyValue) { |
247 base::FilePath pref_file = temp_dir_.path().AppendASCII("write.json"); | 242 base::FilePath pref_file = temp_dir_.path().AppendASCII("write.json"); |
248 | 243 |
249 ASSERT_TRUE(file_util::CopyFile( | 244 ASSERT_TRUE(file_util::CopyFile( |
250 data_dir_.AppendASCII("read.need_empty_value.json"), | 245 data_dir_.AppendASCII("read.need_empty_value.json"), |
251 pref_file)); | 246 pref_file)); |
252 | 247 |
253 // Test that the persistent value can be loaded. | 248 // Test that the persistent value can be loaded. |
254 ASSERT_TRUE(file_util::PathExists(pref_file)); | 249 ASSERT_TRUE(file_util::PathExists(pref_file)); |
255 scoped_refptr<JsonPrefStore> pref_store = | 250 scoped_refptr<JsonPrefStore> pref_store = |
256 new JsonPrefStore( | 251 new JsonPrefStore(pref_file, message_loop_.message_loop_proxy().get()); |
257 pref_file, message_loop_.message_loop_proxy()); | |
258 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 252 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
259 ASSERT_FALSE(pref_store->ReadOnly()); | 253 ASSERT_FALSE(pref_store->ReadOnly()); |
260 | 254 |
261 // The JSON file looks like this: | 255 // The JSON file looks like this: |
262 // { | 256 // { |
263 // "list": [ 1 ], | 257 // "list": [ 1 ], |
264 // "list_needs_empty_value": [ 2 ], | 258 // "list_needs_empty_value": [ 2 ], |
265 // "dict": { | 259 // "dict": { |
266 // "dummy": true, | 260 // "dummy": true, |
267 // }, | 261 // }, |
(...skipping 17 matching lines...) Expand all Loading... |
285 RunLoop().RunUntilIdle(); | 279 RunLoop().RunUntilIdle(); |
286 | 280 |
287 // Compare to expected output. | 281 // Compare to expected output. |
288 base::FilePath golden_output_file = | 282 base::FilePath golden_output_file = |
289 data_dir_.AppendASCII("write.golden.need_empty_value.json"); | 283 data_dir_.AppendASCII("write.golden.need_empty_value.json"); |
290 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | 284 ASSERT_TRUE(file_util::PathExists(golden_output_file)); |
291 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); | 285 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); |
292 } | 286 } |
293 | 287 |
294 } // namespace base | 288 } // namespace base |
OLD | NEW |