gcm_driver_desktop_unittest.cc revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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.
4
5#include "components/gcm_driver/gcm_driver_desktop.h"
6
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 "net/url_request/url_request_context_getter.h"
23#include "net/url_request/url_request_test_util.h"
24#include "testing/gtest/include/gtest/gtest.h"
25
26namespace gcm {
27
28namespace {
29
30const char kTestAccountID1[] = "user1@example.com";
31const char kTestAccountID2[] = "user2@example.com";
32const char kTestAppID1[] = "TestApp1";
33const char kTestAppID2[] = "TestApp2";
34const char kUserID1[] = "user1";
35
36void PumpCurrentLoop() {
37  base::MessageLoop::ScopedNestableTaskAllower
38      nestable_task_allower(base::MessageLoop::current());
39  base::RunLoop().RunUntilIdle();
40}
41
42void PumpUILoop() {
43  PumpCurrentLoop();
44}
45
46std::vector<std::string> ToSenderList(const std::string& sender_ids) {
47  std::vector<std::string> senders;
48  Tokenize(sender_ids, ",", &senders);
49  return senders;
50}
51
52}  // namespace
53
54class GCMDriverTest : public testing::Test {
55 public:
56  enum WaitToFinish {
57    DO_NOT_WAIT,
58    WAIT
59  };
60
61  GCMDriverTest();
62  virtual ~GCMDriverTest();
63
64  // testing::Test:
65  virtual void SetUp() OVERRIDE;
66  virtual void TearDown() OVERRIDE;
67
68  GCMDriver* driver() { return driver_.get(); }
69  FakeGCMAppHandler* gcm_app_handler() { return gcm_app_handler_.get(); }
70  const std::string& registration_id() const { return registration_id_; }
71  GCMClient::Result registration_result() const { return registration_result_; }
72  const std::string& send_message_id() const { return send_message_id_; }
73  GCMClient::Result send_result() const { return send_result_; }
74  GCMClient::Result unregistration_result() const {
75    return unregistration_result_;
76  }
77
78  void PumpIOLoop();
79
80  void ClearResults();
81
82  bool HasAppHandlers() const;
83  FakeGCMClient* GetGCMClient();
84
85  void CreateDriver(FakeGCMClient::StartMode gcm_client_start_mode);
86  void AddAppHandlers();
87  void RemoveAppHandlers();
88
89  void SignIn(const std::string& account_id);
90  void SignOut();
91
92  void Register(const std::string& app_id,
93                const std::vector<std::string>& sender_ids,
94                WaitToFinish wait_to_finish);
95  void Send(const std::string& app_id,
96            const std::string& receiver_id,
97            const GCMClient::OutgoingMessage& message,
98            WaitToFinish wait_to_finish);
99  void Unregister(const std::string& app_id, WaitToFinish wait_to_finish);
100
101  void WaitForAsyncOperation();
102
103 private:
104  void RegisterCompleted(const std::string& registration_id,
105                         GCMClient::Result result);
106  void SendCompleted(const std::string& message_id, GCMClient::Result result);
107  void UnregisterCompleted(GCMClient::Result result);
108
109  base::ScopedTempDir temp_dir_;
110  scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
111  base::MessageLoopForUI message_loop_;
112  base::Thread io_thread_;
113  scoped_ptr<GCMDriver> driver_;
114  scoped_ptr<FakeGCMAppHandler> gcm_app_handler_;
115
116  base::Closure async_operation_completed_callback_;
117
118  std::string registration_id_;
119  GCMClient::Result registration_result_;
120  std::string send_message_id_;
121  GCMClient::Result send_result_;
122  GCMClient::Result unregistration_result_;
123
124  DISALLOW_COPY_AND_ASSIGN(GCMDriverTest);
125};
126
127GCMDriverTest::GCMDriverTest()
128    : task_runner_(new base::TestSimpleTaskRunner()),
129      io_thread_("IOThread"),
130      registration_result_(GCMClient::UNKNOWN_ERROR),
131      send_result_(GCMClient::UNKNOWN_ERROR),
132      unregistration_result_(GCMClient::UNKNOWN_ERROR) {
133}
134
135GCMDriverTest::~GCMDriverTest() {
136}
137
138void GCMDriverTest::SetUp() {
139  io_thread_.Start();
140  ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
141}
142
143void GCMDriverTest::TearDown() {
144  if (!driver_)
145    return;
146
147  driver_->Shutdown();
148  driver_.reset();
149  PumpIOLoop();
150
151  io_thread_.Stop();
152}
153
154void GCMDriverTest::PumpIOLoop() {
155  base::RunLoop run_loop;
156  io_thread_.message_loop_proxy()->PostTaskAndReply(
157      FROM_HERE,
158      base::Bind(&PumpCurrentLoop),
159      run_loop.QuitClosure());
160  run_loop.Run();
161}
162
163void GCMDriverTest::ClearResults() {
164  registration_id_.clear();
165  registration_result_ = GCMClient::UNKNOWN_ERROR;
166
167  send_message_id_.clear();
168  send_result_ = GCMClient::UNKNOWN_ERROR;
169
170  unregistration_result_ = GCMClient::UNKNOWN_ERROR;
171}
172
173bool GCMDriverTest::HasAppHandlers() const {
174  return !driver_->app_handlers().empty();
175}
176
177FakeGCMClient* GCMDriverTest::GetGCMClient() {
178  return static_cast<FakeGCMClient*>(driver_->GetGCMClientForTesting());
179}
180
181void GCMDriverTest::CreateDriver(
182    FakeGCMClient::StartMode gcm_client_start_mode) {
183  scoped_refptr<net::URLRequestContextGetter> request_context =
184      new net::TestURLRequestContextGetter(io_thread_.message_loop_proxy());
185  // TODO(johnme): Need equivalent test coverage of GCMDriverAndroid.
186  driver_.reset(new GCMDriverDesktop(
187      scoped_ptr<GCMClientFactory>(new FakeGCMClientFactory(
188          gcm_client_start_mode,
189          base::MessageLoopProxy::current(),
190          io_thread_.message_loop_proxy())).Pass(),
191      GCMClient::ChromeBuildInfo(),
192      temp_dir_.path(),
193      request_context,
194      base::MessageLoopProxy::current(),
195      io_thread_.message_loop_proxy(),
196      task_runner_));
197
198  gcm_app_handler_.reset(new FakeGCMAppHandler);
199}
200
201void GCMDriverTest::AddAppHandlers() {
202  driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get());
203  driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get());
204}
205
206void GCMDriverTest::RemoveAppHandlers() {
207  driver_->RemoveAppHandler(kTestAppID1);
208  driver_->RemoveAppHandler(kTestAppID2);
209}
210
211void GCMDriverTest::SignIn(const std::string& account_id) {
212  driver_->OnSignedIn();
213  PumpIOLoop();
214  PumpUILoop();
215}
216
217void GCMDriverTest::SignOut() {
218  driver_->Purge();
219  PumpIOLoop();
220  PumpUILoop();
221}
222
223void GCMDriverTest::Register(const std::string& app_id,
224                             const std::vector<std::string>& sender_ids,
225                             WaitToFinish wait_to_finish) {
226  base::RunLoop run_loop;
227  async_operation_completed_callback_ = run_loop.QuitClosure();
228  driver_->Register(app_id,
229                     sender_ids,
230                     base::Bind(&GCMDriverTest::RegisterCompleted,
231                                base::Unretained(this)));
232  if (wait_to_finish == WAIT)
233    run_loop.Run();
234}
235
236void GCMDriverTest::Send(const std::string& app_id,
237                         const std::string& receiver_id,
238                         const GCMClient::OutgoingMessage& message,
239                         WaitToFinish wait_to_finish) {
240  base::RunLoop run_loop;
241  async_operation_completed_callback_ = run_loop.QuitClosure();
242  driver_->Send(app_id,
243                 receiver_id,
244                 message,
245                 base::Bind(&GCMDriverTest::SendCompleted,
246                            base::Unretained(this)));
247  if (wait_to_finish == WAIT)
248    run_loop.Run();
249}
250
251void GCMDriverTest::Unregister(const std::string& app_id,
252                               WaitToFinish wait_to_finish) {
253  base::RunLoop run_loop;
254  async_operation_completed_callback_ = run_loop.QuitClosure();
255  driver_->Unregister(app_id,
256                       base::Bind(&GCMDriverTest::UnregisterCompleted,
257                                  base::Unretained(this)));
258  if (wait_to_finish == WAIT)
259    run_loop.Run();
260}
261
262void GCMDriverTest::WaitForAsyncOperation() {
263  base::RunLoop run_loop;
264  async_operation_completed_callback_ = run_loop.QuitClosure();
265  run_loop.Run();
266}
267
268void GCMDriverTest::RegisterCompleted(const std::string& registration_id,
269                                      GCMClient::Result result) {
270  registration_id_ = registration_id;
271  registration_result_ = result;
272  if (!async_operation_completed_callback_.is_null())
273    async_operation_completed_callback_.Run();
274}
275
276void GCMDriverTest::SendCompleted(const std::string& message_id,
277                                  GCMClient::Result result) {
278  send_message_id_ = message_id;
279  send_result_ = result;
280  if (!async_operation_completed_callback_.is_null())
281    async_operation_completed_callback_.Run();
282}
283
284void GCMDriverTest::UnregisterCompleted(GCMClient::Result result) {
285  unregistration_result_ = result;
286  if (!async_operation_completed_callback_.is_null())
287    async_operation_completed_callback_.Run();
288}
289
290TEST_F(GCMDriverTest, Create) {
291  // Create GCMDriver first. GCM is not started.
292  CreateDriver(FakeGCMClient::NO_DELAY_START);
293  EXPECT_FALSE(driver()->IsStarted());
294
295  // Sign in. GCM is still not started.
296  SignIn(kTestAccountID1);
297  EXPECT_FALSE(driver()->IsStarted());
298  EXPECT_FALSE(driver()->IsConnected());
299  EXPECT_FALSE(gcm_app_handler()->connected());
300
301  // GCM will be started only after both sign-in and app handler being added.
302  AddAppHandlers();
303  EXPECT_TRUE(driver()->IsStarted());
304  PumpIOLoop();
305  EXPECT_TRUE(driver()->IsConnected());
306  EXPECT_TRUE(gcm_app_handler()->connected());
307}
308
309TEST_F(GCMDriverTest, Shutdown) {
310  CreateDriver(FakeGCMClient::NO_DELAY_START);
311  EXPECT_FALSE(HasAppHandlers());
312
313  AddAppHandlers();
314  EXPECT_TRUE(HasAppHandlers());
315
316  driver()->Shutdown();
317  EXPECT_FALSE(HasAppHandlers());
318  EXPECT_FALSE(driver()->IsConnected());
319  EXPECT_FALSE(gcm_app_handler()->connected());
320}
321
322TEST_F(GCMDriverTest, SignInAndSignOutOnGCMEnabled) {
323  // By default, GCM is enabled.
324  CreateDriver(FakeGCMClient::NO_DELAY_START);
325  AddAppHandlers();
326
327  // GCMClient should be started after sign-in.
328  SignIn(kTestAccountID1);
329  EXPECT_TRUE(driver()->IsGCMClientReady());
330  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
331
332  // GCMClient should be checked out after sign-out.
333  SignOut();
334  EXPECT_FALSE(driver()->IsGCMClientReady());
335  EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status());
336}
337
338TEST_F(GCMDriverTest, SignInAndSignOutOnGCMDisabled) {
339  // By default, GCM is enabled.
340  CreateDriver(FakeGCMClient::NO_DELAY_START);
341  AddAppHandlers();
342
343  // Disable GCM.
344  driver()->Disable();
345
346  // GCMClient should not be started after sign-in.
347  SignIn(kTestAccountID1);
348  EXPECT_FALSE(driver()->IsGCMClientReady());
349  EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status());
350
351  // Check-out should still be performed after sign-out.
352  SignOut();
353  EXPECT_FALSE(driver()->IsGCMClientReady());
354  EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status());
355}
356
357TEST_F(GCMDriverTest, SignOutAndThenSignIn) {
358  CreateDriver(FakeGCMClient::NO_DELAY_START);
359  AddAppHandlers();
360
361  // GCMClient should be started after sign-in.
362  SignIn(kTestAccountID1);
363  EXPECT_TRUE(driver()->IsGCMClientReady());
364  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
365
366  // GCMClient should be checked out after sign-out.
367  SignOut();
368  EXPECT_FALSE(driver()->IsGCMClientReady());
369  EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status());
370
371  // Sign-in with a different account.
372  SignIn(kTestAccountID2);
373
374  // GCMClient should be started again.
375  EXPECT_TRUE(driver()->IsGCMClientReady());
376  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
377}
378
379TEST_F(GCMDriverTest, DisableAndReenableGCM) {
380  CreateDriver(FakeGCMClient::NO_DELAY_START);
381  AddAppHandlers();
382  SignIn(kTestAccountID1);
383
384  // GCMClient should be started.
385  EXPECT_TRUE(driver()->IsGCMClientReady());
386  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
387
388  // Disables the GCM.
389  driver()->Disable();
390  PumpIOLoop();
391  PumpUILoop();
392
393  // GCMClient should be stopped.
394  EXPECT_FALSE(driver()->IsGCMClientReady());
395  EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status());
396
397  // Enables the GCM.
398  driver()->Enable();
399  PumpIOLoop();
400  PumpUILoop();
401
402  // GCMClient should be started.
403  EXPECT_TRUE(driver()->IsGCMClientReady());
404  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
405
406  // Disables the GCM.
407  driver()->Disable();
408  PumpIOLoop();
409  PumpUILoop();
410
411  // GCMClient should be stopped.
412  EXPECT_FALSE(driver()->IsGCMClientReady());
413  EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status());
414
415  // Sign out.
416  SignOut();
417
418  // GCMClient should be checked out.
419  EXPECT_FALSE(driver()->IsGCMClientReady());
420  EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status());
421}
422
423TEST_F(GCMDriverTest, StartOrStopGCMOnDemand) {
424  CreateDriver(FakeGCMClient::NO_DELAY_START);
425  SignIn(kTestAccountID1);
426
427  // GCMClient is not started.
428  EXPECT_FALSE(driver()->IsGCMClientReady());
429  EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status());
430
431  // GCMClient is started after an app handler has been added.
432  driver()->AddAppHandler(kTestAppID1, gcm_app_handler());
433  PumpIOLoop();
434  PumpUILoop();
435  EXPECT_TRUE(driver()->IsGCMClientReady());
436  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
437
438  // Add another app handler.
439  driver()->AddAppHandler(kTestAppID2, gcm_app_handler());
440  PumpIOLoop();
441  PumpUILoop();
442  EXPECT_TRUE(driver()->IsGCMClientReady());
443  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
444
445  // GCMClient remains active after one app handler is gone.
446  driver()->RemoveAppHandler(kTestAppID1);
447  PumpIOLoop();
448  PumpUILoop();
449  EXPECT_TRUE(driver()->IsGCMClientReady());
450  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
451
452  // GCMClient should be stopped after the last app handler is gone.
453  driver()->RemoveAppHandler(kTestAppID2);
454  PumpIOLoop();
455  PumpUILoop();
456  EXPECT_FALSE(driver()->IsGCMClientReady());
457  EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status());
458
459  // GCMClient is restarted after an app handler has been added.
460  driver()->AddAppHandler(kTestAppID2, gcm_app_handler());
461  PumpIOLoop();
462  PumpUILoop();
463  EXPECT_TRUE(driver()->IsGCMClientReady());
464  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
465}
466
467TEST_F(GCMDriverTest, RegisterFailed) {
468  std::vector<std::string> sender_ids;
469  sender_ids.push_back("sender1");
470
471  CreateDriver(FakeGCMClient::NO_DELAY_START);
472
473  // Registration fails when GCM is disabled.
474  driver()->Disable();
475  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
476  EXPECT_TRUE(registration_id().empty());
477  EXPECT_EQ(GCMClient::GCM_DISABLED, registration_result());
478
479  ClearResults();
480
481  // Registration fails when the sign-in does not occur.
482  driver()->Enable();
483  AddAppHandlers();
484  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
485  EXPECT_TRUE(registration_id().empty());
486  EXPECT_EQ(GCMClient::NOT_SIGNED_IN, registration_result());
487
488  ClearResults();
489
490  // Registration fails when the no app handler is added.
491  RemoveAppHandlers();
492  SignIn(kTestAccountID1);
493  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
494  EXPECT_TRUE(registration_id().empty());
495  EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result());
496}
497
498TEST_F(GCMDriverTest, UnregisterFailed) {
499  CreateDriver(FakeGCMClient::NO_DELAY_START);
500
501  // Unregistration fails when GCM is disabled.
502  driver()->Disable();
503  Unregister(kTestAppID1, GCMDriverTest::WAIT);
504  EXPECT_EQ(GCMClient::GCM_DISABLED, unregistration_result());
505
506  ClearResults();
507
508  // Unregistration fails when the sign-in does not occur.
509  driver()->Enable();
510  AddAppHandlers();
511  Unregister(kTestAppID1, GCMDriverTest::WAIT);
512  EXPECT_EQ(GCMClient::NOT_SIGNED_IN, unregistration_result());
513
514  ClearResults();
515
516  // Unregistration fails when the no app handler is added.
517  RemoveAppHandlers();
518  SignIn(kTestAccountID1);
519  Unregister(kTestAppID1, GCMDriverTest::WAIT);
520  EXPECT_EQ(GCMClient::UNKNOWN_ERROR, unregistration_result());
521}
522
523TEST_F(GCMDriverTest, SendFailed) {
524  GCMClient::OutgoingMessage message;
525  message.id = "1";
526  message.data["key1"] = "value1";
527
528  CreateDriver(FakeGCMClient::NO_DELAY_START);
529
530  // Sending fails when GCM is disabled.
531  driver()->Disable();
532  Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
533  EXPECT_TRUE(send_message_id().empty());
534  EXPECT_EQ(GCMClient::GCM_DISABLED, send_result());
535
536  ClearResults();
537
538  // Sending fails when the sign-in does not occur.
539  driver()->Enable();
540  AddAppHandlers();
541  Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
542  EXPECT_TRUE(send_message_id().empty());
543  EXPECT_EQ(GCMClient::NOT_SIGNED_IN, send_result());
544
545  ClearResults();
546
547  // Sending fails when the no app handler is added.
548  RemoveAppHandlers();
549  SignIn(kTestAccountID1);
550  Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
551  EXPECT_TRUE(send_message_id().empty());
552  EXPECT_EQ(GCMClient::UNKNOWN_ERROR, send_result());
553}
554
555TEST_F(GCMDriverTest, GCMClientNotReadyBeforeRegistration) {
556  // Make GCMClient not ready initially.
557  CreateDriver(FakeGCMClient::DELAY_START);
558  SignIn(kTestAccountID1);
559  AddAppHandlers();
560
561  // The registration is on hold until GCMClient is ready.
562  std::vector<std::string> sender_ids;
563  sender_ids.push_back("sender1");
564  Register(kTestAppID1,
565                     sender_ids,
566                     GCMDriverTest::DO_NOT_WAIT);
567  PumpIOLoop();
568  PumpUILoop();
569  EXPECT_TRUE(registration_id().empty());
570  EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result());
571
572  // Register operation will be invoked after GCMClient becomes ready.
573  GetGCMClient()->PerformDelayedLoading();
574  WaitForAsyncOperation();
575  EXPECT_FALSE(registration_id().empty());
576  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
577}
578
579TEST_F(GCMDriverTest, GCMClientNotReadyBeforeSending) {
580  // Make GCMClient not ready initially.
581  CreateDriver(FakeGCMClient::DELAY_START);
582  SignIn(kTestAccountID1);
583  AddAppHandlers();
584
585  // The sending is on hold until GCMClient is ready.
586  GCMClient::OutgoingMessage message;
587  message.id = "1";
588  message.data["key1"] = "value1";
589  message.data["key2"] = "value2";
590  Send(kTestAppID1, kUserID1, message, GCMDriverTest::DO_NOT_WAIT);
591  PumpIOLoop();
592  PumpUILoop();
593
594  EXPECT_TRUE(send_message_id().empty());
595  EXPECT_EQ(GCMClient::UNKNOWN_ERROR, send_result());
596
597  // Send operation will be invoked after GCMClient becomes ready.
598  GetGCMClient()->PerformDelayedLoading();
599  WaitForAsyncOperation();
600  EXPECT_EQ(message.id, send_message_id());
601  EXPECT_EQ(GCMClient::SUCCESS, send_result());
602}
603
604// Tests a single instance of GCMDriver.
605class GCMDriverFunctionalTest : public GCMDriverTest {
606 public:
607  GCMDriverFunctionalTest();
608  virtual ~GCMDriverFunctionalTest();
609
610  // GCMDriverTest:
611  virtual void SetUp() OVERRIDE;
612
613 private:
614  DISALLOW_COPY_AND_ASSIGN(GCMDriverFunctionalTest);
615};
616
617GCMDriverFunctionalTest::GCMDriverFunctionalTest() {
618}
619
620GCMDriverFunctionalTest::~GCMDriverFunctionalTest() {
621}
622
623void GCMDriverFunctionalTest::SetUp() {
624  GCMDriverTest::SetUp();
625
626  CreateDriver(FakeGCMClient::NO_DELAY_START);
627  AddAppHandlers();
628  SignIn(kTestAccountID1);
629}
630
631TEST_F(GCMDriverFunctionalTest, Register) {
632  std::vector<std::string> sender_ids;
633  sender_ids.push_back("sender1");
634  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
635  const std::string expected_registration_id =
636      FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids);
637
638  EXPECT_EQ(expected_registration_id, registration_id());
639  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
640}
641
642TEST_F(GCMDriverFunctionalTest, RegisterError) {
643  std::vector<std::string> sender_ids;
644  sender_ids.push_back("sender1@error");
645  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
646
647  EXPECT_TRUE(registration_id().empty());
648  EXPECT_NE(GCMClient::SUCCESS, registration_result());
649}
650
651TEST_F(GCMDriverFunctionalTest, RegisterAgainWithSameSenderIDs) {
652  std::vector<std::string> sender_ids;
653  sender_ids.push_back("sender1");
654  sender_ids.push_back("sender2");
655  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
656  const std::string expected_registration_id =
657      FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids);
658
659  EXPECT_EQ(expected_registration_id, registration_id());
660  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
661
662  // Clears the results the would be set by the Register callback in preparation
663  // to call register 2nd time.
664  ClearResults();
665
666  // Calling register 2nd time with the same set of sender IDs but different
667  // ordering will get back the same registration ID.
668  std::vector<std::string> another_sender_ids;
669  another_sender_ids.push_back("sender2");
670  another_sender_ids.push_back("sender1");
671  Register(kTestAppID1, another_sender_ids, GCMDriverTest::WAIT);
672
673  EXPECT_EQ(expected_registration_id, registration_id());
674  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
675}
676
677TEST_F(GCMDriverFunctionalTest, RegisterAgainWithDifferentSenderIDs) {
678  std::vector<std::string> sender_ids;
679  sender_ids.push_back("sender1");
680  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
681  const std::string expected_registration_id =
682      FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids);
683
684  EXPECT_EQ(expected_registration_id, registration_id());
685  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
686
687  // Make sender IDs different.
688  sender_ids.push_back("sender2");
689  const std::string expected_registration_id2 =
690      FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids);
691
692  // Calling register 2nd time with the different sender IDs will get back a new
693  // registration ID.
694  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
695  EXPECT_EQ(expected_registration_id2, registration_id());
696  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
697}
698
699TEST_F(GCMDriverFunctionalTest, RegisterAfterSignOut) {
700  // This will trigger check-out.
701  SignOut();
702
703  std::vector<std::string> sender_ids;
704  sender_ids.push_back("sender1");
705  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
706
707  EXPECT_TRUE(registration_id().empty());
708  EXPECT_EQ(GCMClient::NOT_SIGNED_IN, registration_result());
709}
710
711TEST_F(GCMDriverFunctionalTest, UnregisterExplicitly) {
712  std::vector<std::string> sender_ids;
713  sender_ids.push_back("sender1");
714  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
715
716  EXPECT_FALSE(registration_id().empty());
717  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
718
719  Unregister(kTestAppID1, GCMDriverTest::WAIT);
720
721  EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
722}
723
724TEST_F(GCMDriverFunctionalTest, UnregisterWhenAsyncOperationPending) {
725  std::vector<std::string> sender_ids;
726  sender_ids.push_back("sender1");
727  // First start registration without waiting for it to complete.
728  Register(kTestAppID1,
729                     sender_ids,
730                     GCMDriverTest::DO_NOT_WAIT);
731
732  // Test that unregistration fails with async operation pending when there is a
733  // registration already in progress.
734  Unregister(kTestAppID1, GCMDriverTest::WAIT);
735  EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
736            unregistration_result());
737
738  // Complete the unregistration.
739  WaitForAsyncOperation();
740  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
741
742  // Start unregistration without waiting for it to complete. This time no async
743  // operation is pending.
744  Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT);
745
746  // Test that unregistration fails with async operation pending when there is
747  // an unregistration already in progress.
748  Unregister(kTestAppID1, GCMDriverTest::WAIT);
749  EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
750            unregistration_result());
751  ClearResults();
752
753  // Complete unregistration.
754  WaitForAsyncOperation();
755  EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
756}
757
758TEST_F(GCMDriverFunctionalTest, RegisterWhenAsyncOperationPending) {
759  std::vector<std::string> sender_ids;
760  sender_ids.push_back("sender1");
761  // First start registration without waiting for it to complete.
762  Register(kTestAppID1,
763                     sender_ids,
764                     GCMDriverTest::DO_NOT_WAIT);
765
766  // Test that registration fails with async operation pending when there is a
767  // registration already in progress.
768  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
769  EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
770            registration_result());
771  ClearResults();
772
773  // Complete the registration.
774  WaitForAsyncOperation();
775  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
776
777  // Start unregistration without waiting for it to complete. This time no async
778  // operation is pending.
779  Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT);
780
781  // Test that registration fails with async operation pending when there is an
782  // unregistration already in progress.
783  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
784  EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
785            registration_result());
786
787  // Complete the first unregistration expecting success.
788  WaitForAsyncOperation();
789  EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
790
791  // Test that it is ok to register again after unregistration.
792  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
793  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
794}
795
796TEST_F(GCMDriverFunctionalTest, Send) {
797  GCMClient::OutgoingMessage message;
798  message.id = "1";
799  message.data["key1"] = "value1";
800  message.data["key2"] = "value2";
801  Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
802
803  EXPECT_EQ(message.id, send_message_id());
804  EXPECT_EQ(GCMClient::SUCCESS, send_result());
805}
806
807TEST_F(GCMDriverFunctionalTest, SendAfterSignOut) {
808  // This will trigger check-out.
809  SignOut();
810
811  GCMClient::OutgoingMessage message;
812  message.id = "1";
813  message.data["key1"] = "value1";
814  message.data["key2"] = "value2";
815  Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
816
817  EXPECT_TRUE(send_message_id().empty());
818  EXPECT_EQ(GCMClient::NOT_SIGNED_IN, send_result());
819}
820
821TEST_F(GCMDriverFunctionalTest, SendError) {
822  GCMClient::OutgoingMessage message;
823  // Embedding error in id will tell the mock to simulate the send error.
824  message.id = "1@error";
825  message.data["key1"] = "value1";
826  message.data["key2"] = "value2";
827  Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
828
829  EXPECT_EQ(message.id, send_message_id());
830  EXPECT_EQ(GCMClient::SUCCESS, send_result());
831
832  // Wait for the send error.
833  gcm_app_handler()->WaitForNotification();
834  EXPECT_EQ(FakeGCMAppHandler::SEND_ERROR_EVENT,
835            gcm_app_handler()->received_event());
836  EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
837  EXPECT_EQ(message.id,
838            gcm_app_handler()->send_error_details().message_id);
839  EXPECT_NE(GCMClient::SUCCESS,
840            gcm_app_handler()->send_error_details().result);
841  EXPECT_EQ(message.data,
842            gcm_app_handler()->send_error_details().additional_data);
843}
844
845TEST_F(GCMDriverFunctionalTest, MessageReceived) {
846  Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
847  GCMClient::IncomingMessage message;
848  message.data["key1"] = "value1";
849  message.data["key2"] = "value2";
850  message.sender_id = "sender";
851  GetGCMClient()->ReceiveMessage(kTestAppID1, message);
852  gcm_app_handler()->WaitForNotification();
853  EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
854            gcm_app_handler()->received_event());
855  EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
856  EXPECT_EQ(message.data, gcm_app_handler()->message().data);
857  EXPECT_TRUE(gcm_app_handler()->message().collapse_key.empty());
858  EXPECT_EQ(message.sender_id, gcm_app_handler()->message().sender_id);
859}
860
861TEST_F(GCMDriverFunctionalTest, MessageWithCollapseKeyReceived) {
862  Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
863  GCMClient::IncomingMessage message;
864  message.data["key1"] = "value1";
865  message.collapse_key = "collapse_key_value";
866  message.sender_id = "sender";
867  GetGCMClient()->ReceiveMessage(kTestAppID1, message);
868  gcm_app_handler()->WaitForNotification();
869  EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
870            gcm_app_handler()->received_event());
871  EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
872  EXPECT_EQ(message.data, gcm_app_handler()->message().data);
873  EXPECT_EQ(message.collapse_key,
874            gcm_app_handler()->message().collapse_key);
875}
876
877TEST_F(GCMDriverFunctionalTest, MessagesDeleted) {
878  GetGCMClient()->DeleteMessages(kTestAppID1);
879  gcm_app_handler()->WaitForNotification();
880  EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT,
881            gcm_app_handler()->received_event());
882  EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
883}
884
885}  // namespace gcm
886