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" |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 scoped_ptr<base::RunLoop> run_loop_; | 94 scoped_ptr<base::RunLoop> run_loop_; |
95 | 95 |
96 Event received_event_; | 96 Event received_event_; |
97 std::string app_id_; | 97 std::string app_id_; |
98 GCMClient::IncomingMessage message_; | 98 GCMClient::IncomingMessage message_; |
99 GCMClient::SendErrorDetails send_error_details_; | 99 GCMClient::SendErrorDetails send_error_details_; |
100 | 100 |
101 DISALLOW_COPY_AND_ASSIGN(FakeGCMAppHandler); | 101 DISALLOW_COPY_AND_ASSIGN(FakeGCMAppHandler); |
102 }; | 102 }; |
103 | 103 |
104 class TestGCMDriver : public GCMDriver { | |
105 public: | |
106 TestGCMDriver( | |
107 bool start_automatically, | |
108 scoped_ptr<IdentityProvider> identity_provider, | |
109 const scoped_refptr<net::URLRequestContextGetter>& request_context); | |
110 virtual ~TestGCMDriver(); | |
111 | |
112 protected: | |
113 // GCMDriver: | |
114 virtual bool ShouldStartAutomatically() const OVERRIDE; | |
115 virtual base::FilePath GetStorePath() const OVERRIDE; | |
116 virtual scoped_refptr<net::URLRequestContextGetter> | |
117 GetURLRequestContextGetter() const OVERRIDE; | |
118 | |
119 private: | |
120 base::ScopedTempDir temp_dir_; | |
121 scoped_refptr<net::URLRequestContextGetter> request_context_; | |
122 const bool start_automatically_; | |
123 | |
124 DISALLOW_COPY_AND_ASSIGN(TestGCMDriver); | |
125 }; | |
126 | |
127 FakeGCMAppHandler::FakeGCMAppHandler() : received_event_(NO_EVENT) { | 104 FakeGCMAppHandler::FakeGCMAppHandler() : received_event_(NO_EVENT) { |
128 } | 105 } |
129 | 106 |
130 FakeGCMAppHandler::~FakeGCMAppHandler() { | 107 FakeGCMAppHandler::~FakeGCMAppHandler() { |
131 } | 108 } |
132 | 109 |
133 void FakeGCMAppHandler::WaitForNotification() { | 110 void FakeGCMAppHandler::WaitForNotification() { |
134 run_loop_.reset(new base::RunLoop); | 111 run_loop_.reset(new base::RunLoop); |
135 run_loop_->Run(); | 112 run_loop_->Run(); |
136 run_loop_.reset(); | 113 run_loop_.reset(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 run_loop_->Quit(); | 145 run_loop_->Quit(); |
169 } | 146 } |
170 | 147 |
171 void FakeGCMAppHandler::ClearResults() { | 148 void FakeGCMAppHandler::ClearResults() { |
172 received_event_ = NO_EVENT; | 149 received_event_ = NO_EVENT; |
173 app_id_.clear(); | 150 app_id_.clear(); |
174 message_ = GCMClient::IncomingMessage(); | 151 message_ = GCMClient::IncomingMessage(); |
175 send_error_details_ = GCMClient::SendErrorDetails(); | 152 send_error_details_ = GCMClient::SendErrorDetails(); |
176 } | 153 } |
177 | 154 |
178 TestGCMDriver::TestGCMDriver( | |
179 bool start_automatically, | |
180 scoped_ptr<IdentityProvider> identity_provider, | |
181 const scoped_refptr<net::URLRequestContextGetter>& request_context) | |
182 : GCMDriver(identity_provider.Pass()), | |
183 request_context_(request_context), | |
184 start_automatically_(start_automatically) { | |
185 if (!temp_dir_.CreateUniqueTempDir()) | |
186 ADD_FAILURE(); | |
187 } | |
188 | |
189 TestGCMDriver::~TestGCMDriver() { | |
190 } | |
191 | |
192 bool TestGCMDriver::ShouldStartAutomatically() const { | |
193 return start_automatically_; | |
194 } | |
195 | |
196 base::FilePath TestGCMDriver::GetStorePath() const { | |
197 return temp_dir_.path(); | |
198 } | |
199 | |
200 scoped_refptr<net::URLRequestContextGetter> | |
201 TestGCMDriver::GetURLRequestContextGetter() const { | |
202 return request_context_; | |
203 } | |
204 | |
205 } // namespace | 155 } // namespace |
206 | 156 |
207 class GCMDriverTest : public testing::Test { | 157 class GCMDriverTest : public testing::Test { |
208 public: | 158 public: |
209 enum WaitToFinish { | 159 enum WaitToFinish { |
210 DO_NOT_WAIT, | 160 DO_NOT_WAIT, |
211 WAIT | 161 WAIT |
212 }; | 162 }; |
213 | 163 |
214 GCMDriverTest(); | 164 GCMDriverTest(); |
215 virtual ~GCMDriverTest(); | 165 virtual ~GCMDriverTest(); |
216 | 166 |
217 // testing::Test: | 167 // testing::Test: |
218 virtual void SetUp() OVERRIDE; | 168 virtual void SetUp() OVERRIDE; |
219 virtual void TearDown() OVERRIDE; | 169 virtual void TearDown() OVERRIDE; |
220 | 170 |
221 TestGCMDriver* driver() { return driver_.get(); } | 171 GCMDriver* driver() { return driver_.get(); } |
222 FakeGCMAppHandler* gcm_app_handler() { return gcm_app_handler_.get(); } | 172 FakeGCMAppHandler* gcm_app_handler() { return gcm_app_handler_.get(); } |
223 const std::string& registration_id() const { return registration_id_; } | 173 const std::string& registration_id() const { return registration_id_; } |
224 GCMClient::Result registration_result() const { return registration_result_; } | 174 GCMClient::Result registration_result() const { return registration_result_; } |
225 const std::string& send_message_id() const { return send_message_id_; } | 175 const std::string& send_message_id() const { return send_message_id_; } |
226 GCMClient::Result send_result() const { return send_result_; } | 176 GCMClient::Result send_result() const { return send_result_; } |
227 GCMClient::Result unregistration_result() const { | 177 GCMClient::Result unregistration_result() const { |
228 return unregistration_result_; | 178 return unregistration_result_; |
229 } | 179 } |
230 | 180 |
231 void ClearRegistrationResult(); | 181 void ClearResults(); |
232 void ClearUnregistrationResult(); | |
233 | 182 |
234 bool HasAppHandlers() const; | 183 bool HasAppHandlers() const; |
235 FakeGCMClient* GetGCMClient(); | 184 FakeGCMClient* GetGCMClient(); |
236 | 185 |
237 void CreateDriver(bool start_automatically, | 186 void CreateDriver(FakeGCMClient::StartMode gcm_client_start_mode); |
238 FakeGCMClient::StartMode gcm_client_start_mode); | |
239 | 187 |
240 void SignIn(const std::string& account_id); | 188 void SignIn(const std::string& account_id); |
241 void SignOut(); | 189 void SignOut(); |
242 | 190 |
243 void Register(const std::string& app_id, | 191 void Register(const std::string& app_id, |
244 const std::vector<std::string>& sender_ids, | 192 const std::vector<std::string>& sender_ids, |
245 WaitToFinish wait_to_finish); | 193 WaitToFinish wait_to_finish); |
246 void Send(const std::string& app_id, | 194 void Send(const std::string& app_id, |
247 const std::string& receiver_id, | 195 const std::string& receiver_id, |
248 const GCMClient::OutgoingMessage& message, | 196 const GCMClient::OutgoingMessage& message, |
249 WaitToFinish wait_to_finish); | 197 WaitToFinish wait_to_finish); |
250 void Unregister(const std::string& app_id, WaitToFinish wait_to_finish); | 198 void Unregister(const std::string& app_id, WaitToFinish wait_to_finish); |
251 | 199 |
252 void WaitForAsyncOperation(); | 200 void WaitForAsyncOperation(); |
253 | 201 |
254 private: | 202 private: |
255 void RegisterCompleted(const std::string& registration_id, | 203 void RegisterCompleted(const std::string& registration_id, |
256 GCMClient::Result result); | 204 GCMClient::Result result); |
257 void SendCompleted(const std::string& message_id, GCMClient::Result result); | 205 void SendCompleted(const std::string& message_id, GCMClient::Result result); |
258 void UnregisterCompleted(GCMClient::Result result); | 206 void UnregisterCompleted(GCMClient::Result result); |
259 | 207 |
260 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_; | 208 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_; |
| 209 base::ScopedTempDir temp_dir_; |
261 FakeOAuth2TokenService token_service_; | 210 FakeOAuth2TokenService token_service_; |
262 scoped_ptr<FakeIdentityProvider> identity_provider_owner_; | 211 scoped_ptr<FakeIdentityProvider> identity_provider_owner_; |
263 FakeIdentityProvider* identity_provider_; | 212 FakeIdentityProvider* identity_provider_; |
264 scoped_ptr<TestGCMDriver> driver_; | 213 scoped_ptr<GCMDriver> driver_; |
265 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; | 214 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; |
266 | 215 |
267 base::Closure async_operation_completed_callback_; | 216 base::Closure async_operation_completed_callback_; |
268 | 217 |
269 std::string registration_id_; | 218 std::string registration_id_; |
270 GCMClient::Result registration_result_; | 219 GCMClient::Result registration_result_; |
271 std::string send_message_id_; | 220 std::string send_message_id_; |
272 GCMClient::Result send_result_; | 221 GCMClient::Result send_result_; |
273 GCMClient::Result unregistration_result_; | 222 GCMClient::Result unregistration_result_; |
274 | 223 |
275 DISALLOW_COPY_AND_ASSIGN(GCMDriverTest); | 224 DISALLOW_COPY_AND_ASSIGN(GCMDriverTest); |
276 }; | 225 }; |
277 | 226 |
278 GCMDriverTest::GCMDriverTest() | 227 GCMDriverTest::GCMDriverTest() |
279 : identity_provider_(NULL), | 228 : identity_provider_(NULL), |
280 registration_result_(GCMClient::UNKNOWN_ERROR), | 229 registration_result_(GCMClient::UNKNOWN_ERROR), |
281 send_result_(GCMClient::UNKNOWN_ERROR), | 230 send_result_(GCMClient::UNKNOWN_ERROR), |
282 unregistration_result_(GCMClient::UNKNOWN_ERROR) { | 231 unregistration_result_(GCMClient::UNKNOWN_ERROR) { |
283 identity_provider_owner_.reset(new FakeIdentityProvider(&token_service_)); | 232 identity_provider_owner_.reset(new FakeIdentityProvider(&token_service_)); |
284 identity_provider_ = identity_provider_owner_.get(); | 233 identity_provider_ = identity_provider_owner_.get(); |
285 } | 234 } |
286 | 235 |
287 GCMDriverTest::~GCMDriverTest() { | 236 GCMDriverTest::~GCMDriverTest() { |
288 } | 237 } |
289 | 238 |
290 void GCMDriverTest::SetUp() { | 239 void GCMDriverTest::SetUp() { |
291 thread_bundle_.reset(new content::TestBrowserThreadBundle( | 240 thread_bundle_.reset(new content::TestBrowserThreadBundle( |
292 content::TestBrowserThreadBundle::REAL_IO_THREAD)); | 241 content::TestBrowserThreadBundle::REAL_IO_THREAD)); |
| 242 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
293 } | 243 } |
294 | 244 |
295 void GCMDriverTest::TearDown() { | 245 void GCMDriverTest::TearDown() { |
296 if (!driver_) | 246 if (!driver_) |
297 return; | 247 return; |
298 | 248 |
299 driver_->ShutdownService(); | 249 driver_->Shutdown(); |
300 driver_.reset(); | 250 driver_.reset(); |
301 PumpIOLoop(); | 251 PumpIOLoop(); |
302 } | 252 } |
303 | 253 |
304 void GCMDriverTest::ClearRegistrationResult() { | 254 void GCMDriverTest::ClearResults() { |
305 registration_id_.clear(); | 255 registration_id_.clear(); |
306 registration_result_ = GCMClient::UNKNOWN_ERROR; | 256 registration_result_ = GCMClient::UNKNOWN_ERROR; |
307 } | |
308 | 257 |
309 void GCMDriverTest::ClearUnregistrationResult() { | 258 send_message_id_.clear(); |
| 259 send_result_ = GCMClient::UNKNOWN_ERROR; |
| 260 |
310 unregistration_result_ = GCMClient::UNKNOWN_ERROR; | 261 unregistration_result_ = GCMClient::UNKNOWN_ERROR; |
311 } | 262 } |
312 | 263 |
313 bool GCMDriverTest::HasAppHandlers() const { | 264 bool GCMDriverTest::HasAppHandlers() const { |
314 return !driver_->app_handlers().empty(); | 265 return !driver_->app_handlers().empty(); |
315 } | 266 } |
316 | 267 |
317 FakeGCMClient* GCMDriverTest::GetGCMClient() { | 268 FakeGCMClient* GCMDriverTest::GetGCMClient() { |
318 return static_cast<FakeGCMClient*>(driver_->GetGCMClientForTesting()); | 269 return static_cast<FakeGCMClient*>(driver_->GetGCMClientForTesting()); |
319 } | 270 } |
320 | 271 |
321 void GCMDriverTest::CreateDriver( | 272 void GCMDriverTest::CreateDriver( |
322 bool start_automatically, | |
323 FakeGCMClient::StartMode gcm_client_start_mode) { | 273 FakeGCMClient::StartMode gcm_client_start_mode) { |
324 scoped_refptr<net::URLRequestContextGetter> request_context = | 274 scoped_refptr<net::URLRequestContextGetter> request_context = |
325 new net::TestURLRequestContextGetter( | 275 new net::TestURLRequestContextGetter( |
326 content::BrowserThread::GetMessageLoopProxyForThread( | 276 content::BrowserThread::GetMessageLoopProxyForThread( |
327 content::BrowserThread::IO)); | 277 content::BrowserThread::IO)); |
328 driver_.reset(new TestGCMDriver( | 278 driver_.reset(new GCMDriver( |
329 start_automatically, | 279 scoped_ptr<GCMClientFactory>(new FakeGCMClientFactory( |
| 280 gcm_client_start_mode)).Pass(), |
330 identity_provider_owner_.PassAs<IdentityProvider>(), | 281 identity_provider_owner_.PassAs<IdentityProvider>(), |
| 282 temp_dir_.path(), |
331 request_context)); | 283 request_context)); |
332 driver_->Initialize(scoped_ptr<GCMClientFactory>( | |
333 new FakeGCMClientFactory(gcm_client_start_mode))); | |
334 | 284 |
335 gcm_app_handler_.reset(new FakeGCMAppHandler); | 285 gcm_app_handler_.reset(new FakeGCMAppHandler); |
336 driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get()); | 286 driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get()); |
337 driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get()); | 287 driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get()); |
338 } | 288 } |
339 | 289 |
340 void GCMDriverTest::SignIn(const std::string& account_id) { | 290 void GCMDriverTest::SignIn(const std::string& account_id) { |
341 token_service_.AddAccount(account_id); | 291 token_service_.AddAccount(account_id); |
342 identity_provider_->LogIn(account_id); | 292 identity_provider_->LogIn(account_id); |
343 PumpIOLoop(); | 293 PumpIOLoop(); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 } | 362 } |
413 | 363 |
414 void GCMDriverTest::UnregisterCompleted(GCMClient::Result result) { | 364 void GCMDriverTest::UnregisterCompleted(GCMClient::Result result) { |
415 unregistration_result_ = result; | 365 unregistration_result_ = result; |
416 if (!async_operation_completed_callback_.is_null()) | 366 if (!async_operation_completed_callback_.is_null()) |
417 async_operation_completed_callback_.Run(); | 367 async_operation_completed_callback_.Run(); |
418 } | 368 } |
419 | 369 |
420 TEST_F(GCMDriverTest, CreateGCMDriverBeforeSignIn) { | 370 TEST_F(GCMDriverTest, CreateGCMDriverBeforeSignIn) { |
421 // Create CreateGMCService first. | 371 // Create CreateGMCService first. |
422 CreateDriver(true, FakeGCMClient::NO_DELAY_START); | 372 CreateDriver(FakeGCMClient::NO_DELAY_START); |
423 EXPECT_FALSE(driver()->IsStarted()); | 373 EXPECT_FALSE(driver()->IsStarted()); |
424 | 374 |
425 // Sign in. This will kick off the check-in. | 375 // Sign in. This will kick off the check-in. |
426 SignIn(kTestAccountID1); | 376 SignIn(kTestAccountID1); |
427 EXPECT_TRUE(driver()->IsStarted()); | 377 EXPECT_TRUE(driver()->IsStarted()); |
428 } | 378 } |
429 | 379 |
430 TEST_F(GCMDriverTest, CreateGCMDriverAfterSignIn) { | 380 TEST_F(GCMDriverTest, CreateGCMDriverAfterSignIn) { |
431 // Sign in. This will not initiate the check-in. | 381 // Sign in. This will not initiate the check-in. |
432 SignIn(kTestAccountID1); | 382 SignIn(kTestAccountID1); |
433 | 383 |
434 // Create GCMeService after sign-in. | 384 // Create GCMeService after sign-in. |
435 CreateDriver(true, FakeGCMClient::NO_DELAY_START); | 385 CreateDriver(FakeGCMClient::NO_DELAY_START); |
436 EXPECT_TRUE(driver()->IsStarted()); | 386 EXPECT_TRUE(driver()->IsStarted()); |
437 } | 387 } |
438 | 388 |
439 TEST_F(GCMDriverTest, Shutdown) { | 389 TEST_F(GCMDriverTest, Shutdown) { |
440 CreateDriver(true, FakeGCMClient::NO_DELAY_START); | 390 CreateDriver(FakeGCMClient::NO_DELAY_START); |
441 EXPECT_TRUE(HasAppHandlers()); | 391 EXPECT_TRUE(HasAppHandlers()); |
442 | 392 |
443 driver()->ShutdownService(); | 393 driver()->Shutdown(); |
444 EXPECT_FALSE(HasAppHandlers()); | 394 EXPECT_FALSE(HasAppHandlers()); |
445 } | 395 } |
446 | 396 |
447 TEST_F(GCMDriverTest, SignInAndSignOutUnderPositiveChannelSignal) { | 397 TEST_F(GCMDriverTest, SignInAndSignOutUnderPositiveChannelSignal) { |
448 CreateDriver(true, FakeGCMClient::NO_DELAY_START); | 398 CreateDriver(FakeGCMClient::NO_DELAY_START); |
449 SignIn(kTestAccountID1); | 399 SignIn(kTestAccountID1); |
450 | 400 |
451 // GCMClient should be loaded. | 401 // GCMClient should be loaded. |
452 EXPECT_TRUE(driver()->IsGCMClientReady()); | 402 EXPECT_TRUE(driver()->IsGCMClientReady()); |
453 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); | 403 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); |
454 | 404 |
455 SignOut(); | 405 SignOut(); |
456 | 406 |
457 // GCMClient should be checked out. | 407 // GCMClient should be checked out. |
458 EXPECT_FALSE(driver()->IsGCMClientReady()); | 408 EXPECT_FALSE(driver()->IsGCMClientReady()); |
459 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); | 409 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); |
460 } | 410 } |
461 | 411 |
462 TEST_F(GCMDriverTest, SignInAndSignOutUnderNonPositiveChannelSignal) { | |
463 // Non-positive channel signal will prevent GCMClient from checking in during | |
464 // sign-in. | |
465 CreateDriver(false, FakeGCMClient::NO_DELAY_START); | |
466 SignIn(kTestAccountID1); | |
467 | |
468 // GCMClient should not be loaded. | |
469 EXPECT_FALSE(driver()->IsGCMClientReady()); | |
470 EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status()); | |
471 | |
472 SignOut(); | |
473 | |
474 // Check-out should still be performed. | |
475 EXPECT_FALSE(driver()->IsGCMClientReady()); | |
476 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); | |
477 } | |
478 | |
479 TEST_F(GCMDriverTest, SignOutAndThenSignIn) { | 412 TEST_F(GCMDriverTest, SignOutAndThenSignIn) { |
480 CreateDriver(true, FakeGCMClient::NO_DELAY_START); | 413 CreateDriver(FakeGCMClient::NO_DELAY_START); |
481 SignIn(kTestAccountID1); | 414 SignIn(kTestAccountID1); |
482 | 415 |
483 // GCMClient should be loaded. | 416 // GCMClient should be loaded. |
484 EXPECT_TRUE(driver()->IsGCMClientReady()); | 417 EXPECT_TRUE(driver()->IsGCMClientReady()); |
485 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); | 418 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); |
486 | 419 |
487 SignOut(); | 420 SignOut(); |
488 | 421 |
489 // GCMClient should be checked out. | 422 // GCMClient should be checked out. |
490 EXPECT_FALSE(driver()->IsGCMClientReady()); | 423 EXPECT_FALSE(driver()->IsGCMClientReady()); |
491 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); | 424 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); |
492 | 425 |
493 // Sign-in with a different account. | 426 // Sign-in with a different account. |
494 SignIn(kTestAccountID2); | 427 SignIn(kTestAccountID2); |
495 | 428 |
496 // GCMClient should be loaded again. | 429 // GCMClient should be loaded again. |
497 EXPECT_TRUE(driver()->IsGCMClientReady()); | 430 EXPECT_TRUE(driver()->IsGCMClientReady()); |
498 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); | 431 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); |
499 } | 432 } |
500 | 433 |
501 TEST_F(GCMDriverTest, StopAndRestartGCM) { | 434 TEST_F(GCMDriverTest, DisableAndReenableGCM) { |
502 CreateDriver(true, FakeGCMClient::NO_DELAY_START); | 435 CreateDriver(FakeGCMClient::NO_DELAY_START); |
503 SignIn(kTestAccountID1); | 436 SignIn(kTestAccountID1); |
504 | 437 |
505 // GCMClient should be loaded. | 438 // GCMClient should be started. |
506 EXPECT_TRUE(driver()->IsGCMClientReady()); | 439 EXPECT_TRUE(driver()->IsGCMClientReady()); |
507 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); | 440 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); |
508 | 441 |
509 // Stops the GCM. | 442 // Disables the GCM. |
510 driver()->Stop(); | 443 driver()->Disable(); |
511 PumpIOLoop(); | 444 PumpIOLoop(); |
512 PumpUILoop(); | 445 PumpUILoop(); |
513 | 446 |
514 // GCMClient should be stopped. | 447 // GCMClient should be stopped. |
515 EXPECT_FALSE(driver()->IsGCMClientReady()); | 448 EXPECT_FALSE(driver()->IsGCMClientReady()); |
516 EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status()); | 449 EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status()); |
517 | 450 |
518 // Restarts the GCM. | 451 // Enables the GCM. |
519 driver()->Start(); | 452 driver()->Enable(); |
520 PumpIOLoop(); | 453 PumpIOLoop(); |
521 PumpUILoop(); | 454 PumpUILoop(); |
522 | 455 |
523 // GCMClient should be loaded. | 456 // GCMClient should be started. |
524 EXPECT_TRUE(driver()->IsGCMClientReady()); | 457 EXPECT_TRUE(driver()->IsGCMClientReady()); |
525 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); | 458 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); |
526 | 459 |
527 // Stops the GCM. | 460 // Disables the GCM. |
528 driver()->Stop(); | 461 driver()->Disable(); |
529 PumpIOLoop(); | 462 PumpIOLoop(); |
530 PumpUILoop(); | 463 PumpUILoop(); |
531 | 464 |
532 // GCMClient should be stopped. | 465 // GCMClient should be stopped. |
533 EXPECT_FALSE(driver()->IsGCMClientReady()); | 466 EXPECT_FALSE(driver()->IsGCMClientReady()); |
534 EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status()); | 467 EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status()); |
535 | 468 |
536 // Sign out. | 469 // Sign out. |
537 SignOut(); | 470 SignOut(); |
538 | 471 |
539 // GCMClient should be checked out. | 472 // GCMClient should be checked out. |
540 EXPECT_FALSE(driver()->IsGCMClientReady()); | 473 EXPECT_FALSE(driver()->IsGCMClientReady()); |
541 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); | 474 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status()); |
542 } | 475 } |
543 | 476 |
544 TEST_F(GCMDriverTest, RegisterWhenNotSignedIn) { | 477 TEST_F(GCMDriverTest, RegisterFailed) { |
545 CreateDriver(true, FakeGCMClient::NO_DELAY_START); | |
546 | |
547 std::vector<std::string> sender_ids; | 478 std::vector<std::string> sender_ids; |
548 sender_ids.push_back("sender1"); | 479 sender_ids.push_back("sender1"); |
| 480 |
| 481 CreateDriver(FakeGCMClient::NO_DELAY_START); |
| 482 |
| 483 // Registration fails when GCM is disabled. |
| 484 driver()->Disable(); |
549 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | 485 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); |
| 486 EXPECT_TRUE(registration_id().empty()); |
| 487 EXPECT_EQ(GCMClient::GCM_DISABLED, registration_result()); |
550 | 488 |
| 489 ClearResults(); |
| 490 |
| 491 // Registration fails when the sign-in does not occur. |
| 492 driver()->Enable(); |
| 493 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); |
551 EXPECT_TRUE(registration_id().empty()); | 494 EXPECT_TRUE(registration_id().empty()); |
552 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, registration_result()); | 495 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, registration_result()); |
553 } | 496 } |
554 | 497 |
555 TEST_F(GCMDriverTest, RegisterUnderNonPositiveChannelSignal) { | 498 TEST_F(GCMDriverTest, UnregisterFailed) { |
556 // Non-positive channel signal will prevent GCMClient from checking in during | 499 CreateDriver(FakeGCMClient::NO_DELAY_START); |
557 // sign-in. | |
558 CreateDriver(false, FakeGCMClient::NO_DELAY_START); | |
559 SignIn(kTestAccountID1); | |
560 | 500 |
561 // GCMClient should not be checked in. | 501 // Unregistration fails when GCM is disabled. |
562 EXPECT_FALSE(driver()->IsGCMClientReady()); | 502 driver()->Disable(); |
563 EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status()); | 503 Unregister(kTestAppID1, GCMDriverTest::WAIT); |
| 504 EXPECT_EQ(GCMClient::GCM_DISABLED, unregistration_result()); |
564 | 505 |
565 // Invoking register will make GCMClient checked in. | 506 ClearResults(); |
566 std::vector<std::string> sender_ids; | |
567 sender_ids.push_back("sender1"); | |
568 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | |
569 | 507 |
570 // GCMClient should be checked in. | 508 // Unregistration fails when the sign-in does not occur. |
571 EXPECT_TRUE(driver()->IsGCMClientReady()); | 509 driver()->Enable(); |
572 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); | 510 Unregister(kTestAppID1, GCMDriverTest::WAIT); |
573 | 511 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, unregistration_result()); |
574 // Registration should succeed. | |
575 const std::string expected_registration_id = | |
576 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids); | |
577 EXPECT_EQ(expected_registration_id, registration_id()); | |
578 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | |
579 } | 512 } |
580 | 513 |
581 TEST_F(GCMDriverTest, SendWhenNotSignedIn) { | 514 TEST_F(GCMDriverTest, SendFailed) { |
582 CreateDriver(true, FakeGCMClient::NO_DELAY_START); | |
583 | |
584 GCMClient::OutgoingMessage message; | 515 GCMClient::OutgoingMessage message; |
585 message.id = "1"; | 516 message.id = "1"; |
586 message.data["key1"] = "value1"; | 517 message.data["key1"] = "value1"; |
| 518 |
| 519 CreateDriver(FakeGCMClient::NO_DELAY_START); |
| 520 |
| 521 // Sending fails when GCM is disabled. |
| 522 driver()->Disable(); |
587 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); | 523 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); |
| 524 EXPECT_TRUE(send_message_id().empty()); |
| 525 EXPECT_EQ(GCMClient::GCM_DISABLED, send_result()); |
588 | 526 |
| 527 ClearResults(); |
| 528 |
| 529 // Registration fails when the sign-in does not occur. |
| 530 driver()->Enable(); |
| 531 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); |
589 EXPECT_TRUE(send_message_id().empty()); | 532 EXPECT_TRUE(send_message_id().empty()); |
590 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, send_result()); | 533 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, send_result()); |
591 } | 534 } |
592 | 535 |
593 TEST_F(GCMDriverTest, SendUnderNonPositiveChannelSignal) { | |
594 // Non-positive channel signal will prevent GCMClient from checking in during | |
595 // sign-in. | |
596 CreateDriver(false, FakeGCMClient::NO_DELAY_START); | |
597 SignIn(kTestAccountID1); | |
598 | |
599 // GCMClient should not be checked in. | |
600 EXPECT_FALSE(driver()->IsGCMClientReady()); | |
601 EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status()); | |
602 | |
603 // Invoking send will make GCMClient checked in. | |
604 GCMClient::OutgoingMessage message; | |
605 message.id = "1"; | |
606 message.data["key1"] = "value1"; | |
607 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); | |
608 | |
609 // GCMClient should be checked in. | |
610 EXPECT_TRUE(driver()->IsGCMClientReady()); | |
611 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); | |
612 | |
613 // Sending should succeed. | |
614 EXPECT_EQ(message.id, send_message_id()); | |
615 EXPECT_EQ(GCMClient::SUCCESS, send_result()); | |
616 } | |
617 | |
618 TEST_F(GCMDriverTest, GCMClientNotReadyBeforeRegistration) { | 536 TEST_F(GCMDriverTest, GCMClientNotReadyBeforeRegistration) { |
619 // Make GCMClient not ready initially. | 537 // Make GCMClient not ready initially. |
620 CreateDriver(true, FakeGCMClient::DELAY_START); | 538 CreateDriver(FakeGCMClient::DELAY_START); |
621 SignIn(kTestAccountID1); | 539 SignIn(kTestAccountID1); |
622 | 540 |
623 // The registration is on hold until GCMClient is ready. | 541 // The registration is on hold until GCMClient is ready. |
624 std::vector<std::string> sender_ids; | 542 std::vector<std::string> sender_ids; |
625 sender_ids.push_back("sender1"); | 543 sender_ids.push_back("sender1"); |
626 Register(kTestAppID1, | 544 Register(kTestAppID1, |
627 sender_ids, | 545 sender_ids, |
628 GCMDriverTest::DO_NOT_WAIT); | 546 GCMDriverTest::DO_NOT_WAIT); |
629 PumpIOLoop(); | 547 PumpIOLoop(); |
630 PumpUILoop(); | 548 PumpUILoop(); |
631 EXPECT_TRUE(registration_id().empty()); | 549 EXPECT_TRUE(registration_id().empty()); |
632 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result()); | 550 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result()); |
633 | 551 |
634 // Register operation will be invoked after GCMClient becomes ready. | 552 // Register operation will be invoked after GCMClient becomes ready. |
635 GetGCMClient()->PerformDelayedLoading(); | 553 GetGCMClient()->PerformDelayedLoading(); |
636 WaitForAsyncOperation(); | 554 WaitForAsyncOperation(); |
637 EXPECT_FALSE(registration_id().empty()); | 555 EXPECT_FALSE(registration_id().empty()); |
638 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | 556 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); |
639 } | 557 } |
640 | 558 |
641 TEST_F(GCMDriverTest, GCMClientNotReadyBeforeSending) { | 559 TEST_F(GCMDriverTest, GCMClientNotReadyBeforeSending) { |
642 // Make GCMClient not ready initially. | 560 // Make GCMClient not ready initially. |
643 CreateDriver(true, FakeGCMClient::DELAY_START); | 561 CreateDriver(FakeGCMClient::DELAY_START); |
644 SignIn(kTestAccountID1); | 562 SignIn(kTestAccountID1); |
645 | 563 |
646 // The sending is on hold until GCMClient is ready. | 564 // The sending is on hold until GCMClient is ready. |
647 GCMClient::OutgoingMessage message; | 565 GCMClient::OutgoingMessage message; |
648 message.id = "1"; | 566 message.id = "1"; |
649 message.data["key1"] = "value1"; | 567 message.data["key1"] = "value1"; |
650 message.data["key2"] = "value2"; | 568 message.data["key2"] = "value2"; |
651 Send(kTestAppID1, kUserID1, message, GCMDriverTest::DO_NOT_WAIT); | 569 Send(kTestAppID1, kUserID1, message, GCMDriverTest::DO_NOT_WAIT); |
652 PumpIOLoop(); | 570 PumpIOLoop(); |
653 PumpUILoop(); | 571 PumpUILoop(); |
(...skipping 23 matching lines...) Expand all Loading... |
677 | 595 |
678 GCMDriverFunctionalTest::GCMDriverFunctionalTest() { | 596 GCMDriverFunctionalTest::GCMDriverFunctionalTest() { |
679 } | 597 } |
680 | 598 |
681 GCMDriverFunctionalTest::~GCMDriverFunctionalTest() { | 599 GCMDriverFunctionalTest::~GCMDriverFunctionalTest() { |
682 } | 600 } |
683 | 601 |
684 void GCMDriverFunctionalTest::SetUp() { | 602 void GCMDriverFunctionalTest::SetUp() { |
685 GCMDriverTest::SetUp(); | 603 GCMDriverTest::SetUp(); |
686 | 604 |
687 CreateDriver(true, FakeGCMClient::NO_DELAY_START); | 605 CreateDriver(FakeGCMClient::NO_DELAY_START); |
688 SignIn(kTestAccountID1); | 606 SignIn(kTestAccountID1); |
689 } | 607 } |
690 | 608 |
691 TEST_F(GCMDriverFunctionalTest, Register) { | 609 TEST_F(GCMDriverFunctionalTest, Register) { |
692 std::vector<std::string> sender_ids; | 610 std::vector<std::string> sender_ids; |
693 sender_ids.push_back("sender1"); | 611 sender_ids.push_back("sender1"); |
694 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | 612 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); |
695 const std::string expected_registration_id = | 613 const std::string expected_registration_id = |
696 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids); | 614 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids); |
697 | 615 |
(...skipping 16 matching lines...) Expand all Loading... |
714 sender_ids.push_back("sender2"); | 632 sender_ids.push_back("sender2"); |
715 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | 633 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); |
716 const std::string expected_registration_id = | 634 const std::string expected_registration_id = |
717 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids); | 635 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids); |
718 | 636 |
719 EXPECT_EQ(expected_registration_id, registration_id()); | 637 EXPECT_EQ(expected_registration_id, registration_id()); |
720 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | 638 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); |
721 | 639 |
722 // Clears the results the would be set by the Register callback in preparation | 640 // Clears the results the would be set by the Register callback in preparation |
723 // to call register 2nd time. | 641 // to call register 2nd time. |
724 ClearRegistrationResult(); | 642 ClearResults(); |
725 | 643 |
726 // Calling register 2nd time with the same set of sender IDs but different | 644 // Calling register 2nd time with the same set of sender IDs but different |
727 // ordering will get back the same registration ID. | 645 // ordering will get back the same registration ID. |
728 std::vector<std::string> another_sender_ids; | 646 std::vector<std::string> another_sender_ids; |
729 another_sender_ids.push_back("sender2"); | 647 another_sender_ids.push_back("sender2"); |
730 another_sender_ids.push_back("sender1"); | 648 another_sender_ids.push_back("sender1"); |
731 Register(kTestAppID1, another_sender_ids, GCMDriverTest::WAIT); | 649 Register(kTestAppID1, another_sender_ids, GCMDriverTest::WAIT); |
732 | 650 |
733 EXPECT_EQ(expected_registration_id, registration_id()); | 651 EXPECT_EQ(expected_registration_id, registration_id()); |
734 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | 652 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
801 | 719 |
802 // Start unregistration without waiting for it to complete. This time no async | 720 // Start unregistration without waiting for it to complete. This time no async |
803 // operation is pending. | 721 // operation is pending. |
804 Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT); | 722 Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT); |
805 | 723 |
806 // Test that unregistration fails with async operation pending when there is | 724 // Test that unregistration fails with async operation pending when there is |
807 // an unregistration already in progress. | 725 // an unregistration already in progress. |
808 Unregister(kTestAppID1, GCMDriverTest::WAIT); | 726 Unregister(kTestAppID1, GCMDriverTest::WAIT); |
809 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, | 727 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, |
810 unregistration_result()); | 728 unregistration_result()); |
811 ClearUnregistrationResult(); | 729 ClearResults(); |
812 | 730 |
813 // Complete unregistration. | 731 // Complete unregistration. |
814 WaitForAsyncOperation(); | 732 WaitForAsyncOperation(); |
815 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result()); | 733 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result()); |
816 } | 734 } |
817 | 735 |
818 TEST_F(GCMDriverFunctionalTest, RegisterWhenAsyncOperationPending) { | 736 TEST_F(GCMDriverFunctionalTest, RegisterWhenAsyncOperationPending) { |
819 std::vector<std::string> sender_ids; | 737 std::vector<std::string> sender_ids; |
820 sender_ids.push_back("sender1"); | 738 sender_ids.push_back("sender1"); |
821 // First start registration without waiting for it to complete. | 739 // First start registration without waiting for it to complete. |
822 Register(kTestAppID1, | 740 Register(kTestAppID1, |
823 sender_ids, | 741 sender_ids, |
824 GCMDriverTest::DO_NOT_WAIT); | 742 GCMDriverTest::DO_NOT_WAIT); |
825 | 743 |
826 // Test that registration fails with async operation pending when there is a | 744 // Test that registration fails with async operation pending when there is a |
827 // registration already in progress. | 745 // registration already in progress. |
828 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | 746 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); |
829 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, | 747 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, |
830 registration_result()); | 748 registration_result()); |
831 ClearRegistrationResult(); | 749 ClearResults(); |
832 | 750 |
833 // Complete the registration. | 751 // Complete the registration. |
834 WaitForAsyncOperation(); | 752 WaitForAsyncOperation(); |
835 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | 753 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); |
836 | 754 |
837 // Start unregistration without waiting for it to complete. This time no async | 755 // Start unregistration without waiting for it to complete. This time no async |
838 // operation is pending. | 756 // operation is pending. |
839 Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT); | 757 Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT); |
840 | 758 |
841 // Test that registration fails with async operation pending when there is an | 759 // Test that registration fails with async operation pending when there is an |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
936 | 854 |
937 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) { | 855 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) { |
938 GetGCMClient()->DeleteMessages(kTestAppID1); | 856 GetGCMClient()->DeleteMessages(kTestAppID1); |
939 gcm_app_handler()->WaitForNotification(); | 857 gcm_app_handler()->WaitForNotification(); |
940 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, | 858 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, |
941 gcm_app_handler()->received_event()); | 859 gcm_app_handler()->received_event()); |
942 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); | 860 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); |
943 } | 861 } |
944 | 862 |
945 } // namespace gcm | 863 } // namespace gcm |
OLD | NEW |