Use the host instead of the title for streamlined hosted apps.
[chromium-blink-merge.git] / components / gcm_driver / gcm_driver_desktop_unittest.cc
blob495e2c400909ff18872110f2c1ba1303a86710a0
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_desktop.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/metrics/field_trial.h"
14 #include "base/run_loop.h"
15 #include "base/strings/string_util.h"
16 #include "base/test/test_simple_task_runner.h"
17 #include "base/threading/thread.h"
18 #include "components/gcm_driver/fake_gcm_app_handler.h"
19 #include "components/gcm_driver/fake_gcm_client.h"
20 #include "components/gcm_driver/fake_gcm_client_factory.h"
21 #include "components/gcm_driver/gcm_app_handler.h"
22 #include "components/gcm_driver/gcm_client_factory.h"
23 #include "net/url_request/url_request_context_getter.h"
24 #include "net/url_request/url_request_test_util.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 namespace gcm {
29 namespace {
31 const char kTestAccountID1[] = "user1@example.com";
32 const char kTestAccountID2[] = "user2@example.com";
33 const char kTestAppID1[] = "TestApp1";
34 const char kTestAppID2[] = "TestApp2";
35 const char kUserID1[] = "user1";
37 void PumpCurrentLoop() {
38 base::MessageLoop::ScopedNestableTaskAllower
39 nestable_task_allower(base::MessageLoop::current());
40 base::RunLoop().RunUntilIdle();
43 void PumpUILoop() {
44 PumpCurrentLoop();
47 std::vector<std::string> ToSenderList(const std::string& sender_ids) {
48 std::vector<std::string> senders;
49 Tokenize(sender_ids, ",", &senders);
50 return senders;
53 } // namespace
55 class GCMDriverTest : public testing::Test {
56 public:
57 enum WaitToFinish {
58 DO_NOT_WAIT,
59 WAIT
62 GCMDriverTest();
63 virtual ~GCMDriverTest();
65 // testing::Test:
66 virtual void SetUp() OVERRIDE;
67 virtual void TearDown() OVERRIDE;
69 GCMDriver* driver() { return driver_.get(); }
70 FakeGCMAppHandler* gcm_app_handler() { return gcm_app_handler_.get(); }
71 const std::string& registration_id() const { return registration_id_; }
72 GCMClient::Result registration_result() const { return registration_result_; }
73 const std::string& send_message_id() const { return send_message_id_; }
74 GCMClient::Result send_result() const { return send_result_; }
75 GCMClient::Result unregistration_result() const {
76 return unregistration_result_;
79 void PumpIOLoop();
81 void ClearResults();
83 bool HasAppHandlers() const;
84 FakeGCMClient* GetGCMClient();
86 void CreateDriver(FakeGCMClient::StartMode gcm_client_start_mode);
87 void AddAppHandlers();
88 void RemoveAppHandlers();
90 void SignIn(const std::string& account_id);
91 void SignOut();
93 void Register(const std::string& app_id,
94 const std::vector<std::string>& sender_ids,
95 WaitToFinish wait_to_finish);
96 void Send(const std::string& app_id,
97 const std::string& receiver_id,
98 const GCMClient::OutgoingMessage& message,
99 WaitToFinish wait_to_finish);
100 void Unregister(const std::string& app_id, WaitToFinish wait_to_finish);
102 void WaitForAsyncOperation();
104 private:
105 void RegisterCompleted(const std::string& registration_id,
106 GCMClient::Result result);
107 void SendCompleted(const std::string& message_id, GCMClient::Result result);
108 void UnregisterCompleted(GCMClient::Result result);
110 base::ScopedTempDir temp_dir_;
111 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
112 base::MessageLoopForUI message_loop_;
113 base::Thread io_thread_;
114 base::FieldTrialList field_trial_list_;
115 scoped_ptr<GCMDriver> driver_;
116 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_;
118 base::Closure async_operation_completed_callback_;
120 std::string registration_id_;
121 GCMClient::Result registration_result_;
122 std::string send_message_id_;
123 GCMClient::Result send_result_;
124 GCMClient::Result unregistration_result_;
126 DISALLOW_COPY_AND_ASSIGN(GCMDriverTest);
129 GCMDriverTest::GCMDriverTest()
130 : task_runner_(new base::TestSimpleTaskRunner()),
131 io_thread_("IOThread"),
132 field_trial_list_(NULL),
133 registration_result_(GCMClient::UNKNOWN_ERROR),
134 send_result_(GCMClient::UNKNOWN_ERROR),
135 unregistration_result_(GCMClient::UNKNOWN_ERROR) {
138 GCMDriverTest::~GCMDriverTest() {
141 void GCMDriverTest::SetUp() {
142 io_thread_.Start();
143 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
146 void GCMDriverTest::TearDown() {
147 if (!driver_)
148 return;
150 driver_->Shutdown();
151 driver_.reset();
152 PumpIOLoop();
154 io_thread_.Stop();
157 void GCMDriverTest::PumpIOLoop() {
158 base::RunLoop run_loop;
159 io_thread_.message_loop_proxy()->PostTaskAndReply(
160 FROM_HERE,
161 base::Bind(&PumpCurrentLoop),
162 run_loop.QuitClosure());
163 run_loop.Run();
166 void GCMDriverTest::ClearResults() {
167 registration_id_.clear();
168 registration_result_ = GCMClient::UNKNOWN_ERROR;
170 send_message_id_.clear();
171 send_result_ = GCMClient::UNKNOWN_ERROR;
173 unregistration_result_ = GCMClient::UNKNOWN_ERROR;
176 bool GCMDriverTest::HasAppHandlers() const {
177 return !driver_->app_handlers().empty();
180 FakeGCMClient* GCMDriverTest::GetGCMClient() {
181 return static_cast<FakeGCMClient*>(driver_->GetGCMClientForTesting());
184 void GCMDriverTest::CreateDriver(
185 FakeGCMClient::StartMode gcm_client_start_mode) {
186 scoped_refptr<net::URLRequestContextGetter> request_context =
187 new net::TestURLRequestContextGetter(io_thread_.message_loop_proxy());
188 // TODO(johnme): Need equivalent test coverage of GCMDriverAndroid.
189 driver_.reset(new GCMDriverDesktop(
190 scoped_ptr<GCMClientFactory>(new FakeGCMClientFactory(
191 gcm_client_start_mode,
192 base::MessageLoopProxy::current(),
193 io_thread_.message_loop_proxy())).Pass(),
194 GCMClient::ChromeBuildInfo(),
195 temp_dir_.path(),
196 request_context,
197 base::MessageLoopProxy::current(),
198 io_thread_.message_loop_proxy(),
199 task_runner_));
201 gcm_app_handler_.reset(new FakeGCMAppHandler);
204 void GCMDriverTest::AddAppHandlers() {
205 driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get());
206 driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get());
209 void GCMDriverTest::RemoveAppHandlers() {
210 driver_->RemoveAppHandler(kTestAppID1);
211 driver_->RemoveAppHandler(kTestAppID2);
214 void GCMDriverTest::SignIn(const std::string& account_id) {
215 driver_->OnSignedIn();
216 PumpIOLoop();
217 PumpUILoop();
220 void GCMDriverTest::SignOut() {
221 driver_->Purge();
222 PumpIOLoop();
223 PumpUILoop();
226 void GCMDriverTest::Register(const std::string& app_id,
227 const std::vector<std::string>& sender_ids,
228 WaitToFinish wait_to_finish) {
229 base::RunLoop run_loop;
230 async_operation_completed_callback_ = run_loop.QuitClosure();
231 driver_->Register(app_id,
232 sender_ids,
233 base::Bind(&GCMDriverTest::RegisterCompleted,
234 base::Unretained(this)));
235 if (wait_to_finish == WAIT)
236 run_loop.Run();
239 void GCMDriverTest::Send(const std::string& app_id,
240 const std::string& receiver_id,
241 const GCMClient::OutgoingMessage& message,
242 WaitToFinish wait_to_finish) {
243 base::RunLoop run_loop;
244 async_operation_completed_callback_ = run_loop.QuitClosure();
245 driver_->Send(app_id,
246 receiver_id,
247 message,
248 base::Bind(&GCMDriverTest::SendCompleted,
249 base::Unretained(this)));
250 if (wait_to_finish == WAIT)
251 run_loop.Run();
254 void GCMDriverTest::Unregister(const std::string& app_id,
255 WaitToFinish wait_to_finish) {
256 base::RunLoop run_loop;
257 async_operation_completed_callback_ = run_loop.QuitClosure();
258 driver_->Unregister(app_id,
259 base::Bind(&GCMDriverTest::UnregisterCompleted,
260 base::Unretained(this)));
261 if (wait_to_finish == WAIT)
262 run_loop.Run();
265 void GCMDriverTest::WaitForAsyncOperation() {
266 base::RunLoop run_loop;
267 async_operation_completed_callback_ = run_loop.QuitClosure();
268 run_loop.Run();
271 void GCMDriverTest::RegisterCompleted(const std::string& registration_id,
272 GCMClient::Result result) {
273 registration_id_ = registration_id;
274 registration_result_ = result;
275 if (!async_operation_completed_callback_.is_null())
276 async_operation_completed_callback_.Run();
279 void GCMDriverTest::SendCompleted(const std::string& message_id,
280 GCMClient::Result result) {
281 send_message_id_ = message_id;
282 send_result_ = result;
283 if (!async_operation_completed_callback_.is_null())
284 async_operation_completed_callback_.Run();
287 void GCMDriverTest::UnregisterCompleted(GCMClient::Result result) {
288 unregistration_result_ = result;
289 if (!async_operation_completed_callback_.is_null())
290 async_operation_completed_callback_.Run();
293 TEST_F(GCMDriverTest, Create) {
294 // Create GCMDriver first. GCM is not started.
295 CreateDriver(FakeGCMClient::NO_DELAY_START);
296 EXPECT_FALSE(driver()->IsStarted());
298 // Sign in. GCM is still not started.
299 SignIn(kTestAccountID1);
300 EXPECT_FALSE(driver()->IsStarted());
301 EXPECT_FALSE(driver()->IsConnected());
302 EXPECT_FALSE(gcm_app_handler()->connected());
304 // GCM will be started only after both sign-in and app handler being added.
305 AddAppHandlers();
306 EXPECT_TRUE(driver()->IsStarted());
307 PumpIOLoop();
308 EXPECT_TRUE(driver()->IsConnected());
309 EXPECT_TRUE(gcm_app_handler()->connected());
312 TEST_F(GCMDriverTest, CreateByFieldTrial) {
313 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("GCM", "Enabled"));
315 // Create GCMDriver first. GCM is not started.
316 CreateDriver(FakeGCMClient::NO_DELAY_START);
317 EXPECT_FALSE(driver()->IsStarted());
318 EXPECT_FALSE(driver()->IsConnected());
319 EXPECT_FALSE(gcm_app_handler()->connected());
321 // GCM will be started after app handler is added.
322 AddAppHandlers();
323 EXPECT_TRUE(driver()->IsStarted());
324 PumpIOLoop();
325 EXPECT_TRUE(driver()->IsConnected());
326 EXPECT_TRUE(gcm_app_handler()->connected());
329 TEST_F(GCMDriverTest, Shutdown) {
330 CreateDriver(FakeGCMClient::NO_DELAY_START);
331 EXPECT_FALSE(HasAppHandlers());
333 AddAppHandlers();
334 EXPECT_TRUE(HasAppHandlers());
336 driver()->Shutdown();
337 EXPECT_FALSE(HasAppHandlers());
338 EXPECT_FALSE(driver()->IsConnected());
339 EXPECT_FALSE(gcm_app_handler()->connected());
342 TEST_F(GCMDriverTest, SignInAndSignOutOnGCMEnabled) {
343 // By default, GCM is enabled.
344 CreateDriver(FakeGCMClient::NO_DELAY_START);
345 AddAppHandlers();
347 // GCMClient should be started after sign-in.
348 SignIn(kTestAccountID1);
349 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
351 // GCMClient should be checked out after sign-out.
352 SignOut();
353 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status());
356 TEST_F(GCMDriverTest, SignInAndSignOutOnGCMDisabled) {
357 // By default, GCM is enabled.
358 CreateDriver(FakeGCMClient::NO_DELAY_START);
359 AddAppHandlers();
361 // Disable GCM.
362 driver()->Disable();
364 // GCMClient should not be started after sign-in.
365 SignIn(kTestAccountID1);
366 EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status());
368 // Check-out should still be performed after sign-out.
369 SignOut();
370 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status());
373 TEST_F(GCMDriverTest, SignOutAndThenSignIn) {
374 CreateDriver(FakeGCMClient::NO_DELAY_START);
375 AddAppHandlers();
377 // GCMClient should be started after sign-in.
378 SignIn(kTestAccountID1);
379 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
381 // GCMClient should be checked out after sign-out.
382 SignOut();
383 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status());
385 // Sign-in with a different account.
386 SignIn(kTestAccountID2);
388 // GCMClient should be started again.
389 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
392 TEST_F(GCMDriverTest, DisableAndReenableGCM) {
393 CreateDriver(FakeGCMClient::NO_DELAY_START);
394 AddAppHandlers();
395 SignIn(kTestAccountID1);
397 // GCMClient should be started.
398 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
400 // Disables the GCM.
401 driver()->Disable();
402 PumpIOLoop();
403 PumpUILoop();
405 // GCMClient should be stopped.
406 EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status());
408 // Enables the GCM.
409 driver()->Enable();
410 PumpIOLoop();
411 PumpUILoop();
413 // GCMClient should be started.
414 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
416 // Disables the GCM.
417 driver()->Disable();
418 PumpIOLoop();
419 PumpUILoop();
421 // GCMClient should be stopped.
422 EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status());
424 // Sign out.
425 SignOut();
427 // GCMClient should be checked out.
428 EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status());
431 TEST_F(GCMDriverTest, StartOrStopGCMOnDemand) {
432 CreateDriver(FakeGCMClient::NO_DELAY_START);
433 SignIn(kTestAccountID1);
435 // GCMClient is not started.
436 EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status());
438 // GCMClient is started after an app handler has been added.
439 driver()->AddAppHandler(kTestAppID1, gcm_app_handler());
440 PumpIOLoop();
441 PumpUILoop();
442 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
444 // Add another app handler.
445 driver()->AddAppHandler(kTestAppID2, gcm_app_handler());
446 PumpIOLoop();
447 PumpUILoop();
448 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
450 // GCMClient remains active after one app handler is gone.
451 driver()->RemoveAppHandler(kTestAppID1);
452 PumpIOLoop();
453 PumpUILoop();
454 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
456 // GCMClient should be stopped after the last app handler is gone.
457 driver()->RemoveAppHandler(kTestAppID2);
458 PumpIOLoop();
459 PumpUILoop();
460 EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status());
462 // GCMClient is restarted after an app handler has been added.
463 driver()->AddAppHandler(kTestAppID2, gcm_app_handler());
464 PumpIOLoop();
465 PumpUILoop();
466 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
469 TEST_F(GCMDriverTest, RegisterFailed) {
470 std::vector<std::string> sender_ids;
471 sender_ids.push_back("sender1");
473 CreateDriver(FakeGCMClient::NO_DELAY_START);
475 // Registration fails when GCM is disabled.
476 driver()->Disable();
477 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
478 EXPECT_TRUE(registration_id().empty());
479 EXPECT_EQ(GCMClient::GCM_DISABLED, registration_result());
481 ClearResults();
483 // Registration fails when the sign-in does not occur.
484 driver()->Enable();
485 AddAppHandlers();
486 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
487 EXPECT_TRUE(registration_id().empty());
488 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, registration_result());
490 ClearResults();
492 // Registration fails when the no app handler is added.
493 RemoveAppHandlers();
494 SignIn(kTestAccountID1);
495 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
496 EXPECT_TRUE(registration_id().empty());
497 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result());
500 TEST_F(GCMDriverTest, UnregisterFailed) {
501 CreateDriver(FakeGCMClient::NO_DELAY_START);
503 // Unregistration fails when GCM is disabled.
504 driver()->Disable();
505 Unregister(kTestAppID1, GCMDriverTest::WAIT);
506 EXPECT_EQ(GCMClient::GCM_DISABLED, unregistration_result());
508 ClearResults();
510 // Unregistration fails when the sign-in does not occur.
511 driver()->Enable();
512 AddAppHandlers();
513 Unregister(kTestAppID1, GCMDriverTest::WAIT);
514 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, unregistration_result());
516 ClearResults();
518 // Unregistration fails when the no app handler is added.
519 RemoveAppHandlers();
520 SignIn(kTestAccountID1);
521 Unregister(kTestAppID1, GCMDriverTest::WAIT);
522 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, unregistration_result());
525 TEST_F(GCMDriverTest, SendFailed) {
526 GCMClient::OutgoingMessage message;
527 message.id = "1";
528 message.data["key1"] = "value1";
530 CreateDriver(FakeGCMClient::NO_DELAY_START);
532 // Sending fails when GCM is disabled.
533 driver()->Disable();
534 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
535 EXPECT_TRUE(send_message_id().empty());
536 EXPECT_EQ(GCMClient::GCM_DISABLED, send_result());
538 ClearResults();
540 // Sending fails when the sign-in does not occur.
541 driver()->Enable();
542 AddAppHandlers();
543 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
544 EXPECT_TRUE(send_message_id().empty());
545 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, send_result());
547 ClearResults();
549 // Sending fails when the no app handler is added.
550 RemoveAppHandlers();
551 SignIn(kTestAccountID1);
552 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
553 EXPECT_TRUE(send_message_id().empty());
554 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, send_result());
557 TEST_F(GCMDriverTest, GCMClientNotReadyBeforeRegistration) {
558 // Make GCMClient not ready initially.
559 CreateDriver(FakeGCMClient::DELAY_START);
560 SignIn(kTestAccountID1);
561 AddAppHandlers();
563 // The registration is on hold until GCMClient is ready.
564 std::vector<std::string> sender_ids;
565 sender_ids.push_back("sender1");
566 Register(kTestAppID1,
567 sender_ids,
568 GCMDriverTest::DO_NOT_WAIT);
569 PumpIOLoop();
570 PumpUILoop();
571 EXPECT_TRUE(registration_id().empty());
572 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result());
574 // Register operation will be invoked after GCMClient becomes ready.
575 GetGCMClient()->PerformDelayedLoading();
576 WaitForAsyncOperation();
577 EXPECT_FALSE(registration_id().empty());
578 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
581 TEST_F(GCMDriverTest, GCMClientNotReadyBeforeSending) {
582 // Make GCMClient not ready initially.
583 CreateDriver(FakeGCMClient::DELAY_START);
584 SignIn(kTestAccountID1);
585 AddAppHandlers();
587 // The sending is on hold until GCMClient is ready.
588 GCMClient::OutgoingMessage message;
589 message.id = "1";
590 message.data["key1"] = "value1";
591 message.data["key2"] = "value2";
592 Send(kTestAppID1, kUserID1, message, GCMDriverTest::DO_NOT_WAIT);
593 PumpIOLoop();
594 PumpUILoop();
596 EXPECT_TRUE(send_message_id().empty());
597 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, send_result());
599 // Send operation will be invoked after GCMClient becomes ready.
600 GetGCMClient()->PerformDelayedLoading();
601 WaitForAsyncOperation();
602 EXPECT_EQ(message.id, send_message_id());
603 EXPECT_EQ(GCMClient::SUCCESS, send_result());
606 // Tests a single instance of GCMDriver.
607 class GCMDriverFunctionalTest : public GCMDriverTest {
608 public:
609 GCMDriverFunctionalTest();
610 virtual ~GCMDriverFunctionalTest();
612 // GCMDriverTest:
613 virtual void SetUp() OVERRIDE;
615 private:
616 DISALLOW_COPY_AND_ASSIGN(GCMDriverFunctionalTest);
619 GCMDriverFunctionalTest::GCMDriverFunctionalTest() {
622 GCMDriverFunctionalTest::~GCMDriverFunctionalTest() {
625 void GCMDriverFunctionalTest::SetUp() {
626 GCMDriverTest::SetUp();
628 CreateDriver(FakeGCMClient::NO_DELAY_START);
629 AddAppHandlers();
630 SignIn(kTestAccountID1);
633 TEST_F(GCMDriverFunctionalTest, Register) {
634 std::vector<std::string> sender_ids;
635 sender_ids.push_back("sender1");
636 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
637 const std::string expected_registration_id =
638 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids);
640 EXPECT_EQ(expected_registration_id, registration_id());
641 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
644 TEST_F(GCMDriverFunctionalTest, RegisterError) {
645 std::vector<std::string> sender_ids;
646 sender_ids.push_back("sender1@error");
647 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
649 EXPECT_TRUE(registration_id().empty());
650 EXPECT_NE(GCMClient::SUCCESS, registration_result());
653 TEST_F(GCMDriverFunctionalTest, RegisterAgainWithSameSenderIDs) {
654 std::vector<std::string> sender_ids;
655 sender_ids.push_back("sender1");
656 sender_ids.push_back("sender2");
657 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
658 const std::string expected_registration_id =
659 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids);
661 EXPECT_EQ(expected_registration_id, registration_id());
662 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
664 // Clears the results the would be set by the Register callback in preparation
665 // to call register 2nd time.
666 ClearResults();
668 // Calling register 2nd time with the same set of sender IDs but different
669 // ordering will get back the same registration ID.
670 std::vector<std::string> another_sender_ids;
671 another_sender_ids.push_back("sender2");
672 another_sender_ids.push_back("sender1");
673 Register(kTestAppID1, another_sender_ids, GCMDriverTest::WAIT);
675 EXPECT_EQ(expected_registration_id, registration_id());
676 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
679 TEST_F(GCMDriverFunctionalTest, RegisterAgainWithDifferentSenderIDs) {
680 std::vector<std::string> sender_ids;
681 sender_ids.push_back("sender1");
682 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
683 const std::string expected_registration_id =
684 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids);
686 EXPECT_EQ(expected_registration_id, registration_id());
687 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
689 // Make sender IDs different.
690 sender_ids.push_back("sender2");
691 const std::string expected_registration_id2 =
692 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids);
694 // Calling register 2nd time with the different sender IDs will get back a new
695 // registration ID.
696 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
697 EXPECT_EQ(expected_registration_id2, registration_id());
698 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
701 TEST_F(GCMDriverFunctionalTest, RegisterAfterSignOut) {
702 // This will trigger check-out.
703 SignOut();
705 std::vector<std::string> sender_ids;
706 sender_ids.push_back("sender1");
707 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
709 EXPECT_TRUE(registration_id().empty());
710 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, registration_result());
713 TEST_F(GCMDriverFunctionalTest, UnregisterExplicitly) {
714 std::vector<std::string> sender_ids;
715 sender_ids.push_back("sender1");
716 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
718 EXPECT_FALSE(registration_id().empty());
719 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
721 Unregister(kTestAppID1, GCMDriverTest::WAIT);
723 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
726 TEST_F(GCMDriverFunctionalTest, UnregisterWhenAsyncOperationPending) {
727 std::vector<std::string> sender_ids;
728 sender_ids.push_back("sender1");
729 // First start registration without waiting for it to complete.
730 Register(kTestAppID1,
731 sender_ids,
732 GCMDriverTest::DO_NOT_WAIT);
734 // Test that unregistration fails with async operation pending when there is a
735 // registration already in progress.
736 Unregister(kTestAppID1, GCMDriverTest::WAIT);
737 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
738 unregistration_result());
740 // Complete the unregistration.
741 WaitForAsyncOperation();
742 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
744 // Start unregistration without waiting for it to complete. This time no async
745 // operation is pending.
746 Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT);
748 // Test that unregistration fails with async operation pending when there is
749 // an unregistration already in progress.
750 Unregister(kTestAppID1, GCMDriverTest::WAIT);
751 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
752 unregistration_result());
753 ClearResults();
755 // Complete unregistration.
756 WaitForAsyncOperation();
757 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
760 TEST_F(GCMDriverFunctionalTest, RegisterWhenAsyncOperationPending) {
761 std::vector<std::string> sender_ids;
762 sender_ids.push_back("sender1");
763 // First start registration without waiting for it to complete.
764 Register(kTestAppID1,
765 sender_ids,
766 GCMDriverTest::DO_NOT_WAIT);
768 // Test that registration fails with async operation pending when there is a
769 // registration already in progress.
770 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
771 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
772 registration_result());
773 ClearResults();
775 // Complete the registration.
776 WaitForAsyncOperation();
777 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
779 // Start unregistration without waiting for it to complete. This time no async
780 // operation is pending.
781 Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT);
783 // Test that registration fails with async operation pending when there is an
784 // unregistration already in progress.
785 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
786 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
787 registration_result());
789 // Complete the first unregistration expecting success.
790 WaitForAsyncOperation();
791 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
793 // Test that it is ok to register again after unregistration.
794 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
795 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
798 TEST_F(GCMDriverFunctionalTest, Send) {
799 GCMClient::OutgoingMessage message;
800 message.id = "1@ack";
801 message.data["key1"] = "value1";
802 message.data["key2"] = "value2";
803 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
805 EXPECT_EQ(message.id, send_message_id());
806 EXPECT_EQ(GCMClient::SUCCESS, send_result());
808 gcm_app_handler()->WaitForNotification();
809 EXPECT_EQ(message.id, gcm_app_handler()->acked_message_id());
810 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
813 TEST_F(GCMDriverFunctionalTest, SendAfterSignOut) {
814 // This will trigger check-out.
815 SignOut();
817 GCMClient::OutgoingMessage message;
818 message.id = "1";
819 message.data["key1"] = "value1";
820 message.data["key2"] = "value2";
821 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
823 EXPECT_TRUE(send_message_id().empty());
824 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, send_result());
827 TEST_F(GCMDriverFunctionalTest, SendError) {
828 GCMClient::OutgoingMessage message;
829 // Embedding error in id will tell the mock to simulate the send error.
830 message.id = "1@error";
831 message.data["key1"] = "value1";
832 message.data["key2"] = "value2";
833 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
835 EXPECT_EQ(message.id, send_message_id());
836 EXPECT_EQ(GCMClient::SUCCESS, send_result());
838 // Wait for the send error.
839 gcm_app_handler()->WaitForNotification();
840 EXPECT_EQ(FakeGCMAppHandler::SEND_ERROR_EVENT,
841 gcm_app_handler()->received_event());
842 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
843 EXPECT_EQ(message.id,
844 gcm_app_handler()->send_error_details().message_id);
845 EXPECT_NE(GCMClient::SUCCESS,
846 gcm_app_handler()->send_error_details().result);
847 EXPECT_EQ(message.data,
848 gcm_app_handler()->send_error_details().additional_data);
851 TEST_F(GCMDriverFunctionalTest, MessageReceived) {
852 Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
853 GCMClient::IncomingMessage message;
854 message.data["key1"] = "value1";
855 message.data["key2"] = "value2";
856 message.sender_id = "sender";
857 GetGCMClient()->ReceiveMessage(kTestAppID1, message);
858 gcm_app_handler()->WaitForNotification();
859 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
860 gcm_app_handler()->received_event());
861 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
862 EXPECT_EQ(message.data, gcm_app_handler()->message().data);
863 EXPECT_TRUE(gcm_app_handler()->message().collapse_key.empty());
864 EXPECT_EQ(message.sender_id, gcm_app_handler()->message().sender_id);
867 TEST_F(GCMDriverFunctionalTest, MessageWithCollapseKeyReceived) {
868 Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
869 GCMClient::IncomingMessage message;
870 message.data["key1"] = "value1";
871 message.collapse_key = "collapse_key_value";
872 message.sender_id = "sender";
873 GetGCMClient()->ReceiveMessage(kTestAppID1, message);
874 gcm_app_handler()->WaitForNotification();
875 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
876 gcm_app_handler()->received_event());
877 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
878 EXPECT_EQ(message.data, gcm_app_handler()->message().data);
879 EXPECT_EQ(message.collapse_key,
880 gcm_app_handler()->message().collapse_key);
883 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) {
884 GetGCMClient()->DeleteMessages(kTestAppID1);
885 gcm_app_handler()->WaitForNotification();
886 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT,
887 gcm_app_handler()->received_event());
888 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
891 } // namespace gcm