gcm_driver_desktop_unittest.cc revision 1675a649fd7a8b3cb80ffddae2dc181f122353c5
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/prefs/pref_registry_simple.h"
15#include "base/prefs/testing_pref_service.h"
16#include "base/run_loop.h"
17#include "base/strings/string_util.h"
18#include "base/test/test_simple_task_runner.h"
19#include "base/threading/thread.h"
20#include "components/gcm_driver/fake_gcm_app_handler.h"
21#include "components/gcm_driver/fake_gcm_client.h"
22#include "components/gcm_driver/fake_gcm_client_factory.h"
23#include "components/gcm_driver/gcm_app_handler.h"
24#include "components/gcm_driver/gcm_channel_status_request.h"
25#include "components/gcm_driver/gcm_channel_status_syncer.h"
26#include "components/gcm_driver/gcm_client_factory.h"
27#include "components/gcm_driver/gcm_connection_observer.h"
28#include "net/url_request/test_url_fetcher_factory.h"
29#include "net/url_request/url_fetcher_delegate.h"
30#include "net/url_request/url_request_context_getter.h"
31#include "net/url_request/url_request_test_util.h"
32#include "sync/protocol/experiment_status.pb.h"
33#include "sync/protocol/experiments_specifics.pb.h"
34#include "testing/gtest/include/gtest/gtest.h"
35
36namespace gcm {
37
38namespace {
39
40const char kTestAccountID1[] = "user1@example.com";
41const char kTestAccountID2[] = "user2@example.com";
42const char kTestAppID1[] = "TestApp1";
43const char kTestAppID2[] = "TestApp2";
44const char kUserID1[] = "user1";
45
46class FakeGCMConnectionObserver : public GCMConnectionObserver {
47 public:
48  FakeGCMConnectionObserver();
49  virtual ~FakeGCMConnectionObserver();
50
51  // gcm::GCMConnectionObserver implementation:
52  virtual void OnConnected(const net::IPEndPoint& ip_endpoint) OVERRIDE;
53  virtual void OnDisconnected() OVERRIDE;
54
55  bool connected() const { return connected_; }
56
57 private:
58  bool connected_;
59};
60
61FakeGCMConnectionObserver::FakeGCMConnectionObserver() : connected_(false) {
62}
63
64FakeGCMConnectionObserver::~FakeGCMConnectionObserver() {
65}
66
67void FakeGCMConnectionObserver::OnConnected(
68    const net::IPEndPoint& ip_endpoint) {
69  connected_ = true;
70}
71
72void FakeGCMConnectionObserver::OnDisconnected() {
73  connected_ = false;
74}
75
76void PumpCurrentLoop() {
77  base::MessageLoop::ScopedNestableTaskAllower
78      nestable_task_allower(base::MessageLoop::current());
79  base::RunLoop().RunUntilIdle();
80}
81
82void PumpUILoop() {
83  PumpCurrentLoop();
84}
85
86std::vector<std::string> ToSenderList(const std::string& sender_ids) {
87  std::vector<std::string> senders;
88  Tokenize(sender_ids, ",", &senders);
89  return senders;
90}
91
92}  // namespace
93
94class GCMDriverTest : public testing::Test {
95 public:
96  enum WaitToFinish {
97    DO_NOT_WAIT,
98    WAIT
99  };
100
101  GCMDriverTest();
102  virtual ~GCMDriverTest();
103
104  // testing::Test:
105  virtual void SetUp() OVERRIDE;
106  virtual void TearDown() OVERRIDE;
107
108  GCMDriverDesktop* driver() { return driver_.get(); }
109  FakeGCMAppHandler* gcm_app_handler() { return gcm_app_handler_.get(); }
110  FakeGCMConnectionObserver* gcm_connection_observer() {
111    return gcm_connection_observer_.get();
112  }
113  const std::string& registration_id() const { return registration_id_; }
114  GCMClient::Result registration_result() const { return registration_result_; }
115  const std::string& send_message_id() const { return send_message_id_; }
116  GCMClient::Result send_result() const { return send_result_; }
117  GCMClient::Result unregistration_result() const {
118    return unregistration_result_;
119  }
120
121  void PumpIOLoop();
122
123  void ClearResults();
124
125  bool HasAppHandlers() const;
126  FakeGCMClient* GetGCMClient();
127
128  void CreateDriver(FakeGCMClient::StartMode gcm_client_start_mode);
129  void ShutdownDriver();
130  void AddAppHandlers();
131  void RemoveAppHandlers();
132
133  void SignIn(const std::string& account_id);
134  void SignOut();
135
136  void Register(const std::string& app_id,
137                const std::vector<std::string>& sender_ids,
138                WaitToFinish wait_to_finish);
139  void Send(const std::string& app_id,
140            const std::string& receiver_id,
141            const GCMClient::OutgoingMessage& message,
142            WaitToFinish wait_to_finish);
143  void Unregister(const std::string& app_id, WaitToFinish wait_to_finish);
144
145  void WaitForAsyncOperation();
146
147 private:
148  void RegisterCompleted(const std::string& registration_id,
149                         GCMClient::Result result);
150  void SendCompleted(const std::string& message_id, GCMClient::Result result);
151  void UnregisterCompleted(GCMClient::Result result);
152
153  base::ScopedTempDir temp_dir_;
154  TestingPrefServiceSimple prefs_;
155  scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
156  base::MessageLoopForUI message_loop_;
157  base::Thread io_thread_;
158  base::FieldTrialList field_trial_list_;
159  scoped_ptr<GCMDriverDesktop> driver_;
160  scoped_ptr<FakeGCMAppHandler> gcm_app_handler_;
161  scoped_ptr<FakeGCMConnectionObserver> gcm_connection_observer_;
162
163  base::Closure async_operation_completed_callback_;
164
165  std::string registration_id_;
166  GCMClient::Result registration_result_;
167  std::string send_message_id_;
168  GCMClient::Result send_result_;
169  GCMClient::Result unregistration_result_;
170
171  DISALLOW_COPY_AND_ASSIGN(GCMDriverTest);
172};
173
174GCMDriverTest::GCMDriverTest()
175    : task_runner_(new base::TestSimpleTaskRunner()),
176      io_thread_("IOThread"),
177      field_trial_list_(NULL),
178      registration_result_(GCMClient::UNKNOWN_ERROR),
179      send_result_(GCMClient::UNKNOWN_ERROR),
180      unregistration_result_(GCMClient::UNKNOWN_ERROR) {
181}
182
183GCMDriverTest::~GCMDriverTest() {
184}
185
186void GCMDriverTest::SetUp() {
187  GCMChannelStatusSyncer::RegisterPrefs(prefs_.registry());
188  io_thread_.Start();
189  ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
190}
191
192void GCMDriverTest::TearDown() {
193  if (!driver_)
194    return;
195
196  ShutdownDriver();
197  driver_.reset();
198  PumpIOLoop();
199
200  io_thread_.Stop();
201}
202
203void GCMDriverTest::PumpIOLoop() {
204  base::RunLoop run_loop;
205  io_thread_.message_loop_proxy()->PostTaskAndReply(
206      FROM_HERE,
207      base::Bind(&PumpCurrentLoop),
208      run_loop.QuitClosure());
209  run_loop.Run();
210}
211
212void GCMDriverTest::ClearResults() {
213  registration_id_.clear();
214  registration_result_ = GCMClient::UNKNOWN_ERROR;
215
216  send_message_id_.clear();
217  send_result_ = GCMClient::UNKNOWN_ERROR;
218
219  unregistration_result_ = GCMClient::UNKNOWN_ERROR;
220}
221
222bool GCMDriverTest::HasAppHandlers() const {
223  return !driver_->app_handlers().empty();
224}
225
226FakeGCMClient* GCMDriverTest::GetGCMClient() {
227  return static_cast<FakeGCMClient*>(driver_->GetGCMClientForTesting());
228}
229
230void GCMDriverTest::CreateDriver(
231    FakeGCMClient::StartMode gcm_client_start_mode) {
232  scoped_refptr<net::URLRequestContextGetter> request_context =
233      new net::TestURLRequestContextGetter(io_thread_.message_loop_proxy());
234  // TODO(johnme): Need equivalent test coverage of GCMDriverAndroid.
235  driver_.reset(new GCMDriverDesktop(
236      scoped_ptr<GCMClientFactory>(
237          new FakeGCMClientFactory(gcm_client_start_mode,
238                                   base::MessageLoopProxy::current(),
239                                   io_thread_.message_loop_proxy())).Pass(),
240      GCMClient::ChromeBuildInfo(),
241      "http://channel.status.request.url",
242      "user-agent-string",
243      &prefs_,
244      temp_dir_.path(),
245      request_context,
246      base::MessageLoopProxy::current(),
247      io_thread_.message_loop_proxy(),
248      task_runner_));
249
250  gcm_app_handler_.reset(new FakeGCMAppHandler);
251  gcm_connection_observer_.reset(new FakeGCMConnectionObserver);
252
253  driver_->AddConnectionObserver(gcm_connection_observer_.get());
254}
255
256void GCMDriverTest::ShutdownDriver() {
257  if (gcm_connection_observer())
258    driver()->RemoveConnectionObserver(gcm_connection_observer());
259  driver()->Shutdown();
260}
261
262void GCMDriverTest::AddAppHandlers() {
263  driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get());
264  driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get());
265}
266
267void GCMDriverTest::RemoveAppHandlers() {
268  driver_->RemoveAppHandler(kTestAppID1);
269  driver_->RemoveAppHandler(kTestAppID2);
270}
271
272void GCMDriverTest::SignIn(const std::string& account_id) {
273  driver_->OnSignedIn();
274  PumpIOLoop();
275  PumpUILoop();
276}
277
278void GCMDriverTest::SignOut() {
279  driver_->OnSignedOut();
280  PumpIOLoop();
281  PumpUILoop();
282}
283
284void GCMDriverTest::Register(const std::string& app_id,
285                             const std::vector<std::string>& sender_ids,
286                             WaitToFinish wait_to_finish) {
287  base::RunLoop run_loop;
288  async_operation_completed_callback_ = run_loop.QuitClosure();
289  driver_->Register(app_id,
290                    sender_ids,
291                    base::Bind(&GCMDriverTest::RegisterCompleted,
292                               base::Unretained(this)));
293  if (wait_to_finish == WAIT)
294    run_loop.Run();
295}
296
297void GCMDriverTest::Send(const std::string& app_id,
298                         const std::string& receiver_id,
299                         const GCMClient::OutgoingMessage& message,
300                         WaitToFinish wait_to_finish) {
301  base::RunLoop run_loop;
302  async_operation_completed_callback_ = run_loop.QuitClosure();
303  driver_->Send(app_id,
304                receiver_id,
305                message,
306                base::Bind(&GCMDriverTest::SendCompleted,
307                           base::Unretained(this)));
308  if (wait_to_finish == WAIT)
309    run_loop.Run();
310}
311
312void GCMDriverTest::Unregister(const std::string& app_id,
313                               WaitToFinish wait_to_finish) {
314  base::RunLoop run_loop;
315  async_operation_completed_callback_ = run_loop.QuitClosure();
316  driver_->Unregister(app_id,
317                       base::Bind(&GCMDriverTest::UnregisterCompleted,
318                                  base::Unretained(this)));
319  if (wait_to_finish == WAIT)
320    run_loop.Run();
321}
322
323void GCMDriverTest::WaitForAsyncOperation() {
324  base::RunLoop run_loop;
325  async_operation_completed_callback_ = run_loop.QuitClosure();
326  run_loop.Run();
327}
328
329void GCMDriverTest::RegisterCompleted(const std::string& registration_id,
330                                      GCMClient::Result result) {
331  registration_id_ = registration_id;
332  registration_result_ = result;
333  if (!async_operation_completed_callback_.is_null())
334    async_operation_completed_callback_.Run();
335}
336
337void GCMDriverTest::SendCompleted(const std::string& message_id,
338                                  GCMClient::Result result) {
339  send_message_id_ = message_id;
340  send_result_ = result;
341  if (!async_operation_completed_callback_.is_null())
342    async_operation_completed_callback_.Run();
343}
344
345void GCMDriverTest::UnregisterCompleted(GCMClient::Result result) {
346  unregistration_result_ = result;
347  if (!async_operation_completed_callback_.is_null())
348    async_operation_completed_callback_.Run();
349}
350
351TEST_F(GCMDriverTest, Create) {
352  // Create GCMDriver first. GCM is not started.
353  CreateDriver(FakeGCMClient::NO_DELAY_START);
354  EXPECT_FALSE(driver()->IsStarted());
355
356  // Sign in. GCM is still not started.
357  SignIn(kTestAccountID1);
358  EXPECT_FALSE(driver()->IsStarted());
359  EXPECT_FALSE(driver()->IsConnected());
360  EXPECT_FALSE(gcm_connection_observer()->connected());
361
362  // GCM will be started only after both sign-in and app handler being added.
363  AddAppHandlers();
364  EXPECT_TRUE(driver()->IsStarted());
365  PumpIOLoop();
366  EXPECT_TRUE(driver()->IsConnected());
367  EXPECT_TRUE(gcm_connection_observer()->connected());
368}
369
370TEST_F(GCMDriverTest, CreateByFieldTrial) {
371  ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("GCM", "Enabled"));
372
373  // Create GCMDriver first. GCM is not started.
374  CreateDriver(FakeGCMClient::NO_DELAY_START);
375  EXPECT_FALSE(driver()->IsStarted());
376  EXPECT_FALSE(driver()->IsConnected());
377  EXPECT_FALSE(gcm_connection_observer()->connected());
378
379  // GCM will be started after app handler is added.
380  AddAppHandlers();
381  EXPECT_TRUE(driver()->IsStarted());
382  PumpIOLoop();
383  EXPECT_TRUE(driver()->IsConnected());
384  EXPECT_TRUE(gcm_connection_observer()->connected());
385
386  // Sign-in will not affect GCM state.
387  SignIn(kTestAccountID1);
388  PumpIOLoop();
389  EXPECT_TRUE(driver()->IsStarted());
390  EXPECT_TRUE(driver()->IsConnected());
391
392  // Sign-out will not affect GCM state.
393  SignOut();
394  PumpIOLoop();
395  EXPECT_TRUE(driver()->IsStarted());
396  EXPECT_TRUE(driver()->IsConnected());
397}
398
399TEST_F(GCMDriverTest, Shutdown) {
400  CreateDriver(FakeGCMClient::NO_DELAY_START);
401  EXPECT_FALSE(HasAppHandlers());
402
403  AddAppHandlers();
404  EXPECT_TRUE(HasAppHandlers());
405
406  ShutdownDriver();
407  EXPECT_FALSE(HasAppHandlers());
408  EXPECT_FALSE(driver()->IsConnected());
409  EXPECT_FALSE(gcm_connection_observer()->connected());
410}
411
412TEST_F(GCMDriverTest, SignInAndSignOutOnGCMEnabled) {
413  // By default, GCM is enabled.
414  CreateDriver(FakeGCMClient::NO_DELAY_START);
415  AddAppHandlers();
416
417  // GCMClient should be started after sign-in.
418  SignIn(kTestAccountID1);
419  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
420
421  // GCMClient should be stopped out after sign-out.
422  // Note: Before we enable the feature that drops the sign-in enforcement and
423  // make GCM work for all users, GCM is only applicable to signed-in users.
424  // Once the users sign out, the GCM will be shut down while the GCM store
425  // remains intact.
426  SignOut();
427  EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status());
428}
429
430TEST_F(GCMDriverTest, SignInAndSignOutOnGCMDisabled) {
431  // By default, GCM is enabled.
432  CreateDriver(FakeGCMClient::NO_DELAY_START);
433  AddAppHandlers();
434
435  // Disable GCM.
436  driver()->Disable();
437
438  // GCMClient should not be started after sign-in.
439  SignIn(kTestAccountID1);
440  EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status());
441
442  // GCMClient should remain not started after sign-out.
443  SignOut();
444  EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status());
445}
446
447TEST_F(GCMDriverTest, SignOutAndThenSignIn) {
448  CreateDriver(FakeGCMClient::NO_DELAY_START);
449  AddAppHandlers();
450
451  // GCMClient should be started after sign-in.
452  SignIn(kTestAccountID1);
453  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
454
455  // GCMClient should be stopped after sign-out.
456  SignOut();
457  EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status());
458
459  // Sign-in with a different account.
460  SignIn(kTestAccountID2);
461
462  // GCMClient should be started again.
463  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
464}
465
466TEST_F(GCMDriverTest, DisableAndReenableGCM) {
467  CreateDriver(FakeGCMClient::NO_DELAY_START);
468  AddAppHandlers();
469  SignIn(kTestAccountID1);
470
471  // GCMClient should be started.
472  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
473
474  // Disables the GCM.
475  driver()->Disable();
476  PumpIOLoop();
477  PumpUILoop();
478
479  // GCMClient should be stopped.
480  EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status());
481
482  // Enables the GCM.
483  driver()->Enable();
484  PumpIOLoop();
485  PumpUILoop();
486
487  // GCMClient should be started.
488  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
489
490  // Disables the GCM.
491  driver()->Disable();
492  PumpIOLoop();
493  PumpUILoop();
494
495  // GCMClient should be stopped.
496  EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status());
497
498  // Sign out.
499  SignOut();
500
501  // GCMClient should be stopped.
502  EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status());
503}
504
505TEST_F(GCMDriverTest, StartOrStopGCMOnDemand) {
506  CreateDriver(FakeGCMClient::NO_DELAY_START);
507  SignIn(kTestAccountID1);
508
509  // GCMClient is not started.
510  EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status());
511
512  // GCMClient is started after an app handler has been added.
513  driver()->AddAppHandler(kTestAppID1, gcm_app_handler());
514  PumpIOLoop();
515  PumpUILoop();
516  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
517
518  // Add another app handler.
519  driver()->AddAppHandler(kTestAppID2, gcm_app_handler());
520  PumpIOLoop();
521  PumpUILoop();
522  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
523
524  // GCMClient remains active after one app handler is gone.
525  driver()->RemoveAppHandler(kTestAppID1);
526  PumpIOLoop();
527  PumpUILoop();
528  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
529
530  // GCMClient should be stopped after the last app handler is gone.
531  driver()->RemoveAppHandler(kTestAppID2);
532  PumpIOLoop();
533  PumpUILoop();
534  EXPECT_EQ(FakeGCMClient::STOPPED, GetGCMClient()->status());
535
536  // GCMClient is restarted after an app handler has been added.
537  driver()->AddAppHandler(kTestAppID2, gcm_app_handler());
538  PumpIOLoop();
539  PumpUILoop();
540  EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
541}
542
543TEST_F(GCMDriverTest, RegisterFailed) {
544  std::vector<std::string> sender_ids;
545  sender_ids.push_back("sender1");
546
547  CreateDriver(FakeGCMClient::NO_DELAY_START);
548
549  // Registration fails when GCM is disabled.
550  driver()->Disable();
551  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
552  EXPECT_TRUE(registration_id().empty());
553  EXPECT_EQ(GCMClient::GCM_DISABLED, registration_result());
554
555  ClearResults();
556
557  // Registration fails when the sign-in does not occur.
558  driver()->Enable();
559  AddAppHandlers();
560  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
561  EXPECT_TRUE(registration_id().empty());
562  EXPECT_EQ(GCMClient::NOT_SIGNED_IN, registration_result());
563
564  ClearResults();
565
566  // Registration fails when the no app handler is added.
567  RemoveAppHandlers();
568  SignIn(kTestAccountID1);
569  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
570  EXPECT_TRUE(registration_id().empty());
571  EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result());
572}
573
574TEST_F(GCMDriverTest, UnregisterFailed) {
575  CreateDriver(FakeGCMClient::NO_DELAY_START);
576
577  // Unregistration fails when GCM is disabled.
578  driver()->Disable();
579  Unregister(kTestAppID1, GCMDriverTest::WAIT);
580  EXPECT_EQ(GCMClient::GCM_DISABLED, unregistration_result());
581
582  ClearResults();
583
584  // Unregistration fails when the sign-in does not occur.
585  driver()->Enable();
586  AddAppHandlers();
587  Unregister(kTestAppID1, GCMDriverTest::WAIT);
588  EXPECT_EQ(GCMClient::NOT_SIGNED_IN, unregistration_result());
589
590  ClearResults();
591
592  // Unregistration fails when the no app handler is added.
593  RemoveAppHandlers();
594  SignIn(kTestAccountID1);
595  Unregister(kTestAppID1, GCMDriverTest::WAIT);
596  EXPECT_EQ(GCMClient::UNKNOWN_ERROR, unregistration_result());
597}
598
599TEST_F(GCMDriverTest, SendFailed) {
600  GCMClient::OutgoingMessage message;
601  message.id = "1";
602  message.data["key1"] = "value1";
603
604  CreateDriver(FakeGCMClient::NO_DELAY_START);
605
606  // Sending fails when GCM is disabled.
607  driver()->Disable();
608  Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
609  EXPECT_TRUE(send_message_id().empty());
610  EXPECT_EQ(GCMClient::GCM_DISABLED, send_result());
611
612  ClearResults();
613
614  // Sending fails when the sign-in does not occur.
615  driver()->Enable();
616  AddAppHandlers();
617  Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
618  EXPECT_TRUE(send_message_id().empty());
619  EXPECT_EQ(GCMClient::NOT_SIGNED_IN, send_result());
620
621  ClearResults();
622
623  // Sending fails when the no app handler is added.
624  RemoveAppHandlers();
625  SignIn(kTestAccountID1);
626  Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
627  EXPECT_TRUE(send_message_id().empty());
628  EXPECT_EQ(GCMClient::UNKNOWN_ERROR, send_result());
629}
630
631TEST_F(GCMDriverTest, GCMClientNotReadyBeforeRegistration) {
632  // Make GCMClient not ready initially.
633  CreateDriver(FakeGCMClient::DELAY_START);
634  SignIn(kTestAccountID1);
635  AddAppHandlers();
636
637  // The registration is on hold until GCMClient is ready.
638  std::vector<std::string> sender_ids;
639  sender_ids.push_back("sender1");
640  Register(kTestAppID1,
641                     sender_ids,
642                     GCMDriverTest::DO_NOT_WAIT);
643  PumpIOLoop();
644  PumpUILoop();
645  EXPECT_TRUE(registration_id().empty());
646  EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result());
647
648  // Register operation will be invoked after GCMClient becomes ready.
649  GetGCMClient()->PerformDelayedLoading();
650  WaitForAsyncOperation();
651  EXPECT_FALSE(registration_id().empty());
652  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
653}
654
655TEST_F(GCMDriverTest, GCMClientNotReadyBeforeSending) {
656  // Make GCMClient not ready initially.
657  CreateDriver(FakeGCMClient::DELAY_START);
658  SignIn(kTestAccountID1);
659  AddAppHandlers();
660
661  // The sending is on hold until GCMClient is ready.
662  GCMClient::OutgoingMessage message;
663  message.id = "1";
664  message.data["key1"] = "value1";
665  message.data["key2"] = "value2";
666  Send(kTestAppID1, kUserID1, message, GCMDriverTest::DO_NOT_WAIT);
667  PumpIOLoop();
668  PumpUILoop();
669
670  EXPECT_TRUE(send_message_id().empty());
671  EXPECT_EQ(GCMClient::UNKNOWN_ERROR, send_result());
672
673  // Send operation will be invoked after GCMClient becomes ready.
674  GetGCMClient()->PerformDelayedLoading();
675  WaitForAsyncOperation();
676  EXPECT_EQ(message.id, send_message_id());
677  EXPECT_EQ(GCMClient::SUCCESS, send_result());
678}
679
680// Tests a single instance of GCMDriver.
681class GCMDriverFunctionalTest : public GCMDriverTest {
682 public:
683  GCMDriverFunctionalTest();
684  virtual ~GCMDriverFunctionalTest();
685
686  // GCMDriverTest:
687  virtual void SetUp() OVERRIDE;
688
689 private:
690  DISALLOW_COPY_AND_ASSIGN(GCMDriverFunctionalTest);
691};
692
693GCMDriverFunctionalTest::GCMDriverFunctionalTest() {
694}
695
696GCMDriverFunctionalTest::~GCMDriverFunctionalTest() {
697}
698
699void GCMDriverFunctionalTest::SetUp() {
700  GCMDriverTest::SetUp();
701
702  CreateDriver(FakeGCMClient::NO_DELAY_START);
703  AddAppHandlers();
704  SignIn(kTestAccountID1);
705}
706
707TEST_F(GCMDriverFunctionalTest, Register) {
708  std::vector<std::string> sender_ids;
709  sender_ids.push_back("sender1");
710  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
711  const std::string expected_registration_id =
712      GetGCMClient()->GetRegistrationIdFromSenderIds(sender_ids);
713
714  EXPECT_EQ(expected_registration_id, registration_id());
715  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
716}
717
718TEST_F(GCMDriverFunctionalTest, RegisterError) {
719  std::vector<std::string> sender_ids;
720  sender_ids.push_back("sender1@error");
721  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
722
723  EXPECT_TRUE(registration_id().empty());
724  EXPECT_NE(GCMClient::SUCCESS, registration_result());
725}
726
727TEST_F(GCMDriverFunctionalTest, RegisterAgainWithSameSenderIDs) {
728  std::vector<std::string> sender_ids;
729  sender_ids.push_back("sender1");
730  sender_ids.push_back("sender2");
731  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
732  const std::string expected_registration_id =
733      GetGCMClient()->GetRegistrationIdFromSenderIds(sender_ids);
734
735  EXPECT_EQ(expected_registration_id, registration_id());
736  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
737
738  // Clears the results the would be set by the Register callback in preparation
739  // to call register 2nd time.
740  ClearResults();
741
742  // Calling register 2nd time with the same set of sender IDs but different
743  // ordering will get back the same registration ID.
744  std::vector<std::string> another_sender_ids;
745  another_sender_ids.push_back("sender2");
746  another_sender_ids.push_back("sender1");
747  Register(kTestAppID1, another_sender_ids, GCMDriverTest::WAIT);
748
749  EXPECT_EQ(expected_registration_id, registration_id());
750  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
751}
752
753TEST_F(GCMDriverFunctionalTest, RegisterAgainWithDifferentSenderIDs) {
754  std::vector<std::string> sender_ids;
755  sender_ids.push_back("sender1");
756  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
757  const std::string expected_registration_id =
758      GetGCMClient()->GetRegistrationIdFromSenderIds(sender_ids);
759
760  EXPECT_EQ(expected_registration_id, registration_id());
761  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
762
763  // Make sender IDs different.
764  sender_ids.push_back("sender2");
765  const std::string expected_registration_id2 =
766      GetGCMClient()->GetRegistrationIdFromSenderIds(sender_ids);
767
768  // Calling register 2nd time with the different sender IDs will get back a new
769  // registration ID.
770  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
771  EXPECT_EQ(expected_registration_id2, registration_id());
772  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
773}
774
775TEST_F(GCMDriverFunctionalTest, RegisterAfterSignOut) {
776  SignOut();
777
778  std::vector<std::string> sender_ids;
779  sender_ids.push_back("sender1");
780  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
781
782  EXPECT_TRUE(registration_id().empty());
783  EXPECT_EQ(GCMClient::NOT_SIGNED_IN, registration_result());
784}
785
786TEST_F(GCMDriverFunctionalTest, RegisterAfterSignOutAndSignInAgain) {
787  std::vector<std::string> sender_ids;
788  sender_ids.push_back("sender1");
789  const std::string expected_registration_id =
790      GetGCMClient()->GetRegistrationIdFromSenderIds(sender_ids);
791
792  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
793  EXPECT_EQ(expected_registration_id, registration_id());
794  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
795
796  // After signing out, the GCM is stopped and calling register should fail.
797  SignOut();
798  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
799  EXPECT_TRUE(registration_id().empty());
800  EXPECT_EQ(GCMClient::NOT_SIGNED_IN, registration_result());
801
802  // After signing in again, same registration ID should be returned because
803  // the GCM data is not affected.
804  SignIn(kTestAccountID1);
805
806  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
807  EXPECT_EQ(expected_registration_id, registration_id());
808  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
809}
810
811TEST_F(GCMDriverFunctionalTest, UnregisterExplicitly) {
812  std::vector<std::string> sender_ids;
813  sender_ids.push_back("sender1");
814  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
815
816  EXPECT_FALSE(registration_id().empty());
817  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
818
819  Unregister(kTestAppID1, GCMDriverTest::WAIT);
820
821  EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
822}
823
824TEST_F(GCMDriverFunctionalTest, UnregisterWhenAsyncOperationPending) {
825  std::vector<std::string> sender_ids;
826  sender_ids.push_back("sender1");
827  // First start registration without waiting for it to complete.
828  Register(kTestAppID1,
829                     sender_ids,
830                     GCMDriverTest::DO_NOT_WAIT);
831
832  // Test that unregistration fails with async operation pending when there is a
833  // registration already in progress.
834  Unregister(kTestAppID1, GCMDriverTest::WAIT);
835  EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
836            unregistration_result());
837
838  // Complete the unregistration.
839  WaitForAsyncOperation();
840  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
841
842  // Start unregistration without waiting for it to complete. This time no async
843  // operation is pending.
844  Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT);
845
846  // Test that unregistration fails with async operation pending when there is
847  // an unregistration already in progress.
848  Unregister(kTestAppID1, GCMDriverTest::WAIT);
849  EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
850            unregistration_result());
851  ClearResults();
852
853  // Complete unregistration.
854  WaitForAsyncOperation();
855  EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
856}
857
858TEST_F(GCMDriverFunctionalTest, RegisterWhenAsyncOperationPending) {
859  std::vector<std::string> sender_ids;
860  sender_ids.push_back("sender1");
861  // First start registration without waiting for it to complete.
862  Register(kTestAppID1,
863                     sender_ids,
864                     GCMDriverTest::DO_NOT_WAIT);
865
866  // Test that registration fails with async operation pending when there is a
867  // registration already in progress.
868  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
869  EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
870            registration_result());
871  ClearResults();
872
873  // Complete the registration.
874  WaitForAsyncOperation();
875  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
876
877  // Start unregistration without waiting for it to complete. This time no async
878  // operation is pending.
879  Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT);
880
881  // Test that registration fails with async operation pending when there is an
882  // unregistration already in progress.
883  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
884  EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
885            registration_result());
886
887  // Complete the first unregistration expecting success.
888  WaitForAsyncOperation();
889  EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
890
891  // Test that it is ok to register again after unregistration.
892  Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
893  EXPECT_EQ(GCMClient::SUCCESS, registration_result());
894}
895
896TEST_F(GCMDriverFunctionalTest, Send) {
897  GCMClient::OutgoingMessage message;
898  message.id = "1@ack";
899  message.data["key1"] = "value1";
900  message.data["key2"] = "value2";
901  Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
902
903  EXPECT_EQ(message.id, send_message_id());
904  EXPECT_EQ(GCMClient::SUCCESS, send_result());
905
906  gcm_app_handler()->WaitForNotification();
907  EXPECT_EQ(message.id, gcm_app_handler()->acked_message_id());
908  EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
909}
910
911TEST_F(GCMDriverFunctionalTest, SendAfterSignOut) {
912  SignOut();
913
914  GCMClient::OutgoingMessage message;
915  message.id = "1";
916  message.data["key1"] = "value1";
917  message.data["key2"] = "value2";
918  Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
919
920  EXPECT_TRUE(send_message_id().empty());
921  EXPECT_EQ(GCMClient::NOT_SIGNED_IN, send_result());
922}
923
924TEST_F(GCMDriverFunctionalTest, SendError) {
925  GCMClient::OutgoingMessage message;
926  // Embedding error in id will tell the mock to simulate the send error.
927  message.id = "1@error";
928  message.data["key1"] = "value1";
929  message.data["key2"] = "value2";
930  Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
931
932  EXPECT_EQ(message.id, send_message_id());
933  EXPECT_EQ(GCMClient::SUCCESS, send_result());
934
935  // Wait for the send error.
936  gcm_app_handler()->WaitForNotification();
937  EXPECT_EQ(FakeGCMAppHandler::SEND_ERROR_EVENT,
938            gcm_app_handler()->received_event());
939  EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
940  EXPECT_EQ(message.id,
941            gcm_app_handler()->send_error_details().message_id);
942  EXPECT_NE(GCMClient::SUCCESS,
943            gcm_app_handler()->send_error_details().result);
944  EXPECT_EQ(message.data,
945            gcm_app_handler()->send_error_details().additional_data);
946}
947
948TEST_F(GCMDriverFunctionalTest, MessageReceived) {
949  Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
950  GCMClient::IncomingMessage message;
951  message.data["key1"] = "value1";
952  message.data["key2"] = "value2";
953  message.sender_id = "sender";
954  GetGCMClient()->ReceiveMessage(kTestAppID1, message);
955  gcm_app_handler()->WaitForNotification();
956  EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
957            gcm_app_handler()->received_event());
958  EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
959  EXPECT_EQ(message.data, gcm_app_handler()->message().data);
960  EXPECT_TRUE(gcm_app_handler()->message().collapse_key.empty());
961  EXPECT_EQ(message.sender_id, gcm_app_handler()->message().sender_id);
962}
963
964TEST_F(GCMDriverFunctionalTest, MessageWithCollapseKeyReceived) {
965  Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
966  GCMClient::IncomingMessage message;
967  message.data["key1"] = "value1";
968  message.collapse_key = "collapse_key_value";
969  message.sender_id = "sender";
970  GetGCMClient()->ReceiveMessage(kTestAppID1, message);
971  gcm_app_handler()->WaitForNotification();
972  EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
973            gcm_app_handler()->received_event());
974  EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
975  EXPECT_EQ(message.data, gcm_app_handler()->message().data);
976  EXPECT_EQ(message.collapse_key,
977            gcm_app_handler()->message().collapse_key);
978}
979
980TEST_F(GCMDriverFunctionalTest, MessagesDeleted) {
981  GetGCMClient()->DeleteMessages(kTestAppID1);
982  gcm_app_handler()->WaitForNotification();
983  EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT,
984            gcm_app_handler()->received_event());
985  EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
986}
987
988// Tests a single instance of GCMDriver.
989class GCMChannelStatusSyncerTest : public GCMDriverTest {
990 public:
991  GCMChannelStatusSyncerTest();
992  virtual ~GCMChannelStatusSyncerTest();
993
994  // testing::Test:
995  virtual void SetUp() OVERRIDE;
996
997  void CompleteGCMChannelStatusRequest(bool enabled, int poll_interval_seconds);
998  bool CompareDelaySeconds(bool expected_delay_seconds,
999                           bool actual_delay_seconds);
1000
1001  GCMChannelStatusSyncer* syncer() {
1002    return driver()->gcm_channel_status_syncer_for_testing();
1003  }
1004
1005 private:
1006  net::TestURLFetcherFactory url_fetcher_factory_;
1007
1008  DISALLOW_COPY_AND_ASSIGN(GCMChannelStatusSyncerTest);
1009};
1010
1011GCMChannelStatusSyncerTest::GCMChannelStatusSyncerTest() {
1012}
1013
1014GCMChannelStatusSyncerTest::~GCMChannelStatusSyncerTest() {
1015}
1016
1017void GCMChannelStatusSyncerTest::SetUp() {
1018  GCMDriverTest::SetUp();
1019
1020  url_fetcher_factory_.set_remove_fetcher_on_delete(true);
1021
1022  // Turn on all-user support.
1023  ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("GCM", "Enabled"));
1024}
1025
1026void GCMChannelStatusSyncerTest::CompleteGCMChannelStatusRequest(
1027    bool enabled, int poll_interval_seconds) {
1028  sync_pb::ExperimentStatusResponse response_proto;
1029  sync_pb::ExperimentsSpecifics* experiment_specifics =
1030      response_proto.add_experiment();
1031  experiment_specifics->mutable_gcm_channel()->set_enabled(enabled);
1032
1033  if (poll_interval_seconds)
1034    response_proto.set_poll_interval_seconds(poll_interval_seconds);
1035
1036  std::string response_string;
1037  response_proto.SerializeToString(&response_string);
1038
1039  net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
1040  ASSERT_TRUE(fetcher);
1041  fetcher->set_response_code(net::HTTP_OK);
1042  fetcher->SetResponseString(response_string);
1043  fetcher->delegate()->OnURLFetchComplete(fetcher);
1044}
1045
1046bool GCMChannelStatusSyncerTest::CompareDelaySeconds(
1047    bool expected_delay_seconds, bool actual_delay_seconds) {
1048  // Most of time, the actual delay should not be smaller than the expected
1049  // delay.
1050  if (actual_delay_seconds >= expected_delay_seconds)
1051    return true;
1052  // It is also OK that the actual delay is a bit smaller than the expected
1053  // delay in case that the test runs slowly.
1054  return expected_delay_seconds - actual_delay_seconds < 30;
1055}
1056
1057TEST_F(GCMChannelStatusSyncerTest, DisableAndEnable) {
1058  // Create GCMDriver first. GCM is not started.
1059  CreateDriver(FakeGCMClient::NO_DELAY_START);
1060  EXPECT_FALSE(driver()->IsStarted());
1061
1062  // By default, GCM is enabled.
1063  EXPECT_TRUE(driver()->gcm_enabled());
1064  EXPECT_TRUE(syncer()->gcm_enabled());
1065
1066  // Remove delay such that the request could be executed immediately.
1067  syncer()->set_delay_removed_for_testing(true);
1068
1069  // GCM will be started after app handler is added.
1070  AddAppHandlers();
1071  EXPECT_TRUE(driver()->IsStarted());
1072
1073  // GCM is still enabled at this point.
1074  EXPECT_TRUE(driver()->gcm_enabled());
1075  EXPECT_TRUE(syncer()->gcm_enabled());
1076
1077  // Wait until the GCM channel status request gets triggered.
1078  PumpUILoop();
1079
1080  // Complete the request that disables the GCM.
1081  CompleteGCMChannelStatusRequest(false, 0);
1082  EXPECT_FALSE(driver()->gcm_enabled());
1083  EXPECT_FALSE(syncer()->gcm_enabled());
1084  EXPECT_FALSE(driver()->IsStarted());
1085
1086  // Wait until next GCM channel status request gets triggered.
1087  PumpUILoop();
1088
1089  // Complete the request that enables the GCM.
1090  CompleteGCMChannelStatusRequest(true, 0);
1091  EXPECT_TRUE(driver()->gcm_enabled());
1092  EXPECT_TRUE(syncer()->gcm_enabled());
1093  EXPECT_TRUE(driver()->IsStarted());
1094}
1095
1096TEST_F(GCMChannelStatusSyncerTest, DisableRestartAndEnable) {
1097  // Create GCMDriver first. GCM is not started.
1098  CreateDriver(FakeGCMClient::NO_DELAY_START);
1099  EXPECT_FALSE(driver()->IsStarted());
1100
1101  // By default, GCM is enabled.
1102  EXPECT_TRUE(driver()->gcm_enabled());
1103  EXPECT_TRUE(syncer()->gcm_enabled());
1104
1105  // Remove delay such that the request could be executed immediately.
1106  syncer()->set_delay_removed_for_testing(true);
1107
1108  // GCM will be started after app handler is added.
1109  AddAppHandlers();
1110  EXPECT_TRUE(driver()->IsStarted());
1111
1112  // GCM is still enabled at this point.
1113  EXPECT_TRUE(driver()->gcm_enabled());
1114  EXPECT_TRUE(syncer()->gcm_enabled());
1115
1116  // Wait until the GCM channel status request gets triggered.
1117  PumpUILoop();
1118
1119  // Complete the request that disables the GCM.
1120  CompleteGCMChannelStatusRequest(false, 0);
1121  EXPECT_FALSE(driver()->gcm_enabled());
1122  EXPECT_FALSE(syncer()->gcm_enabled());
1123  EXPECT_FALSE(driver()->IsStarted());
1124
1125  // Simulate browser start by recreating GCMDriver.
1126  ShutdownDriver();
1127  CreateDriver(FakeGCMClient::NO_DELAY_START);
1128
1129  // Remove delay such that the request could be executed immediately.
1130  syncer()->set_delay_removed_for_testing(true);
1131
1132  // GCM is still disabled.
1133  EXPECT_FALSE(driver()->gcm_enabled());
1134  EXPECT_FALSE(syncer()->gcm_enabled());
1135  EXPECT_FALSE(driver()->IsStarted());
1136
1137  AddAppHandlers();
1138  EXPECT_FALSE(driver()->gcm_enabled());
1139  EXPECT_FALSE(syncer()->gcm_enabled());
1140  EXPECT_FALSE(driver()->IsStarted());
1141
1142  // Wait until the GCM channel status request gets triggered.
1143  PumpUILoop();
1144
1145  // Complete the request that re-enables the GCM.
1146  CompleteGCMChannelStatusRequest(true, 0);
1147  EXPECT_TRUE(driver()->gcm_enabled());
1148  EXPECT_TRUE(syncer()->gcm_enabled());
1149  EXPECT_TRUE(driver()->IsStarted());
1150}
1151
1152TEST_F(GCMChannelStatusSyncerTest, FirstTimePolling) {
1153  // Start GCM.
1154  CreateDriver(FakeGCMClient::NO_DELAY_START);
1155  AddAppHandlers();
1156
1157  // The 1st request should be triggered shortly without jittering.
1158  EXPECT_EQ(GCMChannelStatusSyncer::first_time_delay_seconds(),
1159            syncer()->current_request_delay_interval().InSeconds());
1160}
1161
1162TEST_F(GCMChannelStatusSyncerTest, SubsequentPollingWithDefaultInterval) {
1163  // Create GCMDriver first. GCM is not started.
1164  CreateDriver(FakeGCMClient::NO_DELAY_START);
1165
1166  // Remove delay such that the request could be executed immediately.
1167  syncer()->set_delay_removed_for_testing(true);
1168
1169  // Now GCM is started.
1170  AddAppHandlers();
1171
1172  // Wait until the GCM channel status request gets triggered.
1173  PumpUILoop();
1174
1175  // Keep delay such that we can find out the computed delay time.
1176  syncer()->set_delay_removed_for_testing(false);
1177
1178  // Complete the request. The default interval is intact.
1179  CompleteGCMChannelStatusRequest(true, 0);
1180
1181  // The next request should be scheduled at the expected default interval.
1182  int64 actual_delay_seconds =
1183      syncer()->current_request_delay_interval().InSeconds();
1184  int64 expected_delay_seconds =
1185      GCMChannelStatusRequest::default_poll_interval_seconds();
1186  EXPECT_TRUE(CompareDelaySeconds(expected_delay_seconds, actual_delay_seconds))
1187      << "expected delay: " << expected_delay_seconds
1188      << " actual delay: " << actual_delay_seconds;
1189
1190  // Simulate browser start by recreating GCMDriver.
1191  ShutdownDriver();
1192  CreateDriver(FakeGCMClient::NO_DELAY_START);
1193  AddAppHandlers();
1194
1195  // After start-up, the request should still be scheduled at the expected
1196  // default interval.
1197  actual_delay_seconds =
1198      syncer()->current_request_delay_interval().InSeconds();
1199  EXPECT_TRUE(CompareDelaySeconds(expected_delay_seconds, actual_delay_seconds))
1200      << "expected delay: " << expected_delay_seconds
1201      << " actual delay: " << actual_delay_seconds;
1202}
1203
1204TEST_F(GCMChannelStatusSyncerTest, SubsequentPollingWithUpdatedInterval) {
1205  // Create GCMDriver first. GCM is not started.
1206  CreateDriver(FakeGCMClient::NO_DELAY_START);
1207
1208  // Remove delay such that the request could be executed immediately.
1209  syncer()->set_delay_removed_for_testing(true);
1210
1211  // Now GCM is started.
1212  AddAppHandlers();
1213
1214  // Wait until the GCM channel status request gets triggered.
1215  PumpUILoop();
1216
1217  // Keep delay such that we can find out the computed delay time.
1218  syncer()->set_delay_removed_for_testing(false);
1219
1220  // Complete the request. The interval is being changed.
1221  int new_poll_interval_seconds =
1222      GCMChannelStatusRequest::default_poll_interval_seconds() * 2;
1223  CompleteGCMChannelStatusRequest(true, new_poll_interval_seconds);
1224
1225  // The next request should be scheduled at the expected updated interval.
1226  int64 actual_delay_seconds =
1227      syncer()->current_request_delay_interval().InSeconds();
1228  int64 expected_delay_seconds = new_poll_interval_seconds;
1229  EXPECT_TRUE(CompareDelaySeconds(expected_delay_seconds, actual_delay_seconds))
1230      << "expected delay: " << expected_delay_seconds
1231      << " actual delay: " << actual_delay_seconds;
1232
1233  // Simulate browser start by recreating GCMDriver.
1234  ShutdownDriver();
1235  CreateDriver(FakeGCMClient::NO_DELAY_START);
1236  AddAppHandlers();
1237
1238  // After start-up, the request should still be scheduled at the expected
1239  // updated interval.
1240  actual_delay_seconds =
1241      syncer()->current_request_delay_interval().InSeconds();
1242  EXPECT_TRUE(CompareDelaySeconds(expected_delay_seconds, actual_delay_seconds))
1243      << "expected delay: " << expected_delay_seconds
1244      << " actual delay: " << actual_delay_seconds;
1245}
1246
1247}  // namespace gcm
1248