OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 <string> | |
bartfab (slow)
2015/05/15 13:28:09
Nit: Not used.
binjin
2015/05/15 14:50:41
Done.
| |
6 | |
7 #include "base/macros.h" | |
bartfab (slow)
2015/05/15 13:28:09
Nit: Already included by header.
binjin
2015/05/15 14:50:41
Done.
| |
8 #include "base/memory/ref_counted.h" | |
bartfab (slow)
2015/05/15 13:28:08
Nit: Already included by header.
binjin
2015/05/15 14:50:39
Done.
| |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "chrome/browser/invalidation/fake_invalidation_service.h" | |
12 #include "chrome/browser/policy/cloud/remote_commands_invalidator_base.h" | |
bartfab (slow)
2015/05/15 13:28:10
Nit: Make this the first included, followed by a b
binjin
2015/05/15 14:50:41
Done.
| |
13 #include "components/invalidation/invalidation.h" | |
14 #include "components/invalidation/invalidation_util.h" | |
15 #include "policy/proto/device_management_backend.pb.h" | |
bartfab (slow)
2015/05/15 13:28:09
Nit: Already included by header.
binjin
2015/05/15 14:50:41
Done.
| |
16 #include "testing/gmock/include/gmock/gmock.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 namespace em = enterprise_management; | |
20 | |
21 using ::testing::InSequence; | |
bartfab (slow)
2015/05/15 13:28:10
Nit: Not actually needed.
binjin
2015/05/15 14:50:41
I think it's used later.
| |
22 using ::testing::Mock; | |
23 using ::testing::StrictMock; | |
24 | |
25 namespace policy { | |
26 | |
27 class MockRemoteCommandInvalidator : public RemoteCommandsInvalidatorBase { | |
28 public: | |
29 MockRemoteCommandInvalidator( | |
30 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | |
bartfab (slow)
2015/05/15 13:28:09
Nit: #include "base/sequenced_task_runner.h" (scop
binjin
2015/05/15 14:50:40
Done.
| |
31 : RemoteCommandsInvalidatorBase(task_runner) {} | |
32 ~MockRemoteCommandInvalidator() override {} | |
bartfab (slow)
2015/05/15 13:28:08
Nit: No need to override the destructor here at al
binjin
2015/05/15 14:50:40
Done.
| |
33 | |
34 MOCK_METHOD0(OnInitialize, void()); | |
35 MOCK_METHOD0(OnShutdown, void()); | |
36 MOCK_METHOD0(OnStart, void()); | |
37 MOCK_METHOD0(OnStop, void()); | |
38 MOCK_METHOD0(DoRemoteCommandsFetch, void()); | |
39 | |
40 void SetInvalidationObjectID(const invalidation::ObjectId& object_id) { | |
41 scoped_ptr<em::PolicyData> policy_data(new em::PolicyData()); | |
bartfab (slow)
2015/05/15 13:28:09
Why do you not just use a stack-allocated |em::Pol
binjin
2015/05/15 14:50:40
Done.
| |
42 policy_data->set_command_invalidation_source(object_id.source()); | |
43 policy_data->set_command_invalidation_name(object_id.name()); | |
44 ReloadPolicyData(policy_data.get()); | |
45 } | |
46 | |
47 void ClearInvalidationObjectID() { | |
48 scoped_ptr<em::PolicyData> policy_data(new em::PolicyData()); | |
bartfab (slow)
2015/05/15 13:28:10
As above, why not just allcoate the |em::PolicyDat
binjin
2015/05/15 14:50:41
Done.
| |
49 ReloadPolicyData(policy_data.get()); | |
50 } | |
51 | |
52 private: | |
53 DISALLOW_COPY_AND_ASSIGN(MockRemoteCommandInvalidator); | |
54 }; | |
55 | |
56 class RemoteCommandsInvalidatorBaseTest : public testing::Test { | |
57 public: | |
58 RemoteCommandsInvalidatorBaseTest() | |
59 : kTestingObjectId1(123456, "abcdef"), | |
60 kTestingObjectId2(654321, "defabc"), | |
61 invalidation_service_(new invalidation::FakeInvalidationService), | |
bartfab (slow)
2015/05/15 13:28:10
Nit: Why does this need to be a scoped_ptr? Why ca
binjin
2015/05/15 14:50:40
Done.
| |
62 invalidator_(new StrictMock<MockRemoteCommandInvalidator>( | |
bartfab (slow)
2015/05/15 13:28:09
Nit: Why does this need to be a scoped_ptr? Why ca
binjin
2015/05/15 14:50:40
Done.
| |
63 loop_.message_loop_proxy())) {} | |
bartfab (slow)
2015/05/15 13:28:08
Nit 1: MessageLoopProxy is deprecated. Use base::T
binjin
2015/05/15 14:50:41
1: I can't use ThreadTaskRunnerHandle. It's my pre
bartfab (slow)
2015/05/15 15:37:12
You are not passing the MessageLoopProxy you grab
| |
64 | |
65 void EnableInvalidationService() { | |
66 invalidation_service_->SetInvalidatorState(syncer::INVALIDATIONS_ENABLED); | |
bartfab (slow)
2015/05/15 13:28:09
Nit: #include "components/invalidation/invalidator
binjin
2015/05/15 14:50:41
Done.
| |
67 } | |
68 | |
69 void DisableInvalidationService() { | |
70 invalidation_service_->SetInvalidatorState( | |
71 syncer::TRANSIENT_INVALIDATION_ERROR); | |
72 } | |
73 | |
74 syncer::Invalidation FireInvalidation( | |
75 const invalidation::ObjectId& object_id) { | |
76 auto invalidation = syncer::Invalidation::InitUnknownVersion(object_id); | |
bartfab (slow)
2015/05/15 13:28:10
Nit 1: Do not use |auto| here. We only use it in t
binjin
2015/05/15 14:50:39
Done.
| |
77 invalidation_service_->EmitInvalidationForTest(invalidation); | |
78 return invalidation; | |
79 } | |
80 | |
81 bool IsInvalidationSent(const syncer::Invalidation& invalidation) { | |
82 return !invalidation_service_->GetMockAckHandler()->IsUnsent(invalidation); | |
bartfab (slow)
2015/05/15 13:28:09
Nit: #include "components/invalidation/mock_ack_ha
binjin
2015/05/15 14:50:41
Done.
| |
83 } | |
84 | |
85 bool IsInvalidationAcknowledged(const syncer::Invalidation& invalidation) { | |
86 return invalidation_service_->GetMockAckHandler()->IsAcknowledged( | |
87 invalidation); | |
88 } | |
89 | |
90 bool IsInvalidatorRegistered() { | |
91 return !invalidation_service_->invalidator_registrar() | |
bartfab (slow)
2015/05/15 13:28:09
Nit: #include "components/invalidation/invalidator
binjin
2015/05/15 14:50:41
Done.
| |
92 .GetRegisteredIds(invalidator_.get()) | |
93 .empty(); | |
94 } | |
95 | |
96 void VerifyExpectations() { | |
97 Mock::VerifyAndClearExpectations(invalidator_.get()); | |
98 } | |
99 | |
100 protected: | |
101 // Initialize and start the invalidator. | |
102 void InitializeAndStart() { | |
103 EXPECT_CALL(*invalidator_, OnInitialize()).Times(1); | |
104 invalidator_->Initialize(invalidation_service_.get()); | |
105 EXPECT_CALL(*invalidator_, OnStart()).Times(1); | |
bartfab (slow)
2015/05/15 13:28:09
GMock does not allow mixing EXPECT_CALL() with inv
binjin
2015/05/15 14:50:41
Done.
| |
106 invalidator_->Start(); | |
107 | |
108 VerifyExpectations(); | |
109 } | |
110 | |
111 // Stop and shutdown the invalidator. | |
112 void StopAndShutdown() { | |
113 EXPECT_CALL(*invalidator_, OnStop()).Times(1); | |
114 EXPECT_CALL(*invalidator_, OnShutdown()).Times(1); | |
115 invalidator_->Shutdown(); | |
116 | |
117 VerifyExpectations(); | |
118 } | |
119 | |
120 // Fire an invalidation to verify that invalidaion is not working. | |
bartfab (slow)
2015/05/15 13:28:08
Nit: s/invalidaion/invalidation/
binjin
2015/05/15 14:50:40
Done.
| |
121 void VerifiesInvalidationDisabled(const invalidation::ObjectId& object_id) { | |
bartfab (slow)
2015/05/15 13:28:10
Nit: s/Verifies/Verify/
binjin
2015/05/15 14:50:40
Done.
| |
122 VerifyExpectations(); | |
bartfab (slow)
2015/05/15 13:28:09
Why do you need to do this? Does any of the tests
binjin
2015/05/15 14:50:41
Done.
| |
123 | |
124 EXPECT_FALSE(invalidator_->invalidations_enabled()); | |
125 | |
126 syncer::Invalidation inv = FireInvalidation(object_id); | |
bartfab (slow)
2015/05/15 13:28:08
Nit 1: Avoid abbreviaitons like |inv|.
Nit 2: cons
binjin
2015/05/15 14:50:41
Done.
| |
127 | |
128 loop_.RunUntilIdle(); | |
bartfab (slow)
2015/05/15 13:28:09
Nit: Here and throughout the file: Never spin the
binjin
2015/05/15 14:50:40
Done.
| |
129 EXPECT_FALSE(IsInvalidationSent(inv)); | |
130 VerifyExpectations(); | |
bartfab (slow)
2015/05/15 13:28:09
What expectations are you verifying here?
binjin
2015/05/15 14:50:40
Done.
| |
131 } | |
132 | |
133 // Fire an invalidation to verify that invalidaion works. | |
bartfab (slow)
2015/05/15 13:28:09
Nit: s/invalidaion/invalidation/
binjin
2015/05/15 14:50:41
Done.
| |
134 void VerifiesInvalidationEnabled(const invalidation::ObjectId& object_id) { | |
bartfab (slow)
2015/05/15 13:28:09
Nit: s/Verifies/Verify/
binjin
2015/05/15 14:50:40
Done.
| |
135 VerifyExpectations(); | |
bartfab (slow)
2015/05/15 13:28:09
Why do you need to do this? Does any of the tests
binjin
2015/05/15 14:50:41
Done.
| |
136 | |
137 EXPECT_TRUE(invalidator_->invalidations_enabled()); | |
138 | |
139 EXPECT_CALL(*invalidator_, DoRemoteCommandsFetch()).Times(1); | |
140 syncer::Invalidation inv = FireInvalidation(object_id); | |
bartfab (slow)
2015/05/15 13:28:09
Nit 1: Avoid abbreviaitons like |inv|.
Nit 2: cons
binjin
2015/05/15 14:50:41
Done.
| |
141 | |
142 loop_.RunUntilIdle(); | |
143 EXPECT_TRUE(IsInvalidationSent(inv)); | |
144 EXPECT_TRUE(IsInvalidationAcknowledged(inv)); | |
145 VerifyExpectations(); | |
146 } | |
147 | |
148 const invalidation::ObjectId kTestingObjectId1; | |
149 const invalidation::ObjectId kTestingObjectId2; | |
150 | |
151 base::MessageLoop loop_; | |
152 | |
153 scoped_ptr<invalidation::FakeInvalidationService> invalidation_service_; | |
154 scoped_ptr<MockRemoteCommandInvalidator> invalidator_; | |
155 | |
156 private: | |
157 DISALLOW_COPY_AND_ASSIGN(RemoteCommandsInvalidatorBaseTest); | |
158 }; | |
159 | |
160 // Verifies that only the fired invalidations will be received. | |
161 TEST_F(RemoteCommandsInvalidatorBaseTest, FiredInvalidation) { | |
162 InSequence dummy; | |
bartfab (slow)
2015/05/15 13:28:10
Nit: There is no need for a sequence in this test.
binjin
2015/05/15 14:50:40
Done.
| |
163 | |
164 InitializeAndStart(); | |
165 | |
166 // Invalidator won't work at this point. | |
167 EXPECT_FALSE(invalidator_->invalidations_enabled()); | |
168 | |
169 // Load the policy data, it should work now. | |
170 invalidator_->SetInvalidationObjectID(kTestingObjectId1); | |
171 EXPECT_TRUE(invalidator_->invalidations_enabled()); | |
172 | |
173 // Disable invalidation service, it should stop working. | |
174 DisableInvalidationService(); | |
bartfab (slow)
2015/05/15 13:28:08
Nit: The disabling and re-enabling is already bein
binjin
2015/05/15 14:50:40
Done. Note that the invalidations_enabled() is act
| |
175 EXPECT_FALSE(invalidator_->invalidations_enabled()); | |
176 | |
177 // Re-enable invalidation service. | |
178 EnableInvalidationService(); | |
179 EXPECT_TRUE(invalidator_->invalidations_enabled()); | |
180 | |
181 // No invalidation will be received if no invalidation is fired. | |
182 VerifyExpectations(); | |
bartfab (slow)
2015/05/15 13:28:10
Nit 1: Should you not spin the loop before verifyi
binjin
2015/05/15 14:50:41
Done.
| |
183 | |
184 // Fire an invalidation with different object id, no invalidation will be | |
185 // received. | |
186 syncer::Invalidation inv1 = FireInvalidation(kTestingObjectId2); | |
bartfab (slow)
2015/05/15 13:28:10
Nit 1: Avoid abbreviaitons like |inv1|.
Nit 2: con
binjin
2015/05/15 14:50:40
Done.
| |
187 | |
188 loop_.RunUntilIdle(); | |
189 EXPECT_FALSE(IsInvalidationSent(inv1)); | |
190 VerifyExpectations(); | |
191 | |
192 // Fire the invalidation, it should be acknowledged and trigger a remote | |
193 // commands fetch. | |
194 EXPECT_CALL(*invalidator_, DoRemoteCommandsFetch()).Times(1); | |
195 syncer::Invalidation inv2 = FireInvalidation(kTestingObjectId1); | |
196 | |
197 loop_.RunUntilIdle(); | |
198 EXPECT_TRUE(IsInvalidationSent(inv2)); | |
199 EXPECT_TRUE(IsInvalidationAcknowledged(inv2)); | |
200 VerifyExpectations(); | |
201 | |
202 StopAndShutdown(); | |
203 } | |
204 | |
205 // Verifies that no invalidation will be received when invalidator is shutdown. | |
206 TEST_F(RemoteCommandsInvalidatorBaseTest, ShutDown) { | |
207 EXPECT_FALSE(invalidator_->invalidations_enabled()); | |
208 FireInvalidation(kTestingObjectId1); | |
209 | |
210 loop_.RunUntilIdle(); | |
211 EXPECT_FALSE(invalidator_->invalidations_enabled()); | |
212 } | |
213 | |
214 // Verifies that no invalidation will be received when invalidator is stopped. | |
215 TEST_F(RemoteCommandsInvalidatorBaseTest, Stopped) { | |
216 EXPECT_CALL(*invalidator_, OnInitialize()).Times(1); | |
217 invalidator_->Initialize(invalidation_service_.get()); | |
218 VerifyExpectations(); | |
219 | |
220 EXPECT_FALSE(invalidator_->invalidations_enabled()); | |
221 FireInvalidation(kTestingObjectId2); | |
222 | |
223 loop_.RunUntilIdle(); | |
224 EXPECT_FALSE(invalidator_->invalidations_enabled()); | |
225 | |
226 EXPECT_CALL(*invalidator_, OnShutdown()).Times(1); | |
227 invalidator_->Shutdown(); | |
228 } | |
229 | |
230 // Verifies that stated/stopped state changes works as expected. | |
231 TEST_F(RemoteCommandsInvalidatorBaseTest, StartedStateChange) { | |
232 InSequence dummy; | |
233 | |
234 InitializeAndStart(); | |
235 | |
236 // Invalidator requires object id to work. | |
237 VerifiesInvalidationDisabled(kTestingObjectId1); | |
238 invalidator_->SetInvalidationObjectID(kTestingObjectId1); | |
239 VerifiesInvalidationEnabled(kTestingObjectId1); | |
240 | |
241 // Stop and restart invalidator. | |
242 EXPECT_CALL(*invalidator_, OnStop()).Times(1); | |
243 invalidator_->Stop(); | |
244 VerifyExpectations(); | |
245 | |
246 VerifiesInvalidationDisabled(kTestingObjectId1); | |
247 | |
248 EXPECT_CALL(*invalidator_, OnStart()).Times(1); | |
249 invalidator_->Start(); | |
250 VerifyExpectations(); | |
251 | |
252 // Invalidator requires object id to work. | |
253 invalidator_->SetInvalidationObjectID(kTestingObjectId1); | |
254 VerifiesInvalidationEnabled(kTestingObjectId1); | |
255 | |
256 StopAndShutdown(); | |
257 } | |
258 | |
259 // Verifies that registered state changes works as expected. | |
bartfab (slow)
2015/05/15 13:28:08
Nit: s/works/work/
binjin
2015/05/15 14:50:40
Done.
| |
260 TEST_F(RemoteCommandsInvalidatorBaseTest, RegistedStateChange) { | |
261 InSequence dummy; | |
262 | |
263 InitializeAndStart(); | |
264 | |
265 invalidator_->SetInvalidationObjectID(kTestingObjectId1); | |
266 VerifiesInvalidationEnabled(kTestingObjectId1); | |
267 | |
268 invalidator_->SetInvalidationObjectID(kTestingObjectId2); | |
269 VerifiesInvalidationEnabled(kTestingObjectId2); | |
bartfab (slow)
2015/05/15 13:28:09
Nit: Maybe double-check that |kTestingObjectId1| i
binjin
2015/05/15 14:50:41
Done.
| |
270 | |
271 invalidator_->SetInvalidationObjectID(kTestingObjectId1); | |
272 VerifiesInvalidationEnabled(kTestingObjectId1); | |
bartfab (slow)
2015/05/15 13:28:09
Nit: Maybe double-check that |kTestingObjectId2| i
binjin
2015/05/15 14:50:41
Done.
| |
273 | |
274 invalidator_->ClearInvalidationObjectID(); | |
275 VerifiesInvalidationDisabled(kTestingObjectId1); | |
276 | |
277 invalidator_->SetInvalidationObjectID(kTestingObjectId2); | |
278 VerifiesInvalidationEnabled(kTestingObjectId2); | |
bartfab (slow)
2015/05/15 13:28:09
Nit: Maybe double-check that |kTestingObjectId1| i
binjin
2015/05/15 14:50:40
Done.
| |
279 | |
280 StopAndShutdown(); | |
281 } | |
282 | |
283 // Verifies that invalidation service enabled state change works as expected. | |
284 TEST_F(RemoteCommandsInvalidatorBaseTest, | |
285 InvalidationServiceEnabledStateChanged) { | |
286 InSequence dummy; | |
287 | |
288 InitializeAndStart(); | |
289 | |
290 invalidator_->SetInvalidationObjectID(kTestingObjectId1); | |
291 VerifiesInvalidationEnabled(kTestingObjectId1); | |
292 | |
293 DisableInvalidationService(); | |
294 VerifiesInvalidationDisabled(kTestingObjectId2); | |
bartfab (slow)
2015/05/15 13:28:09
Here and below: Why are you sometimes testing the
binjin
2015/05/15 14:50:40
They are meant to be "EXPECT_FALSE(invalidator_.in
| |
295 | |
296 EnableInvalidationService(); | |
297 VerifiesInvalidationEnabled(kTestingObjectId1); | |
298 | |
299 EnableInvalidationService(); | |
300 VerifiesInvalidationEnabled(kTestingObjectId1); | |
301 | |
302 DisableInvalidationService(); | |
303 VerifiesInvalidationDisabled(kTestingObjectId2); | |
304 | |
305 DisableInvalidationService(); | |
306 VerifiesInvalidationDisabled(kTestingObjectId2); | |
307 | |
308 StopAndShutdown(); | |
309 } | |
310 | |
311 } // namespace policy | |
OLD | NEW |