1 // Copyright (c) 2012 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.
5 #include "sync/notifier/sync_system_resources.h"
10 #include "base/bind_helpers.h"
11 #include "base/callback.h"
12 #include "base/message_loop/message_loop.h"
14 #include "google/cacheinvalidation/include/types.h"
15 #include "jingle/notifier/listener/fake_push_client.h"
16 #include "sync/notifier/push_client_channel.h"
17 #include "sync/notifier/state_writer.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
25 using ::testing::SaveArg
;
27 class MockStateWriter
: public StateWriter
{
29 MOCK_METHOD1(WriteState
, void(const std::string
&));
34 MOCK_CONST_METHOD0(Run
, void(void));
35 base::Closure
* CreateClosure() {
36 return new base::Closure(
37 base::Bind(&MockClosure::Run
, base::Unretained(this)));
41 class MockStorageCallback
{
43 MOCK_CONST_METHOD1(Run
, void(invalidation::Status
));
44 base::Callback
<void(invalidation::Status
)>* CreateCallback() {
45 return new base::Callback
<void(invalidation::Status
)>(
46 base::Bind(&MockStorageCallback::Run
, base::Unretained(this)));
50 class SyncSystemResourcesTest
: public testing::Test
{
52 SyncSystemResourcesTest()
53 : push_client_channel_(
54 scoped_ptr
<notifier::PushClient
>(new notifier::FakePushClient())),
55 sync_system_resources_(&push_client_channel_
, &mock_state_writer_
) {}
57 virtual ~SyncSystemResourcesTest() {}
59 void ScheduleShouldNotRun() {
61 // Owned by ScheduleImmediately.
62 MockClosure mock_closure
;
63 base::Closure
* should_not_run
= mock_closure
.CreateClosure();
64 EXPECT_CALL(mock_closure
, Run()).Times(0);
65 sync_system_resources_
.internal_scheduler()->Schedule(
66 invalidation::Scheduler::NoDelay(), should_not_run
);
69 // Owned by ScheduleOnListenerThread.
70 MockClosure mock_closure
;
71 base::Closure
* should_not_run
= mock_closure
.CreateClosure();
72 EXPECT_CALL(mock_closure
, Run()).Times(0);
73 sync_system_resources_
.listener_scheduler()->Schedule(
74 invalidation::Scheduler::NoDelay(), should_not_run
);
77 // Owned by ScheduleWithDelay.
78 MockClosure mock_closure
;
79 base::Closure
* should_not_run
= mock_closure
.CreateClosure();
80 EXPECT_CALL(mock_closure
, Run()).Times(0);
81 sync_system_resources_
.internal_scheduler()->Schedule(
82 invalidation::TimeDelta::FromSeconds(0), should_not_run
);
86 // Needed by |sync_system_resources_|.
87 base::MessageLoop message_loop_
;
88 MockStateWriter mock_state_writer_
;
89 PushClientChannel push_client_channel_
;
90 SyncSystemResources sync_system_resources_
;
93 DISALLOW_COPY_AND_ASSIGN(SyncSystemResourcesTest
);
96 // Make sure current_time() doesn't crash or leak.
97 TEST_F(SyncSystemResourcesTest
, CurrentTime
) {
98 invalidation::Time current_time
=
99 sync_system_resources_
.internal_scheduler()->GetCurrentTime();
100 DVLOG(1) << "current_time returned: " << current_time
.ToInternalValue();
103 // Make sure Log() doesn't crash or leak.
104 TEST_F(SyncSystemResourcesTest
, Log
) {
105 sync_system_resources_
.logger()->Log(SyncLogger::INFO_LEVEL
,
106 __FILE__
, __LINE__
, "%s %d",
110 TEST_F(SyncSystemResourcesTest
, ScheduleBeforeStart
) {
111 ScheduleShouldNotRun();
112 sync_system_resources_
.Start();
115 TEST_F(SyncSystemResourcesTest
, ScheduleAfterStop
) {
116 sync_system_resources_
.Start();
117 sync_system_resources_
.Stop();
118 ScheduleShouldNotRun();
121 TEST_F(SyncSystemResourcesTest
, ScheduleAndStop
) {
122 sync_system_resources_
.Start();
123 ScheduleShouldNotRun();
124 sync_system_resources_
.Stop();
127 TEST_F(SyncSystemResourcesTest
, ScheduleAndDestroy
) {
128 sync_system_resources_
.Start();
129 ScheduleShouldNotRun();
132 TEST_F(SyncSystemResourcesTest
, ScheduleImmediately
) {
133 sync_system_resources_
.Start();
134 MockClosure mock_closure
;
135 EXPECT_CALL(mock_closure
, Run());
136 sync_system_resources_
.internal_scheduler()->Schedule(
137 invalidation::Scheduler::NoDelay(), mock_closure
.CreateClosure());
138 message_loop_
.RunUntilIdle();
141 TEST_F(SyncSystemResourcesTest
, ScheduleOnListenerThread
) {
142 sync_system_resources_
.Start();
143 MockClosure mock_closure
;
144 EXPECT_CALL(mock_closure
, Run());
145 sync_system_resources_
.listener_scheduler()->Schedule(
146 invalidation::Scheduler::NoDelay(), mock_closure
.CreateClosure());
148 sync_system_resources_
.internal_scheduler()->IsRunningOnThread());
149 message_loop_
.RunUntilIdle();
152 TEST_F(SyncSystemResourcesTest
, ScheduleWithZeroDelay
) {
153 sync_system_resources_
.Start();
154 MockClosure mock_closure
;
155 EXPECT_CALL(mock_closure
, Run());
156 sync_system_resources_
.internal_scheduler()->Schedule(
157 invalidation::TimeDelta::FromSeconds(0), mock_closure
.CreateClosure());
158 message_loop_
.RunUntilIdle();
161 // TODO(akalin): Figure out how to test with a non-zero delay.
163 TEST_F(SyncSystemResourcesTest
, WriteState
) {
164 sync_system_resources_
.Start();
165 EXPECT_CALL(mock_state_writer_
, WriteState(_
));
166 // Owned by WriteState.
167 MockStorageCallback mock_storage_callback
;
168 invalidation::Status
results(invalidation::Status::PERMANENT_FAILURE
,
170 EXPECT_CALL(mock_storage_callback
, Run(_
))
171 .WillOnce(SaveArg
<0>(&results
));
172 sync_system_resources_
.storage()->WriteKey(
173 std::string(), "state", mock_storage_callback
.CreateCallback());
174 message_loop_
.RunUntilIdle();
175 EXPECT_EQ(invalidation::Status(invalidation::Status::SUCCESS
, std::string()),
179 class TestSyncNetworkChannel
: public SyncNetworkChannel
{
181 TestSyncNetworkChannel() {}
182 virtual ~TestSyncNetworkChannel() {}
184 using SyncNetworkChannel::NotifyStateChange
;
185 using SyncNetworkChannel::DeliverIncomingMessage
;
187 virtual void SendEncodedMessage(const std::string
& encoded_message
) OVERRIDE
{
188 last_encoded_message_
= encoded_message
;
191 virtual void UpdateCredentials(const std::string
& email
,
192 const std::string
& token
) OVERRIDE
{
195 std::string last_encoded_message_
;
198 class SyncNetworkChannelTest
199 : public testing::Test
,
200 public SyncNetworkChannel::Observer
{
202 SyncNetworkChannelTest()
203 : last_invalidator_state_(DEFAULT_INVALIDATION_ERROR
),
205 network_channel_
.AddObserver(this);
206 network_channel_
.SetMessageReceiver(
207 invalidation::NewPermanentCallback(
208 this, &SyncNetworkChannelTest::OnIncomingMessage
));
209 network_channel_
.AddNetworkStatusReceiver(
210 invalidation::NewPermanentCallback(
211 this, &SyncNetworkChannelTest::OnNetworkStatusChange
));
214 virtual ~SyncNetworkChannelTest() {
215 network_channel_
.RemoveObserver(this);
218 virtual void OnNetworkChannelStateChanged(
219 InvalidatorState invalidator_state
) OVERRIDE
{
220 last_invalidator_state_
= invalidator_state
;
223 void OnIncomingMessage(std::string incoming_message
) {
224 last_message_
= incoming_message
;
227 void OnNetworkStatusChange(bool connected
) {
228 connected_
= connected
;
231 TestSyncNetworkChannel network_channel_
;
232 InvalidatorState last_invalidator_state_
;
233 std::string last_message_
;
237 const char kMessage
[] = "message";
238 const char kServiceContext
[] = "service context";
239 const int64 kSchedulingHash
= 100;
241 // Encode a message with some context and then decode it. The decoded info
242 // should match the original info.
243 TEST_F(SyncNetworkChannelTest
, EncodeDecode
) {
244 const std::string
& data
=
245 SyncNetworkChannel::EncodeMessageForTest(
246 kMessage
, kServiceContext
, kSchedulingHash
);
248 std::string service_context
;
249 int64 scheduling_hash
= 0LL;
250 EXPECT_TRUE(SyncNetworkChannel::DecodeMessageForTest(
251 data
, &message
, &service_context
, &scheduling_hash
));
252 EXPECT_EQ(kMessage
, message
);
253 EXPECT_EQ(kServiceContext
, service_context
);
254 EXPECT_EQ(kSchedulingHash
, scheduling_hash
);
257 // Encode a message with no context and then decode it. The decoded message
258 // should match the original message, but the context and hash should be
260 TEST_F(SyncNetworkChannelTest
, EncodeDecodeNoContext
) {
261 const std::string
& data
=
262 SyncNetworkChannel::EncodeMessageForTest(
263 kMessage
, std::string(), kSchedulingHash
);
265 std::string service_context
= kServiceContext
;
266 int64 scheduling_hash
= kSchedulingHash
+ 1;
267 EXPECT_TRUE(SyncNetworkChannel::DecodeMessageForTest(
268 data
, &message
, &service_context
, &scheduling_hash
));
269 EXPECT_EQ(kMessage
, message
);
270 EXPECT_EQ(kServiceContext
, service_context
);
271 EXPECT_EQ(kSchedulingHash
+ 1, scheduling_hash
);
274 // Decode an empty notification. It should result in an empty message
275 // but should leave the context and hash untouched.
276 TEST_F(SyncNetworkChannelTest
, DecodeEmpty
) {
277 std::string message
= kMessage
;
278 std::string service_context
= kServiceContext
;
279 int64 scheduling_hash
= kSchedulingHash
;
280 EXPECT_TRUE(SyncNetworkChannel::DecodeMessageForTest(
281 std::string(), &message
, &service_context
, &scheduling_hash
));
282 EXPECT_TRUE(message
.empty());
283 EXPECT_EQ(kServiceContext
, service_context
);
284 EXPECT_EQ(kSchedulingHash
, scheduling_hash
);
287 // Try to decode a garbage notification. It should leave all its
288 // arguments untouched and return false.
289 TEST_F(SyncNetworkChannelTest
, DecodeGarbage
) {
290 std::string data
= "garbage";
291 std::string message
= kMessage
;
292 std::string service_context
= kServiceContext
;
293 int64 scheduling_hash
= kSchedulingHash
;
294 EXPECT_FALSE(SyncNetworkChannel::DecodeMessageForTest(
295 data
, &message
, &service_context
, &scheduling_hash
));
296 EXPECT_EQ(kMessage
, message
);
297 EXPECT_EQ(kServiceContext
, service_context
);
298 EXPECT_EQ(kSchedulingHash
, scheduling_hash
);
301 // Simulate network channel state change. It should propagate to observer.
302 TEST_F(SyncNetworkChannelTest
, OnNetworkChannelStateChanged
) {
303 EXPECT_EQ(DEFAULT_INVALIDATION_ERROR
, last_invalidator_state_
);
304 EXPECT_FALSE(connected_
);
305 network_channel_
.NotifyStateChange(INVALIDATIONS_ENABLED
);
306 EXPECT_EQ(INVALIDATIONS_ENABLED
, last_invalidator_state_
);
307 EXPECT_TRUE(connected_
);
308 network_channel_
.NotifyStateChange(INVALIDATION_CREDENTIALS_REJECTED
);
309 EXPECT_EQ(INVALIDATION_CREDENTIALS_REJECTED
, last_invalidator_state_
);
310 EXPECT_FALSE(connected_
);
313 // Call SendMessage on the channel. SendEncodedMessage should be called for it.
314 TEST_F(SyncNetworkChannelTest
, SendMessage
) {
315 network_channel_
.SendMessage(kMessage
);
316 std::string expected_encoded_message
=
317 SyncNetworkChannel::EncodeMessageForTest(
319 network_channel_
.GetServiceContextForTest(),
320 network_channel_
.GetSchedulingHashForTest());
321 ASSERT_EQ(expected_encoded_message
, network_channel_
.last_encoded_message_
);
324 // Simulate an incoming notification. It should be decoded properly
326 TEST_F(SyncNetworkChannelTest
, OnIncomingMessage
) {
327 const std::string message
=
328 SyncNetworkChannel::EncodeMessageForTest(
329 kMessage
, kServiceContext
, kSchedulingHash
);
331 network_channel_
.DeliverIncomingMessage(message
);
332 EXPECT_EQ(kServiceContext
,
333 network_channel_
.GetServiceContextForTest());
334 EXPECT_EQ(kSchedulingHash
,
335 network_channel_
.GetSchedulingHashForTest());
336 EXPECT_EQ(kMessage
, last_message_
);
339 // Simulate an incoming notification with no receiver. It should be dropped by
341 TEST_F(SyncNetworkChannelTest
, OnIncomingMessageNoReceiver
) {
342 const std::string message
=
343 SyncNetworkChannel::EncodeMessageForTest(
344 kMessage
, kServiceContext
, kSchedulingHash
);
346 network_channel_
.SetMessageReceiver(NULL
);
347 network_channel_
.DeliverIncomingMessage(message
);
348 EXPECT_TRUE(network_channel_
.GetServiceContextForTest().empty());
349 EXPECT_EQ(static_cast<int64
>(0),
350 network_channel_
.GetSchedulingHashForTest());
351 EXPECT_TRUE(last_message_
.empty());
354 // Simulate an incoming garbage notification. It should be dropped by
356 TEST_F(SyncNetworkChannelTest
, OnIncomingMessageGarbage
) {
357 std::string message
= "garbage";
359 network_channel_
.DeliverIncomingMessage(message
);
360 EXPECT_TRUE(network_channel_
.GetServiceContextForTest().empty());
361 EXPECT_EQ(static_cast<int64
>(0),
362 network_channel_
.GetSchedulingHashForTest());
363 EXPECT_TRUE(last_message_
.empty());
366 // Send a message, simulate an incoming message with context, and then
367 // send the same message again. The first sent message should not
368 // have any context, but the second sent message should have the
369 // context from the incoming emssage.
370 TEST_F(SyncNetworkChannelTest
, PersistedMessageState
) {
371 network_channel_
.SendMessage(kMessage
);
372 ASSERT_FALSE(network_channel_
.last_encoded_message_
.empty());
375 std::string service_context
;
376 int64 scheduling_hash
= 0LL;
377 EXPECT_TRUE(SyncNetworkChannel::DecodeMessageForTest(
378 network_channel_
.last_encoded_message_
,
379 &message
, &service_context
, &scheduling_hash
));
380 EXPECT_EQ(kMessage
, message
);
381 EXPECT_TRUE(service_context
.empty());
382 EXPECT_EQ(0LL, scheduling_hash
);
385 const std::string
& encoded_message
=
386 SyncNetworkChannel::EncodeMessageForTest(
387 kMessage
, kServiceContext
, kSchedulingHash
);
388 network_channel_
.DeliverIncomingMessage(encoded_message
);
390 network_channel_
.last_encoded_message_
.clear();
391 network_channel_
.SendMessage(kMessage
);
392 ASSERT_FALSE(network_channel_
.last_encoded_message_
.empty());
395 std::string service_context
;
396 int64 scheduling_hash
= 0LL;
397 EXPECT_TRUE(SyncNetworkChannel::DecodeMessageForTest(
398 network_channel_
.last_encoded_message_
,
399 &message
, &service_context
, &scheduling_hash
));
400 EXPECT_EQ(kMessage
, message
);
401 EXPECT_EQ(kServiceContext
, service_context
);
402 EXPECT_EQ(kSchedulingHash
, scheduling_hash
);
407 } // namespace syncer