Index: services/preferences/public/cpp/tests/pref_store_client_unittest.cc |
diff --git a/services/preferences/public/cpp/tests/pref_store_client_unittest.cc b/services/preferences/public/cpp/tests/pref_store_client_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8b929027b16d668882f02173b31d67d7947a2d56 |
--- /dev/null |
+++ b/services/preferences/public/cpp/tests/pref_store_client_unittest.cc |
@@ -0,0 +1,176 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "services/preferences/public/cpp/pref_store_client.h" |
+ |
+#include "base/macros.h" |
+#include "base/memory/ptr_util.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/run_loop.h" |
+#include "base/values.h" |
+#include "services/preferences/public/interfaces/preferences.mojom.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using testing::Invoke; |
+using testing::WithArg; |
+using testing::WithoutArgs; |
+using testing::_; |
+ |
+namespace prefs { |
+ |
+namespace { |
+ |
+class PrefStoreObserverMock : public PrefStore::Observer { |
+ public: |
+ MOCK_METHOD1(OnPrefValueChanged, void(const std::string&)); |
+ MOCK_METHOD1(OnInitializationCompleted, void(bool succeeded)); |
+}; |
+ |
+class PrefStoreConnectorMock : public mojom::PrefStoreConnector { |
+ public: |
+ MOCK_METHOD1(Connect, void(const ConnectCallback&)); |
+}; |
+ |
+} // namespace |
+ |
+class PrefStoreClientTest : public testing::Test { |
+ public: |
+ PrefStoreClientTest() = default; |
+ ~PrefStoreClientTest() override {} |
+ |
+ PrefStoreObserverMock* observer() { return &observer_; } |
Sam McNally
2017/02/24 04:14:54
How about returning a PrefStoreObserverMock&?
tibell
2017/02/27 00:02:54
VerifyAndClearExpectations needs a pointer.
|
+ PrefStoreClient* store() { return store_.get(); } |
+ |
+ bool Initialized() { return store_->initialized_; } |
Sam McNally
2017/02/24 04:14:53
initialized()
tibell
2017/02/27 00:02:54
Done.
|
+ void OnPreferencesChanged(const base::DictionaryValue& preferences) { |
+ observer_ptr_->OnPreferencesChanged(preferences.CreateDeepCopy()); |
+ } |
+ void OnInitializationCompleted() { |
+ observer_ptr_->OnInitializationCompleted(true); |
+ } |
+ |
+ // testing::Test: |
+ void SetUp() override; |
+ void TearDown() override; |
+ |
+ private: |
+ mojom::PrefStoreObserverPtr observer_ptr_; |
+ PrefStoreObserverMock observer_; |
+ scoped_refptr<PrefStoreClient> store_; |
+ |
+ // Required by mojo binding code within PrefStoreClient. |
+ base::MessageLoop message_loop_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(PrefStoreClientTest); |
+}; |
+ |
+void PrefStoreClientTest::SetUp() { |
+ mojom::PrefStoreConnectionPtr connection_ptr = |
Sam McNally
2017/02/24 04:14:54
connection
tibell
2017/02/27 00:02:54
I like this naming pattern for InterfacePtrs.
|
+ mojom::PrefStoreConnection::New(); |
+ connection_ptr->observer = mojo::MakeRequest(&observer_ptr_); |
+ connection_ptr->initial_prefs = base::MakeUnique<base::DictionaryValue>(); |
+ connection_ptr->is_initialized = false; |
+ store_ = new PrefStoreClient(std::move(connection_ptr)); |
+ store_->AddObserver(&observer_); |
+} |
+ |
+void PrefStoreClientTest::TearDown() { |
+ store_->RemoveObserver(&observer_); |
+} |
Sam McNally
2017/02/24 04:14:53
store_.reset();
tibell
2017/02/27 00:02:54
Done.
|
+ |
+// Tests that observers are notified upon the completion of initialization, and |
+// that values become available. |
+TEST_F(PrefStoreClientTest, Initialization) { |
+ // The store should start out uninitialized if the backing store does. |
+ EXPECT_FALSE(Initialized()); |
+ EXPECT_CALL(*observer(), OnInitializationCompleted(_)).Times(0); |
+ |
+ testing::Mock::VerifyAndClearExpectations(observer()); |
+ |
+ const std::string key("hey"); |
Sam McNally
2017/02/24 04:14:53
Can't use const char []?
tibell
2017/02/27 00:02:54
Done.
|
+ const int kValue = 42; |
+ base::FundamentalValue pref(kValue); |
+ base::DictionaryValue prefs; |
+ prefs.Set(key, pref.CreateDeepCopy()); |
+ |
+ // PrefStore notifies of PreferencesChanged, completing |
+ // initialization. |
+ base::RunLoop loop; |
+ EXPECT_CALL(*observer(), OnInitializationCompleted(true)); |
+ EXPECT_CALL(*observer(), OnPrefValueChanged(key)) |
+ .WillOnce(WithoutArgs(Invoke([&loop]() { loop.Quit(); }))); |
+ OnInitializationCompleted(); |
+ OnPreferencesChanged(prefs); |
+ loop.Run(); |
+ EXPECT_TRUE(Initialized()); |
+ |
+ const base::Value* value = nullptr; |
+ int actual_value; |
+ EXPECT_TRUE(store()->GetValue(key, &value)); |
Sam McNally
2017/02/24 04:14:54
ASSERT_TRUE
tibell
2017/02/27 00:02:54
Not needed since the next test won't crash even if
|
+ EXPECT_NE(nullptr, value); |
Sam McNally
2017/02/24 04:14:54
ASSERT_TRUE(value)
tibell
2017/02/27 00:02:54
Done.
|
+ EXPECT_TRUE(value->GetAsInteger(&actual_value)); |
Sam McNally
2017/02/24 04:14:53
ASSERT_TRUE
tibell
2017/02/27 00:02:54
If we get here this comparison can't crash.
|
+ EXPECT_EQ(kValue, actual_value); |
+} |
+ |
+// Test that when initialized with multiple keys, that observers receive a |
+// notification for each key. |
+TEST_F(PrefStoreClientTest, MultipleKeyInitialization) { |
+ const std::string key1("hey"); |
+ const std::string key2("listen"); |
+ |
+ EXPECT_FALSE(Initialized()); |
+ EXPECT_CALL(*observer(), OnInitializationCompleted(_)).Times(0); |
+ |
+ testing::Mock::VerifyAndClearExpectations(observer()); |
+ |
+ const int kValue = 42; |
+ base::FundamentalValue pref1(kValue); |
+ const std::string kStringValue("look"); |
+ base::StringValue pref2(kStringValue); |
+ |
+ base::DictionaryValue prefs; |
+ prefs.Set(key1, pref1.CreateDeepCopy()); |
+ prefs.Set(key2, pref2.CreateDeepCopy()); |
+ |
+ // The observer should be notified of all keys set. |
+ base::RunLoop loop; |
+ EXPECT_CALL(*observer(), OnInitializationCompleted(true)); |
+ EXPECT_CALL(*observer(), OnPrefValueChanged(key1)); |
+ EXPECT_CALL(*observer(), OnPrefValueChanged(key2)) |
+ .WillOnce(WithoutArgs(Invoke([&loop]() { loop.Quit(); }))); |
+ OnInitializationCompleted(); |
+ OnPreferencesChanged(prefs); |
+ loop.Run(); |
+ EXPECT_TRUE(Initialized()); |
+} |
+ |
+// Tests that multiple PrefStore::Observers can be added to a PrefStoreClient |
+// and that they are each notified of changes. |
+TEST_F(PrefStoreClientTest, MultipleObservers) { |
+ PrefStoreObserverMock observer2; |
+ store()->AddObserver(&observer2); |
+ |
+ const std::string key("hey"); |
+ const int kValue = 42; |
+ base::FundamentalValue pref(kValue); |
+ base::DictionaryValue prefs; |
+ prefs.Set(key, pref.CreateDeepCopy()); |
+ |
+ // PrefStore notifies of PreferencesChanged, completing |
+ // initialization. |
+ base::RunLoop loop; |
+ EXPECT_CALL(*observer(), OnInitializationCompleted(true)); |
+ EXPECT_CALL(observer2, OnInitializationCompleted(true)); |
+ EXPECT_CALL(*observer(), OnPrefValueChanged(key)); |
+ EXPECT_CALL(observer2, OnPrefValueChanged(key)) |
+ .WillOnce(WithoutArgs(Invoke([&loop]() { loop.Quit(); }))); |
+ OnInitializationCompleted(); |
+ OnPreferencesChanged(prefs); |
+ loop.Run(); |
+ |
+ store()->RemoveObserver(&observer2); |
+} |
+ |
+} // namespace prefs |