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