Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: chrome/browser/services/gcm/gcm_driver_unittest.cc

Issue 286213003: Make GCMProfileService own GCMDriver, instead of deriving from it (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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 ClearRegistrationResult();
232 void ClearUnregistrationResult(); 182 void ClearUnregistrationResult();
233 183
234 bool HasAppHandlers() const; 184 bool HasAppHandlers() const;
235 FakeGCMClient* GetGCMClient(); 185 FakeGCMClient* GetGCMClient();
236 186
237 void CreateDriver(bool start_automatically, 187 void CreateDriver(FakeGCMClient::StartMode gcm_client_start_mode);
238 FakeGCMClient::StartMode gcm_client_start_mode);
239 188
240 void SignIn(const std::string& account_id); 189 void SignIn(const std::string& account_id);
241 void SignOut(); 190 void SignOut();
242 191
243 void Register(const std::string& app_id, 192 void Register(const std::string& app_id,
244 const std::vector<std::string>& sender_ids, 193 const std::vector<std::string>& sender_ids,
245 WaitToFinish wait_to_finish); 194 WaitToFinish wait_to_finish);
246 void Send(const std::string& app_id, 195 void Send(const std::string& app_id,
247 const std::string& receiver_id, 196 const std::string& receiver_id,
248 const GCMClient::OutgoingMessage& message, 197 const GCMClient::OutgoingMessage& message,
249 WaitToFinish wait_to_finish); 198 WaitToFinish wait_to_finish);
250 void Unregister(const std::string& app_id, WaitToFinish wait_to_finish); 199 void Unregister(const std::string& app_id, WaitToFinish wait_to_finish);
251 200
252 void WaitForAsyncOperation(); 201 void WaitForAsyncOperation();
253 202
254 private: 203 private:
255 void RegisterCompleted(const std::string& registration_id, 204 void RegisterCompleted(const std::string& registration_id,
256 GCMClient::Result result); 205 GCMClient::Result result);
257 void SendCompleted(const std::string& message_id, GCMClient::Result result); 206 void SendCompleted(const std::string& message_id, GCMClient::Result result);
258 void UnregisterCompleted(GCMClient::Result result); 207 void UnregisterCompleted(GCMClient::Result result);
259 208
260 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_; 209 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_;
210 base::ScopedTempDir temp_dir_;
261 FakeOAuth2TokenService token_service_; 211 FakeOAuth2TokenService token_service_;
262 scoped_ptr<FakeIdentityProvider> identity_provider_owner_; 212 scoped_ptr<FakeIdentityProvider> identity_provider_owner_;
263 FakeIdentityProvider* identity_provider_; 213 FakeIdentityProvider* identity_provider_;
264 scoped_ptr<TestGCMDriver> driver_; 214 scoped_ptr<GCMDriver> driver_;
265 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; 215 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_;
266 216
267 base::Closure async_operation_completed_callback_; 217 base::Closure async_operation_completed_callback_;
268 218
269 std::string registration_id_; 219 std::string registration_id_;
270 GCMClient::Result registration_result_; 220 GCMClient::Result registration_result_;
271 std::string send_message_id_; 221 std::string send_message_id_;
272 GCMClient::Result send_result_; 222 GCMClient::Result send_result_;
273 GCMClient::Result unregistration_result_; 223 GCMClient::Result unregistration_result_;
274 224
275 DISALLOW_COPY_AND_ASSIGN(GCMDriverTest); 225 DISALLOW_COPY_AND_ASSIGN(GCMDriverTest);
276 }; 226 };
277 227
278 GCMDriverTest::GCMDriverTest() 228 GCMDriverTest::GCMDriverTest()
279 : identity_provider_(NULL), 229 : identity_provider_(NULL),
280 registration_result_(GCMClient::UNKNOWN_ERROR), 230 registration_result_(GCMClient::UNKNOWN_ERROR),
281 send_result_(GCMClient::UNKNOWN_ERROR), 231 send_result_(GCMClient::UNKNOWN_ERROR),
282 unregistration_result_(GCMClient::UNKNOWN_ERROR) { 232 unregistration_result_(GCMClient::UNKNOWN_ERROR) {
283 identity_provider_owner_.reset(new FakeIdentityProvider(&token_service_)); 233 identity_provider_owner_.reset(new FakeIdentityProvider(&token_service_));
284 identity_provider_ = identity_provider_owner_.get(); 234 identity_provider_ = identity_provider_owner_.get();
285 } 235 }
286 236
287 GCMDriverTest::~GCMDriverTest() { 237 GCMDriverTest::~GCMDriverTest() {
288 } 238 }
289 239
290 void GCMDriverTest::SetUp() { 240 void GCMDriverTest::SetUp() {
291 thread_bundle_.reset(new content::TestBrowserThreadBundle( 241 thread_bundle_.reset(new content::TestBrowserThreadBundle(
292 content::TestBrowserThreadBundle::REAL_IO_THREAD)); 242 content::TestBrowserThreadBundle::REAL_IO_THREAD));
243 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
293 } 244 }
294 245
295 void GCMDriverTest::TearDown() { 246 void GCMDriverTest::TearDown() {
296 if (!driver_) 247 if (!driver_)
297 return; 248 return;
298 249
299 driver_->ShutdownService(); 250 driver_->Shutdown();
300 driver_.reset(); 251 driver_.reset();
301 PumpIOLoop(); 252 PumpIOLoop();
302 } 253 }
303 254
304 void GCMDriverTest::ClearRegistrationResult() { 255 void GCMDriverTest::ClearRegistrationResult() {
305 registration_id_.clear(); 256 registration_id_.clear();
306 registration_result_ = GCMClient::UNKNOWN_ERROR; 257 registration_result_ = GCMClient::UNKNOWN_ERROR;
307 } 258 }
308 259
309 void GCMDriverTest::ClearUnregistrationResult() { 260 void GCMDriverTest::ClearUnregistrationResult() {
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
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, StopAndRestartGCM) {
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 loaded.
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 // Stops 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 // Restarts 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 loaded.
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 // Stops 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, RegisterWhenNotSignedIn) {
545 CreateDriver(true, FakeGCMClient::NO_DELAY_START); 478 CreateDriver(FakeGCMClient::NO_DELAY_START);
546 479
547 std::vector<std::string> sender_ids; 480 std::vector<std::string> sender_ids;
548 sender_ids.push_back("sender1"); 481 sender_ids.push_back("sender1");
549 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); 482 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
550 483
551 EXPECT_TRUE(registration_id().empty()); 484 EXPECT_TRUE(registration_id().empty());
552 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, registration_result()); 485 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, registration_result());
553 } 486 }
554 487
555 TEST_F(GCMDriverTest, RegisterUnderNonPositiveChannelSignal) {
556 // Non-positive channel signal will prevent GCMClient from checking in during
557 // sign-in.
558 CreateDriver(false, FakeGCMClient::NO_DELAY_START);
559 SignIn(kTestAccountID1);
560
561 // GCMClient should not be checked in.
562 EXPECT_FALSE(driver()->IsGCMClientReady());
563 EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status());
564
565 // Invoking register will make GCMClient checked in.
566 std::vector<std::string> sender_ids;
567 sender_ids.push_back("sender1");
568 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
569
570 // GCMClient should be checked in.
571 EXPECT_TRUE(driver()->IsGCMClientReady());
572 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
573
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 }
580
581 TEST_F(GCMDriverTest, SendWhenNotSignedIn) { 488 TEST_F(GCMDriverTest, SendWhenNotSignedIn) {
582 CreateDriver(true, FakeGCMClient::NO_DELAY_START); 489 CreateDriver(FakeGCMClient::NO_DELAY_START);
583 490
584 GCMClient::OutgoingMessage message; 491 GCMClient::OutgoingMessage message;
585 message.id = "1"; 492 message.id = "1";
586 message.data["key1"] = "value1"; 493 message.data["key1"] = "value1";
587 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT); 494 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
588 495
589 EXPECT_TRUE(send_message_id().empty()); 496 EXPECT_TRUE(send_message_id().empty());
590 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, send_result()); 497 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, send_result());
591 } 498 }
592 499
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) { 500 TEST_F(GCMDriverTest, GCMClientNotReadyBeforeRegistration) {
619 // Make GCMClient not ready initially. 501 // Make GCMClient not ready initially.
620 CreateDriver(true, FakeGCMClient::DELAY_START); 502 CreateDriver(FakeGCMClient::DELAY_START);
621 SignIn(kTestAccountID1); 503 SignIn(kTestAccountID1);
622 504
623 // The registration is on hold until GCMClient is ready. 505 // The registration is on hold until GCMClient is ready.
624 std::vector<std::string> sender_ids; 506 std::vector<std::string> sender_ids;
625 sender_ids.push_back("sender1"); 507 sender_ids.push_back("sender1");
626 Register(kTestAppID1, 508 Register(kTestAppID1,
627 sender_ids, 509 sender_ids,
628 GCMDriverTest::DO_NOT_WAIT); 510 GCMDriverTest::DO_NOT_WAIT);
629 PumpIOLoop(); 511 PumpIOLoop();
630 PumpUILoop(); 512 PumpUILoop();
631 EXPECT_TRUE(registration_id().empty()); 513 EXPECT_TRUE(registration_id().empty());
632 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result()); 514 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result());
633 515
634 // Register operation will be invoked after GCMClient becomes ready. 516 // Register operation will be invoked after GCMClient becomes ready.
635 GetGCMClient()->PerformDelayedLoading(); 517 GetGCMClient()->PerformDelayedLoading();
636 WaitForAsyncOperation(); 518 WaitForAsyncOperation();
637 EXPECT_FALSE(registration_id().empty()); 519 EXPECT_FALSE(registration_id().empty());
638 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); 520 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
639 } 521 }
640 522
641 TEST_F(GCMDriverTest, GCMClientNotReadyBeforeSending) { 523 TEST_F(GCMDriverTest, GCMClientNotReadyBeforeSending) {
642 // Make GCMClient not ready initially. 524 // Make GCMClient not ready initially.
643 CreateDriver(true, FakeGCMClient::DELAY_START); 525 CreateDriver(FakeGCMClient::DELAY_START);
644 SignIn(kTestAccountID1); 526 SignIn(kTestAccountID1);
645 527
646 // The sending is on hold until GCMClient is ready. 528 // The sending is on hold until GCMClient is ready.
647 GCMClient::OutgoingMessage message; 529 GCMClient::OutgoingMessage message;
648 message.id = "1"; 530 message.id = "1";
649 message.data["key1"] = "value1"; 531 message.data["key1"] = "value1";
650 message.data["key2"] = "value2"; 532 message.data["key2"] = "value2";
651 Send(kTestAppID1, kUserID1, message, GCMDriverTest::DO_NOT_WAIT); 533 Send(kTestAppID1, kUserID1, message, GCMDriverTest::DO_NOT_WAIT);
652 PumpIOLoop(); 534 PumpIOLoop();
653 PumpUILoop(); 535 PumpUILoop();
(...skipping 23 matching lines...) Expand all
677 559
678 GCMDriverFunctionalTest::GCMDriverFunctionalTest() { 560 GCMDriverFunctionalTest::GCMDriverFunctionalTest() {
679 } 561 }
680 562
681 GCMDriverFunctionalTest::~GCMDriverFunctionalTest() { 563 GCMDriverFunctionalTest::~GCMDriverFunctionalTest() {
682 } 564 }
683 565
684 void GCMDriverFunctionalTest::SetUp() { 566 void GCMDriverFunctionalTest::SetUp() {
685 GCMDriverTest::SetUp(); 567 GCMDriverTest::SetUp();
686 568
687 CreateDriver(true, FakeGCMClient::NO_DELAY_START); 569 CreateDriver(FakeGCMClient::NO_DELAY_START);
688 SignIn(kTestAccountID1); 570 SignIn(kTestAccountID1);
689 } 571 }
690 572
691 TEST_F(GCMDriverFunctionalTest, Register) { 573 TEST_F(GCMDriverFunctionalTest, Register) {
692 std::vector<std::string> sender_ids; 574 std::vector<std::string> sender_ids;
693 sender_ids.push_back("sender1"); 575 sender_ids.push_back("sender1");
694 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); 576 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
695 const std::string expected_registration_id = 577 const std::string expected_registration_id =
696 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids); 578 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids);
697 579
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
936 818
937 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) { 819 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) {
938 GetGCMClient()->DeleteMessages(kTestAppID1); 820 GetGCMClient()->DeleteMessages(kTestAppID1);
939 gcm_app_handler()->WaitForNotification(); 821 gcm_app_handler()->WaitForNotification();
940 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, 822 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT,
941 gcm_app_handler()->received_event()); 823 gcm_app_handler()->received_event());
942 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); 824 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
943 } 825 }
944 826
945 } // namespace gcm 827 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698