1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/prefs/json_pref_store.h"
#include <stdint.h>
#include <memory>
#include <utility>
#include "base/compiler_specific.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "base/memory/ref_counted.h"
#include "base/metrics/histogram_samples.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/waitable_event.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/threading/thread.h"
#include "base/values.h"
#include "components/prefs/persistent_pref_store_unittest.h"
#include "components/prefs/pref_filter.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
const char kHomePage[] = "homepage";
const char kReadJson[] =
"{\n"
" \"homepage\": \"http://www.cnn.com\",\n"
" \"some_directory\": \"/usr/local/\",\n"
" \"tabs\": {\n"
" \"new_windows_in_tabs\": true,\n"
" \"max_tabs\": 20\n"
" }\n"
"}";
const char kInvalidJson[] = "!@#$%^&";
// Expected output for tests using RunBasicJsonPrefStoreTest().
const char kWriteGolden[] =
"{\"homepage\":\"http://www.cnn.com\","
"\"long_int\":{\"pref\":\"214748364842\"},"
"\"some_directory\":\"/usr/sbin/\","
"\"tabs\":{\"max_tabs\":10,\"new_windows_in_tabs\":false}}";
// A PrefFilter that will intercept all calls to FilterOnLoad() and hold on
// to the |prefs| until explicitly asked to release them.
class InterceptingPrefFilter : public PrefFilter {
public:
InterceptingPrefFilter();
InterceptingPrefFilter(OnWriteCallbackPair callback_pair);
InterceptingPrefFilter(const InterceptingPrefFilter&) = delete;
InterceptingPrefFilter& operator=(const InterceptingPrefFilter&) = delete;
~InterceptingPrefFilter() override;
// PrefFilter implementation:
void FilterOnLoad(PostFilterOnLoadCallback post_filter_on_load_callback,
base::Value::Dict pref_store_contents) override;
void FilterUpdate(const std::string& path) override {}
OnWriteCallbackPair FilterSerializeData(
base::Value::Dict& pref_store_contents) override {
return std::move(on_write_callback_pair_);
}
void OnStoreDeletionFromDisk() override {}
bool has_intercepted_prefs() const { return intercepted_prefs_ != nullptr; }
// Finalize an intercepted read, handing |intercepted_prefs_| back to its
// JsonPrefStore.
void ReleasePrefs();
private:
PostFilterOnLoadCallback post_filter_on_load_callback_;
std::unique_ptr<base::Value::Dict> intercepted_prefs_;
OnWriteCallbackPair on_write_callback_pair_;
};
InterceptingPrefFilter::InterceptingPrefFilter() {}
InterceptingPrefFilter::InterceptingPrefFilter(
OnWriteCallbackPair callback_pair) {
on_write_callback_pair_ = std::move(callback_pair);
}
InterceptingPrefFilter::~InterceptingPrefFilter() {}
void InterceptingPrefFilter::FilterOnLoad(
PostFilterOnLoadCallback post_filter_on_load_callback,
base::Value::Dict pref_store_contents) {
post_filter_on_load_callback_ = std::move(post_filter_on_load_callback);
intercepted_prefs_ =
std::make_unique<base::Value::Dict>(std::move(pref_store_contents));
}
void InterceptingPrefFilter::ReleasePrefs() {
EXPECT_FALSE(post_filter_on_load_callback_.is_null());
std::unique_ptr<base::Value::Dict> prefs = std::move(intercepted_prefs_);
std::move(post_filter_on_load_callback_).Run(std::move(*prefs), false);
}
class MockPrefStoreObserver : public PrefStore::Observer {
public:
MOCK_METHOD1(OnPrefValueChanged, void (const std::string&));
MOCK_METHOD1(OnInitializationCompleted, void (bool));
};
class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate {
public:
MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError));
};
enum class CommitPendingWriteMode {
// Basic mode.
WITHOUT_CALLBACK,
// With reply callback.
WITH_CALLBACK,
// With synchronous notify callback (synchronous after the write -- shouldn't
// require pumping messages to observe).
WITH_SYNCHRONOUS_CALLBACK,
};
base::test::TaskEnvironment::ThreadPoolExecutionMode GetExecutionMode(
CommitPendingWriteMode commit_mode) {
switch (commit_mode) {
case CommitPendingWriteMode::WITHOUT_CALLBACK:
[[fallthrough]];
case CommitPendingWriteMode::WITH_CALLBACK:
return base::test::TaskEnvironment::ThreadPoolExecutionMode::QUEUED;
case CommitPendingWriteMode::WITH_SYNCHRONOUS_CALLBACK:
// Synchronous callbacks require async tasks to run on their own.
return base::test::TaskEnvironment::ThreadPoolExecutionMode::ASYNC;
}
}
void CommitPendingWrite(JsonPrefStore* pref_store,
CommitPendingWriteMode commit_pending_write_mode,
base::test::TaskEnvironment* task_environment) {
switch (commit_pending_write_mode) {
case CommitPendingWriteMode::WITHOUT_CALLBACK: {
pref_store->CommitPendingWrite();
task_environment->RunUntilIdle();
break;
}
case CommitPendingWriteMode::WITH_CALLBACK: {
TestCommitPendingWriteWithCallback(pref_store, task_environment);
break;
}
case CommitPendingWriteMode::WITH_SYNCHRONOUS_CALLBACK: {
base::WaitableEvent written;
pref_store->CommitPendingWrite(
base::OnceClosure(),
base::BindOnce(&base::WaitableEvent::Signal, Unretained(&written)));
written.Wait();
break;
}
}
}
class JsonPrefStoreTest
: public testing::TestWithParam<CommitPendingWriteMode> {
public:
JsonPrefStoreTest()
: task_environment_(base::test::TaskEnvironment::MainThreadType::DEFAULT,
GetExecutionMode(GetParam())) {}
JsonPrefStoreTest(const JsonPrefStoreTest&) = delete;
JsonPrefStoreTest& operator=(const JsonPrefStoreTest&) = delete;
protected:
void SetUp() override {
commit_pending_write_mode_ = GetParam();
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
}
// The path to temporary directory used to contain the test operations.
base::ScopedTempDir temp_dir_;
base::test::TaskEnvironment task_environment_;
CommitPendingWriteMode commit_pending_write_mode_;
};
} // namespace
// Test fallback behavior for a nonexistent file.
TEST_P(JsonPrefStoreTest, NonExistentFile) {
base::FilePath bogus_input_file = temp_dir_.GetPath().AppendASCII("read.txt");
ASSERT_FALSE(PathExists(bogus_input_file));
auto pref_store = base::MakeRefCounted<JsonPrefStore>(bogus_input_file);
EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
pref_store->ReadPrefs());
EXPECT_FALSE(pref_store->ReadOnly());
EXPECT_EQ(0u, pref_store->get_writer().previous_data_size());
}
// Test fallback behavior for an invalid file.
TEST_P(JsonPrefStoreTest, InvalidFile) {
base::FilePath invalid_file = temp_dir_.GetPath().AppendASCII("invalid.json");
ASSERT_TRUE(base::WriteFile(invalid_file, kInvalidJson));
auto pref_store = base::MakeRefCounted<JsonPrefStore>(invalid_file);
EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE,
pref_store->ReadPrefs());
EXPECT_FALSE(pref_store->ReadOnly());
// The file should have been moved aside.
EXPECT_FALSE(PathExists(invalid_file));
base::FilePath moved_aside = temp_dir_.GetPath().AppendASCII("invalid.bad");
EXPECT_TRUE(PathExists(moved_aside));
std::string moved_aside_contents;
ASSERT_TRUE(base::ReadFileToString(moved_aside, &moved_aside_contents));
EXPECT_EQ(kInvalidJson, moved_aside_contents);
}
// This function is used to avoid code duplication while testing synchronous
// and asynchronous version of the JsonPrefStore loading. It validates that the
// given output file's contents matches kWriteGolden.
void RunBasicJsonPrefStoreTest(JsonPrefStore* pref_store,
const base::FilePath& output_file,
CommitPendingWriteMode commit_pending_write_mode,
base::test::TaskEnvironment* task_environment) {
const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs";
const char kMaxTabs[] = "tabs.max_tabs";
const char kLongIntPref[] = "long_int.pref";
std::string cnn("http://www.cnn.com");
const Value* actual;
EXPECT_TRUE(pref_store->GetValue(kHomePage, &actual));
EXPECT_TRUE(actual->is_string());
EXPECT_EQ(cnn, actual->GetString());
const char kSomeDirectory[] = "some_directory";
EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual));
EXPECT_TRUE(actual->is_string());
EXPECT_EQ("/usr/local/", actual->GetString());
base::FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/"));
pref_store->SetValue(kSomeDirectory, Value(some_path.AsUTF8Unsafe()),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual));
EXPECT_TRUE(actual->is_string());
EXPECT_EQ(some_path.AsUTF8Unsafe(), actual->GetString());
// Test reading some other data types from sub-dictionaries.
EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual));
EXPECT_TRUE(actual->is_bool());
EXPECT_TRUE(actual->GetBool());
pref_store->SetValue(kNewWindowsInTabs, Value(false),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual));
EXPECT_TRUE(actual->is_bool());
EXPECT_FALSE(actual->GetBool());
EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual));
ASSERT_TRUE(actual->is_int());
EXPECT_EQ(20, actual->GetInt());
pref_store->SetValue(kMaxTabs, Value(10),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual));
ASSERT_TRUE(actual->is_int());
EXPECT_EQ(10, actual->GetInt());
pref_store->SetValue(kLongIntPref,
Value(base::NumberToString(214748364842LL)),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual));
EXPECT_TRUE(actual->is_string());
int64_t value;
base::StringToInt64(actual->GetString(), &value);
EXPECT_EQ(214748364842LL, value);
// Serialize and compare to expected output.
CommitPendingWrite(pref_store, commit_pending_write_mode, task_environment);
std::string output_contents;
ASSERT_TRUE(base::ReadFileToString(output_file, &output_contents));
EXPECT_EQ(kWriteGolden, output_contents);
ASSERT_TRUE(base::DeleteFile(output_file));
}
TEST_P(JsonPrefStoreTest, Basic) {
base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json");
ASSERT_TRUE(base::WriteFile(input_file, kReadJson));
// Test that the persistent value can be loaded.
ASSERT_TRUE(PathExists(input_file));
auto pref_store = base::MakeRefCounted<JsonPrefStore>(input_file);
ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
EXPECT_FALSE(pref_store->ReadOnly());
EXPECT_TRUE(pref_store->IsInitializationComplete());
EXPECT_GT(pref_store->get_writer().previous_data_size(), 0u);
// The JSON file looks like this:
// {
// "homepage": "http://www.cnn.com",
// "some_directory": "/usr/local/",
// "tabs": {
// "new_windows_in_tabs": true,
// "max_tabs": 20
// }
// }
RunBasicJsonPrefStoreTest(pref_store.get(), input_file,
commit_pending_write_mode_, &task_environment_);
}
TEST_P(JsonPrefStoreTest, BasicAsync) {
base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json");
ASSERT_TRUE(base::WriteFile(input_file, kReadJson));
// Test that the persistent value can be loaded.
auto pref_store = base::MakeRefCounted<JsonPrefStore>(input_file);
{
MockPrefStoreObserver mock_observer;
pref_store->AddObserver(&mock_observer);
MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate;
pref_store->ReadPrefsAsync(mock_error_delegate);
EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
EXPECT_CALL(*mock_error_delegate,
OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0);
task_environment_.RunUntilIdle();
pref_store->RemoveObserver(&mock_observer);
EXPECT_FALSE(pref_store->ReadOnly());
EXPECT_TRUE(pref_store->IsInitializationComplete());
EXPECT_GT(pref_store->get_writer().previous_data_size(), 0u);
}
// The JSON file looks like this:
// {
// "homepage": "http://www.cnn.com",
// "some_directory": "/usr/local/",
// "tabs": {
// "new_windows_in_tabs": true,
// "max_tabs": 20
// }
// }
RunBasicJsonPrefStoreTest(pref_store.get(), input_file,
commit_pending_write_mode_, &task_environment_);
}
TEST_P(JsonPrefStoreTest, PreserveEmptyValues) {
FilePath pref_file = temp_dir_.GetPath().AppendASCII("empty_values.json");
auto pref_store = base::MakeRefCounted<JsonPrefStore>(pref_file);
// Set some keys with empty values.
pref_store->SetValue("list", base::Value(base::Value::List()),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
pref_store->SetValue("dict", base::Value(base::Value::Dict()),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
// Write to file.
CommitPendingWrite(pref_store.get(), commit_pending_write_mode_,
&task_environment_);
// Reload.
pref_store = base::MakeRefCounted<JsonPrefStore>(pref_file);
ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
ASSERT_FALSE(pref_store->ReadOnly());
// Check values.
const Value* result = nullptr;
EXPECT_TRUE(pref_store->GetValue("list", &result));
EXPECT_EQ(Value::List(), *result);
EXPECT_TRUE(pref_store->GetValue("dict", &result));
EXPECT_EQ(Value::Dict(), *result);
}
// This test is just documenting some potentially non-obvious behavior. It
// shouldn't be taken as normative.
TEST_P(JsonPrefStoreTest, RemoveClearsEmptyParent) {
FilePath pref_file = temp_dir_.GetPath().AppendASCII("empty_values.json");
auto pref_store = base::MakeRefCounted<JsonPrefStore>(pref_file);
base::Value::Dict dict;
dict.Set("key", "value");
pref_store->SetValue("dict", base::Value(std::move(dict)),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
pref_store->RemoveValue("dict.key",
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
const base::Value* retrieved_dict = nullptr;
bool has_dict = pref_store->GetValue("dict", &retrieved_dict);
EXPECT_FALSE(has_dict);
}
// Tests asynchronous reading of the file when there is no file.
TEST_P(JsonPrefStoreTest, AsyncNonExistingFile) {
base::FilePath bogus_input_file = temp_dir_.GetPath().AppendASCII("read.txt");
ASSERT_FALSE(PathExists(bogus_input_file));
auto pref_store = base::MakeRefCounted<JsonPrefStore>(bogus_input_file);
MockPrefStoreObserver mock_observer;
pref_store->AddObserver(&mock_observer);
MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate;
pref_store->ReadPrefsAsync(mock_error_delegate);
EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
EXPECT_CALL(*mock_error_delegate,
OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1);
task_environment_.RunUntilIdle();
pref_store->RemoveObserver(&mock_observer);
EXPECT_FALSE(pref_store->ReadOnly());
}
TEST_P(JsonPrefStoreTest, ReadWithInterceptor) {
base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json");
ASSERT_TRUE(base::WriteFile(input_file, kReadJson));
std::unique_ptr<InterceptingPrefFilter> intercepting_pref_filter(
new InterceptingPrefFilter());
InterceptingPrefFilter* raw_intercepting_pref_filter_ =
intercepting_pref_filter.get();
auto pref_store = base::MakeRefCounted<JsonPrefStore>(
input_file, std::move(intercepting_pref_filter));
ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE,
pref_store->ReadPrefs());
EXPECT_FALSE(pref_store->ReadOnly());
// The store shouldn't be considered initialized until the interceptor
// returns.
EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs());
EXPECT_FALSE(pref_store->IsInitializationComplete());
EXPECT_FALSE(pref_store->GetValue(kHomePage, nullptr));
raw_intercepting_pref_filter_->ReleasePrefs();
EXPECT_FALSE(raw_intercepting_pref_filter_->has_intercepted_prefs());
EXPECT_TRUE(pref_store->IsInitializationComplete());
EXPECT_TRUE(pref_store->GetValue(kHomePage, nullptr));
// The JSON file looks like this:
// {
// "homepage": "http://www.cnn.com",
// "some_directory": "/usr/local/",
// "tabs": {
// "new_windows_in_tabs": true,
// "max_tabs": 20
// }
// }
RunBasicJsonPrefStoreTest(pref_store.get(), input_file,
commit_pending_write_mode_, &task_environment_);
}
TEST_P(JsonPrefStoreTest, ReadAsyncWithInterceptor) {
base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json");
ASSERT_TRUE(base::WriteFile(input_file, kReadJson));
std::unique_ptr<InterceptingPrefFilter> intercepting_pref_filter(
new InterceptingPrefFilter());
InterceptingPrefFilter* raw_intercepting_pref_filter_ =
intercepting_pref_filter.get();
auto pref_store = base::MakeRefCounted<JsonPrefStore>(
input_file, std::move(intercepting_pref_filter));
MockPrefStoreObserver mock_observer;
pref_store->AddObserver(&mock_observer);
// Ownership of the |mock_error_delegate| is handed to the |pref_store| below.
MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate;
{
pref_store->ReadPrefsAsync(mock_error_delegate);
EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(0);
// EXPECT_CALL(*mock_error_delegate,
// OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0);
task_environment_.RunUntilIdle();
EXPECT_FALSE(pref_store->ReadOnly());
EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs());
EXPECT_FALSE(pref_store->IsInitializationComplete());
EXPECT_FALSE(pref_store->GetValue(kHomePage, nullptr));
}
{
EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
// EXPECT_CALL(*mock_error_delegate,
// OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0);
raw_intercepting_pref_filter_->ReleasePrefs();
EXPECT_FALSE(pref_store->ReadOnly());
EXPECT_FALSE(raw_intercepting_pref_filter_->has_intercepted_prefs());
EXPECT_TRUE(pref_store->IsInitializationComplete());
EXPECT_TRUE(pref_store->GetValue(kHomePage, nullptr));
}
pref_store->RemoveObserver(&mock_observer);
// The JSON file looks like this:
// {
// "homepage": "http://www.cnn.com",
// "some_directory": "/usr/local/",
// "tabs": {
// "new_windows_in_tabs": true,
// "max_tabs": 20
// }
// }
RunBasicJsonPrefStoreTest(pref_store.get(), input_file,
commit_pending_write_mode_, &task_environment_);
}
TEST_P(JsonPrefStoreTest, RemoveValuesByPrefix) {
FilePath pref_file = temp_dir_.GetPath().AppendASCII("empty.json");
auto pref_store = base::MakeRefCounted<JsonPrefStore>(pref_file);
const Value* value;
const std::string prefix = "pref";
const std::string subpref_name1 = "pref.a";
const std::string subpref_name2 = "pref.b";
const std::string other_name = "other";
pref_store->SetValue(subpref_name1, base::Value(42),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
pref_store->SetValue(subpref_name2, base::Value(42),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
pref_store->SetValue(other_name, base::Value(42),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
pref_store->RemoveValuesByPrefixSilently(prefix);
EXPECT_FALSE(pref_store->GetValue(subpref_name1, &value));
EXPECT_FALSE(pref_store->GetValue(subpref_name2, &value));
EXPECT_TRUE(pref_store->GetValue(other_name, &value));
}
INSTANTIATE_TEST_SUITE_P(
JsonPrefStoreTestVariations,
JsonPrefStoreTest,
::testing::Values(CommitPendingWriteMode::WITHOUT_CALLBACK,
CommitPendingWriteMode::WITH_CALLBACK,
CommitPendingWriteMode::WITH_SYNCHRONOUS_CALLBACK));
class JsonPrefStoreLossyWriteTest : public JsonPrefStoreTest {
public:
JsonPrefStoreLossyWriteTest() = default;
JsonPrefStoreLossyWriteTest(const JsonPrefStoreLossyWriteTest&) = delete;
JsonPrefStoreLossyWriteTest& operator=(const JsonPrefStoreLossyWriteTest&) =
delete;
protected:
void SetUp() override {
JsonPrefStoreTest::SetUp();
test_file_ = temp_dir_.GetPath().AppendASCII("test.json");
}
scoped_refptr<JsonPrefStore> CreatePrefStore() {
return base::MakeRefCounted<JsonPrefStore>(test_file_);
}
// Return the ImportantFileWriter for a given JsonPrefStore.
ImportantFileWriter* GetImportantFileWriter(JsonPrefStore* pref_store) {
return &(pref_store->writer_);
}
// Get the contents of kTestFile. Pumps the message loop before returning the
// result.
std::string GetTestFileContents() {
task_environment_.RunUntilIdle();
std::string file_contents;
ReadFileToString(test_file_, &file_contents);
return file_contents;
}
private:
base::FilePath test_file_;
};
TEST_P(JsonPrefStoreLossyWriteTest, LossyWriteBasic) {
scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore();
ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get());
// Set a normal pref and check that it gets scheduled to be written.
ASSERT_FALSE(file_writer->HasPendingWrite());
pref_store->SetValue("normal", base::Value("normal"),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
ASSERT_TRUE(file_writer->HasPendingWrite());
file_writer->DoScheduledWrite();
ASSERT_EQ("{\"normal\":\"normal\"}", GetTestFileContents());
ASSERT_FALSE(file_writer->HasPendingWrite());
// Set a lossy pref and check that it is not scheduled to be written.
// SetValue/RemoveValue.
pref_store->SetValue("lossy", base::Value("lossy"),
WriteablePrefStore::LOSSY_PREF_WRITE_FLAG);
ASSERT_FALSE(file_writer->HasPendingWrite());
pref_store->RemoveValue("lossy", WriteablePrefStore::LOSSY_PREF_WRITE_FLAG);
ASSERT_FALSE(file_writer->HasPendingWrite());
// SetValueSilently/RemoveValueSilently.
pref_store->SetValueSilently("lossy", base::Value("lossy"),
WriteablePrefStore::LOSSY_PREF_WRITE_FLAG);
ASSERT_FALSE(file_writer->HasPendingWrite());
pref_store->RemoveValueSilently("lossy",
WriteablePrefStore::LOSSY_PREF_WRITE_FLAG);
ASSERT_FALSE(file_writer->HasPendingWrite());
// ReportValueChanged.
pref_store->SetValue("lossy", base::Value("lossy"),
WriteablePrefStore::LOSSY_PREF_WRITE_FLAG);
ASSERT_FALSE(file_writer->HasPendingWrite());
pref_store->ReportValueChanged("lossy",
WriteablePrefStore::LOSSY_PREF_WRITE_FLAG);
ASSERT_FALSE(file_writer->HasPendingWrite());
// Call CommitPendingWrite and check that the lossy pref and the normal pref
// are there with the last values set above.
pref_store->CommitPendingWrite(base::OnceClosure());
ASSERT_FALSE(file_writer->HasPendingWrite());
ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}",
GetTestFileContents());
}
TEST_P(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossyFirst) {
scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore();
ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get());
// Set a lossy pref and check that it is not scheduled to be written.
ASSERT_FALSE(file_writer->HasPendingWrite());
pref_store->SetValue("lossy", base::Value("lossy"),
WriteablePrefStore::LOSSY_PREF_WRITE_FLAG);
ASSERT_FALSE(file_writer->HasPendingWrite());
// Set a normal pref and check that it is scheduled to be written.
pref_store->SetValue("normal", base::Value("normal"),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
ASSERT_TRUE(file_writer->HasPendingWrite());
// Call DoScheduledWrite and check both prefs get written.
file_writer->DoScheduledWrite();
ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}",
GetTestFileContents());
ASSERT_FALSE(file_writer->HasPendingWrite());
}
TEST_P(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossySecond) {
scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore();
ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get());
// Set a normal pref and check that it is scheduled to be written.
ASSERT_FALSE(file_writer->HasPendingWrite());
pref_store->SetValue("normal", base::Value("normal"),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
ASSERT_TRUE(file_writer->HasPendingWrite());
// Set a lossy pref and check that the write is still scheduled.
pref_store->SetValue("lossy", base::Value("lossy"),
WriteablePrefStore::LOSSY_PREF_WRITE_FLAG);
ASSERT_TRUE(file_writer->HasPendingWrite());
// Call DoScheduledWrite and check both prefs get written.
file_writer->DoScheduledWrite();
ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}",
GetTestFileContents());
ASSERT_FALSE(file_writer->HasPendingWrite());
}
TEST_P(JsonPrefStoreLossyWriteTest, ScheduleLossyWrite) {
scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore();
ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get());
// Set a lossy pref and check that it is not scheduled to be written.
pref_store->SetValue("lossy", base::Value("lossy"),
WriteablePrefStore::LOSSY_PREF_WRITE_FLAG);
ASSERT_FALSE(file_writer->HasPendingWrite());
// Schedule pending lossy writes and check that it is scheduled.
pref_store->SchedulePendingLossyWrites();
ASSERT_TRUE(file_writer->HasPendingWrite());
// Call CommitPendingWrite and check that the lossy pref is there with the
// last value set above.
pref_store->CommitPendingWrite(base::OnceClosure());
ASSERT_FALSE(file_writer->HasPendingWrite());
ASSERT_EQ("{\"lossy\":\"lossy\"}", GetTestFileContents());
}
INSTANTIATE_TEST_SUITE_P(
JsonPrefStoreLossyWriteTestVariations,
JsonPrefStoreLossyWriteTest,
::testing::Values(CommitPendingWriteMode::WITHOUT_CALLBACK,
CommitPendingWriteMode::WITH_CALLBACK,
CommitPendingWriteMode::WITH_SYNCHRONOUS_CALLBACK));
class SuccessfulWriteReplyObserver {
public:
SuccessfulWriteReplyObserver() = default;
SuccessfulWriteReplyObserver(const SuccessfulWriteReplyObserver&) = delete;
SuccessfulWriteReplyObserver& operator=(const SuccessfulWriteReplyObserver&) =
delete;
// Returns true if a successful write was observed via on_successful_write()
// and resets the observation state to false regardless.
bool GetAndResetObservationState() {
bool was_successful_write_observed = successful_write_reply_observed_;
successful_write_reply_observed_ = false;
return was_successful_write_observed;
}
// Register OnWrite() to be called on the next write of |json_pref_store|.
void ObserveNextWriteCallback(JsonPrefStore* json_pref_store);
void OnSuccessfulWrite() {
EXPECT_FALSE(successful_write_reply_observed_);
successful_write_reply_observed_ = true;
}
private:
bool successful_write_reply_observed_ = false;
};
void SuccessfulWriteReplyObserver::ObserveNextWriteCallback(
JsonPrefStore* json_pref_store) {
json_pref_store->RegisterOnNextSuccessfulWriteReply(
base::BindOnce(&SuccessfulWriteReplyObserver::OnSuccessfulWrite,
base::Unretained(this)));
}
enum WriteCallbackObservationState {
NOT_CALLED,
CALLED_WITH_ERROR,
CALLED_WITH_SUCCESS,
};
class WriteCallbacksObserver {
public:
WriteCallbacksObserver() = default;
WriteCallbacksObserver(const WriteCallbacksObserver&) = delete;
WriteCallbacksObserver& operator=(const WriteCallbacksObserver&) = delete;
// Register OnWrite() to be called on the next write of |json_pref_store|.
void ObserveNextWriteCallback(JsonPrefStore* json_pref_store);
// Returns whether OnPreWrite() was called, and resets the observation state
// to false.
bool GetAndResetPreWriteObservationState();
// Returns the |WriteCallbackObservationState| which was observed, then resets
// it to |NOT_CALLED|.
WriteCallbackObservationState GetAndResetPostWriteObservationState();
JsonPrefStore::OnWriteCallbackPair GetCallbackPair() {
return std::make_pair(base::BindOnce(&WriteCallbacksObserver::OnPreWrite,
base::Unretained(this)),
base::BindOnce(&WriteCallbacksObserver::OnPostWrite,
base::Unretained(this)));
}
void OnPreWrite() {
EXPECT_FALSE(pre_write_called_);
pre_write_called_ = true;
}
void OnPostWrite(bool success) {
EXPECT_EQ(NOT_CALLED, post_write_observation_state_);
post_write_observation_state_ =
success ? CALLED_WITH_SUCCESS : CALLED_WITH_ERROR;
}
private:
bool pre_write_called_ = false;
WriteCallbackObservationState post_write_observation_state_ = NOT_CALLED;
};
void WriteCallbacksObserver::ObserveNextWriteCallback(JsonPrefStore* writer) {
writer->RegisterOnNextWriteSynchronousCallbacks(GetCallbackPair());
}
bool WriteCallbacksObserver::GetAndResetPreWriteObservationState() {
bool observation_state = pre_write_called_;
pre_write_called_ = false;
return observation_state;
}
WriteCallbackObservationState
WriteCallbacksObserver::GetAndResetPostWriteObservationState() {
WriteCallbackObservationState state = post_write_observation_state_;
pre_write_called_ = false;
post_write_observation_state_ = NOT_CALLED;
return state;
}
class JsonPrefStoreCallbackTest : public testing::Test {
public:
JsonPrefStoreCallbackTest() = default;
JsonPrefStoreCallbackTest(const JsonPrefStoreCallbackTest&) = delete;
JsonPrefStoreCallbackTest& operator=(const JsonPrefStoreCallbackTest&) =
delete;
protected:
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
test_file_ = temp_dir_.GetPath().AppendASCII("test.json");
}
scoped_refptr<JsonPrefStore> CreatePrefStore() {
return base::MakeRefCounted<JsonPrefStore>(test_file_);
}
// Return the ImportantFileWriter for a given JsonPrefStore.
ImportantFileWriter* GetImportantFileWriter(JsonPrefStore* pref_store) {
return &(pref_store->writer_);
}
void TriggerFakeWriteForCallback(JsonPrefStore* pref_store, bool success) {
JsonPrefStore::PostWriteCallback(
base::BindOnce(&JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback,
pref_store->AsWeakPtr()),
base::BindOnce(&WriteCallbacksObserver::OnPostWrite,
base::Unretained(&write_callback_observer_)),
base::SequencedTaskRunner::GetCurrentDefault(), success);
}
SuccessfulWriteReplyObserver successful_write_reply_observer_;
WriteCallbacksObserver write_callback_observer_;
protected:
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::MainThreadType::DEFAULT,
base::test::TaskEnvironment::ThreadPoolExecutionMode::QUEUED};
base::ScopedTempDir temp_dir_;
private:
base::FilePath test_file_;
};
TEST_F(JsonPrefStoreCallbackTest, TestSerializeDataCallbacks) {
base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json");
ASSERT_TRUE(base::WriteFile(input_file, kReadJson));
std::unique_ptr<InterceptingPrefFilter> intercepting_pref_filter(
new InterceptingPrefFilter(write_callback_observer_.GetCallbackPair()));
auto pref_store = base::MakeRefCounted<JsonPrefStore>(
input_file, std::move(intercepting_pref_filter));
ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get());
EXPECT_EQ(NOT_CALLED,
write_callback_observer_.GetAndResetPostWriteObservationState());
pref_store->SetValue("normal", base::Value("normal"),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
file_writer->DoScheduledWrite();
// The observer should not be invoked right away.
EXPECT_FALSE(write_callback_observer_.GetAndResetPreWriteObservationState());
EXPECT_EQ(NOT_CALLED,
write_callback_observer_.GetAndResetPostWriteObservationState());
task_environment_.RunUntilIdle();
EXPECT_TRUE(write_callback_observer_.GetAndResetPreWriteObservationState());
EXPECT_EQ(CALLED_WITH_SUCCESS,
write_callback_observer_.GetAndResetPostWriteObservationState());
}
TEST_F(JsonPrefStoreCallbackTest, TestPostWriteCallbacks) {
scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore();
ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get());
// Test RegisterOnNextWriteSynchronousCallbacks after
// RegisterOnNextSuccessfulWriteReply.
successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get());
write_callback_observer_.ObserveNextWriteCallback(pref_store.get());
file_writer->WriteNow("foo");
task_environment_.RunUntilIdle();
EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState());
EXPECT_TRUE(write_callback_observer_.GetAndResetPreWriteObservationState());
EXPECT_EQ(CALLED_WITH_SUCCESS,
write_callback_observer_.GetAndResetPostWriteObservationState());
// Test RegisterOnNextSuccessfulWriteReply after
// RegisterOnNextWriteSynchronousCallbacks.
successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get());
write_callback_observer_.ObserveNextWriteCallback(pref_store.get());
file_writer->WriteNow("foo");
task_environment_.RunUntilIdle();
EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState());
EXPECT_TRUE(write_callback_observer_.GetAndResetPreWriteObservationState());
EXPECT_EQ(CALLED_WITH_SUCCESS,
write_callback_observer_.GetAndResetPostWriteObservationState());
// Test RegisterOnNextSuccessfulWriteReply only.
successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get());
file_writer->WriteNow("foo");
task_environment_.RunUntilIdle();
EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState());
EXPECT_FALSE(write_callback_observer_.GetAndResetPreWriteObservationState());
EXPECT_EQ(NOT_CALLED,
write_callback_observer_.GetAndResetPostWriteObservationState());
// Test RegisterOnNextWriteSynchronousCallbacks only.
write_callback_observer_.ObserveNextWriteCallback(pref_store.get());
file_writer->WriteNow("foo");
task_environment_.RunUntilIdle();
EXPECT_FALSE(successful_write_reply_observer_.GetAndResetObservationState());
EXPECT_TRUE(write_callback_observer_.GetAndResetPreWriteObservationState());
EXPECT_EQ(CALLED_WITH_SUCCESS,
write_callback_observer_.GetAndResetPostWriteObservationState());
}
TEST_F(JsonPrefStoreCallbackTest, TestPostWriteCallbacksWithFakeFailure) {
scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore();
// Confirm that the observers are invoked.
successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get());
TriggerFakeWriteForCallback(pref_store.get(), true);
task_environment_.RunUntilIdle();
EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState());
EXPECT_EQ(CALLED_WITH_SUCCESS,
write_callback_observer_.GetAndResetPostWriteObservationState());
// Confirm that the observation states were reset.
EXPECT_FALSE(successful_write_reply_observer_.GetAndResetObservationState());
EXPECT_EQ(NOT_CALLED,
write_callback_observer_.GetAndResetPostWriteObservationState());
// Confirm that re-installing the observers works for another write.
successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get());
TriggerFakeWriteForCallback(pref_store.get(), true);
task_environment_.RunUntilIdle();
EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState());
EXPECT_EQ(CALLED_WITH_SUCCESS,
write_callback_observer_.GetAndResetPostWriteObservationState());
// Confirm that the successful observer is not invoked by an unsuccessful
// write, and that the synchronous observer is invoked.
successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get());
TriggerFakeWriteForCallback(pref_store.get(), false);
task_environment_.RunUntilIdle();
EXPECT_FALSE(successful_write_reply_observer_.GetAndResetObservationState());
EXPECT_EQ(CALLED_WITH_ERROR,
write_callback_observer_.GetAndResetPostWriteObservationState());
// Do a real write, and confirm that the successful observer was invoked after
// being set by |PostWriteCallback| by the last TriggerFakeWriteCallback.
ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get());
file_writer->WriteNow("foo");
task_environment_.RunUntilIdle();
EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState());
EXPECT_EQ(NOT_CALLED,
write_callback_observer_.GetAndResetPostWriteObservationState());
}
TEST_F(JsonPrefStoreCallbackTest, TestPostWriteCallbacksDuringProfileDeath) {
// Create a JsonPrefStore and attach observers to it, then delete it by making
// it go out of scope to simulate profile switch or Chrome shutdown.
{
scoped_refptr<JsonPrefStore> soon_out_of_scope_pref_store =
CreatePrefStore();
ImportantFileWriter* file_writer =
GetImportantFileWriter(soon_out_of_scope_pref_store.get());
successful_write_reply_observer_.ObserveNextWriteCallback(
soon_out_of_scope_pref_store.get());
write_callback_observer_.ObserveNextWriteCallback(
soon_out_of_scope_pref_store.get());
file_writer->WriteNow("foo");
}
task_environment_.RunUntilIdle();
EXPECT_FALSE(successful_write_reply_observer_.GetAndResetObservationState());
EXPECT_TRUE(write_callback_observer_.GetAndResetPreWriteObservationState());
EXPECT_EQ(CALLED_WITH_SUCCESS,
write_callback_observer_.GetAndResetPostWriteObservationState());
}
} // namespace base
|