| OLD | NEW |
| (Empty) |
| 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.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 | |
| 28 namespace gcm { | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 const char kTestAccountID1[] = "user1@example.com"; | |
| 33 const char kTestAccountID2[] = "user2@example.com"; | |
| 34 const char kTestAppID1[] = "TestApp1"; | |
| 35 const char kTestAppID2[] = "TestApp2"; | |
| 36 const char kUserID1[] = "user1"; | |
| 37 | |
| 38 void PumpCurrentLoop() { | |
| 39 base::MessageLoop::ScopedNestableTaskAllower | |
| 40 nestable_task_allower(base::MessageLoop::current()); | |
| 41 base::RunLoop().RunUntilIdle(); | |
| 42 } | |
| 43 | |
| 44 void PumpUILoop() { | |
| 45 PumpCurrentLoop(); | |
| 46 } | |
| 47 | |
| 48 std::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 | |
| 56 class 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 | |
| 132 GCMDriverTest::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 | |
| 143 GCMDriverTest::~GCMDriverTest() { | |
| 144 } | |
| 145 | |
| 146 void GCMDriverTest::SetUp() { | |
| 147 io_thread_.Start(); | |
| 148 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 149 } | |
| 150 | |
| 151 void GCMDriverTest::TearDown() { | |
| 152 if (!driver_) | |
| 153 return; | |
| 154 | |
| 155 driver_->Shutdown(); | |
| 156 driver_.reset(); | |
| 157 PumpIOLoop(); | |
| 158 | |
| 159 io_thread_.Stop(); | |
| 160 } | |
| 161 | |
| 162 void 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 | |
| 171 void 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 | |
| 181 bool GCMDriverTest::HasAppHandlers() const { | |
| 182 return !driver_->app_handlers().empty(); | |
| 183 } | |
| 184 | |
| 185 FakeGCMClient* GCMDriverTest::GetGCMClient() { | |
| 186 return static_cast<FakeGCMClient*>(driver_->GetGCMClientForTesting()); | |
| 187 } | |
| 188 | |
| 189 void 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 driver_.reset(new GCMDriver( | |
| 194 scoped_ptr<GCMClientFactory>(new FakeGCMClientFactory( | |
| 195 gcm_client_start_mode, | |
| 196 base::MessageLoopProxy::current(), | |
| 197 io_thread_.message_loop_proxy())).Pass(), | |
| 198 identity_provider_owner_.PassAs<IdentityProvider>(), | |
| 199 GCMClient::ChromeBuildInfo(), | |
| 200 temp_dir_.path(), | |
| 201 request_context, | |
| 202 base::MessageLoopProxy::current(), | |
| 203 io_thread_.message_loop_proxy(), | |
| 204 task_runner_)); | |
| 205 | |
| 206 gcm_app_handler_.reset(new FakeGCMAppHandler); | |
| 207 } | |
| 208 | |
| 209 void GCMDriverTest::AddAppHandlers() { | |
| 210 driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get()); | |
| 211 driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get()); | |
| 212 } | |
| 213 | |
| 214 void GCMDriverTest::RemoveAppHandlers() { | |
| 215 driver_->RemoveAppHandler(kTestAppID1); | |
| 216 driver_->RemoveAppHandler(kTestAppID2); | |
| 217 } | |
| 218 | |
| 219 void GCMDriverTest::SignIn(const std::string& account_id) { | |
| 220 token_service_.AddAccount(account_id); | |
| 221 identity_provider_->LogIn(account_id); | |
| 222 PumpIOLoop(); | |
| 223 PumpUILoop(); | |
| 224 } | |
| 225 | |
| 226 void GCMDriverTest::SignOut() { | |
| 227 identity_provider_->LogOut(); | |
| 228 PumpIOLoop(); | |
| 229 PumpUILoop(); | |
| 230 } | |
| 231 | |
| 232 void GCMDriverTest::Register(const std::string& app_id, | |
| 233 const std::vector<std::string>& sender_ids, | |
| 234 WaitToFinish wait_to_finish) { | |
| 235 base::RunLoop run_loop; | |
| 236 async_operation_completed_callback_ = run_loop.QuitClosure(); | |
| 237 driver_->Register(app_id, | |
| 238 sender_ids, | |
| 239 base::Bind(&GCMDriverTest::RegisterCompleted, | |
| 240 base::Unretained(this))); | |
| 241 if (wait_to_finish == WAIT) | |
| 242 run_loop.Run(); | |
| 243 } | |
| 244 | |
| 245 void GCMDriverTest::Send(const std::string& app_id, | |
| 246 const std::string& receiver_id, | |
| 247 const GCMClient::OutgoingMessage& message, | |
| 248 WaitToFinish wait_to_finish) { | |
| 249 base::RunLoop run_loop; | |
| 250 async_operation_completed_callback_ = run_loop.QuitClosure(); | |
| 251 driver_->Send(app_id, | |
| 252 receiver_id, | |
| 253 message, | |
| 254 base::Bind(&GCMDriverTest::SendCompleted, | |
| 255 base::Unretained(this))); | |
| 256 if (wait_to_finish == WAIT) | |
| 257 run_loop.Run(); | |
| 258 } | |
| 259 | |
| 260 void GCMDriverTest::Unregister(const std::string& app_id, | |
| 261 WaitToFinish wait_to_finish) { | |
| 262 base::RunLoop run_loop; | |
| 263 async_operation_completed_callback_ = run_loop.QuitClosure(); | |
| 264 driver_->Unregister(app_id, | |
| 265 base::Bind(&GCMDriverTest::UnregisterCompleted, | |
| 266 base::Unretained(this))); | |
| 267 if (wait_to_finish == WAIT) | |
| 268 run_loop.Run(); | |
| 269 } | |
| 270 | |
| 271 void GCMDriverTest::WaitForAsyncOperation() { | |
| 272 base::RunLoop run_loop; | |
| 273 async_operation_completed_callback_ = run_loop.QuitClosure(); | |
| 274 run_loop.Run(); | |
| 275 } | |
| 276 | |
| 277 void GCMDriverTest::RegisterCompleted(const std::string& registration_id, | |
| 278 GCMClient::Result result) { | |
| 279 registration_id_ = registration_id; | |
| 280 registration_result_ = result; | |
| 281 if (!async_operation_completed_callback_.is_null()) | |
| 282 async_operation_completed_callback_.Run(); | |
| 283 } | |
| 284 | |
| 285 void GCMDriverTest::SendCompleted(const std::string& message_id, | |
| 286 GCMClient::Result result) { | |
| 287 send_message_id_ = message_id; | |
| 288 send_result_ = result; | |
| 289 if (!async_operation_completed_callback_.is_null()) | |
| 290 async_operation_completed_callback_.Run(); | |
| 291 } | |
| 292 | |
| 293 void GCMDriverTest::UnregisterCompleted(GCMClient::Result result) { | |
| 294 unregistration_result_ = result; | |
| 295 if (!async_operation_completed_callback_.is_null()) | |
| 296 async_operation_completed_callback_.Run(); | |
| 297 } | |
| 298 | |
| 299 TEST_F(GCMDriverTest, CreateGCMDriverBeforeSignIn) { | |
| 300 // Create GCMDriver first. GCM is not started. | |
| 301 CreateDriver(FakeGCMClient::NO_DELAY_START); | |
| 302 EXPECT_FALSE(driver()->IsStarted()); | |
| 303 | |
| 304 // Sign in. GCM is still not started. | |
| 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(); | |
| 310 EXPECT_TRUE(driver()->IsStarted()); | |
| 311 } | |
| 312 | |
| 313 TEST_F(GCMDriverTest, CreateGCMDriverAfterSignIn) { | |
| 314 // Sign in. Nothings happens since GCMDriver is not created. | |
| 315 SignIn(kTestAccountID1); | |
| 316 | |
| 317 // Create GCMDriver after sign-in. GCM is not started. | |
| 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(); | |
| 323 EXPECT_TRUE(driver()->IsStarted()); | |
| 324 } | |
| 325 | |
| 326 TEST_F(GCMDriverTest, Shutdown) { | |
| 327 CreateDriver(FakeGCMClient::NO_DELAY_START); | |
| 328 EXPECT_FALSE(HasAppHandlers()); | |
| 329 | |
| 330 AddAppHandlers(); | |
| 331 EXPECT_TRUE(HasAppHandlers()); | |
| 332 | |
| 333 driver()->Shutdown(); | |
| 334 EXPECT_FALSE(HasAppHandlers()); | |
| 335 } | |
| 336 | |
| 337 TEST_F(GCMDriverTest, SignInAndSignOutOnGCMEnabled) { | |
| 338 // By default, GCM is enabled. | |
| 339 CreateDriver(FakeGCMClient::NO_DELAY_START); | |
| 340 AddAppHandlers(); | |
| 341 | |
| 342 // GCMClient should be started after sign-in. | |
| 343 SignIn(kTestAccountID1); | |
| 344 EXPECT_TRUE(driver()->IsGCMClientReady()); | |
| 345 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); | |
| 346 | |
| 347 // GCMClient should be checked out after sign-out. | |
| 348 SignOut(); | |
| 349 EXPECT_FALSE(driver()->IsGCMClientReady()); | |
| 350 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); | |
| 351 } | |
| 352 | |
| 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(); | |
| 368 EXPECT_FALSE(driver()->IsGCMClientReady()); | |
| 369 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); | |
| 370 } | |
| 371 | |
| 372 TEST_F(GCMDriverTest, SignOutAndThenSignIn) { | |
| 373 CreateDriver(FakeGCMClient::NO_DELAY_START); | |
| 374 AddAppHandlers(); | |
| 375 | |
| 376 // GCMClient should be started after sign-in. | |
| 377 SignIn(kTestAccountID1); | |
| 378 EXPECT_TRUE(driver()->IsGCMClientReady()); | |
| 379 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); | |
| 380 | |
| 381 // GCMClient should be checked out after sign-out. | |
| 382 SignOut(); | |
| 383 EXPECT_FALSE(driver()->IsGCMClientReady()); | |
| 384 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); | |
| 385 | |
| 386 // Sign-in with a different account. | |
| 387 SignIn(kTestAccountID2); | |
| 388 | |
| 389 // GCMClient should be started again. | |
| 390 EXPECT_TRUE(driver()->IsGCMClientReady()); | |
| 391 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); | |
| 392 } | |
| 393 | |
| 394 TEST_F(GCMDriverTest, DisableAndReenableGCM) { | |
| 395 CreateDriver(FakeGCMClient::NO_DELAY_START); | |
| 396 AddAppHandlers(); | |
| 397 SignIn(kTestAccountID1); | |
| 398 | |
| 399 // GCMClient should be started. | |
| 400 EXPECT_TRUE(driver()->IsGCMClientReady()); | |
| 401 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); | |
| 402 | |
| 403 // Disables the GCM. | |
| 404 driver()->Disable(); | |
| 405 PumpIOLoop(); | |
| 406 PumpUILoop(); | |
| 407 | |
| 408 // GCMClient should be stopped. | |
| 409 EXPECT_FALSE(driver()->IsGCMClientReady()); | |
| 410 EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status()); | |
| 411 | |
| 412 // Enables the GCM. | |
| 413 driver()->Enable(); | |
| 414 PumpIOLoop(); | |
| 415 PumpUILoop(); | |
| 416 | |
| 417 // GCMClient should be started. | |
| 418 EXPECT_TRUE(driver()->IsGCMClientReady()); | |
| 419 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); | |
| 420 | |
| 421 // Disables the GCM. | |
| 422 driver()->Disable(); | |
| 423 PumpIOLoop(); | |
| 424 PumpUILoop(); | |
| 425 | |
| 426 // GCMClient should be stopped. | |
| 427 EXPECT_FALSE(driver()->IsGCMClientReady()); | |
| 428 EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status()); | |
| 429 | |
| 430 // Sign out. | |
| 431 SignOut(); | |
| 432 | |
| 433 // GCMClient should be checked out. | |
| 434 EXPECT_FALSE(driver()->IsGCMClientReady()); | |
| 435 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); | |
| 436 } | |
| 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 | |
| 482 TEST_F(GCMDriverTest, RegisterFailed) { | |
| 483 std::vector<std::string> sender_ids; | |
| 484 sender_ids.push_back("sender1"); | |
| 485 | |
| 486 CreateDriver(FakeGCMClient::NO_DELAY_START); | |
| 487 | |
| 488 // Registration fails when GCM is disabled. | |
| 489 driver()->Disable(); | |
| 490 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | |
| 491 EXPECT_TRUE(registration_id().empty()); | |
| 492 EXPECT_EQ(GCMClient::GCM_DISABLED, registration_result()); | |
| 493 | |
| 494 ClearResults(); | |
| 495 | |
| 496 // Registration fails when the sign-in does not occur. | |
| 497 driver()->Enable(); | |
| 498 AddAppHandlers(); | |
| 499 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | |
| 500 EXPECT_TRUE(registration_id().empty()); | |
| 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()); | |
| 511 } | |
| 512 | |
| 513 TEST_F(GCMDriverTest, UnregisterFailed) { | |
| 514 CreateDriver(FakeGCMClient::NO_DELAY_START); | |
| 515 | |
| 516 // Unregistration fails when GCM is disabled. | |
| 517 driver()->Disable(); | |
| 518 Unregister(kTestAppID1, GCMDriverTest::WAIT); | |
| 519 EXPECT_EQ(GCMClient::GCM_DISABLED, unregistration_result()); | |
| 520 | |
| 521 ClearResults(); | |
| 522 | |
| 523 // Unregistration fails when the sign-in does not occur. | |
| 524 driver()->Enable(); | |
| 525 AddAppHandlers(); | |
| 526 Unregister(kTestAppID1, GCMDriverTest::WAIT); | |
| 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()); | |
| 536 } | |
| 537 | |
| 538 TEST_F(GCMDriverTest, SendFailed) { | |
| 539 GCMClient::OutgoingMessage message; | |
| 540 message.id = "1"; | |
| 541 message.data["key1"] = "value1"; | |
| 542 | |
| 543 CreateDriver(FakeGCMClient::NO_DELAY_START); | |
| 544 | |
| 545 // Sending fails when GCM is disabled. | |
| 546 driver()->Disable(); | |
| 547 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); | |
| 548 EXPECT_TRUE(send_message_id().empty()); | |
| 549 EXPECT_EQ(GCMClient::GCM_DISABLED, send_result()); | |
| 550 | |
| 551 ClearResults(); | |
| 552 | |
| 553 // Sending fails when the sign-in does not occur. | |
| 554 driver()->Enable(); | |
| 555 AddAppHandlers(); | |
| 556 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); | |
| 557 EXPECT_TRUE(send_message_id().empty()); | |
| 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()); | |
| 568 } | |
| 569 | |
| 570 TEST_F(GCMDriverTest, GCMClientNotReadyBeforeRegistration) { | |
| 571 // Make GCMClient not ready initially. | |
| 572 CreateDriver(FakeGCMClient::DELAY_START); | |
| 573 SignIn(kTestAccountID1); | |
| 574 AddAppHandlers(); | |
| 575 | |
| 576 // The registration is on hold until GCMClient is ready. | |
| 577 std::vector<std::string> sender_ids; | |
| 578 sender_ids.push_back("sender1"); | |
| 579 Register(kTestAppID1, | |
| 580 sender_ids, | |
| 581 GCMDriverTest::DO_NOT_WAIT); | |
| 582 PumpIOLoop(); | |
| 583 PumpUILoop(); | |
| 584 EXPECT_TRUE(registration_id().empty()); | |
| 585 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result()); | |
| 586 | |
| 587 // Register operation will be invoked after GCMClient becomes ready. | |
| 588 GetGCMClient()->PerformDelayedLoading(); | |
| 589 WaitForAsyncOperation(); | |
| 590 EXPECT_FALSE(registration_id().empty()); | |
| 591 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | |
| 592 } | |
| 593 | |
| 594 TEST_F(GCMDriverTest, GCMClientNotReadyBeforeSending) { | |
| 595 // Make GCMClient not ready initially. | |
| 596 CreateDriver(FakeGCMClient::DELAY_START); | |
| 597 SignIn(kTestAccountID1); | |
| 598 AddAppHandlers(); | |
| 599 | |
| 600 // The sending is on hold until GCMClient is ready. | |
| 601 GCMClient::OutgoingMessage message; | |
| 602 message.id = "1"; | |
| 603 message.data["key1"] = "value1"; | |
| 604 message.data["key2"] = "value2"; | |
| 605 Send(kTestAppID1, kUserID1, message, GCMDriverTest::DO_NOT_WAIT); | |
| 606 PumpIOLoop(); | |
| 607 PumpUILoop(); | |
| 608 | |
| 609 EXPECT_TRUE(send_message_id().empty()); | |
| 610 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, send_result()); | |
| 611 | |
| 612 // Send operation will be invoked after GCMClient becomes ready. | |
| 613 GetGCMClient()->PerformDelayedLoading(); | |
| 614 WaitForAsyncOperation(); | |
| 615 EXPECT_EQ(message.id, send_message_id()); | |
| 616 EXPECT_EQ(GCMClient::SUCCESS, send_result()); | |
| 617 } | |
| 618 | |
| 619 // Tests a single instance of GCMDriver. | |
| 620 class GCMDriverFunctionalTest : public GCMDriverTest { | |
| 621 public: | |
| 622 GCMDriverFunctionalTest(); | |
| 623 virtual ~GCMDriverFunctionalTest(); | |
| 624 | |
| 625 // GCMDriverTest: | |
| 626 virtual void SetUp() OVERRIDE; | |
| 627 | |
| 628 private: | |
| 629 DISALLOW_COPY_AND_ASSIGN(GCMDriverFunctionalTest); | |
| 630 }; | |
| 631 | |
| 632 GCMDriverFunctionalTest::GCMDriverFunctionalTest() { | |
| 633 } | |
| 634 | |
| 635 GCMDriverFunctionalTest::~GCMDriverFunctionalTest() { | |
| 636 } | |
| 637 | |
| 638 void GCMDriverFunctionalTest::SetUp() { | |
| 639 GCMDriverTest::SetUp(); | |
| 640 | |
| 641 CreateDriver(FakeGCMClient::NO_DELAY_START); | |
| 642 AddAppHandlers(); | |
| 643 SignIn(kTestAccountID1); | |
| 644 } | |
| 645 | |
| 646 TEST_F(GCMDriverFunctionalTest, Register) { | |
| 647 std::vector<std::string> sender_ids; | |
| 648 sender_ids.push_back("sender1"); | |
| 649 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | |
| 650 const std::string expected_registration_id = | |
| 651 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids); | |
| 652 | |
| 653 EXPECT_EQ(expected_registration_id, registration_id()); | |
| 654 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | |
| 655 } | |
| 656 | |
| 657 TEST_F(GCMDriverFunctionalTest, RegisterError) { | |
| 658 std::vector<std::string> sender_ids; | |
| 659 sender_ids.push_back("sender1@error"); | |
| 660 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | |
| 661 | |
| 662 EXPECT_TRUE(registration_id().empty()); | |
| 663 EXPECT_NE(GCMClient::SUCCESS, registration_result()); | |
| 664 } | |
| 665 | |
| 666 TEST_F(GCMDriverFunctionalTest, RegisterAgainWithSameSenderIDs) { | |
| 667 std::vector<std::string> sender_ids; | |
| 668 sender_ids.push_back("sender1"); | |
| 669 sender_ids.push_back("sender2"); | |
| 670 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | |
| 671 const std::string expected_registration_id = | |
| 672 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids); | |
| 673 | |
| 674 EXPECT_EQ(expected_registration_id, registration_id()); | |
| 675 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | |
| 676 | |
| 677 // Clears the results the would be set by the Register callback in preparation | |
| 678 // to call register 2nd time. | |
| 679 ClearResults(); | |
| 680 | |
| 681 // Calling register 2nd time with the same set of sender IDs but different | |
| 682 // ordering will get back the same registration ID. | |
| 683 std::vector<std::string> another_sender_ids; | |
| 684 another_sender_ids.push_back("sender2"); | |
| 685 another_sender_ids.push_back("sender1"); | |
| 686 Register(kTestAppID1, another_sender_ids, GCMDriverTest::WAIT); | |
| 687 | |
| 688 EXPECT_EQ(expected_registration_id, registration_id()); | |
| 689 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | |
| 690 } | |
| 691 | |
| 692 TEST_F(GCMDriverFunctionalTest, RegisterAgainWithDifferentSenderIDs) { | |
| 693 std::vector<std::string> sender_ids; | |
| 694 sender_ids.push_back("sender1"); | |
| 695 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | |
| 696 const std::string expected_registration_id = | |
| 697 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids); | |
| 698 | |
| 699 EXPECT_EQ(expected_registration_id, registration_id()); | |
| 700 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | |
| 701 | |
| 702 // Make sender IDs different. | |
| 703 sender_ids.push_back("sender2"); | |
| 704 const std::string expected_registration_id2 = | |
| 705 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids); | |
| 706 | |
| 707 // Calling register 2nd time with the different sender IDs will get back a new | |
| 708 // registration ID. | |
| 709 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | |
| 710 EXPECT_EQ(expected_registration_id2, registration_id()); | |
| 711 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | |
| 712 } | |
| 713 | |
| 714 TEST_F(GCMDriverFunctionalTest, RegisterAfterSignOut) { | |
| 715 // This will trigger check-out. | |
| 716 SignOut(); | |
| 717 | |
| 718 std::vector<std::string> sender_ids; | |
| 719 sender_ids.push_back("sender1"); | |
| 720 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | |
| 721 | |
| 722 EXPECT_TRUE(registration_id().empty()); | |
| 723 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, registration_result()); | |
| 724 } | |
| 725 | |
| 726 TEST_F(GCMDriverFunctionalTest, UnregisterExplicitly) { | |
| 727 std::vector<std::string> sender_ids; | |
| 728 sender_ids.push_back("sender1"); | |
| 729 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | |
| 730 | |
| 731 EXPECT_FALSE(registration_id().empty()); | |
| 732 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | |
| 733 | |
| 734 Unregister(kTestAppID1, GCMDriverTest::WAIT); | |
| 735 | |
| 736 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result()); | |
| 737 } | |
| 738 | |
| 739 TEST_F(GCMDriverFunctionalTest, UnregisterWhenAsyncOperationPending) { | |
| 740 std::vector<std::string> sender_ids; | |
| 741 sender_ids.push_back("sender1"); | |
| 742 // First start registration without waiting for it to complete. | |
| 743 Register(kTestAppID1, | |
| 744 sender_ids, | |
| 745 GCMDriverTest::DO_NOT_WAIT); | |
| 746 | |
| 747 // Test that unregistration fails with async operation pending when there is a | |
| 748 // registration already in progress. | |
| 749 Unregister(kTestAppID1, GCMDriverTest::WAIT); | |
| 750 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, | |
| 751 unregistration_result()); | |
| 752 | |
| 753 // Complete the unregistration. | |
| 754 WaitForAsyncOperation(); | |
| 755 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | |
| 756 | |
| 757 // Start unregistration without waiting for it to complete. This time no async | |
| 758 // operation is pending. | |
| 759 Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT); | |
| 760 | |
| 761 // Test that unregistration fails with async operation pending when there is | |
| 762 // an unregistration already in progress. | |
| 763 Unregister(kTestAppID1, GCMDriverTest::WAIT); | |
| 764 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, | |
| 765 unregistration_result()); | |
| 766 ClearResults(); | |
| 767 | |
| 768 // Complete unregistration. | |
| 769 WaitForAsyncOperation(); | |
| 770 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result()); | |
| 771 } | |
| 772 | |
| 773 TEST_F(GCMDriverFunctionalTest, RegisterWhenAsyncOperationPending) { | |
| 774 std::vector<std::string> sender_ids; | |
| 775 sender_ids.push_back("sender1"); | |
| 776 // First start registration without waiting for it to complete. | |
| 777 Register(kTestAppID1, | |
| 778 sender_ids, | |
| 779 GCMDriverTest::DO_NOT_WAIT); | |
| 780 | |
| 781 // Test that registration fails with async operation pending when there is a | |
| 782 // registration already in progress. | |
| 783 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | |
| 784 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, | |
| 785 registration_result()); | |
| 786 ClearResults(); | |
| 787 | |
| 788 // Complete the registration. | |
| 789 WaitForAsyncOperation(); | |
| 790 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | |
| 791 | |
| 792 // Start unregistration without waiting for it to complete. This time no async | |
| 793 // operation is pending. | |
| 794 Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT); | |
| 795 | |
| 796 // Test that registration fails with async operation pending when there is an | |
| 797 // unregistration already in progress. | |
| 798 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | |
| 799 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, | |
| 800 registration_result()); | |
| 801 | |
| 802 // Complete the first unregistration expecting success. | |
| 803 WaitForAsyncOperation(); | |
| 804 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result()); | |
| 805 | |
| 806 // Test that it is ok to register again after unregistration. | |
| 807 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | |
| 808 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | |
| 809 } | |
| 810 | |
| 811 TEST_F(GCMDriverFunctionalTest, Send) { | |
| 812 GCMClient::OutgoingMessage message; | |
| 813 message.id = "1"; | |
| 814 message.data["key1"] = "value1"; | |
| 815 message.data["key2"] = "value2"; | |
| 816 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); | |
| 817 | |
| 818 EXPECT_EQ(message.id, send_message_id()); | |
| 819 EXPECT_EQ(GCMClient::SUCCESS, send_result()); | |
| 820 } | |
| 821 | |
| 822 TEST_F(GCMDriverFunctionalTest, SendAfterSignOut) { | |
| 823 // This will trigger check-out. | |
| 824 SignOut(); | |
| 825 | |
| 826 GCMClient::OutgoingMessage message; | |
| 827 message.id = "1"; | |
| 828 message.data["key1"] = "value1"; | |
| 829 message.data["key2"] = "value2"; | |
| 830 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); | |
| 831 | |
| 832 EXPECT_TRUE(send_message_id().empty()); | |
| 833 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, send_result()); | |
| 834 } | |
| 835 | |
| 836 TEST_F(GCMDriverFunctionalTest, SendError) { | |
| 837 GCMClient::OutgoingMessage message; | |
| 838 // Embedding error in id will tell the mock to simulate the send error. | |
| 839 message.id = "1@error"; | |
| 840 message.data["key1"] = "value1"; | |
| 841 message.data["key2"] = "value2"; | |
| 842 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); | |
| 843 | |
| 844 EXPECT_EQ(message.id, send_message_id()); | |
| 845 EXPECT_EQ(GCMClient::SUCCESS, send_result()); | |
| 846 | |
| 847 // Wait for the send error. | |
| 848 gcm_app_handler()->WaitForNotification(); | |
| 849 EXPECT_EQ(FakeGCMAppHandler::SEND_ERROR_EVENT, | |
| 850 gcm_app_handler()->received_event()); | |
| 851 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); | |
| 852 EXPECT_EQ(message.id, | |
| 853 gcm_app_handler()->send_error_details().message_id); | |
| 854 EXPECT_NE(GCMClient::SUCCESS, | |
| 855 gcm_app_handler()->send_error_details().result); | |
| 856 EXPECT_EQ(message.data, | |
| 857 gcm_app_handler()->send_error_details().additional_data); | |
| 858 } | |
| 859 | |
| 860 TEST_F(GCMDriverFunctionalTest, MessageReceived) { | |
| 861 Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT); | |
| 862 GCMClient::IncomingMessage message; | |
| 863 message.data["key1"] = "value1"; | |
| 864 message.data["key2"] = "value2"; | |
| 865 message.sender_id = "sender"; | |
| 866 GetGCMClient()->ReceiveMessage(kTestAppID1, message); | |
| 867 gcm_app_handler()->WaitForNotification(); | |
| 868 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, | |
| 869 gcm_app_handler()->received_event()); | |
| 870 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); | |
| 871 EXPECT_EQ(message.data, gcm_app_handler()->message().data); | |
| 872 EXPECT_TRUE(gcm_app_handler()->message().collapse_key.empty()); | |
| 873 EXPECT_EQ(message.sender_id, gcm_app_handler()->message().sender_id); | |
| 874 } | |
| 875 | |
| 876 TEST_F(GCMDriverFunctionalTest, MessageWithCollapseKeyReceived) { | |
| 877 Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT); | |
| 878 GCMClient::IncomingMessage message; | |
| 879 message.data["key1"] = "value1"; | |
| 880 message.collapse_key = "collapse_key_value"; | |
| 881 message.sender_id = "sender"; | |
| 882 GetGCMClient()->ReceiveMessage(kTestAppID1, message); | |
| 883 gcm_app_handler()->WaitForNotification(); | |
| 884 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, | |
| 885 gcm_app_handler()->received_event()); | |
| 886 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); | |
| 887 EXPECT_EQ(message.data, gcm_app_handler()->message().data); | |
| 888 EXPECT_EQ(message.collapse_key, | |
| 889 gcm_app_handler()->message().collapse_key); | |
| 890 } | |
| 891 | |
| 892 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) { | |
| 893 GetGCMClient()->DeleteMessages(kTestAppID1); | |
| 894 gcm_app_handler()->WaitForNotification(); | |
| 895 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, | |
| 896 gcm_app_handler()->received_event()); | |
| 897 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); | |
| 898 } | |
| 899 | |
| 900 } // namespace gcm | |
| OLD | NEW |