diff options
author | erikwright@chromium.org <erikwright@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-28 16:17:41 +0000 |
---|---|---|
committer | erikwright@chromium.org <erikwright@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-28 16:17:41 +0000 |
commit | b90439bdf1b1abb5a68095f7e15adc7988815611 (patch) | |
tree | c44bea1e0818cad65f376cf709e3773307793ef9 /base | |
parent | 5eda6dc3f3e53bd486269c71d01304d882151ad6 (diff) | |
download | chromium_src-b90439bdf1b1abb5a68095f7e15adc7988815611.zip chromium_src-b90439bdf1b1abb5a68095f7e15adc7988815611.tar.gz chromium_src-b90439bdf1b1abb5a68095f7e15adc7988815611.tar.bz2 |
Refactor TestingPrefStore to make it useful to some tests of load errors and asynchrony.
BUG=349158
R=mnissler@chromium.org
Review URL: https://codereview.chromium.org/210063003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@260150 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/prefs/overlay_user_pref_store_unittest.cc | 68 | ||||
-rw-r--r-- | base/prefs/pref_store_observer_mock.cc | 22 | ||||
-rw-r--r-- | base/prefs/pref_store_observer_mock.h | 20 | ||||
-rw-r--r-- | base/prefs/testing_pref_store.cc | 61 | ||||
-rw-r--r-- | base/prefs/testing_pref_store.h | 28 |
5 files changed, 134 insertions, 65 deletions
diff --git a/base/prefs/overlay_user_pref_store_unittest.cc b/base/prefs/overlay_user_pref_store_unittest.cc index c4e980b..18e9a5c 100644 --- a/base/prefs/overlay_user_pref_store_unittest.cc +++ b/base/prefs/overlay_user_pref_store_unittest.cc @@ -48,47 +48,39 @@ TEST_F(OverlayUserPrefStoreTest, Observer) { overlay_->AddObserver(&obs); // Check that underlay first value is reported. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(overlay_key))).Times(1); underlay_->SetValue(overlay_key, new FundamentalValue(42)); - Mock::VerifyAndClearExpectations(&obs); + obs.VerifyAndResetChangedKey(overlay_key); // Check that underlay overwriting is reported. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(overlay_key))).Times(1); underlay_->SetValue(overlay_key, new FundamentalValue(43)); - Mock::VerifyAndClearExpectations(&obs); + obs.VerifyAndResetChangedKey(overlay_key); // Check that overwriting change in overlay is reported. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(overlay_key))).Times(1); overlay_->SetValue(overlay_key, new FundamentalValue(44)); - Mock::VerifyAndClearExpectations(&obs); + obs.VerifyAndResetChangedKey(overlay_key); // Check that hidden underlay change is not reported. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(overlay_key))).Times(0); underlay_->SetValue(overlay_key, new FundamentalValue(45)); - Mock::VerifyAndClearExpectations(&obs); + EXPECT_TRUE(obs.changed_keys.empty()); // Check that overlay remove is reported. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(overlay_key))).Times(1); overlay_->RemoveValue(overlay_key); - Mock::VerifyAndClearExpectations(&obs); + obs.VerifyAndResetChangedKey(overlay_key); // Check that underlay remove is reported. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(overlay_key))).Times(1); underlay_->RemoveValue(overlay_key); - Mock::VerifyAndClearExpectations(&obs); + obs.VerifyAndResetChangedKey(overlay_key); // Check respecting of silence. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(overlay_key))).Times(0); overlay_->SetValueSilently(overlay_key, new FundamentalValue(46)); - Mock::VerifyAndClearExpectations(&obs); + EXPECT_TRUE(obs.changed_keys.empty()); overlay_->RemoveObserver(&obs); // Check successful unsubscription. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(overlay_key))).Times(0); underlay_->SetValue(overlay_key, new FundamentalValue(47)); overlay_->SetValue(overlay_key, new FundamentalValue(48)); - Mock::VerifyAndClearExpectations(&obs); + EXPECT_TRUE(obs.changed_keys.empty()); } TEST_F(OverlayUserPrefStoreTest, GetAndSet) { @@ -154,23 +146,20 @@ TEST_F(OverlayUserPrefStoreTest, GlobalPref) { const Value* value = NULL; // Check that underlay first value is reported. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(regular_key))).Times(1); underlay_->SetValue(regular_key, new FundamentalValue(42)); - Mock::VerifyAndClearExpectations(&obs); + obs.VerifyAndResetChangedKey(regular_key); // Check that underlay overwriting is reported. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(regular_key))).Times(1); underlay_->SetValue(regular_key, new FundamentalValue(43)); - Mock::VerifyAndClearExpectations(&obs); + obs.VerifyAndResetChangedKey(regular_key); // Check that we get this value from the overlay EXPECT_TRUE(overlay_->GetValue(regular_key, &value)); EXPECT_TRUE(base::FundamentalValue(43).Equals(value)); // Check that overwriting change in overlay is reported. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(regular_key))).Times(1); overlay_->SetValue(regular_key, new FundamentalValue(44)); - Mock::VerifyAndClearExpectations(&obs); + obs.VerifyAndResetChangedKey(regular_key); // Check that we get this value from the overlay and the underlay. EXPECT_TRUE(overlay_->GetValue(regular_key, &value)); @@ -179,26 +168,23 @@ TEST_F(OverlayUserPrefStoreTest, GlobalPref) { EXPECT_TRUE(base::FundamentalValue(44).Equals(value)); // Check that overlay remove is reported. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(regular_key))).Times(1); overlay_->RemoveValue(regular_key); - Mock::VerifyAndClearExpectations(&obs); + obs.VerifyAndResetChangedKey(regular_key); // Check that value was removed from overlay and underlay EXPECT_FALSE(overlay_->GetValue(regular_key, &value)); EXPECT_FALSE(underlay_->GetValue(regular_key, &value)); // Check respecting of silence. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(regular_key))).Times(0); overlay_->SetValueSilently(regular_key, new FundamentalValue(46)); - Mock::VerifyAndClearExpectations(&obs); + EXPECT_TRUE(obs.changed_keys.empty()); overlay_->RemoveObserver(&obs); // Check successful unsubscription. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(regular_key))).Times(0); underlay_->SetValue(regular_key, new FundamentalValue(47)); overlay_->SetValue(regular_key, new FundamentalValue(48)); - Mock::VerifyAndClearExpectations(&obs); + EXPECT_TRUE(obs.changed_keys.empty()); } // Check that names mapping works correctly. @@ -210,14 +196,12 @@ TEST_F(OverlayUserPrefStoreTest, NamesMapping) { // Check that if there is no override in the overlay, changing underlay value // is reported as changing an overlay value. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(mapped_overlay_key))).Times(1); underlay_->SetValue(mapped_underlay_key, new FundamentalValue(42)); - Mock::VerifyAndClearExpectations(&obs); + obs.VerifyAndResetChangedKey(mapped_overlay_key); // Check that underlay overwriting is reported. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(mapped_overlay_key))).Times(1); underlay_->SetValue(mapped_underlay_key, new FundamentalValue(43)); - Mock::VerifyAndClearExpectations(&obs); + obs.VerifyAndResetChangedKey(mapped_overlay_key); // Check that we get this value from the overlay with both keys EXPECT_TRUE(overlay_->GetValue(mapped_overlay_key, &value)); @@ -227,9 +211,8 @@ TEST_F(OverlayUserPrefStoreTest, NamesMapping) { EXPECT_TRUE(base::FundamentalValue(43).Equals(value)); // Check that overwriting change in overlay is reported. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(mapped_overlay_key))).Times(1); overlay_->SetValue(mapped_overlay_key, new FundamentalValue(44)); - Mock::VerifyAndClearExpectations(&obs); + obs.VerifyAndResetChangedKey(mapped_overlay_key); // Check that we get an overriden value from overlay, while reading the // value from underlay still holds an old value. @@ -241,38 +224,31 @@ TEST_F(OverlayUserPrefStoreTest, NamesMapping) { EXPECT_TRUE(base::FundamentalValue(43).Equals(value)); // Check that hidden underlay change is not reported. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(mapped_overlay_key))).Times(0); underlay_->SetValue(mapped_underlay_key, new FundamentalValue(45)); - Mock::VerifyAndClearExpectations(&obs); + EXPECT_TRUE(obs.changed_keys.empty()); // Check that overlay remove is reported. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(mapped_overlay_key))).Times(1); overlay_->RemoveValue(mapped_overlay_key); - Mock::VerifyAndClearExpectations(&obs); + obs.VerifyAndResetChangedKey(mapped_overlay_key); // Check that underlay remove is reported. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(mapped_overlay_key))).Times(1); underlay_->RemoveValue(mapped_underlay_key); - Mock::VerifyAndClearExpectations(&obs); + obs.VerifyAndResetChangedKey(mapped_overlay_key); // Check that value was removed. EXPECT_FALSE(overlay_->GetValue(mapped_overlay_key, &value)); EXPECT_FALSE(overlay_->GetValue(mapped_underlay_key, &value)); // Check respecting of silence. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(mapped_overlay_key))).Times(0); - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(mapped_underlay_key))).Times(0); overlay_->SetValueSilently(mapped_overlay_key, new FundamentalValue(46)); - Mock::VerifyAndClearExpectations(&obs); + EXPECT_TRUE(obs.changed_keys.empty()); overlay_->RemoveObserver(&obs); // Check successful unsubscription. - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(mapped_overlay_key))).Times(0); - EXPECT_CALL(obs, OnPrefValueChanged(StrEq(mapped_underlay_key))).Times(0); underlay_->SetValue(mapped_underlay_key, new FundamentalValue(47)); overlay_->SetValue(mapped_overlay_key, new FundamentalValue(48)); - Mock::VerifyAndClearExpectations(&obs); + EXPECT_TRUE(obs.changed_keys.empty()); } } // namespace base diff --git a/base/prefs/pref_store_observer_mock.cc b/base/prefs/pref_store_observer_mock.cc index 0970e63..f1a31bb 100644 --- a/base/prefs/pref_store_observer_mock.cc +++ b/base/prefs/pref_store_observer_mock.cc @@ -4,6 +4,26 @@ #include "base/prefs/pref_store_observer_mock.h" -PrefStoreObserverMock::PrefStoreObserverMock() {} +#include "testing/gtest/include/gtest/gtest.h" + +PrefStoreObserverMock::PrefStoreObserverMock() + : initialized(false), initialization_success(false) {} PrefStoreObserverMock::~PrefStoreObserverMock() {} + +void PrefStoreObserverMock::VerifyAndResetChangedKey( + const std::string& expected) { + EXPECT_EQ(1u, changed_keys.size()); + if (changed_keys.size() >= 1) + EXPECT_EQ(expected, changed_keys.front()); + changed_keys.clear(); +} + +void PrefStoreObserverMock::OnPrefValueChanged(const std::string& key) { + changed_keys.push_back(key); +} + +void PrefStoreObserverMock::OnInitializationCompleted(bool success) { + initialized = true; + initialization_success = success; +} diff --git a/base/prefs/pref_store_observer_mock.h b/base/prefs/pref_store_observer_mock.h index 8252c3b..594807f 100644 --- a/base/prefs/pref_store_observer_mock.h +++ b/base/prefs/pref_store_observer_mock.h @@ -5,18 +5,28 @@ #ifndef BASE_PREFS_PREF_STORE_OBSERVER_MOCK_H_ #define BASE_PREFS_PREF_STORE_OBSERVER_MOCK_H_ -#include "base/basictypes.h" +#include <string> +#include <vector> + +#include "base/compiler_specific.h" +#include "base/macros.h" #include "base/prefs/pref_store.h" -#include "testing/gmock/include/gmock/gmock.h" -// A gmock-ified implementation of PrefStore::Observer. +// A mock implementation of PrefStore::Observer. class PrefStoreObserverMock : public PrefStore::Observer { public: PrefStoreObserverMock(); virtual ~PrefStoreObserverMock(); - MOCK_METHOD1(OnPrefValueChanged, void(const std::string&)); - MOCK_METHOD1(OnInitializationCompleted, void(bool)); + void VerifyAndResetChangedKey(const std::string& expected); + + // PrefStore::Observer implementation + virtual void OnPrefValueChanged(const std::string& key) OVERRIDE; + virtual void OnInitializationCompleted(bool success) OVERRIDE; + + std::vector<std::string> changed_keys; + bool initialized; + bool initialization_success; // Only valid if |initialized|. private: DISALLOW_COPY_AND_ASSIGN(PrefStoreObserverMock); diff --git a/base/prefs/testing_pref_store.cc b/base/prefs/testing_pref_store.cc index 2f429c9..f20824e 100644 --- a/base/prefs/testing_pref_store.cc +++ b/base/prefs/testing_pref_store.cc @@ -9,8 +9,12 @@ TestingPrefStore::TestingPrefStore() : read_only_(true), - init_complete_(false) { -} + read_success_(true), + read_error_(PersistentPrefStore::PREF_READ_ERROR_NONE), + block_async_read_(false), + pending_async_read_(false), + init_complete_(false), + committed_(true) {} bool TestingPrefStore::GetValue(const std::string& key, const base::Value** value) const { @@ -39,18 +43,23 @@ bool TestingPrefStore::IsInitializationComplete() const { } void TestingPrefStore::SetValue(const std::string& key, base::Value* value) { - if (prefs_.SetValue(key, value)) + if (prefs_.SetValue(key, value)) { + committed_ = false; NotifyPrefValueChanged(key); + } } void TestingPrefStore::SetValueSilently(const std::string& key, base::Value* value) { - prefs_.SetValue(key, value); + if (prefs_.SetValue(key, value)) + committed_ = false; } void TestingPrefStore::RemoveValue(const std::string& key) { - if (prefs_.RemoveValue(key)) + if (prefs_.RemoveValue(key)) { + committed_ = false; NotifyPrefValueChanged(key); + } } bool TestingPrefStore::ReadOnly() const { @@ -58,21 +67,26 @@ bool TestingPrefStore::ReadOnly() const { } PersistentPrefStore::PrefReadError TestingPrefStore::GetReadError() const { - return PersistentPrefStore::PREF_READ_ERROR_NONE; + return read_error_; } PersistentPrefStore::PrefReadError TestingPrefStore::ReadPrefs() { NotifyInitializationCompleted(); - return PersistentPrefStore::PREF_READ_ERROR_NONE; + return read_error_; } -void TestingPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate_raw) { - scoped_ptr<ReadErrorDelegate> error_delegate(error_delegate_raw); - NotifyInitializationCompleted(); +void TestingPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) { + DCHECK(!pending_async_read_); + error_delegate_.reset(error_delegate); + if (block_async_read_) + pending_async_read_ = true; + else + NotifyInitializationCompleted(); } +void TestingPrefStore::CommitPendingWrite() { committed_ = true; } + void TestingPrefStore::SetInitializationCompleted() { - init_complete_ = true; NotifyInitializationCompleted(); } @@ -81,7 +95,12 @@ void TestingPrefStore::NotifyPrefValueChanged(const std::string& key) { } void TestingPrefStore::NotifyInitializationCompleted() { - FOR_EACH_OBSERVER(Observer, observers_, OnInitializationCompleted(true)); + DCHECK(!init_complete_); + init_complete_ = true; + if (read_success_ && read_error_ != PREF_READ_ERROR_NONE && error_delegate_) + error_delegate_->OnError(read_error_); + FOR_EACH_OBSERVER( + Observer, observers_, OnInitializationCompleted(read_success_)); } void TestingPrefStore::ReportValueChanged(const std::string& key) { @@ -126,8 +145,26 @@ bool TestingPrefStore::GetBoolean(const std::string& key, bool* value) const { return stored_value->GetAsBoolean(value); } +void TestingPrefStore::SetBlockAsyncRead(bool block_async_read) { + DCHECK(!init_complete_); + block_async_read_ = block_async_read; + if (pending_async_read_ && !block_async_read_) + NotifyInitializationCompleted(); +} + void TestingPrefStore::set_read_only(bool read_only) { read_only_ = read_only; } +void TestingPrefStore::set_read_success(bool read_success) { + DCHECK(!init_complete_); + read_success_ = read_success; +} + +void TestingPrefStore::set_read_error( + PersistentPrefStore::PrefReadError read_error) { + DCHECK(!init_complete_); + read_error_ = read_error; +} + TestingPrefStore::~TestingPrefStore() {} diff --git a/base/prefs/testing_pref_store.h b/base/prefs/testing_pref_store.h index c6a2b83..785f935 100644 --- a/base/prefs/testing_pref_store.h +++ b/base/prefs/testing_pref_store.h @@ -40,7 +40,7 @@ class TestingPrefStore : public PersistentPrefStore { virtual PrefReadError GetReadError() const OVERRIDE; virtual PersistentPrefStore::PrefReadError ReadPrefs() OVERRIDE; virtual void ReadPrefsAsync(ReadErrorDelegate* error_delegate) OVERRIDE; - virtual void CommitPendingWrite() OVERRIDE {} + virtual void CommitPendingWrite() OVERRIDE; // Marks the store as having completed initialization. void SetInitializationCompleted(); @@ -58,9 +58,18 @@ class TestingPrefStore : public PersistentPrefStore { bool GetInteger(const std::string& key, int* value) const; bool GetBoolean(const std::string& key, bool* value) const; + // Determines whether ReadPrefsAsync completes immediately. Defaults to false + // (non-blocking). To block, invoke this with true (blocking) before the call + // to ReadPrefsAsync. To unblock, invoke again with false (non-blocking) after + // the call to ReadPrefsAsync. + void SetBlockAsyncRead(bool block_async_read); + // Getter and Setter methods for setting and getting the state of the // |TestingPrefStore|. virtual void set_read_only(bool read_only); + void set_read_success(bool read_success); + void set_read_error(PersistentPrefStore::PrefReadError read_error); + bool committed() { return committed_; } protected: virtual ~TestingPrefStore(); @@ -72,9 +81,26 @@ class TestingPrefStore : public PersistentPrefStore { // Flag that indicates if the PrefStore is read-only bool read_only_; + // The result to pass to PrefStore::Observer::OnInitializationCompleted + bool read_success_; + + // The result to return from ReadPrefs or ReadPrefsAsync. + PersistentPrefStore::PrefReadError read_error_; + + // Whether a call to ReadPrefsAsync should block. + bool block_async_read_; + + // Whether there is a pending call to ReadPrefsAsync. + bool pending_async_read_; + // Whether initialization has been completed. bool init_complete_; + // Whether the store contents have been committed to disk since the last + // mutation. + bool committed_; + + scoped_ptr<ReadErrorDelegate> error_delegate_; ObserverList<PrefStore::Observer, true> observers_; DISALLOW_COPY_AND_ASSIGN(TestingPrefStore); |