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_; } | |
Sam McNally
2017/02/24 04:14:54
How about returning a PrefStoreObserverMock&?
tibell
2017/02/27 00:02:54
VerifyAndClearExpectations needs a pointer.
| |
44 PrefStoreClient* store() { return store_.get(); } | |
45 | |
46 bool Initialized() { return store_->initialized_; } | |
Sam McNally
2017/02/24 04:14:53
initialized()
tibell
2017/02/27 00:02:54
Done.
| |
47 void OnPreferencesChanged(const base::DictionaryValue& preferences) { | |
48 observer_ptr_->OnPreferencesChanged(preferences.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 = | |
Sam McNally
2017/02/24 04:14:54
connection
tibell
2017/02/27 00:02:54
I like this naming pattern for InterfacePtrs.
| |
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 } | |
Sam McNally
2017/02/24 04:14:53
store_.reset();
tibell
2017/02/27 00:02:54
Done.
| |
82 | |
83 // Tests that observers are notified upon the completion of initialization, and | |
84 // that values become available. | |
85 TEST_F(PrefStoreClientTest, Initialization) { | |
86 // The store should start out uninitialized if the backing store does. | |
87 EXPECT_FALSE(Initialized()); | |
88 EXPECT_CALL(*observer(), OnInitializationCompleted(_)).Times(0); | |
89 | |
90 testing::Mock::VerifyAndClearExpectations(observer()); | |
91 | |
92 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.
| |
93 const int kValue = 42; | |
94 base::FundamentalValue pref(kValue); | |
95 base::DictionaryValue prefs; | |
96 prefs.Set(key, pref.CreateDeepCopy()); | |
97 | |
98 // PrefStore notifies of PreferencesChanged, completing | |
99 // initialization. | |
100 base::RunLoop loop; | |
101 EXPECT_CALL(*observer(), OnInitializationCompleted(true)); | |
102 EXPECT_CALL(*observer(), OnPrefValueChanged(key)) | |
103 .WillOnce(WithoutArgs(Invoke([&loop]() { loop.Quit(); }))); | |
104 OnInitializationCompleted(); | |
105 OnPreferencesChanged(prefs); | |
106 loop.Run(); | |
107 EXPECT_TRUE(Initialized()); | |
108 | |
109 const base::Value* value = nullptr; | |
110 int actual_value; | |
111 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
| |
112 EXPECT_NE(nullptr, value); | |
Sam McNally
2017/02/24 04:14:54
ASSERT_TRUE(value)
tibell
2017/02/27 00:02:54
Done.
| |
113 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.
| |
114 EXPECT_EQ(kValue, actual_value); | |
115 } | |
116 | |
117 // Test that when initialized with multiple keys, that observers receive a | |
118 // notification for each key. | |
119 TEST_F(PrefStoreClientTest, MultipleKeyInitialization) { | |
120 const std::string key1("hey"); | |
121 const std::string key2("listen"); | |
122 | |
123 EXPECT_FALSE(Initialized()); | |
124 EXPECT_CALL(*observer(), OnInitializationCompleted(_)).Times(0); | |
125 | |
126 testing::Mock::VerifyAndClearExpectations(observer()); | |
127 | |
128 const int kValue = 42; | |
129 base::FundamentalValue pref1(kValue); | |
130 const std::string kStringValue("look"); | |
131 base::StringValue pref2(kStringValue); | |
132 | |
133 base::DictionaryValue prefs; | |
134 prefs.Set(key1, pref1.CreateDeepCopy()); | |
135 prefs.Set(key2, pref2.CreateDeepCopy()); | |
136 | |
137 // The observer should be notified of all keys set. | |
138 base::RunLoop loop; | |
139 EXPECT_CALL(*observer(), OnInitializationCompleted(true)); | |
140 EXPECT_CALL(*observer(), OnPrefValueChanged(key1)); | |
141 EXPECT_CALL(*observer(), OnPrefValueChanged(key2)) | |
142 .WillOnce(WithoutArgs(Invoke([&loop]() { loop.Quit(); }))); | |
143 OnInitializationCompleted(); | |
144 OnPreferencesChanged(prefs); | |
145 loop.Run(); | |
146 EXPECT_TRUE(Initialized()); | |
147 } | |
148 | |
149 // Tests that multiple PrefStore::Observers can be added to a PrefStoreClient | |
150 // and that they are each notified of changes. | |
151 TEST_F(PrefStoreClientTest, MultipleObservers) { | |
152 PrefStoreObserverMock observer2; | |
153 store()->AddObserver(&observer2); | |
154 | |
155 const std::string key("hey"); | |
156 const int kValue = 42; | |
157 base::FundamentalValue pref(kValue); | |
158 base::DictionaryValue prefs; | |
159 prefs.Set(key, pref.CreateDeepCopy()); | |
160 | |
161 // PrefStore notifies of PreferencesChanged, completing | |
162 // initialization. | |
163 base::RunLoop loop; | |
164 EXPECT_CALL(*observer(), OnInitializationCompleted(true)); | |
165 EXPECT_CALL(observer2, OnInitializationCompleted(true)); | |
166 EXPECT_CALL(*observer(), OnPrefValueChanged(key)); | |
167 EXPECT_CALL(observer2, OnPrefValueChanged(key)) | |
168 .WillOnce(WithoutArgs(Invoke([&loop]() { loop.Quit(); }))); | |
169 OnInitializationCompleted(); | |
170 OnPreferencesChanged(prefs); | |
171 loop.Run(); | |
172 | |
173 store()->RemoveObserver(&observer2); | |
174 } | |
175 | |
176 } // namespace prefs | |
OLD | NEW |