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/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/location.h" | |
11 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
14 #include "base/metrics/histogram_samples.h" | 13 #include "base/metrics/histogram_samples.h" |
15 #include "base/metrics/statistics_recorder.h" | 14 #include "base/metrics/statistics_recorder.h" |
16 #include "base/path_service.h" | 15 #include "base/path_service.h" |
17 #include "base/prefs/pref_filter.h" | 16 #include "base/prefs/pref_filter.h" |
18 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
19 #include "base/single_thread_task_runner.h" | |
20 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
21 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
22 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
23 #include "base/test/simple_test_clock.h" | 21 #include "base/test/simple_test_clock.h" |
24 #include "base/threading/sequenced_worker_pool.h" | 22 #include "base/threading/sequenced_worker_pool.h" |
25 #include "base/threading/thread.h" | 23 #include "base/threading/thread.h" |
26 #include "base/values.h" | 24 #include "base/values.h" |
27 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
29 | 27 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 99 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
102 | 100 |
103 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_)); | 101 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_)); |
104 data_dir_ = data_dir_.AppendASCII("prefs"); | 102 data_dir_ = data_dir_.AppendASCII("prefs"); |
105 ASSERT_TRUE(PathExists(data_dir_)); | 103 ASSERT_TRUE(PathExists(data_dir_)); |
106 } | 104 } |
107 | 105 |
108 void TearDown() override { | 106 void TearDown() override { |
109 // Make sure all pending tasks have been processed (e.g., deleting the | 107 // Make sure all pending tasks have been processed (e.g., deleting the |
110 // JsonPrefStore may post write tasks). | 108 // JsonPrefStore may post write tasks). |
111 message_loop_.task_runner()->PostTask(FROM_HERE, | 109 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure()); |
112 MessageLoop::QuitWhenIdleClosure()); | |
113 message_loop_.Run(); | 110 message_loop_.Run(); |
114 } | 111 } |
115 | 112 |
116 // The path to temporary directory used to contain the test operations. | 113 // The path to temporary directory used to contain the test operations. |
117 base::ScopedTempDir temp_dir_; | 114 base::ScopedTempDir temp_dir_; |
118 // The path to the directory where the test data is stored. | 115 // The path to the directory where the test data is stored. |
119 base::FilePath data_dir_; | 116 base::FilePath data_dir_; |
120 // A message loop that we can use as the file thread message loop. | 117 // A message loop that we can use as the file thread message loop. |
121 MessageLoop message_loop_; | 118 MessageLoop message_loop_; |
122 | 119 |
123 private: | 120 private: |
124 // Ensure histograms are reset for each test. | 121 // Ensure histograms are reset for each test. |
125 StatisticsRecorder statistics_recorder_; | 122 StatisticsRecorder statistics_recorder_; |
126 }; | 123 }; |
127 | 124 |
128 // Test fallback behavior for a nonexistent file. | 125 // Test fallback behavior for a nonexistent file. |
129 TEST_F(JsonPrefStoreTest, NonExistentFile) { | 126 TEST_F(JsonPrefStoreTest, NonExistentFile) { |
130 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 127 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
131 ASSERT_FALSE(PathExists(bogus_input_file)); | 128 ASSERT_FALSE(PathExists(bogus_input_file)); |
132 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 129 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
133 bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 130 bogus_input_file, |
| 131 message_loop_.message_loop_proxy().get(), |
| 132 scoped_ptr<PrefFilter>()); |
134 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 133 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
135 pref_store->ReadPrefs()); | 134 pref_store->ReadPrefs()); |
136 EXPECT_FALSE(pref_store->ReadOnly()); | 135 EXPECT_FALSE(pref_store->ReadOnly()); |
137 } | 136 } |
138 | 137 |
139 // Test fallback behavior for a nonexistent file and alternate file. | 138 // Test fallback behavior for a nonexistent file and alternate file. |
140 TEST_F(JsonPrefStoreTest, NonExistentFileAndAlternateFile) { | 139 TEST_F(JsonPrefStoreTest, NonExistentFileAndAlternateFile) { |
141 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 140 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
142 base::FilePath bogus_alternate_input_file = | 141 base::FilePath bogus_alternate_input_file = |
143 data_dir_.AppendASCII("read_alternate.txt"); | 142 data_dir_.AppendASCII("read_alternate.txt"); |
144 ASSERT_FALSE(PathExists(bogus_input_file)); | 143 ASSERT_FALSE(PathExists(bogus_input_file)); |
145 ASSERT_FALSE(PathExists(bogus_alternate_input_file)); | 144 ASSERT_FALSE(PathExists(bogus_alternate_input_file)); |
146 scoped_refptr<JsonPrefStore> pref_store = | 145 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
147 new JsonPrefStore(bogus_input_file, bogus_alternate_input_file, | 146 bogus_input_file, |
148 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 147 bogus_alternate_input_file, |
| 148 message_loop_.message_loop_proxy().get(), |
| 149 scoped_ptr<PrefFilter>()); |
149 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 150 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
150 pref_store->ReadPrefs()); | 151 pref_store->ReadPrefs()); |
151 EXPECT_FALSE(pref_store->ReadOnly()); | 152 EXPECT_FALSE(pref_store->ReadOnly()); |
152 } | 153 } |
153 | 154 |
154 // Test fallback behavior for an invalid file. | 155 // Test fallback behavior for an invalid file. |
155 TEST_F(JsonPrefStoreTest, InvalidFile) { | 156 TEST_F(JsonPrefStoreTest, InvalidFile) { |
156 base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); | 157 base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); |
157 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); | 158 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); |
158 ASSERT_TRUE(base::CopyFile(invalid_file_original, invalid_file)); | 159 ASSERT_TRUE(base::CopyFile(invalid_file_original, invalid_file)); |
159 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 160 scoped_refptr<JsonPrefStore> pref_store = |
160 invalid_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 161 new JsonPrefStore(invalid_file, |
| 162 message_loop_.message_loop_proxy().get(), |
| 163 scoped_ptr<PrefFilter>()); |
161 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, | 164 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, |
162 pref_store->ReadPrefs()); | 165 pref_store->ReadPrefs()); |
163 EXPECT_FALSE(pref_store->ReadOnly()); | 166 EXPECT_FALSE(pref_store->ReadOnly()); |
164 | 167 |
165 // The file should have been moved aside. | 168 // The file should have been moved aside. |
166 EXPECT_FALSE(PathExists(invalid_file)); | 169 EXPECT_FALSE(PathExists(invalid_file)); |
167 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); | 170 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); |
168 EXPECT_TRUE(PathExists(moved_aside)); | 171 EXPECT_TRUE(PathExists(moved_aside)); |
169 EXPECT_TRUE(TextContentsEqual(invalid_file_original, moved_aside)); | 172 EXPECT_TRUE(TextContentsEqual(invalid_file_original, moved_aside)); |
170 } | 173 } |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 } | 239 } |
237 | 240 |
238 TEST_F(JsonPrefStoreTest, Basic) { | 241 TEST_F(JsonPrefStoreTest, Basic) { |
239 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), | 242 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), |
240 temp_dir_.path().AppendASCII("write.json"))); | 243 temp_dir_.path().AppendASCII("write.json"))); |
241 | 244 |
242 // Test that the persistent value can be loaded. | 245 // Test that the persistent value can be loaded. |
243 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 246 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
244 ASSERT_TRUE(PathExists(input_file)); | 247 ASSERT_TRUE(PathExists(input_file)); |
245 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 248 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
246 input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 249 input_file, |
| 250 message_loop_.message_loop_proxy().get(), |
| 251 scoped_ptr<PrefFilter>()); |
247 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 252 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
248 EXPECT_FALSE(pref_store->ReadOnly()); | 253 EXPECT_FALSE(pref_store->ReadOnly()); |
249 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 254 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
250 | 255 |
251 // The JSON file looks like this: | 256 // The JSON file looks like this: |
252 // { | 257 // { |
253 // "homepage": "http://www.cnn.com", | 258 // "homepage": "http://www.cnn.com", |
254 // "some_directory": "/usr/local/", | 259 // "some_directory": "/usr/local/", |
255 // "tabs": { | 260 // "tabs": { |
256 // "new_windows_in_tabs": true, | 261 // "new_windows_in_tabs": true, |
257 // "max_tabs": 20 | 262 // "max_tabs": 20 |
258 // } | 263 // } |
259 // } | 264 // } |
260 | 265 |
261 RunBasicJsonPrefStoreTest( | 266 RunBasicJsonPrefStoreTest( |
262 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 267 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
263 } | 268 } |
264 | 269 |
265 TEST_F(JsonPrefStoreTest, BasicAsync) { | 270 TEST_F(JsonPrefStoreTest, BasicAsync) { |
266 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), | 271 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), |
267 temp_dir_.path().AppendASCII("write.json"))); | 272 temp_dir_.path().AppendASCII("write.json"))); |
268 | 273 |
269 // Test that the persistent value can be loaded. | 274 // Test that the persistent value can be loaded. |
270 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 275 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
271 ASSERT_TRUE(PathExists(input_file)); | 276 ASSERT_TRUE(PathExists(input_file)); |
272 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 277 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
273 input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 278 input_file, |
| 279 message_loop_.message_loop_proxy().get(), |
| 280 scoped_ptr<PrefFilter>()); |
274 | 281 |
275 { | 282 { |
276 MockPrefStoreObserver mock_observer; | 283 MockPrefStoreObserver mock_observer; |
277 pref_store->AddObserver(&mock_observer); | 284 pref_store->AddObserver(&mock_observer); |
278 | 285 |
279 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | 286 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
280 pref_store->ReadPrefsAsync(mock_error_delegate); | 287 pref_store->ReadPrefsAsync(mock_error_delegate); |
281 | 288 |
282 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 289 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
283 EXPECT_CALL(*mock_error_delegate, | 290 EXPECT_CALL(*mock_error_delegate, |
(...skipping 16 matching lines...) Expand all Loading... |
300 // } | 307 // } |
301 | 308 |
302 RunBasicJsonPrefStoreTest( | 309 RunBasicJsonPrefStoreTest( |
303 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 310 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
304 } | 311 } |
305 | 312 |
306 TEST_F(JsonPrefStoreTest, PreserveEmptyValues) { | 313 TEST_F(JsonPrefStoreTest, PreserveEmptyValues) { |
307 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); | 314 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); |
308 | 315 |
309 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 316 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
310 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 317 pref_file, |
| 318 message_loop_.message_loop_proxy(), |
| 319 scoped_ptr<PrefFilter>()); |
311 | 320 |
312 // Set some keys with empty values. | 321 // Set some keys with empty values. |
313 pref_store->SetValue("list", new base::ListValue); | 322 pref_store->SetValue("list", new base::ListValue); |
314 pref_store->SetValue("dict", new base::DictionaryValue); | 323 pref_store->SetValue("dict", new base::DictionaryValue); |
315 | 324 |
316 // Write to file. | 325 // Write to file. |
317 pref_store->CommitPendingWrite(); | 326 pref_store->CommitPendingWrite(); |
318 MessageLoop::current()->RunUntilIdle(); | 327 MessageLoop::current()->RunUntilIdle(); |
319 | 328 |
320 // Reload. | 329 // Reload. |
321 pref_store = new JsonPrefStore(pref_file, message_loop_.task_runner(), | 330 pref_store = new JsonPrefStore( |
322 scoped_ptr<PrefFilter>()); | 331 pref_file, |
| 332 message_loop_.message_loop_proxy(), |
| 333 scoped_ptr<PrefFilter>()); |
323 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 334 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
324 ASSERT_FALSE(pref_store->ReadOnly()); | 335 ASSERT_FALSE(pref_store->ReadOnly()); |
325 | 336 |
326 // Check values. | 337 // Check values. |
327 const Value* result = NULL; | 338 const Value* result = NULL; |
328 EXPECT_TRUE(pref_store->GetValue("list", &result)); | 339 EXPECT_TRUE(pref_store->GetValue("list", &result)); |
329 EXPECT_TRUE(ListValue().Equals(result)); | 340 EXPECT_TRUE(ListValue().Equals(result)); |
330 EXPECT_TRUE(pref_store->GetValue("dict", &result)); | 341 EXPECT_TRUE(pref_store->GetValue("dict", &result)); |
331 EXPECT_TRUE(DictionaryValue().Equals(result)); | 342 EXPECT_TRUE(DictionaryValue().Equals(result)); |
332 } | 343 } |
333 | 344 |
334 // This test is just documenting some potentially non-obvious behavior. It | 345 // This test is just documenting some potentially non-obvious behavior. It |
335 // shouldn't be taken as normative. | 346 // shouldn't be taken as normative. |
336 TEST_F(JsonPrefStoreTest, RemoveClearsEmptyParent) { | 347 TEST_F(JsonPrefStoreTest, RemoveClearsEmptyParent) { |
337 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); | 348 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); |
338 | 349 |
339 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 350 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
340 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 351 pref_file, |
| 352 message_loop_.message_loop_proxy(), |
| 353 scoped_ptr<PrefFilter>()); |
341 | 354 |
342 base::DictionaryValue* dict = new base::DictionaryValue; | 355 base::DictionaryValue* dict = new base::DictionaryValue; |
343 dict->SetString("key", "value"); | 356 dict->SetString("key", "value"); |
344 pref_store->SetValue("dict", dict); | 357 pref_store->SetValue("dict", dict); |
345 | 358 |
346 pref_store->RemoveValue("dict.key"); | 359 pref_store->RemoveValue("dict.key"); |
347 | 360 |
348 const base::Value* retrieved_dict = NULL; | 361 const base::Value* retrieved_dict = NULL; |
349 bool has_dict = pref_store->GetValue("dict", &retrieved_dict); | 362 bool has_dict = pref_store->GetValue("dict", &retrieved_dict); |
350 EXPECT_FALSE(has_dict); | 363 EXPECT_FALSE(has_dict); |
351 } | 364 } |
352 | 365 |
353 // Tests asynchronous reading of the file when there is no file. | 366 // Tests asynchronous reading of the file when there is no file. |
354 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { | 367 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { |
355 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 368 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
356 ASSERT_FALSE(PathExists(bogus_input_file)); | 369 ASSERT_FALSE(PathExists(bogus_input_file)); |
357 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 370 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
358 bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 371 bogus_input_file, |
| 372 message_loop_.message_loop_proxy().get(), |
| 373 scoped_ptr<PrefFilter>()); |
359 MockPrefStoreObserver mock_observer; | 374 MockPrefStoreObserver mock_observer; |
360 pref_store->AddObserver(&mock_observer); | 375 pref_store->AddObserver(&mock_observer); |
361 | 376 |
362 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; | 377 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; |
363 pref_store->ReadPrefsAsync(mock_error_delegate); | 378 pref_store->ReadPrefsAsync(mock_error_delegate); |
364 | 379 |
365 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 380 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
366 EXPECT_CALL(*mock_error_delegate, | 381 EXPECT_CALL(*mock_error_delegate, |
367 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); | 382 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); |
368 RunLoop().RunUntilIdle(); | 383 RunLoop().RunUntilIdle(); |
369 pref_store->RemoveObserver(&mock_observer); | 384 pref_store->RemoveObserver(&mock_observer); |
370 | 385 |
371 EXPECT_FALSE(pref_store->ReadOnly()); | 386 EXPECT_FALSE(pref_store->ReadOnly()); |
372 } | 387 } |
373 | 388 |
374 TEST_F(JsonPrefStoreTest, ReadWithInterceptor) { | 389 TEST_F(JsonPrefStoreTest, ReadWithInterceptor) { |
375 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), | 390 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), |
376 temp_dir_.path().AppendASCII("write.json"))); | 391 temp_dir_.path().AppendASCII("write.json"))); |
377 | 392 |
378 // Test that the persistent value can be loaded. | 393 // Test that the persistent value can be loaded. |
379 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 394 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
380 ASSERT_TRUE(PathExists(input_file)); | 395 ASSERT_TRUE(PathExists(input_file)); |
381 | 396 |
382 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( | 397 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
383 new InterceptingPrefFilter()); | 398 new InterceptingPrefFilter()); |
384 InterceptingPrefFilter* raw_intercepting_pref_filter_ = | 399 InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
385 intercepting_pref_filter.get(); | 400 intercepting_pref_filter.get(); |
386 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 401 scoped_refptr<JsonPrefStore> pref_store = |
387 input_file, message_loop_.task_runner(), intercepting_pref_filter.Pass()); | 402 new JsonPrefStore(input_file, |
| 403 message_loop_.message_loop_proxy().get(), |
| 404 intercepting_pref_filter.Pass()); |
388 | 405 |
389 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE, | 406 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE, |
390 pref_store->ReadPrefs()); | 407 pref_store->ReadPrefs()); |
391 EXPECT_FALSE(pref_store->ReadOnly()); | 408 EXPECT_FALSE(pref_store->ReadOnly()); |
392 | 409 |
393 // The store shouldn't be considered initialized until the interceptor | 410 // The store shouldn't be considered initialized until the interceptor |
394 // returns. | 411 // returns. |
395 EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); | 412 EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
396 EXPECT_FALSE(pref_store->IsInitializationComplete()); | 413 EXPECT_FALSE(pref_store->IsInitializationComplete()); |
397 EXPECT_FALSE(pref_store->GetValue(kHomePage, NULL)); | 414 EXPECT_FALSE(pref_store->GetValue(kHomePage, NULL)); |
(...skipping 23 matching lines...) Expand all Loading... |
421 temp_dir_.path().AppendASCII("write.json"))); | 438 temp_dir_.path().AppendASCII("write.json"))); |
422 | 439 |
423 // Test that the persistent value can be loaded. | 440 // Test that the persistent value can be loaded. |
424 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 441 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
425 ASSERT_TRUE(PathExists(input_file)); | 442 ASSERT_TRUE(PathExists(input_file)); |
426 | 443 |
427 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( | 444 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
428 new InterceptingPrefFilter()); | 445 new InterceptingPrefFilter()); |
429 InterceptingPrefFilter* raw_intercepting_pref_filter_ = | 446 InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
430 intercepting_pref_filter.get(); | 447 intercepting_pref_filter.get(); |
431 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 448 scoped_refptr<JsonPrefStore> pref_store = |
432 input_file, message_loop_.task_runner(), intercepting_pref_filter.Pass()); | 449 new JsonPrefStore(input_file, |
| 450 message_loop_.message_loop_proxy().get(), |
| 451 intercepting_pref_filter.Pass()); |
433 | 452 |
434 MockPrefStoreObserver mock_observer; | 453 MockPrefStoreObserver mock_observer; |
435 pref_store->AddObserver(&mock_observer); | 454 pref_store->AddObserver(&mock_observer); |
436 | 455 |
437 // Ownership of the |mock_error_delegate| is handed to the |pref_store| below. | 456 // Ownership of the |mock_error_delegate| is handed to the |pref_store| below. |
438 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | 457 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
439 | 458 |
440 { | 459 { |
441 pref_store->ReadPrefsAsync(mock_error_delegate); | 460 pref_store->ReadPrefsAsync(mock_error_delegate); |
442 | 461 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 base::CopyFile(data_dir_.AppendASCII("read.json"), | 504 base::CopyFile(data_dir_.AppendASCII("read.json"), |
486 temp_dir_.path().AppendASCII("alternate.json"))); | 505 temp_dir_.path().AppendASCII("alternate.json"))); |
487 | 506 |
488 // Test that the alternate file is moved to the main file and read as-is from | 507 // Test that the alternate file is moved to the main file and read as-is from |
489 // there. | 508 // there. |
490 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 509 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
491 base::FilePath alternate_input_file = | 510 base::FilePath alternate_input_file = |
492 temp_dir_.path().AppendASCII("alternate.json"); | 511 temp_dir_.path().AppendASCII("alternate.json"); |
493 ASSERT_FALSE(PathExists(input_file)); | 512 ASSERT_FALSE(PathExists(input_file)); |
494 ASSERT_TRUE(PathExists(alternate_input_file)); | 513 ASSERT_TRUE(PathExists(alternate_input_file)); |
495 scoped_refptr<JsonPrefStore> pref_store = | 514 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
496 new JsonPrefStore(input_file, alternate_input_file, | 515 input_file, |
497 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 516 alternate_input_file, |
| 517 message_loop_.message_loop_proxy().get(), |
| 518 scoped_ptr<PrefFilter>()); |
498 | 519 |
499 ASSERT_FALSE(PathExists(input_file)); | 520 ASSERT_FALSE(PathExists(input_file)); |
500 ASSERT_TRUE(PathExists(alternate_input_file)); | 521 ASSERT_TRUE(PathExists(alternate_input_file)); |
501 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 522 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
502 | 523 |
503 ASSERT_TRUE(PathExists(input_file)); | 524 ASSERT_TRUE(PathExists(input_file)); |
504 ASSERT_FALSE(PathExists(alternate_input_file)); | 525 ASSERT_FALSE(PathExists(alternate_input_file)); |
505 | 526 |
506 EXPECT_FALSE(pref_store->ReadOnly()); | 527 EXPECT_FALSE(pref_store->ReadOnly()); |
507 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 528 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
(...skipping 21 matching lines...) Expand all Loading... |
529 temp_dir_.path().AppendASCII("alternate.json"))); | 550 temp_dir_.path().AppendASCII("alternate.json"))); |
530 | 551 |
531 // Test that the alternate file is ignored and that the read occurs from the | 552 // Test that the alternate file is ignored and that the read occurs from the |
532 // existing main file. There is no attempt at even deleting the alternate | 553 // existing main file. There is no attempt at even deleting the alternate |
533 // file as this scenario should never happen in normal user-data-dirs. | 554 // file as this scenario should never happen in normal user-data-dirs. |
534 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 555 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
535 base::FilePath alternate_input_file = | 556 base::FilePath alternate_input_file = |
536 temp_dir_.path().AppendASCII("alternate.json"); | 557 temp_dir_.path().AppendASCII("alternate.json"); |
537 ASSERT_TRUE(PathExists(input_file)); | 558 ASSERT_TRUE(PathExists(input_file)); |
538 ASSERT_TRUE(PathExists(alternate_input_file)); | 559 ASSERT_TRUE(PathExists(alternate_input_file)); |
539 scoped_refptr<JsonPrefStore> pref_store = | 560 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
540 new JsonPrefStore(input_file, alternate_input_file, | 561 input_file, |
541 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 562 alternate_input_file, |
| 563 message_loop_.message_loop_proxy().get(), |
| 564 scoped_ptr<PrefFilter>()); |
542 | 565 |
543 ASSERT_TRUE(PathExists(input_file)); | 566 ASSERT_TRUE(PathExists(input_file)); |
544 ASSERT_TRUE(PathExists(alternate_input_file)); | 567 ASSERT_TRUE(PathExists(alternate_input_file)); |
545 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 568 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
546 | 569 |
547 ASSERT_TRUE(PathExists(input_file)); | 570 ASSERT_TRUE(PathExists(input_file)); |
548 ASSERT_TRUE(PathExists(alternate_input_file)); | 571 ASSERT_TRUE(PathExists(alternate_input_file)); |
549 | 572 |
550 EXPECT_FALSE(pref_store->ReadOnly()); | 573 EXPECT_FALSE(pref_store->ReadOnly()); |
551 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 574 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
(...skipping 17 matching lines...) Expand all Loading... |
569 base::CopyFile(data_dir_.AppendASCII("read.json"), | 592 base::CopyFile(data_dir_.AppendASCII("read.json"), |
570 temp_dir_.path().AppendASCII("write.json"))); | 593 temp_dir_.path().AppendASCII("write.json"))); |
571 | 594 |
572 // Test that the basic read works fine when an alternate file is specified but | 595 // Test that the basic read works fine when an alternate file is specified but |
573 // does not exist. | 596 // does not exist. |
574 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 597 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
575 base::FilePath alternate_input_file = | 598 base::FilePath alternate_input_file = |
576 temp_dir_.path().AppendASCII("alternate.json"); | 599 temp_dir_.path().AppendASCII("alternate.json"); |
577 ASSERT_TRUE(PathExists(input_file)); | 600 ASSERT_TRUE(PathExists(input_file)); |
578 ASSERT_FALSE(PathExists(alternate_input_file)); | 601 ASSERT_FALSE(PathExists(alternate_input_file)); |
579 scoped_refptr<JsonPrefStore> pref_store = | 602 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
580 new JsonPrefStore(input_file, alternate_input_file, | 603 input_file, |
581 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 604 alternate_input_file, |
| 605 message_loop_.message_loop_proxy().get(), |
| 606 scoped_ptr<PrefFilter>()); |
582 | 607 |
583 ASSERT_TRUE(PathExists(input_file)); | 608 ASSERT_TRUE(PathExists(input_file)); |
584 ASSERT_FALSE(PathExists(alternate_input_file)); | 609 ASSERT_FALSE(PathExists(alternate_input_file)); |
585 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 610 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
586 | 611 |
587 ASSERT_TRUE(PathExists(input_file)); | 612 ASSERT_TRUE(PathExists(input_file)); |
588 ASSERT_FALSE(PathExists(alternate_input_file)); | 613 ASSERT_FALSE(PathExists(alternate_input_file)); |
589 | 614 |
590 EXPECT_FALSE(pref_store->ReadOnly()); | 615 EXPECT_FALSE(pref_store->ReadOnly()); |
591 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 616 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
(...skipping 17 matching lines...) Expand all Loading... |
609 base::CopyFile(data_dir_.AppendASCII("read.json"), | 634 base::CopyFile(data_dir_.AppendASCII("read.json"), |
610 temp_dir_.path().AppendASCII("alternate.json"))); | 635 temp_dir_.path().AppendASCII("alternate.json"))); |
611 | 636 |
612 // Test that the alternate file is moved to the main file and read as-is from | 637 // Test that the alternate file is moved to the main file and read as-is from |
613 // there even when the read is made asynchronously. | 638 // there even when the read is made asynchronously. |
614 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 639 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
615 base::FilePath alternate_input_file = | 640 base::FilePath alternate_input_file = |
616 temp_dir_.path().AppendASCII("alternate.json"); | 641 temp_dir_.path().AppendASCII("alternate.json"); |
617 ASSERT_FALSE(PathExists(input_file)); | 642 ASSERT_FALSE(PathExists(input_file)); |
618 ASSERT_TRUE(PathExists(alternate_input_file)); | 643 ASSERT_TRUE(PathExists(alternate_input_file)); |
619 scoped_refptr<JsonPrefStore> pref_store = | 644 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
620 new JsonPrefStore(input_file, alternate_input_file, | 645 input_file, |
621 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 646 alternate_input_file, |
| 647 message_loop_.message_loop_proxy().get(), |
| 648 scoped_ptr<PrefFilter>()); |
622 | 649 |
623 ASSERT_FALSE(PathExists(input_file)); | 650 ASSERT_FALSE(PathExists(input_file)); |
624 ASSERT_TRUE(PathExists(alternate_input_file)); | 651 ASSERT_TRUE(PathExists(alternate_input_file)); |
625 | 652 |
626 { | 653 { |
627 MockPrefStoreObserver mock_observer; | 654 MockPrefStoreObserver mock_observer; |
628 pref_store->AddObserver(&mock_observer); | 655 pref_store->AddObserver(&mock_observer); |
629 | 656 |
630 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | 657 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
631 pref_store->ReadPrefsAsync(mock_error_delegate); | 658 pref_store->ReadPrefsAsync(mock_error_delegate); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
795 scoped_ptr<HistogramSamples> samples = | 822 scoped_ptr<HistogramSamples> samples = |
796 histogram.GetHistogram()->SnapshotSamples(); | 823 histogram.GetHistogram()->SnapshotSamples(); |
797 ASSERT_EQ(3, samples->GetCount(0)); | 824 ASSERT_EQ(3, samples->GetCount(0)); |
798 ASSERT_EQ(1, samples->GetCount(1)); | 825 ASSERT_EQ(1, samples->GetCount(1)); |
799 ASSERT_EQ(1, samples->GetCount(2)); | 826 ASSERT_EQ(1, samples->GetCount(2)); |
800 ASSERT_EQ(1, samples->GetCount(3)); | 827 ASSERT_EQ(1, samples->GetCount(3)); |
801 ASSERT_EQ(6, samples->TotalCount()); | 828 ASSERT_EQ(6, samples->TotalCount()); |
802 } | 829 } |
803 | 830 |
804 } // namespace base | 831 } // namespace base |
OLD | NEW |