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_service.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/run_loop.h" |
| 12 #include "base/strings/string_util.h" |
| 13 #include "chrome/browser/services/gcm/fake_gcm_client_factory.h" |
| 14 #include "chrome/browser/services/gcm/gcm_app_handler.h" |
| 15 #include "chrome/browser/services/gcm/gcm_client_factory.h" |
| 16 #include "chrome/browser/services/gcm/gcm_client_mock.h" |
| 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "google_apis/gaia/fake_identity_provider.h" |
| 20 #include "google_apis/gaia/fake_oauth2_token_service.h" |
| 21 #include "net/url_request/url_request_context_getter.h" |
| 22 #include "net/url_request/url_request_test_util.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 |
| 25 namespace gcm { |
| 26 |
| 27 namespace { |
| 28 |
| 29 const char kTestAccountID1[] = "user1@example.com"; |
| 30 const char kTestAccountID2[] = "user2@example.com"; |
| 31 const char kTestAccountID3[] = "user3@example.com"; |
| 32 const char kTestAppID1[] = "TestApp1"; |
| 33 const char kTestAppID2[] = "TestApp2"; |
| 34 const char kUserID1[] = "user1"; |
| 35 const char kUserID2[] = "user2"; |
| 36 |
| 37 void PumpCurrentLoop() { |
| 38 base::RunLoop().RunUntilIdle(); |
| 39 } |
| 40 |
| 41 void PumpUILoop() { |
| 42 PumpCurrentLoop(); |
| 43 } |
| 44 |
| 45 void PumpIOLoop() { |
| 46 base::RunLoop run_loop; |
| 47 content::BrowserThread::PostTaskAndReply(content::BrowserThread::IO, |
| 48 FROM_HERE, |
| 49 base::Bind(&PumpCurrentLoop), |
| 50 run_loop.QuitClosure()); |
| 51 run_loop.Run(); |
| 52 } |
| 53 |
| 54 std::vector<std::string> ToSenderList(const std::string& sender_ids) { |
| 55 std::vector<std::string> senders; |
| 56 Tokenize(sender_ids, ",", &senders); |
| 57 return senders; |
| 58 } |
| 59 |
| 60 class FakeGCMAppHandler : public GCMAppHandler { |
| 61 public: |
| 62 enum Event { |
| 63 NO_EVENT, |
| 64 MESSAGE_EVENT, |
| 65 MESSAGES_DELETED_EVENT, |
| 66 SEND_ERROR_EVENT |
| 67 }; |
| 68 |
| 69 FakeGCMAppHandler(); |
| 70 virtual ~FakeGCMAppHandler(); |
| 71 |
| 72 const Event& received_event() const { return received_event_; } |
| 73 const std::string& app_id() const { return app_id_; } |
| 74 const GCMClient::IncomingMessage& message() const { return message_; } |
| 75 const GCMClient::SendErrorDetails& send_error_details() const { |
| 76 return send_error_details_; |
| 77 } |
| 78 |
| 79 void WaitForNotification(); |
| 80 |
| 81 // GCMAppHandler: |
| 82 virtual void ShutdownHandler() OVERRIDE; |
| 83 virtual void OnMessage(const std::string& app_id, |
| 84 const GCMClient::IncomingMessage& message) OVERRIDE; |
| 85 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; |
| 86 virtual void OnSendError( |
| 87 const std::string& app_id, |
| 88 const GCMClient::SendErrorDetails& send_error_details) OVERRIDE; |
| 89 |
| 90 private: |
| 91 void ClearResults(); |
| 92 |
| 93 scoped_ptr<base::RunLoop> run_loop_; |
| 94 |
| 95 Event received_event_; |
| 96 std::string app_id_; |
| 97 GCMClient::IncomingMessage message_; |
| 98 GCMClient::SendErrorDetails send_error_details_; |
| 99 |
| 100 DISALLOW_COPY_AND_ASSIGN(FakeGCMAppHandler); |
| 101 }; |
| 102 |
| 103 class TestGCMService : public GCMService { |
| 104 public: |
| 105 TestGCMService( |
| 106 bool start_automatically, |
| 107 scoped_ptr<IdentityProvider> identity_provider, |
| 108 const scoped_refptr<net::URLRequestContextGetter>& request_context); |
| 109 virtual ~TestGCMService(); |
| 110 |
| 111 protected: |
| 112 // GCMService: |
| 113 virtual bool ShouldStartAutomatically() const OVERRIDE; |
| 114 virtual base::FilePath GetStorePath() const OVERRIDE; |
| 115 virtual scoped_refptr<net::URLRequestContextGetter> |
| 116 GetURLRequestContextGetter() const OVERRIDE; |
| 117 |
| 118 private: |
| 119 base::ScopedTempDir temp_dir_; |
| 120 scoped_refptr<net::URLRequestContextGetter> request_context_; |
| 121 const bool start_automatically_; |
| 122 |
| 123 DISALLOW_COPY_AND_ASSIGN(TestGCMService); |
| 124 }; |
| 125 |
| 126 FakeGCMAppHandler::FakeGCMAppHandler() : received_event_(NO_EVENT) { |
| 127 } |
| 128 |
| 129 FakeGCMAppHandler::~FakeGCMAppHandler() { |
| 130 } |
| 131 |
| 132 void FakeGCMAppHandler::WaitForNotification() { |
| 133 run_loop_.reset(new base::RunLoop); |
| 134 run_loop_->Run(); |
| 135 run_loop_.reset(); |
| 136 } |
| 137 |
| 138 void FakeGCMAppHandler::ShutdownHandler() { |
| 139 } |
| 140 |
| 141 void FakeGCMAppHandler::OnMessage(const std::string& app_id, |
| 142 const GCMClient::IncomingMessage& message) { |
| 143 ClearResults(); |
| 144 received_event_ = MESSAGE_EVENT; |
| 145 app_id_ = app_id; |
| 146 message_ = message; |
| 147 if (run_loop_) |
| 148 run_loop_->Quit(); |
| 149 } |
| 150 |
| 151 void FakeGCMAppHandler::OnMessagesDeleted(const std::string& app_id) { |
| 152 ClearResults(); |
| 153 received_event_ = MESSAGES_DELETED_EVENT; |
| 154 app_id_ = app_id; |
| 155 if (run_loop_) |
| 156 run_loop_->Quit(); |
| 157 } |
| 158 |
| 159 void FakeGCMAppHandler::OnSendError( |
| 160 const std::string& app_id, |
| 161 const GCMClient::SendErrorDetails& send_error_details) { |
| 162 ClearResults(); |
| 163 received_event_ = SEND_ERROR_EVENT; |
| 164 app_id_ = app_id; |
| 165 send_error_details_ = send_error_details; |
| 166 if (run_loop_) |
| 167 run_loop_->Quit(); |
| 168 } |
| 169 |
| 170 void FakeGCMAppHandler::ClearResults() { |
| 171 received_event_ = NO_EVENT; |
| 172 app_id_.clear(); |
| 173 message_ = GCMClient::IncomingMessage(); |
| 174 send_error_details_ = GCMClient::SendErrorDetails(); |
| 175 } |
| 176 |
| 177 TestGCMService::TestGCMService( |
| 178 bool start_automatically, |
| 179 scoped_ptr<IdentityProvider> identity_provider, |
| 180 const scoped_refptr<net::URLRequestContextGetter>& request_context) |
| 181 : GCMService(identity_provider.Pass()), |
| 182 request_context_(request_context), |
| 183 start_automatically_(start_automatically) { |
| 184 if (!temp_dir_.CreateUniqueTempDir()) |
| 185 ADD_FAILURE(); |
| 186 } |
| 187 |
| 188 TestGCMService::~TestGCMService() { |
| 189 } |
| 190 |
| 191 bool TestGCMService::ShouldStartAutomatically() const { |
| 192 return start_automatically_; |
| 193 } |
| 194 |
| 195 base::FilePath TestGCMService::GetStorePath() const { |
| 196 return temp_dir_.path(); |
| 197 } |
| 198 |
| 199 scoped_refptr<net::URLRequestContextGetter> |
| 200 TestGCMService::GetURLRequestContextGetter() const { |
| 201 return request_context_; |
| 202 } |
| 203 |
| 204 } // namespace |
| 205 |
| 206 class TestGCMServiceWrapper { |
| 207 public: |
| 208 enum WaitToFinish { |
| 209 DO_NOT_WAIT, |
| 210 WAIT |
| 211 }; |
| 212 |
| 213 explicit TestGCMServiceWrapper( |
| 214 const scoped_refptr<net::URLRequestContextGetter>& request_context); |
| 215 ~TestGCMServiceWrapper(); |
| 216 |
| 217 TestGCMService* service() { return service_.get(); } |
| 218 FakeGCMAppHandler* gcm_app_handler() { return gcm_app_handler_.get(); } |
| 219 const std::string& registration_id() const { return registration_id_; } |
| 220 GCMClient::Result registration_result() const { return registration_result_; } |
| 221 const std::string& send_message_id() const { return send_message_id_; } |
| 222 GCMClient::Result send_result() const { return send_result_; } |
| 223 GCMClient::Result unregistration_result() const { |
| 224 return unregistration_result_; |
| 225 } |
| 226 |
| 227 void ClearRegistrationResult(); |
| 228 void ClearUnregistrationResult(); |
| 229 |
| 230 bool ServiceHasAppHandlers() const; |
| 231 GCMClientMock* GetGCMClient(); |
| 232 |
| 233 void CreateService(bool start_automatically, |
| 234 GCMClientMock::LoadingDelay gcm_client_loading_delay); |
| 235 |
| 236 void SignIn(const std::string& account_id); |
| 237 void SignOut(); |
| 238 |
| 239 void Register(const std::string& app_id, |
| 240 const std::vector<std::string>& sender_ids, |
| 241 WaitToFinish wait_to_finish); |
| 242 void Send(const std::string& app_id, |
| 243 const std::string& receiver_id, |
| 244 const GCMClient::OutgoingMessage& message, |
| 245 WaitToFinish wait_to_finish); |
| 246 void Unregister(const std::string& app_id, WaitToFinish wait_to_finish); |
| 247 |
| 248 void WaitForAsyncOperation(); |
| 249 |
| 250 private: |
| 251 void RegisterCompleted(const std::string& registration_id, |
| 252 GCMClient::Result result); |
| 253 void SendCompleted(const std::string& message_id, GCMClient::Result result); |
| 254 void UnregisterCompleted(GCMClient::Result result); |
| 255 |
| 256 scoped_refptr<net::URLRequestContextGetter> request_context_; |
| 257 FakeOAuth2TokenService token_service_; |
| 258 scoped_ptr<FakeIdentityProvider> identity_provider_owner_; |
| 259 FakeIdentityProvider* identity_provider_; |
| 260 scoped_ptr<TestGCMService> service_; |
| 261 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; |
| 262 |
| 263 base::Closure async_operation_completed_callback_; |
| 264 |
| 265 std::string registration_id_; |
| 266 GCMClient::Result registration_result_; |
| 267 std::string send_message_id_; |
| 268 GCMClient::Result send_result_; |
| 269 GCMClient::Result unregistration_result_; |
| 270 |
| 271 DISALLOW_COPY_AND_ASSIGN(TestGCMServiceWrapper); |
| 272 }; |
| 273 |
| 274 TestGCMServiceWrapper::TestGCMServiceWrapper( |
| 275 const scoped_refptr<net::URLRequestContextGetter>& request_context) |
| 276 : request_context_(request_context), |
| 277 identity_provider_(NULL), |
| 278 registration_result_(GCMClient::UNKNOWN_ERROR), |
| 279 send_result_(GCMClient::UNKNOWN_ERROR), |
| 280 unregistration_result_(GCMClient::UNKNOWN_ERROR) { |
| 281 identity_provider_owner_.reset(new FakeIdentityProvider(&token_service_)); |
| 282 identity_provider_ = identity_provider_owner_.get(); |
| 283 } |
| 284 |
| 285 TestGCMServiceWrapper::~TestGCMServiceWrapper() { |
| 286 if (!service_) |
| 287 return; |
| 288 |
| 289 service_->ShutdownService(); |
| 290 service_.reset(); |
| 291 PumpIOLoop(); |
| 292 } |
| 293 |
| 294 void TestGCMServiceWrapper::ClearRegistrationResult() { |
| 295 registration_id_.clear(); |
| 296 registration_result_ = GCMClient::UNKNOWN_ERROR; |
| 297 } |
| 298 |
| 299 void TestGCMServiceWrapper::ClearUnregistrationResult() { |
| 300 unregistration_result_ = GCMClient::UNKNOWN_ERROR; |
| 301 } |
| 302 |
| 303 bool TestGCMServiceWrapper::ServiceHasAppHandlers() const { |
| 304 return !service_->app_handlers_.empty(); |
| 305 } |
| 306 |
| 307 GCMClientMock* TestGCMServiceWrapper::GetGCMClient() { |
| 308 return static_cast<GCMClientMock*>(service_->GetGCMClientForTesting()); |
| 309 } |
| 310 |
| 311 void TestGCMServiceWrapper::CreateService( |
| 312 bool start_automatically, |
| 313 GCMClientMock::LoadingDelay gcm_client_loading_delay) { |
| 314 service_.reset(new TestGCMService( |
| 315 start_automatically, |
| 316 identity_provider_owner_.PassAs<IdentityProvider>(), |
| 317 request_context_)); |
| 318 service_->Initialize(scoped_ptr<GCMClientFactory>( |
| 319 new FakeGCMClientFactory(gcm_client_loading_delay))); |
| 320 |
| 321 gcm_app_handler_.reset(new FakeGCMAppHandler); |
| 322 service_->AddAppHandler(kTestAppID1, gcm_app_handler_.get()); |
| 323 service_->AddAppHandler(kTestAppID2, gcm_app_handler_.get()); |
| 324 } |
| 325 |
| 326 void TestGCMServiceWrapper::SignIn(const std::string& account_id) { |
| 327 token_service_.AddAccount(account_id); |
| 328 identity_provider_->LogIn(account_id); |
| 329 PumpIOLoop(); |
| 330 PumpUILoop(); |
| 331 } |
| 332 |
| 333 void TestGCMServiceWrapper::SignOut() { |
| 334 identity_provider_->LogOut(); |
| 335 PumpIOLoop(); |
| 336 PumpUILoop(); |
| 337 } |
| 338 |
| 339 void TestGCMServiceWrapper::Register(const std::string& app_id, |
| 340 const std::vector<std::string>& sender_ids, |
| 341 WaitToFinish wait_to_finish) { |
| 342 base::RunLoop run_loop; |
| 343 async_operation_completed_callback_ = run_loop.QuitClosure(); |
| 344 service_->Register(app_id, |
| 345 sender_ids, |
| 346 base::Bind(&TestGCMServiceWrapper::RegisterCompleted, |
| 347 base::Unretained(this))); |
| 348 if (wait_to_finish == WAIT) |
| 349 run_loop.Run(); |
| 350 } |
| 351 |
| 352 void TestGCMServiceWrapper::Send(const std::string& app_id, |
| 353 const std::string& receiver_id, |
| 354 const GCMClient::OutgoingMessage& message, |
| 355 WaitToFinish wait_to_finish) { |
| 356 base::RunLoop run_loop; |
| 357 async_operation_completed_callback_ = run_loop.QuitClosure(); |
| 358 service_->Send(app_id, |
| 359 receiver_id, |
| 360 message, |
| 361 base::Bind(&TestGCMServiceWrapper::SendCompleted, |
| 362 base::Unretained(this))); |
| 363 if (wait_to_finish == WAIT) |
| 364 run_loop.Run(); |
| 365 } |
| 366 |
| 367 void TestGCMServiceWrapper::Unregister(const std::string& app_id, |
| 368 WaitToFinish wait_to_finish) { |
| 369 base::RunLoop run_loop; |
| 370 async_operation_completed_callback_ = run_loop.QuitClosure(); |
| 371 service_->Unregister(app_id, |
| 372 base::Bind(&TestGCMServiceWrapper::UnregisterCompleted, |
| 373 base::Unretained(this))); |
| 374 if (wait_to_finish == WAIT) |
| 375 run_loop.Run(); |
| 376 } |
| 377 |
| 378 void TestGCMServiceWrapper::WaitForAsyncOperation() { |
| 379 base::RunLoop run_loop; |
| 380 async_operation_completed_callback_ = run_loop.QuitClosure(); |
| 381 run_loop.Run(); |
| 382 } |
| 383 |
| 384 void TestGCMServiceWrapper::RegisterCompleted( |
| 385 const std::string& registration_id, |
| 386 GCMClient::Result result) { |
| 387 registration_id_ = registration_id; |
| 388 registration_result_ = result; |
| 389 if (!async_operation_completed_callback_.is_null()) |
| 390 async_operation_completed_callback_.Run(); |
| 391 } |
| 392 |
| 393 void TestGCMServiceWrapper::SendCompleted(const std::string& message_id, |
| 394 GCMClient::Result result) { |
| 395 send_message_id_ = message_id; |
| 396 send_result_ = result; |
| 397 if (!async_operation_completed_callback_.is_null()) |
| 398 async_operation_completed_callback_.Run(); |
| 399 } |
| 400 |
| 401 void TestGCMServiceWrapper::UnregisterCompleted(GCMClient::Result result) { |
| 402 unregistration_result_ = result; |
| 403 if (!async_operation_completed_callback_.is_null()) |
| 404 async_operation_completed_callback_.Run(); |
| 405 } |
| 406 |
| 407 class GCMServiceTest : public testing::Test { |
| 408 protected: |
| 409 GCMServiceTest(); |
| 410 virtual ~GCMServiceTest(); |
| 411 |
| 412 // testing::Test: |
| 413 virtual void SetUp() OVERRIDE; |
| 414 virtual void TearDown() OVERRIDE; |
| 415 |
| 416 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_; |
| 417 scoped_refptr<net::URLRequestContextGetter> request_context_; |
| 418 scoped_ptr<TestGCMServiceWrapper> wrapper_; |
| 419 |
| 420 private: |
| 421 DISALLOW_COPY_AND_ASSIGN(GCMServiceTest); |
| 422 }; |
| 423 |
| 424 GCMServiceTest::GCMServiceTest() { |
| 425 } |
| 426 |
| 427 GCMServiceTest::~GCMServiceTest() { |
| 428 } |
| 429 |
| 430 void GCMServiceTest::SetUp() { |
| 431 thread_bundle_.reset(new content::TestBrowserThreadBundle( |
| 432 content::TestBrowserThreadBundle::REAL_IO_THREAD)); |
| 433 request_context_ = new net::TestURLRequestContextGetter( |
| 434 content::BrowserThread::GetMessageLoopProxyForThread( |
| 435 content::BrowserThread::IO)); |
| 436 wrapper_.reset(new TestGCMServiceWrapper(request_context_)); |
| 437 } |
| 438 |
| 439 void GCMServiceTest::TearDown() { |
| 440 wrapper_.reset(); |
| 441 } |
| 442 |
| 443 TEST_F(GCMServiceTest, CreateGCMServiceBeforeSignIn) { |
| 444 // Create CreateGMCService first. |
| 445 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); |
| 446 EXPECT_FALSE(wrapper_->service()->IsStarted()); |
| 447 |
| 448 // Sign in. This will kick off the check-in. |
| 449 wrapper_->SignIn(kTestAccountID1); |
| 450 EXPECT_TRUE(wrapper_->service()->IsStarted()); |
| 451 } |
| 452 |
| 453 TEST_F(GCMServiceTest, CreateGCMServiceAfterSignIn) { |
| 454 // Sign in. This will not initiate the check-in. |
| 455 wrapper_->SignIn(kTestAccountID1); |
| 456 |
| 457 // Create GCMeService after sign-in. |
| 458 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); |
| 459 EXPECT_TRUE(wrapper_->service()->IsStarted()); |
| 460 } |
| 461 |
| 462 TEST_F(GCMServiceTest, Shutdown) { |
| 463 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); |
| 464 EXPECT_TRUE(wrapper_->ServiceHasAppHandlers()); |
| 465 |
| 466 wrapper_->service()->ShutdownService(); |
| 467 EXPECT_FALSE(wrapper_->ServiceHasAppHandlers()); |
| 468 } |
| 469 |
| 470 TEST_F(GCMServiceTest, SignInAndSignOutUnderPositiveChannelSignal) { |
| 471 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); |
| 472 wrapper_->SignIn(kTestAccountID1); |
| 473 |
| 474 // GCMClient should be loaded. |
| 475 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady()); |
| 476 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status()); |
| 477 |
| 478 wrapper_->SignOut(); |
| 479 |
| 480 // GCMClient should be checked out. |
| 481 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady()); |
| 482 EXPECT_EQ(GCMClientMock::CHECKED_OUT, wrapper_->GetGCMClient()->status()); |
| 483 } |
| 484 |
| 485 TEST_F(GCMServiceTest, SignInAndSignOutUnderNonPositiveChannelSignal) { |
| 486 // Non-positive channel signal will prevent GCMClient from checking in during |
| 487 // sign-in. |
| 488 wrapper_->CreateService(false, GCMClientMock::NO_DELAY_LOADING); |
| 489 wrapper_->SignIn(kTestAccountID1); |
| 490 |
| 491 // GCMClient should not be loaded. |
| 492 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady()); |
| 493 EXPECT_EQ(GCMClientMock::UNINITIALIZED, wrapper_->GetGCMClient()->status()); |
| 494 |
| 495 wrapper_->SignOut(); |
| 496 |
| 497 // Check-out should still be performed. |
| 498 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady()); |
| 499 EXPECT_EQ(GCMClientMock::CHECKED_OUT, wrapper_->GetGCMClient()->status()); |
| 500 } |
| 501 |
| 502 TEST_F(GCMServiceTest, SignOutAndThenSignIn) { |
| 503 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); |
| 504 wrapper_->SignIn(kTestAccountID1); |
| 505 |
| 506 // GCMClient should be loaded. |
| 507 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady()); |
| 508 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status()); |
| 509 |
| 510 wrapper_->SignOut(); |
| 511 |
| 512 // GCMClient should be checked out. |
| 513 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady()); |
| 514 EXPECT_EQ(GCMClientMock::CHECKED_OUT, wrapper_->GetGCMClient()->status()); |
| 515 |
| 516 // Sign-in with a different account. |
| 517 wrapper_->SignIn(kTestAccountID2); |
| 518 |
| 519 // GCMClient should be loaded again. |
| 520 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady()); |
| 521 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status()); |
| 522 } |
| 523 |
| 524 TEST_F(GCMServiceTest, StopAndRestartGCM) { |
| 525 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); |
| 526 wrapper_->SignIn(kTestAccountID1); |
| 527 |
| 528 // GCMClient should be loaded. |
| 529 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady()); |
| 530 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status()); |
| 531 |
| 532 // Stops the GCM. |
| 533 wrapper_->service()->Stop(); |
| 534 PumpIOLoop(); |
| 535 PumpUILoop(); |
| 536 |
| 537 // GCMClient should be stopped. |
| 538 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady()); |
| 539 EXPECT_EQ(GCMClientMock::STOPPED, wrapper_->GetGCMClient()->status()); |
| 540 |
| 541 // Restarts the GCM. |
| 542 wrapper_->service()->Start(); |
| 543 PumpIOLoop(); |
| 544 PumpUILoop(); |
| 545 |
| 546 // GCMClient should be loaded. |
| 547 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady()); |
| 548 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status()); |
| 549 |
| 550 // Stops the GCM. |
| 551 wrapper_->service()->Stop(); |
| 552 PumpIOLoop(); |
| 553 PumpUILoop(); |
| 554 |
| 555 // GCMClient should be stopped. |
| 556 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady()); |
| 557 EXPECT_EQ(GCMClientMock::STOPPED, wrapper_->GetGCMClient()->status()); |
| 558 |
| 559 // Sign out. |
| 560 wrapper_->SignOut(); |
| 561 |
| 562 // GCMClient should be checked out. |
| 563 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady()); |
| 564 EXPECT_EQ(GCMClientMock::CHECKED_OUT, wrapper_->GetGCMClient()->status()); |
| 565 } |
| 566 |
| 567 TEST_F(GCMServiceTest, RegisterWhenNotSignedIn) { |
| 568 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); |
| 569 |
| 570 std::vector<std::string> sender_ids; |
| 571 sender_ids.push_back("sender1"); |
| 572 wrapper_->Register(kTestAppID1, sender_ids, TestGCMServiceWrapper::WAIT); |
| 573 |
| 574 EXPECT_TRUE(wrapper_->registration_id().empty()); |
| 575 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, wrapper_->registration_result()); |
| 576 } |
| 577 |
| 578 TEST_F(GCMServiceTest, RegisterUnderNonPositiveChannelSignal) { |
| 579 // Non-positive channel signal will prevent GCMClient from checking in during |
| 580 // sign-in. |
| 581 wrapper_->CreateService(false, GCMClientMock::NO_DELAY_LOADING); |
| 582 wrapper_->SignIn(kTestAccountID1); |
| 583 |
| 584 // GCMClient should not be checked in. |
| 585 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady()); |
| 586 EXPECT_EQ(GCMClientMock::UNINITIALIZED, wrapper_->GetGCMClient()->status()); |
| 587 |
| 588 // Invoking register will make GCMClient checked in. |
| 589 std::vector<std::string> sender_ids; |
| 590 sender_ids.push_back("sender1"); |
| 591 wrapper_->Register(kTestAppID1, sender_ids, TestGCMServiceWrapper::WAIT); |
| 592 |
| 593 // GCMClient should be checked in. |
| 594 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady()); |
| 595 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status()); |
| 596 |
| 597 // Registration should succeed. |
| 598 const std::string expected_registration_id = |
| 599 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids); |
| 600 EXPECT_EQ(expected_registration_id, wrapper_->registration_id()); |
| 601 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); |
| 602 } |
| 603 |
| 604 TEST_F(GCMServiceTest, SendWhenNotSignedIn) { |
| 605 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); |
| 606 |
| 607 GCMClient::OutgoingMessage message; |
| 608 message.id = "1"; |
| 609 message.data["key1"] = "value1"; |
| 610 wrapper_->Send(kTestAppID1, kUserID1, message, TestGCMServiceWrapper::WAIT); |
| 611 |
| 612 EXPECT_TRUE(wrapper_->send_message_id().empty()); |
| 613 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, wrapper_->send_result()); |
| 614 } |
| 615 |
| 616 TEST_F(GCMServiceTest, SendUnderNonPositiveChannelSignal) { |
| 617 // Non-positive channel signal will prevent GCMClient from checking in during |
| 618 // sign-in. |
| 619 wrapper_->CreateService(false, GCMClientMock::NO_DELAY_LOADING); |
| 620 wrapper_->SignIn(kTestAccountID1); |
| 621 |
| 622 // GCMClient should not be checked in. |
| 623 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady()); |
| 624 EXPECT_EQ(GCMClientMock::UNINITIALIZED, wrapper_->GetGCMClient()->status()); |
| 625 |
| 626 // Invoking send will make GCMClient checked in. |
| 627 GCMClient::OutgoingMessage message; |
| 628 message.id = "1"; |
| 629 message.data["key1"] = "value1"; |
| 630 wrapper_->Send(kTestAppID1, kUserID1, message, TestGCMServiceWrapper::WAIT); |
| 631 |
| 632 // GCMClient should be checked in. |
| 633 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady()); |
| 634 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status()); |
| 635 |
| 636 // Sending should succeed. |
| 637 EXPECT_EQ(message.id, wrapper_->send_message_id()); |
| 638 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->send_result()); |
| 639 } |
| 640 |
| 641 // Tests a single instance of GCMService. |
| 642 class GCMServiceSingleInstanceTest : public GCMServiceTest { |
| 643 public: |
| 644 GCMServiceSingleInstanceTest(); |
| 645 virtual ~GCMServiceSingleInstanceTest(); |
| 646 |
| 647 // GCMServiceTest: |
| 648 virtual void SetUp() OVERRIDE; |
| 649 |
| 650 private: |
| 651 DISALLOW_COPY_AND_ASSIGN(GCMServiceSingleInstanceTest); |
| 652 }; |
| 653 |
| 654 GCMServiceSingleInstanceTest::GCMServiceSingleInstanceTest() { |
| 655 } |
| 656 |
| 657 GCMServiceSingleInstanceTest::~GCMServiceSingleInstanceTest() { |
| 658 } |
| 659 |
| 660 void GCMServiceSingleInstanceTest::SetUp() { |
| 661 GCMServiceTest::SetUp(); |
| 662 |
| 663 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); |
| 664 wrapper_->SignIn(kTestAccountID1); |
| 665 } |
| 666 |
| 667 TEST_F(GCMServiceSingleInstanceTest, Register) { |
| 668 std::vector<std::string> sender_ids; |
| 669 sender_ids.push_back("sender1"); |
| 670 wrapper_->Register(kTestAppID1, sender_ids, TestGCMServiceWrapper::WAIT); |
| 671 const std::string expected_registration_id = |
| 672 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids); |
| 673 |
| 674 EXPECT_EQ(expected_registration_id, wrapper_->registration_id()); |
| 675 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); |
| 676 } |
| 677 |
| 678 TEST_F(GCMServiceSingleInstanceTest, RegisterError) { |
| 679 std::vector<std::string> sender_ids; |
| 680 sender_ids.push_back("sender1@error"); |
| 681 wrapper_->Register(kTestAppID1, sender_ids, TestGCMServiceWrapper::WAIT); |
| 682 |
| 683 EXPECT_TRUE(wrapper_->registration_id().empty()); |
| 684 EXPECT_NE(GCMClient::SUCCESS, wrapper_->registration_result()); |
| 685 } |
| 686 |
| 687 TEST_F(GCMServiceSingleInstanceTest, RegisterAgainWithSameSenderIDs) { |
| 688 std::vector<std::string> sender_ids; |
| 689 sender_ids.push_back("sender1"); |
| 690 sender_ids.push_back("sender2"); |
| 691 wrapper_->Register(kTestAppID1, sender_ids, TestGCMServiceWrapper::WAIT); |
| 692 const std::string expected_registration_id = |
| 693 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids); |
| 694 |
| 695 EXPECT_EQ(expected_registration_id, wrapper_->registration_id()); |
| 696 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); |
| 697 |
| 698 // Clears the results the would be set by the Register callback in preparation |
| 699 // to call register 2nd time. |
| 700 wrapper_->ClearRegistrationResult(); |
| 701 |
| 702 // Calling register 2nd time with the same set of sender IDs but different |
| 703 // ordering will get back the same registration ID. |
| 704 std::vector<std::string> another_sender_ids; |
| 705 another_sender_ids.push_back("sender2"); |
| 706 another_sender_ids.push_back("sender1"); |
| 707 wrapper_->Register(kTestAppID1, |
| 708 another_sender_ids, |
| 709 TestGCMServiceWrapper::WAIT); |
| 710 |
| 711 EXPECT_EQ(expected_registration_id, wrapper_->registration_id()); |
| 712 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); |
| 713 } |
| 714 |
| 715 TEST_F(GCMServiceSingleInstanceTest, RegisterAgainWithDifferentSenderIDs) { |
| 716 std::vector<std::string> sender_ids; |
| 717 sender_ids.push_back("sender1"); |
| 718 wrapper_->Register(kTestAppID1, sender_ids, TestGCMServiceWrapper::WAIT); |
| 719 const std::string expected_registration_id = |
| 720 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids); |
| 721 |
| 722 EXPECT_EQ(expected_registration_id, wrapper_->registration_id()); |
| 723 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); |
| 724 |
| 725 // Make sender IDs different. |
| 726 sender_ids.push_back("sender2"); |
| 727 const std::string expected_registration_id2 = |
| 728 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids); |
| 729 |
| 730 // Calling register 2nd time with the different sender IDs will get back a new |
| 731 // registration ID. |
| 732 wrapper_->Register(kTestAppID1, sender_ids, TestGCMServiceWrapper::WAIT); |
| 733 EXPECT_EQ(expected_registration_id2, wrapper_->registration_id()); |
| 734 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); |
| 735 } |
| 736 |
| 737 TEST_F(GCMServiceSingleInstanceTest, GCMClientNotReadyBeforeRegistration) { |
| 738 // Make GCMClient not ready initially. |
| 739 wrapper_.reset(new TestGCMServiceWrapper(request_context_)); |
| 740 wrapper_->CreateService(true, GCMClientMock::DELAY_LOADING); |
| 741 wrapper_->SignIn(kTestAccountID1); |
| 742 |
| 743 // The registration is on hold until GCMClient is ready. |
| 744 std::vector<std::string> sender_ids; |
| 745 sender_ids.push_back("sender1"); |
| 746 wrapper_->Register(kTestAppID1, |
| 747 sender_ids, |
| 748 TestGCMServiceWrapper::DO_NOT_WAIT); |
| 749 PumpIOLoop(); |
| 750 PumpUILoop(); |
| 751 EXPECT_TRUE(wrapper_->registration_id().empty()); |
| 752 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, wrapper_->registration_result()); |
| 753 |
| 754 // Register operation will be invoked after GCMClient becomes ready. |
| 755 wrapper_->GetGCMClient()->PerformDelayedLoading(); |
| 756 wrapper_->WaitForAsyncOperation(); |
| 757 EXPECT_FALSE(wrapper_->registration_id().empty()); |
| 758 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); |
| 759 } |
| 760 |
| 761 TEST_F(GCMServiceSingleInstanceTest, RegisterAfterSignOut) { |
| 762 // This will trigger check-out. |
| 763 wrapper_->SignOut(); |
| 764 |
| 765 std::vector<std::string> sender_ids; |
| 766 sender_ids.push_back("sender1"); |
| 767 wrapper_->Register(kTestAppID1, sender_ids, TestGCMServiceWrapper::WAIT); |
| 768 |
| 769 EXPECT_TRUE(wrapper_->registration_id().empty()); |
| 770 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, wrapper_->registration_result()); |
| 771 } |
| 772 |
| 773 TEST_F(GCMServiceSingleInstanceTest, UnregisterExplicitly) { |
| 774 std::vector<std::string> sender_ids; |
| 775 sender_ids.push_back("sender1"); |
| 776 wrapper_->Register(kTestAppID1, sender_ids, TestGCMServiceWrapper::WAIT); |
| 777 |
| 778 EXPECT_FALSE(wrapper_->registration_id().empty()); |
| 779 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); |
| 780 |
| 781 wrapper_->Unregister(kTestAppID1, TestGCMServiceWrapper::WAIT); |
| 782 |
| 783 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->unregistration_result()); |
| 784 } |
| 785 |
| 786 TEST_F(GCMServiceSingleInstanceTest, UnregisterWhenAsyncOperationPending) { |
| 787 std::vector<std::string> sender_ids; |
| 788 sender_ids.push_back("sender1"); |
| 789 // First start registration without waiting for it to complete. |
| 790 wrapper_->Register(kTestAppID1, |
| 791 sender_ids, |
| 792 TestGCMServiceWrapper::DO_NOT_WAIT); |
| 793 |
| 794 // Test that unregistration fails with async operation pending when there is a |
| 795 // registration already in progress. |
| 796 wrapper_->Unregister(kTestAppID1, TestGCMServiceWrapper::WAIT); |
| 797 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, |
| 798 wrapper_->unregistration_result()); |
| 799 |
| 800 // Complete the unregistration. |
| 801 wrapper_->WaitForAsyncOperation(); |
| 802 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); |
| 803 |
| 804 // Start unregistration without waiting for it to complete. This time no async |
| 805 // operation is pending. |
| 806 wrapper_->Unregister(kTestAppID1, TestGCMServiceWrapper::DO_NOT_WAIT); |
| 807 |
| 808 // Test that unregistration fails with async operation pending when there is |
| 809 // an unregistration already in progress. |
| 810 wrapper_->Unregister(kTestAppID1, TestGCMServiceWrapper::WAIT); |
| 811 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, |
| 812 wrapper_->unregistration_result()); |
| 813 wrapper_->ClearUnregistrationResult(); |
| 814 |
| 815 // Complete unregistration. |
| 816 wrapper_->WaitForAsyncOperation(); |
| 817 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->unregistration_result()); |
| 818 } |
| 819 |
| 820 TEST_F(GCMServiceSingleInstanceTest, RegisterWhenAsyncOperationPending) { |
| 821 std::vector<std::string> sender_ids; |
| 822 sender_ids.push_back("sender1"); |
| 823 // First start registration without waiting for it to complete. |
| 824 wrapper_->Register(kTestAppID1, |
| 825 sender_ids, |
| 826 TestGCMServiceWrapper::DO_NOT_WAIT); |
| 827 |
| 828 // Test that registration fails with async operation pending when there is a |
| 829 // registration already in progress. |
| 830 wrapper_->Register(kTestAppID1, sender_ids, TestGCMServiceWrapper::WAIT); |
| 831 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, |
| 832 wrapper_->registration_result()); |
| 833 wrapper_->ClearRegistrationResult(); |
| 834 |
| 835 // Complete the registration. |
| 836 wrapper_->WaitForAsyncOperation(); |
| 837 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); |
| 838 |
| 839 // Start unregistration without waiting for it to complete. This time no async |
| 840 // operation is pending. |
| 841 wrapper_->Unregister(kTestAppID1, TestGCMServiceWrapper::DO_NOT_WAIT); |
| 842 |
| 843 // Test that registration fails with async operation pending when there is an |
| 844 // unregistration already in progress. |
| 845 wrapper_->Register(kTestAppID1, sender_ids, TestGCMServiceWrapper::WAIT); |
| 846 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, |
| 847 wrapper_->registration_result()); |
| 848 |
| 849 // Complete the first unregistration expecting success. |
| 850 wrapper_->WaitForAsyncOperation(); |
| 851 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->unregistration_result()); |
| 852 |
| 853 // Test that it is ok to register again after unregistration. |
| 854 wrapper_->Register(kTestAppID1, sender_ids, TestGCMServiceWrapper::WAIT); |
| 855 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); |
| 856 } |
| 857 |
| 858 TEST_F(GCMServiceSingleInstanceTest, Send) { |
| 859 GCMClient::OutgoingMessage message; |
| 860 message.id = "1"; |
| 861 message.data["key1"] = "value1"; |
| 862 message.data["key2"] = "value2"; |
| 863 wrapper_->Send(kTestAppID1, kUserID1, message, TestGCMServiceWrapper::WAIT); |
| 864 |
| 865 EXPECT_EQ(message.id, wrapper_->send_message_id()); |
| 866 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->send_result()); |
| 867 } |
| 868 |
| 869 TEST_F(GCMServiceSingleInstanceTest, GCMClientNotReadyBeforeSending) { |
| 870 // Make GCMClient not ready initially. |
| 871 wrapper_.reset(new TestGCMServiceWrapper(request_context_)); |
| 872 wrapper_->CreateService(true, GCMClientMock::DELAY_LOADING); |
| 873 wrapper_->SignIn(kTestAccountID1); |
| 874 |
| 875 // The sending is on hold until GCMClient is ready. |
| 876 GCMClient::OutgoingMessage message; |
| 877 message.id = "1"; |
| 878 message.data["key1"] = "value1"; |
| 879 message.data["key2"] = "value2"; |
| 880 wrapper_->Send(kTestAppID1, |
| 881 kUserID1, |
| 882 message, |
| 883 TestGCMServiceWrapper::DO_NOT_WAIT); |
| 884 PumpIOLoop(); |
| 885 PumpUILoop(); |
| 886 |
| 887 EXPECT_TRUE(wrapper_->send_message_id().empty()); |
| 888 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, wrapper_->send_result()); |
| 889 |
| 890 // Send operation will be invoked after GCMClient becomes ready. |
| 891 wrapper_->GetGCMClient()->PerformDelayedLoading(); |
| 892 wrapper_->WaitForAsyncOperation(); |
| 893 EXPECT_EQ(message.id, wrapper_->send_message_id()); |
| 894 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->send_result()); |
| 895 } |
| 896 |
| 897 TEST_F(GCMServiceSingleInstanceTest, SendAfterSignOut) { |
| 898 // This will trigger check-out. |
| 899 wrapper_->SignOut(); |
| 900 |
| 901 GCMClient::OutgoingMessage message; |
| 902 message.id = "1"; |
| 903 message.data["key1"] = "value1"; |
| 904 message.data["key2"] = "value2"; |
| 905 wrapper_->Send(kTestAppID1, kUserID1, message, TestGCMServiceWrapper::WAIT); |
| 906 |
| 907 EXPECT_TRUE(wrapper_->send_message_id().empty()); |
| 908 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, wrapper_->send_result()); |
| 909 } |
| 910 |
| 911 TEST_F(GCMServiceSingleInstanceTest, SendError) { |
| 912 GCMClient::OutgoingMessage message; |
| 913 // Embedding error in id will tell the mock to simulate the send error. |
| 914 message.id = "1@error"; |
| 915 message.data["key1"] = "value1"; |
| 916 message.data["key2"] = "value2"; |
| 917 wrapper_->Send(kTestAppID1, kUserID1, message, TestGCMServiceWrapper::WAIT); |
| 918 |
| 919 EXPECT_EQ(message.id, wrapper_->send_message_id()); |
| 920 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->send_result()); |
| 921 |
| 922 // Wait for the send error. |
| 923 wrapper_->gcm_app_handler()->WaitForNotification(); |
| 924 EXPECT_EQ(FakeGCMAppHandler::SEND_ERROR_EVENT, |
| 925 wrapper_->gcm_app_handler()->received_event()); |
| 926 EXPECT_EQ(kTestAppID1, wrapper_->gcm_app_handler()->app_id()); |
| 927 EXPECT_EQ(message.id, |
| 928 wrapper_->gcm_app_handler()->send_error_details().message_id); |
| 929 EXPECT_NE(GCMClient::SUCCESS, |
| 930 wrapper_->gcm_app_handler()->send_error_details().result); |
| 931 EXPECT_EQ(message.data, |
| 932 wrapper_->gcm_app_handler()->send_error_details().additional_data); |
| 933 } |
| 934 |
| 935 TEST_F(GCMServiceSingleInstanceTest, MessageReceived) { |
| 936 wrapper_->Register(kTestAppID1, |
| 937 ToSenderList("sender"), |
| 938 TestGCMServiceWrapper::WAIT); |
| 939 GCMClient::IncomingMessage message; |
| 940 message.data["key1"] = "value1"; |
| 941 message.data["key2"] = "value2"; |
| 942 message.sender_id = "sender"; |
| 943 wrapper_->GetGCMClient()->ReceiveMessage(kTestAppID1, message); |
| 944 wrapper_->gcm_app_handler()->WaitForNotification(); |
| 945 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, |
| 946 wrapper_->gcm_app_handler()->received_event()); |
| 947 EXPECT_EQ(kTestAppID1, wrapper_->gcm_app_handler()->app_id()); |
| 948 EXPECT_EQ(message.data, wrapper_->gcm_app_handler()->message().data); |
| 949 EXPECT_TRUE(wrapper_->gcm_app_handler()->message().collapse_key.empty()); |
| 950 EXPECT_EQ(message.sender_id, |
| 951 wrapper_->gcm_app_handler()->message().sender_id); |
| 952 } |
| 953 |
| 954 TEST_F(GCMServiceSingleInstanceTest, MessageWithCollapseKeyReceived) { |
| 955 wrapper_->Register(kTestAppID1, |
| 956 ToSenderList("sender"), |
| 957 TestGCMServiceWrapper::WAIT); |
| 958 GCMClient::IncomingMessage message; |
| 959 message.data["key1"] = "value1"; |
| 960 message.collapse_key = "collapse_key_value"; |
| 961 message.sender_id = "sender"; |
| 962 wrapper_->GetGCMClient()->ReceiveMessage(kTestAppID1, message); |
| 963 wrapper_->gcm_app_handler()->WaitForNotification(); |
| 964 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, |
| 965 wrapper_->gcm_app_handler()->received_event()); |
| 966 EXPECT_EQ(kTestAppID1, wrapper_->gcm_app_handler()->app_id()); |
| 967 EXPECT_EQ(message.data, wrapper_->gcm_app_handler()->message().data); |
| 968 EXPECT_EQ(message.collapse_key, |
| 969 wrapper_->gcm_app_handler()->message().collapse_key); |
| 970 } |
| 971 |
| 972 TEST_F(GCMServiceSingleInstanceTest, MessagesDeleted) { |
| 973 wrapper_->GetGCMClient()->DeleteMessages(kTestAppID1); |
| 974 wrapper_->gcm_app_handler()->WaitForNotification(); |
| 975 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, |
| 976 wrapper_->gcm_app_handler()->received_event()); |
| 977 EXPECT_EQ(kTestAppID1, wrapper_->gcm_app_handler()->app_id()); |
| 978 } |
| 979 |
| 980 // Tests to make sure that concurrent GCMService instances work correctly |
| 981 // regardless how GCMClient is created. |
| 982 class GCMServiceMultipleInstanceTest : public GCMServiceTest { |
| 983 protected: |
| 984 GCMServiceMultipleInstanceTest(); |
| 985 virtual ~GCMServiceMultipleInstanceTest(); |
| 986 |
| 987 // GCMServiceTest: |
| 988 virtual void SetUp() OVERRIDE; |
| 989 virtual void TearDown() OVERRIDE; |
| 990 |
| 991 scoped_ptr<TestGCMServiceWrapper> wrapper2_; |
| 992 |
| 993 private: |
| 994 DISALLOW_COPY_AND_ASSIGN(GCMServiceMultipleInstanceTest); |
| 995 }; |
| 996 |
| 997 GCMServiceMultipleInstanceTest::GCMServiceMultipleInstanceTest() { |
| 998 } |
| 999 |
| 1000 GCMServiceMultipleInstanceTest::~GCMServiceMultipleInstanceTest() { |
| 1001 } |
| 1002 |
| 1003 void GCMServiceMultipleInstanceTest::SetUp() { |
| 1004 GCMServiceTest::SetUp(); |
| 1005 |
| 1006 wrapper2_.reset(new TestGCMServiceWrapper(request_context_)); |
| 1007 |
| 1008 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); |
| 1009 wrapper2_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); |
| 1010 |
| 1011 // Initiate check-in for each instance. |
| 1012 wrapper_->SignIn(kTestAccountID1); |
| 1013 wrapper2_->SignIn(kTestAccountID2); |
| 1014 } |
| 1015 |
| 1016 void GCMServiceMultipleInstanceTest::TearDown() { |
| 1017 wrapper2_.reset(); |
| 1018 } |
| 1019 |
| 1020 TEST_F(GCMServiceMultipleInstanceTest, Register) { |
| 1021 // Register an app. |
| 1022 std::vector<std::string> sender_ids; |
| 1023 sender_ids.push_back("sender1"); |
| 1024 wrapper_->Register(kTestAppID1, sender_ids, TestGCMServiceWrapper::WAIT); |
| 1025 |
| 1026 // Register the same app in a different instance. |
| 1027 std::vector<std::string> sender_ids2; |
| 1028 sender_ids2.push_back("foo"); |
| 1029 sender_ids2.push_back("bar"); |
| 1030 wrapper2_->Register(kTestAppID1, sender_ids2, TestGCMServiceWrapper::WAIT); |
| 1031 |
| 1032 EXPECT_EQ(GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids), |
| 1033 wrapper_->registration_id()); |
| 1034 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); |
| 1035 |
| 1036 EXPECT_EQ(GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids2), |
| 1037 wrapper2_->registration_id()); |
| 1038 EXPECT_EQ(GCMClient::SUCCESS, wrapper2_->registration_result()); |
| 1039 |
| 1040 // Register a different app in a different instance. |
| 1041 std::vector<std::string> sender_ids3; |
| 1042 sender_ids3.push_back("sender1"); |
| 1043 sender_ids3.push_back("sender2"); |
| 1044 sender_ids3.push_back("sender3"); |
| 1045 wrapper2_->Register(kTestAppID2, sender_ids3, TestGCMServiceWrapper::WAIT); |
| 1046 |
| 1047 EXPECT_EQ(GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids3), |
| 1048 wrapper2_->registration_id()); |
| 1049 EXPECT_EQ(GCMClient::SUCCESS, wrapper2_->registration_result()); |
| 1050 } |
| 1051 |
| 1052 TEST_F(GCMServiceMultipleInstanceTest, Send) { |
| 1053 // Send a message from one app in one instance. |
| 1054 GCMClient::OutgoingMessage message; |
| 1055 message.id = "1"; |
| 1056 message.data["key1"] = "value1"; |
| 1057 message.data["key2"] = "value2"; |
| 1058 wrapper_->Send(kTestAppID1, kUserID1, message, TestGCMServiceWrapper::WAIT); |
| 1059 |
| 1060 // Send a message from same app in another instance. |
| 1061 GCMClient::OutgoingMessage message2; |
| 1062 message2.id = "2"; |
| 1063 message2.data["foo"] = "bar"; |
| 1064 wrapper2_->Send(kTestAppID1, kUserID2, message2, TestGCMServiceWrapper::WAIT); |
| 1065 |
| 1066 EXPECT_EQ(message.id, wrapper_->send_message_id()); |
| 1067 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->send_result()); |
| 1068 |
| 1069 EXPECT_EQ(message2.id, wrapper2_->send_message_id()); |
| 1070 EXPECT_EQ(GCMClient::SUCCESS, wrapper2_->send_result()); |
| 1071 |
| 1072 // Send another message from different app in another instance. |
| 1073 GCMClient::OutgoingMessage message3; |
| 1074 message3.id = "3"; |
| 1075 message3.data["hello"] = "world"; |
| 1076 wrapper2_->Send(kTestAppID2, kUserID1, message3, TestGCMServiceWrapper::WAIT); |
| 1077 |
| 1078 EXPECT_EQ(message3.id, wrapper2_->send_message_id()); |
| 1079 EXPECT_EQ(GCMClient::SUCCESS, wrapper2_->send_result()); |
| 1080 } |
| 1081 |
| 1082 TEST_F(GCMServiceMultipleInstanceTest, MessageReceived) { |
| 1083 wrapper_->Register(kTestAppID1, |
| 1084 ToSenderList("sender"), |
| 1085 TestGCMServiceWrapper::WAIT); |
| 1086 wrapper2_->Register(kTestAppID1, |
| 1087 ToSenderList("sender"), |
| 1088 TestGCMServiceWrapper::WAIT); |
| 1089 wrapper2_->Register(kTestAppID2, |
| 1090 ToSenderList("sender2"), |
| 1091 TestGCMServiceWrapper::WAIT); |
| 1092 |
| 1093 // Trigger an incoming message for an app in one instance. |
| 1094 GCMClient::IncomingMessage message; |
| 1095 message.data["key1"] = "value1"; |
| 1096 message.data["key2"] = "value2"; |
| 1097 message.sender_id = "sender"; |
| 1098 wrapper_->GetGCMClient()->ReceiveMessage(kTestAppID1, message); |
| 1099 wrapper_->gcm_app_handler()->WaitForNotification(); |
| 1100 |
| 1101 // Trigger an incoming message for the same app in another instance. |
| 1102 GCMClient::IncomingMessage message2; |
| 1103 message2.data["foo"] = "bar"; |
| 1104 message2.sender_id = "sender"; |
| 1105 wrapper2_->GetGCMClient()->ReceiveMessage(kTestAppID1, message2); |
| 1106 wrapper2_->gcm_app_handler()->WaitForNotification(); |
| 1107 |
| 1108 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, |
| 1109 wrapper_->gcm_app_handler()->received_event()); |
| 1110 EXPECT_EQ(kTestAppID1, wrapper_->gcm_app_handler()->app_id()); |
| 1111 EXPECT_EQ(message.data, wrapper_->gcm_app_handler()->message().data); |
| 1112 EXPECT_EQ("sender", wrapper_->gcm_app_handler()->message().sender_id); |
| 1113 |
| 1114 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, |
| 1115 wrapper2_->gcm_app_handler()->received_event()); |
| 1116 EXPECT_EQ(kTestAppID1, wrapper2_->gcm_app_handler()->app_id()); |
| 1117 EXPECT_EQ(message2.data, wrapper2_->gcm_app_handler()->message().data); |
| 1118 EXPECT_EQ("sender", wrapper2_->gcm_app_handler()->message().sender_id); |
| 1119 |
| 1120 // Trigger another incoming message for a different app in another instance. |
| 1121 GCMClient::IncomingMessage message3; |
| 1122 message3.data["bar1"] = "foo1"; |
| 1123 message3.data["bar2"] = "foo2"; |
| 1124 message3.sender_id = "sender2"; |
| 1125 wrapper2_->GetGCMClient()->ReceiveMessage(kTestAppID2, message3); |
| 1126 wrapper2_->gcm_app_handler()->WaitForNotification(); |
| 1127 |
| 1128 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, |
| 1129 wrapper2_->gcm_app_handler()->received_event()); |
| 1130 EXPECT_EQ(kTestAppID2, wrapper2_->gcm_app_handler()->app_id()); |
| 1131 EXPECT_EQ(message3.data, wrapper2_->gcm_app_handler()->message().data); |
| 1132 EXPECT_EQ("sender2", wrapper2_->gcm_app_handler()->message().sender_id); |
| 1133 } |
| 1134 |
| 1135 // Test a set of GCM operations on multiple instances. |
| 1136 // 1) Register 1 app in instance 1 and register 2 apps in instance 2; |
| 1137 // 2) Send a message from instance 1; |
| 1138 // 3) Receive a message to an app in instance 1 and receive a message for each |
| 1139 // of the two apps in instance 2; |
| 1140 // 4) Send a message for each of the two apps in instance 2; |
| 1141 // 5) Sign out of instance 1. |
| 1142 // 6) Register/send stops working for instance 1; |
| 1143 // 7) The app in instance 2 can still receive these events; |
| 1144 // 8) Sign into instance 1 with a different account. |
| 1145 // 9) The message to the newly signed-in account will be routed. |
| 1146 TEST_F(GCMServiceMultipleInstanceTest, Combined) { |
| 1147 // Register an app. |
| 1148 std::vector<std::string> sender_ids; |
| 1149 sender_ids.push_back("sender1"); |
| 1150 wrapper_->Register(kTestAppID1, sender_ids, TestGCMServiceWrapper::WAIT); |
| 1151 |
| 1152 // Register the same app in a different instance. |
| 1153 std::vector<std::string> sender_ids2; |
| 1154 sender_ids2.push_back("foo"); |
| 1155 sender_ids2.push_back("bar"); |
| 1156 wrapper2_->Register(kTestAppID1, sender_ids2, TestGCMServiceWrapper::WAIT); |
| 1157 |
| 1158 // Register a different app in a different instance. |
| 1159 std::vector<std::string> sender_ids3; |
| 1160 sender_ids3.push_back("sender1"); |
| 1161 sender_ids3.push_back("sender2"); |
| 1162 sender_ids3.push_back("sender3"); |
| 1163 wrapper2_->Register(kTestAppID2, sender_ids3, TestGCMServiceWrapper::WAIT); |
| 1164 |
| 1165 EXPECT_EQ(GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids), |
| 1166 wrapper_->registration_id()); |
| 1167 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); |
| 1168 |
| 1169 EXPECT_EQ(GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids3), |
| 1170 wrapper2_->registration_id()); |
| 1171 EXPECT_EQ(GCMClient::SUCCESS, wrapper2_->registration_result()); |
| 1172 |
| 1173 // Send a message from one instance. |
| 1174 GCMClient::OutgoingMessage out_message; |
| 1175 out_message.id = "1"; |
| 1176 out_message.data["out1"] = "out_data1"; |
| 1177 out_message.data["out1_2"] = "out_data1_2"; |
| 1178 wrapper_->Send(kTestAppID1, |
| 1179 kUserID1, |
| 1180 out_message, |
| 1181 TestGCMServiceWrapper::WAIT); |
| 1182 |
| 1183 EXPECT_EQ(out_message.id, wrapper_->send_message_id()); |
| 1184 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->send_result()); |
| 1185 |
| 1186 // Trigger an incoming message for an app in one instance. |
| 1187 GCMClient::IncomingMessage in_message; |
| 1188 in_message.data["in1"] = "in_data1"; |
| 1189 in_message.data["in1_2"] = "in_data1_2"; |
| 1190 in_message.sender_id = "sender1"; |
| 1191 wrapper_->GetGCMClient()->ReceiveMessage(kTestAppID1, in_message); |
| 1192 wrapper_->gcm_app_handler()->WaitForNotification(); |
| 1193 |
| 1194 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, |
| 1195 wrapper_->gcm_app_handler()->received_event()); |
| 1196 EXPECT_EQ(kTestAppID1, wrapper_->gcm_app_handler()->app_id()); |
| 1197 EXPECT_EQ(in_message.data, wrapper_->gcm_app_handler()->message().data); |
| 1198 |
| 1199 // Trigger 2 incoming messages, one for each app respectively, in another |
| 1200 // instance. |
| 1201 GCMClient::IncomingMessage in_message2; |
| 1202 in_message2.data["in2"] = "in_data2"; |
| 1203 in_message2.sender_id = "sender3"; |
| 1204 wrapper2_->GetGCMClient()->ReceiveMessage(kTestAppID2, in_message2); |
| 1205 |
| 1206 GCMClient::IncomingMessage in_message3; |
| 1207 in_message3.data["in3"] = "in_data3"; |
| 1208 in_message3.data["in3_2"] = "in_data3_2"; |
| 1209 in_message3.sender_id = "foo"; |
| 1210 wrapper2_->GetGCMClient()->ReceiveMessage(kTestAppID1, in_message3); |
| 1211 |
| 1212 wrapper2_->gcm_app_handler()->WaitForNotification(); |
| 1213 |
| 1214 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, |
| 1215 wrapper2_->gcm_app_handler()->received_event()); |
| 1216 EXPECT_EQ(kTestAppID2, wrapper2_->gcm_app_handler()->app_id()); |
| 1217 EXPECT_EQ(in_message2.data, wrapper2_->gcm_app_handler()->message().data); |
| 1218 |
| 1219 wrapper2_->gcm_app_handler()->WaitForNotification(); |
| 1220 |
| 1221 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, |
| 1222 wrapper2_->gcm_app_handler()->received_event()); |
| 1223 EXPECT_EQ(kTestAppID1, wrapper2_->gcm_app_handler()->app_id()); |
| 1224 EXPECT_EQ(in_message3.data, wrapper2_->gcm_app_handler()->message().data); |
| 1225 |
| 1226 // Send two messages, one for each app respectively, from another instance. |
| 1227 GCMClient::OutgoingMessage out_message2; |
| 1228 out_message2.id = "2"; |
| 1229 out_message2.data["out2"] = "out_data2"; |
| 1230 wrapper2_->Send(kTestAppID1, |
| 1231 kUserID2, |
| 1232 out_message2, |
| 1233 TestGCMServiceWrapper::WAIT); |
| 1234 |
| 1235 GCMClient::OutgoingMessage out_message3; |
| 1236 out_message3.id = "3"; |
| 1237 out_message3.data["out3"] = "out_data3"; |
| 1238 wrapper2_->Send(kTestAppID2, |
| 1239 kUserID2, |
| 1240 out_message3, |
| 1241 TestGCMServiceWrapper::DO_NOT_WAIT); |
| 1242 |
| 1243 EXPECT_EQ(out_message2.id, wrapper2_->send_message_id()); |
| 1244 EXPECT_EQ(GCMClient::SUCCESS, wrapper2_->send_result()); |
| 1245 |
| 1246 wrapper2_->WaitForAsyncOperation(); |
| 1247 |
| 1248 EXPECT_EQ(out_message3.id, wrapper2_->send_message_id()); |
| 1249 EXPECT_EQ(GCMClient::SUCCESS, wrapper2_->send_result()); |
| 1250 |
| 1251 // Sign out of one instance. |
| 1252 wrapper_->SignOut(); |
| 1253 |
| 1254 // Register/send stops working for signed-out instance. |
| 1255 wrapper_->Register(kTestAppID1, sender_ids, TestGCMServiceWrapper::WAIT); |
| 1256 EXPECT_TRUE(wrapper_->registration_id().empty()); |
| 1257 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, wrapper_->registration_result()); |
| 1258 |
| 1259 wrapper_->Send(kTestAppID2, |
| 1260 kUserID2, |
| 1261 out_message3, |
| 1262 TestGCMServiceWrapper::WAIT); |
| 1263 EXPECT_TRUE(wrapper_->send_message_id().empty()); |
| 1264 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, wrapper_->send_result()); |
| 1265 |
| 1266 // Deleted messages event will go through for another signed-in instance. |
| 1267 wrapper2_->GetGCMClient()->DeleteMessages(kTestAppID2); |
| 1268 wrapper2_->gcm_app_handler()->WaitForNotification(); |
| 1269 |
| 1270 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, |
| 1271 wrapper2_->gcm_app_handler()->received_event()); |
| 1272 EXPECT_EQ(kTestAppID2, wrapper2_->gcm_app_handler()->app_id()); |
| 1273 |
| 1274 // Send error event will go through for another signed-in instance. |
| 1275 GCMClient::OutgoingMessage out_message4; |
| 1276 out_message4.id = "1@error"; |
| 1277 out_message4.data["out4"] = "out_data4"; |
| 1278 wrapper2_->Send(kTestAppID1, |
| 1279 kUserID1, |
| 1280 out_message4, |
| 1281 TestGCMServiceWrapper::WAIT); |
| 1282 |
| 1283 EXPECT_EQ(out_message4.id, wrapper2_->send_message_id()); |
| 1284 EXPECT_EQ(GCMClient::SUCCESS, wrapper2_->send_result()); |
| 1285 |
| 1286 wrapper2_->gcm_app_handler()->WaitForNotification(); |
| 1287 EXPECT_EQ(FakeGCMAppHandler::SEND_ERROR_EVENT, |
| 1288 wrapper2_->gcm_app_handler()->received_event()); |
| 1289 EXPECT_EQ(kTestAppID1, wrapper2_->gcm_app_handler()->app_id()); |
| 1290 EXPECT_EQ(out_message4.id, |
| 1291 wrapper2_->gcm_app_handler()->send_error_details().message_id); |
| 1292 EXPECT_NE(GCMClient::SUCCESS, |
| 1293 wrapper2_->gcm_app_handler()->send_error_details().result); |
| 1294 EXPECT_EQ(out_message4.data, |
| 1295 wrapper2_->gcm_app_handler()->send_error_details().additional_data); |
| 1296 |
| 1297 // Sign in with a different account. |
| 1298 wrapper_->SignIn(kTestAccountID3); |
| 1299 |
| 1300 // Signing out cleared all registrations, so we need to register again. |
| 1301 wrapper_->Register(kTestAppID1, |
| 1302 ToSenderList("sender1"), |
| 1303 TestGCMServiceWrapper::WAIT); |
| 1304 |
| 1305 // Incoming message will go through for the new signed-in account. |
| 1306 GCMClient::IncomingMessage in_message5; |
| 1307 in_message5.data["in5"] = "in_data5"; |
| 1308 in_message5.sender_id = "sender1"; |
| 1309 wrapper_->GetGCMClient()->ReceiveMessage(kTestAppID1, in_message5); |
| 1310 |
| 1311 wrapper_->gcm_app_handler()->WaitForNotification(); |
| 1312 |
| 1313 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, |
| 1314 wrapper_->gcm_app_handler()->received_event()); |
| 1315 EXPECT_EQ(in_message5.data, wrapper_->gcm_app_handler()->message().data); |
| 1316 } |
| 1317 |
| 1318 } // namespace gcm |
OLD | NEW |