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