| 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 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 Event received_event_; | 207 Event received_event_; |
| 208 std::string app_id_; | 208 std::string app_id_; |
| 209 GCMClient::IncomingMessage message_; | 209 GCMClient::IncomingMessage message_; |
| 210 std::string send_error_message_id_; | 210 std::string send_error_message_id_; |
| 211 GCMClient::Result send_error_result_; | 211 GCMClient::Result send_error_result_; |
| 212 }; | 212 }; |
| 213 | 213 |
| 214 class FakeGCMClientFactory : public GCMClientFactory { | 214 class FakeGCMClientFactory : public GCMClientFactory { |
| 215 public: | 215 public: |
| 216 FakeGCMClientFactory( | 216 FakeGCMClientFactory( |
| 217 GCMClientMock::Status gcm_client_initial_status, | 217 GCMClientMock::CheckinDelay gcm_client_checkin_delay, |
| 218 GCMClientMock::ErrorSimulation gcm_client_error_simulation) | 218 GCMClientMock::ErrorSimulation gcm_client_error_simulation) |
| 219 : gcm_client_initial_status_(gcm_client_initial_status), | 219 : gcm_client_checkin_delay_(gcm_client_checkin_delay), |
| 220 gcm_client_error_simulation_(gcm_client_error_simulation), | 220 gcm_client_error_simulation_(gcm_client_error_simulation), |
| 221 gcm_client_(NULL) { | 221 gcm_client_(NULL) { |
| 222 } | 222 } |
| 223 | 223 |
| 224 virtual ~FakeGCMClientFactory() { | 224 virtual ~FakeGCMClientFactory() { |
| 225 } | 225 } |
| 226 | 226 |
| 227 virtual scoped_ptr<GCMClient> BuildInstance() OVERRIDE { | 227 virtual scoped_ptr<GCMClient> BuildInstance() OVERRIDE { |
| 228 gcm_client_ = new GCMClientMock(gcm_client_initial_status_, | 228 gcm_client_ = new GCMClientMock(gcm_client_checkin_delay_, |
| 229 gcm_client_error_simulation_); | 229 gcm_client_error_simulation_); |
| 230 return scoped_ptr<GCMClient>(gcm_client_); | 230 return scoped_ptr<GCMClient>(gcm_client_); |
| 231 } | 231 } |
| 232 | 232 |
| 233 GCMClientMock* gcm_client() const { return gcm_client_; } | 233 GCMClientMock* gcm_client() const { return gcm_client_; } |
| 234 | 234 |
| 235 private: | 235 private: |
| 236 GCMClientMock::Status gcm_client_initial_status_; | 236 GCMClientMock::CheckinDelay gcm_client_checkin_delay_; |
| 237 GCMClientMock::ErrorSimulation gcm_client_error_simulation_; | 237 GCMClientMock::ErrorSimulation gcm_client_error_simulation_; |
| 238 GCMClientMock* gcm_client_; | 238 GCMClientMock* gcm_client_; |
| 239 | 239 |
| 240 DISALLOW_COPY_AND_ASSIGN(FakeGCMClientFactory); | 240 DISALLOW_COPY_AND_ASSIGN(FakeGCMClientFactory); |
| 241 }; | 241 }; |
| 242 | 242 |
| 243 class GCMProfileServiceTestConsumer : public GCMProfileService::TestingDelegate{ | 243 class GCMProfileServiceTestConsumer : public GCMProfileService::TestingDelegate{ |
| 244 public: | 244 public: |
| 245 static BrowserContextKeyedService* BuildFakeSigninManager( | 245 static BrowserContextKeyedService* BuildFakeSigninManager( |
| 246 content::BrowserContext* context) { | 246 content::BrowserContext* context) { |
| 247 return new FakeSigninManager(static_cast<Profile*>(context)); | 247 return new FakeSigninManager(static_cast<Profile*>(context)); |
| 248 } | 248 } |
| 249 | 249 |
| 250 static BrowserContextKeyedService* BuildGCMProfileService( | 250 static BrowserContextKeyedService* BuildGCMProfileService( |
| 251 content::BrowserContext* context) { | 251 content::BrowserContext* context) { |
| 252 return new GCMProfileService(static_cast<Profile*>(context)); | 252 return new GCMProfileService(static_cast<Profile*>(context)); |
| 253 } | 253 } |
| 254 | 254 |
| 255 explicit GCMProfileServiceTestConsumer(Waiter* waiter) | 255 explicit GCMProfileServiceTestConsumer(Waiter* waiter) |
| 256 : waiter_(waiter), | 256 : waiter_(waiter), |
| 257 extension_service_(NULL), | 257 extension_service_(NULL), |
| 258 signin_manager_(NULL), | 258 signin_manager_(NULL), |
| 259 gcm_client_initial_status_(GCMClientMock::READY), | 259 gcm_client_checkin_delay_(GCMClientMock::NO_CHECKIN_DELAY), |
| 260 gcm_client_error_simulation_(GCMClientMock::ALWAYS_SUCCEED), | 260 gcm_client_error_simulation_(GCMClientMock::ALWAYS_SUCCEED), |
| 261 registration_result_(GCMClient::SUCCESS), | 261 registration_result_(GCMClient::SUCCESS), |
| 262 has_persisted_registration_info_(false), | 262 has_persisted_registration_info_(false), |
| 263 send_result_(GCMClient::SUCCESS) { | 263 send_result_(GCMClient::SUCCESS) { |
| 264 // Create a new profile. | 264 // Create a new profile. |
| 265 profile_.reset(new TestingProfile); | 265 profile_.reset(new TestingProfile); |
| 266 | 266 |
| 267 // Use a fake version of SigninManager. | 267 // Use a fake version of SigninManager. |
| 268 signin_manager_ = static_cast<FakeSigninManager*>( | 268 signin_manager_ = static_cast<FakeSigninManager*>( |
| 269 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( | 269 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( |
| 270 profile(), &GCMProfileServiceTestConsumer::BuildFakeSigninManager)); | 270 profile(), &GCMProfileServiceTestConsumer::BuildFakeSigninManager)); |
| 271 | 271 |
| 272 // Create extension service in order to uninstall the extension. | 272 // Create extension service in order to uninstall the extension. |
| 273 extensions::TestExtensionSystem* extension_system( | 273 extensions::TestExtensionSystem* extension_system( |
| 274 static_cast<extensions::TestExtensionSystem*>( | 274 static_cast<extensions::TestExtensionSystem*>( |
| 275 extensions::ExtensionSystem::Get(profile()))); | 275 extensions::ExtensionSystem::Get(profile()))); |
| 276 extension_system->CreateExtensionService( | 276 extension_system->CreateExtensionService( |
| 277 CommandLine::ForCurrentProcess(), base::FilePath(), false); | 277 CommandLine::ForCurrentProcess(), base::FilePath(), false); |
| 278 extension_service_ = extension_system->Get(profile())->extension_service(); | 278 extension_service_ = extension_system->Get(profile())->extension_service(); |
| 279 | 279 |
| 280 // Enable GCM such that tests could be run on all channels. | 280 // Enable GCM such that tests could be run on all channels. |
| 281 GCMProfileService::enable_gcm_for_testing_ = true; | 281 GCMProfileService::enable_gcm_for_testing_ = true; |
| 282 profile()->GetPrefs()->SetBoolean(prefs::kGCMChannelEnabled, true); |
| 282 | 283 |
| 283 // Mock a GCMEventRouter. | 284 // Mock a GCMEventRouter. |
| 284 gcm_event_router_.reset(new FakeGCMEventRouter(waiter_)); | 285 gcm_event_router_.reset(new FakeGCMEventRouter(waiter_)); |
| 285 } | 286 } |
| 286 | 287 |
| 287 virtual ~GCMProfileServiceTestConsumer() { | 288 virtual ~GCMProfileServiceTestConsumer() { |
| 288 } | 289 } |
| 289 | 290 |
| 290 // Returns a barebones test extension. | 291 // Returns a barebones test extension. |
| 291 scoped_refptr<Extension> CreateExtension() { | 292 scoped_refptr<Extension> CreateExtension() { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 extension->id(), UnloadedExtensionInfo::REASON_TERMINATE); | 328 extension->id(), UnloadedExtensionInfo::REASON_TERMINATE); |
| 328 extension_service_->AddExtension(extension); | 329 extension_service_->AddExtension(extension); |
| 329 } | 330 } |
| 330 | 331 |
| 331 void CreateGCMProfileServiceInstance() { | 332 void CreateGCMProfileServiceInstance() { |
| 332 GCMProfileService* gcm_profile_service = static_cast<GCMProfileService*>( | 333 GCMProfileService* gcm_profile_service = static_cast<GCMProfileService*>( |
| 333 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 334 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 334 profile(), &GCMProfileServiceTestConsumer::BuildGCMProfileService)); | 335 profile(), &GCMProfileServiceTestConsumer::BuildGCMProfileService)); |
| 335 gcm_profile_service->set_testing_delegate(this); | 336 gcm_profile_service->set_testing_delegate(this); |
| 336 scoped_ptr<GCMClientFactory> gcm_client_factory( | 337 scoped_ptr<GCMClientFactory> gcm_client_factory( |
| 337 new FakeGCMClientFactory(gcm_client_initial_status_, | 338 new FakeGCMClientFactory(gcm_client_checkin_delay_, |
| 338 gcm_client_error_simulation_)); | 339 gcm_client_error_simulation_)); |
| 339 gcm_profile_service->Initialize(gcm_client_factory.Pass()); | 340 gcm_profile_service->Initialize(gcm_client_factory.Pass()); |
| 340 waiter_->PumpIOLoop(); | 341 waiter_->PumpIOLoop(); |
| 341 } | 342 } |
| 342 | 343 |
| 343 void SignIn(const std::string& username) { | 344 void SignIn(const std::string& username) { |
| 344 signin_manager_->SignIn(username); | 345 signin_manager_->SignIn(username); |
| 345 waiter_->PumpIOLoop(); | 346 waiter_->PumpIOLoop(); |
| 346 waiter_->PumpUILoop(); | 347 waiter_->PumpUILoop(); |
| 347 } | 348 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 send_message_id_ = message_id; | 405 send_message_id_ = message_id; |
| 405 send_result_ = result; | 406 send_result_ = result; |
| 406 waiter_->SignalCompleted(); | 407 waiter_->SignalCompleted(); |
| 407 } | 408 } |
| 408 | 409 |
| 409 GCMProfileService* GetGCMProfileService() const { | 410 GCMProfileService* GetGCMProfileService() const { |
| 410 return GCMProfileServiceFactory::GetForProfile(profile()); | 411 return GCMProfileServiceFactory::GetForProfile(profile()); |
| 411 } | 412 } |
| 412 | 413 |
| 413 GCMClientMock* GetGCMClient() const { | 414 GCMClientMock* GetGCMClient() const { |
| 414 return static_cast<FakeGCMClientFactory*>( | 415 return static_cast<GCMClientMock*>( |
| 415 GetGCMProfileService()->gcm_client_factory_.get())->gcm_client(); | 416 GetGCMProfileService()->GetGCMClientForTesting()); |
| 416 } | 417 } |
| 417 | 418 |
| 418 const std::string& GetUsername() const { | 419 const std::string& GetUsername() const { |
| 419 return GetGCMProfileService()->username_; | 420 return GetGCMProfileService()->username_; |
| 420 } | 421 } |
| 421 | 422 |
| 422 bool IsGCMClientReady() const { | 423 bool IsGCMClientReady() const { |
| 423 return GetGCMProfileService()->gcm_client_ready_; | 424 return GetGCMProfileService()->gcm_client_ready_; |
| 424 } | 425 } |
| 425 | 426 |
| 426 bool ExistsCachedRegistrationInfo() const { | 427 bool ExistsCachedRegistrationInfo() const { |
| 427 return !GetGCMProfileService()->registration_info_map_.empty(); | 428 return !GetGCMProfileService()->registration_info_map_.empty(); |
| 428 } | 429 } |
| 429 | 430 |
| 430 Profile* profile() const { return profile_.get(); } | 431 Profile* profile() const { return profile_.get(); } |
| 431 FakeGCMEventRouter* gcm_event_router() const { | 432 FakeGCMEventRouter* gcm_event_router() const { |
| 432 return gcm_event_router_.get(); | 433 return gcm_event_router_.get(); |
| 433 } | 434 } |
| 434 | 435 |
| 435 void set_gcm_client_initial_status(GCMClientMock::Status status) { | 436 void set_gcm_client_checkin_delay(GCMClientMock::CheckinDelay delay) { |
| 436 gcm_client_initial_status_ = status; | 437 gcm_client_checkin_delay_ = delay; |
| 437 } | 438 } |
| 438 void set_gcm_client_error_simulation(GCMClientMock::ErrorSimulation error) { | 439 void set_gcm_client_error_simulation(GCMClientMock::ErrorSimulation error) { |
| 439 gcm_client_error_simulation_ = error; | 440 gcm_client_error_simulation_ = error; |
| 440 } | 441 } |
| 441 | 442 |
| 442 const std::string& registration_id() const { return registration_id_; } | 443 const std::string& registration_id() const { return registration_id_; } |
| 443 GCMClient::Result registration_result() const { return registration_result_; } | 444 GCMClient::Result registration_result() const { return registration_result_; } |
| 444 const std::string& send_message_id() const { return send_message_id_; } | 445 const std::string& send_message_id() const { return send_message_id_; } |
| 445 GCMClient::Result send_result() const { return send_result_; } | 446 GCMClient::Result send_result() const { return send_result_; } |
| 446 | 447 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 458 virtual GCMEventRouter* GetEventRouter() const OVERRIDE { | 459 virtual GCMEventRouter* GetEventRouter() const OVERRIDE { |
| 459 return gcm_event_router_.get(); | 460 return gcm_event_router_.get(); |
| 460 } | 461 } |
| 461 | 462 |
| 462 Waiter* waiter_; // Not owned. | 463 Waiter* waiter_; // Not owned. |
| 463 scoped_ptr<TestingProfile> profile_; | 464 scoped_ptr<TestingProfile> profile_; |
| 464 ExtensionService* extension_service_; // Not owned. | 465 ExtensionService* extension_service_; // Not owned. |
| 465 FakeSigninManager* signin_manager_; // Not owned. | 466 FakeSigninManager* signin_manager_; // Not owned. |
| 466 scoped_ptr<FakeGCMEventRouter> gcm_event_router_; | 467 scoped_ptr<FakeGCMEventRouter> gcm_event_router_; |
| 467 | 468 |
| 468 GCMClientMock::Status gcm_client_initial_status_; | 469 GCMClientMock::CheckinDelay gcm_client_checkin_delay_; |
| 469 GCMClientMock::ErrorSimulation gcm_client_error_simulation_; | 470 GCMClientMock::ErrorSimulation gcm_client_error_simulation_; |
| 470 | 471 |
| 471 std::string registration_id_; | 472 std::string registration_id_; |
| 472 GCMClient::Result registration_result_; | 473 GCMClient::Result registration_result_; |
| 473 bool has_persisted_registration_info_; | 474 bool has_persisted_registration_info_; |
| 474 | 475 |
| 475 std::string send_message_id_; | 476 std::string send_message_id_; |
| 476 GCMClient::Result send_result_; | 477 GCMClient::Result send_result_; |
| 477 | 478 |
| 478 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTestConsumer); | 479 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTestConsumer); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 | 578 |
| 578 TEST_F(GCMProfileServiceTest, CreateGCMProfileServiceAfterProfileSignIn) { | 579 TEST_F(GCMProfileServiceTest, CreateGCMProfileServiceAfterProfileSignIn) { |
| 579 // Sign in to a profile. This will not initiate the check-in. | 580 // Sign in to a profile. This will not initiate the check-in. |
| 580 consumer()->SignIn(kTestingUsername); | 581 consumer()->SignIn(kTestingUsername); |
| 581 | 582 |
| 582 // Create GCMProfileService after sign-in. | 583 // Create GCMProfileService after sign-in. |
| 583 consumer()->CreateGCMProfileServiceInstance(); | 584 consumer()->CreateGCMProfileServiceInstance(); |
| 584 EXPECT_FALSE(consumer()->GetUsername().empty()); | 585 EXPECT_FALSE(consumer()->GetUsername().empty()); |
| 585 } | 586 } |
| 586 | 587 |
| 587 TEST_F(GCMProfileServiceTest, RegsiterWhenNotSignedIn) { | 588 TEST_F(GCMProfileServiceTest, SignInAndSignOutUnderPositiveChannelSignal) { |
| 589 // Positive channel signal is provided in SetUp. |
| 590 consumer()->CreateGCMProfileServiceInstance(); |
| 591 consumer()->SignIn(kTestingUsername); |
| 592 |
| 593 // GCMClient should be checked in. |
| 594 EXPECT_TRUE(consumer()->IsGCMClientReady()); |
| 595 EXPECT_EQ(GCMClientMock::CHECKED_IN, consumer()->GetGCMClient()->status()); |
| 596 |
| 597 consumer()->SignOut(); |
| 598 |
| 599 // GCMClient should be checked out. |
| 600 EXPECT_FALSE(consumer()->IsGCMClientReady()); |
| 601 EXPECT_EQ(GCMClientMock::CHECKED_OUT, consumer()->GetGCMClient()->status()); |
| 602 } |
| 603 |
| 604 TEST_F(GCMProfileServiceTest, SignInAndSignOutUnderNegativeChannelSignal) { |
| 605 // Negative channel signal will prevent GCMClient from checking in when the |
| 606 // profile is signed in. |
| 607 profile()->GetPrefs()->SetBoolean(prefs::kGCMChannelEnabled, false); |
| 608 |
| 609 consumer()->CreateGCMProfileServiceInstance(); |
| 610 consumer()->SignIn(kTestingUsername); |
| 611 |
| 612 // GCMClient should not be checked in. |
| 613 EXPECT_FALSE(consumer()->IsGCMClientReady()); |
| 614 EXPECT_EQ(GCMClientMock::UNINITIALIZED, consumer()->GetGCMClient()->status()); |
| 615 |
| 616 consumer()->SignOut(); |
| 617 |
| 618 // Check-out should still be performed. |
| 619 EXPECT_FALSE(consumer()->IsGCMClientReady()); |
| 620 EXPECT_EQ(GCMClientMock::CHECKED_OUT, consumer()->GetGCMClient()->status()); |
| 621 } |
| 622 |
| 623 TEST_F(GCMProfileServiceTest, SignOutAndThenSignIn) { |
| 624 // Positive channel signal is provided in SetUp. |
| 625 consumer()->CreateGCMProfileServiceInstance(); |
| 626 consumer()->SignIn(kTestingUsername); |
| 627 |
| 628 // GCMClient should not be checked in. |
| 629 EXPECT_TRUE(consumer()->IsGCMClientReady()); |
| 630 EXPECT_EQ(GCMClientMock::CHECKED_IN, consumer()->GetGCMClient()->status()); |
| 631 |
| 632 consumer()->SignOut(); |
| 633 |
| 634 // GCMClient should be checked out. |
| 635 EXPECT_FALSE(consumer()->IsGCMClientReady()); |
| 636 EXPECT_EQ(GCMClientMock::CHECKED_OUT, consumer()->GetGCMClient()->status()); |
| 637 |
| 638 // Sign-in with a different username. |
| 639 consumer()->SignIn(kTestingUsername2); |
| 640 |
| 641 // GCMClient should not be checked in again. |
| 642 EXPECT_TRUE(consumer()->IsGCMClientReady()); |
| 643 EXPECT_EQ(GCMClientMock::CHECKED_IN, consumer()->GetGCMClient()->status()); |
| 644 } |
| 645 |
| 646 TEST_F(GCMProfileServiceTest, RegisterWhenNotSignedIn) { |
| 647 consumer()->CreateGCMProfileServiceInstance(); |
| 648 |
| 588 std::vector<std::string> sender_ids; | 649 std::vector<std::string> sender_ids; |
| 589 sender_ids.push_back("sender1"); | 650 sender_ids.push_back("sender1"); |
| 590 consumer()->Register(kTestingAppId, sender_ids); | 651 consumer()->Register(kTestingAppId, sender_ids); |
| 591 | 652 |
| 592 EXPECT_TRUE(consumer()->registration_id().empty()); | 653 EXPECT_TRUE(consumer()->registration_id().empty()); |
| 593 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, consumer()->registration_result()); | 654 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, consumer()->registration_result()); |
| 594 } | 655 } |
| 595 | 656 |
| 657 TEST_F(GCMProfileServiceTest, RegisterUnderNegativeChannelSignal) { |
| 658 // Negative channel signal will prevent GCMClient from checking in when the |
| 659 // profile is signed in. |
| 660 profile()->GetPrefs()->SetBoolean(prefs::kGCMChannelEnabled, false); |
| 661 |
| 662 consumer()->CreateGCMProfileServiceInstance(); |
| 663 consumer()->SignIn(kTestingUsername); |
| 664 |
| 665 // GCMClient should not be checked in. |
| 666 EXPECT_FALSE(consumer()->IsGCMClientReady()); |
| 667 EXPECT_EQ(GCMClientMock::UNINITIALIZED, consumer()->GetGCMClient()->status()); |
| 668 |
| 669 // Invoking register will make GCMClient checked in. |
| 670 std::vector<std::string> sender_ids; |
| 671 sender_ids.push_back("sender1"); |
| 672 consumer()->Register(kTestingAppId, sender_ids); |
| 673 WaitUntilCompleted(); |
| 674 |
| 675 // GCMClient should be checked in. |
| 676 EXPECT_TRUE(consumer()->IsGCMClientReady()); |
| 677 EXPECT_EQ(GCMClientMock::CHECKED_IN, consumer()->GetGCMClient()->status()); |
| 678 |
| 679 // Registration should succeed. |
| 680 std::string expected_registration_id = |
| 681 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids); |
| 682 EXPECT_EQ(expected_registration_id, consumer()->registration_id()); |
| 683 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); |
| 684 } |
| 685 |
| 596 TEST_F(GCMProfileServiceTest, SendWhenNotSignedIn) { | 686 TEST_F(GCMProfileServiceTest, SendWhenNotSignedIn) { |
| 687 consumer()->CreateGCMProfileServiceInstance(); |
| 688 |
| 597 GCMClient::OutgoingMessage message; | 689 GCMClient::OutgoingMessage message; |
| 598 message.id = "1"; | 690 message.id = "1"; |
| 599 message.data["key1"] = "value1"; | 691 message.data["key1"] = "value1"; |
| 600 consumer()->Send(kTestingAppId, kUserId, message); | 692 consumer()->Send(kTestingAppId, kUserId, message); |
| 601 | 693 |
| 602 EXPECT_TRUE(consumer()->send_message_id().empty()); | 694 EXPECT_TRUE(consumer()->send_message_id().empty()); |
| 603 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, consumer()->send_result()); | 695 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, consumer()->send_result()); |
| 604 } | 696 } |
| 605 | 697 |
| 698 TEST_F(GCMProfileServiceTest, SendUnderNegativeChannelSignal) { |
| 699 // Negative channel signal will prevent GCMClient from checking in when the |
| 700 // profile is signed in. |
| 701 profile()->GetPrefs()->SetBoolean(prefs::kGCMChannelEnabled, false); |
| 702 |
| 703 consumer()->CreateGCMProfileServiceInstance(); |
| 704 consumer()->SignIn(kTestingUsername); |
| 705 |
| 706 // GCMClient should not be checked in. |
| 707 EXPECT_FALSE(consumer()->IsGCMClientReady()); |
| 708 EXPECT_EQ(GCMClientMock::UNINITIALIZED, consumer()->GetGCMClient()->status()); |
| 709 |
| 710 // Invoking send will make GCMClient checked in. |
| 711 GCMClient::OutgoingMessage message; |
| 712 message.id = "1"; |
| 713 message.data["key1"] = "value1"; |
| 714 consumer()->Send(kTestingAppId, kUserId, message); |
| 715 WaitUntilCompleted(); |
| 716 |
| 717 // GCMClient should be checked in. |
| 718 EXPECT_TRUE(consumer()->IsGCMClientReady()); |
| 719 EXPECT_EQ(GCMClientMock::CHECKED_IN, consumer()->GetGCMClient()->status()); |
| 720 |
| 721 // Sending should succeed. |
| 722 EXPECT_EQ(consumer()->send_message_id(), message.id); |
| 723 EXPECT_EQ(GCMClient::SUCCESS, consumer()->send_result()); |
| 724 } |
| 725 |
| 606 // Tests single-profile. | 726 // Tests single-profile. |
| 607 class GCMProfileServiceSingleProfileTest : public GCMProfileServiceTest { | 727 class GCMProfileServiceSingleProfileTest : public GCMProfileServiceTest { |
| 608 public: | 728 public: |
| 609 GCMProfileServiceSingleProfileTest() { | 729 GCMProfileServiceSingleProfileTest() { |
| 610 } | 730 } |
| 611 | 731 |
| 612 virtual ~GCMProfileServiceSingleProfileTest() { | 732 virtual ~GCMProfileServiceSingleProfileTest() { |
| 613 } | 733 } |
| 614 | 734 |
| 615 virtual void SetUp() OVERRIDE { | 735 virtual void SetUp() OVERRIDE { |
| 616 GCMProfileServiceTest::SetUp(); | 736 GCMProfileServiceTest::SetUp(); |
| 617 | 737 |
| 618 consumer()->CreateGCMProfileServiceInstance(); | 738 consumer()->CreateGCMProfileServiceInstance(); |
| 619 consumer()->SignIn(kTestingUsername); | 739 consumer()->SignIn(kTestingUsername); |
| 620 } | 740 } |
| 621 }; | 741 }; |
| 622 | 742 |
| 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) { | 743 TEST_F(GCMProfileServiceSingleProfileTest, Register) { |
| 647 std::vector<std::string> sender_ids; | 744 std::vector<std::string> sender_ids; |
| 648 sender_ids.push_back("sender1"); | 745 sender_ids.push_back("sender1"); |
| 649 consumer()->Register(kTestingAppId, sender_ids); | 746 consumer()->Register(kTestingAppId, sender_ids); |
| 650 std::string expected_registration_id = | 747 std::string expected_registration_id = |
| 651 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids); | 748 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids); |
| 652 | 749 |
| 653 WaitUntilCompleted(); | 750 WaitUntilCompleted(); |
| 654 EXPECT_EQ(expected_registration_id, consumer()->registration_id()); | 751 EXPECT_EQ(expected_registration_id, consumer()->registration_id()); |
| 655 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); | 752 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 781 | 878 |
| 782 WaitUntilCompleted(); | 879 WaitUntilCompleted(); |
| 783 EXPECT_FALSE(consumer()->registration_id().empty()); | 880 EXPECT_FALSE(consumer()->registration_id().empty()); |
| 784 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); | 881 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); |
| 785 std::string old_registration_id = consumer()->registration_id(); | 882 std::string old_registration_id = consumer()->registration_id(); |
| 786 | 883 |
| 787 // Clears the results that would be set by the Register callback in | 884 // Clears the results that would be set by the Register callback in |
| 788 // preparation to call register 2nd time. | 885 // preparation to call register 2nd time. |
| 789 consumer()->clear_registration_result(); | 886 consumer()->clear_registration_result(); |
| 790 | 887 |
| 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. | 888 // Simulate start-up by recreating GCMProfileService. |
| 889 consumer()->set_gcm_client_checkin_delay(GCMClientMock::CHECKIN_DELAY); |
| 795 consumer()->CreateGCMProfileServiceInstance(); | 890 consumer()->CreateGCMProfileServiceInstance(); |
| 796 | 891 |
| 797 // Simulate start-up by reloading extension. | 892 // Simulate start-up by reloading extension. |
| 798 consumer()->ReloadExtension(extension); | 893 consumer()->ReloadExtension(extension); |
| 799 | 894 |
| 800 // Read the registration info from the extension's state store. | 895 // Read the registration info from the extension's state store. |
| 801 // This would hold up because GCMClient is in loading state. | 896 // This would hold up because GCMClient is in loading state. |
| 802 consumer()->Register(extension->id(), sender_ids); | 897 consumer()->Register(extension->id(), sender_ids); |
| 803 base::RunLoop().RunUntilIdle(); | 898 base::RunLoop().RunUntilIdle(); |
| 804 EXPECT_TRUE(consumer()->registration_id().empty()); | 899 EXPECT_TRUE(consumer()->registration_id().empty()); |
| 805 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, consumer()->registration_result()); | 900 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, consumer()->registration_result()); |
| 806 | 901 |
| 807 // Register operation will be invoked after GCMClient becomes ready. | 902 // Register operation will be invoked after GCMClient becomes ready. |
| 808 consumer()->GetGCMClient()->SetReady(); | 903 consumer()->GetGCMClient()->PerformDelayedCheckIn(); |
| 809 WaitUntilCompleted(); | 904 WaitUntilCompleted(); |
| 810 EXPECT_EQ(old_registration_id, consumer()->registration_id()); | 905 EXPECT_EQ(old_registration_id, consumer()->registration_id()); |
| 811 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); | 906 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); |
| 812 } | 907 } |
| 813 | 908 |
| 814 TEST_F(GCMProfileServiceSingleProfileTest, | 909 TEST_F(GCMProfileServiceSingleProfileTest, |
| 815 PersistedRegistrationInfoRemoveAfterSignOut) { | 910 PersistedRegistrationInfoRemoveAfterSignOut) { |
| 816 std::vector<std::string> sender_ids; | 911 std::vector<std::string> sender_ids; |
| 817 sender_ids.push_back("sender1"); | 912 sender_ids.push_back("sender1"); |
| 818 consumer()->Register(kTestingAppId, sender_ids); | 913 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(); | 1357 consumer()->gcm_event_router()->clear_results(); |
| 1263 WaitUntilCompleted(); | 1358 WaitUntilCompleted(); |
| 1264 | 1359 |
| 1265 EXPECT_EQ(FakeGCMEventRouter::MESSAGE_EVENT, | 1360 EXPECT_EQ(FakeGCMEventRouter::MESSAGE_EVENT, |
| 1266 consumer()->gcm_event_router()->received_event()); | 1361 consumer()->gcm_event_router()->received_event()); |
| 1267 EXPECT_TRUE( | 1362 EXPECT_TRUE( |
| 1268 in_message5.data == consumer()->gcm_event_router()->message().data); | 1363 in_message5.data == consumer()->gcm_event_router()->message().data); |
| 1269 } | 1364 } |
| 1270 | 1365 |
| 1271 } // namespace gcm | 1366 } // namespace gcm |
| OLD | NEW |