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

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

Powered by Google App Engine
This is Rietveld 408576698