Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(295)

Side by Side Diff: google_apis/gcm/gcm_client_impl_unittest.cc

Issue 270873002: Extract GCMClient data types into separate gcm_types.h (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Reupload Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « google_apis/gcm/gcm_client_impl.cc ('k') | google_apis/gcm/public/gcm_types.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "google_apis/gcm/gcm_client_impl.h" 5 #include "google_apis/gcm/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 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 void CompleteUnregistration(const std::string& app_id); 229 void CompleteUnregistration(const std::string& app_id);
230 230
231 bool ExistsRegistration(const std::string& app_id) const; 231 bool ExistsRegistration(const std::string& app_id) const;
232 void AddRegistration(const std::string& app_id, 232 void AddRegistration(const std::string& app_id,
233 const std::vector<std::string>& sender_ids, 233 const std::vector<std::string>& sender_ids,
234 const std::string& registration_id); 234 const std::string& registration_id);
235 235
236 // GCMClient::Delegate overrides (for verification). 236 // GCMClient::Delegate overrides (for verification).
237 virtual void OnRegisterFinished(const std::string& app_id, 237 virtual void OnRegisterFinished(const std::string& app_id,
238 const std::string& registration_id, 238 const std::string& registration_id,
239 GCMClient::Result result) OVERRIDE; 239 Result result) OVERRIDE;
240 virtual void OnUnregisterFinished(const std::string& app_id, 240 virtual void OnUnregisterFinished(const std::string& app_id,
241 GCMClient::Result result) OVERRIDE; 241 Result result) OVERRIDE;
242 virtual void OnSendFinished(const std::string& app_id, 242 virtual void OnSendFinished(const std::string& app_id,
243 const std::string& message_id, 243 const std::string& message_id,
244 GCMClient::Result result) OVERRIDE {} 244 Result result) OVERRIDE {}
245 virtual void OnMessageReceived(const std::string& registration_id, 245 virtual void OnMessageReceived(const std::string& registration_id,
246 const GCMClient::IncomingMessage& message) 246 const IncomingMessage& message)
247 OVERRIDE; 247 OVERRIDE;
248 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; 248 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE;
249 virtual void OnMessageSendError( 249 virtual void OnMessageSendError(
250 const std::string& app_id, 250 const std::string& app_id,
251 const gcm::GCMClient::SendErrorDetails& send_error_details) OVERRIDE; 251 const gcm::SendErrorDetails& send_error_details) OVERRIDE;
252 virtual void OnGCMReady() OVERRIDE; 252 virtual void OnGCMReady() OVERRIDE;
253 253
254 GCMClientImpl* gcm_client() const { return gcm_client_.get(); } 254 GCMClientImpl* gcm_client() const { return gcm_client_.get(); }
255 FakeMCSClient* mcs_client() const { 255 FakeMCSClient* mcs_client() const {
256 return reinterpret_cast<FakeMCSClient*>(gcm_client_->mcs_client_.get()); 256 return reinterpret_cast<FakeMCSClient*>(gcm_client_->mcs_client_.get());
257 } 257 }
258 ConnectionFactory* connection_factory() const { 258 ConnectionFactory* connection_factory() const {
259 return gcm_client_->connection_factory_.get(); 259 return gcm_client_->connection_factory_.get();
260 } 260 }
261 261
262 void reset_last_event() { 262 void reset_last_event() {
263 last_event_ = NONE; 263 last_event_ = NONE;
264 last_app_id_.clear(); 264 last_app_id_.clear();
265 last_registration_id_.clear(); 265 last_registration_id_.clear();
266 last_message_id_.clear(); 266 last_message_id_.clear();
267 last_result_ = GCMClient::UNKNOWN_ERROR; 267 last_result_ = RESULT_UNKNOWN_ERROR;
268 } 268 }
269 269
270 LastEvent last_event() const { return last_event_; } 270 LastEvent last_event() const { return last_event_; }
271 const std::string& last_app_id() const { return last_app_id_; } 271 const std::string& last_app_id() const { return last_app_id_; }
272 const std::string& last_registration_id() const { 272 const std::string& last_registration_id() const {
273 return last_registration_id_; 273 return last_registration_id_;
274 } 274 }
275 const std::string& last_message_id() const { return last_message_id_; } 275 const std::string& last_message_id() const { return last_message_id_; }
276 GCMClient::Result last_result() const { return last_result_; } 276 Result last_result() const { return last_result_; }
277 const GCMClient::IncomingMessage& last_message() const { 277 const IncomingMessage& last_message() const {
278 return last_message_; 278 return last_message_;
279 } 279 }
280 const GCMClient::SendErrorDetails& last_error_details() const { 280 const SendErrorDetails& last_error_details() const {
281 return last_error_details_; 281 return last_error_details_;
282 } 282 }
283 283
284 const GServicesSettings& gservices_settings() const { 284 const GServicesSettings& gservices_settings() const {
285 return gcm_client_->gservices_settings_; 285 return gcm_client_->gservices_settings_;
286 } 286 }
287 287
288 int64 CurrentTime(); 288 int64 CurrentTime();
289 289
290 // Tooling. 290 // Tooling.
291 void PumpLoop(); 291 void PumpLoop();
292 void PumpLoopUntilIdle(); 292 void PumpLoopUntilIdle();
293 void QuitLoop(); 293 void QuitLoop();
294 void InitializeLoop(); 294 void InitializeLoop();
295 bool CreateUniqueTempDir(); 295 bool CreateUniqueTempDir();
296 AutoAdvancingTestClock* clock() const { 296 AutoAdvancingTestClock* clock() const {
297 return reinterpret_cast<AutoAdvancingTestClock*>(gcm_client_->clock_.get()); 297 return reinterpret_cast<AutoAdvancingTestClock*>(gcm_client_->clock_.get());
298 } 298 }
299 299
300 private: 300 private:
301 // Variables used for verification. 301 // Variables used for verification.
302 LastEvent last_event_; 302 LastEvent last_event_;
303 std::string last_app_id_; 303 std::string last_app_id_;
304 std::string last_registration_id_; 304 std::string last_registration_id_;
305 std::string last_message_id_; 305 std::string last_message_id_;
306 GCMClient::Result last_result_; 306 Result last_result_;
307 GCMClient::IncomingMessage last_message_; 307 IncomingMessage last_message_;
308 GCMClient::SendErrorDetails last_error_details_; 308 SendErrorDetails last_error_details_;
309 309
310 scoped_ptr<GCMClientImpl> gcm_client_; 310 scoped_ptr<GCMClientImpl> gcm_client_;
311 311
312 base::MessageLoop message_loop_; 312 base::MessageLoop message_loop_;
313 scoped_ptr<base::RunLoop> run_loop_; 313 scoped_ptr<base::RunLoop> run_loop_;
314 net::TestURLFetcherFactory url_fetcher_factory_; 314 net::TestURLFetcherFactory url_fetcher_factory_;
315 315
316 // Injected to GCM client: 316 // Injected to GCM client:
317 base::ScopedTempDir temp_directory_; 317 base::ScopedTempDir temp_directory_;
318 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; 318 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
319 }; 319 };
320 320
321 GCMClientImplTest::GCMClientImplTest() 321 GCMClientImplTest::GCMClientImplTest()
322 : last_event_(NONE), 322 : last_event_(NONE),
323 last_result_(GCMClient::UNKNOWN_ERROR), 323 last_result_(RESULT_UNKNOWN_ERROR),
324 url_request_context_getter_(new net::TestURLRequestContextGetter( 324 url_request_context_getter_(new net::TestURLRequestContextGetter(
325 message_loop_.message_loop_proxy())) { 325 message_loop_.message_loop_proxy())) {
326 } 326 }
327 327
328 GCMClientImplTest::~GCMClientImplTest() {} 328 GCMClientImplTest::~GCMClientImplTest() {}
329 329
330 void GCMClientImplTest::SetUp() { 330 void GCMClientImplTest::SetUp() {
331 testing::Test::SetUp(); 331 testing::Test::SetUp();
332 #if defined(OS_MACOSX) 332 #if defined(OS_MACOSX)
333 base::CommandLine::ForCurrentProcess()->AppendSwitch( 333 base::CommandLine::ForCurrentProcess()->AppendSwitch(
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
465 gcm_client_->OnMessageReceivedFromMCS(message); 465 gcm_client_->OnMessageReceivedFromMCS(message);
466 } 466 }
467 467
468 void GCMClientImplTest::OnGCMReady() { 468 void GCMClientImplTest::OnGCMReady() {
469 last_event_ = LOADING_COMPLETED; 469 last_event_ = LOADING_COMPLETED;
470 QuitLoop(); 470 QuitLoop();
471 } 471 }
472 472
473 void GCMClientImplTest::OnMessageReceived( 473 void GCMClientImplTest::OnMessageReceived(
474 const std::string& registration_id, 474 const std::string& registration_id,
475 const GCMClient::IncomingMessage& message) { 475 const IncomingMessage& message) {
476 last_event_ = MESSAGE_RECEIVED; 476 last_event_ = MESSAGE_RECEIVED;
477 last_app_id_ = registration_id; 477 last_app_id_ = registration_id;
478 last_message_ = message; 478 last_message_ = message;
479 QuitLoop(); 479 QuitLoop();
480 } 480 }
481 481
482 void GCMClientImplTest::OnRegisterFinished(const std::string& app_id, 482 void GCMClientImplTest::OnRegisterFinished(const std::string& app_id,
483 const std::string& registration_id, 483 const std::string& registration_id,
484 GCMClient::Result result) { 484 Result result) {
485 last_event_ = REGISTRATION_COMPLETED; 485 last_event_ = REGISTRATION_COMPLETED;
486 last_app_id_ = app_id; 486 last_app_id_ = app_id;
487 last_registration_id_ = registration_id; 487 last_registration_id_ = registration_id;
488 last_result_ = result; 488 last_result_ = result;
489 } 489 }
490 490
491 void GCMClientImplTest::OnUnregisterFinished(const std::string& app_id, 491 void GCMClientImplTest::OnUnregisterFinished(const std::string& app_id,
492 GCMClient::Result result) { 492 Result result) {
493 last_event_ = UNREGISTRATION_COMPLETED; 493 last_event_ = UNREGISTRATION_COMPLETED;
494 last_app_id_ = app_id; 494 last_app_id_ = app_id;
495 last_result_ = result; 495 last_result_ = result;
496 } 496 }
497 497
498 void GCMClientImplTest::OnMessagesDeleted(const std::string& app_id) { 498 void GCMClientImplTest::OnMessagesDeleted(const std::string& app_id) {
499 last_event_ = MESSAGES_DELETED; 499 last_event_ = MESSAGES_DELETED;
500 last_app_id_ = app_id; 500 last_app_id_ = app_id;
501 } 501 }
502 502
503 void GCMClientImplTest::OnMessageSendError( 503 void GCMClientImplTest::OnMessageSendError(
504 const std::string& app_id, 504 const std::string& app_id,
505 const gcm::GCMClient::SendErrorDetails& send_error_details) { 505 const gcm::SendErrorDetails& send_error_details) {
506 last_event_ = MESSAGE_SEND_ERROR; 506 last_event_ = MESSAGE_SEND_ERROR;
507 last_app_id_ = app_id; 507 last_app_id_ = app_id;
508 last_error_details_ = send_error_details; 508 last_error_details_ = send_error_details;
509 } 509 }
510 510
511 int64 GCMClientImplTest::CurrentTime() { 511 int64 GCMClientImplTest::CurrentTime() {
512 return clock()->Now().ToInternalValue() / base::Time::kMicrosecondsPerSecond; 512 return clock()->Now().ToInternalValue() / base::Time::kMicrosecondsPerSecond;
513 } 513 }
514 514
515 TEST_F(GCMClientImplTest, LoadingCompleted) { 515 TEST_F(GCMClientImplTest, LoadingCompleted) {
(...skipping 14 matching lines...) Expand all
530 EXPECT_FALSE(ExistsRegistration(kAppId)); 530 EXPECT_FALSE(ExistsRegistration(kAppId));
531 531
532 std::vector<std::string> senders; 532 std::vector<std::string> senders;
533 senders.push_back("sender"); 533 senders.push_back("sender");
534 gcm_client()->Register(kAppId, senders); 534 gcm_client()->Register(kAppId, senders);
535 CompleteRegistration("reg_id"); 535 CompleteRegistration("reg_id");
536 536
537 EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); 537 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
538 EXPECT_EQ(kAppId, last_app_id()); 538 EXPECT_EQ(kAppId, last_app_id());
539 EXPECT_EQ("reg_id", last_registration_id()); 539 EXPECT_EQ("reg_id", last_registration_id());
540 EXPECT_EQ(GCMClient::SUCCESS, last_result()); 540 EXPECT_EQ(RESULT_SUCCESS, last_result());
541 EXPECT_TRUE(ExistsRegistration(kAppId)); 541 EXPECT_TRUE(ExistsRegistration(kAppId));
542 } 542 }
543 543
544 TEST_F(GCMClientImplTest, DISABLED_RegisterAppFromCache) { 544 TEST_F(GCMClientImplTest, DISABLED_RegisterAppFromCache) {
545 EXPECT_FALSE(ExistsRegistration(kAppId)); 545 EXPECT_FALSE(ExistsRegistration(kAppId));
546 546
547 std::vector<std::string> senders; 547 std::vector<std::string> senders;
548 senders.push_back("sender"); 548 senders.push_back("sender");
549 gcm_client()->Register(kAppId, senders); 549 gcm_client()->Register(kAppId, senders);
550 CompleteRegistration("reg_id"); 550 CompleteRegistration("reg_id");
551 EXPECT_TRUE(ExistsRegistration(kAppId)); 551 EXPECT_TRUE(ExistsRegistration(kAppId));
552 552
553 EXPECT_EQ(kAppId, last_app_id()); 553 EXPECT_EQ(kAppId, last_app_id());
554 EXPECT_EQ("reg_id", last_registration_id()); 554 EXPECT_EQ("reg_id", last_registration_id());
555 EXPECT_EQ(GCMClient::SUCCESS, last_result()); 555 EXPECT_EQ(RESULT_SUCCESS, last_result());
556 EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); 556 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
557 557
558 // Recreate GCMClient in order to load from the persistent store. 558 // Recreate GCMClient in order to load from the persistent store.
559 BuildGCMClient(base::TimeDelta()); 559 BuildGCMClient(base::TimeDelta());
560 InitializeGCMClient(); 560 InitializeGCMClient();
561 561
562 EXPECT_TRUE(ExistsRegistration(kAppId)); 562 EXPECT_TRUE(ExistsRegistration(kAppId));
563 } 563 }
564 564
565 TEST_F(GCMClientImplTest, UnregisterApp) { 565 TEST_F(GCMClientImplTest, UnregisterApp) {
566 EXPECT_FALSE(ExistsRegistration(kAppId)); 566 EXPECT_FALSE(ExistsRegistration(kAppId));
567 567
568 std::vector<std::string> senders; 568 std::vector<std::string> senders;
569 senders.push_back("sender"); 569 senders.push_back("sender");
570 gcm_client()->Register(kAppId, senders); 570 gcm_client()->Register(kAppId, senders);
571 CompleteRegistration("reg_id"); 571 CompleteRegistration("reg_id");
572 EXPECT_TRUE(ExistsRegistration(kAppId)); 572 EXPECT_TRUE(ExistsRegistration(kAppId));
573 573
574 gcm_client()->Unregister(kAppId); 574 gcm_client()->Unregister(kAppId);
575 CompleteUnregistration(kAppId); 575 CompleteUnregistration(kAppId);
576 576
577 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); 577 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
578 EXPECT_EQ(kAppId, last_app_id()); 578 EXPECT_EQ(kAppId, last_app_id());
579 EXPECT_EQ(GCMClient::SUCCESS, last_result()); 579 EXPECT_EQ(RESULT_SUCCESS, last_result());
580 EXPECT_FALSE(ExistsRegistration(kAppId)); 580 EXPECT_FALSE(ExistsRegistration(kAppId));
581 } 581 }
582 582
583 TEST_F(GCMClientImplTest, DispatchDownstreamMessage) { 583 TEST_F(GCMClientImplTest, DispatchDownstreamMessage) {
584 // Register to receive messages from kSender and kSender2 only. 584 // Register to receive messages from kSender and kSender2 only.
585 std::vector<std::string> senders; 585 std::vector<std::string> senders;
586 senders.push_back(kSender); 586 senders.push_back(kSender);
587 senders.push_back(kSender2); 587 senders.push_back(kSender2);
588 AddRegistration(kAppId, senders, "reg_id"); 588 AddRegistration(kAppId, senders, "reg_id");
589 589
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
635 expected_data["error_details"] = "some details"; 635 expected_data["error_details"] = "some details";
636 MCSMessage message(BuildDownstreamMessage( 636 MCSMessage message(BuildDownstreamMessage(
637 kSender, kAppId, expected_data)); 637 kSender, kAppId, expected_data));
638 EXPECT_TRUE(message.IsValid()); 638 EXPECT_TRUE(message.IsValid());
639 ReceiveMessageFromMCS(message); 639 ReceiveMessageFromMCS(message);
640 640
641 EXPECT_EQ(MESSAGE_SEND_ERROR, last_event()); 641 EXPECT_EQ(MESSAGE_SEND_ERROR, last_event());
642 EXPECT_EQ(kAppId, last_app_id()); 642 EXPECT_EQ(kAppId, last_app_id());
643 EXPECT_EQ("007", last_error_details().message_id); 643 EXPECT_EQ("007", last_error_details().message_id);
644 EXPECT_EQ(1UL, last_error_details().additional_data.size()); 644 EXPECT_EQ(1UL, last_error_details().additional_data.size());
645 GCMClient::MessageData::const_iterator iter = 645 MessageData::const_iterator iter =
646 last_error_details().additional_data.find("error_details"); 646 last_error_details().additional_data.find("error_details");
647 EXPECT_TRUE(iter != last_error_details().additional_data.end()); 647 EXPECT_TRUE(iter != last_error_details().additional_data.end());
648 EXPECT_EQ("some details", iter->second); 648 EXPECT_EQ("some details", iter->second);
649 } 649 }
650 650
651 TEST_F(GCMClientImplTest, DispatchDownstreamMessgaesDeleted) { 651 TEST_F(GCMClientImplTest, DispatchDownstreamMessgaesDeleted) {
652 std::map<std::string, std::string> expected_data; 652 std::map<std::string, std::string> expected_data;
653 expected_data["message_type"] = "deleted_messages"; 653 expected_data["message_type"] = "deleted_messages";
654 MCSMessage message(BuildDownstreamMessage( 654 MCSMessage message(BuildDownstreamMessage(
655 kSender, kAppId, expected_data)); 655 kSender, kAppId, expected_data));
656 EXPECT_TRUE(message.IsValid()); 656 EXPECT_TRUE(message.IsValid());
657 ReceiveMessageFromMCS(message); 657 ReceiveMessageFromMCS(message);
658 658
659 EXPECT_EQ(MESSAGES_DELETED, last_event()); 659 EXPECT_EQ(MESSAGES_DELETED, last_event());
660 EXPECT_EQ(kAppId, last_app_id()); 660 EXPECT_EQ(kAppId, last_app_id());
661 } 661 }
662 662
663 TEST_F(GCMClientImplTest, SendMessage) { 663 TEST_F(GCMClientImplTest, SendMessage) {
664 mcs_proto::DataMessageStanza stanza; 664 mcs_proto::DataMessageStanza stanza;
665 stanza.set_ttl(500); 665 stanza.set_ttl(500);
666 666
667 GCMClient::OutgoingMessage message; 667 OutgoingMessage message;
668 message.id = "007"; 668 message.id = "007";
669 message.time_to_live = 500; 669 message.time_to_live = 500;
670 message.data["key"] = "value"; 670 message.data["key"] = "value";
671 gcm_client()->Send(kAppId, kSender, message); 671 gcm_client()->Send(kAppId, kSender, message);
672 672
673 EXPECT_EQ(kDataMessageStanzaTag, mcs_client()->last_message_tag()); 673 EXPECT_EQ(kDataMessageStanzaTag, mcs_client()->last_message_tag());
674 EXPECT_EQ(kAppId, mcs_client()->last_data_message_stanza().category()); 674 EXPECT_EQ(kAppId, mcs_client()->last_data_message_stanza().category());
675 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to()); 675 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to());
676 EXPECT_EQ(500, mcs_client()->last_data_message_stanza().ttl()); 676 EXPECT_EQ(500, mcs_client()->last_data_message_stanza().ttl());
677 EXPECT_EQ(CurrentTime(), mcs_client()->last_data_message_stanza().sent()); 677 EXPECT_EQ(CurrentTime(), mcs_client()->last_data_message_stanza().sent());
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
767 gservices_settings().checkin_interval()); 767 gservices_settings().checkin_interval());
768 EXPECT_EQ(GURL("http://alternative.url/checkin"), 768 EXPECT_EQ(GURL("http://alternative.url/checkin"),
769 gservices_settings().checkin_url()); 769 gservices_settings().checkin_url());
770 EXPECT_EQ(GURL("http://alternative.url/registration"), 770 EXPECT_EQ(GURL("http://alternative.url/registration"),
771 gservices_settings().registration_url()); 771 gservices_settings().registration_url());
772 EXPECT_EQ("http://alternative.gcm.host", gservices_settings().mcs_hostname()); 772 EXPECT_EQ("http://alternative.gcm.host", gservices_settings().mcs_hostname());
773 EXPECT_EQ(443, gservices_settings().mcs_secure_port()); 773 EXPECT_EQ(443, gservices_settings().mcs_secure_port());
774 } 774 }
775 775
776 } // namespace gcm 776 } // namespace gcm
OLDNEW
« no previous file with comments | « google_apis/gcm/gcm_client_impl.cc ('k') | google_apis/gcm/public/gcm_types.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698