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 3 // found in the LICENSE file. 4 5 #include "components/gcm_driver/gcm_driver_desktop.h" 6 7 #include "base/bind.h" 8 #include "base/bind_helpers.h" 9 #include "base/files/scoped_temp_dir.h" 10 #include "base/location.h" 11 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop_proxy.h" 13 #include "base/run_loop.h" 14 #include "base/strings/string_util.h" 15 #include "base/test/test_simple_task_runner.h" 16 #include "base/threading/thread.h" 17 #include "components/gcm_driver/fake_gcm_app_handler.h" 18 #include "components/gcm_driver/fake_gcm_client.h" 19 #include "components/gcm_driver/fake_gcm_client_factory.h" 20 #include "components/gcm_driver/gcm_app_handler.h" 21 #include "components/gcm_driver/gcm_client_factory.h" 22 #include "net/url_request/url_request_context_getter.h" 23 #include "net/url_request/url_request_test_util.h" 24 #include "testing/gtest/include/gtest/gtest.h" 25 26 namespace gcm { 27 28 namespace { 29 30 const char kTestAccountID1[] = "user1 (at) example.com"; 31 const char kTestAccountID2[] = "user2 (at) example.com"; 32 const char kTestAppID1[] = "TestApp1"; 33 const char kTestAppID2[] = "TestApp2"; 34 const char kUserID1[] = "user1"; 35 36 void PumpCurrentLoop() { 37 base::MessageLoop::ScopedNestableTaskAllower 38 nestable_task_allower(base::MessageLoop::current()); 39 base::RunLoop().RunUntilIdle(); 40 } 41 42 void PumpUILoop() { 43 PumpCurrentLoop(); 44 } 45 46 std::vector<std::string> ToSenderList(const std::string& sender_ids) { 47 std::vector<std::string> senders; 48 Tokenize(sender_ids, ",", &senders); 49 return senders; 50 } 51 52 } // namespace 53 54 class GCMDriverTest : public testing::Test { 55 public: 56 enum WaitToFinish { 57 DO_NOT_WAIT, 58 WAIT 59 }; 60 61 GCMDriverTest(); 62 virtual ~GCMDriverTest(); 63 64 // testing::Test: 65 virtual void SetUp() OVERRIDE; 66 virtual void TearDown() OVERRIDE; 67 68 GCMDriver* driver() { return driver_.get(); } 69 FakeGCMAppHandler* gcm_app_handler() { return gcm_app_handler_.get(); } 70 const std::string& registration_id() const { return registration_id_; } 71 GCMClient::Result registration_result() const { return registration_result_; } 72 const std::string& send_message_id() const { return send_message_id_; } 73 GCMClient::Result send_result() const { return send_result_; } 74 GCMClient::Result unregistration_result() const { 75 return unregistration_result_; 76 } 77 78 void PumpIOLoop(); 79 80 void ClearResults(); 81 82 bool HasAppHandlers() const; 83 FakeGCMClient* GetGCMClient(); 84 85 void CreateDriver(FakeGCMClient::StartMode gcm_client_start_mode); 86 void AddAppHandlers(); 87 void RemoveAppHandlers(); 88 89 void SignIn(const std::string& account_id); 90 void SignOut(); 91 92 void Register(const std::string& app_id, 93 const std::vector<std::string>& sender_ids, 94 WaitToFinish wait_to_finish); 95 void Send(const std::string& app_id, 96 const std::string& receiver_id, 97 const GCMClient::OutgoingMessage& message, 98 WaitToFinish wait_to_finish); 99 void Unregister(const std::string& app_id, WaitToFinish wait_to_finish); 100 101 void WaitForAsyncOperation(); 102 103 private: 104 void RegisterCompleted(const std::string& registration_id, 105 GCMClient::Result result); 106 void SendCompleted(const std::string& message_id, GCMClient::Result result); 107 void UnregisterCompleted(GCMClient::Result result); 108 109 base::ScopedTempDir temp_dir_; 110 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 111 base::MessageLoopForUI message_loop_; 112 base::Thread io_thread_; 113 scoped_ptr<GCMDriver> driver_; 114 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; 115 116 base::Closure async_operation_completed_callback_; 117 118 std::string registration_id_; 119 GCMClient::Result registration_result_; 120 std::string send_message_id_; 121 GCMClient::Result send_result_; 122 GCMClient::Result unregistration_result_; 123 124 DISALLOW_COPY_AND_ASSIGN(GCMDriverTest); 125 }; 126 127 GCMDriverTest::GCMDriverTest() 128 : task_runner_(new base::TestSimpleTaskRunner()), 129 io_thread_("IOThread"), 130 registration_result_(GCMClient::UNKNOWN_ERROR), 131 send_result_(GCMClient::UNKNOWN_ERROR), 132 unregistration_result_(GCMClient::UNKNOWN_ERROR) { 133 } 134 135 GCMDriverTest::~GCMDriverTest() { 136 } 137 138 void GCMDriverTest::SetUp() { 139 io_thread_.Start(); 140 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 141 } 142 143 void GCMDriverTest::TearDown() { 144 if (!driver_) 145 return; 146 147 driver_->Shutdown(); 148 driver_.reset(); 149 PumpIOLoop(); 150 151 io_thread_.Stop(); 152 } 153 154 void GCMDriverTest::PumpIOLoop() { 155 base::RunLoop run_loop; 156 io_thread_.message_loop_proxy()->PostTaskAndReply( 157 FROM_HERE, 158 base::Bind(&PumpCurrentLoop), 159 run_loop.QuitClosure()); 160 run_loop.Run(); 161 } 162 163 void GCMDriverTest::ClearResults() { 164 registration_id_.clear(); 165 registration_result_ = GCMClient::UNKNOWN_ERROR; 166 167 send_message_id_.clear(); 168 send_result_ = GCMClient::UNKNOWN_ERROR; 169 170 unregistration_result_ = GCMClient::UNKNOWN_ERROR; 171 } 172 173 bool GCMDriverTest::HasAppHandlers() const { 174 return !driver_->app_handlers().empty(); 175 } 176 177 FakeGCMClient* GCMDriverTest::GetGCMClient() { 178 return static_cast<FakeGCMClient*>(driver_->GetGCMClientForTesting()); 179 } 180 181 void GCMDriverTest::CreateDriver( 182 FakeGCMClient::StartMode gcm_client_start_mode) { 183 scoped_refptr<net::URLRequestContextGetter> request_context = 184 new net::TestURLRequestContextGetter(io_thread_.message_loop_proxy()); 185 // TODO(johnme): Need equivalent test coverage of GCMDriverAndroid. 186 driver_.reset(new GCMDriverDesktop( 187 scoped_ptr<GCMClientFactory>(new FakeGCMClientFactory( 188 gcm_client_start_mode, 189 base::MessageLoopProxy::current(), 190 io_thread_.message_loop_proxy())).Pass(), 191 GCMClient::ChromeBuildInfo(), 192 temp_dir_.path(), 193 request_context, 194 base::MessageLoopProxy::current(), 195 io_thread_.message_loop_proxy(), 196 task_runner_)); 197 198 gcm_app_handler_.reset(new FakeGCMAppHandler); 199 } 200 201 void GCMDriverTest::AddAppHandlers() { 202 driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get()); 203 driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get()); 204 } 205 206 void GCMDriverTest::RemoveAppHandlers() { 207 driver_->RemoveAppHandler(kTestAppID1); 208 driver_->RemoveAppHandler(kTestAppID2); 209 } 210 211 void GCMDriverTest::SignIn(const std::string& account_id) { 212 driver_->OnSignedIn(); 213 PumpIOLoop(); 214 PumpUILoop(); 215 } 216 217 void GCMDriverTest::SignOut() { 218 driver_->Purge(); 219 PumpIOLoop(); 220 PumpUILoop(); 221 } 222 223 void GCMDriverTest::Register(const std::string& app_id, 224 const std::vector<std::string>& sender_ids, 225 WaitToFinish wait_to_finish) { 226 base::RunLoop run_loop; 227 async_operation_completed_callback_ = run_loop.QuitClosure(); 228 driver_->Register(app_id, 229 sender_ids, 230 base::Bind(&GCMDriverTest::RegisterCompleted, 231 base::Unretained(this))); 232 if (wait_to_finish == WAIT) 233 run_loop.Run(); 234 } 235 236 void GCMDriverTest::Send(const std::string& app_id, 237 const std::string& receiver_id, 238 const GCMClient::OutgoingMessage& message, 239 WaitToFinish wait_to_finish) { 240 base::RunLoop run_loop; 241 async_operation_completed_callback_ = run_loop.QuitClosure(); 242 driver_->Send(app_id, 243 receiver_id, 244 message, 245 base::Bind(&GCMDriverTest::SendCompleted, 246 base::Unretained(this))); 247 if (wait_to_finish == WAIT) 248 run_loop.Run(); 249 } 250 251 void GCMDriverTest::Unregister(const std::string& app_id, 252 WaitToFinish wait_to_finish) { 253 base::RunLoop run_loop; 254 async_operation_completed_callback_ = run_loop.QuitClosure(); 255 driver_->Unregister(app_id, 256 base::Bind(&GCMDriverTest::UnregisterCompleted, 257 base::Unretained(this))); 258 if (wait_to_finish == WAIT) 259 run_loop.Run(); 260 } 261 262 void GCMDriverTest::WaitForAsyncOperation() { 263 base::RunLoop run_loop; 264 async_operation_completed_callback_ = run_loop.QuitClosure(); 265 run_loop.Run(); 266 } 267 268 void GCMDriverTest::RegisterCompleted(const std::string& registration_id, 269 GCMClient::Result result) { 270 registration_id_ = registration_id; 271 registration_result_ = result; 272 if (!async_operation_completed_callback_.is_null()) 273 async_operation_completed_callback_.Run(); 274 } 275 276 void GCMDriverTest::SendCompleted(const std::string& message_id, 277 GCMClient::Result result) { 278 send_message_id_ = message_id; 279 send_result_ = result; 280 if (!async_operation_completed_callback_.is_null()) 281 async_operation_completed_callback_.Run(); 282 } 283 284 void GCMDriverTest::UnregisterCompleted(GCMClient::Result result) { 285 unregistration_result_ = result; 286 if (!async_operation_completed_callback_.is_null()) 287 async_operation_completed_callback_.Run(); 288 } 289 290 TEST_F(GCMDriverTest, Create) { 291 // Create GCMDriver first. GCM is not started. 292 CreateDriver(FakeGCMClient::NO_DELAY_START); 293 EXPECT_FALSE(driver()->IsStarted()); 294 295 // Sign in. GCM is still not started. 296 SignIn(kTestAccountID1); 297 EXPECT_FALSE(driver()->IsStarted()); 298 EXPECT_FALSE(driver()->IsConnected()); 299 EXPECT_FALSE(gcm_app_handler()->connected()); 300 301 // GCM will be started only after both sign-in and app handler being added. 302 AddAppHandlers(); 303 EXPECT_TRUE(driver()->IsStarted()); 304 PumpIOLoop(); 305 EXPECT_TRUE(driver()->IsConnected()); 306 EXPECT_TRUE(gcm_app_handler()->connected()); 307 } 308 309 TEST_F(GCMDriverTest, Shutdown) { 310 CreateDriver(FakeGCMClient::NO_DELAY_START); 311 EXPECT_FALSE(HasAppHandlers()); 312 313 AddAppHandlers(); 314 EXPECT_TRUE(HasAppHandlers()); 315 316 driver()->Shutdown(); 317 EXPECT_FALSE(HasAppHandlers()); 318 EXPECT_FALSE(driver()->IsConnected()); 319 EXPECT_FALSE(gcm_app_handler()->connected()); 320 } 321 322 TEST_F(GCMDriverTest, SignInAndSignOutOnGCMEnabled) { 323 // By default, GCM is enabled. 324 CreateDriver(FakeGCMClient::NO_DELAY_START); 325 AddAppHandlers(); 326 327 // GCMClient should be started after sign-in. 328 SignIn(kTestAccountID1); 329 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); 330 331 // GCMClient should be checked out after sign-out. 332 SignOut(); 333 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); 334 } 335 336 TEST_F(GCMDriverTest, SignInAndSignOutOnGCMDisabled) { 337 // By default, GCM is enabled. 338 CreateDriver(FakeGCMClient::NO_DELAY_START); 339 AddAppHandlers(); 340 341 // Disable GCM. 342 driver()->Disable(); 343 344 // GCMClient should not be started after sign-in. 345 SignIn(kTestAccountID1); 346 EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status()); 347 348 // Check-out should still be performed after sign-out. 349 SignOut(); 350 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); 351 } 352 353 TEST_F(GCMDriverTest, SignOutAndThenSignIn) { 354 CreateDriver(FakeGCMClient::NO_DELAY_START); 355 AddAppHandlers(); 356 357 // GCMClient should be started after sign-in. 358 SignIn(kTestAccountID1); 359 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); 360 361 // GCMClient should be checked out after sign-out. 362 SignOut(); 363 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); 364 365 // Sign-in with a different account. 366 SignIn(kTestAccountID2); 367 368 // GCMClient should be started again. 369 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); 370 } 371 372 TEST_F(GCMDriverTest, DisableAndReenableGCM) { 373 CreateDriver(FakeGCMClient::NO_DELAY_START); 374 AddAppHandlers(); 375 SignIn(kTestAccountID1); 376 377 // GCMClient should be started. 378 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); 379 380 // Disables the GCM. 381 driver()->Disable(); 382 PumpIOLoop(); 383 PumpUILoop(); 384 385 // GCMClient should be stopped. 386 EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status()); 387 388 // Enables the GCM. 389 driver()->Enable(); 390 PumpIOLoop(); 391 PumpUILoop(); 392 393 // GCMClient should be started. 394 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); 395 396 // Disables the GCM. 397 driver()->Disable(); 398 PumpIOLoop(); 399 PumpUILoop(); 400 401 // GCMClient should be stopped. 402 EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status()); 403 404 // Sign out. 405 SignOut(); 406 407 // GCMClient should be checked out. 408 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); 409 } 410 411 TEST_F(GCMDriverTest, StartOrStopGCMOnDemand) { 412 CreateDriver(FakeGCMClient::NO_DELAY_START); 413 SignIn(kTestAccountID1); 414 415 // GCMClient is not started. 416 EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status()); 417 418 // GCMClient is started after an app handler has been added. 419 driver()->AddAppHandler(kTestAppID1, gcm_app_handler()); 420 PumpIOLoop(); 421 PumpUILoop(); 422 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); 423 424 // Add another app handler. 425 driver()->AddAppHandler(kTestAppID2, gcm_app_handler()); 426 PumpIOLoop(); 427 PumpUILoop(); 428 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); 429 430 // GCMClient remains active after one app handler is gone. 431 driver()->RemoveAppHandler(kTestAppID1); 432 PumpIOLoop(); 433 PumpUILoop(); 434 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); 435 436 // GCMClient should be stopped after the last app handler is gone. 437 driver()->RemoveAppHandler(kTestAppID2); 438 PumpIOLoop(); 439 PumpUILoop(); 440 EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status()); 441 442 // GCMClient is restarted after an app handler has been added. 443 driver()->AddAppHandler(kTestAppID2, gcm_app_handler()); 444 PumpIOLoop(); 445 PumpUILoop(); 446 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); 447 } 448 449 TEST_F(GCMDriverTest, RegisterFailed) { 450 std::vector<std::string> sender_ids; 451 sender_ids.push_back("sender1"); 452 453 CreateDriver(FakeGCMClient::NO_DELAY_START); 454 455 // Registration fails when GCM is disabled. 456 driver()->Disable(); 457 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); 458 EXPECT_TRUE(registration_id().empty()); 459 EXPECT_EQ(GCMClient::GCM_DISABLED, registration_result()); 460 461 ClearResults(); 462 463 // Registration fails when the sign-in does not occur. 464 driver()->Enable(); 465 AddAppHandlers(); 466 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); 467 EXPECT_TRUE(registration_id().empty()); 468 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, registration_result()); 469 470 ClearResults(); 471 472 // Registration fails when the no app handler is added. 473 RemoveAppHandlers(); 474 SignIn(kTestAccountID1); 475 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); 476 EXPECT_TRUE(registration_id().empty()); 477 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result()); 478 } 479 480 TEST_F(GCMDriverTest, UnregisterFailed) { 481 CreateDriver(FakeGCMClient::NO_DELAY_START); 482 483 // Unregistration fails when GCM is disabled. 484 driver()->Disable(); 485 Unregister(kTestAppID1, GCMDriverTest::WAIT); 486 EXPECT_EQ(GCMClient::GCM_DISABLED, unregistration_result()); 487 488 ClearResults(); 489 490 // Unregistration fails when the sign-in does not occur. 491 driver()->Enable(); 492 AddAppHandlers(); 493 Unregister(kTestAppID1, GCMDriverTest::WAIT); 494 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, unregistration_result()); 495 496 ClearResults(); 497 498 // Unregistration fails when the no app handler is added. 499 RemoveAppHandlers(); 500 SignIn(kTestAccountID1); 501 Unregister(kTestAppID1, GCMDriverTest::WAIT); 502 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, unregistration_result()); 503 } 504 505 TEST_F(GCMDriverTest, SendFailed) { 506 GCMClient::OutgoingMessage message; 507 message.id = "1"; 508 message.data["key1"] = "value1"; 509 510 CreateDriver(FakeGCMClient::NO_DELAY_START); 511 512 // Sending fails when GCM is disabled. 513 driver()->Disable(); 514 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); 515 EXPECT_TRUE(send_message_id().empty()); 516 EXPECT_EQ(GCMClient::GCM_DISABLED, send_result()); 517 518 ClearResults(); 519 520 // Sending fails when the sign-in does not occur. 521 driver()->Enable(); 522 AddAppHandlers(); 523 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); 524 EXPECT_TRUE(send_message_id().empty()); 525 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, send_result()); 526 527 ClearResults(); 528 529 // Sending fails when the no app handler is added. 530 RemoveAppHandlers(); 531 SignIn(kTestAccountID1); 532 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); 533 EXPECT_TRUE(send_message_id().empty()); 534 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, send_result()); 535 } 536 537 TEST_F(GCMDriverTest, GCMClientNotReadyBeforeRegistration) { 538 // Make GCMClient not ready initially. 539 CreateDriver(FakeGCMClient::DELAY_START); 540 SignIn(kTestAccountID1); 541 AddAppHandlers(); 542 543 // The registration is on hold until GCMClient is ready. 544 std::vector<std::string> sender_ids; 545 sender_ids.push_back("sender1"); 546 Register(kTestAppID1, 547 sender_ids, 548 GCMDriverTest::DO_NOT_WAIT); 549 PumpIOLoop(); 550 PumpUILoop(); 551 EXPECT_TRUE(registration_id().empty()); 552 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result()); 553 554 // Register operation will be invoked after GCMClient becomes ready. 555 GetGCMClient()->PerformDelayedLoading(); 556 WaitForAsyncOperation(); 557 EXPECT_FALSE(registration_id().empty()); 558 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); 559 } 560 561 TEST_F(GCMDriverTest, GCMClientNotReadyBeforeSending) { 562 // Make GCMClient not ready initially. 563 CreateDriver(FakeGCMClient::DELAY_START); 564 SignIn(kTestAccountID1); 565 AddAppHandlers(); 566 567 // The sending is on hold until GCMClient is ready. 568 GCMClient::OutgoingMessage message; 569 message.id = "1"; 570 message.data["key1"] = "value1"; 571 message.data["key2"] = "value2"; 572 Send(kTestAppID1, kUserID1, message, GCMDriverTest::DO_NOT_WAIT); 573 PumpIOLoop(); 574 PumpUILoop(); 575 576 EXPECT_TRUE(send_message_id().empty()); 577 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, send_result()); 578 579 // Send operation will be invoked after GCMClient becomes ready. 580 GetGCMClient()->PerformDelayedLoading(); 581 WaitForAsyncOperation(); 582 EXPECT_EQ(message.id, send_message_id()); 583 EXPECT_EQ(GCMClient::SUCCESS, send_result()); 584 } 585 586 // Tests a single instance of GCMDriver. 587 class GCMDriverFunctionalTest : public GCMDriverTest { 588 public: 589 GCMDriverFunctionalTest(); 590 virtual ~GCMDriverFunctionalTest(); 591 592 // GCMDriverTest: 593 virtual void SetUp() OVERRIDE; 594 595 private: 596 DISALLOW_COPY_AND_ASSIGN(GCMDriverFunctionalTest); 597 }; 598 599 GCMDriverFunctionalTest::GCMDriverFunctionalTest() { 600 } 601 602 GCMDriverFunctionalTest::~GCMDriverFunctionalTest() { 603 } 604 605 void GCMDriverFunctionalTest::SetUp() { 606 GCMDriverTest::SetUp(); 607 608 CreateDriver(FakeGCMClient::NO_DELAY_START); 609 AddAppHandlers(); 610 SignIn(kTestAccountID1); 611 } 612 613 TEST_F(GCMDriverFunctionalTest, Register) { 614 std::vector<std::string> sender_ids; 615 sender_ids.push_back("sender1"); 616 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); 617 const std::string expected_registration_id = 618 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids); 619 620 EXPECT_EQ(expected_registration_id, registration_id()); 621 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); 622 } 623 624 TEST_F(GCMDriverFunctionalTest, RegisterError) { 625 std::vector<std::string> sender_ids; 626 sender_ids.push_back("sender1@error"); 627 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); 628 629 EXPECT_TRUE(registration_id().empty()); 630 EXPECT_NE(GCMClient::SUCCESS, registration_result()); 631 } 632 633 TEST_F(GCMDriverFunctionalTest, RegisterAgainWithSameSenderIDs) { 634 std::vector<std::string> sender_ids; 635 sender_ids.push_back("sender1"); 636 sender_ids.push_back("sender2"); 637 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); 638 const std::string expected_registration_id = 639 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids); 640 641 EXPECT_EQ(expected_registration_id, registration_id()); 642 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); 643 644 // Clears the results the would be set by the Register callback in preparation 645 // to call register 2nd time. 646 ClearResults(); 647 648 // Calling register 2nd time with the same set of sender IDs but different 649 // ordering will get back the same registration ID. 650 std::vector<std::string> another_sender_ids; 651 another_sender_ids.push_back("sender2"); 652 another_sender_ids.push_back("sender1"); 653 Register(kTestAppID1, another_sender_ids, GCMDriverTest::WAIT); 654 655 EXPECT_EQ(expected_registration_id, registration_id()); 656 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); 657 } 658 659 TEST_F(GCMDriverFunctionalTest, RegisterAgainWithDifferentSenderIDs) { 660 std::vector<std::string> sender_ids; 661 sender_ids.push_back("sender1"); 662 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); 663 const std::string expected_registration_id = 664 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids); 665 666 EXPECT_EQ(expected_registration_id, registration_id()); 667 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); 668 669 // Make sender IDs different. 670 sender_ids.push_back("sender2"); 671 const std::string expected_registration_id2 = 672 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids); 673 674 // Calling register 2nd time with the different sender IDs will get back a new 675 // registration ID. 676 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); 677 EXPECT_EQ(expected_registration_id2, registration_id()); 678 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); 679 } 680 681 TEST_F(GCMDriverFunctionalTest, RegisterAfterSignOut) { 682 // This will trigger check-out. 683 SignOut(); 684 685 std::vector<std::string> sender_ids; 686 sender_ids.push_back("sender1"); 687 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); 688 689 EXPECT_TRUE(registration_id().empty()); 690 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, registration_result()); 691 } 692 693 TEST_F(GCMDriverFunctionalTest, UnregisterExplicitly) { 694 std::vector<std::string> sender_ids; 695 sender_ids.push_back("sender1"); 696 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); 697 698 EXPECT_FALSE(registration_id().empty()); 699 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); 700 701 Unregister(kTestAppID1, GCMDriverTest::WAIT); 702 703 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result()); 704 } 705 706 TEST_F(GCMDriverFunctionalTest, UnregisterWhenAsyncOperationPending) { 707 std::vector<std::string> sender_ids; 708 sender_ids.push_back("sender1"); 709 // First start registration without waiting for it to complete. 710 Register(kTestAppID1, 711 sender_ids, 712 GCMDriverTest::DO_NOT_WAIT); 713 714 // Test that unregistration fails with async operation pending when there is a 715 // registration already in progress. 716 Unregister(kTestAppID1, GCMDriverTest::WAIT); 717 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, 718 unregistration_result()); 719 720 // Complete the unregistration. 721 WaitForAsyncOperation(); 722 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); 723 724 // Start unregistration without waiting for it to complete. This time no async 725 // operation is pending. 726 Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT); 727 728 // Test that unregistration fails with async operation pending when there is 729 // an unregistration already in progress. 730 Unregister(kTestAppID1, GCMDriverTest::WAIT); 731 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, 732 unregistration_result()); 733 ClearResults(); 734 735 // Complete unregistration. 736 WaitForAsyncOperation(); 737 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result()); 738 } 739 740 TEST_F(GCMDriverFunctionalTest, RegisterWhenAsyncOperationPending) { 741 std::vector<std::string> sender_ids; 742 sender_ids.push_back("sender1"); 743 // First start registration without waiting for it to complete. 744 Register(kTestAppID1, 745 sender_ids, 746 GCMDriverTest::DO_NOT_WAIT); 747 748 // Test that registration fails with async operation pending when there is a 749 // registration already in progress. 750 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); 751 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, 752 registration_result()); 753 ClearResults(); 754 755 // Complete the registration. 756 WaitForAsyncOperation(); 757 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); 758 759 // Start unregistration without waiting for it to complete. This time no async 760 // operation is pending. 761 Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT); 762 763 // Test that registration fails with async operation pending when there is an 764 // unregistration already in progress. 765 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); 766 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, 767 registration_result()); 768 769 // Complete the first unregistration expecting success. 770 WaitForAsyncOperation(); 771 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result()); 772 773 // Test that it is ok to register again after unregistration. 774 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); 775 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); 776 } 777 778 TEST_F(GCMDriverFunctionalTest, Send) { 779 GCMClient::OutgoingMessage message; 780 message.id = "1"; 781 message.data["key1"] = "value1"; 782 message.data["key2"] = "value2"; 783 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); 784 785 EXPECT_EQ(message.id, send_message_id()); 786 EXPECT_EQ(GCMClient::SUCCESS, send_result()); 787 } 788 789 TEST_F(GCMDriverFunctionalTest, SendAfterSignOut) { 790 // This will trigger check-out. 791 SignOut(); 792 793 GCMClient::OutgoingMessage message; 794 message.id = "1"; 795 message.data["key1"] = "value1"; 796 message.data["key2"] = "value2"; 797 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); 798 799 EXPECT_TRUE(send_message_id().empty()); 800 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, send_result()); 801 } 802 803 TEST_F(GCMDriverFunctionalTest, SendError) { 804 GCMClient::OutgoingMessage message; 805 // Embedding error in id will tell the mock to simulate the send error. 806 message.id = "1@error"; 807 message.data["key1"] = "value1"; 808 message.data["key2"] = "value2"; 809 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); 810 811 EXPECT_EQ(message.id, send_message_id()); 812 EXPECT_EQ(GCMClient::SUCCESS, send_result()); 813 814 // Wait for the send error. 815 gcm_app_handler()->WaitForNotification(); 816 EXPECT_EQ(FakeGCMAppHandler::SEND_ERROR_EVENT, 817 gcm_app_handler()->received_event()); 818 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); 819 EXPECT_EQ(message.id, 820 gcm_app_handler()->send_error_details().message_id); 821 EXPECT_NE(GCMClient::SUCCESS, 822 gcm_app_handler()->send_error_details().result); 823 EXPECT_EQ(message.data, 824 gcm_app_handler()->send_error_details().additional_data); 825 } 826 827 TEST_F(GCMDriverFunctionalTest, MessageReceived) { 828 Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT); 829 GCMClient::IncomingMessage message; 830 message.data["key1"] = "value1"; 831 message.data["key2"] = "value2"; 832 message.sender_id = "sender"; 833 GetGCMClient()->ReceiveMessage(kTestAppID1, message); 834 gcm_app_handler()->WaitForNotification(); 835 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, 836 gcm_app_handler()->received_event()); 837 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); 838 EXPECT_EQ(message.data, gcm_app_handler()->message().data); 839 EXPECT_TRUE(gcm_app_handler()->message().collapse_key.empty()); 840 EXPECT_EQ(message.sender_id, gcm_app_handler()->message().sender_id); 841 } 842 843 TEST_F(GCMDriverFunctionalTest, MessageWithCollapseKeyReceived) { 844 Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT); 845 GCMClient::IncomingMessage message; 846 message.data["key1"] = "value1"; 847 message.collapse_key = "collapse_key_value"; 848 message.sender_id = "sender"; 849 GetGCMClient()->ReceiveMessage(kTestAppID1, message); 850 gcm_app_handler()->WaitForNotification(); 851 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, 852 gcm_app_handler()->received_event()); 853 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); 854 EXPECT_EQ(message.data, gcm_app_handler()->message().data); 855 EXPECT_EQ(message.collapse_key, 856 gcm_app_handler()->message().collapse_key); 857 } 858 859 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) { 860 GetGCMClient()->DeleteMessages(kTestAppID1); 861 gcm_app_handler()->WaitForNotification(); 862 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, 863 gcm_app_handler()->received_event()); 864 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); 865 } 866 867 } // namespace gcm 868