| 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 "chrome/browser/services/gcm/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 "chrome/browser/services/gcm/fake_gcm_app_handler.h" | |
| 18 #include "chrome/browser/services/gcm/fake_gcm_client.h" | |
| 19 #include "chrome/browser/services/gcm/fake_gcm_client_factory.h" | |
| 20 #include "chrome/browser/services/gcm/gcm_app_handler.h" | |
| 21 #include "components/gcm_driver/gcm_client_factory.h" | |
| 22 #include "content/public/browser/browser_thread.h" | |
| 23 #include "google_apis/gaia/fake_identity_provider.h" | |
| 24 #include "google_apis/gaia/fake_oauth2_token_service.h" | |
| 25 #include "net/url_request/url_request_context_getter.h" | |
| 26 #include "net/url_request/url_request_test_util.h" | |
| 27 #include "testing/gtest/include/gtest/gtest.h" | |
| 28 | |
| 29 namespace gcm { | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 const char kTestAccountID1[] = "user1@example.com"; | |
| 34 const char kTestAccountID2[] = "user2@example.com"; | |
| 35 const char kTestAppID1[] = "TestApp1"; | |
| 36 const char kTestAppID2[] = "TestApp2"; | |
| 37 const char kUserID1[] = "user1"; | |
| 38 | |
| 39 void PumpCurrentLoop() { | |
| 40 base::MessageLoop::ScopedNestableTaskAllower | |
| 41 nestable_task_allower(base::MessageLoop::current()); | |
| 42 base::RunLoop().RunUntilIdle(); | |
| 43 } | |
| 44 | |
| 45 void PumpUILoop() { | |
| 46 PumpCurrentLoop(); | |
| 47 } | |
| 48 | |
| 49 std::vector<std::string> ToSenderList(const std::string& sender_ids) { | |
| 50 std::vector<std::string> senders; | |
| 51 Tokenize(sender_ids, ",", &senders); | |
| 52 return senders; | |
| 53 } | |
| 54 | |
| 55 } // namespace | |
| 56 | |
| 57 class GCMDriverTest : public testing::Test { | |
| 58 public: | |
| 59 enum WaitToFinish { | |
| 60 DO_NOT_WAIT, | |
| 61 WAIT | |
| 62 }; | |
| 63 | |
| 64 GCMDriverTest(); | |
| 65 virtual ~GCMDriverTest(); | |
| 66 | |
| 67 // testing::Test: | |
| 68 virtual void SetUp() OVERRIDE; | |
| 69 virtual void TearDown() OVERRIDE; | |
| 70 | |
| 71 GCMDriver* driver() { return driver_.get(); } | |
| 72 FakeGCMAppHandler* gcm_app_handler() { return gcm_app_handler_.get(); } | |
| 73 const std::string& registration_id() const { return registration_id_; } | |
| 74 GCMClient::Result registration_result() const { return registration_result_; } | |
| 75 const std::string& send_message_id() const { return send_message_id_; } | |
| 76 GCMClient::Result send_result() const { return send_result_; } | |
| 77 GCMClient::Result unregistration_result() const { | |
| 78 return unregistration_result_; | |
| 79 } | |
| 80 | |
| 81 void PumpIOLoop(); | |
| 82 | |
| 83 void ClearResults(); | |
| 84 | |
| 85 bool HasAppHandlers() const; | |
| 86 FakeGCMClient* GetGCMClient(); | |
| 87 | |
| 88 void CreateDriver(FakeGCMClient::StartMode gcm_client_start_mode); | |
| 89 void AddAppHandlers(); | |
| 90 void RemoveAppHandlers(); | |
| 91 | |
| 92 void SignIn(const std::string& account_id); | |
| 93 void SignOut(); | |
| 94 | |
| 95 void Register(const std::string& app_id, | |
| 96 const std::vector<std::string>& sender_ids, | |
| 97 WaitToFinish wait_to_finish); | |
| 98 void Send(const std::string& app_id, | |
| 99 const std::string& receiver_id, | |
| 100 const GCMClient::OutgoingMessage& message, | |
| 101 WaitToFinish wait_to_finish); | |
| 102 void Unregister(const std::string& app_id, WaitToFinish wait_to_finish); | |
| 103 | |
| 104 void WaitForAsyncOperation(); | |
| 105 | |
| 106 private: | |
| 107 void RegisterCompleted(const std::string& registration_id, | |
| 108 GCMClient::Result result); | |
| 109 void SendCompleted(const std::string& message_id, GCMClient::Result result); | |
| 110 void UnregisterCompleted(GCMClient::Result result); | |
| 111 | |
| 112 base::ScopedTempDir temp_dir_; | |
| 113 FakeOAuth2TokenService token_service_; | |
| 114 scoped_ptr<FakeIdentityProvider> identity_provider_owner_; | |
| 115 FakeIdentityProvider* identity_provider_; | |
| 116 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
| 117 base::MessageLoopForUI message_loop_; | |
| 118 base::Thread io_thread_; | |
| 119 scoped_ptr<GCMDriver> driver_; | |
| 120 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; | |
| 121 | |
| 122 base::Closure async_operation_completed_callback_; | |
| 123 | |
| 124 std::string registration_id_; | |
| 125 GCMClient::Result registration_result_; | |
| 126 std::string send_message_id_; | |
| 127 GCMClient::Result send_result_; | |
| 128 GCMClient::Result unregistration_result_; | |
| 129 | |
| 130 DISALLOW_COPY_AND_ASSIGN(GCMDriverTest); | |
| 131 }; | |
| 132 | |
| 133 GCMDriverTest::GCMDriverTest() | |
| 134 : identity_provider_(NULL), | |
| 135 task_runner_(new base::TestSimpleTaskRunner()), | |
| 136 io_thread_("IOThread"), | |
| 137 registration_result_(GCMClient::UNKNOWN_ERROR), | |
| 138 send_result_(GCMClient::UNKNOWN_ERROR), | |
| 139 unregistration_result_(GCMClient::UNKNOWN_ERROR) { | |
| 140 identity_provider_owner_.reset(new FakeIdentityProvider(&token_service_)); | |
| 141 identity_provider_ = identity_provider_owner_.get(); | |
| 142 } | |
| 143 | |
| 144 GCMDriverTest::~GCMDriverTest() { | |
| 145 } | |
| 146 | |
| 147 void GCMDriverTest::SetUp() { | |
| 148 io_thread_.Start(); | |
| 149 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 150 } | |
| 151 | |
| 152 void GCMDriverTest::TearDown() { | |
| 153 if (!driver_) | |
| 154 return; | |
| 155 | |
| 156 driver_->Shutdown(); | |
| 157 driver_.reset(); | |
| 158 PumpIOLoop(); | |
| 159 | |
| 160 io_thread_.Stop(); | |
| 161 } | |
| 162 | |
| 163 void GCMDriverTest::PumpIOLoop() { | |
| 164 base::RunLoop run_loop; | |
| 165 io_thread_.message_loop_proxy()->PostTaskAndReply( | |
| 166 FROM_HERE, | |
| 167 base::Bind(&PumpCurrentLoop), | |
| 168 run_loop.QuitClosure()); | |
| 169 run_loop.Run(); | |
| 170 } | |
| 171 | |
| 172 void GCMDriverTest::ClearResults() { | |
| 173 registration_id_.clear(); | |
| 174 registration_result_ = GCMClient::UNKNOWN_ERROR; | |
| 175 | |
| 176 send_message_id_.clear(); | |
| 177 send_result_ = GCMClient::UNKNOWN_ERROR; | |
| 178 | |
| 179 unregistration_result_ = GCMClient::UNKNOWN_ERROR; | |
| 180 } | |
| 181 | |
| 182 bool GCMDriverTest::HasAppHandlers() const { | |
| 183 return !driver_->app_handlers().empty(); | |
| 184 } | |
| 185 | |
| 186 FakeGCMClient* GCMDriverTest::GetGCMClient() { | |
| 187 return static_cast<FakeGCMClient*>(driver_->GetGCMClientForTesting()); | |
| 188 } | |
| 189 | |
| 190 void GCMDriverTest::CreateDriver( | |
| 191 FakeGCMClient::StartMode gcm_client_start_mode) { | |
| 192 scoped_refptr<net::URLRequestContextGetter> request_context = | |
| 193 new net::TestURLRequestContextGetter(io_thread_.message_loop_proxy()); | |
| 194 driver_.reset(new GCMDriver( | |
| 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 | |
| 210 void GCMDriverTest::AddAppHandlers() { | |
| 211 driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get()); | |
| 212 driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get()); | |
| 213 } | |
| 214 | |
| 215 void GCMDriverTest::RemoveAppHandlers() { | |
| 216 driver_->RemoveAppHandler(kTestAppID1); | |
| 217 driver_->RemoveAppHandler(kTestAppID2); | |
| 218 } | |
| 219 | |
| 220 void 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 | |
| 227 void GCMDriverTest::SignOut() { | |
| 228 identity_provider_->LogOut(); | |
| 229 PumpIOLoop(); | |
| 230 PumpUILoop(); | |
| 231 } | |
| 232 | |
| 233 void 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 | |
| 246 void 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 | |
| 261 void 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 | |
| 272 void GCMDriverTest::WaitForAsyncOperation() { | |
| 273 base::RunLoop run_loop; | |
| 274 async_operation_completed_callback_ = run_loop.QuitClosure(); | |
| 275 run_loop.Run(); | |
| 276 } | |
| 277 | |
| 278 void 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 | |
| 286 void 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 | |
| 294 void 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 | |
| 300 TEST_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 | |
| 314 TEST_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 | |
| 327 TEST_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 | |
| 338 TEST_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 | |
| 354 TEST_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 | |
| 373 TEST_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 | |
| 395 TEST_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 | |
| 439 TEST_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 | |
| 483 TEST_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 | |
| 514 TEST_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 | |
| 539 TEST_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 | |
| 571 TEST_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 | |
| 595 TEST_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. | |
| 621 class 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 | |
| 633 GCMDriverFunctionalTest::GCMDriverFunctionalTest() { | |
| 634 } | |
| 635 | |
| 636 GCMDriverFunctionalTest::~GCMDriverFunctionalTest() { | |
| 637 } | |
| 638 | |
| 639 void GCMDriverFunctionalTest::SetUp() { | |
| 640 GCMDriverTest::SetUp(); | |
| 641 | |
| 642 CreateDriver(FakeGCMClient::NO_DELAY_START); | |
| 643 AddAppHandlers(); | |
| 644 SignIn(kTestAccountID1); | |
| 645 } | |
| 646 | |
| 647 TEST_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 | |
| 658 TEST_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 | |
| 667 TEST_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 | |
| 693 TEST_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 | |
| 715 TEST_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 | |
| 727 TEST_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 | |
| 740 TEST_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 | |
| 774 TEST_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 | |
| 812 TEST_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 | |
| 823 TEST_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 | |
| 837 TEST_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 | |
| 861 TEST_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 | |
| 877 TEST_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 | |
| 893 TEST_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 | |
| OLD | NEW |