OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "services/preferences/public/cpp/pref_store_client.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "base/values.h" |
| 12 #include "services/preferences/public/interfaces/preferences.mojom.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 using testing::Invoke; |
| 17 using testing::WithArg; |
| 18 using testing::WithoutArgs; |
| 19 using testing::_; |
| 20 |
| 21 namespace prefs { |
| 22 |
| 23 namespace { |
| 24 |
| 25 class PrefStoreObserverMock : public PrefStore::Observer { |
| 26 public: |
| 27 MOCK_METHOD1(OnPrefValueChanged, void(const std::string&)); |
| 28 MOCK_METHOD1(OnInitializationCompleted, void(bool succeeded)); |
| 29 }; |
| 30 |
| 31 class PrefStoreConnectorMock : public mojom::PrefStoreConnector { |
| 32 public: |
| 33 MOCK_METHOD1(Connect, void(const ConnectCallback&)); |
| 34 }; |
| 35 |
| 36 } // namespace |
| 37 |
| 38 class PrefStoreClientTest : public testing::Test { |
| 39 public: |
| 40 PrefStoreClientTest() = default; |
| 41 ~PrefStoreClientTest() override {} |
| 42 |
| 43 PrefStoreObserverMock* observer() { return &observer_; } |
| 44 PrefStoreClient* store() { return store_.get(); } |
| 45 |
| 46 bool initialized() { return store_->initialized_; } |
| 47 void OnPrefChanged(const std::string& key, const base::Value& value) { |
| 48 observer_ptr_->OnPrefChanged(key, value.CreateDeepCopy()); |
| 49 } |
| 50 void OnInitializationCompleted() { |
| 51 observer_ptr_->OnInitializationCompleted(true); |
| 52 } |
| 53 |
| 54 // testing::Test: |
| 55 void SetUp() override; |
| 56 void TearDown() override; |
| 57 |
| 58 private: |
| 59 mojom::PrefStoreObserverPtr observer_ptr_; |
| 60 PrefStoreObserverMock observer_; |
| 61 scoped_refptr<PrefStoreClient> store_; |
| 62 |
| 63 // Required by mojo binding code within PrefStoreClient. |
| 64 base::MessageLoop message_loop_; |
| 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(PrefStoreClientTest); |
| 67 }; |
| 68 |
| 69 void PrefStoreClientTest::SetUp() { |
| 70 mojom::PrefStoreConnectionPtr connection_ptr = |
| 71 mojom::PrefStoreConnection::New(); |
| 72 connection_ptr->observer = mojo::MakeRequest(&observer_ptr_); |
| 73 connection_ptr->initial_prefs = base::MakeUnique<base::DictionaryValue>(); |
| 74 connection_ptr->is_initialized = false; |
| 75 store_ = new PrefStoreClient(std::move(connection_ptr)); |
| 76 store_->AddObserver(&observer_); |
| 77 } |
| 78 |
| 79 void PrefStoreClientTest::TearDown() { |
| 80 store_->RemoveObserver(&observer_); |
| 81 store_.reset(); |
| 82 } |
| 83 |
| 84 // Tests that observers are notified upon the completion of initialization, and |
| 85 // that values become available. |
| 86 TEST_F(PrefStoreClientTest, Initialization) { |
| 87 // The store should start out uninitialized if the backing store does. |
| 88 EXPECT_FALSE(initialized()); |
| 89 EXPECT_CALL(*observer(), OnInitializationCompleted(_)).Times(0); |
| 90 |
| 91 testing::Mock::VerifyAndClearExpectations(observer()); |
| 92 |
| 93 const char key[] = "hey"; |
| 94 const int kValue = 42; |
| 95 base::FundamentalValue pref(kValue); |
| 96 |
| 97 // PrefStore notifies of PreferencesChanged, completing |
| 98 // initialization. |
| 99 base::RunLoop loop; |
| 100 EXPECT_CALL(*observer(), OnInitializationCompleted(true)); |
| 101 EXPECT_CALL(*observer(), OnPrefValueChanged(key)) |
| 102 .WillOnce(WithoutArgs(Invoke([&loop]() { loop.Quit(); }))); |
| 103 OnInitializationCompleted(); |
| 104 OnPrefChanged(key, pref); |
| 105 loop.Run(); |
| 106 EXPECT_TRUE(initialized()); |
| 107 |
| 108 const base::Value* value = nullptr; |
| 109 int actual_value; |
| 110 EXPECT_TRUE(store()->GetValue(key, &value)); |
| 111 ASSERT_TRUE(value); |
| 112 EXPECT_TRUE(value->GetAsInteger(&actual_value)); |
| 113 EXPECT_EQ(kValue, actual_value); |
| 114 } |
| 115 |
| 116 // Test that when initialized with multiple keys, that observers receive a |
| 117 // notification for each key. |
| 118 TEST_F(PrefStoreClientTest, MultipleKeyInitialization) { |
| 119 const char key1[] = "hey"; |
| 120 const char key2[] = "listen"; |
| 121 |
| 122 EXPECT_FALSE(initialized()); |
| 123 EXPECT_CALL(*observer(), OnInitializationCompleted(_)).Times(0); |
| 124 |
| 125 testing::Mock::VerifyAndClearExpectations(observer()); |
| 126 |
| 127 const int kValue = 42; |
| 128 base::FundamentalValue pref1(kValue); |
| 129 base::StringValue pref2("look"); |
| 130 |
| 131 base::DictionaryValue prefs; |
| 132 prefs.Set(key1, pref1.CreateDeepCopy()); |
| 133 prefs.Set(key2, pref2.CreateDeepCopy()); |
| 134 |
| 135 // The observer should be notified of all keys set. |
| 136 base::RunLoop loop; |
| 137 EXPECT_CALL(*observer(), OnInitializationCompleted(true)); |
| 138 EXPECT_CALL(*observer(), OnPrefValueChanged(key1)); |
| 139 EXPECT_CALL(*observer(), OnPrefValueChanged(key2)) |
| 140 .WillOnce(WithoutArgs(Invoke([&loop]() { loop.Quit(); }))); |
| 141 OnInitializationCompleted(); |
| 142 OnPrefChanged(key1, pref1); |
| 143 OnPrefChanged(key2, pref2); |
| 144 loop.Run(); |
| 145 EXPECT_TRUE(initialized()); |
| 146 } |
| 147 |
| 148 // Tests that multiple PrefStore::Observers can be added to a PrefStoreClient |
| 149 // and that they are each notified of changes. |
| 150 TEST_F(PrefStoreClientTest, MultipleObservers) { |
| 151 PrefStoreObserverMock observer2; |
| 152 store()->AddObserver(&observer2); |
| 153 |
| 154 const char key[] = "hey"; |
| 155 const int kValue = 42; |
| 156 base::FundamentalValue pref(kValue); |
| 157 |
| 158 // PrefStore notifies of PreferencesChanged, completing |
| 159 // initialization. |
| 160 base::RunLoop loop; |
| 161 EXPECT_CALL(*observer(), OnInitializationCompleted(true)); |
| 162 EXPECT_CALL(observer2, OnInitializationCompleted(true)); |
| 163 EXPECT_CALL(*observer(), OnPrefValueChanged(key)); |
| 164 EXPECT_CALL(observer2, OnPrefValueChanged(key)) |
| 165 .WillOnce(WithoutArgs(Invoke([&loop]() { loop.Quit(); }))); |
| 166 OnInitializationCompleted(); |
| 167 OnPrefChanged(key, pref); |
| 168 loop.Run(); |
| 169 |
| 170 store()->RemoveObserver(&observer2); |
| 171 } |
| 172 |
| 173 } // namespace prefs |
OLD | NEW |