| Index: base/prefs/json_pref_store_unittest.cc
|
| diff --git a/base/prefs/json_pref_store_unittest.cc b/base/prefs/json_pref_store_unittest.cc
|
| index f4e1e5190dd903bb4c69ffab8f0d003106f987c0..53d9b05c224c61c39d9ec4bc1769a2b816877f2a 100644
|
| --- a/base/prefs/json_pref_store_unittest.cc
|
| +++ b/base/prefs/json_pref_store_unittest.cc
|
| @@ -4,6 +4,7 @@
|
|
|
| #include "base/prefs/json_pref_store.h"
|
|
|
| +#include "base/bind.h"
|
| #include "base/file_util.h"
|
| #include "base/files/scoped_temp_dir.h"
|
| #include "base/memory/ref_counted.h"
|
| @@ -25,6 +26,53 @@ namespace {
|
|
|
| const char kHomePage[] = "homepage";
|
|
|
| +class TestFileReadInterceptor {
|
| + public:
|
| + TestFileReadInterceptor();
|
| + ~TestFileReadInterceptor();
|
| +
|
| + // Tells this TestFileReadInterceptor to intercept the next file read on
|
| + // |json_pref_store|.
|
| + void InterceptNextFileReadOnPrefStore(JsonPrefStore* json_pref_store);
|
| +
|
| + bool has_intercepted_prefs() const { return intercepted_prefs_ != NULL; }
|
| +
|
| + // Finalize an intercepted read, handing |intercept_prefs_| back to its
|
| + // JsonPrefStore.
|
| + void FinalizeInterceptedRead();
|
| +
|
| + private:
|
| + void OnFileRead(
|
| + scoped_ptr<base::DictionaryValue> prefs,
|
| + const JsonPrefStore::FinalizePrefsReadCallback& finalize_prefs_read);
|
| +
|
| + scoped_ptr<base::DictionaryValue> intercepted_prefs_;
|
| + JsonPrefStore::FinalizePrefsReadCallback finalize_prefs_read_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(TestFileReadInterceptor);
|
| +};
|
| +
|
| +TestFileReadInterceptor::TestFileReadInterceptor() {}
|
| +TestFileReadInterceptor::~TestFileReadInterceptor() {}
|
| +
|
| +void TestFileReadInterceptor::InterceptNextFileReadOnPrefStore(
|
| + JsonPrefStore* json_pref_store) {
|
| + json_pref_store->InterceptNextFileRead(
|
| + base::Bind(&TestFileReadInterceptor::OnFileRead, base::Unretained(this)));
|
| +}
|
| +
|
| +void TestFileReadInterceptor::FinalizeInterceptedRead() {
|
| + EXPECT_FALSE(finalize_prefs_read_.is_null());
|
| + finalize_prefs_read_.Run(intercepted_prefs_.Pass(), false);
|
| +}
|
| +
|
| +void TestFileReadInterceptor::OnFileRead(
|
| + scoped_ptr<base::DictionaryValue> prefs,
|
| + const JsonPrefStore::FinalizePrefsReadCallback& finalize_prefs_read) {
|
| + intercepted_prefs_ = prefs.Pass();
|
| + finalize_prefs_read_ = finalize_prefs_read;
|
| +}
|
| +
|
| class MockPrefStoreObserver : public PrefStore::Observer {
|
| public:
|
| MOCK_METHOD1(OnPrefValueChanged, void (const std::string&));
|
| @@ -48,6 +96,8 @@ class JsonPrefStoreTest : public testing::Test {
|
| ASSERT_TRUE(PathExists(data_dir_));
|
| }
|
|
|
| + TestFileReadInterceptor file_read_interceptor_;
|
| +
|
| // The path to temporary directory used to contain the test operations.
|
| base::ScopedTempDir temp_dir_;
|
| // The path to the directory where the test data is stored.
|
| @@ -157,7 +207,7 @@ void RunBasicJsonPrefStoreTest(JsonPrefStore* pref_store,
|
|
|
| TEST_F(JsonPrefStoreTest, Basic) {
|
| ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"),
|
| - temp_dir_.path().AppendASCII("write.json")));
|
| + temp_dir_.path().AppendASCII("write.json")));
|
|
|
| // Test that the persistent value can be loaded.
|
| base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
|
| @@ -167,7 +217,9 @@ TEST_F(JsonPrefStoreTest, Basic) {
|
| message_loop_.message_loop_proxy().get(),
|
| scoped_ptr<PrefFilter>());
|
| ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
|
| - ASSERT_FALSE(pref_store->ReadOnly());
|
| + EXPECT_FALSE(pref_store->ReadOnly());
|
| + EXPECT_FALSE(file_read_interceptor_.has_intercepted_prefs());
|
| + EXPECT_TRUE(pref_store->IsInitializationComplete());
|
|
|
| // The JSON file looks like this:
|
| // {
|
| @@ -185,7 +237,7 @@ TEST_F(JsonPrefStoreTest, Basic) {
|
|
|
| TEST_F(JsonPrefStoreTest, BasicAsync) {
|
| ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"),
|
| - temp_dir_.path().AppendASCII("write.json")));
|
| + temp_dir_.path().AppendASCII("write.json")));
|
|
|
| // Test that the persistent value can be loaded.
|
| base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
|
| @@ -208,7 +260,9 @@ TEST_F(JsonPrefStoreTest, BasicAsync) {
|
| RunLoop().RunUntilIdle();
|
| pref_store->RemoveObserver(&mock_observer);
|
|
|
| - ASSERT_FALSE(pref_store->ReadOnly());
|
| + EXPECT_FALSE(pref_store->ReadOnly());
|
| + EXPECT_FALSE(file_read_interceptor_.has_intercepted_prefs());
|
| + EXPECT_TRUE(pref_store->IsInitializationComplete());
|
| }
|
|
|
| // The JSON file looks like this:
|
| @@ -301,4 +355,147 @@ TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) {
|
| EXPECT_FALSE(pref_store->ReadOnly());
|
| }
|
|
|
| +TEST_F(JsonPrefStoreTest, ReadWithInterceptor) {
|
| + ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"),
|
| + temp_dir_.path().AppendASCII("write.json")));
|
| +
|
| + // Test that the persistent value can be loaded.
|
| + base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
|
| + ASSERT_TRUE(PathExists(input_file));
|
| + scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore(
|
| + input_file,
|
| + message_loop_.message_loop_proxy().get(),
|
| + scoped_ptr<PrefFilter>());
|
| + file_read_interceptor_.InterceptNextFileReadOnPrefStore(pref_store);
|
| +
|
| + ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
|
| + EXPECT_FALSE(pref_store->ReadOnly());
|
| +
|
| + // The store shouldn't be considered initialized until the interceptor
|
| + // returns.
|
| + EXPECT_TRUE(file_read_interceptor_.has_intercepted_prefs());
|
| + EXPECT_FALSE(pref_store->IsInitializationComplete());
|
| + EXPECT_FALSE(pref_store->GetValue(kHomePage, NULL));
|
| +
|
| + file_read_interceptor_.FinalizeInterceptedRead();
|
| +
|
| + EXPECT_FALSE(file_read_interceptor_.has_intercepted_prefs());
|
| + EXPECT_TRUE(pref_store->IsInitializationComplete());
|
| + EXPECT_TRUE(pref_store->GetValue(kHomePage, NULL));
|
| +
|
| + // 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, data_dir_.AppendASCII("write.golden.json"));
|
| +}
|
| +
|
| +TEST_F(JsonPrefStoreTest, ReadAsyncWithInterceptor) {
|
| + ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"),
|
| + temp_dir_.path().AppendASCII("write.json")));
|
| +
|
| + // Test that the persistent value can be loaded.
|
| + base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
|
| + ASSERT_TRUE(PathExists(input_file));
|
| + scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore(
|
| + input_file,
|
| + message_loop_.message_loop_proxy().get(),
|
| + scoped_ptr<PrefFilter>());
|
| + file_read_interceptor_.InterceptNextFileReadOnPrefStore(pref_store);
|
| +
|
| + 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);
|
| + RunLoop().RunUntilIdle();
|
| +
|
| + EXPECT_FALSE(pref_store->ReadOnly());
|
| + EXPECT_TRUE(file_read_interceptor_.has_intercepted_prefs());
|
| + EXPECT_FALSE(pref_store->IsInitializationComplete());
|
| + EXPECT_FALSE(pref_store->GetValue(kHomePage, NULL));
|
| + }
|
| +
|
| + {
|
| + EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
|
| + EXPECT_CALL(*mock_error_delegate,
|
| + OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0);
|
| +
|
| + file_read_interceptor_.FinalizeInterceptedRead();
|
| +
|
| + EXPECT_FALSE(pref_store->ReadOnly());
|
| + EXPECT_FALSE(file_read_interceptor_.has_intercepted_prefs());
|
| + EXPECT_TRUE(pref_store->IsInitializationComplete());
|
| + EXPECT_TRUE(pref_store->GetValue(kHomePage, NULL));
|
| + }
|
| +
|
| + 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, data_dir_.AppendASCII("write.golden.json"));
|
| +}
|
| +
|
| +TEST_F(JsonPrefStoreTest, MultipleReadsResultInASingleIntercept) {
|
| + ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"),
|
| + temp_dir_.path().AppendASCII("write.json")));
|
| +
|
| + // Test that the persistent value can be loaded.
|
| + base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
|
| + ASSERT_TRUE(PathExists(input_file));
|
| + scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore(
|
| + input_file,
|
| + message_loop_.message_loop_proxy().get(),
|
| + scoped_ptr<PrefFilter>());
|
| + file_read_interceptor_.InterceptNextFileReadOnPrefStore(pref_store);
|
| +
|
| + ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
|
| + EXPECT_TRUE(file_read_interceptor_.has_intercepted_prefs());
|
| + file_read_interceptor_.FinalizeInterceptedRead();
|
| + EXPECT_FALSE(file_read_interceptor_.has_intercepted_prefs());
|
| +
|
| + // Read again; expect no intercepts.
|
| + ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
|
| + EXPECT_FALSE(pref_store->ReadOnly());
|
| + EXPECT_FALSE(file_read_interceptor_.has_intercepted_prefs());
|
| + EXPECT_TRUE(pref_store->IsInitializationComplete());
|
| + EXPECT_TRUE(pref_store->GetValue(kHomePage, NULL));
|
| +
|
| + // 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, data_dir_.AppendASCII("write.golden.json"));
|
| +}
|
| +
|
| } // namespace base
|
|
|