Chromium Code Reviews| 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::LoadingDelay gcm_client_loading_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_loading_delay_(gcm_client_loading_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_loading_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::LoadingDelay gcm_client_loading_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_loading_delay_(GCMClientMock::NO_DELAY_LOADING), |
| 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 profile()->GetPrefs()->SetBoolean(prefs::kGCMChannelEnabled, true); |
| 282 | 282 |
| 283 // Mock a GCMEventRouter. | 283 // Mock a GCMEventRouter. |
| 284 gcm_event_router_.reset(new FakeGCMEventRouter(waiter_)); | 284 gcm_event_router_.reset(new FakeGCMEventRouter(waiter_)); |
| 285 } | 285 } |
| 286 | 286 |
| 287 virtual ~GCMProfileServiceTestConsumer() { | 287 virtual ~GCMProfileServiceTestConsumer() { |
| 288 } | 288 } |
| 289 | 289 |
| 290 // Returns a barebones test extension. | 290 // Returns a barebones test extension. |
| 291 scoped_refptr<Extension> CreateExtension() { | 291 scoped_refptr<Extension> CreateExtension() { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 327 extension->id(), UnloadedExtensionInfo::REASON_TERMINATE); | 327 extension->id(), UnloadedExtensionInfo::REASON_TERMINATE); |
| 328 extension_service_->AddExtension(extension); | 328 extension_service_->AddExtension(extension); |
| 329 } | 329 } |
| 330 | 330 |
| 331 void CreateGCMProfileServiceInstance() { | 331 void CreateGCMProfileServiceInstance() { |
| 332 GCMProfileService* gcm_profile_service = static_cast<GCMProfileService*>( | 332 GCMProfileService* gcm_profile_service = static_cast<GCMProfileService*>( |
| 333 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 333 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 334 profile(), &GCMProfileServiceTestConsumer::BuildGCMProfileService)); | 334 profile(), &GCMProfileServiceTestConsumer::BuildGCMProfileService)); |
| 335 gcm_profile_service->set_testing_delegate(this); | 335 gcm_profile_service->set_testing_delegate(this); |
| 336 scoped_ptr<GCMClientFactory> gcm_client_factory( | 336 scoped_ptr<GCMClientFactory> gcm_client_factory( |
| 337 new FakeGCMClientFactory(gcm_client_initial_status_, | 337 new FakeGCMClientFactory(gcm_client_loading_delay_, |
| 338 gcm_client_error_simulation_)); | 338 gcm_client_error_simulation_)); |
| 339 gcm_profile_service->Initialize(gcm_client_factory.Pass()); | 339 gcm_profile_service->Initialize(gcm_client_factory.Pass()); |
| 340 waiter_->PumpIOLoop(); | 340 waiter_->PumpIOLoop(); |
| 341 } | 341 } |
| 342 | 342 |
| 343 void SignIn(const std::string& username) { | 343 void SignIn(const std::string& username) { |
| 344 signin_manager_->SignIn(username); | 344 signin_manager_->SignIn(username); |
| 345 waiter_->PumpIOLoop(); | 345 waiter_->PumpIOLoop(); |
| 346 waiter_->PumpUILoop(); | 346 waiter_->PumpUILoop(); |
| 347 } | 347 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 404 send_message_id_ = message_id; | 404 send_message_id_ = message_id; |
| 405 send_result_ = result; | 405 send_result_ = result; |
| 406 waiter_->SignalCompleted(); | 406 waiter_->SignalCompleted(); |
| 407 } | 407 } |
| 408 | 408 |
| 409 GCMProfileService* GetGCMProfileService() const { | 409 GCMProfileService* GetGCMProfileService() const { |
| 410 return GCMProfileServiceFactory::GetForProfile(profile()); | 410 return GCMProfileServiceFactory::GetForProfile(profile()); |
| 411 } | 411 } |
| 412 | 412 |
| 413 GCMClientMock* GetGCMClient() const { | 413 GCMClientMock* GetGCMClient() const { |
| 414 return static_cast<FakeGCMClientFactory*>( | 414 return static_cast<GCMClientMock*>( |
| 415 GetGCMProfileService()->gcm_client_factory_.get())->gcm_client(); | 415 GetGCMProfileService()->GetGCMClientForTesting()); |
| 416 } | 416 } |
| 417 | 417 |
| 418 const std::string& GetUsername() const { | 418 const std::string& GetUsername() const { |
| 419 return GetGCMProfileService()->username_; | 419 return GetGCMProfileService()->username_; |
| 420 } | 420 } |
| 421 | 421 |
| 422 bool IsGCMClientReady() const { | 422 bool IsGCMClientReady() const { |
| 423 return GetGCMProfileService()->gcm_client_ready_; | 423 return GetGCMProfileService()->gcm_client_ready_; |
| 424 } | 424 } |
| 425 | 425 |
| 426 bool ExistsCachedRegistrationInfo() const { | 426 bool ExistsCachedRegistrationInfo() const { |
| 427 return !GetGCMProfileService()->registration_info_map_.empty(); | 427 return !GetGCMProfileService()->registration_info_map_.empty(); |
| 428 } | 428 } |
| 429 | 429 |
| 430 Profile* profile() const { return profile_.get(); } | 430 Profile* profile() const { return profile_.get(); } |
| 431 FakeGCMEventRouter* gcm_event_router() const { | 431 FakeGCMEventRouter* gcm_event_router() const { |
| 432 return gcm_event_router_.get(); | 432 return gcm_event_router_.get(); |
| 433 } | 433 } |
| 434 | 434 |
| 435 void set_gcm_client_initial_status(GCMClientMock::Status status) { | 435 void set_gcm_client_loading_delay(GCMClientMock::LoadingDelay delay) { |
| 436 gcm_client_initial_status_ = status; | 436 gcm_client_loading_delay_ = delay; |
| 437 } | 437 } |
| 438 void set_gcm_client_error_simulation(GCMClientMock::ErrorSimulation error) { | 438 void set_gcm_client_error_simulation(GCMClientMock::ErrorSimulation error) { |
| 439 gcm_client_error_simulation_ = error; | 439 gcm_client_error_simulation_ = error; |
| 440 } | 440 } |
| 441 | 441 |
| 442 const std::string& registration_id() const { return registration_id_; } | 442 const std::string& registration_id() const { return registration_id_; } |
| 443 GCMClient::Result registration_result() const { return registration_result_; } | 443 GCMClient::Result registration_result() const { return registration_result_; } |
| 444 const std::string& send_message_id() const { return send_message_id_; } | 444 const std::string& send_message_id() const { return send_message_id_; } |
| 445 GCMClient::Result send_result() const { return send_result_; } | 445 GCMClient::Result send_result() const { return send_result_; } |
| 446 | 446 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 458 virtual GCMEventRouter* GetEventRouter() const OVERRIDE { | 458 virtual GCMEventRouter* GetEventRouter() const OVERRIDE { |
| 459 return gcm_event_router_.get(); | 459 return gcm_event_router_.get(); |
| 460 } | 460 } |
| 461 | 461 |
| 462 Waiter* waiter_; // Not owned. | 462 Waiter* waiter_; // Not owned. |
| 463 scoped_ptr<TestingProfile> profile_; | 463 scoped_ptr<TestingProfile> profile_; |
| 464 ExtensionService* extension_service_; // Not owned. | 464 ExtensionService* extension_service_; // Not owned. |
| 465 FakeSigninManager* signin_manager_; // Not owned. | 465 FakeSigninManager* signin_manager_; // Not owned. |
| 466 scoped_ptr<FakeGCMEventRouter> gcm_event_router_; | 466 scoped_ptr<FakeGCMEventRouter> gcm_event_router_; |
| 467 | 467 |
| 468 GCMClientMock::Status gcm_client_initial_status_; | 468 GCMClientMock::LoadingDelay gcm_client_loading_delay_; |
| 469 GCMClientMock::ErrorSimulation gcm_client_error_simulation_; | 469 GCMClientMock::ErrorSimulation gcm_client_error_simulation_; |
| 470 | 470 |
| 471 std::string registration_id_; | 471 std::string registration_id_; |
| 472 GCMClient::Result registration_result_; | 472 GCMClient::Result registration_result_; |
| 473 bool has_persisted_registration_info_; | 473 bool has_persisted_registration_info_; |
| 474 | 474 |
| 475 std::string send_message_id_; | 475 std::string send_message_id_; |
| 476 GCMClient::Result send_result_; | 476 GCMClient::Result send_result_; |
| 477 | 477 |
| 478 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTestConsumer); | 478 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTestConsumer); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 577 | 577 |
| 578 TEST_F(GCMProfileServiceTest, CreateGCMProfileServiceAfterProfileSignIn) { | 578 TEST_F(GCMProfileServiceTest, CreateGCMProfileServiceAfterProfileSignIn) { |
| 579 // Sign in to a profile. This will not initiate the check-in. | 579 // Sign in to a profile. This will not initiate the check-in. |
| 580 consumer()->SignIn(kTestingUsername); | 580 consumer()->SignIn(kTestingUsername); |
| 581 | 581 |
| 582 // Create GCMProfileService after sign-in. | 582 // Create GCMProfileService after sign-in. |
| 583 consumer()->CreateGCMProfileServiceInstance(); | 583 consumer()->CreateGCMProfileServiceInstance(); |
| 584 EXPECT_FALSE(consumer()->GetUsername().empty()); | 584 EXPECT_FALSE(consumer()->GetUsername().empty()); |
| 585 } | 585 } |
| 586 | 586 |
| 587 TEST_F(GCMProfileServiceTest, RegsiterWhenNotSignedIn) { | 587 TEST_F(GCMProfileServiceTest, SignInAndSignOutUnderPositiveChannelSignal) { |
| 588 // Positive channel signal is provided in SetUp. | |
| 589 consumer()->CreateGCMProfileServiceInstance(); | |
| 590 consumer()->SignIn(kTestingUsername); | |
| 591 | |
| 592 // GCMClient should be loaded. | |
| 593 EXPECT_TRUE(consumer()->IsGCMClientReady()); | |
| 594 EXPECT_EQ(GCMClientMock::LOADED, consumer()->GetGCMClient()->status()); | |
| 595 | |
| 596 consumer()->SignOut(); | |
| 597 | |
| 598 // GCMClient should be checked out. | |
| 599 EXPECT_FALSE(consumer()->IsGCMClientReady()); | |
| 600 EXPECT_EQ(GCMClientMock::CHECKED_OUT, consumer()->GetGCMClient()->status()); | |
| 601 } | |
| 602 | |
| 603 TEST_F(GCMProfileServiceTest, SignInAndSignOutUnderNegativeChannelSignal) { | |
| 604 // Negative channel signal will prevent GCMClient from checking in when the | |
| 605 // profile is signed in. | |
| 606 profile()->GetPrefs()->SetBoolean(prefs::kGCMChannelEnabled, false); | |
| 607 | |
| 608 consumer()->CreateGCMProfileServiceInstance(); | |
| 609 consumer()->SignIn(kTestingUsername); | |
| 610 | |
| 611 // GCMClient should not be loaded. | |
| 612 EXPECT_FALSE(consumer()->IsGCMClientReady()); | |
| 613 EXPECT_EQ(GCMClientMock::UNINITIALIZED, consumer()->GetGCMClient()->status()); | |
| 614 | |
| 615 consumer()->SignOut(); | |
| 616 | |
| 617 // Check-out should still be performed. | |
| 618 EXPECT_FALSE(consumer()->IsGCMClientReady()); | |
| 619 EXPECT_EQ(GCMClientMock::CHECKED_OUT, consumer()->GetGCMClient()->status()); | |
| 620 } | |
| 621 | |
| 622 TEST_F(GCMProfileServiceTest, SignOutAndThenSignIn) { | |
| 623 // Positive channel signal is provided in SetUp. | |
| 624 consumer()->CreateGCMProfileServiceInstance(); | |
| 625 consumer()->SignIn(kTestingUsername); | |
| 626 | |
| 627 // GCMClient should not be loaded. | |
|
Nicolas Zea
2014/02/22 00:20:47
update comment
| |
| 628 EXPECT_TRUE(consumer()->IsGCMClientReady()); | |
| 629 EXPECT_EQ(GCMClientMock::LOADED, consumer()->GetGCMClient()->status()); | |
| 630 | |
| 631 consumer()->SignOut(); | |
| 632 | |
| 633 // GCMClient should be checked out. | |
| 634 EXPECT_FALSE(consumer()->IsGCMClientReady()); | |
| 635 EXPECT_EQ(GCMClientMock::CHECKED_OUT, consumer()->GetGCMClient()->status()); | |
| 636 | |
| 637 // Sign-in with a different username. | |
| 638 consumer()->SignIn(kTestingUsername2); | |
| 639 | |
| 640 // GCMClient should be loaded again. | |
| 641 EXPECT_TRUE(consumer()->IsGCMClientReady()); | |
| 642 EXPECT_EQ(GCMClientMock::LOADED, consumer()->GetGCMClient()->status()); | |
| 643 } | |
| 644 | |
| 645 TEST_F(GCMProfileServiceTest, RegisterWhenNotSignedIn) { | |
| 646 consumer()->CreateGCMProfileServiceInstance(); | |
| 647 | |
| 588 std::vector<std::string> sender_ids; | 648 std::vector<std::string> sender_ids; |
| 589 sender_ids.push_back("sender1"); | 649 sender_ids.push_back("sender1"); |
| 590 consumer()->Register(kTestingAppId, sender_ids); | 650 consumer()->Register(kTestingAppId, sender_ids); |
| 591 | 651 |
| 592 EXPECT_TRUE(consumer()->registration_id().empty()); | 652 EXPECT_TRUE(consumer()->registration_id().empty()); |
| 593 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, consumer()->registration_result()); | 653 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, consumer()->registration_result()); |
| 594 } | 654 } |
| 595 | 655 |
| 656 TEST_F(GCMProfileServiceTest, RegisterUnderNeutralChannelSignal) { | |
| 657 // Neutral channel signal will prevent GCMClient from checking in when the | |
| 658 // profile is signed in. | |
| 659 profile()->GetPrefs()->ClearPref(prefs::kGCMChannelEnabled); | |
| 660 | |
| 661 consumer()->CreateGCMProfileServiceInstance(); | |
| 662 consumer()->SignIn(kTestingUsername); | |
| 663 | |
| 664 // GCMClient should not be checked in. | |
| 665 EXPECT_FALSE(consumer()->IsGCMClientReady()); | |
| 666 EXPECT_EQ(GCMClientMock::UNINITIALIZED, consumer()->GetGCMClient()->status()); | |
| 667 | |
| 668 // Invoking register will make GCMClient checked in. | |
| 669 std::vector<std::string> sender_ids; | |
| 670 sender_ids.push_back("sender1"); | |
| 671 consumer()->Register(kTestingAppId, sender_ids); | |
| 672 WaitUntilCompleted(); | |
| 673 | |
| 674 // GCMClient should be checked in. | |
| 675 EXPECT_TRUE(consumer()->IsGCMClientReady()); | |
| 676 EXPECT_EQ(GCMClientMock::LOADED, consumer()->GetGCMClient()->status()); | |
| 677 | |
| 678 // Registration should succeed. | |
| 679 std::string expected_registration_id = | |
| 680 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids); | |
| 681 EXPECT_EQ(expected_registration_id, consumer()->registration_id()); | |
| 682 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); | |
| 683 } | |
| 684 | |
| 596 TEST_F(GCMProfileServiceTest, SendWhenNotSignedIn) { | 685 TEST_F(GCMProfileServiceTest, SendWhenNotSignedIn) { |
| 686 consumer()->CreateGCMProfileServiceInstance(); | |
| 687 | |
| 597 GCMClient::OutgoingMessage message; | 688 GCMClient::OutgoingMessage message; |
| 598 message.id = "1"; | 689 message.id = "1"; |
| 599 message.data["key1"] = "value1"; | 690 message.data["key1"] = "value1"; |
| 600 consumer()->Send(kTestingAppId, kUserId, message); | 691 consumer()->Send(kTestingAppId, kUserId, message); |
| 601 | 692 |
| 602 EXPECT_TRUE(consumer()->send_message_id().empty()); | 693 EXPECT_TRUE(consumer()->send_message_id().empty()); |
| 603 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, consumer()->send_result()); | 694 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, consumer()->send_result()); |
| 604 } | 695 } |
| 605 | 696 |
| 697 TEST_F(GCMProfileServiceTest, SendUnderNeutralChannelSignal) { | |
| 698 // Neutral channel signal will prevent GCMClient from checking in when the | |
| 699 // profile is signed in. | |
| 700 profile()->GetPrefs()->ClearPref(prefs::kGCMChannelEnabled); | |
| 701 | |
| 702 consumer()->CreateGCMProfileServiceInstance(); | |
| 703 consumer()->SignIn(kTestingUsername); | |
| 704 | |
| 705 // GCMClient should not be checked in. | |
| 706 EXPECT_FALSE(consumer()->IsGCMClientReady()); | |
| 707 EXPECT_EQ(GCMClientMock::UNINITIALIZED, consumer()->GetGCMClient()->status()); | |
| 708 | |
| 709 // Invoking send will make GCMClient checked in. | |
| 710 GCMClient::OutgoingMessage message; | |
| 711 message.id = "1"; | |
| 712 message.data["key1"] = "value1"; | |
| 713 consumer()->Send(kTestingAppId, kUserId, message); | |
| 714 WaitUntilCompleted(); | |
| 715 | |
| 716 // GCMClient should be checked in. | |
| 717 EXPECT_TRUE(consumer()->IsGCMClientReady()); | |
| 718 EXPECT_EQ(GCMClientMock::LOADED, consumer()->GetGCMClient()->status()); | |
| 719 | |
| 720 // Sending should succeed. | |
| 721 EXPECT_EQ(consumer()->send_message_id(), message.id); | |
| 722 EXPECT_EQ(GCMClient::SUCCESS, consumer()->send_result()); | |
| 723 } | |
| 724 | |
| 606 // Tests single-profile. | 725 // Tests single-profile. |
| 607 class GCMProfileServiceSingleProfileTest : public GCMProfileServiceTest { | 726 class GCMProfileServiceSingleProfileTest : public GCMProfileServiceTest { |
| 608 public: | 727 public: |
| 609 GCMProfileServiceSingleProfileTest() { | 728 GCMProfileServiceSingleProfileTest() { |
| 610 } | 729 } |
| 611 | 730 |
| 612 virtual ~GCMProfileServiceSingleProfileTest() { | 731 virtual ~GCMProfileServiceSingleProfileTest() { |
| 613 } | 732 } |
| 614 | 733 |
| 615 virtual void SetUp() OVERRIDE { | 734 virtual void SetUp() OVERRIDE { |
| 616 GCMProfileServiceTest::SetUp(); | 735 GCMProfileServiceTest::SetUp(); |
| 617 | 736 |
| 618 consumer()->CreateGCMProfileServiceInstance(); | 737 consumer()->CreateGCMProfileServiceInstance(); |
| 619 consumer()->SignIn(kTestingUsername); | 738 consumer()->SignIn(kTestingUsername); |
| 620 } | 739 } |
| 621 }; | 740 }; |
| 622 | 741 |
| 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) { | 742 TEST_F(GCMProfileServiceSingleProfileTest, Register) { |
| 647 std::vector<std::string> sender_ids; | 743 std::vector<std::string> sender_ids; |
| 648 sender_ids.push_back("sender1"); | 744 sender_ids.push_back("sender1"); |
| 649 consumer()->Register(kTestingAppId, sender_ids); | 745 consumer()->Register(kTestingAppId, sender_ids); |
| 650 std::string expected_registration_id = | 746 std::string expected_registration_id = |
| 651 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids); | 747 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids); |
| 652 | 748 |
| 653 WaitUntilCompleted(); | 749 WaitUntilCompleted(); |
| 654 EXPECT_EQ(expected_registration_id, consumer()->registration_id()); | 750 EXPECT_EQ(expected_registration_id, consumer()->registration_id()); |
| 655 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); | 751 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 781 | 877 |
| 782 WaitUntilCompleted(); | 878 WaitUntilCompleted(); |
| 783 EXPECT_FALSE(consumer()->registration_id().empty()); | 879 EXPECT_FALSE(consumer()->registration_id().empty()); |
| 784 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); | 880 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); |
| 785 std::string old_registration_id = consumer()->registration_id(); | 881 std::string old_registration_id = consumer()->registration_id(); |
| 786 | 882 |
| 787 // Clears the results that would be set by the Register callback in | 883 // Clears the results that would be set by the Register callback in |
| 788 // preparation to call register 2nd time. | 884 // preparation to call register 2nd time. |
| 789 consumer()->clear_registration_result(); | 885 consumer()->clear_registration_result(); |
| 790 | 886 |
| 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. | 887 // Simulate start-up by recreating GCMProfileService. |
| 888 consumer()->set_gcm_client_loading_delay(GCMClientMock::DELAY_LOADING); | |
| 795 consumer()->CreateGCMProfileServiceInstance(); | 889 consumer()->CreateGCMProfileServiceInstance(); |
| 796 | 890 |
| 797 // Simulate start-up by reloading extension. | 891 // Simulate start-up by reloading extension. |
| 798 consumer()->ReloadExtension(extension); | 892 consumer()->ReloadExtension(extension); |
| 799 | 893 |
| 800 // Read the registration info from the extension's state store. | 894 // Read the registration info from the extension's state store. |
| 801 // This would hold up because GCMClient is in loading state. | 895 // This would hold up because GCMClient is in loading state. |
| 802 consumer()->Register(extension->id(), sender_ids); | 896 consumer()->Register(extension->id(), sender_ids); |
| 803 base::RunLoop().RunUntilIdle(); | 897 base::RunLoop().RunUntilIdle(); |
| 804 EXPECT_TRUE(consumer()->registration_id().empty()); | 898 EXPECT_TRUE(consumer()->registration_id().empty()); |
| 805 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, consumer()->registration_result()); | 899 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, consumer()->registration_result()); |
| 806 | 900 |
| 807 // Register operation will be invoked after GCMClient becomes ready. | 901 // Register operation will be invoked after GCMClient becomes ready. |
| 808 consumer()->GetGCMClient()->SetReady(); | 902 consumer()->GetGCMClient()->PerformDelayedLoading(); |
| 809 WaitUntilCompleted(); | 903 WaitUntilCompleted(); |
| 810 EXPECT_EQ(old_registration_id, consumer()->registration_id()); | 904 EXPECT_EQ(old_registration_id, consumer()->registration_id()); |
| 811 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); | 905 EXPECT_EQ(GCMClient::SUCCESS, consumer()->registration_result()); |
| 812 } | 906 } |
| 813 | 907 |
| 814 TEST_F(GCMProfileServiceSingleProfileTest, | 908 TEST_F(GCMProfileServiceSingleProfileTest, |
| 815 PersistedRegistrationInfoRemoveAfterSignOut) { | 909 PersistedRegistrationInfoRemoveAfterSignOut) { |
| 816 std::vector<std::string> sender_ids; | 910 std::vector<std::string> sender_ids; |
| 817 sender_ids.push_back("sender1"); | 911 sender_ids.push_back("sender1"); |
| 818 consumer()->Register(kTestingAppId, sender_ids); | 912 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(); | 1356 consumer()->gcm_event_router()->clear_results(); |
| 1263 WaitUntilCompleted(); | 1357 WaitUntilCompleted(); |
| 1264 | 1358 |
| 1265 EXPECT_EQ(FakeGCMEventRouter::MESSAGE_EVENT, | 1359 EXPECT_EQ(FakeGCMEventRouter::MESSAGE_EVENT, |
| 1266 consumer()->gcm_event_router()->received_event()); | 1360 consumer()->gcm_event_router()->received_event()); |
| 1267 EXPECT_TRUE( | 1361 EXPECT_TRUE( |
| 1268 in_message5.data == consumer()->gcm_event_router()->message().data); | 1362 in_message5.data == consumer()->gcm_event_router()->message().data); |
| 1269 } | 1363 } |
| 1270 | 1364 |
| 1271 } // namespace gcm | 1365 } // namespace gcm |
| OLD | NEW |