gcm_driver_desktop_unittest.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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/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"
26
27namespace gcm {
28
29namespace {
30
31const char kTestAccountID1[] = "user1@example.com";
32const char kTestAccountID2[] = "user2@example.com";
33const char kTestAppID1[] = "TestApp1";
34const char kTestAppID2[] = "TestApp2";
35const char kUserID1[] = "user1";
36
37void PumpCurrentLoop() {
38  base::MessageLoop::ScopedNestableTaskAllower
39      nestable_task_allower(base::MessageLoop::current());
40  base::RunLoop().RunUntilIdle();
41}
42
43void PumpUILoop() {
44  PumpCurrentLoop();
45}
46
47std::vector<std::string> ToSenderList(const std::string& sender_ids) {
48  std::vector<std::string> senders;
49  Tokenize(sender_ids, ",", &senders);
50  return senders;
51}
52
53}  // namespace
54
55class GCMDriverTest : public testing::Test {
56 public:
57  enum WaitToFinish {
58    DO_NOT_WAIT,
59    WAIT
60  };
61
62  GCMDriverTest();
63  virtual ~GCMDriverTest();
64
65  // testing::Test:
66  virtual void SetUp() OVERRIDE;
67  virtual void TearDown() OVERRIDE;
68
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_;
77  }
78
79  void PumpIOLoop();
80
81  void ClearResults();
82
83  bool HasAppHandlers() const;
84  FakeGCMClient* GetGCMClient();
85
86  void CreateDriver(FakeGCMClient::StartMode gcm_client_start_mode);
87  void AddAppHandlers();
88  void RemoveAppHandlers();
89
90  void SignIn(const std::string& account_id);
91  void SignOut();
92
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);
101
102  void WaitForAsyncOperation();
103
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);
109
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_;
117
118  base::Closure async_operation_completed_callback_;
119
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_;
125
126  DISALLOW_COPY_AND_ASSIGN(GCMDriverTest);
127};
128
129GCMDriverTest::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) {
136}
137
138GCMDriverTest::~GCMDriverTest() {
139}
140
141void GCMDriverTest::SetUp() {
142  io_thread_.Start();
143  ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
144}
145
146void GCMDriverTest::TearDown() {
147  if (!driver_)
148    return;
149
150  driver_->Shutdown();
151  driver_.reset();
152  PumpIOLoop();
153
154  io_thread_.Stop();
155}
156
157void 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();
164}
165
166void GCMDriverTest::ClearResults() {
167  registration_id_.clear();
168  registration_result_ = GCMClient::UNKNOWN_ERROR;
169
170  send_message_id_.clear();
171  send_result_ = GCMClient::UNKNOWN_ERROR;
172
173  unregistration_result_ = GCMClient::UNKNOWN_ERROR;
174}
175
176bool GCMDriverTest::HasAppHandlers() const {
177  return !driver_->app_handlers().empty();
178}
179
180FakeGCMClient* GCMDriverTest::GetGCMClient() {
181  return static_cast<FakeGCMClient*>(driver_->GetGCMClientForTesting());
182}
183
184void 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_));
200
201  gcm_app_handler_.reset(new FakeGCMAppHandler);
202}
203
204void GCMDriverTest::AddAppHandlers() {
205  driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get());
206  driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get());
207}
208
209void GCMDriverTest::RemoveAppHandlers() {
210  driver_->RemoveAppHandler(kTestAppID1);
211  driver_->RemoveAppHandler(kTestAppID2);
212}
213
214void GCMDriverTest::SignIn(const std::string& account_id) {
215  driver_->OnSignedIn();
216  PumpIOLoop();
217  PumpUILoop();
218}
219
220void GCMDriverTest::SignOut() {
221  driver_->Purge();
222  PumpIOLoop();
223  PumpUILoop();
224}
225
226void 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();
237}
238
239void 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();
252}
253
254void 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();
263}
264
265void GCMDriverTest::WaitForAsyncOperation() {
266  base::RunLoop run_loop;
267  async_operation_completed_callback_ = run_loop.QuitClosure();
268  run_loop.Run();
269}
270
271void 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();
277}
278
279void 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();
285}
286
287void GCMDriverTest::UnregisterCompleted(GCMClient::Result result) {
288  unregistration_result_ = result;
289  if (!async_operation_completed_callback_.is_null())
290    async_operation_completed_callback_.Run();
291}
292
293TEST_F(GCMDriverTest, Create) {
294  // Create GCMDriver first. GCM is not started.
295  CreateDriver(FakeGCMClient::NO_DELAY_START);
296  EXPECT_FALSE(driver()->IsStarted());
297
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());
303
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());
310}
311
312TEST_F(GCMDriverTest, CreateByFieldTrial) {
313  ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("GCM", "Enabled"));
314
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());
320
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());
327}
328
329TEST_F(GCMDriverTest, Shutdown) {
330  CreateDriver(FakeGCMClient::NO_DELAY_START);
331  EXPECT_FALSE(HasAppHandlers());
332
333  AddAppHandlers();
334  EXPECT_TRUE(HasAppHandlers());
335
336  driver()->Shutdown();
337  EXPECT_FALSE(HasAppHandlers());
338  EXPECT_FALSE(driver()->IsConnected());
339  EXPECT_FALSE(gcm_app_handler()->connected());
340}
341
342TEST_F(GCMDriverTest, SignInAndSignOutOnGCMEnabled) {
343  // By default, GCM is enabled.
344  CreateDriver(FakeGCMClient::NO_DELAY_START);
345  AddAppHandlers();
346
347  // GCMClient should be started after sign-in.
348  SignIn(kTestAccountID1);
349  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
350
351  // GCMClient should be checked out after sign-out.
352  SignOut();
353  EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status());
354}
355
356TEST_F(GCMDriverTest, SignInAndSignOutOnGCMDisabled) {
357  // By default, GCM is enabled.
358  CreateDriver(FakeGCMClient::NO_DELAY_START);
359  AddAppHandlers();
360
361  // Disable GCM.
362  driver()->Disable();
363
364  // GCMClient should not be started after sign-in.
365  SignIn(kTestAccountID1);
366  EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status());
367
368  // Check-out should still be performed after sign-out.
369  SignOut();
370  EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status());
371}
372
373TEST_F(GCMDriverTest, SignOutAndThenSignIn) {
374  CreateDriver(FakeGCMClient::NO_DELAY_START);
375  AddAppHandlers();
376
377  // GCMClient should be started after sign-in.
378  SignIn(kTestAccountID1);
379  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
380
381  // GCMClient should be checked out after sign-out.
382  SignOut();
383  EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status());
384
385  // Sign-in with a different account.
386  SignIn(kTestAccountID2);
387
388  // GCMClient should be started again.
389  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
390}
391
392TEST_F(GCMDriverTest, DisableAndReenableGCM) {
393  CreateDriver(FakeGCMClient::NO_DELAY_START);
394  AddAppHandlers();
395  SignIn(kTestAccountID1);
396
397  // GCMClient should be started.
398  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
399
400  // Disables the GCM.
401  driver()->Disable();
402  PumpIOLoop();
403  PumpUILoop();
404
405  // GCMClient should be stopped.
406  EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status());
407
408  // Enables the GCM.
409  driver()->Enable();
410  PumpIOLoop();
411  PumpUILoop();
412
413  // GCMClient should be started.
414  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
415
416  // Disables the GCM.
417  driver()->Disable();
418  PumpIOLoop();
419  PumpUILoop();
420
421  // GCMClient should be stopped.
422  EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status());
423
424  // Sign out.
425  SignOut();
426
427  // GCMClient should be checked out.
428  EXPECT_EQ(FakeGCMClient::CHECKED_OUT, GetGCMClient()->status());
429}
430
431TEST_F(GCMDriverTest, StartOrStopGCMOnDemand) {
432  CreateDriver(FakeGCMClient::NO_DELAY_START);
433  SignIn(kTestAccountID1);
434
435  // GCMClient is not started.
436  EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status());
437
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());
443
444  // Add another app handler.
445  driver()->AddAppHandler(kTestAppID2, gcm_app_handler());
446  PumpIOLoop();
447  PumpUILoop();
448  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
449
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());
455
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());
461
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());
467}
468
469TEST_F(GCMDriverTest, RegisterFailed) {
470  std::vector<std::string> sender_ids;
471  sender_ids.push_back("sender1");
472
473  CreateDriver(FakeGCMClient::NO_DELAY_START);
474
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());
480
481  ClearResults();
482
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());
489
490  ClearResults();
491
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());
498}
499
500TEST_F(GCMDriverTest, UnregisterFailed) {
501  CreateDriver(FakeGCMClient::NO_DELAY_START);
502
503  // Unregistration fails when GCM is disabled.
504  driver()->Disable();
505  Unregister(kTestAppID1, GCMDriverTest::WAIT);
506  EXPECT_EQ(GCMClient::GCM_DISABLED, unregistration_result());
507
508  ClearResults();
509
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());
515
516  ClearResults();
517
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());
523}
524
525TEST_F(GCMDriverTest, SendFailed) {
526  GCMClient::OutgoingMessage message;
527  message.id = "1";
528  message.data["key1"] = "value1";
529
530  CreateDriver(FakeGCMClient::NO_DELAY_START);
531
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());
537
538  ClearResults();
539
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());
546
547  ClearResults();
548
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());
555}
556
557TEST_F(GCMDriverTest, GCMClientNotReadyBeforeRegistration) {
558  // Make GCMClient not ready initially.
559  CreateDriver(FakeGCMClient::DELAY_START);
560  SignIn(kTestAccountID1);
561  AddAppHandlers();
562
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());
573
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());
579}
580
581TEST_F(GCMDriverTest, GCMClientNotReadyBeforeSending) {
582  // Make GCMClient not ready initially.
583  CreateDriver(FakeGCMClient::DELAY_START);
584  SignIn(kTestAccountID1);
585  AddAppHandlers();
586
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();
595
596  EXPECT_TRUE(send_message_id().empty());
597  EXPECT_EQ(GCMClient::UNKNOWN_ERROR, send_result());
598
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());
604}
605
606// Tests a single instance of GCMDriver.
607class GCMDriverFunctionalTest : public GCMDriverTest {
608 public:
609  GCMDriverFunctionalTest();
610  virtual ~GCMDriverFunctionalTest();
611
612  // GCMDriverTest:
613  virtual void SetUp() OVERRIDE;
614
615 private:
616  DISALLOW_COPY_AND_ASSIGN(GCMDriverFunctionalTest);
617};
618
619GCMDriverFunctionalTest::GCMDriverFunctionalTest() {
620}
621
622GCMDriverFunctionalTest::~GCMDriverFunctionalTest() {
623}
624
625void GCMDriverFunctionalTest::SetUp() {
626  GCMDriverTest::SetUp();
627
628  CreateDriver(FakeGCMClient::NO_DELAY_START);
629  AddAppHandlers();
630  SignIn(kTestAccountID1);
631}
632
633TEST_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);
639
640  EXPECT_EQ(expected_registration_id, registration_id());
641  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
642}
643
644TEST_F(GCMDriverFunctionalTest, RegisterError) {
645  std::vector<std::string> sender_ids;
646  sender_ids.push_back("sender1@error");
647  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
648
649  EXPECT_TRUE(registration_id().empty());
650  EXPECT_NE(GCMClient::SUCCESS, registration_result());
651}
652
653TEST_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);
660
661  EXPECT_EQ(expected_registration_id, registration_id());
662  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
663
664  // Clears the results the would be set by the Register callback in preparation
665  // to call register 2nd time.
666  ClearResults();
667
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);
674
675  EXPECT_EQ(expected_registration_id, registration_id());
676  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
677}
678
679TEST_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);
685
686  EXPECT_EQ(expected_registration_id, registration_id());
687  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
688
689  // Make sender IDs different.
690  sender_ids.push_back("sender2");
691  const std::string expected_registration_id2 =
692      FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids);
693
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());
699}
700
701TEST_F(GCMDriverFunctionalTest, RegisterAfterSignOut) {
702  // This will trigger check-out.
703  SignOut();
704
705  std::vector<std::string> sender_ids;
706  sender_ids.push_back("sender1");
707  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
708
709  EXPECT_TRUE(registration_id().empty());
710  EXPECT_EQ(GCMClient::NOT_SIGNED_IN, registration_result());
711}
712
713TEST_F(GCMDriverFunctionalTest, UnregisterExplicitly) {
714  std::vector<std::string> sender_ids;
715  sender_ids.push_back("sender1");
716  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
717
718  EXPECT_FALSE(registration_id().empty());
719  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
720
721  Unregister(kTestAppID1, GCMDriverTest::WAIT);
722
723  EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
724}
725
726TEST_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);
733
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());
739
740  // Complete the unregistration.
741  WaitForAsyncOperation();
742  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
743
744  // Start unregistration without waiting for it to complete. This time no async
745  // operation is pending.
746  Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT);
747
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();
754
755  // Complete unregistration.
756  WaitForAsyncOperation();
757  EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
758}
759
760TEST_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);
767
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();
774
775  // Complete the registration.
776  WaitForAsyncOperation();
777  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
778
779  // Start unregistration without waiting for it to complete. This time no async
780  // operation is pending.
781  Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT);
782
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());
788
789  // Complete the first unregistration expecting success.
790  WaitForAsyncOperation();
791  EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
792
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());
796}
797
798TEST_F(GCMDriverFunctionalTest, Send) {
799  GCMClient::OutgoingMessage message;
800  message.id = "1";
801  message.data["key1"] = "value1";
802  message.data["key2"] = "value2";
803  Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
804
805  EXPECT_EQ(message.id, send_message_id());
806  EXPECT_EQ(GCMClient::SUCCESS, send_result());
807}
808
809TEST_F(GCMDriverFunctionalTest, SendAfterSignOut) {
810  // This will trigger check-out.
811  SignOut();
812
813  GCMClient::OutgoingMessage message;
814  message.id = "1";
815  message.data["key1"] = "value1";
816  message.data["key2"] = "value2";
817  Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
818
819  EXPECT_TRUE(send_message_id().empty());
820  EXPECT_EQ(GCMClient::NOT_SIGNED_IN, send_result());
821}
822
823TEST_F(GCMDriverFunctionalTest, SendError) {
824  GCMClient::OutgoingMessage message;
825  // Embedding error in id will tell the mock to simulate the send error.
826  message.id = "1@error";
827  message.data["key1"] = "value1";
828  message.data["key2"] = "value2";
829  Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
830
831  EXPECT_EQ(message.id, send_message_id());
832  EXPECT_EQ(GCMClient::SUCCESS, send_result());
833
834  // Wait for the send error.
835  gcm_app_handler()->WaitForNotification();
836  EXPECT_EQ(FakeGCMAppHandler::SEND_ERROR_EVENT,
837            gcm_app_handler()->received_event());
838  EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
839  EXPECT_EQ(message.id,
840            gcm_app_handler()->send_error_details().message_id);
841  EXPECT_NE(GCMClient::SUCCESS,
842            gcm_app_handler()->send_error_details().result);
843  EXPECT_EQ(message.data,
844            gcm_app_handler()->send_error_details().additional_data);
845}
846
847TEST_F(GCMDriverFunctionalTest, MessageReceived) {
848  Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
849  GCMClient::IncomingMessage message;
850  message.data["key1"] = "value1";
851  message.data["key2"] = "value2";
852  message.sender_id = "sender";
853  GetGCMClient()->ReceiveMessage(kTestAppID1, message);
854  gcm_app_handler()->WaitForNotification();
855  EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
856            gcm_app_handler()->received_event());
857  EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
858  EXPECT_EQ(message.data, gcm_app_handler()->message().data);
859  EXPECT_TRUE(gcm_app_handler()->message().collapse_key.empty());
860  EXPECT_EQ(message.sender_id, gcm_app_handler()->message().sender_id);
861}
862
863TEST_F(GCMDriverFunctionalTest, MessageWithCollapseKeyReceived) {
864  Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
865  GCMClient::IncomingMessage message;
866  message.data["key1"] = "value1";
867  message.collapse_key = "collapse_key_value";
868  message.sender_id = "sender";
869  GetGCMClient()->ReceiveMessage(kTestAppID1, message);
870  gcm_app_handler()->WaitForNotification();
871  EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
872            gcm_app_handler()->received_event());
873  EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
874  EXPECT_EQ(message.data, gcm_app_handler()->message().data);
875  EXPECT_EQ(message.collapse_key,
876            gcm_app_handler()->message().collapse_key);
877}
878
879TEST_F(GCMDriverFunctionalTest, MessagesDeleted) {
880  GetGCMClient()->DeleteMessages(kTestAppID1);
881  gcm_app_handler()->WaitForNotification();
882  EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT,
883            gcm_app_handler()->received_event());
884  EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
885}
886
887}  // namespace gcm
888