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

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

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

Powered by Google App Engine
This is Rietveld 408576698