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 "chrome/browser/services/gcm/gcm_service.h"
8 #include "base/bind_helpers.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/location.h"
11 #include "base/run_loop.h"
12 #include "base/strings/string_util.h"
13 #include "chrome/browser/services/gcm/fake_gcm_client_factory.h"
14 #include "chrome/browser/services/gcm/gcm_app_handler.h"
15 #include "chrome/browser/services/gcm/gcm_client_factory.h"
16 #include "chrome/browser/services/gcm/gcm_client_mock.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "google_apis/gaia/fake_identity_provider.h"
20 #include "google_apis/gaia/fake_oauth2_token_service.h"
21 #include "net/url_request/url_request_context_getter.h"
22 #include "net/url_request/url_request_test_util.h"
23 #include "testing/gtest/include/gtest/gtest.h"
29 const char kTestAccountID1
[] = "user1@example.com";
30 const char kTestAccountID2
[] = "user2@example.com";
31 const char kTestAccountID3
[] = "user3@example.com";
32 const char kTestAppID1
[] = "TestApp1";
33 const char kTestAppID2
[] = "TestApp2";
34 const char kUserID1
[] = "user1";
35 const char kUserID2
[] = "user2";
37 void PumpCurrentLoop() {
38 base::RunLoop().RunUntilIdle();
46 base::RunLoop run_loop
;
47 content::BrowserThread::PostTaskAndReply(content::BrowserThread::IO
,
49 base::Bind(&PumpCurrentLoop
),
50 run_loop
.QuitClosure());
54 std::vector
<std::string
> ToSenderList(const std::string
& sender_ids
) {
55 std::vector
<std::string
> senders
;
56 Tokenize(sender_ids
, ",", &senders
);
60 class FakeGCMAppHandler
: public GCMAppHandler
{
65 MESSAGES_DELETED_EVENT
,
70 virtual ~FakeGCMAppHandler();
72 const Event
& received_event() const { return received_event_
; }
73 const std::string
& app_id() const { return app_id_
; }
74 const GCMClient::IncomingMessage
& message() const { return message_
; }
75 const GCMClient::SendErrorDetails
& send_error_details() const {
76 return send_error_details_
;
79 void WaitForNotification();
82 virtual void ShutdownHandler() OVERRIDE
;
83 virtual void OnMessage(const std::string
& app_id
,
84 const GCMClient::IncomingMessage
& message
) OVERRIDE
;
85 virtual void OnMessagesDeleted(const std::string
& app_id
) OVERRIDE
;
86 virtual void OnSendError(
87 const std::string
& app_id
,
88 const GCMClient::SendErrorDetails
& send_error_details
) OVERRIDE
;
93 scoped_ptr
<base::RunLoop
> run_loop_
;
95 Event received_event_
;
97 GCMClient::IncomingMessage message_
;
98 GCMClient::SendErrorDetails send_error_details_
;
100 DISALLOW_COPY_AND_ASSIGN(FakeGCMAppHandler
);
103 class TestGCMService
: public GCMService
{
106 bool start_automatically
,
107 scoped_ptr
<IdentityProvider
> identity_provider
,
108 const scoped_refptr
<net::URLRequestContextGetter
>& request_context
);
109 virtual ~TestGCMService();
113 virtual bool ShouldStartAutomatically() const OVERRIDE
;
114 virtual base::FilePath
GetStorePath() const OVERRIDE
;
115 virtual scoped_refptr
<net::URLRequestContextGetter
>
116 GetURLRequestContextGetter() const OVERRIDE
;
119 base::ScopedTempDir temp_dir_
;
120 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
121 const bool start_automatically_
;
123 DISALLOW_COPY_AND_ASSIGN(TestGCMService
);
126 FakeGCMAppHandler::FakeGCMAppHandler() : received_event_(NO_EVENT
) {
129 FakeGCMAppHandler::~FakeGCMAppHandler() {
132 void FakeGCMAppHandler::WaitForNotification() {
133 run_loop_
.reset(new base::RunLoop
);
138 void FakeGCMAppHandler::ShutdownHandler() {
141 void FakeGCMAppHandler::OnMessage(const std::string
& app_id
,
142 const GCMClient::IncomingMessage
& message
) {
144 received_event_
= MESSAGE_EVENT
;
151 void FakeGCMAppHandler::OnMessagesDeleted(const std::string
& app_id
) {
153 received_event_
= MESSAGES_DELETED_EVENT
;
159 void FakeGCMAppHandler::OnSendError(
160 const std::string
& app_id
,
161 const GCMClient::SendErrorDetails
& send_error_details
) {
163 received_event_
= SEND_ERROR_EVENT
;
165 send_error_details_
= send_error_details
;
170 void FakeGCMAppHandler::ClearResults() {
171 received_event_
= NO_EVENT
;
173 message_
= GCMClient::IncomingMessage();
174 send_error_details_
= GCMClient::SendErrorDetails();
177 TestGCMService::TestGCMService(
178 bool start_automatically
,
179 scoped_ptr
<IdentityProvider
> identity_provider
,
180 const scoped_refptr
<net::URLRequestContextGetter
>& request_context
)
181 : GCMService(identity_provider
.Pass()),
182 request_context_(request_context
),
183 start_automatically_(start_automatically
) {
184 if (!temp_dir_
.CreateUniqueTempDir())
188 TestGCMService::~TestGCMService() {
191 bool TestGCMService::ShouldStartAutomatically() const {
192 return start_automatically_
;
195 base::FilePath
TestGCMService::GetStorePath() const {
196 return temp_dir_
.path();
199 scoped_refptr
<net::URLRequestContextGetter
>
200 TestGCMService::GetURLRequestContextGetter() const {
201 return request_context_
;
206 class TestGCMServiceWrapper
{
213 explicit TestGCMServiceWrapper(
214 const scoped_refptr
<net::URLRequestContextGetter
>& request_context
);
215 ~TestGCMServiceWrapper();
217 TestGCMService
* service() { return service_
.get(); }
218 FakeGCMAppHandler
* gcm_app_handler() { return gcm_app_handler_
.get(); }
219 const std::string
& registration_id() const { return registration_id_
; }
220 GCMClient::Result
registration_result() const { return registration_result_
; }
221 const std::string
& send_message_id() const { return send_message_id_
; }
222 GCMClient::Result
send_result() const { return send_result_
; }
223 GCMClient::Result
unregistration_result() const {
224 return unregistration_result_
;
227 void ClearRegistrationResult();
228 void ClearUnregistrationResult();
230 bool ServiceHasAppHandlers() const;
231 GCMClientMock
* GetGCMClient();
233 void CreateService(bool start_automatically
,
234 GCMClientMock::LoadingDelay gcm_client_loading_delay
);
236 void SignIn(const std::string
& account_id
);
239 void Register(const std::string
& app_id
,
240 const std::vector
<std::string
>& sender_ids
,
241 WaitToFinish wait_to_finish
);
242 void Send(const std::string
& app_id
,
243 const std::string
& receiver_id
,
244 const GCMClient::OutgoingMessage
& message
,
245 WaitToFinish wait_to_finish
);
246 void Unregister(const std::string
& app_id
, WaitToFinish wait_to_finish
);
248 void WaitForAsyncOperation();
251 void RegisterCompleted(const std::string
& registration_id
,
252 GCMClient::Result result
);
253 void SendCompleted(const std::string
& message_id
, GCMClient::Result result
);
254 void UnregisterCompleted(GCMClient::Result result
);
256 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
257 FakeOAuth2TokenService token_service_
;
258 scoped_ptr
<FakeIdentityProvider
> identity_provider_owner_
;
259 FakeIdentityProvider
* identity_provider_
;
260 scoped_ptr
<TestGCMService
> service_
;
261 scoped_ptr
<FakeGCMAppHandler
> gcm_app_handler_
;
263 base::Closure async_operation_completed_callback_
;
265 std::string registration_id_
;
266 GCMClient::Result registration_result_
;
267 std::string send_message_id_
;
268 GCMClient::Result send_result_
;
269 GCMClient::Result unregistration_result_
;
271 DISALLOW_COPY_AND_ASSIGN(TestGCMServiceWrapper
);
274 TestGCMServiceWrapper::TestGCMServiceWrapper(
275 const scoped_refptr
<net::URLRequestContextGetter
>& request_context
)
276 : request_context_(request_context
),
277 identity_provider_(NULL
),
278 registration_result_(GCMClient::UNKNOWN_ERROR
),
279 send_result_(GCMClient::UNKNOWN_ERROR
),
280 unregistration_result_(GCMClient::UNKNOWN_ERROR
) {
281 identity_provider_owner_
.reset(new FakeIdentityProvider(&token_service_
));
282 identity_provider_
= identity_provider_owner_
.get();
285 TestGCMServiceWrapper::~TestGCMServiceWrapper() {
289 service_
->ShutdownService();
294 void TestGCMServiceWrapper::ClearRegistrationResult() {
295 registration_id_
.clear();
296 registration_result_
= GCMClient::UNKNOWN_ERROR
;
299 void TestGCMServiceWrapper::ClearUnregistrationResult() {
300 unregistration_result_
= GCMClient::UNKNOWN_ERROR
;
303 bool TestGCMServiceWrapper::ServiceHasAppHandlers() const {
304 return !service_
->app_handlers_
.empty();
307 GCMClientMock
* TestGCMServiceWrapper::GetGCMClient() {
308 return static_cast<GCMClientMock
*>(service_
->GetGCMClientForTesting());
311 void TestGCMServiceWrapper::CreateService(
312 bool start_automatically
,
313 GCMClientMock::LoadingDelay gcm_client_loading_delay
) {
314 service_
.reset(new TestGCMService(
316 identity_provider_owner_
.PassAs
<IdentityProvider
>(),
318 service_
->Initialize(scoped_ptr
<GCMClientFactory
>(
319 new FakeGCMClientFactory(gcm_client_loading_delay
)));
321 gcm_app_handler_
.reset(new FakeGCMAppHandler
);
322 service_
->AddAppHandler(kTestAppID1
, gcm_app_handler_
.get());
323 service_
->AddAppHandler(kTestAppID2
, gcm_app_handler_
.get());
326 void TestGCMServiceWrapper::SignIn(const std::string
& account_id
) {
327 token_service_
.AddAccount(account_id
);
328 identity_provider_
->LogIn(account_id
);
333 void TestGCMServiceWrapper::SignOut() {
334 identity_provider_
->LogOut();
339 void TestGCMServiceWrapper::Register(const std::string
& app_id
,
340 const std::vector
<std::string
>& sender_ids
,
341 WaitToFinish wait_to_finish
) {
342 base::RunLoop run_loop
;
343 async_operation_completed_callback_
= run_loop
.QuitClosure();
344 service_
->Register(app_id
,
346 base::Bind(&TestGCMServiceWrapper::RegisterCompleted
,
347 base::Unretained(this)));
348 if (wait_to_finish
== WAIT
)
352 void TestGCMServiceWrapper::Send(const std::string
& app_id
,
353 const std::string
& receiver_id
,
354 const GCMClient::OutgoingMessage
& message
,
355 WaitToFinish wait_to_finish
) {
356 base::RunLoop run_loop
;
357 async_operation_completed_callback_
= run_loop
.QuitClosure();
358 service_
->Send(app_id
,
361 base::Bind(&TestGCMServiceWrapper::SendCompleted
,
362 base::Unretained(this)));
363 if (wait_to_finish
== WAIT
)
367 void TestGCMServiceWrapper::Unregister(const std::string
& app_id
,
368 WaitToFinish wait_to_finish
) {
369 base::RunLoop run_loop
;
370 async_operation_completed_callback_
= run_loop
.QuitClosure();
371 service_
->Unregister(app_id
,
372 base::Bind(&TestGCMServiceWrapper::UnregisterCompleted
,
373 base::Unretained(this)));
374 if (wait_to_finish
== WAIT
)
378 void TestGCMServiceWrapper::WaitForAsyncOperation() {
379 base::RunLoop run_loop
;
380 async_operation_completed_callback_
= run_loop
.QuitClosure();
384 void TestGCMServiceWrapper::RegisterCompleted(
385 const std::string
& registration_id
,
386 GCMClient::Result result
) {
387 registration_id_
= registration_id
;
388 registration_result_
= result
;
389 if (!async_operation_completed_callback_
.is_null())
390 async_operation_completed_callback_
.Run();
393 void TestGCMServiceWrapper::SendCompleted(const std::string
& message_id
,
394 GCMClient::Result result
) {
395 send_message_id_
= message_id
;
396 send_result_
= result
;
397 if (!async_operation_completed_callback_
.is_null())
398 async_operation_completed_callback_
.Run();
401 void TestGCMServiceWrapper::UnregisterCompleted(GCMClient::Result result
) {
402 unregistration_result_
= result
;
403 if (!async_operation_completed_callback_
.is_null())
404 async_operation_completed_callback_
.Run();
407 class GCMServiceTest
: public testing::Test
{
410 virtual ~GCMServiceTest();
413 virtual void SetUp() OVERRIDE
;
414 virtual void TearDown() OVERRIDE
;
416 scoped_ptr
<content::TestBrowserThreadBundle
> thread_bundle_
;
417 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
418 scoped_ptr
<TestGCMServiceWrapper
> wrapper_
;
421 DISALLOW_COPY_AND_ASSIGN(GCMServiceTest
);
424 GCMServiceTest::GCMServiceTest() {
427 GCMServiceTest::~GCMServiceTest() {
430 void GCMServiceTest::SetUp() {
431 thread_bundle_
.reset(new content::TestBrowserThreadBundle(
432 content::TestBrowserThreadBundle::REAL_IO_THREAD
));
433 request_context_
= new net::TestURLRequestContextGetter(
434 content::BrowserThread::GetMessageLoopProxyForThread(
435 content::BrowserThread::IO
));
436 wrapper_
.reset(new TestGCMServiceWrapper(request_context_
));
439 void GCMServiceTest::TearDown() {
443 TEST_F(GCMServiceTest
, CreateGCMServiceBeforeSignIn
) {
444 // Create CreateGMCService first.
445 wrapper_
->CreateService(true, GCMClientMock::NO_DELAY_LOADING
);
446 EXPECT_FALSE(wrapper_
->service()->IsStarted());
448 // Sign in. This will kick off the check-in.
449 wrapper_
->SignIn(kTestAccountID1
);
450 EXPECT_TRUE(wrapper_
->service()->IsStarted());
453 TEST_F(GCMServiceTest
, CreateGCMServiceAfterSignIn
) {
454 // Sign in. This will not initiate the check-in.
455 wrapper_
->SignIn(kTestAccountID1
);
457 // Create GCMeService after sign-in.
458 wrapper_
->CreateService(true, GCMClientMock::NO_DELAY_LOADING
);
459 EXPECT_TRUE(wrapper_
->service()->IsStarted());
462 TEST_F(GCMServiceTest
, Shutdown
) {
463 wrapper_
->CreateService(true, GCMClientMock::NO_DELAY_LOADING
);
464 EXPECT_TRUE(wrapper_
->ServiceHasAppHandlers());
466 wrapper_
->service()->ShutdownService();
467 EXPECT_FALSE(wrapper_
->ServiceHasAppHandlers());
470 TEST_F(GCMServiceTest
, SignInAndSignOutUnderPositiveChannelSignal
) {
471 wrapper_
->CreateService(true, GCMClientMock::NO_DELAY_LOADING
);
472 wrapper_
->SignIn(kTestAccountID1
);
474 // GCMClient should be loaded.
475 EXPECT_TRUE(wrapper_
->service()->IsGCMClientReady());
476 EXPECT_EQ(GCMClientMock::LOADED
, wrapper_
->GetGCMClient()->status());
480 // GCMClient should be checked out.
481 EXPECT_FALSE(wrapper_
->service()->IsGCMClientReady());
482 EXPECT_EQ(GCMClientMock::CHECKED_OUT
, wrapper_
->GetGCMClient()->status());
485 TEST_F(GCMServiceTest
, SignInAndSignOutUnderNonPositiveChannelSignal
) {
486 // Non-positive channel signal will prevent GCMClient from checking in during
488 wrapper_
->CreateService(false, GCMClientMock::NO_DELAY_LOADING
);
489 wrapper_
->SignIn(kTestAccountID1
);
491 // GCMClient should not be loaded.
492 EXPECT_FALSE(wrapper_
->service()->IsGCMClientReady());
493 EXPECT_EQ(GCMClientMock::UNINITIALIZED
, wrapper_
->GetGCMClient()->status());
497 // Check-out should still be performed.
498 EXPECT_FALSE(wrapper_
->service()->IsGCMClientReady());
499 EXPECT_EQ(GCMClientMock::CHECKED_OUT
, wrapper_
->GetGCMClient()->status());
502 TEST_F(GCMServiceTest
, SignOutAndThenSignIn
) {
503 wrapper_
->CreateService(true, GCMClientMock::NO_DELAY_LOADING
);
504 wrapper_
->SignIn(kTestAccountID1
);
506 // GCMClient should be loaded.
507 EXPECT_TRUE(wrapper_
->service()->IsGCMClientReady());
508 EXPECT_EQ(GCMClientMock::LOADED
, wrapper_
->GetGCMClient()->status());
512 // GCMClient should be checked out.
513 EXPECT_FALSE(wrapper_
->service()->IsGCMClientReady());
514 EXPECT_EQ(GCMClientMock::CHECKED_OUT
, wrapper_
->GetGCMClient()->status());
516 // Sign-in with a different account.
517 wrapper_
->SignIn(kTestAccountID2
);
519 // GCMClient should be loaded again.
520 EXPECT_TRUE(wrapper_
->service()->IsGCMClientReady());
521 EXPECT_EQ(GCMClientMock::LOADED
, wrapper_
->GetGCMClient()->status());
524 TEST_F(GCMServiceTest
, StopAndRestartGCM
) {
525 wrapper_
->CreateService(true, GCMClientMock::NO_DELAY_LOADING
);
526 wrapper_
->SignIn(kTestAccountID1
);
528 // GCMClient should be loaded.
529 EXPECT_TRUE(wrapper_
->service()->IsGCMClientReady());
530 EXPECT_EQ(GCMClientMock::LOADED
, wrapper_
->GetGCMClient()->status());
533 wrapper_
->service()->Stop();
537 // GCMClient should be stopped.
538 EXPECT_FALSE(wrapper_
->service()->IsGCMClientReady());
539 EXPECT_EQ(GCMClientMock::STOPPED
, wrapper_
->GetGCMClient()->status());
542 wrapper_
->service()->Start();
546 // GCMClient should be loaded.
547 EXPECT_TRUE(wrapper_
->service()->IsGCMClientReady());
548 EXPECT_EQ(GCMClientMock::LOADED
, wrapper_
->GetGCMClient()->status());
551 wrapper_
->service()->Stop();
555 // GCMClient should be stopped.
556 EXPECT_FALSE(wrapper_
->service()->IsGCMClientReady());
557 EXPECT_EQ(GCMClientMock::STOPPED
, wrapper_
->GetGCMClient()->status());
562 // GCMClient should be checked out.
563 EXPECT_FALSE(wrapper_
->service()->IsGCMClientReady());
564 EXPECT_EQ(GCMClientMock::CHECKED_OUT
, wrapper_
->GetGCMClient()->status());
567 TEST_F(GCMServiceTest
, RegisterWhenNotSignedIn
) {
568 wrapper_
->CreateService(true, GCMClientMock::NO_DELAY_LOADING
);
570 std::vector
<std::string
> sender_ids
;
571 sender_ids
.push_back("sender1");
572 wrapper_
->Register(kTestAppID1
, sender_ids
, TestGCMServiceWrapper::WAIT
);
574 EXPECT_TRUE(wrapper_
->registration_id().empty());
575 EXPECT_EQ(GCMClient::NOT_SIGNED_IN
, wrapper_
->registration_result());
578 TEST_F(GCMServiceTest
, RegisterUnderNonPositiveChannelSignal
) {
579 // Non-positive channel signal will prevent GCMClient from checking in during
581 wrapper_
->CreateService(false, GCMClientMock::NO_DELAY_LOADING
);
582 wrapper_
->SignIn(kTestAccountID1
);
584 // GCMClient should not be checked in.
585 EXPECT_FALSE(wrapper_
->service()->IsGCMClientReady());
586 EXPECT_EQ(GCMClientMock::UNINITIALIZED
, wrapper_
->GetGCMClient()->status());
588 // Invoking register will make GCMClient checked in.
589 std::vector
<std::string
> sender_ids
;
590 sender_ids
.push_back("sender1");
591 wrapper_
->Register(kTestAppID1
, sender_ids
, TestGCMServiceWrapper::WAIT
);
593 // GCMClient should be checked in.
594 EXPECT_TRUE(wrapper_
->service()->IsGCMClientReady());
595 EXPECT_EQ(GCMClientMock::LOADED
, wrapper_
->GetGCMClient()->status());
597 // Registration should succeed.
598 const std::string expected_registration_id
=
599 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids
);
600 EXPECT_EQ(expected_registration_id
, wrapper_
->registration_id());
601 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->registration_result());
604 TEST_F(GCMServiceTest
, SendWhenNotSignedIn
) {
605 wrapper_
->CreateService(true, GCMClientMock::NO_DELAY_LOADING
);
607 GCMClient::OutgoingMessage message
;
609 message
.data
["key1"] = "value1";
610 wrapper_
->Send(kTestAppID1
, kUserID1
, message
, TestGCMServiceWrapper::WAIT
);
612 EXPECT_TRUE(wrapper_
->send_message_id().empty());
613 EXPECT_EQ(GCMClient::NOT_SIGNED_IN
, wrapper_
->send_result());
616 TEST_F(GCMServiceTest
, SendUnderNonPositiveChannelSignal
) {
617 // Non-positive channel signal will prevent GCMClient from checking in during
619 wrapper_
->CreateService(false, GCMClientMock::NO_DELAY_LOADING
);
620 wrapper_
->SignIn(kTestAccountID1
);
622 // GCMClient should not be checked in.
623 EXPECT_FALSE(wrapper_
->service()->IsGCMClientReady());
624 EXPECT_EQ(GCMClientMock::UNINITIALIZED
, wrapper_
->GetGCMClient()->status());
626 // Invoking send will make GCMClient checked in.
627 GCMClient::OutgoingMessage message
;
629 message
.data
["key1"] = "value1";
630 wrapper_
->Send(kTestAppID1
, kUserID1
, message
, TestGCMServiceWrapper::WAIT
);
632 // GCMClient should be checked in.
633 EXPECT_TRUE(wrapper_
->service()->IsGCMClientReady());
634 EXPECT_EQ(GCMClientMock::LOADED
, wrapper_
->GetGCMClient()->status());
636 // Sending should succeed.
637 EXPECT_EQ(message
.id
, wrapper_
->send_message_id());
638 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->send_result());
641 // Tests a single instance of GCMService.
642 class GCMServiceSingleInstanceTest
: public GCMServiceTest
{
644 GCMServiceSingleInstanceTest();
645 virtual ~GCMServiceSingleInstanceTest();
648 virtual void SetUp() OVERRIDE
;
651 DISALLOW_COPY_AND_ASSIGN(GCMServiceSingleInstanceTest
);
654 GCMServiceSingleInstanceTest::GCMServiceSingleInstanceTest() {
657 GCMServiceSingleInstanceTest::~GCMServiceSingleInstanceTest() {
660 void GCMServiceSingleInstanceTest::SetUp() {
661 GCMServiceTest::SetUp();
663 wrapper_
->CreateService(true, GCMClientMock::NO_DELAY_LOADING
);
664 wrapper_
->SignIn(kTestAccountID1
);
667 TEST_F(GCMServiceSingleInstanceTest
, Register
) {
668 std::vector
<std::string
> sender_ids
;
669 sender_ids
.push_back("sender1");
670 wrapper_
->Register(kTestAppID1
, sender_ids
, TestGCMServiceWrapper::WAIT
);
671 const std::string expected_registration_id
=
672 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids
);
674 EXPECT_EQ(expected_registration_id
, wrapper_
->registration_id());
675 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->registration_result());
678 TEST_F(GCMServiceSingleInstanceTest
, RegisterError
) {
679 std::vector
<std::string
> sender_ids
;
680 sender_ids
.push_back("sender1@error");
681 wrapper_
->Register(kTestAppID1
, sender_ids
, TestGCMServiceWrapper::WAIT
);
683 EXPECT_TRUE(wrapper_
->registration_id().empty());
684 EXPECT_NE(GCMClient::SUCCESS
, wrapper_
->registration_result());
687 TEST_F(GCMServiceSingleInstanceTest
, RegisterAgainWithSameSenderIDs
) {
688 std::vector
<std::string
> sender_ids
;
689 sender_ids
.push_back("sender1");
690 sender_ids
.push_back("sender2");
691 wrapper_
->Register(kTestAppID1
, sender_ids
, TestGCMServiceWrapper::WAIT
);
692 const std::string expected_registration_id
=
693 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids
);
695 EXPECT_EQ(expected_registration_id
, wrapper_
->registration_id());
696 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->registration_result());
698 // Clears the results the would be set by the Register callback in preparation
699 // to call register 2nd time.
700 wrapper_
->ClearRegistrationResult();
702 // Calling register 2nd time with the same set of sender IDs but different
703 // ordering will get back the same registration ID.
704 std::vector
<std::string
> another_sender_ids
;
705 another_sender_ids
.push_back("sender2");
706 another_sender_ids
.push_back("sender1");
707 wrapper_
->Register(kTestAppID1
,
709 TestGCMServiceWrapper::WAIT
);
711 EXPECT_EQ(expected_registration_id
, wrapper_
->registration_id());
712 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->registration_result());
715 TEST_F(GCMServiceSingleInstanceTest
, RegisterAgainWithDifferentSenderIDs
) {
716 std::vector
<std::string
> sender_ids
;
717 sender_ids
.push_back("sender1");
718 wrapper_
->Register(kTestAppID1
, sender_ids
, TestGCMServiceWrapper::WAIT
);
719 const std::string expected_registration_id
=
720 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids
);
722 EXPECT_EQ(expected_registration_id
, wrapper_
->registration_id());
723 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->registration_result());
725 // Make sender IDs different.
726 sender_ids
.push_back("sender2");
727 const std::string expected_registration_id2
=
728 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids
);
730 // Calling register 2nd time with the different sender IDs will get back a new
732 wrapper_
->Register(kTestAppID1
, sender_ids
, TestGCMServiceWrapper::WAIT
);
733 EXPECT_EQ(expected_registration_id2
, wrapper_
->registration_id());
734 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->registration_result());
737 TEST_F(GCMServiceSingleInstanceTest
, GCMClientNotReadyBeforeRegistration
) {
738 // Make GCMClient not ready initially.
739 wrapper_
.reset(new TestGCMServiceWrapper(request_context_
));
740 wrapper_
->CreateService(true, GCMClientMock::DELAY_LOADING
);
741 wrapper_
->SignIn(kTestAccountID1
);
743 // The registration is on hold until GCMClient is ready.
744 std::vector
<std::string
> sender_ids
;
745 sender_ids
.push_back("sender1");
746 wrapper_
->Register(kTestAppID1
,
748 TestGCMServiceWrapper::DO_NOT_WAIT
);
751 EXPECT_TRUE(wrapper_
->registration_id().empty());
752 EXPECT_EQ(GCMClient::UNKNOWN_ERROR
, wrapper_
->registration_result());
754 // Register operation will be invoked after GCMClient becomes ready.
755 wrapper_
->GetGCMClient()->PerformDelayedLoading();
756 wrapper_
->WaitForAsyncOperation();
757 EXPECT_FALSE(wrapper_
->registration_id().empty());
758 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->registration_result());
761 TEST_F(GCMServiceSingleInstanceTest
, RegisterAfterSignOut
) {
762 // This will trigger check-out.
765 std::vector
<std::string
> sender_ids
;
766 sender_ids
.push_back("sender1");
767 wrapper_
->Register(kTestAppID1
, sender_ids
, TestGCMServiceWrapper::WAIT
);
769 EXPECT_TRUE(wrapper_
->registration_id().empty());
770 EXPECT_EQ(GCMClient::NOT_SIGNED_IN
, wrapper_
->registration_result());
773 TEST_F(GCMServiceSingleInstanceTest
, UnregisterExplicitly
) {
774 std::vector
<std::string
> sender_ids
;
775 sender_ids
.push_back("sender1");
776 wrapper_
->Register(kTestAppID1
, sender_ids
, TestGCMServiceWrapper::WAIT
);
778 EXPECT_FALSE(wrapper_
->registration_id().empty());
779 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->registration_result());
781 wrapper_
->Unregister(kTestAppID1
, TestGCMServiceWrapper::WAIT
);
783 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->unregistration_result());
786 TEST_F(GCMServiceSingleInstanceTest
, UnregisterWhenAsyncOperationPending
) {
787 std::vector
<std::string
> sender_ids
;
788 sender_ids
.push_back("sender1");
789 // First start registration without waiting for it to complete.
790 wrapper_
->Register(kTestAppID1
,
792 TestGCMServiceWrapper::DO_NOT_WAIT
);
794 // Test that unregistration fails with async operation pending when there is a
795 // registration already in progress.
796 wrapper_
->Unregister(kTestAppID1
, TestGCMServiceWrapper::WAIT
);
797 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING
,
798 wrapper_
->unregistration_result());
800 // Complete the unregistration.
801 wrapper_
->WaitForAsyncOperation();
802 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->registration_result());
804 // Start unregistration without waiting for it to complete. This time no async
805 // operation is pending.
806 wrapper_
->Unregister(kTestAppID1
, TestGCMServiceWrapper::DO_NOT_WAIT
);
808 // Test that unregistration fails with async operation pending when there is
809 // an unregistration already in progress.
810 wrapper_
->Unregister(kTestAppID1
, TestGCMServiceWrapper::WAIT
);
811 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING
,
812 wrapper_
->unregistration_result());
813 wrapper_
->ClearUnregistrationResult();
815 // Complete unregistration.
816 wrapper_
->WaitForAsyncOperation();
817 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->unregistration_result());
820 TEST_F(GCMServiceSingleInstanceTest
, RegisterWhenAsyncOperationPending
) {
821 std::vector
<std::string
> sender_ids
;
822 sender_ids
.push_back("sender1");
823 // First start registration without waiting for it to complete.
824 wrapper_
->Register(kTestAppID1
,
826 TestGCMServiceWrapper::DO_NOT_WAIT
);
828 // Test that registration fails with async operation pending when there is a
829 // registration already in progress.
830 wrapper_
->Register(kTestAppID1
, sender_ids
, TestGCMServiceWrapper::WAIT
);
831 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING
,
832 wrapper_
->registration_result());
833 wrapper_
->ClearRegistrationResult();
835 // Complete the registration.
836 wrapper_
->WaitForAsyncOperation();
837 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->registration_result());
839 // Start unregistration without waiting for it to complete. This time no async
840 // operation is pending.
841 wrapper_
->Unregister(kTestAppID1
, TestGCMServiceWrapper::DO_NOT_WAIT
);
843 // Test that registration fails with async operation pending when there is an
844 // unregistration already in progress.
845 wrapper_
->Register(kTestAppID1
, sender_ids
, TestGCMServiceWrapper::WAIT
);
846 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING
,
847 wrapper_
->registration_result());
849 // Complete the first unregistration expecting success.
850 wrapper_
->WaitForAsyncOperation();
851 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->unregistration_result());
853 // Test that it is ok to register again after unregistration.
854 wrapper_
->Register(kTestAppID1
, sender_ids
, TestGCMServiceWrapper::WAIT
);
855 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->registration_result());
858 TEST_F(GCMServiceSingleInstanceTest
, Send
) {
859 GCMClient::OutgoingMessage message
;
861 message
.data
["key1"] = "value1";
862 message
.data
["key2"] = "value2";
863 wrapper_
->Send(kTestAppID1
, kUserID1
, message
, TestGCMServiceWrapper::WAIT
);
865 EXPECT_EQ(message
.id
, wrapper_
->send_message_id());
866 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->send_result());
869 TEST_F(GCMServiceSingleInstanceTest
, GCMClientNotReadyBeforeSending
) {
870 // Make GCMClient not ready initially.
871 wrapper_
.reset(new TestGCMServiceWrapper(request_context_
));
872 wrapper_
->CreateService(true, GCMClientMock::DELAY_LOADING
);
873 wrapper_
->SignIn(kTestAccountID1
);
875 // The sending is on hold until GCMClient is ready.
876 GCMClient::OutgoingMessage message
;
878 message
.data
["key1"] = "value1";
879 message
.data
["key2"] = "value2";
880 wrapper_
->Send(kTestAppID1
,
883 TestGCMServiceWrapper::DO_NOT_WAIT
);
887 EXPECT_TRUE(wrapper_
->send_message_id().empty());
888 EXPECT_EQ(GCMClient::UNKNOWN_ERROR
, wrapper_
->send_result());
890 // Send operation will be invoked after GCMClient becomes ready.
891 wrapper_
->GetGCMClient()->PerformDelayedLoading();
892 wrapper_
->WaitForAsyncOperation();
893 EXPECT_EQ(message
.id
, wrapper_
->send_message_id());
894 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->send_result());
897 TEST_F(GCMServiceSingleInstanceTest
, SendAfterSignOut
) {
898 // This will trigger check-out.
901 GCMClient::OutgoingMessage message
;
903 message
.data
["key1"] = "value1";
904 message
.data
["key2"] = "value2";
905 wrapper_
->Send(kTestAppID1
, kUserID1
, message
, TestGCMServiceWrapper::WAIT
);
907 EXPECT_TRUE(wrapper_
->send_message_id().empty());
908 EXPECT_EQ(GCMClient::NOT_SIGNED_IN
, wrapper_
->send_result());
911 TEST_F(GCMServiceSingleInstanceTest
, SendError
) {
912 GCMClient::OutgoingMessage message
;
913 // Embedding error in id will tell the mock to simulate the send error.
914 message
.id
= "1@error";
915 message
.data
["key1"] = "value1";
916 message
.data
["key2"] = "value2";
917 wrapper_
->Send(kTestAppID1
, kUserID1
, message
, TestGCMServiceWrapper::WAIT
);
919 EXPECT_EQ(message
.id
, wrapper_
->send_message_id());
920 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->send_result());
922 // Wait for the send error.
923 wrapper_
->gcm_app_handler()->WaitForNotification();
924 EXPECT_EQ(FakeGCMAppHandler::SEND_ERROR_EVENT
,
925 wrapper_
->gcm_app_handler()->received_event());
926 EXPECT_EQ(kTestAppID1
, wrapper_
->gcm_app_handler()->app_id());
927 EXPECT_EQ(message
.id
,
928 wrapper_
->gcm_app_handler()->send_error_details().message_id
);
929 EXPECT_NE(GCMClient::SUCCESS
,
930 wrapper_
->gcm_app_handler()->send_error_details().result
);
931 EXPECT_EQ(message
.data
,
932 wrapper_
->gcm_app_handler()->send_error_details().additional_data
);
935 TEST_F(GCMServiceSingleInstanceTest
, MessageReceived
) {
936 wrapper_
->Register(kTestAppID1
,
937 ToSenderList("sender"),
938 TestGCMServiceWrapper::WAIT
);
939 GCMClient::IncomingMessage message
;
940 message
.data
["key1"] = "value1";
941 message
.data
["key2"] = "value2";
942 message
.sender_id
= "sender";
943 wrapper_
->GetGCMClient()->ReceiveMessage(kTestAppID1
, message
);
944 wrapper_
->gcm_app_handler()->WaitForNotification();
945 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT
,
946 wrapper_
->gcm_app_handler()->received_event());
947 EXPECT_EQ(kTestAppID1
, wrapper_
->gcm_app_handler()->app_id());
948 EXPECT_EQ(message
.data
, wrapper_
->gcm_app_handler()->message().data
);
949 EXPECT_TRUE(wrapper_
->gcm_app_handler()->message().collapse_key
.empty());
950 EXPECT_EQ(message
.sender_id
,
951 wrapper_
->gcm_app_handler()->message().sender_id
);
954 TEST_F(GCMServiceSingleInstanceTest
, MessageWithCollapseKeyReceived
) {
955 wrapper_
->Register(kTestAppID1
,
956 ToSenderList("sender"),
957 TestGCMServiceWrapper::WAIT
);
958 GCMClient::IncomingMessage message
;
959 message
.data
["key1"] = "value1";
960 message
.collapse_key
= "collapse_key_value";
961 message
.sender_id
= "sender";
962 wrapper_
->GetGCMClient()->ReceiveMessage(kTestAppID1
, message
);
963 wrapper_
->gcm_app_handler()->WaitForNotification();
964 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT
,
965 wrapper_
->gcm_app_handler()->received_event());
966 EXPECT_EQ(kTestAppID1
, wrapper_
->gcm_app_handler()->app_id());
967 EXPECT_EQ(message
.data
, wrapper_
->gcm_app_handler()->message().data
);
968 EXPECT_EQ(message
.collapse_key
,
969 wrapper_
->gcm_app_handler()->message().collapse_key
);
972 TEST_F(GCMServiceSingleInstanceTest
, MessagesDeleted
) {
973 wrapper_
->GetGCMClient()->DeleteMessages(kTestAppID1
);
974 wrapper_
->gcm_app_handler()->WaitForNotification();
975 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT
,
976 wrapper_
->gcm_app_handler()->received_event());
977 EXPECT_EQ(kTestAppID1
, wrapper_
->gcm_app_handler()->app_id());
980 // Tests to make sure that concurrent GCMService instances work correctly
981 // regardless how GCMClient is created.
982 class GCMServiceMultipleInstanceTest
: public GCMServiceTest
{
984 GCMServiceMultipleInstanceTest();
985 virtual ~GCMServiceMultipleInstanceTest();
988 virtual void SetUp() OVERRIDE
;
989 virtual void TearDown() OVERRIDE
;
991 scoped_ptr
<TestGCMServiceWrapper
> wrapper2_
;
994 DISALLOW_COPY_AND_ASSIGN(GCMServiceMultipleInstanceTest
);
997 GCMServiceMultipleInstanceTest::GCMServiceMultipleInstanceTest() {
1000 GCMServiceMultipleInstanceTest::~GCMServiceMultipleInstanceTest() {
1003 void GCMServiceMultipleInstanceTest::SetUp() {
1004 GCMServiceTest::SetUp();
1006 wrapper2_
.reset(new TestGCMServiceWrapper(request_context_
));
1008 wrapper_
->CreateService(true, GCMClientMock::NO_DELAY_LOADING
);
1009 wrapper2_
->CreateService(true, GCMClientMock::NO_DELAY_LOADING
);
1011 // Initiate check-in for each instance.
1012 wrapper_
->SignIn(kTestAccountID1
);
1013 wrapper2_
->SignIn(kTestAccountID2
);
1016 void GCMServiceMultipleInstanceTest::TearDown() {
1020 TEST_F(GCMServiceMultipleInstanceTest
, Register
) {
1022 std::vector
<std::string
> sender_ids
;
1023 sender_ids
.push_back("sender1");
1024 wrapper_
->Register(kTestAppID1
, sender_ids
, TestGCMServiceWrapper::WAIT
);
1026 // Register the same app in a different instance.
1027 std::vector
<std::string
> sender_ids2
;
1028 sender_ids2
.push_back("foo");
1029 sender_ids2
.push_back("bar");
1030 wrapper2_
->Register(kTestAppID1
, sender_ids2
, TestGCMServiceWrapper::WAIT
);
1032 EXPECT_EQ(GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids
),
1033 wrapper_
->registration_id());
1034 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->registration_result());
1036 EXPECT_EQ(GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids2
),
1037 wrapper2_
->registration_id());
1038 EXPECT_EQ(GCMClient::SUCCESS
, wrapper2_
->registration_result());
1040 // Register a different app in a different instance.
1041 std::vector
<std::string
> sender_ids3
;
1042 sender_ids3
.push_back("sender1");
1043 sender_ids3
.push_back("sender2");
1044 sender_ids3
.push_back("sender3");
1045 wrapper2_
->Register(kTestAppID2
, sender_ids3
, TestGCMServiceWrapper::WAIT
);
1047 EXPECT_EQ(GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids3
),
1048 wrapper2_
->registration_id());
1049 EXPECT_EQ(GCMClient::SUCCESS
, wrapper2_
->registration_result());
1052 TEST_F(GCMServiceMultipleInstanceTest
, Send
) {
1053 // Send a message from one app in one instance.
1054 GCMClient::OutgoingMessage message
;
1056 message
.data
["key1"] = "value1";
1057 message
.data
["key2"] = "value2";
1058 wrapper_
->Send(kTestAppID1
, kUserID1
, message
, TestGCMServiceWrapper::WAIT
);
1060 // Send a message from same app in another instance.
1061 GCMClient::OutgoingMessage message2
;
1063 message2
.data
["foo"] = "bar";
1064 wrapper2_
->Send(kTestAppID1
, kUserID2
, message2
, TestGCMServiceWrapper::WAIT
);
1066 EXPECT_EQ(message
.id
, wrapper_
->send_message_id());
1067 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->send_result());
1069 EXPECT_EQ(message2
.id
, wrapper2_
->send_message_id());
1070 EXPECT_EQ(GCMClient::SUCCESS
, wrapper2_
->send_result());
1072 // Send another message from different app in another instance.
1073 GCMClient::OutgoingMessage message3
;
1075 message3
.data
["hello"] = "world";
1076 wrapper2_
->Send(kTestAppID2
, kUserID1
, message3
, TestGCMServiceWrapper::WAIT
);
1078 EXPECT_EQ(message3
.id
, wrapper2_
->send_message_id());
1079 EXPECT_EQ(GCMClient::SUCCESS
, wrapper2_
->send_result());
1082 TEST_F(GCMServiceMultipleInstanceTest
, MessageReceived
) {
1083 wrapper_
->Register(kTestAppID1
,
1084 ToSenderList("sender"),
1085 TestGCMServiceWrapper::WAIT
);
1086 wrapper2_
->Register(kTestAppID1
,
1087 ToSenderList("sender"),
1088 TestGCMServiceWrapper::WAIT
);
1089 wrapper2_
->Register(kTestAppID2
,
1090 ToSenderList("sender2"),
1091 TestGCMServiceWrapper::WAIT
);
1093 // Trigger an incoming message for an app in one instance.
1094 GCMClient::IncomingMessage message
;
1095 message
.data
["key1"] = "value1";
1096 message
.data
["key2"] = "value2";
1097 message
.sender_id
= "sender";
1098 wrapper_
->GetGCMClient()->ReceiveMessage(kTestAppID1
, message
);
1099 wrapper_
->gcm_app_handler()->WaitForNotification();
1101 // Trigger an incoming message for the same app in another instance.
1102 GCMClient::IncomingMessage message2
;
1103 message2
.data
["foo"] = "bar";
1104 message2
.sender_id
= "sender";
1105 wrapper2_
->GetGCMClient()->ReceiveMessage(kTestAppID1
, message2
);
1106 wrapper2_
->gcm_app_handler()->WaitForNotification();
1108 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT
,
1109 wrapper_
->gcm_app_handler()->received_event());
1110 EXPECT_EQ(kTestAppID1
, wrapper_
->gcm_app_handler()->app_id());
1111 EXPECT_EQ(message
.data
, wrapper_
->gcm_app_handler()->message().data
);
1112 EXPECT_EQ("sender", wrapper_
->gcm_app_handler()->message().sender_id
);
1114 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT
,
1115 wrapper2_
->gcm_app_handler()->received_event());
1116 EXPECT_EQ(kTestAppID1
, wrapper2_
->gcm_app_handler()->app_id());
1117 EXPECT_EQ(message2
.data
, wrapper2_
->gcm_app_handler()->message().data
);
1118 EXPECT_EQ("sender", wrapper2_
->gcm_app_handler()->message().sender_id
);
1120 // Trigger another incoming message for a different app in another instance.
1121 GCMClient::IncomingMessage message3
;
1122 message3
.data
["bar1"] = "foo1";
1123 message3
.data
["bar2"] = "foo2";
1124 message3
.sender_id
= "sender2";
1125 wrapper2_
->GetGCMClient()->ReceiveMessage(kTestAppID2
, message3
);
1126 wrapper2_
->gcm_app_handler()->WaitForNotification();
1128 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT
,
1129 wrapper2_
->gcm_app_handler()->received_event());
1130 EXPECT_EQ(kTestAppID2
, wrapper2_
->gcm_app_handler()->app_id());
1131 EXPECT_EQ(message3
.data
, wrapper2_
->gcm_app_handler()->message().data
);
1132 EXPECT_EQ("sender2", wrapper2_
->gcm_app_handler()->message().sender_id
);
1135 // Test a set of GCM operations on multiple instances.
1136 // 1) Register 1 app in instance 1 and register 2 apps in instance 2;
1137 // 2) Send a message from instance 1;
1138 // 3) Receive a message to an app in instance 1 and receive a message for each
1139 // of the two apps in instance 2;
1140 // 4) Send a message for each of the two apps in instance 2;
1141 // 5) Sign out of instance 1.
1142 // 6) Register/send stops working for instance 1;
1143 // 7) The app in instance 2 can still receive these events;
1144 // 8) Sign into instance 1 with a different account.
1145 // 9) The message to the newly signed-in account will be routed.
1146 TEST_F(GCMServiceMultipleInstanceTest
, Combined
) {
1148 std::vector
<std::string
> sender_ids
;
1149 sender_ids
.push_back("sender1");
1150 wrapper_
->Register(kTestAppID1
, sender_ids
, TestGCMServiceWrapper::WAIT
);
1152 // Register the same app in a different instance.
1153 std::vector
<std::string
> sender_ids2
;
1154 sender_ids2
.push_back("foo");
1155 sender_ids2
.push_back("bar");
1156 wrapper2_
->Register(kTestAppID1
, sender_ids2
, TestGCMServiceWrapper::WAIT
);
1158 // Register a different app in a different instance.
1159 std::vector
<std::string
> sender_ids3
;
1160 sender_ids3
.push_back("sender1");
1161 sender_ids3
.push_back("sender2");
1162 sender_ids3
.push_back("sender3");
1163 wrapper2_
->Register(kTestAppID2
, sender_ids3
, TestGCMServiceWrapper::WAIT
);
1165 EXPECT_EQ(GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids
),
1166 wrapper_
->registration_id());
1167 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->registration_result());
1169 EXPECT_EQ(GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids3
),
1170 wrapper2_
->registration_id());
1171 EXPECT_EQ(GCMClient::SUCCESS
, wrapper2_
->registration_result());
1173 // Send a message from one instance.
1174 GCMClient::OutgoingMessage out_message
;
1175 out_message
.id
= "1";
1176 out_message
.data
["out1"] = "out_data1";
1177 out_message
.data
["out1_2"] = "out_data1_2";
1178 wrapper_
->Send(kTestAppID1
,
1181 TestGCMServiceWrapper::WAIT
);
1183 EXPECT_EQ(out_message
.id
, wrapper_
->send_message_id());
1184 EXPECT_EQ(GCMClient::SUCCESS
, wrapper_
->send_result());
1186 // Trigger an incoming message for an app in one instance.
1187 GCMClient::IncomingMessage in_message
;
1188 in_message
.data
["in1"] = "in_data1";
1189 in_message
.data
["in1_2"] = "in_data1_2";
1190 in_message
.sender_id
= "sender1";
1191 wrapper_
->GetGCMClient()->ReceiveMessage(kTestAppID1
, in_message
);
1192 wrapper_
->gcm_app_handler()->WaitForNotification();
1194 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT
,
1195 wrapper_
->gcm_app_handler()->received_event());
1196 EXPECT_EQ(kTestAppID1
, wrapper_
->gcm_app_handler()->app_id());
1197 EXPECT_EQ(in_message
.data
, wrapper_
->gcm_app_handler()->message().data
);
1199 // Trigger 2 incoming messages, one for each app respectively, in another
1201 GCMClient::IncomingMessage in_message2
;
1202 in_message2
.data
["in2"] = "in_data2";
1203 in_message2
.sender_id
= "sender3";
1204 wrapper2_
->GetGCMClient()->ReceiveMessage(kTestAppID2
, in_message2
);
1206 GCMClient::IncomingMessage in_message3
;
1207 in_message3
.data
["in3"] = "in_data3";
1208 in_message3
.data
["in3_2"] = "in_data3_2";
1209 in_message3
.sender_id
= "foo";
1210 wrapper2_
->GetGCMClient()->ReceiveMessage(kTestAppID1
, in_message3
);
1212 wrapper2_
->gcm_app_handler()->WaitForNotification();
1214 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT
,
1215 wrapper2_
->gcm_app_handler()->received_event());
1216 EXPECT_EQ(kTestAppID2
, wrapper2_
->gcm_app_handler()->app_id());
1217 EXPECT_EQ(in_message2
.data
, wrapper2_
->gcm_app_handler()->message().data
);
1219 wrapper2_
->gcm_app_handler()->WaitForNotification();
1221 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT
,
1222 wrapper2_
->gcm_app_handler()->received_event());
1223 EXPECT_EQ(kTestAppID1
, wrapper2_
->gcm_app_handler()->app_id());
1224 EXPECT_EQ(in_message3
.data
, wrapper2_
->gcm_app_handler()->message().data
);
1226 // Send two messages, one for each app respectively, from another instance.
1227 GCMClient::OutgoingMessage out_message2
;
1228 out_message2
.id
= "2";
1229 out_message2
.data
["out2"] = "out_data2";
1230 wrapper2_
->Send(kTestAppID1
,
1233 TestGCMServiceWrapper::WAIT
);
1235 GCMClient::OutgoingMessage out_message3
;
1236 out_message3
.id
= "3";
1237 out_message3
.data
["out3"] = "out_data3";
1238 wrapper2_
->Send(kTestAppID2
,
1241 TestGCMServiceWrapper::DO_NOT_WAIT
);
1243 EXPECT_EQ(out_message2
.id
, wrapper2_
->send_message_id());
1244 EXPECT_EQ(GCMClient::SUCCESS
, wrapper2_
->send_result());
1246 wrapper2_
->WaitForAsyncOperation();
1248 EXPECT_EQ(out_message3
.id
, wrapper2_
->send_message_id());
1249 EXPECT_EQ(GCMClient::SUCCESS
, wrapper2_
->send_result());
1251 // Sign out of one instance.
1252 wrapper_
->SignOut();
1254 // Register/send stops working for signed-out instance.
1255 wrapper_
->Register(kTestAppID1
, sender_ids
, TestGCMServiceWrapper::WAIT
);
1256 EXPECT_TRUE(wrapper_
->registration_id().empty());
1257 EXPECT_EQ(GCMClient::NOT_SIGNED_IN
, wrapper_
->registration_result());
1259 wrapper_
->Send(kTestAppID2
,
1262 TestGCMServiceWrapper::WAIT
);
1263 EXPECT_TRUE(wrapper_
->send_message_id().empty());
1264 EXPECT_EQ(GCMClient::NOT_SIGNED_IN
, wrapper_
->send_result());
1266 // Deleted messages event will go through for another signed-in instance.
1267 wrapper2_
->GetGCMClient()->DeleteMessages(kTestAppID2
);
1268 wrapper2_
->gcm_app_handler()->WaitForNotification();
1270 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT
,
1271 wrapper2_
->gcm_app_handler()->received_event());
1272 EXPECT_EQ(kTestAppID2
, wrapper2_
->gcm_app_handler()->app_id());
1274 // Send error event will go through for another signed-in instance.
1275 GCMClient::OutgoingMessage out_message4
;
1276 out_message4
.id
= "1@error";
1277 out_message4
.data
["out4"] = "out_data4";
1278 wrapper2_
->Send(kTestAppID1
,
1281 TestGCMServiceWrapper::WAIT
);
1283 EXPECT_EQ(out_message4
.id
, wrapper2_
->send_message_id());
1284 EXPECT_EQ(GCMClient::SUCCESS
, wrapper2_
->send_result());
1286 wrapper2_
->gcm_app_handler()->WaitForNotification();
1287 EXPECT_EQ(FakeGCMAppHandler::SEND_ERROR_EVENT
,
1288 wrapper2_
->gcm_app_handler()->received_event());
1289 EXPECT_EQ(kTestAppID1
, wrapper2_
->gcm_app_handler()->app_id());
1290 EXPECT_EQ(out_message4
.id
,
1291 wrapper2_
->gcm_app_handler()->send_error_details().message_id
);
1292 EXPECT_NE(GCMClient::SUCCESS
,
1293 wrapper2_
->gcm_app_handler()->send_error_details().result
);
1294 EXPECT_EQ(out_message4
.data
,
1295 wrapper2_
->gcm_app_handler()->send_error_details().additional_data
);
1297 // Sign in with a different account.
1298 wrapper_
->SignIn(kTestAccountID3
);
1300 // Signing out cleared all registrations, so we need to register again.
1301 wrapper_
->Register(kTestAppID1
,
1302 ToSenderList("sender1"),
1303 TestGCMServiceWrapper::WAIT
);
1305 // Incoming message will go through for the new signed-in account.
1306 GCMClient::IncomingMessage in_message5
;
1307 in_message5
.data
["in5"] = "in_data5";
1308 in_message5
.sender_id
= "sender1";
1309 wrapper_
->GetGCMClient()->ReceiveMessage(kTestAppID1
, in_message5
);
1311 wrapper_
->gcm_app_handler()->WaitForNotification();
1313 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT
,
1314 wrapper_
->gcm_app_handler()->received_event());
1315 EXPECT_EQ(in_message5
.data
, wrapper_
->gcm_app_handler()->message().data
);