Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(521)

Side by Side Diff: chrome/browser/services/gcm/gcm_service_unittest.cc

Issue 225403021: Extract Profile-independent GCMService from GCMProfileService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Pass scoped_refptr as const reference. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/services/gcm/gcm_service.h"
6
7 #include "base/bind.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/gcm_app_handler.h"
14 #include "chrome/browser/services/gcm/gcm_client_factory.h"
15 #include "chrome/browser/services/gcm/gcm_client_mock.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "google_apis/gaia/oauth2_access_token_consumer.h"
19 #include "google_apis/gaia/oauth2_access_token_fetcher.h"
20 #include "google_apis/gaia/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"
24
25 namespace gcm {
26
27 namespace {
28
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";
36
37 void PumpUILoop() {
38 base::RunLoop().RunUntilIdle();
39 }
40
41 void PumpIOLoop() {
42 base::RunLoop run_loop;
43 content::BrowserThread::PostTaskAndReply(content::BrowserThread::IO,
44 FROM_HERE,
45 base::Bind(&base::DoNothing),
46 run_loop.QuitClosure());
47 run_loop.Run();
48 }
49
50 std::vector<std::string> ToSenderList(const std::string& sender_ids) {
51 std::vector<std::string> senders;
52 Tokenize(sender_ids, ",", &senders);
53 return senders;
54 }
55
56 class FakeOAuth2TokenService : public OAuth2TokenService {
57 public:
58 explicit FakeOAuth2TokenService(
59 const scoped_refptr<net::URLRequestContextGetter>& request_context);
60 virtual ~FakeOAuth2TokenService();
61
62 // OAuth2TokenService:
63 virtual bool RefreshTokenIsAvailable(
64 const std::string& account_id) const OVERRIDE;
65
66 protected:
67 virtual OAuth2AccessTokenFetcher* CreateAccessTokenFetcher(
68 const std::string& account_id,
69 net::URLRequestContextGetter* getter,
70 OAuth2AccessTokenConsumer* consumer) OVERRIDE;
71 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE;
72
73 private:
74 scoped_refptr<net::URLRequestContextGetter> request_context_;
75
76 DISALLOW_COPY_AND_ASSIGN(FakeOAuth2TokenService);
77 };
78
79 class FakeInvalidationAuthProvider
80 : public invalidation::InvalidationAuthProvider {
81 public:
82 explicit FakeInvalidationAuthProvider(
83 const scoped_refptr<net::URLRequestContextGetter>& request_context);
84 virtual ~FakeInvalidationAuthProvider();
85
86 // invalidation::InvalidationAuthProvider:
87 virtual OAuth2TokenService* GetTokenService() OVERRIDE;
88 virtual std::string GetUsername() OVERRIDE;
89 virtual std::string GetAccountId() OVERRIDE;
90 virtual bool ShowLoginUI() OVERRIDE;
91
92 void SignIn(const std::string& account_id);
93 void SignOut();
94
95 private:
96 std::string account_id_;
97 FakeOAuth2TokenService token_service_;
98
99 DISALLOW_COPY_AND_ASSIGN(FakeInvalidationAuthProvider);
100 };
101
102 class FakeGCMClientFactory : public GCMClientFactory {
103 public:
104 explicit FakeGCMClientFactory(
105 GCMClientMock::LoadingDelay gcm_client_loading_delay);
106 virtual ~FakeGCMClientFactory();
107
108 // GCMClientFactory:
109 virtual scoped_ptr<GCMClient> BuildInstance() OVERRIDE;
110
111 private:
112 GCMClientMock::LoadingDelay gcm_client_loading_delay_;
113
114 DISALLOW_COPY_AND_ASSIGN(FakeGCMClientFactory);
115 };
116
117 class FakeGCMAppHandler : public GCMAppHandler {
118 public:
119 enum Event {
120 NO_EVENT,
121 MESSAGE_EVENT,
122 MESSAGES_DELETED_EVENT,
123 SEND_ERROR_EVENT
124 };
125
126 FakeGCMAppHandler();
127 virtual ~FakeGCMAppHandler();
128
129 const Event& received_event() const { return received_event_; }
130 const std::string& app_id() const { return app_id_; }
131 const GCMClient::IncomingMessage& message() const { return message_; }
132 const GCMClient::SendErrorDetails& send_error_details() const {
133 return send_error_details_;
134 }
135
136 void WaitForNotification();
137
138 // GCMAppHandler:
139 virtual void ShutdownHandler() OVERRIDE;
140 virtual void OnMessage(const std::string& app_id,
141 const GCMClient::IncomingMessage& message) OVERRIDE;
142 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE;
143 virtual void OnSendError(
144 const std::string& app_id,
145 const GCMClient::SendErrorDetails& send_error_details) OVERRIDE;
146
147 private:
148 void ClearResults();
149
150 scoped_ptr<base::RunLoop> run_loop_;
151
152 Event received_event_;
153 std::string app_id_;
154 GCMClient::IncomingMessage message_;
155 GCMClient::SendErrorDetails send_error_details_;
156
157 DISALLOW_COPY_AND_ASSIGN(FakeGCMAppHandler);
158 };
159
160 class TestGCMService : public GCMService {
161 public:
162 TestGCMService(
163 bool is_always_enabled,
164 scoped_ptr<invalidation::InvalidationAuthProvider> auth_provider,
165 const scoped_refptr<net::URLRequestContextGetter>& request_context,
166 const base::FilePath& store_path);
167 virtual ~TestGCMService();
168
169 protected:
170 // GCMService:
171 virtual bool IsAlwaysEnabled() OVERRIDE;
172
173 private:
174 const bool is_always_enabled_;
175
176 DISALLOW_COPY_AND_ASSIGN(TestGCMService);
177 };
178
179 FakeOAuth2TokenService::FakeOAuth2TokenService(
180 const scoped_refptr<net::URLRequestContextGetter>& request_context)
181 : request_context_(request_context) {
182 }
183
184 FakeOAuth2TokenService::~FakeOAuth2TokenService() {
185 }
186
187 bool FakeOAuth2TokenService::RefreshTokenIsAvailable(
188 const std::string& account_id) const {
189 return false;
190 }
191
192 OAuth2AccessTokenFetcher* FakeOAuth2TokenService::CreateAccessTokenFetcher(
193 const std::string& account_id,
194 net::URLRequestContextGetter* getter,
195 OAuth2AccessTokenConsumer* consumer) {
196 return NULL;
197 }
198
199 net::URLRequestContextGetter* FakeOAuth2TokenService::GetRequestContext() {
200 return request_context_;
201 }
202
203 FakeInvalidationAuthProvider::FakeInvalidationAuthProvider(
204 const scoped_refptr<net::URLRequestContextGetter>& request_context)
205 : token_service_(request_context) {
206 }
207
208 FakeInvalidationAuthProvider::~FakeInvalidationAuthProvider() {
209 }
210
211 OAuth2TokenService* FakeInvalidationAuthProvider::GetTokenService() {
212 return &token_service_;
213 }
214
215 std::string FakeInvalidationAuthProvider::GetUsername() {
216 return account_id_;
217 }
218
219 std::string FakeInvalidationAuthProvider::GetAccountId() {
220 return account_id_;
221 }
222
223 bool FakeInvalidationAuthProvider::ShowLoginUI() {
224 return true;
225 }
226
227 void FakeInvalidationAuthProvider::SignIn(const std::string& account_id) {
228 account_id_ = account_id;
229 FireInvalidationAuthLogin();
230 }
231
232 void FakeInvalidationAuthProvider::SignOut() {
233 account_id_.clear();
234 FireInvalidationAuthLogout();
235 }
236
237 FakeGCMClientFactory::FakeGCMClientFactory(
238 GCMClientMock::LoadingDelay gcm_client_loading_delay)
239 : gcm_client_loading_delay_(gcm_client_loading_delay) {
240 }
241
242 FakeGCMClientFactory::~FakeGCMClientFactory() {
243 }
244
245 scoped_ptr<GCMClient> FakeGCMClientFactory::BuildInstance() {
246 return scoped_ptr<GCMClient>(new GCMClientMock(gcm_client_loading_delay_));
247 }
248
249 FakeGCMAppHandler::FakeGCMAppHandler() : received_event_(NO_EVENT) {
250 }
251
252 FakeGCMAppHandler::~FakeGCMAppHandler() {
253 }
254
255 void FakeGCMAppHandler::WaitForNotification() {
256 run_loop_.reset(new base::RunLoop);
257 run_loop_->Run();
258 run_loop_.reset();
259 }
260
261 void FakeGCMAppHandler::ShutdownHandler() {
262 }
263
264 void FakeGCMAppHandler::OnMessage(const std::string& app_id,
265 const GCMClient::IncomingMessage& message) {
266 ClearResults();
267 received_event_ = MESSAGE_EVENT;
268 app_id_ = app_id;
269 message_ = message;
270 if (run_loop_)
271 run_loop_->Quit();
272 }
273
274 void FakeGCMAppHandler::OnMessagesDeleted(const std::string& app_id) {
275 ClearResults();
276 received_event_ = MESSAGES_DELETED_EVENT;
277 app_id_ = app_id;
278 if (run_loop_)
279 run_loop_->Quit();
280 }
281
282 void FakeGCMAppHandler::OnSendError(
283 const std::string& app_id,
284 const GCMClient::SendErrorDetails& send_error_details) {
285 ClearResults();
286 received_event_ = SEND_ERROR_EVENT;
287 app_id_ = app_id;
288 send_error_details_ = send_error_details;
289 if (run_loop_)
290 run_loop_->Quit();
291 }
292
293 void FakeGCMAppHandler::ClearResults() {
294 received_event_ = NO_EVENT;
295 app_id_.clear();
296 message_ = GCMClient::IncomingMessage();
297 send_error_details_ = GCMClient::SendErrorDetails();
298 }
299
300 TestGCMService::TestGCMService(
301 bool is_always_enabled,
302 scoped_ptr<invalidation::InvalidationAuthProvider> auth_provider,
303 const scoped_refptr<net::URLRequestContextGetter>& request_context,
304 const base::FilePath& store_path)
305 : GCMService(auth_provider.Pass(), request_context, store_path),
306 is_always_enabled_(is_always_enabled) {
307 }
308
309 TestGCMService::~TestGCMService() {
310 }
311
312 bool TestGCMService::IsAlwaysEnabled() {
313 return is_always_enabled_;
314 }
315
316 } // namespace
317
318 class TestGCMServiceWrapper {
319 public:
320 explicit TestGCMServiceWrapper(
321 const scoped_refptr<net::URLRequestContextGetter>& request_context);
322 ~TestGCMServiceWrapper();
323
324 TestGCMService* service() { return service_.get(); }
325 FakeGCMAppHandler* gcm_app_handler() { return gcm_app_handler_.get(); }
326 const std::string& registration_id() const { return registration_id_; }
327 GCMClient::Result registration_result() const { return registration_result_; }
328 const std::string& send_message_id() const { return send_message_id_; }
329 GCMClient::Result send_result() const { return send_result_; }
330 GCMClient::Result unregistration_result() const {
331 return unregistration_result_;
332 }
333
334 void ClearRegistrationResult();
335 void ClearUnregistrationResult();
336
337 bool ServiceHasAppHandlers() const;
338 GCMClientMock* GetGCMClient();
339
340 void CreateService(bool service_is_always_enabled,
341 GCMClientMock::LoadingDelay gcm_client_loading_delay);
342
343 void SignIn(const std::string& account_id);
344 void SignOut();
345
346 void Register(const std::string& app_id,
347 const std::vector<std::string>& sender_ids,
348 bool wait);
349 void Send(const std::string& app_id,
350 const std::string& receiver_id,
351 const GCMClient::OutgoingMessage& message,
352 bool wait);
353 void Unregister(const std::string& app_id, bool wait);
354
355 private:
356 scoped_refptr<net::URLRequestContextGetter> request_context_;
357 base::ScopedTempDir temp_dir_;
358 scoped_ptr<FakeInvalidationAuthProvider> auth_provider_owner_;
359 FakeInvalidationAuthProvider* auth_provider_;
360 scoped_ptr<TestGCMService> service_;
361 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_;
362
363 std::string registration_id_;
364 GCMClient::Result registration_result_;
365 std::string send_message_id_;
366 GCMClient::Result send_result_;
367 GCMClient::Result unregistration_result_;
368
369 void RegisterCompleted(const base::Closure& callback,
370 const std::string& registration_id,
371 GCMClient::Result result);
372 void SendCompleted(const base::Closure& callback,
373 const std::string& message_id,
374 GCMClient::Result result);
375 void UnregisterCompleted(const base::Closure& callback,
376 GCMClient::Result result);
377
378 DISALLOW_COPY_AND_ASSIGN(TestGCMServiceWrapper);
379 };
380
381 TestGCMServiceWrapper::TestGCMServiceWrapper(
382 const scoped_refptr<net::URLRequestContextGetter>& request_context)
383 : request_context_(request_context),
384 auth_provider_(NULL),
385 registration_result_(GCMClient::UNKNOWN_ERROR),
386 send_result_(GCMClient::UNKNOWN_ERROR),
387 unregistration_result_(GCMClient::UNKNOWN_ERROR) {
388 auth_provider_owner_.reset(
389 new FakeInvalidationAuthProvider(request_context_));
390 auth_provider_ = auth_provider_owner_.get();
391 }
392
393 TestGCMServiceWrapper::~TestGCMServiceWrapper() {
394 if (!service_)
395 return;
396
397 service_->Shutdown();
398 service_.reset();
399 PumpIOLoop();
400 }
401
402 void TestGCMServiceWrapper::ClearRegistrationResult() {
403 registration_id_.clear();
404 registration_result_ = GCMClient::UNKNOWN_ERROR;
405 }
406
407 void TestGCMServiceWrapper::ClearUnregistrationResult() {
408 unregistration_result_ = GCMClient::UNKNOWN_ERROR;
409 }
410
411 bool TestGCMServiceWrapper::ServiceHasAppHandlers() const {
412 return !service_->app_handlers_.empty();
413 }
414
415 GCMClientMock* TestGCMServiceWrapper::GetGCMClient() {
416 return static_cast<GCMClientMock*>(service_->GetGCMClientForTesting());
417 }
418
419 void TestGCMServiceWrapper::CreateService(
420 bool service_is_always_enabled,
421 GCMClientMock::LoadingDelay gcm_client_loading_delay) {
422 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
423 service_.reset(new TestGCMService(
424 service_is_always_enabled,
425 auth_provider_owner_.PassAs<invalidation::InvalidationAuthProvider>(),
426 request_context_,
427 temp_dir_.path()));
428 service_->Initialize(scoped_ptr<GCMClientFactory>(
429 new FakeGCMClientFactory(gcm_client_loading_delay)));
430
431 gcm_app_handler_.reset(new FakeGCMAppHandler);
432 service_->AddAppHandler(kTestAppID1, gcm_app_handler_.get());
433 service_->AddAppHandler(kTestAppID2, gcm_app_handler_.get());
434 }
435
436 void TestGCMServiceWrapper::SignIn(const std::string& account_id) {
437 auth_provider_->SignIn(account_id);
438 PumpIOLoop();
439 PumpUILoop();
440 }
441
442 void TestGCMServiceWrapper::SignOut() {
443 auth_provider_->SignOut();
444 PumpIOLoop();
445 PumpUILoop();
446 }
447
448 void TestGCMServiceWrapper::Register(const std::string& app_id,
449 const std::vector<std::string>& sender_ids,
450 bool wait) {
451 base::RunLoop run_loop;
452 service_->Register(
453 app_id,
454 sender_ids,
455 base::Bind(&TestGCMServiceWrapper::RegisterCompleted,
456 base::Unretained(this),
457 wait ? run_loop.QuitClosure() : base::Bind(&base::DoNothing)));
458 if (wait)
459 run_loop.Run();
460 }
461
462 void TestGCMServiceWrapper::Send(const std::string& app_id,
463 const std::string& receiver_id,
464 const GCMClient::OutgoingMessage& message,
465 bool wait) {
466 base::RunLoop run_loop;
467 service_->Send(
468 app_id,
469 receiver_id,
470 message,
471 base::Bind(&TestGCMServiceWrapper::SendCompleted,
472 base::Unretained(this),
473 wait ? run_loop.QuitClosure() : base::Bind(&base::DoNothing)));
474 if (wait)
475 run_loop.Run();
476 }
477
478 void TestGCMServiceWrapper::Unregister(const std::string& app_id, bool wait) {
479 base::RunLoop run_loop;
480 service_->Unregister(
481 app_id,
482 base::Bind(&TestGCMServiceWrapper::UnregisterCompleted,
483 base::Unretained(this),
484 wait ? run_loop.QuitClosure() : base::Bind(&base::DoNothing)));
485 if (wait)
486 run_loop.Run();
487 }
488
489 void TestGCMServiceWrapper::RegisterCompleted(
490 const base::Closure& callback,
491 const std::string& registration_id,
492 GCMClient::Result result) {
493 registration_id_ = registration_id;
494 registration_result_ = result;
495 callback.Run();
496 }
497
498 void TestGCMServiceWrapper::SendCompleted(const base::Closure& callback,
499 const std::string& message_id,
500 GCMClient::Result result) {
501 send_message_id_ = message_id;
502 send_result_ = result;
503 callback.Run();
504 }
505
506 void TestGCMServiceWrapper::UnregisterCompleted(const base::Closure& callback,
507 GCMClient::Result result) {
508 unregistration_result_ = result;
509 callback.Run();
510 }
511
512 class GCMServiceTest : public testing::Test {
513 protected:
514 GCMServiceTest();
515 virtual ~GCMServiceTest();
516
517 // testing::Test:
518 void SetUp() OVERRIDE;
519 void TearDown() OVERRIDE;
520
521 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_;
522 scoped_refptr<net::URLRequestContextGetter> request_context_;
523 scoped_ptr<TestGCMServiceWrapper> wrapper_;
524
525 private:
526 DISALLOW_COPY_AND_ASSIGN(GCMServiceTest);
527 };
528
529 GCMServiceTest::GCMServiceTest() {
530 }
531
532 GCMServiceTest::~GCMServiceTest() {
533 }
534
535 void GCMServiceTest::SetUp() {
536 thread_bundle_.reset(new content::TestBrowserThreadBundle(
537 content::TestBrowserThreadBundle::REAL_IO_THREAD));
538 request_context_ = new net::TestURLRequestContextGetter(
539 content::BrowserThread::GetMessageLoopProxyForThread(
540 content::BrowserThread::IO));
541 wrapper_.reset(new TestGCMServiceWrapper(request_context_));
542 }
543
544 void GCMServiceTest::TearDown() {
545 wrapper_.reset();
546 }
547
548 TEST_F(GCMServiceTest, CreateGCMServiceBeforeSignIn) {
549 // Create CreateGMCService first.
550 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING);
551 EXPECT_FALSE(wrapper_->service()->IsStarted());
552
553 // Sign in. This will kick off the check-in.
554 wrapper_->SignIn(kTestAccountID1);
555 EXPECT_TRUE(wrapper_->service()->IsStarted());
556 }
557
558 TEST_F(GCMServiceTest, CreateGCMServiceAfterSignIn) {
559 // Sign in. This will not initiate the check-in.
560 wrapper_->SignIn(kTestAccountID1);
561
562 // Create GCMeService after sign-in.
563 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING);
564 EXPECT_TRUE(wrapper_->service()->IsStarted());
565 }
566
567 TEST_F(GCMServiceTest, Shutdown) {
568 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING);
569 EXPECT_TRUE(wrapper_->ServiceHasAppHandlers());
570
571 wrapper_->service()->Shutdown();
572 EXPECT_FALSE(wrapper_->ServiceHasAppHandlers());
573 }
574
575 TEST_F(GCMServiceTest, SignInAndSignOutUnderPositiveChannelSignal) {
576 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING);
577 wrapper_->SignIn(kTestAccountID1);
578
579 // GCMClient should be loaded.
580 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady());
581 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status());
582
583 wrapper_->SignOut();
584
585 // GCMClient should be checked out.
586 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady());
587 EXPECT_EQ(GCMClientMock::CHECKED_OUT, wrapper_->GetGCMClient()->status());
588 }
589
590 TEST_F(GCMServiceTest, SignInAndSignOutUnderNonPositiveChannelSignal) {
591 // Non-positive channel signal will prevent GCMClient from checking in during
592 // sign-in.
593 wrapper_->CreateService(false, GCMClientMock::NO_DELAY_LOADING);
594 wrapper_->SignIn(kTestAccountID1);
595
596 // GCMClient should not be loaded.
597 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady());
598 EXPECT_EQ(GCMClientMock::UNINITIALIZED, wrapper_->GetGCMClient()->status());
599
600 wrapper_->SignOut();
601
602 // Check-out should still be performed.
603 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady());
604 EXPECT_EQ(GCMClientMock::CHECKED_OUT, wrapper_->GetGCMClient()->status());
605 }
606
607 TEST_F(GCMServiceTest, SignOutAndThenSignIn) {
608 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING);
609 wrapper_->SignIn(kTestAccountID1);
610
611 // GCMClient should be loaded.
612 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady());
613 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status());
614
615 wrapper_->SignOut();
616
617 // GCMClient should be checked out.
618 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady());
619 EXPECT_EQ(GCMClientMock::CHECKED_OUT, wrapper_->GetGCMClient()->status());
620
621 // Sign-in with a different account.
622 wrapper_->SignIn(kTestAccountID2);
623
624 // GCMClient should be loaded again.
625 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady());
626 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status());
627 }
628
629 TEST_F(GCMServiceTest, StopAndRestartGCM) {
630 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING);
631 wrapper_->SignIn(kTestAccountID1);
632
633 // GCMClient should be loaded.
634 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady());
635 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status());
636
637 // Stops the GCM.
638 wrapper_->service()->Stop();
639 PumpIOLoop();
640 PumpUILoop();
641
642 // GCMClient should be stopped.
643 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady());
644 EXPECT_EQ(GCMClientMock::STOPPED, wrapper_->GetGCMClient()->status());
645
646 // Restarts the GCM.
647 wrapper_->service()->Start();
648 PumpIOLoop();
649 PumpUILoop();
650
651 // GCMClient should be loaded.
652 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady());
653 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status());
654
655 // Stops the GCM.
656 wrapper_->service()->Stop();
657 PumpIOLoop();
658 PumpUILoop();
659
660 // GCMClient should be stopped.
661 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady());
662 EXPECT_EQ(GCMClientMock::STOPPED, wrapper_->GetGCMClient()->status());
663
664 // Sign out.
665 wrapper_->SignOut();
666
667 // GCMClient should be checked out.
668 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady());
669 EXPECT_EQ(GCMClientMock::CHECKED_OUT, wrapper_->GetGCMClient()->status());
670 }
671
672 TEST_F(GCMServiceTest, RegisterWhenNotSignedIn) {
673 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING);
674
675 std::vector<std::string> sender_ids;
676 sender_ids.push_back("sender1");
677 wrapper_->Register(kTestAppID1, sender_ids, true);
678
679 EXPECT_TRUE(wrapper_->registration_id().empty());
680 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, wrapper_->registration_result());
681 }
682
683 TEST_F(GCMServiceTest, RegisterUnderNonPositiveChannelSignal) {
684 // Non-positive channel signal will prevent GCMClient from checking in during
685 // sign-in.
686 wrapper_->CreateService(false, GCMClientMock::NO_DELAY_LOADING);
687 wrapper_->SignIn(kTestAccountID1);
688
689 // GCMClient should not be checked in.
690 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady());
691 EXPECT_EQ(GCMClientMock::UNINITIALIZED, wrapper_->GetGCMClient()->status());
692
693 // Invoking register will make GCMClient checked in.
694 std::vector<std::string> sender_ids;
695 sender_ids.push_back("sender1");
696 wrapper_->Register(kTestAppID1, sender_ids, true);
697
698 // GCMClient should be checked in.
699 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady());
700 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status());
701
702 // Registration should succeed.
703 const std::string expected_registration_id =
704 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids);
705 EXPECT_EQ(expected_registration_id, wrapper_->registration_id());
706 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result());
707 }
708
709 TEST_F(GCMServiceTest, SendWhenNotSignedIn) {
710 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING);
711
712 GCMClient::OutgoingMessage message;
713 message.id = "1";
714 message.data["key1"] = "value1";
715 wrapper_->Send(kTestAppID1, kUserID1, message, true);
716
717 EXPECT_TRUE(wrapper_->send_message_id().empty());
718 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, wrapper_->send_result());
719 }
720
721 TEST_F(GCMServiceTest, SendUnderNonPositiveChannelSignal) {
722 // Non-positive channel signal will prevent GCMClient from checking in during
723 // sign-in.
724 wrapper_->CreateService(false, GCMClientMock::NO_DELAY_LOADING);
725 wrapper_->SignIn(kTestAccountID1);
726
727 // GCMClient should not be checked in.
728 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady());
729 EXPECT_EQ(GCMClientMock::UNINITIALIZED, wrapper_->GetGCMClient()->status());
730
731 // Invoking send will make GCMClient checked in.
732 GCMClient::OutgoingMessage message;
733 message.id = "1";
734 message.data["key1"] = "value1";
735 wrapper_->Send(kTestAppID1, kUserID1, message, true);
736
737 // GCMClient should be checked in.
738 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady());
739 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status());
740
741 // Sending should succeed.
742 EXPECT_EQ(message.id, wrapper_->send_message_id());
743 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->send_result());
744 }
745
746 // Tests a single instance of GCMService.
747 class GCMServiceSingleInstanceTest : public GCMServiceTest {
748 public:
749 GCMServiceSingleInstanceTest();
750 virtual ~GCMServiceSingleInstanceTest();
751
752 // GCMServiceTest:
753 virtual void SetUp() OVERRIDE;
754
755 private:
756 DISALLOW_COPY_AND_ASSIGN(GCMServiceSingleInstanceTest);
757 };
758
759 GCMServiceSingleInstanceTest::GCMServiceSingleInstanceTest() {
760 }
761
762 GCMServiceSingleInstanceTest::~GCMServiceSingleInstanceTest() {
763 }
764
765 void GCMServiceSingleInstanceTest::SetUp() {
766 GCMServiceTest::SetUp();
767
768 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING);
769 wrapper_->SignIn(kTestAccountID1);
770 }
771
772 TEST_F(GCMServiceSingleInstanceTest, Register) {
773 std::vector<std::string> sender_ids;
774 sender_ids.push_back("sender1");
775 wrapper_->Register(kTestAppID1, sender_ids, true);
776 const std::string expected_registration_id =
777 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids);
778
779 EXPECT_EQ(expected_registration_id, wrapper_->registration_id());
780 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result());
781 }
782
783 TEST_F(GCMServiceSingleInstanceTest, RegisterError) {
784 std::vector<std::string> sender_ids;
785 sender_ids.push_back("sender1@error");
786 wrapper_->Register(kTestAppID1, sender_ids, true);
787
788 EXPECT_TRUE(wrapper_->registration_id().empty());
789 EXPECT_NE(GCMClient::SUCCESS, wrapper_->registration_result());
790 }
791
792 TEST_F(GCMServiceSingleInstanceTest, RegisterAgainWithSameSenderIDs) {
793 std::vector<std::string> sender_ids;
794 sender_ids.push_back("sender1");
795 sender_ids.push_back("sender2");
796 wrapper_->Register(kTestAppID1, sender_ids, true);
797 const std::string expected_registration_id =
798 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids);
799
800 EXPECT_EQ(expected_registration_id, wrapper_->registration_id());
801 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result());
802
803 // Clears the results the would be set by the Register callback in preparation
804 // to call register 2nd time.
805 wrapper_->ClearRegistrationResult();
806
807 // Calling register 2nd time with the same set of sender IDs but different
808 // ordering will get back the same registration ID.
809 std::vector<std::string> another_sender_ids;
810 another_sender_ids.push_back("sender2");
811 another_sender_ids.push_back("sender1");
812 wrapper_->Register(kTestAppID1, another_sender_ids, true);
813
814 EXPECT_EQ(expected_registration_id, wrapper_->registration_id());
815 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result());
816 }
817
818 TEST_F(GCMServiceSingleInstanceTest, RegisterAgainWithDifferentSenderIDs) {
819 std::vector<std::string> sender_ids;
820 sender_ids.push_back("sender1");
821 wrapper_->Register(kTestAppID1, sender_ids, true);
822 const std::string expected_registration_id =
823 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids);
824
825 EXPECT_EQ(expected_registration_id, wrapper_->registration_id());
826 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result());
827
828 // Make sender IDs different.
829 sender_ids.push_back("sender2");
830 const std::string expected_registration_id_2 =
831 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids);
832
833 // Calling register 2nd time with the different sender IDs will get back a new
834 // registration ID.
835 wrapper_->Register(kTestAppID1, sender_ids, true);
836 EXPECT_EQ(expected_registration_id_2, wrapper_->registration_id());
837 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result());
838 }
839
840 TEST_F(GCMServiceSingleInstanceTest, GCMClientNotReadyBeforeRegistration) {
841 // Make GCMClient not ready initially.
842 wrapper_.reset(new TestGCMServiceWrapper(request_context_));
843 wrapper_->CreateService(true, GCMClientMock::DELAY_LOADING);
844 wrapper_->SignIn(kTestAccountID1);
845
846 // The registration is on hold until GCMClient is ready.
847 std::vector<std::string> sender_ids;
848 sender_ids.push_back("sender1");
849 wrapper_->Register(kTestAppID1, sender_ids, false);
850 PumpIOLoop();
851 PumpUILoop();
852 EXPECT_TRUE(wrapper_->registration_id().empty());
853 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, wrapper_->registration_result());
854
855 // Register operation will be invoked after GCMClient becomes ready.
856 wrapper_->GetGCMClient()->PerformDelayedLoading();
857 PumpIOLoop();
858 PumpIOLoop();
859 PumpIOLoop();
860 PumpUILoop();
861 EXPECT_FALSE(wrapper_->registration_id().empty());
862 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result());
863 }
864
865 TEST_F(GCMServiceSingleInstanceTest, RegisterAfterSignOut) {
866 // This will trigger check-out.
867 wrapper_->SignOut();
868
869 std::vector<std::string> sender_ids;
870 sender_ids.push_back("sender1");
871 wrapper_->Register(kTestAppID1, sender_ids, true);
872
873 EXPECT_TRUE(wrapper_->registration_id().empty());
874 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, wrapper_->registration_result());
875 }
876
877 TEST_F(GCMServiceSingleInstanceTest, UnregisterExplicitly) {
878 std::vector<std::string> sender_ids;
879 sender_ids.push_back("sender1");
880 wrapper_->Register(kTestAppID1, sender_ids, true);
881
882 EXPECT_FALSE(wrapper_->registration_id().empty());
883 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result());
884
885 wrapper_->Unregister(kTestAppID1, true);
886
887 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->unregistration_result());
888 }
889
890 TEST_F(GCMServiceSingleInstanceTest, UnregisterWhenAsyncOperationPending) {
891 std::vector<std::string> sender_ids;
892 sender_ids.push_back("sender1");
893 // First start registration without waiting for it to complete.
894 wrapper_->Register(kTestAppID1, sender_ids, false);
895
896 // Test that unregistration fails with async operation pending when there is a
897 // registration already in progress.
898 wrapper_->Unregister(kTestAppID1, true);
899 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
900 wrapper_->unregistration_result());
901
902 // Complete the unregistration.
903 PumpIOLoop();
904 PumpUILoop();
905 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result());
906
907 // Start unregistration without waiting for it to complete. This time no async
908 // operation is pending.
909 wrapper_->Unregister(kTestAppID1, false);
910
911 // Test that unregistration fails with async operation pending when there is
912 // an unregistration already in progress.
913 wrapper_->Unregister(kTestAppID1, true);
914 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
915 wrapper_->unregistration_result());
916 wrapper_->ClearUnregistrationResult();
917
918 // Complete unregistration.
919 PumpIOLoop();
920 PumpUILoop();
921 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->unregistration_result());
922 }
923
924 TEST_F(GCMServiceSingleInstanceTest, RegisterWhenAsyncOperationPending) {
925 std::vector<std::string> sender_ids;
926 sender_ids.push_back("sender1");
927 // First start registration without waiting for it to complete.
928 wrapper_->Register(kTestAppID1, sender_ids, false);
929
930 // Test that registration fails with async operation pending when there is a
931 // registration already in progress.
932 wrapper_->Register(kTestAppID1, sender_ids, true);
933 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
934 wrapper_->registration_result());
935 wrapper_->ClearRegistrationResult();
936
937 // Complete the registration.
938 PumpIOLoop();
939 PumpUILoop();
940 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result());
941
942 // Start unregistration without waiting for it to complete. This time no async
943 // operation is pending.
944 wrapper_->Unregister(kTestAppID1, false);
945
946 // Test that registration fails with async operation pending when there is an
947 // unregistration already in progress.
948 wrapper_->Register(kTestAppID1, sender_ids, true);
949 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
950 wrapper_->registration_result());
951
952 // Complete the first unregistration expecting success.
953 PumpIOLoop();
954 PumpUILoop();
955 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->unregistration_result());
956
957 // Test that it is ok to register again after unregistration.
958 wrapper_->Register(kTestAppID1, sender_ids, true);
959 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result());
960 }
961
962 TEST_F(GCMServiceSingleInstanceTest, Send) {
963 GCMClient::OutgoingMessage message;
964 message.id = "1";
965 message.data["key1"] = "value1";
966 message.data["key2"] = "value2";
967 wrapper_->Send(kTestAppID1, kUserID1, message, true);
968
969 EXPECT_EQ(message.id, wrapper_->send_message_id());
970 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->send_result());
971 }
972
973 TEST_F(GCMServiceSingleInstanceTest, GCMClientNotReadyBeforeSending) {
974 // Make GCMClient not ready initially.
975 wrapper_.reset(new TestGCMServiceWrapper(request_context_));
976 wrapper_->CreateService(true, GCMClientMock::DELAY_LOADING);
977 wrapper_->SignIn(kTestAccountID1);
978
979 // The sending is on hold until GCMClient is ready.
980 GCMClient::OutgoingMessage message;
981 message.id = "1";
982 message.data["key1"] = "value1";
983 message.data["key2"] = "value2";
984 wrapper_->Send(kTestAppID1, kUserID1, message, false);
985 PumpIOLoop();
986 PumpUILoop();
987
988 EXPECT_TRUE(wrapper_->send_message_id().empty());
989 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, wrapper_->send_result());
990
991 // Send operation will be invoked after GCMClient becomes ready.
992 wrapper_->GetGCMClient()->PerformDelayedLoading();
993 PumpIOLoop();
994 PumpIOLoop();
995 PumpIOLoop();
996 PumpUILoop();
997 EXPECT_EQ(message.id, wrapper_->send_message_id());
998 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->send_result());
999 }
1000
1001 TEST_F(GCMServiceSingleInstanceTest, SendAfterSignOut) {
1002 // This will trigger check-out.
1003 wrapper_->SignOut();
1004
1005 GCMClient::OutgoingMessage message;
1006 message.id = "1";
1007 message.data["key1"] = "value1";
1008 message.data["key2"] = "value2";
1009 wrapper_->Send(kTestAppID1, kUserID1, message, true);
1010
1011 EXPECT_TRUE(wrapper_->send_message_id().empty());
1012 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, wrapper_->send_result());
1013 }
1014
1015 TEST_F(GCMServiceSingleInstanceTest, SendError) {
1016 GCMClient::OutgoingMessage message;
1017 // Embedding error in id will tell the mock to simulate the send error.
1018 message.id = "1@error";
1019 message.data["key1"] = "value1";
1020 message.data["key2"] = "value2";
1021 wrapper_->Send(kTestAppID1, kUserID1, message, true);
1022
1023 EXPECT_EQ(message.id, wrapper_->send_message_id());
1024 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->send_result());
1025
1026 // Wait for the send error.
1027 wrapper_->gcm_app_handler()->WaitForNotification();
1028 EXPECT_EQ(FakeGCMAppHandler::SEND_ERROR_EVENT,
1029 wrapper_->gcm_app_handler()->received_event());
1030 EXPECT_EQ(kTestAppID1, wrapper_->gcm_app_handler()->app_id());
1031 EXPECT_EQ(message.id,
1032 wrapper_->gcm_app_handler()->send_error_details().message_id);
1033 EXPECT_NE(GCMClient::SUCCESS,
1034 wrapper_->gcm_app_handler()->send_error_details().result);
1035 EXPECT_EQ(message.data,
1036 wrapper_->gcm_app_handler()->send_error_details().additional_data);
1037 }
1038
1039 TEST_F(GCMServiceSingleInstanceTest, MessageReceived) {
1040 wrapper_->Register(kTestAppID1, ToSenderList("sender"), true);
1041 GCMClient::IncomingMessage message;
1042 message.data["key1"] = "value1";
1043 message.data["key2"] = "value2";
1044 message.sender_id = "sender";
1045 wrapper_->GetGCMClient()->ReceiveMessage(kTestAppID1, message);
1046 wrapper_->gcm_app_handler()->WaitForNotification();
1047 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
1048 wrapper_->gcm_app_handler()->received_event());
1049 EXPECT_EQ(kTestAppID1, wrapper_->gcm_app_handler()->app_id());
1050 EXPECT_EQ(message.data, wrapper_->gcm_app_handler()->message().data);
1051 EXPECT_TRUE(wrapper_->gcm_app_handler()->message().collapse_key.empty());
1052 EXPECT_EQ(message.sender_id,
1053 wrapper_->gcm_app_handler()->message().sender_id);
1054 }
1055
1056 TEST_F(GCMServiceSingleInstanceTest, MessageWithCollapseKeyReceived) {
1057 wrapper_->Register(kTestAppID1, ToSenderList("sender"), true);
1058 GCMClient::IncomingMessage message;
1059 message.data["key1"] = "value1";
1060 message.collapse_key = "collapse_key_value";
1061 message.sender_id = "sender";
1062 wrapper_->GetGCMClient()->ReceiveMessage(kTestAppID1, message);
1063 wrapper_->gcm_app_handler()->WaitForNotification();
1064 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
1065 wrapper_->gcm_app_handler()->received_event());
1066 EXPECT_EQ(kTestAppID1, wrapper_->gcm_app_handler()->app_id());
1067 EXPECT_EQ(message.data, wrapper_->gcm_app_handler()->message().data);
1068 EXPECT_EQ(message.collapse_key,
1069 wrapper_->gcm_app_handler()->message().collapse_key);
1070 }
1071
1072 TEST_F(GCMServiceSingleInstanceTest, MessagesDeleted) {
1073 wrapper_->GetGCMClient()->DeleteMessages(kTestAppID1);
1074 wrapper_->gcm_app_handler()->WaitForNotification();
1075 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT,
1076 wrapper_->gcm_app_handler()->received_event());
1077 EXPECT_EQ(kTestAppID1, wrapper_->gcm_app_handler()->app_id());
1078 }
1079
1080 // Tests to make sure that concurrent GCMService instances work correctly
1081 // regardless how GCMClient is created.
1082 class GCMServiceMultipleInstanceTest : public GCMServiceTest {
1083 protected:
1084 GCMServiceMultipleInstanceTest();
1085 virtual ~GCMServiceMultipleInstanceTest();
1086
1087 // GCMServiceTest:
1088 virtual void SetUp() OVERRIDE;
1089 virtual void TearDown() OVERRIDE;
1090
1091 scoped_ptr<TestGCMServiceWrapper> wrapper_2_;
1092
1093 private:
1094 DISALLOW_COPY_AND_ASSIGN(GCMServiceMultipleInstanceTest);
1095 };
1096
1097 GCMServiceMultipleInstanceTest::GCMServiceMultipleInstanceTest() {
1098 }
1099
1100 GCMServiceMultipleInstanceTest::~GCMServiceMultipleInstanceTest() {
1101 }
1102
1103 void GCMServiceMultipleInstanceTest::SetUp() {
1104 GCMServiceTest::SetUp();
1105
1106 wrapper_2_.reset(new TestGCMServiceWrapper(request_context_));
1107
1108 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING);
1109 wrapper_2_->CreateService(true, GCMClientMock::NO_DELAY_LOADING);
1110
1111 // Initiate check-in for each instance.
1112 wrapper_->SignIn(kTestAccountID1);
1113 wrapper_2_->SignIn(kTestAccountID2);
1114 }
1115
1116 void GCMServiceMultipleInstanceTest::TearDown() {
1117 wrapper_2_.reset();
1118 }
1119
1120 TEST_F(GCMServiceMultipleInstanceTest, Register) {
1121 // Register an app.
1122 std::vector<std::string> sender_ids;
1123 sender_ids.push_back("sender1");
1124 wrapper_->Register(kTestAppID1, sender_ids, true);
1125
1126 // Register the same app in a different instance.
1127 std::vector<std::string> sender_ids2;
1128 sender_ids2.push_back("foo");
1129 sender_ids2.push_back("bar");
1130 wrapper_2_->Register(kTestAppID1, sender_ids2, true);
1131
1132 EXPECT_EQ(GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids),
1133 wrapper_->registration_id());
1134 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result());
1135
1136 EXPECT_EQ(GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids2),
1137 wrapper_2_->registration_id());
1138 EXPECT_EQ(GCMClient::SUCCESS, wrapper_2_->registration_result());
1139
1140 // Register a different app in a different instance.
1141 std::vector<std::string> sender_ids3;
1142 sender_ids3.push_back("sender1");
1143 sender_ids3.push_back("sender2");
1144 sender_ids3.push_back("sender3");
1145 wrapper_2_->Register(kTestAppID2, sender_ids3, true);
1146
1147 EXPECT_EQ(GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids3),
1148 wrapper_2_->registration_id());
1149 EXPECT_EQ(GCMClient::SUCCESS, wrapper_2_->registration_result());
1150 }
1151
1152 TEST_F(GCMServiceMultipleInstanceTest, Send) {
1153 // Send a message from one app in one instance.
1154 GCMClient::OutgoingMessage message;
1155 message.id = "1";
1156 message.data["key1"] = "value1";
1157 message.data["key2"] = "value2";
1158 wrapper_->Send(kTestAppID1, kUserID1, message, true);
1159
1160 // Send a message from same app in another instance.
1161 GCMClient::OutgoingMessage message2;
1162 message2.id = "2";
1163 message2.data["foo"] = "bar";
1164 wrapper_2_->Send(kTestAppID1, kUserID2, message2, true);
1165
1166 EXPECT_EQ(message.id, wrapper_->send_message_id());
1167 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->send_result());
1168
1169 EXPECT_EQ(message2.id, wrapper_2_->send_message_id());
1170 EXPECT_EQ(GCMClient::SUCCESS, wrapper_2_->send_result());
1171
1172 // Send another message from different app in another instance.
1173 GCMClient::OutgoingMessage message3;
1174 message3.id = "3";
1175 message3.data["hello"] = "world";
1176 wrapper_2_->Send(kTestAppID2, kUserID1, message3, true);
1177
1178 EXPECT_EQ(message3.id, wrapper_2_->send_message_id());
1179 EXPECT_EQ(GCMClient::SUCCESS, wrapper_2_->send_result());
1180 }
1181
1182 TEST_F(GCMServiceMultipleInstanceTest, MessageReceived) {
1183 wrapper_->Register(kTestAppID1, ToSenderList("sender"), true);
1184 wrapper_2_->Register(kTestAppID1, ToSenderList("sender"), true);
1185 wrapper_2_->Register(kTestAppID2, ToSenderList("sender2"), true);
1186
1187 // Trigger an incoming message for an app in one instance.
1188 GCMClient::IncomingMessage message;
1189 message.data["key1"] = "value1";
1190 message.data["key2"] = "value2";
1191 message.sender_id = "sender";
1192 wrapper_->GetGCMClient()->ReceiveMessage(kTestAppID1, message);
1193 wrapper_->gcm_app_handler()->WaitForNotification();
1194
1195 // Trigger an incoming message for the same app in another instance.
1196 GCMClient::IncomingMessage message2;
1197 message2.data["foo"] = "bar";
1198 message2.sender_id = "sender";
1199 wrapper_2_->GetGCMClient()->ReceiveMessage(kTestAppID1, message2);
1200 wrapper_2_->gcm_app_handler()->WaitForNotification();
1201
1202 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
1203 wrapper_->gcm_app_handler()->received_event());
1204 EXPECT_EQ(kTestAppID1, wrapper_->gcm_app_handler()->app_id());
1205 EXPECT_EQ(message.data, wrapper_->gcm_app_handler()->message().data);
1206 EXPECT_EQ("sender", wrapper_->gcm_app_handler()->message().sender_id);
1207
1208 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
1209 wrapper_2_->gcm_app_handler()->received_event());
1210 EXPECT_EQ(kTestAppID1, wrapper_2_->gcm_app_handler()->app_id());
1211 EXPECT_EQ(message2.data, wrapper_2_->gcm_app_handler()->message().data);
1212 EXPECT_EQ("sender", wrapper_2_->gcm_app_handler()->message().sender_id);
1213
1214 // Trigger another incoming message for a different app in another instance.
1215 GCMClient::IncomingMessage message3;
1216 message3.data["bar1"] = "foo1";
1217 message3.data["bar2"] = "foo2";
1218 message3.sender_id = "sender2";
1219 wrapper_2_->GetGCMClient()->ReceiveMessage(kTestAppID2, message3);
1220 wrapper_2_->gcm_app_handler()->WaitForNotification();
1221
1222 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
1223 wrapper_2_->gcm_app_handler()->received_event());
1224 EXPECT_EQ(kTestAppID2, wrapper_2_->gcm_app_handler()->app_id());
1225 EXPECT_EQ(message3.data, wrapper_2_->gcm_app_handler()->message().data);
1226 EXPECT_EQ("sender2", wrapper_2_->gcm_app_handler()->message().sender_id);
1227 }
1228
1229 // Test a set of GCM operations on multiple instances.
1230 // 1) Register 1 app in instance 1 and register 2 apps in instance 2;
1231 // 2) Send a message from instance 1;
1232 // 3) Receive a message to an app in instance 1 and receive a message for each
1233 // of the two apps in instance 2;
1234 // 4) Send a message for each of the two apps in instance 2;
1235 // 5) Sign out of instance 1.
1236 // 6) Register/send stops working for instance 1;
1237 // 7) The app in instance 2 can still receive these events;
1238 // 8) Sign into instance 1 with a different account.
1239 // 9) The message to the newly signed-in account will be routed.
1240 TEST_F(GCMServiceMultipleInstanceTest, Combined) {
1241 // Register an app.
1242 std::vector<std::string> sender_ids;
1243 sender_ids.push_back("sender1");
1244 wrapper_->Register(kTestAppID1, sender_ids, true);
1245
1246 // Register the same app in a different instance.
1247 std::vector<std::string> sender_ids2;
1248 sender_ids2.push_back("foo");
1249 sender_ids2.push_back("bar");
1250 wrapper_2_->Register(kTestAppID1, sender_ids2, true);
1251
1252 // Register a different app in a different instance.
1253 std::vector<std::string> sender_ids3;
1254 sender_ids3.push_back("sender1");
1255 sender_ids3.push_back("sender2");
1256 sender_ids3.push_back("sender3");
1257 wrapper_2_->Register(kTestAppID2, sender_ids3, true);
1258
1259 EXPECT_EQ(GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids),
1260 wrapper_->registration_id());
1261 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result());
1262
1263 EXPECT_EQ(GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids3),
1264 wrapper_2_->registration_id());
1265 EXPECT_EQ(GCMClient::SUCCESS, wrapper_2_->registration_result());
1266
1267 // Send a message from one instance.
1268 GCMClient::OutgoingMessage out_message;
1269 out_message.id = "1";
1270 out_message.data["out1"] = "out_data1";
1271 out_message.data["out1_2"] = "out_data1_2";
1272 wrapper_->Send(kTestAppID1, kUserID1, out_message, true);
1273
1274 EXPECT_EQ(out_message.id, wrapper_->send_message_id());
1275 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->send_result());
1276
1277 // Trigger an incoming message for an app in one instance.
1278 GCMClient::IncomingMessage in_message;
1279 in_message.data["in1"] = "in_data1";
1280 in_message.data["in1_2"] = "in_data1_2";
1281 in_message.sender_id = "sender1";
1282 wrapper_->GetGCMClient()->ReceiveMessage(kTestAppID1, in_message);
1283 wrapper_->gcm_app_handler()->WaitForNotification();
1284
1285 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
1286 wrapper_->gcm_app_handler()->received_event());
1287 EXPECT_EQ(kTestAppID1, wrapper_->gcm_app_handler()->app_id());
1288 EXPECT_EQ(in_message.data, wrapper_->gcm_app_handler()->message().data);
1289
1290 // Trigger 2 incoming messages, one for each app respectively, in another
1291 // instance.
1292 GCMClient::IncomingMessage in_message2;
1293 in_message2.data["in2"] = "in_data2";
1294 in_message2.sender_id = "sender3";
1295 wrapper_2_->GetGCMClient()->ReceiveMessage(kTestAppID2, in_message2);
1296
1297 GCMClient::IncomingMessage in_message3;
1298 in_message3.data["in3"] = "in_data3";
1299 in_message3.data["in3_2"] = "in_data3_2";
1300 in_message3.sender_id = "foo";
1301 wrapper_2_->GetGCMClient()->ReceiveMessage(kTestAppID1, in_message3);
1302
1303 wrapper_2_->gcm_app_handler()->WaitForNotification();
1304
1305 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
1306 wrapper_2_->gcm_app_handler()->received_event());
1307 EXPECT_EQ(kTestAppID2, wrapper_2_->gcm_app_handler()->app_id());
1308 EXPECT_EQ(in_message2.data, wrapper_2_->gcm_app_handler()->message().data);
1309
1310 wrapper_2_->gcm_app_handler()->WaitForNotification();
1311
1312 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
1313 wrapper_2_->gcm_app_handler()->received_event());
1314 EXPECT_EQ(kTestAppID1, wrapper_2_->gcm_app_handler()->app_id());
1315 EXPECT_EQ(in_message3.data, wrapper_2_->gcm_app_handler()->message().data);
1316
1317 // Send two messages, one for each app respectively, from another instance.
1318 GCMClient::OutgoingMessage out_message2;
1319 out_message2.id = "2";
1320 out_message2.data["out2"] = "out_data2";
1321 wrapper_2_->Send(kTestAppID1, kUserID2, out_message2, true);
1322
1323 GCMClient::OutgoingMessage out_message3;
1324 out_message3.id = "3";
1325 out_message3.data["out3"] = "out_data3";
1326 wrapper_2_->Send(kTestAppID2, kUserID2, out_message3, false);
1327
1328 EXPECT_EQ(out_message2.id, wrapper_2_->send_message_id());
1329 EXPECT_EQ(GCMClient::SUCCESS, wrapper_2_->send_result());
1330
1331 PumpIOLoop();
1332 PumpUILoop();
1333
1334 EXPECT_EQ(out_message3.id, wrapper_2_->send_message_id());
1335 EXPECT_EQ(GCMClient::SUCCESS, wrapper_2_->send_result());
1336
1337 // Sign out of one instance.
1338 wrapper_->SignOut();
1339
1340 // Register/send stops working for signed-out instance.
1341 wrapper_->Register(kTestAppID1, sender_ids, true);
1342 EXPECT_TRUE(wrapper_->registration_id().empty());
1343 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, wrapper_->registration_result());
1344
1345 wrapper_->Send(kTestAppID2, kUserID2, out_message3, true);
1346 EXPECT_TRUE(wrapper_->send_message_id().empty());
1347 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, wrapper_->send_result());
1348
1349 // Deleted messages event will go through for another signed-in instance.
1350 wrapper_2_->GetGCMClient()->DeleteMessages(kTestAppID2);
1351 wrapper_2_->gcm_app_handler()->WaitForNotification();
1352
1353 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT,
1354 wrapper_2_->gcm_app_handler()->received_event());
1355 EXPECT_EQ(kTestAppID2, wrapper_2_->gcm_app_handler()->app_id());
1356
1357 // Send error event will go through for another signed-in instance.
1358 GCMClient::OutgoingMessage out_message4;
1359 out_message4.id = "1@error";
1360 out_message4.data["out4"] = "out_data4";
1361 wrapper_2_->Send(kTestAppID1, kUserID1, out_message4, true);
1362
1363 EXPECT_EQ(out_message4.id, wrapper_2_->send_message_id());
1364 EXPECT_EQ(GCMClient::SUCCESS, wrapper_2_->send_result());
1365
1366 wrapper_2_->gcm_app_handler()->WaitForNotification();
1367 EXPECT_EQ(FakeGCMAppHandler::SEND_ERROR_EVENT,
1368 wrapper_2_->gcm_app_handler()->received_event());
1369 EXPECT_EQ(kTestAppID1, wrapper_2_->gcm_app_handler()->app_id());
1370 EXPECT_EQ(out_message4.id,
1371 wrapper_2_->gcm_app_handler()->send_error_details().message_id);
1372 EXPECT_NE(GCMClient::SUCCESS,
1373 wrapper_2_->gcm_app_handler()->send_error_details().result);
1374 EXPECT_EQ(
1375 out_message4.data,
1376 wrapper_2_->gcm_app_handler()->send_error_details().additional_data);
1377
1378 // Sign in with a different account.
1379 wrapper_->SignIn(kTestAccountID3);
1380
1381 // Signing out cleared all registrations, so we need to register again.
1382 wrapper_->Register(kTestAppID1, ToSenderList("sender1"), true);
1383
1384 // Incoming message will go through for the new signed-in account.
1385 GCMClient::IncomingMessage in_message5;
1386 in_message5.data["in5"] = "in_data5";
1387 in_message5.sender_id = "sender1";
1388 wrapper_->GetGCMClient()->ReceiveMessage(kTestAppID1, in_message5);
1389
1390 wrapper_->gcm_app_handler()->WaitForNotification();
1391
1392 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
1393 wrapper_->gcm_app_handler()->received_event());
1394 EXPECT_EQ(in_message5.data, wrapper_->gcm_app_handler()->message().data);
1395 }
1396
1397 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698