1 // Copyright 2014 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 "components/gcm_driver/gcm_driver_desktop.h"
8 #include "base/bind_helpers.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/location.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/message_loop/message_loop_proxy.h"
13 #include "base/metrics/field_trial.h"
14 #include "base/prefs/pref_registry_simple.h"
15 #include "base/prefs/testing_pref_service.h"
16 #include "base/run_loop.h"
17 #include "base/strings/string_util.h"
18 #include "base/test/test_simple_task_runner.h"
19 #include "base/threading/thread.h"
20 #include "components/gcm_driver/fake_gcm_app_handler.h"
21 #include "components/gcm_driver/fake_gcm_client.h"
22 #include "components/gcm_driver/fake_gcm_client_factory.h"
23 #include "components/gcm_driver/gcm_app_handler.h"
24 #include "components/gcm_driver/gcm_channel_status_request.h"
25 #include "components/gcm_driver/gcm_channel_status_syncer.h"
26 #include "components/gcm_driver/gcm_client_factory.h"
27 #include "components/gcm_driver/gcm_connection_observer.h"
28 #include "net/url_request/test_url_fetcher_factory.h"
29 #include "net/url_request/url_fetcher_delegate.h"
30 #include "net/url_request/url_request_context_getter.h"
31 #include "net/url_request/url_request_test_util.h"
32 #include "sync/protocol/experiment_status.pb.h"
33 #include "sync/protocol/experiments_specifics.pb.h"
34 #include "testing/gtest/include/gtest/gtest.h"
40 const char kTestAccountID1
[] = "user1@example.com";
41 const char kTestAccountID2
[] = "user2@example.com";
42 const char kTestAppID1
[] = "TestApp1";
43 const char kTestAppID2
[] = "TestApp2";
44 const char kUserID1
[] = "user1";
46 class FakeGCMConnectionObserver
: public GCMConnectionObserver
{
48 FakeGCMConnectionObserver();
49 ~FakeGCMConnectionObserver() override
;
51 // gcm::GCMConnectionObserver implementation:
52 void OnConnected(const net::IPEndPoint
& ip_endpoint
) override
;
53 void OnDisconnected() override
;
55 bool connected() const { return connected_
; }
61 FakeGCMConnectionObserver::FakeGCMConnectionObserver() : connected_(false) {
64 FakeGCMConnectionObserver::~FakeGCMConnectionObserver() {
67 void FakeGCMConnectionObserver::OnConnected(
68 const net::IPEndPoint
& ip_endpoint
) {
72 void FakeGCMConnectionObserver::OnDisconnected() {
76 void PumpCurrentLoop() {
77 base::MessageLoop::ScopedNestableTaskAllower
78 nestable_task_allower(base::MessageLoop::current());
79 base::RunLoop().RunUntilIdle();
86 std::vector
<std::string
> ToSenderList(const std::string
& sender_ids
) {
87 std::vector
<std::string
> senders
;
88 Tokenize(sender_ids
, ",", &senders
);
94 class GCMDriverTest
: public testing::Test
{
102 ~GCMDriverTest() override
;
105 void SetUp() override
;
106 void TearDown() override
;
108 GCMDriverDesktop
* driver() { return driver_
.get(); }
109 FakeGCMAppHandler
* gcm_app_handler() { return gcm_app_handler_
.get(); }
110 FakeGCMConnectionObserver
* gcm_connection_observer() {
111 return gcm_connection_observer_
.get();
113 const std::string
& registration_id() const { return registration_id_
; }
114 GCMClient::Result
registration_result() const { return registration_result_
; }
115 const std::string
& send_message_id() const { return send_message_id_
; }
116 GCMClient::Result
send_result() const { return send_result_
; }
117 GCMClient::Result
unregistration_result() const {
118 return unregistration_result_
;
125 bool HasAppHandlers() const;
126 FakeGCMClient
* GetGCMClient();
128 void CreateDriver(FakeGCMClient::StartMode gcm_client_start_mode
);
129 void ShutdownDriver();
130 void AddAppHandlers();
131 void RemoveAppHandlers();
133 void SignIn(const std::string
& account_id
);
136 void Register(const std::string
& app_id
,
137 const std::vector
<std::string
>& sender_ids
,
138 WaitToFinish wait_to_finish
);
139 void Send(const std::string
& app_id
,
140 const std::string
& receiver_id
,
141 const GCMClient::OutgoingMessage
& message
,
142 WaitToFinish wait_to_finish
);
143 void Unregister(const std::string
& app_id
, WaitToFinish wait_to_finish
);
145 void WaitForAsyncOperation();
148 void RegisterCompleted(const std::string
& registration_id
,
149 GCMClient::Result result
);
150 void SendCompleted(const std::string
& message_id
, GCMClient::Result result
);
151 void UnregisterCompleted(GCMClient::Result result
);
153 base::ScopedTempDir temp_dir_
;
154 TestingPrefServiceSimple prefs_
;
155 scoped_refptr
<base::TestSimpleTaskRunner
> task_runner_
;
156 base::MessageLoopForUI message_loop_
;
157 base::Thread io_thread_
;
158 base::FieldTrialList field_trial_list_
;
159 scoped_ptr
<GCMDriverDesktop
> driver_
;
160 scoped_ptr
<FakeGCMAppHandler
> gcm_app_handler_
;
161 scoped_ptr
<FakeGCMConnectionObserver
> gcm_connection_observer_
;
163 base::Closure async_operation_completed_callback_
;
165 std::string registration_id_
;
166 GCMClient::Result registration_result_
;
167 std::string send_message_id_
;
168 GCMClient::Result send_result_
;
169 GCMClient::Result unregistration_result_
;
171 DISALLOW_COPY_AND_ASSIGN(GCMDriverTest
);
174 GCMDriverTest::GCMDriverTest()
175 : task_runner_(new base::TestSimpleTaskRunner()),
176 io_thread_("IOThread"),
177 field_trial_list_(NULL
),
178 registration_result_(GCMClient::UNKNOWN_ERROR
),
179 send_result_(GCMClient::UNKNOWN_ERROR
),
180 unregistration_result_(GCMClient::UNKNOWN_ERROR
) {
183 GCMDriverTest::~GCMDriverTest() {
186 void GCMDriverTest::SetUp() {
187 GCMChannelStatusSyncer::RegisterPrefs(prefs_
.registry());
189 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
192 void GCMDriverTest::TearDown() {
203 void GCMDriverTest::PumpIOLoop() {
204 base::RunLoop run_loop
;
205 io_thread_
.message_loop_proxy()->PostTaskAndReply(
207 base::Bind(&PumpCurrentLoop
),
208 run_loop
.QuitClosure());
212 void GCMDriverTest::ClearResults() {
213 registration_id_
.clear();
214 registration_result_
= GCMClient::UNKNOWN_ERROR
;
216 send_message_id_
.clear();
217 send_result_
= GCMClient::UNKNOWN_ERROR
;
219 unregistration_result_
= GCMClient::UNKNOWN_ERROR
;
222 bool GCMDriverTest::HasAppHandlers() const {
223 return !driver_
->app_handlers().empty();
226 FakeGCMClient
* GCMDriverTest::GetGCMClient() {
227 return static_cast<FakeGCMClient
*>(driver_
->GetGCMClientForTesting());
230 void GCMDriverTest::CreateDriver(
231 FakeGCMClient::StartMode gcm_client_start_mode
) {
232 scoped_refptr
<net::URLRequestContextGetter
> request_context
=
233 new net::TestURLRequestContextGetter(io_thread_
.message_loop_proxy());
234 // TODO(johnme): Need equivalent test coverage of GCMDriverAndroid.
235 driver_
.reset(new GCMDriverDesktop(
236 scoped_ptr
<GCMClientFactory
>(
237 new FakeGCMClientFactory(gcm_client_start_mode
,
238 base::MessageLoopProxy::current(),
239 io_thread_
.message_loop_proxy())).Pass(),
240 GCMClient::ChromeBuildInfo(),
241 "http://channel.status.request.url",
246 base::MessageLoopProxy::current(),
247 io_thread_
.message_loop_proxy(),
250 gcm_app_handler_
.reset(new FakeGCMAppHandler
);
251 gcm_connection_observer_
.reset(new FakeGCMConnectionObserver
);
253 driver_
->AddConnectionObserver(gcm_connection_observer_
.get());
256 void GCMDriverTest::ShutdownDriver() {
257 if (gcm_connection_observer())
258 driver()->RemoveConnectionObserver(gcm_connection_observer());
259 driver()->Shutdown();
262 void GCMDriverTest::AddAppHandlers() {
263 driver_
->AddAppHandler(kTestAppID1
, gcm_app_handler_
.get());
264 driver_
->AddAppHandler(kTestAppID2
, gcm_app_handler_
.get());
267 void GCMDriverTest::RemoveAppHandlers() {
268 driver_
->RemoveAppHandler(kTestAppID1
);
269 driver_
->RemoveAppHandler(kTestAppID2
);
272 void GCMDriverTest::SignIn(const std::string
& account_id
) {
273 driver_
->OnSignedIn();
278 void GCMDriverTest::SignOut() {
279 driver_
->OnSignedOut();
284 void GCMDriverTest::Register(const std::string
& app_id
,
285 const std::vector
<std::string
>& sender_ids
,
286 WaitToFinish wait_to_finish
) {
287 base::RunLoop run_loop
;
288 async_operation_completed_callback_
= run_loop
.QuitClosure();
289 driver_
->Register(app_id
,
291 base::Bind(&GCMDriverTest::RegisterCompleted
,
292 base::Unretained(this)));
293 if (wait_to_finish
== WAIT
)
297 void GCMDriverTest::Send(const std::string
& app_id
,
298 const std::string
& receiver_id
,
299 const GCMClient::OutgoingMessage
& message
,
300 WaitToFinish wait_to_finish
) {
301 base::RunLoop run_loop
;
302 async_operation_completed_callback_
= run_loop
.QuitClosure();
303 driver_
->Send(app_id
,
306 base::Bind(&GCMDriverTest::SendCompleted
,
307 base::Unretained(this)));
308 if (wait_to_finish
== WAIT
)
312 void GCMDriverTest::Unregister(const std::string
& app_id
,
313 WaitToFinish wait_to_finish
) {
314 base::RunLoop run_loop
;
315 async_operation_completed_callback_
= run_loop
.QuitClosure();
316 driver_
->Unregister(app_id
,
317 base::Bind(&GCMDriverTest::UnregisterCompleted
,
318 base::Unretained(this)));
319 if (wait_to_finish
== WAIT
)
323 void GCMDriverTest::WaitForAsyncOperation() {
324 base::RunLoop run_loop
;
325 async_operation_completed_callback_
= run_loop
.QuitClosure();
329 void GCMDriverTest::RegisterCompleted(const std::string
& registration_id
,
330 GCMClient::Result result
) {
331 registration_id_
= registration_id
;
332 registration_result_
= result
;
333 if (!async_operation_completed_callback_
.is_null())
334 async_operation_completed_callback_
.Run();
337 void GCMDriverTest::SendCompleted(const std::string
& message_id
,
338 GCMClient::Result result
) {
339 send_message_id_
= message_id
;
340 send_result_
= result
;
341 if (!async_operation_completed_callback_
.is_null())
342 async_operation_completed_callback_
.Run();
345 void GCMDriverTest::UnregisterCompleted(GCMClient::Result result
) {
346 unregistration_result_
= result
;
347 if (!async_operation_completed_callback_
.is_null())
348 async_operation_completed_callback_
.Run();
351 TEST_F(GCMDriverTest
, Create
) {
352 // Create GCMDriver first. GCM is not started.
353 CreateDriver(FakeGCMClient::NO_DELAY_START
);
354 EXPECT_FALSE(driver()->IsStarted());
356 // Sign in. GCM is still not started.
357 SignIn(kTestAccountID1
);
358 EXPECT_FALSE(driver()->IsStarted());
359 EXPECT_FALSE(driver()->IsConnected());
360 EXPECT_FALSE(gcm_connection_observer()->connected());
362 // GCM will be started only after both sign-in and app handler being added.
364 EXPECT_TRUE(driver()->IsStarted());
366 EXPECT_TRUE(driver()->IsConnected());
367 EXPECT_TRUE(gcm_connection_observer()->connected());
370 TEST_F(GCMDriverTest
, CreateByFieldTrial
) {
371 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("GCM", "Enabled"));
373 // Create GCMDriver first. GCM is not started.
374 CreateDriver(FakeGCMClient::NO_DELAY_START
);
375 EXPECT_FALSE(driver()->IsStarted());
376 EXPECT_FALSE(driver()->IsConnected());
377 EXPECT_FALSE(gcm_connection_observer()->connected());
379 // GCM will be started after app handler is added.
381 EXPECT_TRUE(driver()->IsStarted());
383 EXPECT_TRUE(driver()->IsConnected());
384 EXPECT_TRUE(gcm_connection_observer()->connected());
386 // Sign-in will not affect GCM state.
387 SignIn(kTestAccountID1
);
389 EXPECT_TRUE(driver()->IsStarted());
390 EXPECT_TRUE(driver()->IsConnected());
392 // Sign-out will not affect GCM state.
395 EXPECT_TRUE(driver()->IsStarted());
396 EXPECT_TRUE(driver()->IsConnected());
399 TEST_F(GCMDriverTest
, Shutdown
) {
400 CreateDriver(FakeGCMClient::NO_DELAY_START
);
401 EXPECT_FALSE(HasAppHandlers());
404 EXPECT_TRUE(HasAppHandlers());
407 EXPECT_FALSE(HasAppHandlers());
408 EXPECT_FALSE(driver()->IsConnected());
409 EXPECT_FALSE(gcm_connection_observer()->connected());
412 TEST_F(GCMDriverTest
, SignInAndSignOutOnGCMEnabled
) {
413 // By default, GCM is enabled.
414 CreateDriver(FakeGCMClient::NO_DELAY_START
);
417 // GCMClient should be started after sign-in.
418 SignIn(kTestAccountID1
);
419 EXPECT_EQ(FakeGCMClient::STARTED
, GetGCMClient()->status());
421 // GCMClient should be stopped out after sign-out.
422 // Note: Before we enable the feature that drops the sign-in enforcement and
423 // make GCM work for all users, GCM is only applicable to signed-in users.
424 // Once the users sign out, the GCM will be shut down while the GCM store
427 EXPECT_EQ(FakeGCMClient::STOPPED
, GetGCMClient()->status());
430 TEST_F(GCMDriverTest
, SignInAndSignOutOnGCMDisabled
) {
431 // By default, GCM is enabled.
432 CreateDriver(FakeGCMClient::NO_DELAY_START
);
438 // GCMClient should not be started after sign-in.
439 SignIn(kTestAccountID1
);
440 EXPECT_EQ(FakeGCMClient::UNINITIALIZED
, GetGCMClient()->status());
442 // GCMClient should remain not started after sign-out.
444 EXPECT_EQ(FakeGCMClient::UNINITIALIZED
, GetGCMClient()->status());
447 TEST_F(GCMDriverTest
, SignOutAndThenSignIn
) {
448 CreateDriver(FakeGCMClient::NO_DELAY_START
);
451 // GCMClient should be started after sign-in.
452 SignIn(kTestAccountID1
);
453 EXPECT_EQ(FakeGCMClient::STARTED
, GetGCMClient()->status());
455 // GCMClient should be stopped after sign-out.
457 EXPECT_EQ(FakeGCMClient::STOPPED
, GetGCMClient()->status());
459 // Sign-in with a different account.
460 SignIn(kTestAccountID2
);
462 // GCMClient should be started again.
463 EXPECT_EQ(FakeGCMClient::STARTED
, GetGCMClient()->status());
466 TEST_F(GCMDriverTest
, DisableAndReenableGCM
) {
467 CreateDriver(FakeGCMClient::NO_DELAY_START
);
469 SignIn(kTestAccountID1
);
471 // GCMClient should be started.
472 EXPECT_EQ(FakeGCMClient::STARTED
, GetGCMClient()->status());
479 // GCMClient should be stopped.
480 EXPECT_EQ(FakeGCMClient::STOPPED
, GetGCMClient()->status());
487 // GCMClient should be started.
488 EXPECT_EQ(FakeGCMClient::STARTED
, GetGCMClient()->status());
495 // GCMClient should be stopped.
496 EXPECT_EQ(FakeGCMClient::STOPPED
, GetGCMClient()->status());
501 // GCMClient should be stopped.
502 EXPECT_EQ(FakeGCMClient::STOPPED
, GetGCMClient()->status());
505 TEST_F(GCMDriverTest
, StartOrStopGCMOnDemand
) {
506 CreateDriver(FakeGCMClient::NO_DELAY_START
);
507 SignIn(kTestAccountID1
);
509 // GCMClient is not started.
510 EXPECT_EQ(FakeGCMClient::UNINITIALIZED
, GetGCMClient()->status());
512 // GCMClient is started after an app handler has been added.
513 driver()->AddAppHandler(kTestAppID1
, gcm_app_handler());
516 EXPECT_EQ(FakeGCMClient::STARTED
, GetGCMClient()->status());
518 // Add another app handler.
519 driver()->AddAppHandler(kTestAppID2
, gcm_app_handler());
522 EXPECT_EQ(FakeGCMClient::STARTED
, GetGCMClient()->status());
524 // GCMClient remains active after one app handler is gone.
525 driver()->RemoveAppHandler(kTestAppID1
);
528 EXPECT_EQ(FakeGCMClient::STARTED
, GetGCMClient()->status());
530 // GCMClient should be stopped after the last app handler is gone.
531 driver()->RemoveAppHandler(kTestAppID2
);
534 EXPECT_EQ(FakeGCMClient::STOPPED
, GetGCMClient()->status());
536 // GCMClient is restarted after an app handler has been added.
537 driver()->AddAppHandler(kTestAppID2
, gcm_app_handler());
540 EXPECT_EQ(FakeGCMClient::STARTED
, GetGCMClient()->status());
543 TEST_F(GCMDriverTest
, RegisterFailed
) {
544 std::vector
<std::string
> sender_ids
;
545 sender_ids
.push_back("sender1");
547 CreateDriver(FakeGCMClient::NO_DELAY_START
);
549 // Registration fails when GCM is disabled.
551 Register(kTestAppID1
, sender_ids
, GCMDriverTest::WAIT
);
552 EXPECT_TRUE(registration_id().empty());
553 EXPECT_EQ(GCMClient::GCM_DISABLED
, registration_result());
557 // Registration fails when the sign-in does not occur.
560 Register(kTestAppID1
, sender_ids
, GCMDriverTest::WAIT
);
561 EXPECT_TRUE(registration_id().empty());
562 EXPECT_EQ(GCMClient::NOT_SIGNED_IN
, registration_result());
566 // Registration fails when the no app handler is added.
568 SignIn(kTestAccountID1
);
569 Register(kTestAppID1
, sender_ids
, GCMDriverTest::WAIT
);
570 EXPECT_TRUE(registration_id().empty());
571 EXPECT_EQ(GCMClient::UNKNOWN_ERROR
, registration_result());
574 TEST_F(GCMDriverTest
, UnregisterFailed
) {
575 CreateDriver(FakeGCMClient::NO_DELAY_START
);
577 // Unregistration fails when GCM is disabled.
579 Unregister(kTestAppID1
, GCMDriverTest::WAIT
);
580 EXPECT_EQ(GCMClient::GCM_DISABLED
, unregistration_result());
584 // Unregistration fails when the sign-in does not occur.
587 Unregister(kTestAppID1
, GCMDriverTest::WAIT
);
588 EXPECT_EQ(GCMClient::NOT_SIGNED_IN
, unregistration_result());
592 // Unregistration fails when the no app handler is added.
594 SignIn(kTestAccountID1
);
595 Unregister(kTestAppID1
, GCMDriverTest::WAIT
);
596 EXPECT_EQ(GCMClient::UNKNOWN_ERROR
, unregistration_result());
599 TEST_F(GCMDriverTest
, SendFailed
) {
600 GCMClient::OutgoingMessage message
;
602 message
.data
["key1"] = "value1";
604 CreateDriver(FakeGCMClient::NO_DELAY_START
);
606 // Sending fails when GCM is disabled.
608 Send(kTestAppID1
, kUserID1
, message
, GCMDriverTest::WAIT
);
609 EXPECT_TRUE(send_message_id().empty());
610 EXPECT_EQ(GCMClient::GCM_DISABLED
, send_result());
614 // Sending fails when the sign-in does not occur.
617 Send(kTestAppID1
, kUserID1
, message
, GCMDriverTest::WAIT
);
618 EXPECT_TRUE(send_message_id().empty());
619 EXPECT_EQ(GCMClient::NOT_SIGNED_IN
, send_result());
623 // Sending fails when the no app handler is added.
625 SignIn(kTestAccountID1
);
626 Send(kTestAppID1
, kUserID1
, message
, GCMDriverTest::WAIT
);
627 EXPECT_TRUE(send_message_id().empty());
628 EXPECT_EQ(GCMClient::UNKNOWN_ERROR
, send_result());
631 TEST_F(GCMDriverTest
, GCMClientNotReadyBeforeRegistration
) {
632 // Make GCMClient not ready initially.
633 CreateDriver(FakeGCMClient::DELAY_START
);
634 SignIn(kTestAccountID1
);
637 // The registration is on hold until GCMClient is ready.
638 std::vector
<std::string
> sender_ids
;
639 sender_ids
.push_back("sender1");
640 Register(kTestAppID1
,
642 GCMDriverTest::DO_NOT_WAIT
);
645 EXPECT_TRUE(registration_id().empty());
646 EXPECT_EQ(GCMClient::UNKNOWN_ERROR
, registration_result());
648 // Register operation will be invoked after GCMClient becomes ready.
649 GetGCMClient()->PerformDelayedLoading();
650 WaitForAsyncOperation();
651 EXPECT_FALSE(registration_id().empty());
652 EXPECT_EQ(GCMClient::SUCCESS
, registration_result());
655 TEST_F(GCMDriverTest
, GCMClientNotReadyBeforeSending
) {
656 // Make GCMClient not ready initially.
657 CreateDriver(FakeGCMClient::DELAY_START
);
658 SignIn(kTestAccountID1
);
661 // The sending is on hold until GCMClient is ready.
662 GCMClient::OutgoingMessage message
;
664 message
.data
["key1"] = "value1";
665 message
.data
["key2"] = "value2";
666 Send(kTestAppID1
, kUserID1
, message
, GCMDriverTest::DO_NOT_WAIT
);
670 EXPECT_TRUE(send_message_id().empty());
671 EXPECT_EQ(GCMClient::UNKNOWN_ERROR
, send_result());
673 // Send operation will be invoked after GCMClient becomes ready.
674 GetGCMClient()->PerformDelayedLoading();
675 WaitForAsyncOperation();
676 EXPECT_EQ(message
.id
, send_message_id());
677 EXPECT_EQ(GCMClient::SUCCESS
, send_result());
680 // Tests a single instance of GCMDriver.
681 class GCMDriverFunctionalTest
: public GCMDriverTest
{
683 GCMDriverFunctionalTest();
684 ~GCMDriverFunctionalTest() override
;
687 void SetUp() override
;
690 DISALLOW_COPY_AND_ASSIGN(GCMDriverFunctionalTest
);
693 GCMDriverFunctionalTest::GCMDriverFunctionalTest() {
696 GCMDriverFunctionalTest::~GCMDriverFunctionalTest() {
699 void GCMDriverFunctionalTest::SetUp() {
700 GCMDriverTest::SetUp();
702 CreateDriver(FakeGCMClient::NO_DELAY_START
);
704 SignIn(kTestAccountID1
);
707 TEST_F(GCMDriverFunctionalTest
, Register
) {
708 std::vector
<std::string
> sender_ids
;
709 sender_ids
.push_back("sender1");
710 Register(kTestAppID1
, sender_ids
, GCMDriverTest::WAIT
);
711 const std::string expected_registration_id
=
712 GetGCMClient()->GetRegistrationIdFromSenderIds(sender_ids
);
714 EXPECT_EQ(expected_registration_id
, registration_id());
715 EXPECT_EQ(GCMClient::SUCCESS
, registration_result());
718 TEST_F(GCMDriverFunctionalTest
, RegisterError
) {
719 std::vector
<std::string
> sender_ids
;
720 sender_ids
.push_back("sender1@error");
721 Register(kTestAppID1
, sender_ids
, GCMDriverTest::WAIT
);
723 EXPECT_TRUE(registration_id().empty());
724 EXPECT_NE(GCMClient::SUCCESS
, registration_result());
727 TEST_F(GCMDriverFunctionalTest
, RegisterAgainWithSameSenderIDs
) {
728 std::vector
<std::string
> sender_ids
;
729 sender_ids
.push_back("sender1");
730 sender_ids
.push_back("sender2");
731 Register(kTestAppID1
, sender_ids
, GCMDriverTest::WAIT
);
732 const std::string expected_registration_id
=
733 GetGCMClient()->GetRegistrationIdFromSenderIds(sender_ids
);
735 EXPECT_EQ(expected_registration_id
, registration_id());
736 EXPECT_EQ(GCMClient::SUCCESS
, registration_result());
738 // Clears the results the would be set by the Register callback in preparation
739 // to call register 2nd time.
742 // Calling register 2nd time with the same set of sender IDs but different
743 // ordering will get back the same registration ID.
744 std::vector
<std::string
> another_sender_ids
;
745 another_sender_ids
.push_back("sender2");
746 another_sender_ids
.push_back("sender1");
747 Register(kTestAppID1
, another_sender_ids
, GCMDriverTest::WAIT
);
749 EXPECT_EQ(expected_registration_id
, registration_id());
750 EXPECT_EQ(GCMClient::SUCCESS
, registration_result());
753 TEST_F(GCMDriverFunctionalTest
, RegisterAgainWithDifferentSenderIDs
) {
754 std::vector
<std::string
> sender_ids
;
755 sender_ids
.push_back("sender1");
756 Register(kTestAppID1
, sender_ids
, GCMDriverTest::WAIT
);
757 const std::string expected_registration_id
=
758 GetGCMClient()->GetRegistrationIdFromSenderIds(sender_ids
);
760 EXPECT_EQ(expected_registration_id
, registration_id());
761 EXPECT_EQ(GCMClient::SUCCESS
, registration_result());
763 // Make sender IDs different.
764 sender_ids
.push_back("sender2");
765 const std::string expected_registration_id2
=
766 GetGCMClient()->GetRegistrationIdFromSenderIds(sender_ids
);
768 // Calling register 2nd time with the different sender IDs will get back a new
770 Register(kTestAppID1
, sender_ids
, GCMDriverTest::WAIT
);
771 EXPECT_EQ(expected_registration_id2
, registration_id());
772 EXPECT_EQ(GCMClient::SUCCESS
, registration_result());
775 TEST_F(GCMDriverFunctionalTest
, RegisterAfterSignOut
) {
778 std::vector
<std::string
> sender_ids
;
779 sender_ids
.push_back("sender1");
780 Register(kTestAppID1
, sender_ids
, GCMDriverTest::WAIT
);
782 EXPECT_TRUE(registration_id().empty());
783 EXPECT_EQ(GCMClient::NOT_SIGNED_IN
, registration_result());
786 TEST_F(GCMDriverFunctionalTest
, RegisterAfterSignOutAndSignInAgain
) {
787 std::vector
<std::string
> sender_ids
;
788 sender_ids
.push_back("sender1");
789 const std::string expected_registration_id
=
790 GetGCMClient()->GetRegistrationIdFromSenderIds(sender_ids
);
792 Register(kTestAppID1
, sender_ids
, GCMDriverTest::WAIT
);
793 EXPECT_EQ(expected_registration_id
, registration_id());
794 EXPECT_EQ(GCMClient::SUCCESS
, registration_result());
796 // After signing out, the GCM is stopped and calling register should fail.
798 Register(kTestAppID1
, sender_ids
, GCMDriverTest::WAIT
);
799 EXPECT_TRUE(registration_id().empty());
800 EXPECT_EQ(GCMClient::NOT_SIGNED_IN
, registration_result());
802 // After signing in again, same registration ID should be returned because
803 // the GCM data is not affected.
804 SignIn(kTestAccountID1
);
806 Register(kTestAppID1
, sender_ids
, GCMDriverTest::WAIT
);
807 EXPECT_EQ(expected_registration_id
, registration_id());
808 EXPECT_EQ(GCMClient::SUCCESS
, registration_result());
811 TEST_F(GCMDriverFunctionalTest
, UnregisterExplicitly
) {
812 std::vector
<std::string
> sender_ids
;
813 sender_ids
.push_back("sender1");
814 Register(kTestAppID1
, sender_ids
, GCMDriverTest::WAIT
);
816 EXPECT_FALSE(registration_id().empty());
817 EXPECT_EQ(GCMClient::SUCCESS
, registration_result());
819 Unregister(kTestAppID1
, GCMDriverTest::WAIT
);
821 EXPECT_EQ(GCMClient::SUCCESS
, unregistration_result());
824 TEST_F(GCMDriverFunctionalTest
, UnregisterWhenAsyncOperationPending
) {
825 std::vector
<std::string
> sender_ids
;
826 sender_ids
.push_back("sender1");
827 // First start registration without waiting for it to complete.
828 Register(kTestAppID1
, sender_ids
, GCMDriverTest::DO_NOT_WAIT
);
830 // Test that unregistration fails with async operation pending when there is a
831 // registration already in progress.
832 Unregister(kTestAppID1
, GCMDriverTest::WAIT
);
833 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING
,
834 unregistration_result());
836 // Complete the unregistration.
837 WaitForAsyncOperation();
838 EXPECT_EQ(GCMClient::SUCCESS
, registration_result());
840 // Start unregistration without waiting for it to complete. This time no async
841 // operation is pending.
842 Unregister(kTestAppID1
, GCMDriverTest::DO_NOT_WAIT
);
844 // Test that unregistration fails with async operation pending when there is
845 // an unregistration already in progress.
846 Unregister(kTestAppID1
, GCMDriverTest::WAIT
);
847 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING
,
848 unregistration_result());
851 // Complete unregistration.
852 WaitForAsyncOperation();
853 EXPECT_EQ(GCMClient::SUCCESS
, unregistration_result());
856 TEST_F(GCMDriverFunctionalTest
, RegisterWhenAsyncOperationPending
) {
857 std::vector
<std::string
> sender_ids
;
858 sender_ids
.push_back("sender1");
859 // First start registration without waiting for it to complete.
860 Register(kTestAppID1
, sender_ids
, GCMDriverTest::DO_NOT_WAIT
);
862 // Test that registration fails with async operation pending when there is a
863 // registration already in progress.
864 Register(kTestAppID1
, sender_ids
, GCMDriverTest::WAIT
);
865 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING
,
866 registration_result());
869 // Complete the registration.
870 WaitForAsyncOperation();
871 EXPECT_EQ(GCMClient::SUCCESS
, registration_result());
874 TEST_F(GCMDriverFunctionalTest
, RegisterAfterUnfinishedUnregister
) {
875 // Register and wait for it to complete.
876 std::vector
<std::string
> sender_ids
;
877 sender_ids
.push_back("sender1");
878 Register(kTestAppID1
, sender_ids
, GCMDriverTest::WAIT
);
879 EXPECT_EQ(GCMClient::SUCCESS
, registration_result());
880 EXPECT_EQ(GetGCMClient()->GetRegistrationIdFromSenderIds(sender_ids
),
883 // Clears the results the would be set by the Register callback in preparation
884 // to call register 2nd time.
887 // Start unregistration without waiting for it to complete.
888 Unregister(kTestAppID1
, GCMDriverTest::DO_NOT_WAIT
);
890 // Register immeidately after unregistration is not completed.
891 sender_ids
.push_back("sender2");
892 Register(kTestAppID1
, sender_ids
, GCMDriverTest::WAIT
);
894 // We need one more waiting since the waiting in Register is indeed for
895 // uncompleted Unregister.
896 WaitForAsyncOperation();
897 EXPECT_EQ(GCMClient::SUCCESS
, registration_result());
898 EXPECT_EQ(GetGCMClient()->GetRegistrationIdFromSenderIds(sender_ids
),
902 TEST_F(GCMDriverFunctionalTest
, Send
) {
903 GCMClient::OutgoingMessage message
;
904 message
.id
= "1@ack";
905 message
.data
["key1"] = "value1";
906 message
.data
["key2"] = "value2";
907 Send(kTestAppID1
, kUserID1
, message
, GCMDriverTest::WAIT
);
909 EXPECT_EQ(message
.id
, send_message_id());
910 EXPECT_EQ(GCMClient::SUCCESS
, send_result());
912 gcm_app_handler()->WaitForNotification();
913 EXPECT_EQ(message
.id
, gcm_app_handler()->acked_message_id());
914 EXPECT_EQ(kTestAppID1
, gcm_app_handler()->app_id());
917 TEST_F(GCMDriverFunctionalTest
, SendAfterSignOut
) {
920 GCMClient::OutgoingMessage message
;
922 message
.data
["key1"] = "value1";
923 message
.data
["key2"] = "value2";
924 Send(kTestAppID1
, kUserID1
, message
, GCMDriverTest::WAIT
);
926 EXPECT_TRUE(send_message_id().empty());
927 EXPECT_EQ(GCMClient::NOT_SIGNED_IN
, send_result());
930 TEST_F(GCMDriverFunctionalTest
, SendError
) {
931 GCMClient::OutgoingMessage message
;
932 // Embedding error in id will tell the mock to simulate the send error.
933 message
.id
= "1@error";
934 message
.data
["key1"] = "value1";
935 message
.data
["key2"] = "value2";
936 Send(kTestAppID1
, kUserID1
, message
, GCMDriverTest::WAIT
);
938 EXPECT_EQ(message
.id
, send_message_id());
939 EXPECT_EQ(GCMClient::SUCCESS
, send_result());
941 // Wait for the send error.
942 gcm_app_handler()->WaitForNotification();
943 EXPECT_EQ(FakeGCMAppHandler::SEND_ERROR_EVENT
,
944 gcm_app_handler()->received_event());
945 EXPECT_EQ(kTestAppID1
, gcm_app_handler()->app_id());
946 EXPECT_EQ(message
.id
,
947 gcm_app_handler()->send_error_details().message_id
);
948 EXPECT_NE(GCMClient::SUCCESS
,
949 gcm_app_handler()->send_error_details().result
);
950 EXPECT_EQ(message
.data
,
951 gcm_app_handler()->send_error_details().additional_data
);
954 TEST_F(GCMDriverFunctionalTest
, MessageReceived
) {
955 Register(kTestAppID1
, ToSenderList("sender"), GCMDriverTest::WAIT
);
956 GCMClient::IncomingMessage message
;
957 message
.data
["key1"] = "value1";
958 message
.data
["key2"] = "value2";
959 message
.sender_id
= "sender";
960 GetGCMClient()->ReceiveMessage(kTestAppID1
, message
);
961 gcm_app_handler()->WaitForNotification();
962 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT
,
963 gcm_app_handler()->received_event());
964 EXPECT_EQ(kTestAppID1
, gcm_app_handler()->app_id());
965 EXPECT_EQ(message
.data
, gcm_app_handler()->message().data
);
966 EXPECT_TRUE(gcm_app_handler()->message().collapse_key
.empty());
967 EXPECT_EQ(message
.sender_id
, gcm_app_handler()->message().sender_id
);
970 TEST_F(GCMDriverFunctionalTest
, MessageWithCollapseKeyReceived
) {
971 Register(kTestAppID1
, ToSenderList("sender"), GCMDriverTest::WAIT
);
972 GCMClient::IncomingMessage message
;
973 message
.data
["key1"] = "value1";
974 message
.collapse_key
= "collapse_key_value";
975 message
.sender_id
= "sender";
976 GetGCMClient()->ReceiveMessage(kTestAppID1
, message
);
977 gcm_app_handler()->WaitForNotification();
978 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT
,
979 gcm_app_handler()->received_event());
980 EXPECT_EQ(kTestAppID1
, gcm_app_handler()->app_id());
981 EXPECT_EQ(message
.data
, gcm_app_handler()->message().data
);
982 EXPECT_EQ(message
.collapse_key
,
983 gcm_app_handler()->message().collapse_key
);
986 TEST_F(GCMDriverFunctionalTest
, MessagesDeleted
) {
987 GetGCMClient()->DeleteMessages(kTestAppID1
);
988 gcm_app_handler()->WaitForNotification();
989 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT
,
990 gcm_app_handler()->received_event());
991 EXPECT_EQ(kTestAppID1
, gcm_app_handler()->app_id());
994 TEST_F(GCMDriverFunctionalTest
, LastTokenFetchTime
) {
995 EXPECT_EQ(base::Time(), driver()->GetLastTokenFetchTime());
996 base::Time fetch_time
= base::Time::Now();
997 driver()->SetLastTokenFetchTime(fetch_time
);
998 EXPECT_EQ(fetch_time
, driver()->GetLastTokenFetchTime());
1001 // Tests a single instance of GCMDriver.
1002 class GCMChannelStatusSyncerTest
: public GCMDriverTest
{
1004 GCMChannelStatusSyncerTest();
1005 ~GCMChannelStatusSyncerTest() override
;
1008 void SetUp() override
;
1010 void CompleteGCMChannelStatusRequest(bool enabled
, int poll_interval_seconds
);
1011 bool CompareDelaySeconds(int64 expected_delay_seconds
,
1012 int64 actual_delay_seconds
);
1014 GCMChannelStatusSyncer
* syncer() {
1015 return driver()->gcm_channel_status_syncer_for_testing();
1019 net::TestURLFetcherFactory url_fetcher_factory_
;
1021 DISALLOW_COPY_AND_ASSIGN(GCMChannelStatusSyncerTest
);
1024 GCMChannelStatusSyncerTest::GCMChannelStatusSyncerTest() {
1027 GCMChannelStatusSyncerTest::~GCMChannelStatusSyncerTest() {
1030 void GCMChannelStatusSyncerTest::SetUp() {
1031 GCMDriverTest::SetUp();
1033 url_fetcher_factory_
.set_remove_fetcher_on_delete(true);
1035 // Turn on all-user support.
1036 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("GCM", "Enabled"));
1039 void GCMChannelStatusSyncerTest::CompleteGCMChannelStatusRequest(
1040 bool enabled
, int poll_interval_seconds
) {
1041 sync_pb::ExperimentStatusResponse response_proto
;
1042 sync_pb::ExperimentsSpecifics
* experiment_specifics
=
1043 response_proto
.add_experiment();
1044 experiment_specifics
->mutable_gcm_channel()->set_enabled(enabled
);
1046 if (poll_interval_seconds
)
1047 response_proto
.set_poll_interval_seconds(poll_interval_seconds
);
1049 std::string response_string
;
1050 response_proto
.SerializeToString(&response_string
);
1052 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
1053 ASSERT_TRUE(fetcher
);
1054 fetcher
->set_response_code(net::HTTP_OK
);
1055 fetcher
->SetResponseString(response_string
);
1056 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
1059 bool GCMChannelStatusSyncerTest::CompareDelaySeconds(
1060 int64 expected_delay_seconds
, int64 actual_delay_seconds
) {
1061 // Most of time, the actual delay should not be smaller than the expected
1063 if (actual_delay_seconds
>= expected_delay_seconds
)
1065 // It is also OK that the actual delay is a bit smaller than the expected
1066 // delay in case that the test runs slowly.
1067 return expected_delay_seconds
- actual_delay_seconds
< 30;
1070 TEST_F(GCMChannelStatusSyncerTest
, DisableAndEnable
) {
1071 // Create GCMDriver first. GCM is not started.
1072 CreateDriver(FakeGCMClient::NO_DELAY_START
);
1073 EXPECT_FALSE(driver()->IsStarted());
1075 // By default, GCM is enabled.
1076 EXPECT_TRUE(driver()->gcm_enabled());
1077 EXPECT_TRUE(syncer()->gcm_enabled());
1079 // Remove delay such that the request could be executed immediately.
1080 syncer()->set_delay_removed_for_testing(true);
1082 // GCM will be started after app handler is added.
1084 EXPECT_TRUE(driver()->IsStarted());
1086 // GCM is still enabled at this point.
1087 EXPECT_TRUE(driver()->gcm_enabled());
1088 EXPECT_TRUE(syncer()->gcm_enabled());
1090 // Wait until the GCM channel status request gets triggered.
1093 // Complete the request that disables the GCM.
1094 CompleteGCMChannelStatusRequest(false, 0);
1095 EXPECT_FALSE(driver()->gcm_enabled());
1096 EXPECT_FALSE(syncer()->gcm_enabled());
1097 EXPECT_FALSE(driver()->IsStarted());
1099 // Wait until next GCM channel status request gets triggered.
1102 // Complete the request that enables the GCM.
1103 CompleteGCMChannelStatusRequest(true, 0);
1104 EXPECT_TRUE(driver()->gcm_enabled());
1105 EXPECT_TRUE(syncer()->gcm_enabled());
1106 EXPECT_TRUE(driver()->IsStarted());
1109 TEST_F(GCMChannelStatusSyncerTest
, DisableRestartAndEnable
) {
1110 // Create GCMDriver first. GCM is not started.
1111 CreateDriver(FakeGCMClient::NO_DELAY_START
);
1112 EXPECT_FALSE(driver()->IsStarted());
1114 // By default, GCM is enabled.
1115 EXPECT_TRUE(driver()->gcm_enabled());
1116 EXPECT_TRUE(syncer()->gcm_enabled());
1118 // Remove delay such that the request could be executed immediately.
1119 syncer()->set_delay_removed_for_testing(true);
1121 // GCM will be started after app handler is added.
1123 EXPECT_TRUE(driver()->IsStarted());
1125 // GCM is still enabled at this point.
1126 EXPECT_TRUE(driver()->gcm_enabled());
1127 EXPECT_TRUE(syncer()->gcm_enabled());
1129 // Wait until the GCM channel status request gets triggered.
1132 // Complete the request that disables the GCM.
1133 CompleteGCMChannelStatusRequest(false, 0);
1134 EXPECT_FALSE(driver()->gcm_enabled());
1135 EXPECT_FALSE(syncer()->gcm_enabled());
1136 EXPECT_FALSE(driver()->IsStarted());
1138 // Simulate browser start by recreating GCMDriver.
1140 CreateDriver(FakeGCMClient::NO_DELAY_START
);
1142 // Remove delay such that the request could be executed immediately.
1143 syncer()->set_delay_removed_for_testing(true);
1145 // GCM is still disabled.
1146 EXPECT_FALSE(driver()->gcm_enabled());
1147 EXPECT_FALSE(syncer()->gcm_enabled());
1148 EXPECT_FALSE(driver()->IsStarted());
1151 EXPECT_FALSE(driver()->gcm_enabled());
1152 EXPECT_FALSE(syncer()->gcm_enabled());
1153 EXPECT_FALSE(driver()->IsStarted());
1155 // Wait until the GCM channel status request gets triggered.
1158 // Complete the request that re-enables the GCM.
1159 CompleteGCMChannelStatusRequest(true, 0);
1160 EXPECT_TRUE(driver()->gcm_enabled());
1161 EXPECT_TRUE(syncer()->gcm_enabled());
1162 EXPECT_TRUE(driver()->IsStarted());
1165 TEST_F(GCMChannelStatusSyncerTest
, FirstTimePolling
) {
1167 CreateDriver(FakeGCMClient::NO_DELAY_START
);
1170 // The 1st request should be triggered shortly without jittering.
1171 EXPECT_EQ(GCMChannelStatusSyncer::first_time_delay_seconds(),
1172 syncer()->current_request_delay_interval().InSeconds());
1175 TEST_F(GCMChannelStatusSyncerTest
, SubsequentPollingWithDefaultInterval
) {
1176 // Create GCMDriver first. GCM is not started.
1177 CreateDriver(FakeGCMClient::NO_DELAY_START
);
1179 // Remove delay such that the request could be executed immediately.
1180 syncer()->set_delay_removed_for_testing(true);
1182 // Now GCM is started.
1185 // Wait until the GCM channel status request gets triggered.
1188 // Keep delay such that we can find out the computed delay time.
1189 syncer()->set_delay_removed_for_testing(false);
1191 // Complete the request. The default interval is intact.
1192 CompleteGCMChannelStatusRequest(true, 0);
1194 // The next request should be scheduled at the expected default interval.
1195 int64 actual_delay_seconds
=
1196 syncer()->current_request_delay_interval().InSeconds();
1197 int64 expected_delay_seconds
=
1198 GCMChannelStatusRequest::default_poll_interval_seconds();
1199 EXPECT_TRUE(CompareDelaySeconds(expected_delay_seconds
, actual_delay_seconds
))
1200 << "expected delay: " << expected_delay_seconds
1201 << " actual delay: " << actual_delay_seconds
;
1203 // Simulate browser start by recreating GCMDriver.
1205 CreateDriver(FakeGCMClient::NO_DELAY_START
);
1208 // After start-up, the request should still be scheduled at the expected
1209 // default interval.
1210 actual_delay_seconds
=
1211 syncer()->current_request_delay_interval().InSeconds();
1212 EXPECT_TRUE(CompareDelaySeconds(expected_delay_seconds
, actual_delay_seconds
))
1213 << "expected delay: " << expected_delay_seconds
1214 << " actual delay: " << actual_delay_seconds
;
1217 TEST_F(GCMChannelStatusSyncerTest
, SubsequentPollingWithUpdatedInterval
) {
1218 // Create GCMDriver first. GCM is not started.
1219 CreateDriver(FakeGCMClient::NO_DELAY_START
);
1221 // Remove delay such that the request could be executed immediately.
1222 syncer()->set_delay_removed_for_testing(true);
1224 // Now GCM is started.
1227 // Wait until the GCM channel status request gets triggered.
1230 // Keep delay such that we can find out the computed delay time.
1231 syncer()->set_delay_removed_for_testing(false);
1233 // Complete the request. The interval is being changed.
1234 int new_poll_interval_seconds
=
1235 GCMChannelStatusRequest::default_poll_interval_seconds() * 2;
1236 CompleteGCMChannelStatusRequest(true, new_poll_interval_seconds
);
1238 // The next request should be scheduled at the expected updated interval.
1239 int64 actual_delay_seconds
=
1240 syncer()->current_request_delay_interval().InSeconds();
1241 int64 expected_delay_seconds
= new_poll_interval_seconds
;
1242 EXPECT_TRUE(CompareDelaySeconds(expected_delay_seconds
, actual_delay_seconds
))
1243 << "expected delay: " << expected_delay_seconds
1244 << " actual delay: " << actual_delay_seconds
;
1246 // Simulate browser start by recreating GCMDriver.
1248 CreateDriver(FakeGCMClient::NO_DELAY_START
);
1251 // After start-up, the request should still be scheduled at the expected
1252 // updated interval.
1253 actual_delay_seconds
=
1254 syncer()->current_request_delay_interval().InSeconds();
1255 EXPECT_TRUE(CompareDelaySeconds(expected_delay_seconds
, actual_delay_seconds
))
1256 << "expected delay: " << expected_delay_seconds
1257 << " actual delay: " << actual_delay_seconds
;