| 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 "chrome/browser/services/gcm/gcm_driver.h" | 5 #include "chrome/browser/services/gcm/gcm_driver.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/message_loop/message_loop_proxy.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/test/test_simple_task_runner.h" | 15 #include "base/test/test_simple_task_runner.h" |
| 16 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
| 17 #include "chrome/browser/services/gcm/fake_gcm_app_handler.h" |
| 17 #include "chrome/browser/services/gcm/fake_gcm_client.h" | 18 #include "chrome/browser/services/gcm/fake_gcm_client.h" |
| 18 #include "chrome/browser/services/gcm/fake_gcm_client_factory.h" | 19 #include "chrome/browser/services/gcm/fake_gcm_client_factory.h" |
| 19 #include "chrome/browser/services/gcm/gcm_app_handler.h" | 20 #include "chrome/browser/services/gcm/gcm_app_handler.h" |
| 20 #include "components/gcm_driver/gcm_client_factory.h" | 21 #include "components/gcm_driver/gcm_client_factory.h" |
| 21 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 22 #include "google_apis/gaia/fake_identity_provider.h" | 23 #include "google_apis/gaia/fake_identity_provider.h" |
| 23 #include "google_apis/gaia/fake_oauth2_token_service.h" | 24 #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_context_getter.h" |
| 25 #include "net/url_request/url_request_test_util.h" | 26 #include "net/url_request/url_request_test_util.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 44 void PumpUILoop() { | 45 void PumpUILoop() { |
| 45 PumpCurrentLoop(); | 46 PumpCurrentLoop(); |
| 46 } | 47 } |
| 47 | 48 |
| 48 std::vector<std::string> ToSenderList(const std::string& sender_ids) { | 49 std::vector<std::string> ToSenderList(const std::string& sender_ids) { |
| 49 std::vector<std::string> senders; | 50 std::vector<std::string> senders; |
| 50 Tokenize(sender_ids, ",", &senders); | 51 Tokenize(sender_ids, ",", &senders); |
| 51 return senders; | 52 return senders; |
| 52 } | 53 } |
| 53 | 54 |
| 54 class FakeGCMAppHandler : public GCMAppHandler { | |
| 55 public: | |
| 56 enum Event { | |
| 57 NO_EVENT, | |
| 58 MESSAGE_EVENT, | |
| 59 MESSAGES_DELETED_EVENT, | |
| 60 SEND_ERROR_EVENT | |
| 61 }; | |
| 62 | |
| 63 FakeGCMAppHandler(); | |
| 64 virtual ~FakeGCMAppHandler(); | |
| 65 | |
| 66 const Event& received_event() const { return received_event_; } | |
| 67 const std::string& app_id() const { return app_id_; } | |
| 68 const GCMClient::IncomingMessage& message() const { return message_; } | |
| 69 const GCMClient::SendErrorDetails& send_error_details() const { | |
| 70 return send_error_details_; | |
| 71 } | |
| 72 | |
| 73 void WaitForNotification(); | |
| 74 | |
| 75 // GCMAppHandler: | |
| 76 virtual void ShutdownHandler() OVERRIDE; | |
| 77 virtual void OnMessage(const std::string& app_id, | |
| 78 const GCMClient::IncomingMessage& message) OVERRIDE; | |
| 79 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; | |
| 80 virtual void OnSendError( | |
| 81 const std::string& app_id, | |
| 82 const GCMClient::SendErrorDetails& send_error_details) OVERRIDE; | |
| 83 | |
| 84 private: | |
| 85 void ClearResults(); | |
| 86 | |
| 87 scoped_ptr<base::RunLoop> run_loop_; | |
| 88 | |
| 89 Event received_event_; | |
| 90 std::string app_id_; | |
| 91 GCMClient::IncomingMessage message_; | |
| 92 GCMClient::SendErrorDetails send_error_details_; | |
| 93 | |
| 94 DISALLOW_COPY_AND_ASSIGN(FakeGCMAppHandler); | |
| 95 }; | |
| 96 | |
| 97 FakeGCMAppHandler::FakeGCMAppHandler() : received_event_(NO_EVENT) { | |
| 98 } | |
| 99 | |
| 100 FakeGCMAppHandler::~FakeGCMAppHandler() { | |
| 101 } | |
| 102 | |
| 103 void FakeGCMAppHandler::WaitForNotification() { | |
| 104 run_loop_.reset(new base::RunLoop); | |
| 105 run_loop_->Run(); | |
| 106 run_loop_.reset(); | |
| 107 } | |
| 108 | |
| 109 void FakeGCMAppHandler::ShutdownHandler() { | |
| 110 } | |
| 111 | |
| 112 void FakeGCMAppHandler::OnMessage(const std::string& app_id, | |
| 113 const GCMClient::IncomingMessage& message) { | |
| 114 ClearResults(); | |
| 115 received_event_ = MESSAGE_EVENT; | |
| 116 app_id_ = app_id; | |
| 117 message_ = message; | |
| 118 if (run_loop_) | |
| 119 run_loop_->Quit(); | |
| 120 } | |
| 121 | |
| 122 void FakeGCMAppHandler::OnMessagesDeleted(const std::string& app_id) { | |
| 123 ClearResults(); | |
| 124 received_event_ = MESSAGES_DELETED_EVENT; | |
| 125 app_id_ = app_id; | |
| 126 if (run_loop_) | |
| 127 run_loop_->Quit(); | |
| 128 } | |
| 129 | |
| 130 void FakeGCMAppHandler::OnSendError( | |
| 131 const std::string& app_id, | |
| 132 const GCMClient::SendErrorDetails& send_error_details) { | |
| 133 ClearResults(); | |
| 134 received_event_ = SEND_ERROR_EVENT; | |
| 135 app_id_ = app_id; | |
| 136 send_error_details_ = send_error_details; | |
| 137 if (run_loop_) | |
| 138 run_loop_->Quit(); | |
| 139 } | |
| 140 | |
| 141 void FakeGCMAppHandler::ClearResults() { | |
| 142 received_event_ = NO_EVENT; | |
| 143 app_id_.clear(); | |
| 144 message_ = GCMClient::IncomingMessage(); | |
| 145 send_error_details_ = GCMClient::SendErrorDetails(); | |
| 146 } | |
| 147 | |
| 148 } // namespace | 55 } // namespace |
| 149 | 56 |
| 150 class GCMDriverTest : public testing::Test { | 57 class GCMDriverTest : public testing::Test { |
| 151 public: | 58 public: |
| 152 enum WaitToFinish { | 59 enum WaitToFinish { |
| 153 DO_NOT_WAIT, | 60 DO_NOT_WAIT, |
| 154 WAIT | 61 WAIT |
| 155 }; | 62 }; |
| 156 | 63 |
| 157 GCMDriverTest(); | 64 GCMDriverTest(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 172 } | 79 } |
| 173 | 80 |
| 174 void PumpIOLoop(); | 81 void PumpIOLoop(); |
| 175 | 82 |
| 176 void ClearResults(); | 83 void ClearResults(); |
| 177 | 84 |
| 178 bool HasAppHandlers() const; | 85 bool HasAppHandlers() const; |
| 179 FakeGCMClient* GetGCMClient(); | 86 FakeGCMClient* GetGCMClient(); |
| 180 | 87 |
| 181 void CreateDriver(FakeGCMClient::StartMode gcm_client_start_mode); | 88 void CreateDriver(FakeGCMClient::StartMode gcm_client_start_mode); |
| 89 void AddAppHandlers(); |
| 90 void RemoveAppHandlers(); |
| 182 | 91 |
| 183 void SignIn(const std::string& account_id); | 92 void SignIn(const std::string& account_id); |
| 184 void SignOut(); | 93 void SignOut(); |
| 185 | 94 |
| 186 void Register(const std::string& app_id, | 95 void Register(const std::string& app_id, |
| 187 const std::vector<std::string>& sender_ids, | 96 const std::vector<std::string>& sender_ids, |
| 188 WaitToFinish wait_to_finish); | 97 WaitToFinish wait_to_finish); |
| 189 void Send(const std::string& app_id, | 98 void Send(const std::string& app_id, |
| 190 const std::string& receiver_id, | 99 const std::string& receiver_id, |
| 191 const GCMClient::OutgoingMessage& message, | 100 const GCMClient::OutgoingMessage& message, |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 base::MessageLoopProxy::current(), | 197 base::MessageLoopProxy::current(), |
| 289 io_thread_.message_loop_proxy())).Pass(), | 198 io_thread_.message_loop_proxy())).Pass(), |
| 290 identity_provider_owner_.PassAs<IdentityProvider>(), | 199 identity_provider_owner_.PassAs<IdentityProvider>(), |
| 291 temp_dir_.path(), | 200 temp_dir_.path(), |
| 292 request_context, | 201 request_context, |
| 293 base::MessageLoopProxy::current(), | 202 base::MessageLoopProxy::current(), |
| 294 io_thread_.message_loop_proxy(), | 203 io_thread_.message_loop_proxy(), |
| 295 task_runner_)); | 204 task_runner_)); |
| 296 | 205 |
| 297 gcm_app_handler_.reset(new FakeGCMAppHandler); | 206 gcm_app_handler_.reset(new FakeGCMAppHandler); |
| 207 } |
| 208 |
| 209 void GCMDriverTest::AddAppHandlers() { |
| 298 driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get()); | 210 driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get()); |
| 299 driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get()); | 211 driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get()); |
| 300 } | 212 } |
| 301 | 213 |
| 214 void GCMDriverTest::RemoveAppHandlers() { |
| 215 driver_->RemoveAppHandler(kTestAppID1); |
| 216 driver_->RemoveAppHandler(kTestAppID2); |
| 217 } |
| 218 |
| 302 void GCMDriverTest::SignIn(const std::string& account_id) { | 219 void GCMDriverTest::SignIn(const std::string& account_id) { |
| 303 token_service_.AddAccount(account_id); | 220 token_service_.AddAccount(account_id); |
| 304 identity_provider_->LogIn(account_id); | 221 identity_provider_->LogIn(account_id); |
| 305 PumpIOLoop(); | 222 PumpIOLoop(); |
| 306 PumpUILoop(); | 223 PumpUILoop(); |
| 307 } | 224 } |
| 308 | 225 |
| 309 void GCMDriverTest::SignOut() { | 226 void GCMDriverTest::SignOut() { |
| 310 identity_provider_->LogOut(); | 227 identity_provider_->LogOut(); |
| 311 PumpIOLoop(); | 228 PumpIOLoop(); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 async_operation_completed_callback_.Run(); | 290 async_operation_completed_callback_.Run(); |
| 374 } | 291 } |
| 375 | 292 |
| 376 void GCMDriverTest::UnregisterCompleted(GCMClient::Result result) { | 293 void GCMDriverTest::UnregisterCompleted(GCMClient::Result result) { |
| 377 unregistration_result_ = result; | 294 unregistration_result_ = result; |
| 378 if (!async_operation_completed_callback_.is_null()) | 295 if (!async_operation_completed_callback_.is_null()) |
| 379 async_operation_completed_callback_.Run(); | 296 async_operation_completed_callback_.Run(); |
| 380 } | 297 } |
| 381 | 298 |
| 382 TEST_F(GCMDriverTest, CreateGCMDriverBeforeSignIn) { | 299 TEST_F(GCMDriverTest, CreateGCMDriverBeforeSignIn) { |
| 383 // Create CreateGMCService first. | 300 // Create GCMDriver first. GCM is not started. |
| 384 CreateDriver(FakeGCMClient::NO_DELAY_START); | 301 CreateDriver(FakeGCMClient::NO_DELAY_START); |
| 385 EXPECT_FALSE(driver()->IsStarted()); | 302 EXPECT_FALSE(driver()->IsStarted()); |
| 386 | 303 |
| 387 // Sign in. This will kick off the check-in. | 304 // Sign in. GCM is still not started. |
| 388 SignIn(kTestAccountID1); | 305 SignIn(kTestAccountID1); |
| 306 EXPECT_FALSE(driver()->IsStarted()); |
| 307 |
| 308 // GCM will be started only after both sign-in and app handler being |
| 309 AddAppHandlers(); |
| 389 EXPECT_TRUE(driver()->IsStarted()); | 310 EXPECT_TRUE(driver()->IsStarted()); |
| 390 } | 311 } |
| 391 | 312 |
| 392 TEST_F(GCMDriverTest, CreateGCMDriverAfterSignIn) { | 313 TEST_F(GCMDriverTest, CreateGCMDriverAfterSignIn) { |
| 393 // Sign in. This will not initiate the check-in. | 314 // Sign in. Nothings happens since GCMDriver is not created. |
| 394 SignIn(kTestAccountID1); | 315 SignIn(kTestAccountID1); |
| 395 | 316 |
| 396 // Create GCMeService after sign-in. | 317 // Create GCMDriver after sign-in. GCM is not started. |
| 397 CreateDriver(FakeGCMClient::NO_DELAY_START); | 318 CreateDriver(FakeGCMClient::NO_DELAY_START); |
| 319 EXPECT_FALSE(driver()->IsStarted()); |
| 320 |
| 321 // GCM will be started only after both sign-in and app handler being |
| 322 AddAppHandlers(); |
| 398 EXPECT_TRUE(driver()->IsStarted()); | 323 EXPECT_TRUE(driver()->IsStarted()); |
| 399 } | 324 } |
| 400 | 325 |
| 401 TEST_F(GCMDriverTest, Shutdown) { | 326 TEST_F(GCMDriverTest, Shutdown) { |
| 402 CreateDriver(FakeGCMClient::NO_DELAY_START); | 327 CreateDriver(FakeGCMClient::NO_DELAY_START); |
| 328 EXPECT_FALSE(HasAppHandlers()); |
| 329 |
| 330 AddAppHandlers(); |
| 403 EXPECT_TRUE(HasAppHandlers()); | 331 EXPECT_TRUE(HasAppHandlers()); |
| 404 | 332 |
| 405 driver()->Shutdown(); | 333 driver()->Shutdown(); |
| 406 EXPECT_FALSE(HasAppHandlers()); | 334 EXPECT_FALSE(HasAppHandlers()); |
| 407 } | 335 } |
| 408 | 336 |
| 409 TEST_F(GCMDriverTest, SignInAndSignOutUnderPositiveChannelSignal) { | 337 TEST_F(GCMDriverTest, SignInAndSignOutOnGCMEnabled) { |
| 338 // By default, GCM is enabled. |
| 410 CreateDriver(FakeGCMClient::NO_DELAY_START); | 339 CreateDriver(FakeGCMClient::NO_DELAY_START); |
| 340 AddAppHandlers(); |
| 341 |
| 342 // GCMClient should be started after sign-in. |
| 411 SignIn(kTestAccountID1); | 343 SignIn(kTestAccountID1); |
| 412 | |
| 413 // GCMClient should be loaded. | |
| 414 EXPECT_TRUE(driver()->IsGCMClientReady()); | 344 EXPECT_TRUE(driver()->IsGCMClientReady()); |
| 415 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); | 345 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); |
| 416 | 346 |
| 347 // GCMClient should be checked out after sign-out. |
| 417 SignOut(); | 348 SignOut(); |
| 349 EXPECT_FALSE(driver()->IsGCMClientReady()); |
| 350 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); |
| 351 } |
| 418 | 352 |
| 419 // GCMClient should be checked out. | 353 TEST_F(GCMDriverTest, SignInAndSignOutOnGCMDisabled) { |
| 354 // By default, GCM is enabled. |
| 355 CreateDriver(FakeGCMClient::NO_DELAY_START); |
| 356 AddAppHandlers(); |
| 357 |
| 358 // Disable GCM. |
| 359 driver()->Disable(); |
| 360 |
| 361 // GCMClient should not be started after sign-in. |
| 362 SignIn(kTestAccountID1); |
| 363 EXPECT_FALSE(driver()->IsGCMClientReady()); |
| 364 EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status()); |
| 365 |
| 366 // Check-out should still be performed after sign-out. |
| 367 SignOut(); |
| 420 EXPECT_FALSE(driver()->IsGCMClientReady()); | 368 EXPECT_FALSE(driver()->IsGCMClientReady()); |
| 421 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); | 369 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); |
| 422 } | 370 } |
| 423 | 371 |
| 424 TEST_F(GCMDriverTest, SignOutAndThenSignIn) { | 372 TEST_F(GCMDriverTest, SignOutAndThenSignIn) { |
| 425 CreateDriver(FakeGCMClient::NO_DELAY_START); | 373 CreateDriver(FakeGCMClient::NO_DELAY_START); |
| 374 AddAppHandlers(); |
| 375 |
| 376 // GCMClient should be started after sign-in. |
| 426 SignIn(kTestAccountID1); | 377 SignIn(kTestAccountID1); |
| 427 | |
| 428 // GCMClient should be loaded. | |
| 429 EXPECT_TRUE(driver()->IsGCMClientReady()); | 378 EXPECT_TRUE(driver()->IsGCMClientReady()); |
| 430 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); | 379 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); |
| 431 | 380 |
| 381 // GCMClient should be checked out after sign-out. |
| 432 SignOut(); | 382 SignOut(); |
| 433 | |
| 434 // GCMClient should be checked out. | |
| 435 EXPECT_FALSE(driver()->IsGCMClientReady()); | 383 EXPECT_FALSE(driver()->IsGCMClientReady()); |
| 436 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); | 384 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); |
| 437 | 385 |
| 438 // Sign-in with a different account. | 386 // Sign-in with a different account. |
| 439 SignIn(kTestAccountID2); | 387 SignIn(kTestAccountID2); |
| 440 | 388 |
| 441 // GCMClient should be loaded again. | 389 // GCMClient should be started again. |
| 442 EXPECT_TRUE(driver()->IsGCMClientReady()); | 390 EXPECT_TRUE(driver()->IsGCMClientReady()); |
| 443 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); | 391 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); |
| 444 } | 392 } |
| 445 | 393 |
| 446 TEST_F(GCMDriverTest, DisableAndReenableGCM) { | 394 TEST_F(GCMDriverTest, DisableAndReenableGCM) { |
| 447 CreateDriver(FakeGCMClient::NO_DELAY_START); | 395 CreateDriver(FakeGCMClient::NO_DELAY_START); |
| 396 AddAppHandlers(); |
| 448 SignIn(kTestAccountID1); | 397 SignIn(kTestAccountID1); |
| 449 | 398 |
| 450 // GCMClient should be started. | 399 // GCMClient should be started. |
| 451 EXPECT_TRUE(driver()->IsGCMClientReady()); | 400 EXPECT_TRUE(driver()->IsGCMClientReady()); |
| 452 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); | 401 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); |
| 453 | 402 |
| 454 // Disables the GCM. | 403 // Disables the GCM. |
| 455 driver()->Disable(); | 404 driver()->Disable(); |
| 456 PumpIOLoop(); | 405 PumpIOLoop(); |
| 457 PumpUILoop(); | 406 PumpUILoop(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 479 EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status()); | 428 EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status()); |
| 480 | 429 |
| 481 // Sign out. | 430 // Sign out. |
| 482 SignOut(); | 431 SignOut(); |
| 483 | 432 |
| 484 // GCMClient should be checked out. | 433 // GCMClient should be checked out. |
| 485 EXPECT_FALSE(driver()->IsGCMClientReady()); | 434 EXPECT_FALSE(driver()->IsGCMClientReady()); |
| 486 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); | 435 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); |
| 487 } | 436 } |
| 488 | 437 |
| 438 TEST_F(GCMDriverTest, StartOrStopGCMOnDemand) { |
| 439 CreateDriver(FakeGCMClient::NO_DELAY_START); |
| 440 SignIn(kTestAccountID1); |
| 441 |
| 442 // GCMClient is not started. |
| 443 EXPECT_FALSE(driver()->IsGCMClientReady()); |
| 444 EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status()); |
| 445 |
| 446 // GCMClient is started after an app handler has been added. |
| 447 driver()->AddAppHandler(kTestAppID1, gcm_app_handler()); |
| 448 PumpIOLoop(); |
| 449 PumpUILoop(); |
| 450 EXPECT_TRUE(driver()->IsGCMClientReady()); |
| 451 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); |
| 452 |
| 453 // Add another app handler. |
| 454 driver()->AddAppHandler(kTestAppID2, gcm_app_handler()); |
| 455 PumpIOLoop(); |
| 456 PumpUILoop(); |
| 457 EXPECT_TRUE(driver()->IsGCMClientReady()); |
| 458 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); |
| 459 |
| 460 // GCMClient remains active after one app handler is gone. |
| 461 driver()->RemoveAppHandler(kTestAppID1); |
| 462 PumpIOLoop(); |
| 463 PumpUILoop(); |
| 464 EXPECT_TRUE(driver()->IsGCMClientReady()); |
| 465 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); |
| 466 |
| 467 // GCMClient should be stopped after the last app handler is gone. |
| 468 driver()->RemoveAppHandler(kTestAppID2); |
| 469 PumpIOLoop(); |
| 470 PumpUILoop(); |
| 471 EXPECT_FALSE(driver()->IsGCMClientReady()); |
| 472 EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status()); |
| 473 |
| 474 // GCMClient is restarted after an app handler has been added. |
| 475 driver()->AddAppHandler(kTestAppID2, gcm_app_handler()); |
| 476 PumpIOLoop(); |
| 477 PumpUILoop(); |
| 478 EXPECT_TRUE(driver()->IsGCMClientReady()); |
| 479 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); |
| 480 } |
| 481 |
| 489 TEST_F(GCMDriverTest, RegisterFailed) { | 482 TEST_F(GCMDriverTest, RegisterFailed) { |
| 490 std::vector<std::string> sender_ids; | 483 std::vector<std::string> sender_ids; |
| 491 sender_ids.push_back("sender1"); | 484 sender_ids.push_back("sender1"); |
| 492 | 485 |
| 493 CreateDriver(FakeGCMClient::NO_DELAY_START); | 486 CreateDriver(FakeGCMClient::NO_DELAY_START); |
| 494 | 487 |
| 495 // Registration fails when GCM is disabled. | 488 // Registration fails when GCM is disabled. |
| 496 driver()->Disable(); | 489 driver()->Disable(); |
| 497 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | 490 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); |
| 498 EXPECT_TRUE(registration_id().empty()); | 491 EXPECT_TRUE(registration_id().empty()); |
| 499 EXPECT_EQ(GCMClient::GCM_DISABLED, registration_result()); | 492 EXPECT_EQ(GCMClient::GCM_DISABLED, registration_result()); |
| 500 | 493 |
| 501 ClearResults(); | 494 ClearResults(); |
| 502 | 495 |
| 503 // Registration fails when the sign-in does not occur. | 496 // Registration fails when the sign-in does not occur. |
| 504 driver()->Enable(); | 497 driver()->Enable(); |
| 498 AddAppHandlers(); |
| 505 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | 499 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); |
| 506 EXPECT_TRUE(registration_id().empty()); | 500 EXPECT_TRUE(registration_id().empty()); |
| 507 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, registration_result()); | 501 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, registration_result()); |
| 502 |
| 503 ClearResults(); |
| 504 |
| 505 // Registration fails when the no app handler is added. |
| 506 RemoveAppHandlers(); |
| 507 SignIn(kTestAccountID1); |
| 508 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); |
| 509 EXPECT_TRUE(registration_id().empty()); |
| 510 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result()); |
| 508 } | 511 } |
| 509 | 512 |
| 510 TEST_F(GCMDriverTest, UnregisterFailed) { | 513 TEST_F(GCMDriverTest, UnregisterFailed) { |
| 511 CreateDriver(FakeGCMClient::NO_DELAY_START); | 514 CreateDriver(FakeGCMClient::NO_DELAY_START); |
| 512 | 515 |
| 513 // Unregistration fails when GCM is disabled. | 516 // Unregistration fails when GCM is disabled. |
| 514 driver()->Disable(); | 517 driver()->Disable(); |
| 515 Unregister(kTestAppID1, GCMDriverTest::WAIT); | 518 Unregister(kTestAppID1, GCMDriverTest::WAIT); |
| 516 EXPECT_EQ(GCMClient::GCM_DISABLED, unregistration_result()); | 519 EXPECT_EQ(GCMClient::GCM_DISABLED, unregistration_result()); |
| 517 | 520 |
| 518 ClearResults(); | 521 ClearResults(); |
| 519 | 522 |
| 520 // Unregistration fails when the sign-in does not occur. | 523 // Unregistration fails when the sign-in does not occur. |
| 521 driver()->Enable(); | 524 driver()->Enable(); |
| 525 AddAppHandlers(); |
| 522 Unregister(kTestAppID1, GCMDriverTest::WAIT); | 526 Unregister(kTestAppID1, GCMDriverTest::WAIT); |
| 523 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, unregistration_result()); | 527 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, unregistration_result()); |
| 528 |
| 529 ClearResults(); |
| 530 |
| 531 // Unregistration fails when the no app handler is added. |
| 532 RemoveAppHandlers(); |
| 533 SignIn(kTestAccountID1); |
| 534 Unregister(kTestAppID1, GCMDriverTest::WAIT); |
| 535 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, unregistration_result()); |
| 524 } | 536 } |
| 525 | 537 |
| 526 TEST_F(GCMDriverTest, SendFailed) { | 538 TEST_F(GCMDriverTest, SendFailed) { |
| 527 GCMClient::OutgoingMessage message; | 539 GCMClient::OutgoingMessage message; |
| 528 message.id = "1"; | 540 message.id = "1"; |
| 529 message.data["key1"] = "value1"; | 541 message.data["key1"] = "value1"; |
| 530 | 542 |
| 531 CreateDriver(FakeGCMClient::NO_DELAY_START); | 543 CreateDriver(FakeGCMClient::NO_DELAY_START); |
| 532 | 544 |
| 533 // Sending fails when GCM is disabled. | 545 // Sending fails when GCM is disabled. |
| 534 driver()->Disable(); | 546 driver()->Disable(); |
| 535 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); | 547 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); |
| 536 EXPECT_TRUE(send_message_id().empty()); | 548 EXPECT_TRUE(send_message_id().empty()); |
| 537 EXPECT_EQ(GCMClient::GCM_DISABLED, send_result()); | 549 EXPECT_EQ(GCMClient::GCM_DISABLED, send_result()); |
| 538 | 550 |
| 539 ClearResults(); | 551 ClearResults(); |
| 540 | 552 |
| 541 // Registration fails when the sign-in does not occur. | 553 // Sending fails when the sign-in does not occur. |
| 542 driver()->Enable(); | 554 driver()->Enable(); |
| 555 AddAppHandlers(); |
| 543 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); | 556 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); |
| 544 EXPECT_TRUE(send_message_id().empty()); | 557 EXPECT_TRUE(send_message_id().empty()); |
| 545 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, send_result()); | 558 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, send_result()); |
| 559 |
| 560 ClearResults(); |
| 561 |
| 562 // Sending fails when the no app handler is added. |
| 563 RemoveAppHandlers(); |
| 564 SignIn(kTestAccountID1); |
| 565 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); |
| 566 EXPECT_TRUE(send_message_id().empty()); |
| 567 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, send_result()); |
| 546 } | 568 } |
| 547 | 569 |
| 548 TEST_F(GCMDriverTest, GCMClientNotReadyBeforeRegistration) { | 570 TEST_F(GCMDriverTest, GCMClientNotReadyBeforeRegistration) { |
| 549 // Make GCMClient not ready initially. | 571 // Make GCMClient not ready initially. |
| 550 CreateDriver(FakeGCMClient::DELAY_START); | 572 CreateDriver(FakeGCMClient::DELAY_START); |
| 551 SignIn(kTestAccountID1); | 573 SignIn(kTestAccountID1); |
| 574 AddAppHandlers(); |
| 552 | 575 |
| 553 // The registration is on hold until GCMClient is ready. | 576 // The registration is on hold until GCMClient is ready. |
| 554 std::vector<std::string> sender_ids; | 577 std::vector<std::string> sender_ids; |
| 555 sender_ids.push_back("sender1"); | 578 sender_ids.push_back("sender1"); |
| 556 Register(kTestAppID1, | 579 Register(kTestAppID1, |
| 557 sender_ids, | 580 sender_ids, |
| 558 GCMDriverTest::DO_NOT_WAIT); | 581 GCMDriverTest::DO_NOT_WAIT); |
| 559 PumpIOLoop(); | 582 PumpIOLoop(); |
| 560 PumpUILoop(); | 583 PumpUILoop(); |
| 561 EXPECT_TRUE(registration_id().empty()); | 584 EXPECT_TRUE(registration_id().empty()); |
| 562 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result()); | 585 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result()); |
| 563 | 586 |
| 564 // Register operation will be invoked after GCMClient becomes ready. | 587 // Register operation will be invoked after GCMClient becomes ready. |
| 565 GetGCMClient()->PerformDelayedLoading(); | 588 GetGCMClient()->PerformDelayedLoading(); |
| 566 WaitForAsyncOperation(); | 589 WaitForAsyncOperation(); |
| 567 EXPECT_FALSE(registration_id().empty()); | 590 EXPECT_FALSE(registration_id().empty()); |
| 568 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | 591 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); |
| 569 } | 592 } |
| 570 | 593 |
| 571 TEST_F(GCMDriverTest, GCMClientNotReadyBeforeSending) { | 594 TEST_F(GCMDriverTest, GCMClientNotReadyBeforeSending) { |
| 572 // Make GCMClient not ready initially. | 595 // Make GCMClient not ready initially. |
| 573 CreateDriver(FakeGCMClient::DELAY_START); | 596 CreateDriver(FakeGCMClient::DELAY_START); |
| 574 SignIn(kTestAccountID1); | 597 SignIn(kTestAccountID1); |
| 598 AddAppHandlers(); |
| 575 | 599 |
| 576 // The sending is on hold until GCMClient is ready. | 600 // The sending is on hold until GCMClient is ready. |
| 577 GCMClient::OutgoingMessage message; | 601 GCMClient::OutgoingMessage message; |
| 578 message.id = "1"; | 602 message.id = "1"; |
| 579 message.data["key1"] = "value1"; | 603 message.data["key1"] = "value1"; |
| 580 message.data["key2"] = "value2"; | 604 message.data["key2"] = "value2"; |
| 581 Send(kTestAppID1, kUserID1, message, GCMDriverTest::DO_NOT_WAIT); | 605 Send(kTestAppID1, kUserID1, message, GCMDriverTest::DO_NOT_WAIT); |
| 582 PumpIOLoop(); | 606 PumpIOLoop(); |
| 583 PumpUILoop(); | 607 PumpUILoop(); |
| 584 | 608 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 608 GCMDriverFunctionalTest::GCMDriverFunctionalTest() { | 632 GCMDriverFunctionalTest::GCMDriverFunctionalTest() { |
| 609 } | 633 } |
| 610 | 634 |
| 611 GCMDriverFunctionalTest::~GCMDriverFunctionalTest() { | 635 GCMDriverFunctionalTest::~GCMDriverFunctionalTest() { |
| 612 } | 636 } |
| 613 | 637 |
| 614 void GCMDriverFunctionalTest::SetUp() { | 638 void GCMDriverFunctionalTest::SetUp() { |
| 615 GCMDriverTest::SetUp(); | 639 GCMDriverTest::SetUp(); |
| 616 | 640 |
| 617 CreateDriver(FakeGCMClient::NO_DELAY_START); | 641 CreateDriver(FakeGCMClient::NO_DELAY_START); |
| 642 AddAppHandlers(); |
| 618 SignIn(kTestAccountID1); | 643 SignIn(kTestAccountID1); |
| 619 } | 644 } |
| 620 | 645 |
| 621 TEST_F(GCMDriverFunctionalTest, Register) { | 646 TEST_F(GCMDriverFunctionalTest, Register) { |
| 622 std::vector<std::string> sender_ids; | 647 std::vector<std::string> sender_ids; |
| 623 sender_ids.push_back("sender1"); | 648 sender_ids.push_back("sender1"); |
| 624 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | 649 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); |
| 625 const std::string expected_registration_id = | 650 const std::string expected_registration_id = |
| 626 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids); | 651 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids); |
| 627 | 652 |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 866 | 891 |
| 867 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) { | 892 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) { |
| 868 GetGCMClient()->DeleteMessages(kTestAppID1); | 893 GetGCMClient()->DeleteMessages(kTestAppID1); |
| 869 gcm_app_handler()->WaitForNotification(); | 894 gcm_app_handler()->WaitForNotification(); |
| 870 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, | 895 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, |
| 871 gcm_app_handler()->received_event()); | 896 gcm_app_handler()->received_event()); |
| 872 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); | 897 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); |
| 873 } | 898 } |
| 874 | 899 |
| 875 } // namespace gcm | 900 } // namespace gcm |
| OLD | NEW |