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 "components/prefs/json_pref_store.h" | 5 #include "components/prefs/json_pref_store.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 } | 133 } |
134 | 134 |
135 // The path to temporary directory used to contain the test operations. | 135 // The path to temporary directory used to contain the test operations. |
136 base::ScopedTempDir temp_dir_; | 136 base::ScopedTempDir temp_dir_; |
137 // A message loop that we can use as the file thread message loop. | 137 // A message loop that we can use as the file thread message loop. |
138 MessageLoop message_loop_; | 138 MessageLoop message_loop_; |
139 }; | 139 }; |
140 | 140 |
141 // Test fallback behavior for a nonexistent file. | 141 // Test fallback behavior for a nonexistent file. |
142 TEST_F(JsonPrefStoreTest, NonExistentFile) { | 142 TEST_F(JsonPrefStoreTest, NonExistentFile) { |
143 base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); | 143 base::FilePath bogus_input_file = temp_dir_.GetPath().AppendASCII("read.txt"); |
144 ASSERT_FALSE(PathExists(bogus_input_file)); | 144 ASSERT_FALSE(PathExists(bogus_input_file)); |
145 scoped_refptr<JsonPrefStore> pref_store = | 145 scoped_refptr<JsonPrefStore> pref_store = |
146 new JsonPrefStore(bogus_input_file, message_loop_.task_runner(), | 146 new JsonPrefStore(bogus_input_file, message_loop_.task_runner(), |
147 std::unique_ptr<PrefFilter>()); | 147 std::unique_ptr<PrefFilter>()); |
148 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 148 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
149 pref_store->ReadPrefs()); | 149 pref_store->ReadPrefs()); |
150 EXPECT_FALSE(pref_store->ReadOnly()); | 150 EXPECT_FALSE(pref_store->ReadOnly()); |
151 } | 151 } |
152 | 152 |
153 // Test fallback behavior for a nonexistent file and alternate file. | 153 // Test fallback behavior for a nonexistent file and alternate file. |
154 TEST_F(JsonPrefStoreTest, NonExistentFileAndAlternateFile) { | 154 TEST_F(JsonPrefStoreTest, NonExistentFileAndAlternateFile) { |
155 base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); | 155 base::FilePath bogus_input_file = temp_dir_.GetPath().AppendASCII("read.txt"); |
156 base::FilePath bogus_alternate_input_file = | 156 base::FilePath bogus_alternate_input_file = |
157 temp_dir_.path().AppendASCII("read_alternate.txt"); | 157 temp_dir_.GetPath().AppendASCII("read_alternate.txt"); |
158 ASSERT_FALSE(PathExists(bogus_input_file)); | 158 ASSERT_FALSE(PathExists(bogus_input_file)); |
159 ASSERT_FALSE(PathExists(bogus_alternate_input_file)); | 159 ASSERT_FALSE(PathExists(bogus_alternate_input_file)); |
160 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 160 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
161 bogus_input_file, bogus_alternate_input_file, message_loop_.task_runner(), | 161 bogus_input_file, bogus_alternate_input_file, message_loop_.task_runner(), |
162 std::unique_ptr<PrefFilter>()); | 162 std::unique_ptr<PrefFilter>()); |
163 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 163 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
164 pref_store->ReadPrefs()); | 164 pref_store->ReadPrefs()); |
165 EXPECT_FALSE(pref_store->ReadOnly()); | 165 EXPECT_FALSE(pref_store->ReadOnly()); |
166 } | 166 } |
167 | 167 |
168 // Test fallback behavior for an invalid file. | 168 // Test fallback behavior for an invalid file. |
169 TEST_F(JsonPrefStoreTest, InvalidFile) { | 169 TEST_F(JsonPrefStoreTest, InvalidFile) { |
170 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); | 170 base::FilePath invalid_file = temp_dir_.GetPath().AppendASCII("invalid.json"); |
171 ASSERT_LT(0, base::WriteFile(invalid_file, | 171 ASSERT_LT(0, base::WriteFile(invalid_file, |
172 kInvalidJson, arraysize(kInvalidJson) - 1)); | 172 kInvalidJson, arraysize(kInvalidJson) - 1)); |
173 | 173 |
174 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 174 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
175 invalid_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); | 175 invalid_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); |
176 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, | 176 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, |
177 pref_store->ReadPrefs()); | 177 pref_store->ReadPrefs()); |
178 EXPECT_FALSE(pref_store->ReadOnly()); | 178 EXPECT_FALSE(pref_store->ReadOnly()); |
179 | 179 |
180 // The file should have been moved aside. | 180 // The file should have been moved aside. |
181 EXPECT_FALSE(PathExists(invalid_file)); | 181 EXPECT_FALSE(PathExists(invalid_file)); |
182 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); | 182 base::FilePath moved_aside = temp_dir_.GetPath().AppendASCII("invalid.bad"); |
183 EXPECT_TRUE(PathExists(moved_aside)); | 183 EXPECT_TRUE(PathExists(moved_aside)); |
184 | 184 |
185 std::string moved_aside_contents; | 185 std::string moved_aside_contents; |
186 ASSERT_TRUE(base::ReadFileToString(moved_aside, &moved_aside_contents)); | 186 ASSERT_TRUE(base::ReadFileToString(moved_aside, &moved_aside_contents)); |
187 EXPECT_EQ(kInvalidJson, moved_aside_contents); | 187 EXPECT_EQ(kInvalidJson, moved_aside_contents); |
188 } | 188 } |
189 | 189 |
190 // This function is used to avoid code duplication while testing synchronous | 190 // This function is used to avoid code duplication while testing synchronous |
191 // and asynchronous version of the JsonPrefStore loading. It validates that the | 191 // and asynchronous version of the JsonPrefStore loading. It validates that the |
192 // given output file's contents matches kWriteGolden. | 192 // given output file's contents matches kWriteGolden. |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 pref_store->CommitPendingWrite(); | 255 pref_store->CommitPendingWrite(); |
256 RunLoop().RunUntilIdle(); | 256 RunLoop().RunUntilIdle(); |
257 | 257 |
258 std::string output_contents; | 258 std::string output_contents; |
259 ASSERT_TRUE(base::ReadFileToString(output_file, &output_contents)); | 259 ASSERT_TRUE(base::ReadFileToString(output_file, &output_contents)); |
260 EXPECT_EQ(kWriteGolden, output_contents); | 260 EXPECT_EQ(kWriteGolden, output_contents); |
261 ASSERT_TRUE(base::DeleteFile(output_file, false)); | 261 ASSERT_TRUE(base::DeleteFile(output_file, false)); |
262 } | 262 } |
263 | 263 |
264 TEST_F(JsonPrefStoreTest, Basic) { | 264 TEST_F(JsonPrefStoreTest, Basic) { |
265 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 265 base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json"); |
266 ASSERT_LT(0, base::WriteFile(input_file, | 266 ASSERT_LT(0, base::WriteFile(input_file, |
267 kReadJson, arraysize(kReadJson) - 1)); | 267 kReadJson, arraysize(kReadJson) - 1)); |
268 | 268 |
269 // Test that the persistent value can be loaded. | 269 // Test that the persistent value can be loaded. |
270 ASSERT_TRUE(PathExists(input_file)); | 270 ASSERT_TRUE(PathExists(input_file)); |
271 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 271 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
272 input_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); | 272 input_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); |
273 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 273 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
274 EXPECT_FALSE(pref_store->ReadOnly()); | 274 EXPECT_FALSE(pref_store->ReadOnly()); |
275 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 275 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
276 | 276 |
277 // The JSON file looks like this: | 277 // The JSON file looks like this: |
278 // { | 278 // { |
279 // "homepage": "http://www.cnn.com", | 279 // "homepage": "http://www.cnn.com", |
280 // "some_directory": "/usr/local/", | 280 // "some_directory": "/usr/local/", |
281 // "tabs": { | 281 // "tabs": { |
282 // "new_windows_in_tabs": true, | 282 // "new_windows_in_tabs": true, |
283 // "max_tabs": 20 | 283 // "max_tabs": 20 |
284 // } | 284 // } |
285 // } | 285 // } |
286 | 286 |
287 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); | 287 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
288 } | 288 } |
289 | 289 |
290 TEST_F(JsonPrefStoreTest, BasicAsync) { | 290 TEST_F(JsonPrefStoreTest, BasicAsync) { |
291 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 291 base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json"); |
292 ASSERT_LT(0, base::WriteFile(input_file, | 292 ASSERT_LT(0, base::WriteFile(input_file, |
293 kReadJson, arraysize(kReadJson) - 1)); | 293 kReadJson, arraysize(kReadJson) - 1)); |
294 | 294 |
295 // Test that the persistent value can be loaded. | 295 // Test that the persistent value can be loaded. |
296 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 296 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
297 input_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); | 297 input_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); |
298 | 298 |
299 { | 299 { |
300 MockPrefStoreObserver mock_observer; | 300 MockPrefStoreObserver mock_observer; |
301 pref_store->AddObserver(&mock_observer); | 301 pref_store->AddObserver(&mock_observer); |
(...skipping 18 matching lines...) Expand all Loading... |
320 // "tabs": { | 320 // "tabs": { |
321 // "new_windows_in_tabs": true, | 321 // "new_windows_in_tabs": true, |
322 // "max_tabs": 20 | 322 // "max_tabs": 20 |
323 // } | 323 // } |
324 // } | 324 // } |
325 | 325 |
326 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); | 326 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
327 } | 327 } |
328 | 328 |
329 TEST_F(JsonPrefStoreTest, PreserveEmptyValues) { | 329 TEST_F(JsonPrefStoreTest, PreserveEmptyValues) { |
330 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); | 330 FilePath pref_file = temp_dir_.GetPath().AppendASCII("empty_values.json"); |
331 | 331 |
332 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 332 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
333 pref_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); | 333 pref_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); |
334 | 334 |
335 // Set some keys with empty values. | 335 // Set some keys with empty values. |
336 pref_store->SetValue("list", base::WrapUnique(new base::ListValue), | 336 pref_store->SetValue("list", base::WrapUnique(new base::ListValue), |
337 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 337 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
338 pref_store->SetValue("dict", base::WrapUnique(new base::DictionaryValue), | 338 pref_store->SetValue("dict", base::WrapUnique(new base::DictionaryValue), |
339 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 339 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
340 | 340 |
(...skipping 11 matching lines...) Expand all Loading... |
352 const Value* result = NULL; | 352 const Value* result = NULL; |
353 EXPECT_TRUE(pref_store->GetValue("list", &result)); | 353 EXPECT_TRUE(pref_store->GetValue("list", &result)); |
354 EXPECT_TRUE(ListValue().Equals(result)); | 354 EXPECT_TRUE(ListValue().Equals(result)); |
355 EXPECT_TRUE(pref_store->GetValue("dict", &result)); | 355 EXPECT_TRUE(pref_store->GetValue("dict", &result)); |
356 EXPECT_TRUE(DictionaryValue().Equals(result)); | 356 EXPECT_TRUE(DictionaryValue().Equals(result)); |
357 } | 357 } |
358 | 358 |
359 // This test is just documenting some potentially non-obvious behavior. It | 359 // This test is just documenting some potentially non-obvious behavior. It |
360 // shouldn't be taken as normative. | 360 // shouldn't be taken as normative. |
361 TEST_F(JsonPrefStoreTest, RemoveClearsEmptyParent) { | 361 TEST_F(JsonPrefStoreTest, RemoveClearsEmptyParent) { |
362 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); | 362 FilePath pref_file = temp_dir_.GetPath().AppendASCII("empty_values.json"); |
363 | 363 |
364 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 364 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
365 pref_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); | 365 pref_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); |
366 | 366 |
367 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue); | 367 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
368 dict->SetString("key", "value"); | 368 dict->SetString("key", "value"); |
369 pref_store->SetValue("dict", std::move(dict), | 369 pref_store->SetValue("dict", std::move(dict), |
370 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 370 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
371 | 371 |
372 pref_store->RemoveValue("dict.key", | 372 pref_store->RemoveValue("dict.key", |
373 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 373 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
374 | 374 |
375 const base::Value* retrieved_dict = NULL; | 375 const base::Value* retrieved_dict = NULL; |
376 bool has_dict = pref_store->GetValue("dict", &retrieved_dict); | 376 bool has_dict = pref_store->GetValue("dict", &retrieved_dict); |
377 EXPECT_FALSE(has_dict); | 377 EXPECT_FALSE(has_dict); |
378 } | 378 } |
379 | 379 |
380 // Tests asynchronous reading of the file when there is no file. | 380 // Tests asynchronous reading of the file when there is no file. |
381 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { | 381 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { |
382 base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); | 382 base::FilePath bogus_input_file = temp_dir_.GetPath().AppendASCII("read.txt"); |
383 ASSERT_FALSE(PathExists(bogus_input_file)); | 383 ASSERT_FALSE(PathExists(bogus_input_file)); |
384 scoped_refptr<JsonPrefStore> pref_store = | 384 scoped_refptr<JsonPrefStore> pref_store = |
385 new JsonPrefStore(bogus_input_file, message_loop_.task_runner(), | 385 new JsonPrefStore(bogus_input_file, message_loop_.task_runner(), |
386 std::unique_ptr<PrefFilter>()); | 386 std::unique_ptr<PrefFilter>()); |
387 MockPrefStoreObserver mock_observer; | 387 MockPrefStoreObserver mock_observer; |
388 pref_store->AddObserver(&mock_observer); | 388 pref_store->AddObserver(&mock_observer); |
389 | 389 |
390 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; | 390 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; |
391 pref_store->ReadPrefsAsync(mock_error_delegate); | 391 pref_store->ReadPrefsAsync(mock_error_delegate); |
392 | 392 |
393 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 393 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
394 EXPECT_CALL(*mock_error_delegate, | 394 EXPECT_CALL(*mock_error_delegate, |
395 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); | 395 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); |
396 RunLoop().RunUntilIdle(); | 396 RunLoop().RunUntilIdle(); |
397 pref_store->RemoveObserver(&mock_observer); | 397 pref_store->RemoveObserver(&mock_observer); |
398 | 398 |
399 EXPECT_FALSE(pref_store->ReadOnly()); | 399 EXPECT_FALSE(pref_store->ReadOnly()); |
400 } | 400 } |
401 | 401 |
402 TEST_F(JsonPrefStoreTest, ReadWithInterceptor) { | 402 TEST_F(JsonPrefStoreTest, ReadWithInterceptor) { |
403 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 403 base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json"); |
404 ASSERT_LT(0, base::WriteFile(input_file, | 404 ASSERT_LT(0, base::WriteFile(input_file, |
405 kReadJson, arraysize(kReadJson) - 1)); | 405 kReadJson, arraysize(kReadJson) - 1)); |
406 | 406 |
407 std::unique_ptr<InterceptingPrefFilter> intercepting_pref_filter( | 407 std::unique_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
408 new InterceptingPrefFilter()); | 408 new InterceptingPrefFilter()); |
409 InterceptingPrefFilter* raw_intercepting_pref_filter_ = | 409 InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
410 intercepting_pref_filter.get(); | 410 intercepting_pref_filter.get(); |
411 scoped_refptr<JsonPrefStore> pref_store = | 411 scoped_refptr<JsonPrefStore> pref_store = |
412 new JsonPrefStore(input_file, message_loop_.task_runner(), | 412 new JsonPrefStore(input_file, message_loop_.task_runner(), |
413 std::move(intercepting_pref_filter)); | 413 std::move(intercepting_pref_filter)); |
(...skipping 21 matching lines...) Expand all Loading... |
435 // "tabs": { | 435 // "tabs": { |
436 // "new_windows_in_tabs": true, | 436 // "new_windows_in_tabs": true, |
437 // "max_tabs": 20 | 437 // "max_tabs": 20 |
438 // } | 438 // } |
439 // } | 439 // } |
440 | 440 |
441 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); | 441 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
442 } | 442 } |
443 | 443 |
444 TEST_F(JsonPrefStoreTest, ReadAsyncWithInterceptor) { | 444 TEST_F(JsonPrefStoreTest, ReadAsyncWithInterceptor) { |
445 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 445 base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json"); |
446 ASSERT_LT(0, base::WriteFile(input_file, | 446 ASSERT_LT(0, base::WriteFile(input_file, |
447 kReadJson, arraysize(kReadJson) - 1)); | 447 kReadJson, arraysize(kReadJson) - 1)); |
448 | 448 |
449 std::unique_ptr<InterceptingPrefFilter> intercepting_pref_filter( | 449 std::unique_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
450 new InterceptingPrefFilter()); | 450 new InterceptingPrefFilter()); |
451 InterceptingPrefFilter* raw_intercepting_pref_filter_ = | 451 InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
452 intercepting_pref_filter.get(); | 452 intercepting_pref_filter.get(); |
453 scoped_refptr<JsonPrefStore> pref_store = | 453 scoped_refptr<JsonPrefStore> pref_store = |
454 new JsonPrefStore(input_file, message_loop_.task_runner(), | 454 new JsonPrefStore(input_file, message_loop_.task_runner(), |
455 std::move(intercepting_pref_filter)); | 455 std::move(intercepting_pref_filter)); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 // "new_windows_in_tabs": true, | 497 // "new_windows_in_tabs": true, |
498 // "max_tabs": 20 | 498 // "max_tabs": 20 |
499 // } | 499 // } |
500 // } | 500 // } |
501 | 501 |
502 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); | 502 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
503 } | 503 } |
504 | 504 |
505 TEST_F(JsonPrefStoreTest, AlternateFile) { | 505 TEST_F(JsonPrefStoreTest, AlternateFile) { |
506 base::FilePath alternate_input_file = | 506 base::FilePath alternate_input_file = |
507 temp_dir_.path().AppendASCII("alternate.json"); | 507 temp_dir_.GetPath().AppendASCII("alternate.json"); |
508 ASSERT_LT(0, base::WriteFile(alternate_input_file, | 508 ASSERT_LT(0, base::WriteFile(alternate_input_file, |
509 kReadJson, arraysize(kReadJson) - 1)); | 509 kReadJson, arraysize(kReadJson) - 1)); |
510 | 510 |
511 // Test that the alternate file is moved to the main file and read as-is from | 511 // Test that the alternate file is moved to the main file and read as-is from |
512 // there. | 512 // there. |
513 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 513 base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json"); |
514 ASSERT_FALSE(PathExists(input_file)); | 514 ASSERT_FALSE(PathExists(input_file)); |
515 ASSERT_TRUE(PathExists(alternate_input_file)); | 515 ASSERT_TRUE(PathExists(alternate_input_file)); |
516 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 516 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
517 input_file, alternate_input_file, message_loop_.task_runner(), | 517 input_file, alternate_input_file, message_loop_.task_runner(), |
518 std::unique_ptr<PrefFilter>()); | 518 std::unique_ptr<PrefFilter>()); |
519 | 519 |
520 ASSERT_FALSE(PathExists(input_file)); | 520 ASSERT_FALSE(PathExists(input_file)); |
521 ASSERT_TRUE(PathExists(alternate_input_file)); | 521 ASSERT_TRUE(PathExists(alternate_input_file)); |
522 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 522 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
523 | 523 |
(...skipping 10 matching lines...) Expand all Loading... |
534 // "tabs": { | 534 // "tabs": { |
535 // "new_windows_in_tabs": true, | 535 // "new_windows_in_tabs": true, |
536 // "max_tabs": 20 | 536 // "max_tabs": 20 |
537 // } | 537 // } |
538 // } | 538 // } |
539 | 539 |
540 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); | 540 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
541 } | 541 } |
542 | 542 |
543 TEST_F(JsonPrefStoreTest, AlternateFileIgnoredWhenMainFileExists) { | 543 TEST_F(JsonPrefStoreTest, AlternateFileIgnoredWhenMainFileExists) { |
544 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 544 base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json"); |
545 ASSERT_LT(0, base::WriteFile(input_file, | 545 ASSERT_LT(0, base::WriteFile(input_file, |
546 kReadJson, arraysize(kReadJson) - 1)); | 546 kReadJson, arraysize(kReadJson) - 1)); |
547 | 547 |
548 base::FilePath alternate_input_file = | 548 base::FilePath alternate_input_file = |
549 temp_dir_.path().AppendASCII("alternate.json"); | 549 temp_dir_.GetPath().AppendASCII("alternate.json"); |
550 ASSERT_LT(0, base::WriteFile(alternate_input_file, | 550 ASSERT_LT(0, base::WriteFile(alternate_input_file, |
551 kInvalidJson, arraysize(kInvalidJson) - 1)); | 551 kInvalidJson, arraysize(kInvalidJson) - 1)); |
552 | 552 |
553 // Test that the alternate file is ignored and that the read occurs from the | 553 // Test that the alternate file is ignored and that the read occurs from the |
554 // existing main file. There is no attempt at even deleting the alternate | 554 // existing main file. There is no attempt at even deleting the alternate |
555 // file as this scenario should never happen in normal user-data-dirs. | 555 // file as this scenario should never happen in normal user-data-dirs. |
556 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 556 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
557 input_file, alternate_input_file, message_loop_.task_runner(), | 557 input_file, alternate_input_file, message_loop_.task_runner(), |
558 std::unique_ptr<PrefFilter>()); | 558 std::unique_ptr<PrefFilter>()); |
559 | 559 |
(...skipping 14 matching lines...) Expand all Loading... |
574 // "tabs": { | 574 // "tabs": { |
575 // "new_windows_in_tabs": true, | 575 // "new_windows_in_tabs": true, |
576 // "max_tabs": 20 | 576 // "max_tabs": 20 |
577 // } | 577 // } |
578 // } | 578 // } |
579 | 579 |
580 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); | 580 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
581 } | 581 } |
582 | 582 |
583 TEST_F(JsonPrefStoreTest, AlternateFileDNE) { | 583 TEST_F(JsonPrefStoreTest, AlternateFileDNE) { |
584 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 584 base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json"); |
585 ASSERT_LT(0, base::WriteFile(input_file, | 585 ASSERT_LT(0, base::WriteFile(input_file, |
586 kReadJson, arraysize(kReadJson) - 1)); | 586 kReadJson, arraysize(kReadJson) - 1)); |
587 | 587 |
588 // Test that the basic read works fine when an alternate file is specified but | 588 // Test that the basic read works fine when an alternate file is specified but |
589 // does not exist. | 589 // does not exist. |
590 base::FilePath alternate_input_file = | 590 base::FilePath alternate_input_file = |
591 temp_dir_.path().AppendASCII("alternate.json"); | 591 temp_dir_.GetPath().AppendASCII("alternate.json"); |
592 ASSERT_TRUE(PathExists(input_file)); | 592 ASSERT_TRUE(PathExists(input_file)); |
593 ASSERT_FALSE(PathExists(alternate_input_file)); | 593 ASSERT_FALSE(PathExists(alternate_input_file)); |
594 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 594 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
595 input_file, alternate_input_file, message_loop_.task_runner(), | 595 input_file, alternate_input_file, message_loop_.task_runner(), |
596 std::unique_ptr<PrefFilter>()); | 596 std::unique_ptr<PrefFilter>()); |
597 | 597 |
598 ASSERT_TRUE(PathExists(input_file)); | 598 ASSERT_TRUE(PathExists(input_file)); |
599 ASSERT_FALSE(PathExists(alternate_input_file)); | 599 ASSERT_FALSE(PathExists(alternate_input_file)); |
600 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 600 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
601 | 601 |
(...skipping 11 matching lines...) Expand all Loading... |
613 // "new_windows_in_tabs": true, | 613 // "new_windows_in_tabs": true, |
614 // "max_tabs": 20 | 614 // "max_tabs": 20 |
615 // } | 615 // } |
616 // } | 616 // } |
617 | 617 |
618 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); | 618 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
619 } | 619 } |
620 | 620 |
621 TEST_F(JsonPrefStoreTest, BasicAsyncWithAlternateFile) { | 621 TEST_F(JsonPrefStoreTest, BasicAsyncWithAlternateFile) { |
622 base::FilePath alternate_input_file = | 622 base::FilePath alternate_input_file = |
623 temp_dir_.path().AppendASCII("alternate.json"); | 623 temp_dir_.GetPath().AppendASCII("alternate.json"); |
624 ASSERT_LT(0, base::WriteFile(alternate_input_file, | 624 ASSERT_LT(0, base::WriteFile(alternate_input_file, |
625 kReadJson, arraysize(kReadJson) - 1)); | 625 kReadJson, arraysize(kReadJson) - 1)); |
626 | 626 |
627 // Test that the alternate file is moved to the main file and read as-is from | 627 // Test that the alternate file is moved to the main file and read as-is from |
628 // there even when the read is made asynchronously. | 628 // there even when the read is made asynchronously. |
629 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 629 base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json"); |
630 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 630 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
631 input_file, alternate_input_file, message_loop_.task_runner(), | 631 input_file, alternate_input_file, message_loop_.task_runner(), |
632 std::unique_ptr<PrefFilter>()); | 632 std::unique_ptr<PrefFilter>()); |
633 | 633 |
634 ASSERT_FALSE(PathExists(input_file)); | 634 ASSERT_FALSE(PathExists(input_file)); |
635 ASSERT_TRUE(PathExists(alternate_input_file)); | 635 ASSERT_TRUE(PathExists(alternate_input_file)); |
636 | 636 |
637 { | 637 { |
638 MockPrefStoreObserver mock_observer; | 638 MockPrefStoreObserver mock_observer; |
639 pref_store->AddObserver(&mock_observer); | 639 pref_store->AddObserver(&mock_observer); |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
813 histogram_tester.ExpectBucketCount(histogram_name, 1, 1); | 813 histogram_tester.ExpectBucketCount(histogram_name, 1, 1); |
814 histogram_tester.ExpectBucketCount(histogram_name, 2, 1); | 814 histogram_tester.ExpectBucketCount(histogram_name, 2, 1); |
815 histogram_tester.ExpectBucketCount(histogram_name, 3, 1); | 815 histogram_tester.ExpectBucketCount(histogram_name, 3, 1); |
816 histogram_tester.ExpectTotalCount(histogram_name, 6); | 816 histogram_tester.ExpectTotalCount(histogram_name, 6); |
817 } | 817 } |
818 | 818 |
819 class JsonPrefStoreLossyWriteTest : public JsonPrefStoreTest { | 819 class JsonPrefStoreLossyWriteTest : public JsonPrefStoreTest { |
820 protected: | 820 protected: |
821 void SetUp() override { | 821 void SetUp() override { |
822 JsonPrefStoreTest::SetUp(); | 822 JsonPrefStoreTest::SetUp(); |
823 test_file_ = temp_dir_.path().AppendASCII("test.json"); | 823 test_file_ = temp_dir_.GetPath().AppendASCII("test.json"); |
824 } | 824 } |
825 | 825 |
826 // Creates a JsonPrefStore with the given |file_writer|. | 826 // Creates a JsonPrefStore with the given |file_writer|. |
827 scoped_refptr<JsonPrefStore> CreatePrefStore() { | 827 scoped_refptr<JsonPrefStore> CreatePrefStore() { |
828 return new JsonPrefStore(test_file_, message_loop_.task_runner(), | 828 return new JsonPrefStore(test_file_, message_loop_.task_runner(), |
829 std::unique_ptr<PrefFilter>()); | 829 std::unique_ptr<PrefFilter>()); |
830 } | 830 } |
831 | 831 |
832 // Return the ImportantFileWriter for a given JsonPrefStore. | 832 // Return the ImportantFileWriter for a given JsonPrefStore. |
833 ImportantFileWriter* GetImportantFileWriter( | 833 ImportantFileWriter* GetImportantFileWriter( |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
952 ASSERT_TRUE(file_writer->HasPendingWrite()); | 952 ASSERT_TRUE(file_writer->HasPendingWrite()); |
953 | 953 |
954 // Call CommitPendingWrite and check that the lossy pref is there with the | 954 // Call CommitPendingWrite and check that the lossy pref is there with the |
955 // last value set above. | 955 // last value set above. |
956 pref_store->CommitPendingWrite(); | 956 pref_store->CommitPendingWrite(); |
957 ASSERT_FALSE(file_writer->HasPendingWrite()); | 957 ASSERT_FALSE(file_writer->HasPendingWrite()); |
958 ASSERT_EQ("{\"lossy\":\"lossy\"}", GetTestFileContents()); | 958 ASSERT_EQ("{\"lossy\":\"lossy\"}", GetTestFileContents()); |
959 } | 959 } |
960 | 960 |
961 } // namespace base | 961 } // namespace base |
OLD | NEW |