OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 #include <algorithm> |
6 #include <map> | 6 #include <map> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 private: | 91 private: |
92 void PumpIOLoopCompleted() { | 92 void PumpIOLoopCompleted() { |
93 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 93 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
94 | 94 |
95 SignalCompleted(); | 95 SignalCompleted(); |
96 } | 96 } |
97 | 97 |
98 void OnIOLoopPump() { | 98 void OnIOLoopPump() { |
99 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 99 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
100 | 100 |
101 base::MessageLoop::current()->RunUntilIdle(); | 101 content::BrowserThread::PostTask( |
| 102 content::BrowserThread::IO, |
| 103 FROM_HERE, |
| 104 base::Bind(&Waiter::OnIOLoopPumpCompleted, base::Unretained(this))); |
| 105 } |
| 106 |
| 107 void OnIOLoopPumpCompleted() { |
| 108 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
102 | 109 |
103 content::BrowserThread::PostTask( | 110 content::BrowserThread::PostTask( |
104 content::BrowserThread::UI, | 111 content::BrowserThread::UI, |
105 FROM_HERE, | 112 FROM_HERE, |
106 base::Bind(&Waiter::PumpIOLoopCompleted, | 113 base::Bind(&Waiter::PumpIOLoopCompleted, |
107 base::Unretained(this))); | 114 base::Unretained(this))); |
108 } | 115 } |
109 | 116 |
110 scoped_ptr<base::RunLoop> run_loop_; | 117 scoped_ptr<base::RunLoop> run_loop_; |
111 }; | 118 }; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 Event received_event_; | 214 Event received_event_; |
208 std::string app_id_; | 215 std::string app_id_; |
209 GCMClient::IncomingMessage message_; | 216 GCMClient::IncomingMessage message_; |
210 std::string send_error_message_id_; | 217 std::string send_error_message_id_; |
211 GCMClient::Result send_error_result_; | 218 GCMClient::Result send_error_result_; |
212 }; | 219 }; |
213 | 220 |
214 class FakeGCMClientFactory : public GCMClientFactory { | 221 class FakeGCMClientFactory : public GCMClientFactory { |
215 public: | 222 public: |
216 FakeGCMClientFactory( | 223 FakeGCMClientFactory( |
217 GCMClientMock::Status gcm_client_initial_status, | 224 GCMClientMock::LoadingDelay gcm_client_loading_delay, |
218 GCMClientMock::ErrorSimulation gcm_client_error_simulation) | 225 GCMClientMock::ErrorSimulation gcm_client_error_simulation) |
219 : gcm_client_initial_status_(gcm_client_initial_status), | 226 : gcm_client_loading_delay_(gcm_client_loading_delay), |
220 gcm_client_error_simulation_(gcm_client_error_simulation), | 227 gcm_client_error_simulation_(gcm_client_error_simulation), |
221 gcm_client_(NULL) { | 228 gcm_client_(NULL) { |
222 } | 229 } |
223 | 230 |
224 virtual ~FakeGCMClientFactory() { | 231 virtual ~FakeGCMClientFactory() { |
225 } | 232 } |
226 | 233 |
227 virtual scoped_ptr<GCMClient> BuildInstance() OVERRIDE { | 234 virtual scoped_ptr<GCMClient> BuildInstance() OVERRIDE { |
228 gcm_client_ = new GCMClientMock(gcm_client_initial_status_, | 235 gcm_client_ = new GCMClientMock(gcm_client_loading_delay_, |
229 gcm_client_error_simulation_); | 236 gcm_client_error_simulation_); |
230 return scoped_ptr<GCMClient>(gcm_client_); | 237 return scoped_ptr<GCMClient>(gcm_client_); |
231 } | 238 } |
232 | 239 |
233 GCMClientMock* gcm_client() const { return gcm_client_; } | 240 GCMClientMock* gcm_client() const { return gcm_client_; } |
234 | 241 |
235 private: | 242 private: |
236 GCMClientMock::Status gcm_client_initial_status_; | 243 GCMClientMock::LoadingDelay gcm_client_loading_delay_; |
237 GCMClientMock::ErrorSimulation gcm_client_error_simulation_; | 244 GCMClientMock::ErrorSimulation gcm_client_error_simulation_; |
238 GCMClientMock* gcm_client_; | 245 GCMClientMock* gcm_client_; |
239 | 246 |
240 DISALLOW_COPY_AND_ASSIGN(FakeGCMClientFactory); | 247 DISALLOW_COPY_AND_ASSIGN(FakeGCMClientFactory); |
241 }; | 248 }; |
242 | 249 |
243 class GCMProfileServiceTestConsumer : public GCMProfileService::TestingDelegate{ | 250 class GCMProfileServiceTestConsumer : public GCMProfileService::TestingDelegate{ |
244 public: | 251 public: |
245 static BrowserContextKeyedService* BuildFakeSigninManager( | 252 static BrowserContextKeyedService* BuildFakeSigninManager( |
246 content::BrowserContext* context) { | 253 content::BrowserContext* context) { |
247 return new FakeSigninManager(static_cast<Profile*>(context)); | 254 return new FakeSigninManager(static_cast<Profile*>(context)); |
248 } | 255 } |
249 | 256 |
250 static BrowserContextKeyedService* BuildGCMProfileService( | 257 static BrowserContextKeyedService* BuildGCMProfileService( |
251 content::BrowserContext* context) { | 258 content::BrowserContext* context) { |
252 return new GCMProfileService(static_cast<Profile*>(context)); | 259 return new GCMProfileService(static_cast<Profile*>(context)); |
253 } | 260 } |
254 | 261 |
255 explicit GCMProfileServiceTestConsumer(Waiter* waiter) | 262 explicit GCMProfileServiceTestConsumer(Waiter* waiter) |
256 : waiter_(waiter), | 263 : waiter_(waiter), |
257 extension_service_(NULL), | 264 extension_service_(NULL), |
258 signin_manager_(NULL), | 265 signin_manager_(NULL), |
259 gcm_client_initial_status_(GCMClientMock::READY), | 266 gcm_client_loading_delay_(GCMClientMock::NO_DELAY_LOADING), |
260 gcm_client_error_simulation_(GCMClientMock::ALWAYS_SUCCEED), | 267 gcm_client_error_simulation_(GCMClientMock::ALWAYS_SUCCEED), |
261 registration_result_(GCMClient::SUCCESS), | 268 registration_result_(GCMClient::SUCCESS), |
262 has_persisted_registration_info_(false), | 269 has_persisted_registration_info_(false), |
263 send_result_(GCMClient::SUCCESS) { | 270 send_result_(GCMClient::SUCCESS) { |
264 // Create a new profile. | 271 // Create a new profile. |
265 profile_.reset(new TestingProfile); | 272 profile_.reset(new TestingProfile); |
266 | 273 |
267 // Use a fake version of SigninManager. | 274 // Use a fake version of SigninManager. |
268 signin_manager_ = static_cast<FakeSigninManager*>( | 275 signin_manager_ = static_cast<FakeSigninManager*>( |
269 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( | 276 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( |
270 profile(), &GCMProfileServiceTestConsumer::BuildFakeSigninManager)); | 277 profile(), &GCMProfileServiceTestConsumer::BuildFakeSigninManager)); |
271 | 278 |
272 // Create extension service in order to uninstall the extension. | 279 // Create extension service in order to uninstall the extension. |
273 extensions::TestExtensionSystem* extension_system( | 280 extensions::TestExtensionSystem* extension_system( |
274 static_cast<extensions::TestExtensionSystem*>( | 281 static_cast<extensions::TestExtensionSystem*>( |
275 extensions::ExtensionSystem::Get(profile()))); | 282 extensions::ExtensionSystem::Get(profile()))); |
276 extension_system->CreateExtensionService( | 283 extension_system->CreateExtensionService( |
277 CommandLine::ForCurrentProcess(), base::FilePath(), false); | 284 CommandLine::ForCurrentProcess(), base::FilePath(), false); |
278 extension_service_ = extension_system->Get(profile())->extension_service(); | 285 extension_service_ = extension_system->Get(profile())->extension_service(); |
279 | 286 |
280 // Enable GCM such that tests could be run on all channels. | 287 // Enable GCM such that tests could be run on all channels. |
281 GCMProfileService::enable_gcm_for_testing_ = true; | 288 profile()->GetPrefs()->SetBoolean(prefs::kGCMChannelEnabled, true); |
282 | 289 |
283 // Mock a GCMEventRouter. | 290 // Mock a GCMEventRouter. |
284 gcm_event_router_.reset(new FakeGCMEventRouter(waiter_)); | 291 gcm_event_router_.reset(new FakeGCMEventRouter(waiter_)); |
285 } | 292 } |
286 | 293 |
287 virtual ~GCMProfileServiceTestConsumer() { | 294 virtual ~GCMProfileServiceTestConsumer() { |
288 } | 295 } |
289 | 296 |
290 // Returns a barebones test extension. | 297 // Returns a barebones test extension. |
291 scoped_refptr<Extension> CreateExtension() { | 298 scoped_refptr<Extension> CreateExtension() { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 extension->id(), UnloadedExtensionInfo::REASON_TERMINATE); | 334 extension->id(), UnloadedExtensionInfo::REASON_TERMINATE); |
328 extension_service_->AddExtension(extension); | 335 extension_service_->AddExtension(extension); |
329 } | 336 } |
330 | 337 |
331 void CreateGCMProfileServiceInstance() { | 338 void CreateGCMProfileServiceInstance() { |
332 GCMProfileService* gcm_profile_service = static_cast<GCMProfileService*>( | 339 GCMProfileService* gcm_profile_service = static_cast<GCMProfileService*>( |
333 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 340 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
334 profile(), &GCMProfileServiceTestConsumer::BuildGCMProfileService)); | 341 profile(), &GCMProfileServiceTestConsumer::BuildGCMProfileService)); |
335 gcm_profile_service->set_testing_delegate(this); | 342 gcm_profile_service->set_testing_delegate(this); |
336 scoped_ptr<GCMClientFactory> gcm_client_factory( | 343 scoped_ptr<GCMClientFactory> gcm_client_factory( |
337 new FakeGCMClientFactory(gcm_client_initial_status_, | 344 new FakeGCMClientFactory(gcm_client_loading_delay_, |
338 gcm_client_error_simulation_)); | 345 gcm_client_error_simulation_)); |
339 gcm_profile_service->Initialize(gcm_client_factory.Pass()); | 346 gcm_profile_service->Initialize(gcm_client_factory.Pass()); |
340 waiter_->PumpIOLoop(); | 347 waiter_->PumpIOLoop(); |
341 } | 348 } |
342 | 349 |
343 void SignIn(const std::string& username) { | 350 void SignIn(const std::string& username) { |
344 signin_manager_->SignIn(username); | 351 signin_manager_->SignIn(username); |
345 waiter_->PumpIOLoop(); | 352 waiter_->PumpIOLoop(); |
346 waiter_->PumpUILoop(); | 353 waiter_->PumpUILoop(); |
347 } | 354 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 send_message_id_ = message_id; | 411 send_message_id_ = message_id; |
405 send_result_ = result; | 412 send_result_ = result; |
406 waiter_->SignalCompleted(); | 413 waiter_->SignalCompleted(); |
407 } | 414 } |
408 | 415 |
409 GCMProfileService* GetGCMProfileService() const { | 416 GCMProfileService* GetGCMProfileService() const { |
410 return GCMProfileServiceFactory::GetForProfile(profile()); | 417 return GCMProfileServiceFactory::GetForProfile(profile()); |
411 } | 418 } |
412 | 419 |
413 GCMClientMock* GetGCMClient() const { | 420 GCMClientMock* GetGCMClient() const { |
414 return static_cast<FakeGCMClientFactory*>( | 421 return static_cast<GCMClientMock*>( |
415 GetGCMProfileService()->gcm_client_factory_.get())->gcm_client(); | 422 GetGCMProfileService()->GetGCMClientForTesting()); |
416 } | 423 } |
417 | 424 |
418 const std::string& GetUsername() const { | 425 const std::string& GetUsername() const { |
419 return GetGCMProfileService()->username_; | 426 return GetGCMProfileService()->username_; |
420 } | 427 } |
421 | 428 |
422 bool IsGCMClientReady() const { | 429 bool IsGCMClientReady() const { |
423 return GetGCMProfileService()->gcm_client_ready_; | 430 return GetGCMProfileService()->gcm_client_ready_; |
424 } | 431 } |
425 | 432 |
426 bool ExistsCachedRegistrationInfo() const { | 433 bool ExistsCachedRegistrationInfo() const { |
427 return !GetGCMProfileService()->registration_info_map_.empty(); | 434 return !GetGCMProfileService()->registration_info_map_.empty(); |
428 } | 435 } |
429 | 436 |
430 Profile* profile() const { return profile_.get(); } | 437 Profile* profile() const { return profile_.get(); } |
431 FakeGCMEventRouter* gcm_event_router() const { | 438 FakeGCMEventRouter* gcm_event_router() const { |
432 return gcm_event_router_.get(); | 439 return gcm_event_router_.get(); |
433 } | 440 } |
434 | 441 |
435 void set_gcm_client_initial_status(GCMClientMock::Status status) { | 442 void set_gcm_client_loading_delay(GCMClientMock::LoadingDelay delay) { |
436 gcm_client_initial_status_ = status; | 443 gcm_client_loading_delay_ = delay; |
437 } | 444 } |
438 void set_gcm_client_error_simulation(GCMClientMock::ErrorSimulation error) { | 445 void set_gcm_client_error_simulation(GCMClientMock::ErrorSimulation error) { |
439 gcm_client_error_simulation_ = error; | 446 gcm_client_error_simulation_ = error; |
440 } | 447 } |
441 | 448 |
442 const std::string& registration_id() const { return registration_id_; } | 449 const std::string& registration_id() const { return registration_id_; } |
443 GCMClient::Result registration_result() const { return registration_result_; } | 450 GCMClient::Result registration_result() const { return registration_result_; } |
444 const std::string& send_message_id() const { return send_message_id_; } | 451 const std::string& send_message_id() const { return send_message_id_; } |
445 GCMClient::Result send_result() const { return send_result_; } | 452 GCMClient::Result send_result() const { return send_result_; } |
446 | 453 |
(...skipping 11 matching lines...) Expand all Loading... |
458 virtual GCMEventRouter* GetEventRouter() const OVERRIDE { | 465 virtual GCMEventRouter* GetEventRouter() const OVERRIDE { |
459 return gcm_event_router_.get(); | 466 return gcm_event_router_.get(); |
460 } | 467 } |
461 | 468 |
462 Waiter* waiter_; // Not owned. | 469 Waiter* waiter_; // Not owned. |
463 scoped_ptr<TestingProfile> profile_; | 470 scoped_ptr<TestingProfile> profile_; |
464 ExtensionService* extension_service_; // Not owned. | 471 ExtensionService* extension_service_; // Not owned. |
465 FakeSigninManager* signin_manager_; // Not owned. | 472 FakeSigninManager* signin_manager_; // Not owned. |
466 scoped_ptr<FakeGCMEventRouter> gcm_event_router_; | 473 scoped_ptr<FakeGCMEventRouter> gcm_event_router_; |
467 | 474 |
468 GCMClientMock::Status gcm_client_initial_status_; | 475 GCMClientMock::LoadingDelay gcm_client_loading_delay_; |
469 GCMClientMock::ErrorSimulation gcm_client_error_simulation_; | 476 GCMClientMock::ErrorSimulation gcm_client_error_simulation_; |
470 | 477 |
471 std::string registration_id_; | 478 std::string registration_id_; |
472 GCMClient::Result registration_result_; | 479 GCMClient::Result registration_result_; |
473 bool has_persisted_registration_info_; | 480 bool has_persisted_registration_info_; |
474 | 481 |
475 std::string send_message_id_; | 482 std::string send_message_id_; |
476 GCMClient::Result send_result_; | 483 GCMClient::Result send_result_; |
477 | 484 |
478 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTestConsumer); | 485 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTestConsumer); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
577 | 584 |
578 TEST_F(GCMProfileServiceTest, CreateGCMProfileServiceAfterProfileSignIn) { | 585 TEST_F(GCMProfileServiceTest, CreateGCMProfileServiceAfterProfileSignIn) { |
579 // Sign in to a profile. This will not initiate the check-in. | 586 // Sign in to a profile. This will not initiate the check-in. |
580 consumer()->SignIn(kTestingUsername); | 587 consumer()->SignIn(kTestingUsername); |
581 | 588 |
582 // Create GCMProfileService after sign-in. | 589 // Create GCMProfileService after sign-in. |
583 consumer()->CreateGCMProfileServiceInstance(); | 590 consumer()->CreateGCMProfileServiceInstance(); |
584 EXPECT_FALSE(consumer()->GetUsername().empty()); | 591 EXPECT_FALSE(consumer()->GetUsername().empty()); |
585 } | 592 } |
586 | 593 |
587 TEST_F(GCMProfileServiceTest, RegsiterWhenNotSignedIn) { | 594 TEST_F(GCMProfileServiceTest, SignInAndSignOutUnderPositiveChannelSignal) { |
| 595 // Positive channel signal is provided in SetUp. |
| 596 consumer()->CreateGCMProfileServiceInstance(); |
| 597 consumer()->SignIn(kTestingUsername); |
| 598 |
| 599 // GCMClient should be loaded. |
| 600 EXPECT_TRUE(consumer()->IsGCMClientReady()); |
| 601 EXPECT_EQ(GCMClientMock::LOADED, consumer()->GetGCMClient()->status()); |
| 602 |
| 603 consumer()->SignOut(); |
| 604 |
| 605 // GCMClient should be checked out. |
| 606 EXPECT_FALSE(consumer()->IsGCMClientReady()); |
| 607 EXPECT_EQ(GCMClientMock::CHECKED_OUT, consumer()->GetGCMClient()->status()); |
| 608 } |
| 609 |
| 610 TEST_F(GCMProfileServiceTest, SignInAndSignOutUnderNegativeChannelSignal) { |
| 611 // Negative channel signal will prevent GCMClient from checking in when the |
| 612 // profile is signed in. |
| 613 profile()->GetPrefs()->SetBoolean(prefs::kGCMChannelEnabled, false); |
| 614 |
| 615 consumer()->CreateGCMProfileServiceInstance(); |
| 616 consumer()->SignIn(kTestingUsername); |
| 617 |
| 618 // GCMClient should not be loaded. |
| 619 EXPECT_FALSE(consumer()->IsGCMClientReady()); |
| 620 EXPECT_EQ(GCMClientMock::UNINITIALIZED, consumer()->GetGCMClient()->status()); |
| 621 |
| 622 consumer()->SignOut(); |
| 623 |
| 624 // Check-out should still be performed. |
| 625 EXPECT_FALSE(consumer()->IsGCMClientReady()); |
| 626 EXPECT_EQ(GCMClientMock::CHECKED_OUT, consumer()->GetGCMClient()->status()); |
| 627 } |
| 628 |
| 629 TEST_F(GCMProfileServiceTest, SignOutAndThenSignIn) { |
| 630 // Positive channel signal is provided in SetUp. |
| 631 consumer()->CreateGCMProfileServiceInstance(); |
| 632 consumer()->SignIn(kTestingUsername); |
| 633 |
| 634 // GCMClient should be loaded. |
| 635 EXPECT_TRUE(consumer()->IsGCMClientReady()); |
| 636 EXPECT_EQ(GCMClientMock::LOADED, consumer()->GetGCMClient()->status()); |
| 637 |
| 638 consumer()->SignOut(); |
| 639 |
| 640 // GCMClient should be checked out. |
| 641 EXPECT_FALSE(consumer()->IsGCMClientReady()); |
| 642 EXPECT_EQ(GCMClientMock::CHECKED_OUT, consumer()->GetGCMClient()->status()); |
| 643 |
| 644 // Sign-in with a different username. |
| 645 consumer()->SignIn(kTestingUsername2); |
| 646 |
| 647 // GCMClient should be loaded again. |
| 648 EXPECT_TRUE(consumer()->IsGCMClientReady()); |
| 649 EXPECT_EQ(GCMClientMock::LOADED, consumer()->GetGCMClient()->status()); |
| 650 } |
| 651 |
| 652 TEST_F(GCMProfileServiceTest, RegisterWhenNotSignedIn) { |
| 653 consumer()->CreateGCMProfileServiceInstance(); |
| 654 |
588 std::vector<std::string> sender_ids; | 655 std::vector<std::string> sender_ids; |
589 sender_ids.push_back("sender1"); | 656 sender_ids.push_back("sender1"); |
590 consumer()->Register(kTestingAppId, sender_ids); | 657 consumer()->Register(kTestingAppId, sender_ids); |
591 | 658 |
592 EXPECT_TRUE(consumer()->registration_id().empty()); | 659 EXPECT_TRUE(consumer()->registration_id().empty()); |
593 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, consumer()->registration_result()); | 660 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, consumer()->registration_result()); |
594 } | 661 } |
595 | 662 |
| 663 TEST_F(GCMProfileServiceTest, RegisterUnderNeutralChannelSignal) { |
| 664 // Neutral channel signal will prevent GCMClient from checking in when the |
| 665 // profile is signed in. |
| 666 profile()->GetPrefs()->ClearPref(prefs::kGCMChannelEnabled); |
| 667 |
| 668 consumer()->CreateGCMProfileServiceInstance(); |
| 669 consumer()->SignIn(kTestingUsername); |
| 670 |
| 671 // GCMClient should not be checked in. |
| 672 EXPECT_FALSE(consumer()->IsGCMClientReady()); |
| 673 EXPECT_EQ(GCMClientMock::UNINITIALIZED, consumer()->GetGCMClient()->status()); |
| 674 |
| 675 // Invoking register will make GCMClient checked in. |
| 676 std::vector<std::string> sender_ids; |
| 677 sender_ids.push_back("sender1"); |
| 678 consumer()->Register(kTestingAppId, sender_ids); |
| 679 WaitUntilCompleted(); |
| 680 |
| 681 // GCMClient should be checked in. |
| 682 EXPECT_TRUE(consumer()->IsGCMClientReady()); |
| 683 EXPECT_EQ(GCMClientMock::LOADED, consumer()->GetGCMClient()->status()); |
| 684 |
| 685 // Registration should succeed. |
| 686 std::string expected_registration_id = |
| 687 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids); |
| 688 EXPECT_EQ(expected_registration_id, consumer()->registration_id()); |
| 689 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); |
| 690 } |
| 691 |
596 TEST_F(GCMProfileServiceTest, SendWhenNotSignedIn) { | 692 TEST_F(GCMProfileServiceTest, SendWhenNotSignedIn) { |
| 693 consumer()->CreateGCMProfileServiceInstance(); |
| 694 |
597 GCMClient::OutgoingMessage message; | 695 GCMClient::OutgoingMessage message; |
598 message.id = "1"; | 696 message.id = "1"; |
599 message.data["key1"] = "value1"; | 697 message.data["key1"] = "value1"; |
600 consumer()->Send(kTestingAppId, kUserId, message); | 698 consumer()->Send(kTestingAppId, kUserId, message); |
601 | 699 |
602 EXPECT_TRUE(consumer()->send_message_id().empty()); | 700 EXPECT_TRUE(consumer()->send_message_id().empty()); |
603 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, consumer()->send_result()); | 701 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, consumer()->send_result()); |
604 } | 702 } |
605 | 703 |
| 704 TEST_F(GCMProfileServiceTest, SendUnderNeutralChannelSignal) { |
| 705 // Neutral channel signal will prevent GCMClient from checking in when the |
| 706 // profile is signed in. |
| 707 profile()->GetPrefs()->ClearPref(prefs::kGCMChannelEnabled); |
| 708 |
| 709 consumer()->CreateGCMProfileServiceInstance(); |
| 710 consumer()->SignIn(kTestingUsername); |
| 711 |
| 712 // GCMClient should not be checked in. |
| 713 EXPECT_FALSE(consumer()->IsGCMClientReady()); |
| 714 EXPECT_EQ(GCMClientMock::UNINITIALIZED, consumer()->GetGCMClient()->status()); |
| 715 |
| 716 // Invoking send will make GCMClient checked in. |
| 717 GCMClient::OutgoingMessage message; |
| 718 message.id = "1"; |
| 719 message.data["key1"] = "value1"; |
| 720 consumer()->Send(kTestingAppId, kUserId, message); |
| 721 WaitUntilCompleted(); |
| 722 |
| 723 // GCMClient should be checked in. |
| 724 EXPECT_TRUE(consumer()->IsGCMClientReady()); |
| 725 EXPECT_EQ(GCMClientMock::LOADED, consumer()->GetGCMClient()->status()); |
| 726 |
| 727 // Sending should succeed. |
| 728 EXPECT_EQ(consumer()->send_message_id(), message.id); |
| 729 EXPECT_EQ(GCMClient::SUCCESS, consumer()->send_result()); |
| 730 } |
| 731 |
606 // Tests single-profile. | 732 // Tests single-profile. |
607 class GCMProfileServiceSingleProfileTest : public GCMProfileServiceTest { | 733 class GCMProfileServiceSingleProfileTest : public GCMProfileServiceTest { |
608 public: | 734 public: |
609 GCMProfileServiceSingleProfileTest() { | 735 GCMProfileServiceSingleProfileTest() { |
610 } | 736 } |
611 | 737 |
612 virtual ~GCMProfileServiceSingleProfileTest() { | 738 virtual ~GCMProfileServiceSingleProfileTest() { |
613 } | 739 } |
614 | 740 |
615 virtual void SetUp() OVERRIDE { | 741 virtual void SetUp() OVERRIDE { |
616 GCMProfileServiceTest::SetUp(); | 742 GCMProfileServiceTest::SetUp(); |
617 | 743 |
618 consumer()->CreateGCMProfileServiceInstance(); | 744 consumer()->CreateGCMProfileServiceInstance(); |
619 consumer()->SignIn(kTestingUsername); | 745 consumer()->SignIn(kTestingUsername); |
620 } | 746 } |
621 }; | 747 }; |
622 | 748 |
623 TEST_F(GCMProfileServiceSingleProfileTest, SignOut) { | |
624 EXPECT_TRUE(consumer()->IsGCMClientReady()); | |
625 | |
626 // This will trigger check-out. | |
627 consumer()->SignOut(); | |
628 | |
629 EXPECT_FALSE(consumer()->IsGCMClientReady()); | |
630 } | |
631 | |
632 TEST_F(GCMProfileServiceSingleProfileTest, SignOutAndThenSignIn) { | |
633 EXPECT_TRUE(consumer()->IsGCMClientReady()); | |
634 | |
635 // This will trigger check-out. | |
636 consumer()->SignOut(); | |
637 | |
638 EXPECT_FALSE(consumer()->IsGCMClientReady()); | |
639 | |
640 // Sign-in with a different username. | |
641 consumer()->SignIn(kTestingUsername2); | |
642 | |
643 EXPECT_TRUE(consumer()->IsGCMClientReady()); | |
644 } | |
645 | |
646 TEST_F(GCMProfileServiceSingleProfileTest, Register) { | 749 TEST_F(GCMProfileServiceSingleProfileTest, Register) { |
647 std::vector<std::string> sender_ids; | 750 std::vector<std::string> sender_ids; |
648 sender_ids.push_back("sender1"); | 751 sender_ids.push_back("sender1"); |
649 consumer()->Register(kTestingAppId, sender_ids); | 752 consumer()->Register(kTestingAppId, sender_ids); |
650 std::string expected_registration_id = | 753 std::string expected_registration_id = |
651 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids); | 754 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids); |
652 | 755 |
653 WaitUntilCompleted(); | 756 WaitUntilCompleted(); |
654 EXPECT_EQ(expected_registration_id, consumer()->registration_id()); | 757 EXPECT_EQ(expected_registration_id, consumer()->registration_id()); |
655 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); | 758 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
759 consumer()->set_gcm_client_error_simulation(GCMClientMock::FORCE_ERROR); | 862 consumer()->set_gcm_client_error_simulation(GCMClientMock::FORCE_ERROR); |
760 | 863 |
761 // Simulate start-up by recreating GCMProfileService. | 864 // Simulate start-up by recreating GCMProfileService. |
762 consumer()->CreateGCMProfileServiceInstance(); | 865 consumer()->CreateGCMProfileServiceInstance(); |
763 | 866 |
764 // Simulate start-up by reloading extension. | 867 // Simulate start-up by reloading extension. |
765 consumer()->ReloadExtension(extension); | 868 consumer()->ReloadExtension(extension); |
766 | 869 |
767 // This should read the registration info from the extension's state store. | 870 // This should read the registration info from the extension's state store. |
768 consumer()->Register(extension->id(), sender_ids); | 871 consumer()->Register(extension->id(), sender_ids); |
| 872 PumpIOLoop(); |
769 PumpUILoop(); | 873 PumpUILoop(); |
770 EXPECT_EQ(old_registration_id, consumer()->registration_id()); | 874 EXPECT_EQ(old_registration_id, consumer()->registration_id()); |
771 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); | 875 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); |
772 } | 876 } |
773 | 877 |
774 TEST_F(GCMProfileServiceSingleProfileTest, | 878 TEST_F(GCMProfileServiceSingleProfileTest, |
775 GCMClientReadyAfterReadingRegistration) { | 879 GCMClientReadyAfterReadingRegistration) { |
776 scoped_refptr<Extension> extension(consumer()->CreateExtension()); | 880 scoped_refptr<Extension> extension(consumer()->CreateExtension()); |
777 | 881 |
778 std::vector<std::string> sender_ids; | 882 std::vector<std::string> sender_ids; |
779 sender_ids.push_back("sender1"); | 883 sender_ids.push_back("sender1"); |
780 consumer()->Register(extension->id(), sender_ids); | 884 consumer()->Register(extension->id(), sender_ids); |
781 | 885 |
782 WaitUntilCompleted(); | 886 WaitUntilCompleted(); |
783 EXPECT_FALSE(consumer()->registration_id().empty()); | 887 EXPECT_FALSE(consumer()->registration_id().empty()); |
784 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); | 888 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); |
785 std::string old_registration_id = consumer()->registration_id(); | 889 std::string old_registration_id = consumer()->registration_id(); |
786 | 890 |
787 // Clears the results that would be set by the Register callback in | 891 // Clears the results that would be set by the Register callback in |
788 // preparation to call register 2nd time. | 892 // preparation to call register 2nd time. |
789 consumer()->clear_registration_result(); | 893 consumer()->clear_registration_result(); |
790 | 894 |
791 // Needs to create a GCMClient instance that is not ready initiallly. | |
792 consumer()->set_gcm_client_initial_status(GCMClientMock::NOT_READY); | |
793 | |
794 // Simulate start-up by recreating GCMProfileService. | 895 // Simulate start-up by recreating GCMProfileService. |
| 896 consumer()->set_gcm_client_loading_delay(GCMClientMock::DELAY_LOADING); |
795 consumer()->CreateGCMProfileServiceInstance(); | 897 consumer()->CreateGCMProfileServiceInstance(); |
796 | 898 |
797 // Simulate start-up by reloading extension. | 899 // Simulate start-up by reloading extension. |
798 consumer()->ReloadExtension(extension); | 900 consumer()->ReloadExtension(extension); |
799 | 901 |
800 // Read the registration info from the extension's state store. | 902 // Read the registration info from the extension's state store. |
801 // This would hold up because GCMClient is in loading state. | 903 // This would hold up because GCMClient is in loading state. |
802 consumer()->Register(extension->id(), sender_ids); | 904 consumer()->Register(extension->id(), sender_ids); |
803 base::RunLoop().RunUntilIdle(); | 905 base::RunLoop().RunUntilIdle(); |
804 EXPECT_TRUE(consumer()->registration_id().empty()); | 906 EXPECT_TRUE(consumer()->registration_id().empty()); |
805 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, consumer()->registration_result()); | 907 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, consumer()->registration_result()); |
806 | 908 |
807 // Register operation will be invoked after GCMClient becomes ready. | 909 // Register operation will be invoked after GCMClient becomes ready. |
808 consumer()->GetGCMClient()->SetReady(); | 910 consumer()->GetGCMClient()->PerformDelayedLoading(); |
809 WaitUntilCompleted(); | 911 WaitUntilCompleted(); |
810 EXPECT_EQ(old_registration_id, consumer()->registration_id()); | 912 EXPECT_EQ(old_registration_id, consumer()->registration_id()); |
811 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); | 913 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); |
812 } | 914 } |
813 | 915 |
814 TEST_F(GCMProfileServiceSingleProfileTest, | 916 TEST_F(GCMProfileServiceSingleProfileTest, |
815 PersistedRegistrationInfoRemoveAfterSignOut) { | 917 PersistedRegistrationInfoRemoveAfterSignOut) { |
816 std::vector<std::string> sender_ids; | 918 std::vector<std::string> sender_ids; |
817 sender_ids.push_back("sender1"); | 919 sender_ids.push_back("sender1"); |
818 consumer()->Register(kTestingAppId, sender_ids); | 920 consumer()->Register(kTestingAppId, sender_ids); |
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1262 consumer()->gcm_event_router()->clear_results(); | 1364 consumer()->gcm_event_router()->clear_results(); |
1263 WaitUntilCompleted(); | 1365 WaitUntilCompleted(); |
1264 | 1366 |
1265 EXPECT_EQ(FakeGCMEventRouter::MESSAGE_EVENT, | 1367 EXPECT_EQ(FakeGCMEventRouter::MESSAGE_EVENT, |
1266 consumer()->gcm_event_router()->received_event()); | 1368 consumer()->gcm_event_router()->received_event()); |
1267 EXPECT_TRUE( | 1369 EXPECT_TRUE( |
1268 in_message5.data == consumer()->gcm_event_router()->message().data); | 1370 in_message5.data == consumer()->gcm_event_router()->message().data); |
1269 } | 1371 } |
1270 | 1372 |
1271 } // namespace gcm | 1373 } // namespace gcm |
OLD | NEW |