Chromium Code Reviews| 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/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 namespace gcm { | 28 namespace gcm { |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 enum LastEvent { | 32 enum LastEvent { |
| 33 NONE, | 33 NONE, |
| 34 LOADING_COMPLETED, | 34 LOADING_COMPLETED, |
| 35 REGISTRATION_COMPLETED, | 35 REGISTRATION_COMPLETED, |
| 36 UNREGISTRATION_COMPLETED, | 36 UNREGISTRATION_COMPLETED, |
| 37 MESSAGE_SEND_ERROR, | 37 MESSAGE_SEND_ERROR, |
| 38 MESSAGE_SEND_ACK, | |
| 38 MESSAGE_RECEIVED, | 39 MESSAGE_RECEIVED, |
| 39 MESSAGES_DELETED, | 40 MESSAGES_DELETED, |
| 40 }; | 41 }; |
| 41 | 42 |
| 42 const uint64 kDeviceAndroidId = 54321; | 43 const uint64 kDeviceAndroidId = 54321; |
| 43 const uint64 kDeviceSecurityToken = 12345; | 44 const uint64 kDeviceSecurityToken = 12345; |
| 44 const int64 kSettingsCheckinInterval = 16 * 60 * 60; | 45 const int64 kSettingsCheckinInterval = 16 * 60 * 60; |
| 45 const char kAppId[] = "app_id"; | 46 const char kAppId[] = "app_id"; |
| 46 const char kSender[] = "project_id"; | 47 const char kSender[] = "project_id"; |
| 47 const char kSender2[] = "project_id2"; | 48 const char kSender2[] = "project_id2"; |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 public: | 217 public: |
| 217 GCMClientImplTest(); | 218 GCMClientImplTest(); |
| 218 virtual ~GCMClientImplTest(); | 219 virtual ~GCMClientImplTest(); |
| 219 | 220 |
| 220 virtual void SetUp() OVERRIDE; | 221 virtual void SetUp() OVERRIDE; |
| 221 | 222 |
| 222 void BuildGCMClient(base::TimeDelta clock_step); | 223 void BuildGCMClient(base::TimeDelta clock_step); |
| 223 void InitializeGCMClient(); | 224 void InitializeGCMClient(); |
| 224 void StartGCMClient(); | 225 void StartGCMClient(); |
| 225 void ReceiveMessageFromMCS(const MCSMessage& message); | 226 void ReceiveMessageFromMCS(const MCSMessage& message); |
| 227 void ReceiveOnMessageSentToMCS( | |
| 228 const std::string& app_id, | |
| 229 const std::string& message_id, | |
| 230 const MCSClient::MessageSendStatus status); | |
| 226 void CompleteCheckin(uint64 android_id, | 231 void CompleteCheckin(uint64 android_id, |
| 227 uint64 security_token, | 232 uint64 security_token, |
| 228 const std::string& digest, | 233 const std::string& digest, |
| 229 const std::map<std::string, std::string>& settings); | 234 const std::map<std::string, std::string>& settings); |
| 230 void CompleteRegistration(const std::string& registration_id); | 235 void CompleteRegistration(const std::string& registration_id); |
| 231 void CompleteUnregistration(const std::string& app_id); | 236 void CompleteUnregistration(const std::string& app_id); |
| 232 | 237 |
| 233 bool ExistsRegistration(const std::string& app_id) const; | 238 bool ExistsRegistration(const std::string& app_id) const; |
| 234 void AddRegistration(const std::string& app_id, | 239 void AddRegistration(const std::string& app_id, |
| 235 const std::vector<std::string>& sender_ids, | 240 const std::vector<std::string>& sender_ids, |
| 236 const std::string& registration_id); | 241 const std::string& registration_id); |
| 237 | 242 |
| 238 // GCMClient::Delegate overrides (for verification). | 243 // GCMClient::Delegate overrides (for verification). |
| 239 virtual void OnRegisterFinished(const std::string& app_id, | 244 virtual void OnRegisterFinished(const std::string& app_id, |
| 240 const std::string& registration_id, | 245 const std::string& registration_id, |
| 241 GCMClient::Result result) OVERRIDE; | 246 GCMClient::Result result) OVERRIDE; |
| 242 virtual void OnUnregisterFinished(const std::string& app_id, | 247 virtual void OnUnregisterFinished(const std::string& app_id, |
| 243 GCMClient::Result result) OVERRIDE; | 248 GCMClient::Result result) OVERRIDE; |
| 244 virtual void OnSendFinished(const std::string& app_id, | 249 virtual void OnSendFinished(const std::string& app_id, |
| 245 const std::string& message_id, | 250 const std::string& message_id, |
| 246 GCMClient::Result result) OVERRIDE {} | 251 GCMClient::Result result) OVERRIDE {} |
| 247 virtual void OnMessageReceived(const std::string& registration_id, | 252 virtual void OnMessageReceived(const std::string& registration_id, |
| 248 const GCMClient::IncomingMessage& message) | 253 const GCMClient::IncomingMessage& message) |
| 249 OVERRIDE; | 254 OVERRIDE; |
| 250 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; | 255 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; |
| 251 virtual void OnMessageSendError( | 256 virtual void OnMessageSendError( |
| 252 const std::string& app_id, | 257 const std::string& app_id, |
| 253 const gcm::GCMClient::SendErrorDetails& send_error_details) OVERRIDE; | 258 const gcm::GCMClient::SendErrorDetails& send_error_details) OVERRIDE; |
| 259 virtual void OnSendAcknowledged(const std::string& app_id, | |
| 260 const std::string& message_id) OVERRIDE; | |
| 254 virtual void OnGCMReady() OVERRIDE; | 261 virtual void OnGCMReady() OVERRIDE; |
| 255 virtual void OnActivityRecorded() OVERRIDE {} | 262 virtual void OnActivityRecorded() OVERRIDE {} |
| 256 virtual void OnConnected(const net::IPEndPoint& ip_endpoint) OVERRIDE {} | 263 virtual void OnConnected(const net::IPEndPoint& ip_endpoint) OVERRIDE {} |
| 257 virtual void OnDisconnected() OVERRIDE {} | 264 virtual void OnDisconnected() OVERRIDE {} |
| 258 | 265 |
| 259 GCMClientImpl* gcm_client() const { return gcm_client_.get(); } | 266 GCMClientImpl* gcm_client() const { return gcm_client_.get(); } |
| 260 FakeMCSClient* mcs_client() const { | 267 FakeMCSClient* mcs_client() const { |
| 261 return reinterpret_cast<FakeMCSClient*>(gcm_client_->mcs_client_.get()); | 268 return reinterpret_cast<FakeMCSClient*>(gcm_client_->mcs_client_.get()); |
| 262 } | 269 } |
| 263 ConnectionFactory* connection_factory() const { | 270 ConnectionFactory* connection_factory() const { |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 467 // Start loading and check-in. | 474 // Start loading and check-in. |
| 468 gcm_client_->Start(); | 475 gcm_client_->Start(); |
| 469 | 476 |
| 470 PumpLoopUntilIdle(); | 477 PumpLoopUntilIdle(); |
| 471 } | 478 } |
| 472 | 479 |
| 473 void GCMClientImplTest::ReceiveMessageFromMCS(const MCSMessage& message) { | 480 void GCMClientImplTest::ReceiveMessageFromMCS(const MCSMessage& message) { |
| 474 gcm_client_->OnMessageReceivedFromMCS(message); | 481 gcm_client_->OnMessageReceivedFromMCS(message); |
| 475 } | 482 } |
| 476 | 483 |
| 484 void GCMClientImplTest::ReceiveOnMessageSentToMCS( | |
| 485 const std::string& app_id, | |
| 486 const std::string& message_id, | |
| 487 const MCSClient::MessageSendStatus status) { | |
| 488 gcm_client_->OnMessageSentToMCS(0LL, app_id, message_id, status); | |
| 489 } | |
| 490 | |
| 477 void GCMClientImplTest::OnGCMReady() { | 491 void GCMClientImplTest::OnGCMReady() { |
| 478 last_event_ = LOADING_COMPLETED; | 492 last_event_ = LOADING_COMPLETED; |
| 479 QuitLoop(); | 493 QuitLoop(); |
| 480 } | 494 } |
| 481 | 495 |
| 482 void GCMClientImplTest::OnMessageReceived( | 496 void GCMClientImplTest::OnMessageReceived( |
| 483 const std::string& registration_id, | 497 const std::string& registration_id, |
| 484 const GCMClient::IncomingMessage& message) { | 498 const GCMClient::IncomingMessage& message) { |
| 485 last_event_ = MESSAGE_RECEIVED; | 499 last_event_ = MESSAGE_RECEIVED; |
| 486 last_app_id_ = registration_id; | 500 last_app_id_ = registration_id; |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 510 } | 524 } |
| 511 | 525 |
| 512 void GCMClientImplTest::OnMessageSendError( | 526 void GCMClientImplTest::OnMessageSendError( |
| 513 const std::string& app_id, | 527 const std::string& app_id, |
| 514 const gcm::GCMClient::SendErrorDetails& send_error_details) { | 528 const gcm::GCMClient::SendErrorDetails& send_error_details) { |
| 515 last_event_ = MESSAGE_SEND_ERROR; | 529 last_event_ = MESSAGE_SEND_ERROR; |
| 516 last_app_id_ = app_id; | 530 last_app_id_ = app_id; |
| 517 last_error_details_ = send_error_details; | 531 last_error_details_ = send_error_details; |
| 518 } | 532 } |
| 519 | 533 |
| 534 void GCMClientImplTest::OnSendAcknowledged(const std::string& app_id, | |
| 535 const std::string& message_id) { | |
| 536 last_event_ = MESSAGE_SEND_ACK; | |
| 537 last_app_id_ = app_id; | |
| 538 last_message_id_ = message_id; | |
| 539 } | |
| 540 | |
| 520 int64 GCMClientImplTest::CurrentTime() { | 541 int64 GCMClientImplTest::CurrentTime() { |
| 521 return clock()->Now().ToInternalValue() / base::Time::kMicrosecondsPerSecond; | 542 return clock()->Now().ToInternalValue() / base::Time::kMicrosecondsPerSecond; |
| 522 } | 543 } |
| 523 | 544 |
| 524 TEST_F(GCMClientImplTest, LoadingCompleted) { | 545 TEST_F(GCMClientImplTest, LoadingCompleted) { |
| 525 EXPECT_EQ(LOADING_COMPLETED, last_event()); | 546 EXPECT_EQ(LOADING_COMPLETED, last_event()); |
| 526 EXPECT_EQ(kDeviceAndroidId, mcs_client()->last_android_id()); | 547 EXPECT_EQ(kDeviceAndroidId, mcs_client()->last_android_id()); |
| 527 EXPECT_EQ(kDeviceSecurityToken, mcs_client()->last_security_token()); | 548 EXPECT_EQ(kDeviceSecurityToken, mcs_client()->last_security_token()); |
| 528 | 549 |
| 529 // Checking freshly loaded CheckinInfo. | 550 // Checking freshly loaded CheckinInfo. |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 671 MCSMessage message(BuildDownstreamMessage( | 692 MCSMessage message(BuildDownstreamMessage( |
| 672 kSender, kAppId, expected_data)); | 693 kSender, kAppId, expected_data)); |
| 673 EXPECT_TRUE(message.IsValid()); | 694 EXPECT_TRUE(message.IsValid()); |
| 674 ReceiveMessageFromMCS(message); | 695 ReceiveMessageFromMCS(message); |
| 675 | 696 |
| 676 EXPECT_EQ(MESSAGES_DELETED, last_event()); | 697 EXPECT_EQ(MESSAGES_DELETED, last_event()); |
| 677 EXPECT_EQ(kAppId, last_app_id()); | 698 EXPECT_EQ(kAppId, last_app_id()); |
| 678 } | 699 } |
| 679 | 700 |
| 680 TEST_F(GCMClientImplTest, SendMessage) { | 701 TEST_F(GCMClientImplTest, SendMessage) { |
| 681 mcs_proto::DataMessageStanza stanza; | |
|
Nicolas Zea
2014/08/05 23:39:48
Why was this removed?
fgorski
2014/08/06 16:22:27
Because it is not needed.
It seems that when the t
| |
| 682 stanza.set_ttl(500); | |
| 683 | |
| 684 GCMClient::OutgoingMessage message; | 702 GCMClient::OutgoingMessage message; |
| 685 message.id = "007"; | 703 message.id = "007"; |
| 686 message.time_to_live = 500; | 704 message.time_to_live = 500; |
| 687 message.data["key"] = "value"; | 705 message.data["key"] = "value"; |
| 688 gcm_client()->Send(kAppId, kSender, message); | 706 gcm_client()->Send(kAppId, kSender, message); |
| 689 | 707 |
| 690 EXPECT_EQ(kDataMessageStanzaTag, mcs_client()->last_message_tag()); | 708 EXPECT_EQ(kDataMessageStanzaTag, mcs_client()->last_message_tag()); |
| 691 EXPECT_EQ(kAppId, mcs_client()->last_data_message_stanza().category()); | 709 EXPECT_EQ(kAppId, mcs_client()->last_data_message_stanza().category()); |
| 692 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to()); | 710 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to()); |
| 693 EXPECT_EQ(500, mcs_client()->last_data_message_stanza().ttl()); | 711 EXPECT_EQ(500, mcs_client()->last_data_message_stanza().ttl()); |
| 694 EXPECT_EQ(CurrentTime(), mcs_client()->last_data_message_stanza().sent()); | 712 EXPECT_EQ(CurrentTime(), mcs_client()->last_data_message_stanza().sent()); |
| 695 EXPECT_EQ("007", mcs_client()->last_data_message_stanza().id()); | 713 EXPECT_EQ("007", mcs_client()->last_data_message_stanza().id()); |
| 696 EXPECT_EQ("gcm@chrome.com", mcs_client()->last_data_message_stanza().from()); | 714 EXPECT_EQ("gcm@chrome.com", mcs_client()->last_data_message_stanza().from()); |
| 697 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to()); | 715 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to()); |
| 698 EXPECT_EQ("key", mcs_client()->last_data_message_stanza().app_data(0).key()); | 716 EXPECT_EQ("key", mcs_client()->last_data_message_stanza().app_data(0).key()); |
| 699 EXPECT_EQ("value", | 717 EXPECT_EQ("value", |
| 700 mcs_client()->last_data_message_stanza().app_data(0).value()); | 718 mcs_client()->last_data_message_stanza().app_data(0).value()); |
| 701 } | 719 } |
| 702 | 720 |
| 721 TEST_F(GCMClientImplTest, SendMessageAcknowledged) { | |
| 722 ReceiveOnMessageSentToMCS(kAppId, "007", MCSClient::SENT); | |
| 723 EXPECT_EQ(MESSAGE_SEND_ACK, last_event()); | |
| 724 EXPECT_EQ(kAppId, last_app_id()); | |
| 725 EXPECT_EQ("007", last_message_id()); | |
| 726 } | |
| 727 | |
| 703 class GCMClientImplCheckinTest : public GCMClientImplTest { | 728 class GCMClientImplCheckinTest : public GCMClientImplTest { |
| 704 public: | 729 public: |
| 705 GCMClientImplCheckinTest(); | 730 GCMClientImplCheckinTest(); |
| 706 virtual ~GCMClientImplCheckinTest(); | 731 virtual ~GCMClientImplCheckinTest(); |
| 707 | 732 |
| 708 virtual void SetUp() OVERRIDE; | 733 virtual void SetUp() OVERRIDE; |
| 709 }; | 734 }; |
| 710 | 735 |
| 711 GCMClientImplCheckinTest::GCMClientImplCheckinTest() { | 736 GCMClientImplCheckinTest::GCMClientImplCheckinTest() { |
| 712 } | 737 } |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 971 TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestartImmediately) { | 996 TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestartImmediately) { |
| 972 // Start the GCM and then stop and restart it immediately. | 997 // Start the GCM and then stop and restart it immediately. |
| 973 gcm_client()->Start(); | 998 gcm_client()->Start(); |
| 974 gcm_client()->Stop(); | 999 gcm_client()->Stop(); |
| 975 gcm_client()->Start(); | 1000 gcm_client()->Start(); |
| 976 | 1001 |
| 977 PumpLoopUntilIdle(); | 1002 PumpLoopUntilIdle(); |
| 978 } | 1003 } |
| 979 | 1004 |
| 980 } // namespace gcm | 1005 } // namespace gcm |
| OLD | NEW |