Make sure webrtc::VideoSource is released when WebRtcVideoTrackAdapter is destroyed.
[chromium-blink-merge.git] / components / gcm_driver / gcm_driver_unittest.cc
blob52f6abb2b5e4113781a683439254133da815b1a0
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/gcm_driver/gcm_driver.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/location.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/message_loop/message_loop_proxy.h"
13 #include "base/run_loop.h"
14 #include "base/strings/string_util.h"
15 #include "base/test/test_simple_task_runner.h"
16 #include "base/threading/thread.h"
17 #include "components/gcm_driver/fake_gcm_app_handler.h"
18 #include "components/gcm_driver/fake_gcm_client.h"
19 #include "components/gcm_driver/fake_gcm_client_factory.h"
20 #include "components/gcm_driver/gcm_app_handler.h"
21 #include "components/gcm_driver/gcm_client_factory.h"
22 #include "google_apis/gaia/fake_identity_provider.h"
23 #include "google_apis/gaia/fake_oauth2_token_service.h"
24 #include "net/url_request/url_request_context_getter.h"
25 #include "net/url_request/url_request_test_util.h"
26 #include "testing/gtest/include/gtest/gtest.h"
28 namespace gcm {
30 namespace {
32 const char kTestAccountID1[] = "user1@example.com";
33 const char kTestAccountID2[] = "user2@example.com";
34 const char kTestAppID1[] = "TestApp1";
35 const char kTestAppID2[] = "TestApp2";
36 const char kUserID1[] = "user1";
38 void PumpCurrentLoop() {
39 base::MessageLoop::ScopedNestableTaskAllower
40 nestable_task_allower(base::MessageLoop::current());
41 base::RunLoop().RunUntilIdle();
44 void PumpUILoop() {
45 PumpCurrentLoop();
48 std::vector<std::string> ToSenderList(const std::string& sender_ids) {
49 std::vector<std::string> senders;
50 Tokenize(sender_ids, ",", &senders);
51 return senders;
54 } // namespace
56 class GCMDriverTest : public testing::Test {
57 public:
58 enum WaitToFinish {
59 DO_NOT_WAIT,
60 WAIT
63 GCMDriverTest();
64 virtual ~GCMDriverTest();
66 // testing::Test:
67 virtual void SetUp() OVERRIDE;
68 virtual void TearDown() OVERRIDE;
70 GCMDriver* driver() { return driver_.get(); }
71 FakeGCMAppHandler* gcm_app_handler() { return gcm_app_handler_.get(); }
72 const std::string& registration_id() const { return registration_id_; }
73 GCMClient::Result registration_result() const { return registration_result_; }
74 const std::string& send_message_id() const { return send_message_id_; }
75 GCMClient::Result send_result() const { return send_result_; }
76 GCMClient::Result unregistration_result() const {
77 return unregistration_result_;
80 void PumpIOLoop();
82 void ClearResults();
84 bool HasAppHandlers() const;
85 FakeGCMClient* GetGCMClient();
87 void CreateDriver(FakeGCMClient::StartMode gcm_client_start_mode);
88 void AddAppHandlers();
89 void RemoveAppHandlers();
91 void SignIn(const std::string& account_id);
92 void SignOut();
94 void Register(const std::string& app_id,
95 const std::vector<std::string>& sender_ids,
96 WaitToFinish wait_to_finish);
97 void Send(const std::string& app_id,
98 const std::string& receiver_id,
99 const GCMClient::OutgoingMessage& message,
100 WaitToFinish wait_to_finish);
101 void Unregister(const std::string& app_id, WaitToFinish wait_to_finish);
103 void WaitForAsyncOperation();
105 private:
106 void RegisterCompleted(const std::string& registration_id,
107 GCMClient::Result result);
108 void SendCompleted(const std::string& message_id, GCMClient::Result result);
109 void UnregisterCompleted(GCMClient::Result result);
111 base::ScopedTempDir temp_dir_;
112 FakeOAuth2TokenService token_service_;
113 scoped_ptr<FakeIdentityProvider> identity_provider_owner_;
114 FakeIdentityProvider* identity_provider_;
115 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
116 base::MessageLoopForUI message_loop_;
117 base::Thread io_thread_;
118 scoped_ptr<GCMDriver> driver_;
119 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_;
121 base::Closure async_operation_completed_callback_;
123 std::string registration_id_;
124 GCMClient::Result registration_result_;
125 std::string send_message_id_;
126 GCMClient::Result send_result_;
127 GCMClient::Result unregistration_result_;
129 DISALLOW_COPY_AND_ASSIGN(GCMDriverTest);
132 GCMDriverTest::GCMDriverTest()
133 : identity_provider_(NULL),
134 task_runner_(new base::TestSimpleTaskRunner()),
135 io_thread_("IOThread"),
136 registration_result_(GCMClient::UNKNOWN_ERROR),
137 send_result_(GCMClient::UNKNOWN_ERROR),
138 unregistration_result_(GCMClient::UNKNOWN_ERROR) {
139 identity_provider_owner_.reset(new FakeIdentityProvider(&token_service_));
140 identity_provider_ = identity_provider_owner_.get();
143 GCMDriverTest::~GCMDriverTest() {
146 void GCMDriverTest::SetUp() {
147 io_thread_.Start();
148 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
151 void GCMDriverTest::TearDown() {
152 if (!driver_)
153 return;
155 driver_->Shutdown();
156 driver_.reset();
157 PumpIOLoop();
159 io_thread_.Stop();
162 void GCMDriverTest::PumpIOLoop() {
163 base::RunLoop run_loop;
164 io_thread_.message_loop_proxy()->PostTaskAndReply(
165 FROM_HERE,
166 base::Bind(&PumpCurrentLoop),
167 run_loop.QuitClosure());
168 run_loop.Run();
171 void GCMDriverTest::ClearResults() {
172 registration_id_.clear();
173 registration_result_ = GCMClient::UNKNOWN_ERROR;
175 send_message_id_.clear();
176 send_result_ = GCMClient::UNKNOWN_ERROR;
178 unregistration_result_ = GCMClient::UNKNOWN_ERROR;
181 bool GCMDriverTest::HasAppHandlers() const {
182 return !driver_->app_handlers().empty();
185 FakeGCMClient* GCMDriverTest::GetGCMClient() {
186 return static_cast<FakeGCMClient*>(driver_->GetGCMClientForTesting());
189 void GCMDriverTest::CreateDriver(
190 FakeGCMClient::StartMode gcm_client_start_mode) {
191 scoped_refptr<net::URLRequestContextGetter> request_context =
192 new net::TestURLRequestContextGetter(io_thread_.message_loop_proxy());
193 driver_.reset(new GCMDriver(
194 scoped_ptr<GCMClientFactory>(new FakeGCMClientFactory(
195 gcm_client_start_mode,
196 base::MessageLoopProxy::current(),
197 io_thread_.message_loop_proxy())).Pass(),
198 identity_provider_owner_.PassAs<IdentityProvider>(),
199 GCMClient::ChromeBuildInfo(),
200 temp_dir_.path(),
201 request_context,
202 base::MessageLoopProxy::current(),
203 io_thread_.message_loop_proxy(),
204 task_runner_));
206 gcm_app_handler_.reset(new FakeGCMAppHandler);
209 void GCMDriverTest::AddAppHandlers() {
210 driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get());
211 driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get());
214 void GCMDriverTest::RemoveAppHandlers() {
215 driver_->RemoveAppHandler(kTestAppID1);
216 driver_->RemoveAppHandler(kTestAppID2);
219 void GCMDriverTest::SignIn(const std::string& account_id) {
220 token_service_.AddAccount(account_id);
221 identity_provider_->LogIn(account_id);
222 PumpIOLoop();
223 PumpUILoop();
226 void GCMDriverTest::SignOut() {
227 identity_provider_->LogOut();
228 PumpIOLoop();
229 PumpUILoop();
232 void GCMDriverTest::Register(const std::string& app_id,
233 const std::vector<std::string>& sender_ids,
234 WaitToFinish wait_to_finish) {
235 base::RunLoop run_loop;
236 async_operation_completed_callback_ = run_loop.QuitClosure();
237 driver_->Register(app_id,
238 sender_ids,
239 base::Bind(&GCMDriverTest::RegisterCompleted,
240 base::Unretained(this)));
241 if (wait_to_finish == WAIT)
242 run_loop.Run();
245 void GCMDriverTest::Send(const std::string& app_id,
246 const std::string& receiver_id,
247 const GCMClient::OutgoingMessage& message,
248 WaitToFinish wait_to_finish) {
249 base::RunLoop run_loop;
250 async_operation_completed_callback_ = run_loop.QuitClosure();
251 driver_->Send(app_id,
252 receiver_id,
253 message,
254 base::Bind(&GCMDriverTest::SendCompleted,
255 base::Unretained(this)));
256 if (wait_to_finish == WAIT)
257 run_loop.Run();
260 void GCMDriverTest::Unregister(const std::string& app_id,
261 WaitToFinish wait_to_finish) {
262 base::RunLoop run_loop;
263 async_operation_completed_callback_ = run_loop.QuitClosure();
264 driver_->Unregister(app_id,
265 base::Bind(&GCMDriverTest::UnregisterCompleted,
266 base::Unretained(this)));
267 if (wait_to_finish == WAIT)
268 run_loop.Run();
271 void GCMDriverTest::WaitForAsyncOperation() {
272 base::RunLoop run_loop;
273 async_operation_completed_callback_ = run_loop.QuitClosure();
274 run_loop.Run();
277 void GCMDriverTest::RegisterCompleted(const std::string& registration_id,
278 GCMClient::Result result) {
279 registration_id_ = registration_id;
280 registration_result_ = result;
281 if (!async_operation_completed_callback_.is_null())
282 async_operation_completed_callback_.Run();
285 void GCMDriverTest::SendCompleted(const std::string& message_id,
286 GCMClient::Result result) {
287 send_message_id_ = message_id;
288 send_result_ = result;
289 if (!async_operation_completed_callback_.is_null())
290 async_operation_completed_callback_.Run();
293 void GCMDriverTest::UnregisterCompleted(GCMClient::Result result) {
294 unregistration_result_ = result;
295 if (!async_operation_completed_callback_.is_null())
296 async_operation_completed_callback_.Run();
299 TEST_F(GCMDriverTest, CreateGCMDriverBeforeSignIn) {
300 // Create GCMDriver first. GCM is not started.
301 CreateDriver(FakeGCMClient::NO_DELAY_START);
302 EXPECT_FALSE(driver()->IsStarted());
304 // Sign in. GCM is still not started.
305 SignIn(kTestAccountID1);
306 EXPECT_FALSE(driver()->IsStarted());
308 // GCM will be started only after both sign-in and app handler being
309 AddAppHandlers();
310 EXPECT_TRUE(driver()->IsStarted());
313 TEST_F(GCMDriverTest, CreateGCMDriverAfterSignIn) {
314 // Sign in. Nothings happens since GCMDriver is not created.
315 SignIn(kTestAccountID1);
317 // Create GCMDriver after sign-in. GCM is not started.
318 CreateDriver(FakeGCMClient::NO_DELAY_START);
319 EXPECT_FALSE(driver()->IsStarted());
321 // GCM will be started only after both sign-in and app handler being
322 AddAppHandlers();
323 EXPECT_TRUE(driver()->IsStarted());
326 TEST_F(GCMDriverTest, Shutdown) {
327 CreateDriver(FakeGCMClient::NO_DELAY_START);
328 EXPECT_FALSE(HasAppHandlers());
330 AddAppHandlers();
331 EXPECT_TRUE(HasAppHandlers());
333 driver()->Shutdown();
334 EXPECT_FALSE(HasAppHandlers());
337 TEST_F(GCMDriverTest, SignInAndSignOutOnGCMEnabled) {
338 // By default, GCM is enabled.
339 CreateDriver(FakeGCMClient::NO_DELAY_START);
340 AddAppHandlers();
342 // GCMClient should be started after sign-in.
343 SignIn(kTestAccountID1);
344 EXPECT_TRUE(driver()->IsGCMClientReady());
345 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
347 // GCMClient should be checked out after sign-out.
348 SignOut();
349 EXPECT_FALSE(driver()->IsGCMClientReady());
350 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status());
353 TEST_F(GCMDriverTest, SignInAndSignOutOnGCMDisabled) {
354 // By default, GCM is enabled.
355 CreateDriver(FakeGCMClient::NO_DELAY_START);
356 AddAppHandlers();
358 // Disable GCM.
359 driver()->Disable();
361 // GCMClient should not be started after sign-in.
362 SignIn(kTestAccountID1);
363 EXPECT_FALSE(driver()->IsGCMClientReady());
364 EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status());
366 // Check-out should still be performed after sign-out.
367 SignOut();
368 EXPECT_FALSE(driver()->IsGCMClientReady());
369 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status());
372 TEST_F(GCMDriverTest, SignOutAndThenSignIn) {
373 CreateDriver(FakeGCMClient::NO_DELAY_START);
374 AddAppHandlers();
376 // GCMClient should be started after sign-in.
377 SignIn(kTestAccountID1);
378 EXPECT_TRUE(driver()->IsGCMClientReady());
379 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
381 // GCMClient should be checked out after sign-out.
382 SignOut();
383 EXPECT_FALSE(driver()->IsGCMClientReady());
384 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status());
386 // Sign-in with a different account.
387 SignIn(kTestAccountID2);
389 // GCMClient should be started again.
390 EXPECT_TRUE(driver()->IsGCMClientReady());
391 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
394 TEST_F(GCMDriverTest, DisableAndReenableGCM) {
395 CreateDriver(FakeGCMClient::NO_DELAY_START);
396 AddAppHandlers();
397 SignIn(kTestAccountID1);
399 // GCMClient should be started.
400 EXPECT_TRUE(driver()->IsGCMClientReady());
401 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
403 // Disables the GCM.
404 driver()->Disable();
405 PumpIOLoop();
406 PumpUILoop();
408 // GCMClient should be stopped.
409 EXPECT_FALSE(driver()->IsGCMClientReady());
410 EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status());
412 // Enables the GCM.
413 driver()->Enable();
414 PumpIOLoop();
415 PumpUILoop();
417 // GCMClient should be started.
418 EXPECT_TRUE(driver()->IsGCMClientReady());
419 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
421 // Disables the GCM.
422 driver()->Disable();
423 PumpIOLoop();
424 PumpUILoop();
426 // GCMClient should be stopped.
427 EXPECT_FALSE(driver()->IsGCMClientReady());
428 EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status());
430 // Sign out.
431 SignOut();
433 // GCMClient should be checked out.
434 EXPECT_FALSE(driver()->IsGCMClientReady());
435 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status());
438 TEST_F(GCMDriverTest, StartOrStopGCMOnDemand) {
439 CreateDriver(FakeGCMClient::NO_DELAY_START);
440 SignIn(kTestAccountID1);
442 // GCMClient is not started.
443 EXPECT_FALSE(driver()->IsGCMClientReady());
444 EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status());
446 // GCMClient is started after an app handler has been added.
447 driver()->AddAppHandler(kTestAppID1, gcm_app_handler());
448 PumpIOLoop();
449 PumpUILoop();
450 EXPECT_TRUE(driver()->IsGCMClientReady());
451 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
453 // Add another app handler.
454 driver()->AddAppHandler(kTestAppID2, gcm_app_handler());
455 PumpIOLoop();
456 PumpUILoop();
457 EXPECT_TRUE(driver()->IsGCMClientReady());
458 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
460 // GCMClient remains active after one app handler is gone.
461 driver()->RemoveAppHandler(kTestAppID1);
462 PumpIOLoop();
463 PumpUILoop();
464 EXPECT_TRUE(driver()->IsGCMClientReady());
465 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
467 // GCMClient should be stopped after the last app handler is gone.
468 driver()->RemoveAppHandler(kTestAppID2);
469 PumpIOLoop();
470 PumpUILoop();
471 EXPECT_FALSE(driver()->IsGCMClientReady());
472 EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status());
474 // GCMClient is restarted after an app handler has been added.
475 driver()->AddAppHandler(kTestAppID2, gcm_app_handler());
476 PumpIOLoop();
477 PumpUILoop();
478 EXPECT_TRUE(driver()->IsGCMClientReady());
479 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
482 TEST_F(GCMDriverTest, RegisterFailed) {
483 std::vector<std::string> sender_ids;
484 sender_ids.push_back("sender1");
486 CreateDriver(FakeGCMClient::NO_DELAY_START);
488 // Registration fails when GCM is disabled.
489 driver()->Disable();
490 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
491 EXPECT_TRUE(registration_id().empty());
492 EXPECT_EQ(GCMClient::GCM_DISABLED, registration_result());
494 ClearResults();
496 // Registration fails when the sign-in does not occur.
497 driver()->Enable();
498 AddAppHandlers();
499 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
500 EXPECT_TRUE(registration_id().empty());
501 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, registration_result());
503 ClearResults();
505 // Registration fails when the no app handler is added.
506 RemoveAppHandlers();
507 SignIn(kTestAccountID1);
508 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
509 EXPECT_TRUE(registration_id().empty());
510 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result());
513 TEST_F(GCMDriverTest, UnregisterFailed) {
514 CreateDriver(FakeGCMClient::NO_DELAY_START);
516 // Unregistration fails when GCM is disabled.
517 driver()->Disable();
518 Unregister(kTestAppID1, GCMDriverTest::WAIT);
519 EXPECT_EQ(GCMClient::GCM_DISABLED, unregistration_result());
521 ClearResults();
523 // Unregistration fails when the sign-in does not occur.
524 driver()->Enable();
525 AddAppHandlers();
526 Unregister(kTestAppID1, GCMDriverTest::WAIT);
527 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, unregistration_result());
529 ClearResults();
531 // Unregistration fails when the no app handler is added.
532 RemoveAppHandlers();
533 SignIn(kTestAccountID1);
534 Unregister(kTestAppID1, GCMDriverTest::WAIT);
535 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, unregistration_result());
538 TEST_F(GCMDriverTest, SendFailed) {
539 GCMClient::OutgoingMessage message;
540 message.id = "1";
541 message.data["key1"] = "value1";
543 CreateDriver(FakeGCMClient::NO_DELAY_START);
545 // Sending fails when GCM is disabled.
546 driver()->Disable();
547 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
548 EXPECT_TRUE(send_message_id().empty());
549 EXPECT_EQ(GCMClient::GCM_DISABLED, send_result());
551 ClearResults();
553 // Sending fails when the sign-in does not occur.
554 driver()->Enable();
555 AddAppHandlers();
556 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
557 EXPECT_TRUE(send_message_id().empty());
558 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, send_result());
560 ClearResults();
562 // Sending fails when the no app handler is added.
563 RemoveAppHandlers();
564 SignIn(kTestAccountID1);
565 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
566 EXPECT_TRUE(send_message_id().empty());
567 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, send_result());
570 TEST_F(GCMDriverTest, GCMClientNotReadyBeforeRegistration) {
571 // Make GCMClient not ready initially.
572 CreateDriver(FakeGCMClient::DELAY_START);
573 SignIn(kTestAccountID1);
574 AddAppHandlers();
576 // The registration is on hold until GCMClient is ready.
577 std::vector<std::string> sender_ids;
578 sender_ids.push_back("sender1");
579 Register(kTestAppID1,
580 sender_ids,
581 GCMDriverTest::DO_NOT_WAIT);
582 PumpIOLoop();
583 PumpUILoop();
584 EXPECT_TRUE(registration_id().empty());
585 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result());
587 // Register operation will be invoked after GCMClient becomes ready.
588 GetGCMClient()->PerformDelayedLoading();
589 WaitForAsyncOperation();
590 EXPECT_FALSE(registration_id().empty());
591 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
594 TEST_F(GCMDriverTest, GCMClientNotReadyBeforeSending) {
595 // Make GCMClient not ready initially.
596 CreateDriver(FakeGCMClient::DELAY_START);
597 SignIn(kTestAccountID1);
598 AddAppHandlers();
600 // The sending is on hold until GCMClient is ready.
601 GCMClient::OutgoingMessage message;
602 message.id = "1";
603 message.data["key1"] = "value1";
604 message.data["key2"] = "value2";
605 Send(kTestAppID1, kUserID1, message, GCMDriverTest::DO_NOT_WAIT);
606 PumpIOLoop();
607 PumpUILoop();
609 EXPECT_TRUE(send_message_id().empty());
610 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, send_result());
612 // Send operation will be invoked after GCMClient becomes ready.
613 GetGCMClient()->PerformDelayedLoading();
614 WaitForAsyncOperation();
615 EXPECT_EQ(message.id, send_message_id());
616 EXPECT_EQ(GCMClient::SUCCESS, send_result());
619 // Tests a single instance of GCMDriver.
620 class GCMDriverFunctionalTest : public GCMDriverTest {
621 public:
622 GCMDriverFunctionalTest();
623 virtual ~GCMDriverFunctionalTest();
625 // GCMDriverTest:
626 virtual void SetUp() OVERRIDE;
628 private:
629 DISALLOW_COPY_AND_ASSIGN(GCMDriverFunctionalTest);
632 GCMDriverFunctionalTest::GCMDriverFunctionalTest() {
635 GCMDriverFunctionalTest::~GCMDriverFunctionalTest() {
638 void GCMDriverFunctionalTest::SetUp() {
639 GCMDriverTest::SetUp();
641 CreateDriver(FakeGCMClient::NO_DELAY_START);
642 AddAppHandlers();
643 SignIn(kTestAccountID1);
646 TEST_F(GCMDriverFunctionalTest, Register) {
647 std::vector<std::string> sender_ids;
648 sender_ids.push_back("sender1");
649 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
650 const std::string expected_registration_id =
651 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids);
653 EXPECT_EQ(expected_registration_id, registration_id());
654 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
657 TEST_F(GCMDriverFunctionalTest, RegisterError) {
658 std::vector<std::string> sender_ids;
659 sender_ids.push_back("sender1@error");
660 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
662 EXPECT_TRUE(registration_id().empty());
663 EXPECT_NE(GCMClient::SUCCESS, registration_result());
666 TEST_F(GCMDriverFunctionalTest, RegisterAgainWithSameSenderIDs) {
667 std::vector<std::string> sender_ids;
668 sender_ids.push_back("sender1");
669 sender_ids.push_back("sender2");
670 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
671 const std::string expected_registration_id =
672 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids);
674 EXPECT_EQ(expected_registration_id, registration_id());
675 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
677 // Clears the results the would be set by the Register callback in preparation
678 // to call register 2nd time.
679 ClearResults();
681 // Calling register 2nd time with the same set of sender IDs but different
682 // ordering will get back the same registration ID.
683 std::vector<std::string> another_sender_ids;
684 another_sender_ids.push_back("sender2");
685 another_sender_ids.push_back("sender1");
686 Register(kTestAppID1, another_sender_ids, GCMDriverTest::WAIT);
688 EXPECT_EQ(expected_registration_id, registration_id());
689 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
692 TEST_F(GCMDriverFunctionalTest, RegisterAgainWithDifferentSenderIDs) {
693 std::vector<std::string> sender_ids;
694 sender_ids.push_back("sender1");
695 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
696 const std::string expected_registration_id =
697 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids);
699 EXPECT_EQ(expected_registration_id, registration_id());
700 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
702 // Make sender IDs different.
703 sender_ids.push_back("sender2");
704 const std::string expected_registration_id2 =
705 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids);
707 // Calling register 2nd time with the different sender IDs will get back a new
708 // registration ID.
709 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
710 EXPECT_EQ(expected_registration_id2, registration_id());
711 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
714 TEST_F(GCMDriverFunctionalTest, RegisterAfterSignOut) {
715 // This will trigger check-out.
716 SignOut();
718 std::vector<std::string> sender_ids;
719 sender_ids.push_back("sender1");
720 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
722 EXPECT_TRUE(registration_id().empty());
723 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, registration_result());
726 TEST_F(GCMDriverFunctionalTest, UnregisterExplicitly) {
727 std::vector<std::string> sender_ids;
728 sender_ids.push_back("sender1");
729 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
731 EXPECT_FALSE(registration_id().empty());
732 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
734 Unregister(kTestAppID1, GCMDriverTest::WAIT);
736 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
739 TEST_F(GCMDriverFunctionalTest, UnregisterWhenAsyncOperationPending) {
740 std::vector<std::string> sender_ids;
741 sender_ids.push_back("sender1");
742 // First start registration without waiting for it to complete.
743 Register(kTestAppID1,
744 sender_ids,
745 GCMDriverTest::DO_NOT_WAIT);
747 // Test that unregistration fails with async operation pending when there is a
748 // registration already in progress.
749 Unregister(kTestAppID1, GCMDriverTest::WAIT);
750 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
751 unregistration_result());
753 // Complete the unregistration.
754 WaitForAsyncOperation();
755 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
757 // Start unregistration without waiting for it to complete. This time no async
758 // operation is pending.
759 Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT);
761 // Test that unregistration fails with async operation pending when there is
762 // an unregistration already in progress.
763 Unregister(kTestAppID1, GCMDriverTest::WAIT);
764 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
765 unregistration_result());
766 ClearResults();
768 // Complete unregistration.
769 WaitForAsyncOperation();
770 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
773 TEST_F(GCMDriverFunctionalTest, RegisterWhenAsyncOperationPending) {
774 std::vector<std::string> sender_ids;
775 sender_ids.push_back("sender1");
776 // First start registration without waiting for it to complete.
777 Register(kTestAppID1,
778 sender_ids,
779 GCMDriverTest::DO_NOT_WAIT);
781 // Test that registration fails with async operation pending when there is a
782 // registration already in progress.
783 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
784 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
785 registration_result());
786 ClearResults();
788 // Complete the registration.
789 WaitForAsyncOperation();
790 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
792 // Start unregistration without waiting for it to complete. This time no async
793 // operation is pending.
794 Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT);
796 // Test that registration fails with async operation pending when there is an
797 // unregistration already in progress.
798 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
799 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
800 registration_result());
802 // Complete the first unregistration expecting success.
803 WaitForAsyncOperation();
804 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
806 // Test that it is ok to register again after unregistration.
807 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
808 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
811 TEST_F(GCMDriverFunctionalTest, Send) {
812 GCMClient::OutgoingMessage message;
813 message.id = "1";
814 message.data["key1"] = "value1";
815 message.data["key2"] = "value2";
816 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
818 EXPECT_EQ(message.id, send_message_id());
819 EXPECT_EQ(GCMClient::SUCCESS, send_result());
822 TEST_F(GCMDriverFunctionalTest, SendAfterSignOut) {
823 // This will trigger check-out.
824 SignOut();
826 GCMClient::OutgoingMessage message;
827 message.id = "1";
828 message.data["key1"] = "value1";
829 message.data["key2"] = "value2";
830 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
832 EXPECT_TRUE(send_message_id().empty());
833 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, send_result());
836 TEST_F(GCMDriverFunctionalTest, SendError) {
837 GCMClient::OutgoingMessage message;
838 // Embedding error in id will tell the mock to simulate the send error.
839 message.id = "1@error";
840 message.data["key1"] = "value1";
841 message.data["key2"] = "value2";
842 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
844 EXPECT_EQ(message.id, send_message_id());
845 EXPECT_EQ(GCMClient::SUCCESS, send_result());
847 // Wait for the send error.
848 gcm_app_handler()->WaitForNotification();
849 EXPECT_EQ(FakeGCMAppHandler::SEND_ERROR_EVENT,
850 gcm_app_handler()->received_event());
851 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
852 EXPECT_EQ(message.id,
853 gcm_app_handler()->send_error_details().message_id);
854 EXPECT_NE(GCMClient::SUCCESS,
855 gcm_app_handler()->send_error_details().result);
856 EXPECT_EQ(message.data,
857 gcm_app_handler()->send_error_details().additional_data);
860 TEST_F(GCMDriverFunctionalTest, MessageReceived) {
861 Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
862 GCMClient::IncomingMessage message;
863 message.data["key1"] = "value1";
864 message.data["key2"] = "value2";
865 message.sender_id = "sender";
866 GetGCMClient()->ReceiveMessage(kTestAppID1, message);
867 gcm_app_handler()->WaitForNotification();
868 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
869 gcm_app_handler()->received_event());
870 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
871 EXPECT_EQ(message.data, gcm_app_handler()->message().data);
872 EXPECT_TRUE(gcm_app_handler()->message().collapse_key.empty());
873 EXPECT_EQ(message.sender_id, gcm_app_handler()->message().sender_id);
876 TEST_F(GCMDriverFunctionalTest, MessageWithCollapseKeyReceived) {
877 Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
878 GCMClient::IncomingMessage message;
879 message.data["key1"] = "value1";
880 message.collapse_key = "collapse_key_value";
881 message.sender_id = "sender";
882 GetGCMClient()->ReceiveMessage(kTestAppID1, message);
883 gcm_app_handler()->WaitForNotification();
884 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
885 gcm_app_handler()->received_event());
886 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
887 EXPECT_EQ(message.data, gcm_app_handler()->message().data);
888 EXPECT_EQ(message.collapse_key,
889 gcm_app_handler()->message().collapse_key);
892 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) {
893 GetGCMClient()->DeleteMessages(kTestAppID1);
894 gcm_app_handler()->WaitForNotification();
895 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT,
896 gcm_app_handler()->received_event());
897 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
900 } // namespace gcm