OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 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 "components/gcm_driver/gcm_client_impl.h" | 5 #include "components/gcm_driver/gcm_client_impl.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 GCMClientImplTest(); | 241 GCMClientImplTest(); |
242 ~GCMClientImplTest() override; | 242 ~GCMClientImplTest() override; |
243 | 243 |
244 void SetUp() override; | 244 void SetUp() override; |
245 | 245 |
246 void SetUpUrlFetcherFactory(); | 246 void SetUpUrlFetcherFactory(); |
247 | 247 |
248 void BuildGCMClient(base::TimeDelta clock_step); | 248 void BuildGCMClient(base::TimeDelta clock_step); |
249 void InitializeGCMClient(); | 249 void InitializeGCMClient(); |
250 void StartGCMClient(); | 250 void StartGCMClient(); |
| 251 void Register(const std::string& app_id, |
| 252 const std::vector<std::string>& senders); |
| 253 void Unregister(const std::string& app_id); |
251 void ReceiveMessageFromMCS(const MCSMessage& message); | 254 void ReceiveMessageFromMCS(const MCSMessage& message); |
252 void ReceiveOnMessageSentToMCS( | 255 void ReceiveOnMessageSentToMCS( |
253 const std::string& app_id, | 256 const std::string& app_id, |
254 const std::string& message_id, | 257 const std::string& message_id, |
255 const MCSClient::MessageSendStatus status); | 258 const MCSClient::MessageSendStatus status); |
256 void CompleteCheckin(uint64 android_id, | 259 void CompleteCheckin(uint64 android_id, |
257 uint64 security_token, | 260 uint64 security_token, |
258 const std::string& digest, | 261 const std::string& digest, |
259 const std::map<std::string, std::string>& settings); | 262 const std::map<std::string, std::string>& settings); |
260 void CompleteRegistration(const std::string& registration_id); | 263 void CompleteRegistration(const std::string& registration_id); |
261 void CompleteUnregistration(const std::string& app_id); | 264 void CompleteUnregistration(const std::string& app_id); |
262 void VerifyPendingRequestFetcherDeleted(); | 265 void VerifyPendingRequestFetcherDeleted(); |
263 | 266 |
264 bool ExistsRegistration(const std::string& app_id) const; | 267 bool ExistsRegistration(const std::string& app_id) const; |
265 void AddRegistration(const std::string& app_id, | 268 void AddRegistration(const std::string& app_id, |
266 const std::vector<std::string>& sender_ids, | 269 const std::vector<std::string>& sender_ids, |
267 const std::string& registration_id); | 270 const std::string& registration_id); |
268 | 271 |
269 // GCMClient::Delegate overrides (for verification). | 272 // GCMClient::Delegate overrides (for verification). |
270 void OnRegisterFinished(const std::string& app_id, | 273 void OnRegisterFinished(const linked_ptr<RegistrationInfo>& registration_info, |
271 const std::string& registration_id, | 274 const std::string& registration_id, |
272 GCMClient::Result result) override; | 275 GCMClient::Result result) override; |
273 void OnUnregisterFinished(const std::string& app_id, | 276 void OnUnregisterFinished( |
274 GCMClient::Result result) override; | 277 const linked_ptr<RegistrationInfo>& registration_info, |
| 278 GCMClient::Result result) override; |
275 void OnSendFinished(const std::string& app_id, | 279 void OnSendFinished(const std::string& app_id, |
276 const std::string& message_id, | 280 const std::string& message_id, |
277 GCMClient::Result result) override {} | 281 GCMClient::Result result) override {} |
278 void OnMessageReceived(const std::string& registration_id, | 282 void OnMessageReceived(const std::string& registration_id, |
279 const GCMClient::IncomingMessage& message) override; | 283 const GCMClient::IncomingMessage& message) override; |
280 void OnMessagesDeleted(const std::string& app_id) override; | 284 void OnMessagesDeleted(const std::string& app_id) override; |
281 void OnMessageSendError( | 285 void OnMessageSendError( |
282 const std::string& app_id, | 286 const std::string& app_id, |
283 const gcm::GCMClient::SendErrorDetails& send_error_details) override; | 287 const gcm::GCMClient::SendErrorDetails& send_error_details) override; |
284 void OnSendAcknowledged(const std::string& app_id, | 288 void OnSendAcknowledged(const std::string& app_id, |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 fetcher->SetResponseString(response); | 491 fetcher->SetResponseString(response); |
488 fetcher->delegate()->OnURLFetchComplete(fetcher); | 492 fetcher->delegate()->OnURLFetchComplete(fetcher); |
489 } | 493 } |
490 | 494 |
491 void GCMClientImplTest::VerifyPendingRequestFetcherDeleted() { | 495 void GCMClientImplTest::VerifyPendingRequestFetcherDeleted() { |
492 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 496 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
493 EXPECT_FALSE(fetcher); | 497 EXPECT_FALSE(fetcher); |
494 } | 498 } |
495 | 499 |
496 bool GCMClientImplTest::ExistsRegistration(const std::string& app_id) const { | 500 bool GCMClientImplTest::ExistsRegistration(const std::string& app_id) const { |
497 return gcm_client_->registrations_.count(app_id) > 0; | 501 return ExistsGCMRegistrationInMap(gcm_client_->registrations_, app_id); |
498 } | 502 } |
499 | 503 |
500 void GCMClientImplTest::AddRegistration( | 504 void GCMClientImplTest::AddRegistration( |
501 const std::string& app_id, | 505 const std::string& app_id, |
502 const std::vector<std::string>& sender_ids, | 506 const std::vector<std::string>& sender_ids, |
503 const std::string& registration_id) { | 507 const std::string& registration_id) { |
504 linked_ptr<RegistrationInfo> registration(new RegistrationInfo); | 508 linked_ptr<GCMRegistrationInfo> registration(new GCMRegistrationInfo); |
| 509 registration->app_id = app_id; |
505 registration->sender_ids = sender_ids; | 510 registration->sender_ids = sender_ids; |
506 registration->registration_id = registration_id; | 511 gcm_client_->registrations_[registration] = registration_id; |
507 gcm_client_->registrations_[app_id] = registration; | |
508 } | 512 } |
509 | 513 |
510 void GCMClientImplTest::InitializeGCMClient() { | 514 void GCMClientImplTest::InitializeGCMClient() { |
511 clock()->Advance(base::TimeDelta::FromMilliseconds(1)); | 515 clock()->Advance(base::TimeDelta::FromMilliseconds(1)); |
512 | 516 |
513 // Actual initialization. | 517 // Actual initialization. |
514 GCMClient::ChromeBuildInfo chrome_build_info; | 518 GCMClient::ChromeBuildInfo chrome_build_info; |
515 gcm_client_->Initialize(chrome_build_info, | 519 gcm_client_->Initialize(chrome_build_info, |
516 temp_directory_.path(), | 520 temp_directory_.path(), |
517 message_loop_.message_loop_proxy(), | 521 message_loop_.message_loop_proxy(), |
518 url_request_context_getter_, | 522 url_request_context_getter_, |
519 make_scoped_ptr<Encryptor>(new FakeEncryptor), | 523 make_scoped_ptr<Encryptor>(new FakeEncryptor), |
520 this); | 524 this); |
521 } | 525 } |
522 | 526 |
523 void GCMClientImplTest::StartGCMClient() { | 527 void GCMClientImplTest::StartGCMClient() { |
524 // Start loading and check-in. | 528 // Start loading and check-in. |
525 gcm_client_->Start(GCMClient::IMMEDIATE_START); | 529 gcm_client_->Start(GCMClient::IMMEDIATE_START); |
526 | 530 |
527 PumpLoopUntilIdle(); | 531 PumpLoopUntilIdle(); |
528 } | 532 } |
529 | 533 |
| 534 void GCMClientImplTest::Register(const std::string& app_id, |
| 535 const std::vector<std::string>& senders) { |
| 536 scoped_ptr<GCMRegistrationInfo> gcm_info(new GCMRegistrationInfo); |
| 537 gcm_info->app_id = app_id; |
| 538 gcm_info->sender_ids = senders; |
| 539 gcm_client()->Register(make_linked_ptr<RegistrationInfo>(gcm_info.release())); |
| 540 } |
| 541 |
| 542 void GCMClientImplTest::Unregister(const std::string& app_id) { |
| 543 scoped_ptr<GCMRegistrationInfo> gcm_info(new GCMRegistrationInfo); |
| 544 gcm_info->app_id = app_id; |
| 545 gcm_client()->Unregister( |
| 546 make_linked_ptr<RegistrationInfo>(gcm_info.release())); |
| 547 } |
| 548 |
530 void GCMClientImplTest::ReceiveMessageFromMCS(const MCSMessage& message) { | 549 void GCMClientImplTest::ReceiveMessageFromMCS(const MCSMessage& message) { |
531 gcm_client_->recorder_.RecordConnectionInitiated(std::string()); | 550 gcm_client_->recorder_.RecordConnectionInitiated(std::string()); |
532 gcm_client_->recorder_.RecordConnectionSuccess(); | 551 gcm_client_->recorder_.RecordConnectionSuccess(); |
533 gcm_client_->OnMessageReceivedFromMCS(message); | 552 gcm_client_->OnMessageReceivedFromMCS(message); |
534 } | 553 } |
535 | 554 |
536 void GCMClientImplTest::ReceiveOnMessageSentToMCS( | 555 void GCMClientImplTest::ReceiveOnMessageSentToMCS( |
537 const std::string& app_id, | 556 const std::string& app_id, |
538 const std::string& message_id, | 557 const std::string& message_id, |
539 const MCSClient::MessageSendStatus status) { | 558 const MCSClient::MessageSendStatus status) { |
(...skipping 11 matching lines...) Expand all Loading... |
551 | 570 |
552 void GCMClientImplTest::OnMessageReceived( | 571 void GCMClientImplTest::OnMessageReceived( |
553 const std::string& registration_id, | 572 const std::string& registration_id, |
554 const GCMClient::IncomingMessage& message) { | 573 const GCMClient::IncomingMessage& message) { |
555 last_event_ = MESSAGE_RECEIVED; | 574 last_event_ = MESSAGE_RECEIVED; |
556 last_app_id_ = registration_id; | 575 last_app_id_ = registration_id; |
557 last_message_ = message; | 576 last_message_ = message; |
558 QuitLoop(); | 577 QuitLoop(); |
559 } | 578 } |
560 | 579 |
561 void GCMClientImplTest::OnRegisterFinished(const std::string& app_id, | 580 void GCMClientImplTest::OnRegisterFinished( |
562 const std::string& registration_id, | 581 const linked_ptr<RegistrationInfo>& registration_info, |
563 GCMClient::Result result) { | 582 const std::string& registration_id, |
| 583 GCMClient::Result result) { |
564 last_event_ = REGISTRATION_COMPLETED; | 584 last_event_ = REGISTRATION_COMPLETED; |
565 last_app_id_ = app_id; | 585 last_app_id_ = registration_info->app_id; |
566 last_registration_id_ = registration_id; | 586 last_registration_id_ = registration_id; |
567 last_result_ = result; | 587 last_result_ = result; |
568 } | 588 } |
569 | 589 |
570 void GCMClientImplTest::OnUnregisterFinished(const std::string& app_id, | 590 void GCMClientImplTest::OnUnregisterFinished( |
571 GCMClient::Result result) { | 591 const linked_ptr<RegistrationInfo>& registration_info, |
| 592 GCMClient::Result result) { |
572 last_event_ = UNREGISTRATION_COMPLETED; | 593 last_event_ = UNREGISTRATION_COMPLETED; |
573 last_app_id_ = app_id; | 594 last_app_id_ = registration_info->app_id; |
574 last_result_ = result; | 595 last_result_ = result; |
575 } | 596 } |
576 | 597 |
577 void GCMClientImplTest::OnMessagesDeleted(const std::string& app_id) { | 598 void GCMClientImplTest::OnMessagesDeleted(const std::string& app_id) { |
578 last_event_ = MESSAGES_DELETED; | 599 last_event_ = MESSAGES_DELETED; |
579 last_app_id_ = app_id; | 600 last_app_id_ = app_id; |
580 } | 601 } |
581 | 602 |
582 void GCMClientImplTest::OnMessageSendError( | 603 void GCMClientImplTest::OnMessageSendError( |
583 const std::string& app_id, | 604 const std::string& app_id, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
635 EXPECT_EQ(LOADING_COMPLETED, last_event()); | 656 EXPECT_EQ(LOADING_COMPLETED, last_event()); |
636 EXPECT_EQ(kDeviceAndroidId2, mcs_client()->last_android_id()); | 657 EXPECT_EQ(kDeviceAndroidId2, mcs_client()->last_android_id()); |
637 EXPECT_EQ(kDeviceSecurityToken2, mcs_client()->last_security_token()); | 658 EXPECT_EQ(kDeviceSecurityToken2, mcs_client()->last_security_token()); |
638 } | 659 } |
639 | 660 |
640 TEST_F(GCMClientImplTest, RegisterApp) { | 661 TEST_F(GCMClientImplTest, RegisterApp) { |
641 EXPECT_FALSE(ExistsRegistration(kAppId)); | 662 EXPECT_FALSE(ExistsRegistration(kAppId)); |
642 | 663 |
643 std::vector<std::string> senders; | 664 std::vector<std::string> senders; |
644 senders.push_back("sender"); | 665 senders.push_back("sender"); |
645 gcm_client()->Register(kAppId, senders); | 666 Register(kAppId, senders); |
646 CompleteRegistration("reg_id"); | 667 CompleteRegistration("reg_id"); |
647 | 668 |
648 EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); | 669 EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
649 EXPECT_EQ(kAppId, last_app_id()); | 670 EXPECT_EQ(kAppId, last_app_id()); |
650 EXPECT_EQ("reg_id", last_registration_id()); | 671 EXPECT_EQ("reg_id", last_registration_id()); |
651 EXPECT_EQ(GCMClient::SUCCESS, last_result()); | 672 EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
652 EXPECT_TRUE(ExistsRegistration(kAppId)); | 673 EXPECT_TRUE(ExistsRegistration(kAppId)); |
653 } | 674 } |
654 | 675 |
655 TEST_F(GCMClientImplTest, DISABLED_RegisterAppFromCache) { | 676 TEST_F(GCMClientImplTest, DISABLED_RegisterAppFromCache) { |
656 EXPECT_FALSE(ExistsRegistration(kAppId)); | 677 EXPECT_FALSE(ExistsRegistration(kAppId)); |
657 | 678 |
658 std::vector<std::string> senders; | 679 std::vector<std::string> senders; |
659 senders.push_back("sender"); | 680 senders.push_back("sender"); |
660 gcm_client()->Register(kAppId, senders); | 681 Register(kAppId, senders); |
661 CompleteRegistration("reg_id"); | 682 CompleteRegistration("reg_id"); |
662 EXPECT_TRUE(ExistsRegistration(kAppId)); | 683 EXPECT_TRUE(ExistsRegistration(kAppId)); |
663 | 684 |
664 EXPECT_EQ(kAppId, last_app_id()); | 685 EXPECT_EQ(kAppId, last_app_id()); |
665 EXPECT_EQ("reg_id", last_registration_id()); | 686 EXPECT_EQ("reg_id", last_registration_id()); |
666 EXPECT_EQ(GCMClient::SUCCESS, last_result()); | 687 EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
667 EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); | 688 EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
668 | 689 |
669 // Recreate GCMClient in order to load from the persistent store. | 690 // Recreate GCMClient in order to load from the persistent store. |
670 BuildGCMClient(base::TimeDelta()); | 691 BuildGCMClient(base::TimeDelta()); |
671 InitializeGCMClient(); | 692 InitializeGCMClient(); |
672 StartGCMClient(); | 693 StartGCMClient(); |
673 | 694 |
674 EXPECT_TRUE(ExistsRegistration(kAppId)); | 695 EXPECT_TRUE(ExistsRegistration(kAppId)); |
675 } | 696 } |
676 | 697 |
677 TEST_F(GCMClientImplTest, UnregisterApp) { | 698 TEST_F(GCMClientImplTest, UnregisterApp) { |
678 EXPECT_FALSE(ExistsRegistration(kAppId)); | 699 EXPECT_FALSE(ExistsRegistration(kAppId)); |
679 | 700 |
680 std::vector<std::string> senders; | 701 std::vector<std::string> senders; |
681 senders.push_back("sender"); | 702 senders.push_back("sender"); |
682 gcm_client()->Register(kAppId, senders); | 703 Register(kAppId, senders); |
683 CompleteRegistration("reg_id"); | 704 CompleteRegistration("reg_id"); |
684 EXPECT_TRUE(ExistsRegistration(kAppId)); | 705 EXPECT_TRUE(ExistsRegistration(kAppId)); |
685 | 706 |
686 gcm_client()->Unregister(kAppId); | 707 Unregister(kAppId); |
687 CompleteUnregistration(kAppId); | 708 CompleteUnregistration(kAppId); |
688 | 709 |
689 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); | 710 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
690 EXPECT_EQ(kAppId, last_app_id()); | 711 EXPECT_EQ(kAppId, last_app_id()); |
691 EXPECT_EQ(GCMClient::SUCCESS, last_result()); | 712 EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
692 EXPECT_FALSE(ExistsRegistration(kAppId)); | 713 EXPECT_FALSE(ExistsRegistration(kAppId)); |
693 } | 714 } |
694 | 715 |
695 // Tests that stopping the GCMClient also deletes pending registration requests. | 716 // Tests that stopping the GCMClient also deletes pending registration requests. |
696 // This is tested by checking that url fetcher contained in the request was | 717 // This is tested by checking that url fetcher contained in the request was |
697 // deleted. | 718 // deleted. |
698 TEST_F(GCMClientImplTest, DeletePendingRequestsWhenStopping) { | 719 TEST_F(GCMClientImplTest, DeletePendingRequestsWhenStopping) { |
699 std::vector<std::string> senders; | 720 std::vector<std::string> senders; |
700 senders.push_back("sender"); | 721 senders.push_back("sender"); |
701 gcm_client()->Register(kAppId, senders); | 722 Register(kAppId, senders); |
702 | 723 |
703 gcm_client()->Stop(); | 724 gcm_client()->Stop(); |
704 VerifyPendingRequestFetcherDeleted(); | 725 VerifyPendingRequestFetcherDeleted(); |
705 } | 726 } |
706 | 727 |
707 TEST_F(GCMClientImplTest, DispatchDownstreamMessage) { | 728 TEST_F(GCMClientImplTest, DispatchDownstreamMessage) { |
708 // Register to receive messages from kSender and kSender2 only. | 729 // Register to receive messages from kSender and kSender2 only. |
709 std::vector<std::string> senders; | 730 std::vector<std::string> senders; |
710 senders.push_back(kSender); | 731 senders.push_back(kSender); |
711 senders.push_back(kSender2); | 732 senders.push_back(kSender2); |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1141 // Start the GCM immediately and complete the checkin. | 1162 // Start the GCM immediately and complete the checkin. |
1142 gcm_client()->Start(GCMClient::IMMEDIATE_START); | 1163 gcm_client()->Start(GCMClient::IMMEDIATE_START); |
1143 PumpLoopUntilIdle(); | 1164 PumpLoopUntilIdle(); |
1144 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); | 1165 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); |
1145 DefaultCompleteCheckin(); | 1166 DefaultCompleteCheckin(); |
1146 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); | 1167 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
1147 | 1168 |
1148 // Registration. | 1169 // Registration. |
1149 std::vector<std::string> senders; | 1170 std::vector<std::string> senders; |
1150 senders.push_back("sender"); | 1171 senders.push_back("sender"); |
1151 gcm_client()->Register(kAppId, senders); | 1172 Register(kAppId, senders); |
1152 CompleteRegistration("reg_id"); | 1173 CompleteRegistration("reg_id"); |
1153 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); | 1174 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
1154 | 1175 |
1155 // Stop the GCM. | 1176 // Stop the GCM. |
1156 gcm_client()->Stop(); | 1177 gcm_client()->Stop(); |
1157 PumpLoopUntilIdle(); | 1178 PumpLoopUntilIdle(); |
1158 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); | 1179 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
1159 | 1180 |
1160 // Delay start the GCM. GCM is indeed started without delay because the | 1181 // Delay start the GCM. GCM is indeed started without delay because the |
1161 // registration record has been found. | 1182 // registration record has been found. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1195 ASSERT_EQ(1UL, last_account_mappings().size()); | 1216 ASSERT_EQ(1UL, last_account_mappings().size()); |
1196 const AccountMapping& actual_mapping = last_account_mappings()[0]; | 1217 const AccountMapping& actual_mapping = last_account_mappings()[0]; |
1197 EXPECT_EQ(expected_mapping.account_id, actual_mapping.account_id); | 1218 EXPECT_EQ(expected_mapping.account_id, actual_mapping.account_id); |
1198 EXPECT_EQ(expected_mapping.email, actual_mapping.email); | 1219 EXPECT_EQ(expected_mapping.email, actual_mapping.email); |
1199 EXPECT_EQ(expected_mapping.status, actual_mapping.status); | 1220 EXPECT_EQ(expected_mapping.status, actual_mapping.status); |
1200 EXPECT_EQ(expected_mapping.status_change_timestamp, | 1221 EXPECT_EQ(expected_mapping.status_change_timestamp, |
1201 actual_mapping.status_change_timestamp); | 1222 actual_mapping.status_change_timestamp); |
1202 } | 1223 } |
1203 | 1224 |
1204 } // namespace gcm | 1225 } // namespace gcm |
OLD | NEW |