OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/gcm_driver/gcm_driver_desktop.h" | 5 #include "components/gcm_driver/gcm_driver_desktop.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/message_loop/message_loop_proxy.h" |
13 #include "base/metrics/field_trial.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" | 14 #include "base/run_loop.h" |
17 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
18 #include "base/test/test_simple_task_runner.h" | 16 #include "base/test/test_simple_task_runner.h" |
19 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
20 #include "components/gcm_driver/fake_gcm_app_handler.h" | 18 #include "components/gcm_driver/fake_gcm_app_handler.h" |
21 #include "components/gcm_driver/fake_gcm_client.h" | 19 #include "components/gcm_driver/fake_gcm_client.h" |
22 #include "components/gcm_driver/fake_gcm_client_factory.h" | 20 #include "components/gcm_driver/fake_gcm_client_factory.h" |
23 #include "components/gcm_driver/gcm_app_handler.h" | 21 #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" | 22 #include "components/gcm_driver/gcm_client_factory.h" |
27 #include "components/gcm_driver/gcm_connection_observer.h" | 23 #include "components/gcm_driver/gcm_connection_observer.h" |
28 #include "components/gcm_driver/proto/gcm_channel_status.pb.h" | |
29 #include "net/url_request/test_url_fetcher_factory.h" | |
30 #include "net/url_request/url_fetcher_delegate.h" | |
31 #include "net/url_request/url_request_context_getter.h" | 24 #include "net/url_request/url_request_context_getter.h" |
32 #include "net/url_request/url_request_test_util.h" | 25 #include "net/url_request/url_request_test_util.h" |
33 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
34 | 27 |
35 namespace gcm { | 28 namespace gcm { |
36 | 29 |
37 namespace { | 30 namespace { |
38 | 31 |
39 const char kTestAccountID1[] = "user1@example.com"; | 32 const char kTestAccountID1[] = "user1@example.com"; |
40 const char kTestAccountID2[] = "user2@example.com"; | 33 const char kTestAccountID2[] = "user2@example.com"; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 WAIT | 90 WAIT |
98 }; | 91 }; |
99 | 92 |
100 GCMDriverTest(); | 93 GCMDriverTest(); |
101 virtual ~GCMDriverTest(); | 94 virtual ~GCMDriverTest(); |
102 | 95 |
103 // testing::Test: | 96 // testing::Test: |
104 virtual void SetUp() OVERRIDE; | 97 virtual void SetUp() OVERRIDE; |
105 virtual void TearDown() OVERRIDE; | 98 virtual void TearDown() OVERRIDE; |
106 | 99 |
107 GCMDriverDesktop* driver() { return driver_.get(); } | 100 GCMDriver* driver() { return driver_.get(); } |
108 FakeGCMAppHandler* gcm_app_handler() { return gcm_app_handler_.get(); } | 101 FakeGCMAppHandler* gcm_app_handler() { return gcm_app_handler_.get(); } |
109 FakeGCMConnectionObserver* gcm_connection_observer() { | 102 FakeGCMConnectionObserver* gcm_connection_observer() { |
110 return gcm_connection_observer_.get(); | 103 return gcm_connection_observer_.get(); |
111 } | 104 } |
112 const std::string& registration_id() const { return registration_id_; } | 105 const std::string& registration_id() const { return registration_id_; } |
113 GCMClient::Result registration_result() const { return registration_result_; } | 106 GCMClient::Result registration_result() const { return registration_result_; } |
114 const std::string& send_message_id() const { return send_message_id_; } | 107 const std::string& send_message_id() const { return send_message_id_; } |
115 GCMClient::Result send_result() const { return send_result_; } | 108 GCMClient::Result send_result() const { return send_result_; } |
116 GCMClient::Result unregistration_result() const { | 109 GCMClient::Result unregistration_result() const { |
117 return unregistration_result_; | 110 return unregistration_result_; |
118 } | 111 } |
119 | 112 |
120 void PumpIOLoop(); | 113 void PumpIOLoop(); |
121 | 114 |
122 void ClearResults(); | 115 void ClearResults(); |
123 | 116 |
124 bool HasAppHandlers() const; | 117 bool HasAppHandlers() const; |
125 FakeGCMClient* GetGCMClient(); | 118 FakeGCMClient* GetGCMClient(); |
126 | 119 |
127 void CreateDriver(FakeGCMClient::StartMode gcm_client_start_mode); | 120 void CreateDriver(FakeGCMClient::StartMode gcm_client_start_mode); |
128 void ShutdownDriver(); | |
129 void AddAppHandlers(); | 121 void AddAppHandlers(); |
130 void RemoveAppHandlers(); | 122 void RemoveAppHandlers(); |
131 | 123 |
132 void SignIn(const std::string& account_id); | 124 void SignIn(const std::string& account_id); |
133 void SignOut(); | 125 void SignOut(); |
134 | 126 |
135 void Register(const std::string& app_id, | 127 void Register(const std::string& app_id, |
136 const std::vector<std::string>& sender_ids, | 128 const std::vector<std::string>& sender_ids, |
137 WaitToFinish wait_to_finish); | 129 WaitToFinish wait_to_finish); |
138 void Send(const std::string& app_id, | 130 void Send(const std::string& app_id, |
139 const std::string& receiver_id, | 131 const std::string& receiver_id, |
140 const GCMClient::OutgoingMessage& message, | 132 const GCMClient::OutgoingMessage& message, |
141 WaitToFinish wait_to_finish); | 133 WaitToFinish wait_to_finish); |
142 void Unregister(const std::string& app_id, WaitToFinish wait_to_finish); | 134 void Unregister(const std::string& app_id, WaitToFinish wait_to_finish); |
143 | 135 |
144 void WaitForAsyncOperation(); | 136 void WaitForAsyncOperation(); |
145 | 137 |
146 private: | 138 private: |
147 void RegisterCompleted(const std::string& registration_id, | 139 void RegisterCompleted(const std::string& registration_id, |
148 GCMClient::Result result); | 140 GCMClient::Result result); |
149 void SendCompleted(const std::string& message_id, GCMClient::Result result); | 141 void SendCompleted(const std::string& message_id, GCMClient::Result result); |
150 void UnregisterCompleted(GCMClient::Result result); | 142 void UnregisterCompleted(GCMClient::Result result); |
151 | 143 |
152 base::ScopedTempDir temp_dir_; | 144 base::ScopedTempDir temp_dir_; |
153 TestingPrefServiceSimple prefs_; | |
154 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 145 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
155 base::MessageLoopForUI message_loop_; | 146 base::MessageLoopForUI message_loop_; |
156 base::Thread io_thread_; | 147 base::Thread io_thread_; |
157 base::FieldTrialList field_trial_list_; | 148 base::FieldTrialList field_trial_list_; |
158 scoped_ptr<GCMDriverDesktop> driver_; | 149 scoped_ptr<GCMDriver> driver_; |
159 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; | 150 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; |
160 scoped_ptr<FakeGCMConnectionObserver> gcm_connection_observer_; | 151 scoped_ptr<FakeGCMConnectionObserver> gcm_connection_observer_; |
161 | 152 |
162 base::Closure async_operation_completed_callback_; | 153 base::Closure async_operation_completed_callback_; |
163 | 154 |
164 std::string registration_id_; | 155 std::string registration_id_; |
165 GCMClient::Result registration_result_; | 156 GCMClient::Result registration_result_; |
166 std::string send_message_id_; | 157 std::string send_message_id_; |
167 GCMClient::Result send_result_; | 158 GCMClient::Result send_result_; |
168 GCMClient::Result unregistration_result_; | 159 GCMClient::Result unregistration_result_; |
169 | 160 |
170 DISALLOW_COPY_AND_ASSIGN(GCMDriverTest); | 161 DISALLOW_COPY_AND_ASSIGN(GCMDriverTest); |
171 }; | 162 }; |
172 | 163 |
173 GCMDriverTest::GCMDriverTest() | 164 GCMDriverTest::GCMDriverTest() |
174 : task_runner_(new base::TestSimpleTaskRunner()), | 165 : task_runner_(new base::TestSimpleTaskRunner()), |
175 io_thread_("IOThread"), | 166 io_thread_("IOThread"), |
176 field_trial_list_(NULL), | 167 field_trial_list_(NULL), |
177 registration_result_(GCMClient::UNKNOWN_ERROR), | 168 registration_result_(GCMClient::UNKNOWN_ERROR), |
178 send_result_(GCMClient::UNKNOWN_ERROR), | 169 send_result_(GCMClient::UNKNOWN_ERROR), |
179 unregistration_result_(GCMClient::UNKNOWN_ERROR) { | 170 unregistration_result_(GCMClient::UNKNOWN_ERROR) { |
180 } | 171 } |
181 | 172 |
182 GCMDriverTest::~GCMDriverTest() { | 173 GCMDriverTest::~GCMDriverTest() { |
183 } | 174 } |
184 | 175 |
185 void GCMDriverTest::SetUp() { | 176 void GCMDriverTest::SetUp() { |
186 GCMChannelStatusSyncer::RegisterPrefs(prefs_.registry()); | |
187 io_thread_.Start(); | 177 io_thread_.Start(); |
188 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 178 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
189 } | 179 } |
190 | 180 |
191 void GCMDriverTest::TearDown() { | 181 void GCMDriverTest::TearDown() { |
192 if (!driver_) | 182 if (!driver_) |
193 return; | 183 return; |
194 | 184 |
195 ShutdownDriver(); | 185 if (gcm_connection_observer_.get()) |
| 186 driver_->RemoveConnectionObserver(gcm_connection_observer_.get()); |
| 187 driver_->Shutdown(); |
196 driver_.reset(); | 188 driver_.reset(); |
197 PumpIOLoop(); | 189 PumpIOLoop(); |
198 | 190 |
199 io_thread_.Stop(); | 191 io_thread_.Stop(); |
200 } | 192 } |
201 | 193 |
202 void GCMDriverTest::PumpIOLoop() { | 194 void GCMDriverTest::PumpIOLoop() { |
203 base::RunLoop run_loop; | 195 base::RunLoop run_loop; |
204 io_thread_.message_loop_proxy()->PostTaskAndReply( | 196 io_thread_.message_loop_proxy()->PostTaskAndReply( |
205 FROM_HERE, | 197 FROM_HERE, |
(...skipping 24 matching lines...) Expand all Loading... |
230 FakeGCMClient::StartMode gcm_client_start_mode) { | 222 FakeGCMClient::StartMode gcm_client_start_mode) { |
231 scoped_refptr<net::URLRequestContextGetter> request_context = | 223 scoped_refptr<net::URLRequestContextGetter> request_context = |
232 new net::TestURLRequestContextGetter(io_thread_.message_loop_proxy()); | 224 new net::TestURLRequestContextGetter(io_thread_.message_loop_proxy()); |
233 // TODO(johnme): Need equivalent test coverage of GCMDriverAndroid. | 225 // TODO(johnme): Need equivalent test coverage of GCMDriverAndroid. |
234 driver_.reset(new GCMDriverDesktop( | 226 driver_.reset(new GCMDriverDesktop( |
235 scoped_ptr<GCMClientFactory>(new FakeGCMClientFactory( | 227 scoped_ptr<GCMClientFactory>(new FakeGCMClientFactory( |
236 gcm_client_start_mode, | 228 gcm_client_start_mode, |
237 base::MessageLoopProxy::current(), | 229 base::MessageLoopProxy::current(), |
238 io_thread_.message_loop_proxy())).Pass(), | 230 io_thread_.message_loop_proxy())).Pass(), |
239 GCMClient::ChromeBuildInfo(), | 231 GCMClient::ChromeBuildInfo(), |
240 &prefs_, | |
241 temp_dir_.path(), | 232 temp_dir_.path(), |
242 request_context, | 233 request_context, |
243 base::MessageLoopProxy::current(), | 234 base::MessageLoopProxy::current(), |
244 io_thread_.message_loop_proxy(), | 235 io_thread_.message_loop_proxy(), |
245 task_runner_)); | 236 task_runner_)); |
246 | 237 |
247 gcm_app_handler_.reset(new FakeGCMAppHandler); | 238 gcm_app_handler_.reset(new FakeGCMAppHandler); |
248 gcm_connection_observer_.reset(new FakeGCMConnectionObserver); | 239 gcm_connection_observer_.reset(new FakeGCMConnectionObserver); |
249 | 240 |
250 driver_->AddConnectionObserver(gcm_connection_observer_.get()); | 241 driver_->AddConnectionObserver(gcm_connection_observer_.get()); |
251 } | 242 } |
252 | 243 |
253 void GCMDriverTest::ShutdownDriver() { | |
254 if (gcm_connection_observer()) | |
255 driver()->RemoveConnectionObserver(gcm_connection_observer()); | |
256 driver()->Shutdown(); | |
257 } | |
258 | |
259 void GCMDriverTest::AddAppHandlers() { | 244 void GCMDriverTest::AddAppHandlers() { |
260 driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get()); | 245 driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get()); |
261 driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get()); | 246 driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get()); |
262 } | 247 } |
263 | 248 |
264 void GCMDriverTest::RemoveAppHandlers() { | 249 void GCMDriverTest::RemoveAppHandlers() { |
265 driver_->RemoveAppHandler(kTestAppID1); | 250 driver_->RemoveAppHandler(kTestAppID1); |
266 driver_->RemoveAppHandler(kTestAppID2); | 251 driver_->RemoveAppHandler(kTestAppID2); |
267 } | 252 } |
268 | 253 |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 EXPECT_TRUE(driver()->IsConnected()); | 379 EXPECT_TRUE(driver()->IsConnected()); |
395 } | 380 } |
396 | 381 |
397 TEST_F(GCMDriverTest, Shutdown) { | 382 TEST_F(GCMDriverTest, Shutdown) { |
398 CreateDriver(FakeGCMClient::NO_DELAY_START); | 383 CreateDriver(FakeGCMClient::NO_DELAY_START); |
399 EXPECT_FALSE(HasAppHandlers()); | 384 EXPECT_FALSE(HasAppHandlers()); |
400 | 385 |
401 AddAppHandlers(); | 386 AddAppHandlers(); |
402 EXPECT_TRUE(HasAppHandlers()); | 387 EXPECT_TRUE(HasAppHandlers()); |
403 | 388 |
404 ShutdownDriver(); | 389 driver()->Shutdown(); |
405 EXPECT_FALSE(HasAppHandlers()); | 390 EXPECT_FALSE(HasAppHandlers()); |
406 EXPECT_FALSE(driver()->IsConnected()); | 391 EXPECT_FALSE(driver()->IsConnected()); |
407 EXPECT_FALSE(gcm_connection_observer()->connected()); | 392 EXPECT_FALSE(gcm_connection_observer()->connected()); |
408 } | 393 } |
409 | 394 |
410 TEST_F(GCMDriverTest, SignInAndSignOutOnGCMEnabled) { | 395 TEST_F(GCMDriverTest, SignInAndSignOutOnGCMEnabled) { |
411 // By default, GCM is enabled. | 396 // By default, GCM is enabled. |
412 CreateDriver(FakeGCMClient::NO_DELAY_START); | 397 CreateDriver(FakeGCMClient::NO_DELAY_START); |
413 AddAppHandlers(); | 398 AddAppHandlers(); |
414 | 399 |
(...skipping 534 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
949 } | 934 } |
950 | 935 |
951 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) { | 936 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) { |
952 GetGCMClient()->DeleteMessages(kTestAppID1); | 937 GetGCMClient()->DeleteMessages(kTestAppID1); |
953 gcm_app_handler()->WaitForNotification(); | 938 gcm_app_handler()->WaitForNotification(); |
954 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, | 939 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, |
955 gcm_app_handler()->received_event()); | 940 gcm_app_handler()->received_event()); |
956 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); | 941 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); |
957 } | 942 } |
958 | 943 |
959 // Tests a single instance of GCMDriver. | |
960 class GCMChannelStatusSyncerTest : public GCMDriverTest { | |
961 public: | |
962 GCMChannelStatusSyncerTest(); | |
963 virtual ~GCMChannelStatusSyncerTest(); | |
964 | |
965 // testing::Test: | |
966 virtual void SetUp() OVERRIDE; | |
967 | |
968 void CompleteGCMChannelStatusRequest(bool enabled, int poll_interval_seconds); | |
969 bool CompareDelaySeconds(bool expected_delay_seconds, | |
970 bool actual_delay_seconds); | |
971 | |
972 GCMChannelStatusSyncer* syncer() { | |
973 return driver()->gcm_channel_status_syncer_for_testing(); | |
974 } | |
975 | |
976 private: | |
977 net::TestURLFetcherFactory url_fetcher_factory_; | |
978 | |
979 DISALLOW_COPY_AND_ASSIGN(GCMChannelStatusSyncerTest); | |
980 }; | |
981 | |
982 GCMChannelStatusSyncerTest::GCMChannelStatusSyncerTest() { | |
983 } | |
984 | |
985 GCMChannelStatusSyncerTest::~GCMChannelStatusSyncerTest() { | |
986 } | |
987 | |
988 void GCMChannelStatusSyncerTest::SetUp() { | |
989 GCMDriverTest::SetUp(); | |
990 | |
991 // Turn on all-user support. | |
992 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("GCM", "Enabled")); | |
993 } | |
994 | |
995 void GCMChannelStatusSyncerTest::CompleteGCMChannelStatusRequest( | |
996 bool enabled, int poll_interval_seconds) { | |
997 gcm_proto::ExperimentStatusResponse response_proto; | |
998 response_proto.mutable_gcm_channel()->set_enabled(enabled); | |
999 | |
1000 if (poll_interval_seconds) | |
1001 response_proto.set_poll_interval_seconds(poll_interval_seconds); | |
1002 | |
1003 std::string response_string; | |
1004 response_proto.SerializeToString(&response_string); | |
1005 | |
1006 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | |
1007 ASSERT_TRUE(fetcher); | |
1008 fetcher->set_response_code(net::HTTP_OK); | |
1009 fetcher->SetResponseString(response_string); | |
1010 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
1011 } | |
1012 | |
1013 bool GCMChannelStatusSyncerTest::CompareDelaySeconds( | |
1014 bool expected_delay_seconds, bool actual_delay_seconds) { | |
1015 // Most of time, the actual delay should not be smaller than the expected | |
1016 // delay. | |
1017 if (actual_delay_seconds >= expected_delay_seconds) | |
1018 return true; | |
1019 // It is also OK that the actual delay is a bit smaller than the expected | |
1020 // delay in case that the test runs slowly. | |
1021 return expected_delay_seconds - actual_delay_seconds < 30; | |
1022 } | |
1023 | |
1024 TEST_F(GCMChannelStatusSyncerTest, DisableAndEnable) { | |
1025 // Create GCMDriver first. GCM is not started. | |
1026 CreateDriver(FakeGCMClient::NO_DELAY_START); | |
1027 EXPECT_FALSE(driver()->IsStarted()); | |
1028 | |
1029 // By default, GCM is enabled. | |
1030 EXPECT_TRUE(driver()->gcm_enabled()); | |
1031 EXPECT_TRUE(syncer()->gcm_enabled()); | |
1032 | |
1033 // Remove delay such that the request could be executed immediately. | |
1034 syncer()->set_delay_removed_for_testing(true); | |
1035 | |
1036 // GCM will be started after app handler is added. | |
1037 AddAppHandlers(); | |
1038 EXPECT_TRUE(driver()->IsStarted()); | |
1039 | |
1040 // GCM is still enabled at this point. | |
1041 EXPECT_TRUE(driver()->gcm_enabled()); | |
1042 EXPECT_TRUE(syncer()->gcm_enabled()); | |
1043 | |
1044 // Wait until the GCM channel status request gets triggered. | |
1045 PumpUILoop(); | |
1046 | |
1047 // Complete the request that disables the GCM. | |
1048 CompleteGCMChannelStatusRequest(false, 0); | |
1049 EXPECT_FALSE(driver()->gcm_enabled()); | |
1050 EXPECT_FALSE(syncer()->gcm_enabled()); | |
1051 EXPECT_FALSE(driver()->IsStarted()); | |
1052 | |
1053 // Wait until next GCM channel status request gets triggered. | |
1054 PumpUILoop(); | |
1055 | |
1056 // Complete the request that enables the GCM. | |
1057 CompleteGCMChannelStatusRequest(true, 0); | |
1058 EXPECT_TRUE(driver()->gcm_enabled()); | |
1059 EXPECT_TRUE(syncer()->gcm_enabled()); | |
1060 EXPECT_TRUE(driver()->IsStarted()); | |
1061 } | |
1062 | |
1063 TEST_F(GCMChannelStatusSyncerTest, DisableAndRestart) { | |
1064 // Create GCMDriver first. GCM is not started. | |
1065 CreateDriver(FakeGCMClient::NO_DELAY_START); | |
1066 EXPECT_FALSE(driver()->IsStarted()); | |
1067 | |
1068 // By default, GCM is enabled. | |
1069 EXPECT_TRUE(driver()->gcm_enabled()); | |
1070 EXPECT_TRUE(syncer()->gcm_enabled()); | |
1071 | |
1072 // Remove delay such that the request could be executed immediately. | |
1073 syncer()->set_delay_removed_for_testing(true); | |
1074 | |
1075 // GCM will be started after app handler is added. | |
1076 AddAppHandlers(); | |
1077 EXPECT_TRUE(driver()->IsStarted()); | |
1078 | |
1079 // GCM is still enabled at this point. | |
1080 EXPECT_TRUE(driver()->gcm_enabled()); | |
1081 EXPECT_TRUE(syncer()->gcm_enabled()); | |
1082 | |
1083 // Wait until the GCM channel status request gets triggered. | |
1084 PumpUILoop(); | |
1085 | |
1086 // Complete the request that disables the GCM. | |
1087 CompleteGCMChannelStatusRequest(false, 0); | |
1088 EXPECT_FALSE(driver()->gcm_enabled()); | |
1089 EXPECT_FALSE(syncer()->gcm_enabled()); | |
1090 EXPECT_FALSE(driver()->IsStarted()); | |
1091 | |
1092 // Simulate browser start by recreating GCMDriver. | |
1093 ShutdownDriver(); | |
1094 CreateDriver(FakeGCMClient::NO_DELAY_START); | |
1095 | |
1096 // GCM is still disabled. | |
1097 EXPECT_FALSE(driver()->gcm_enabled()); | |
1098 EXPECT_FALSE(syncer()->gcm_enabled()); | |
1099 EXPECT_FALSE(driver()->IsStarted()); | |
1100 | |
1101 AddAppHandlers(); | |
1102 EXPECT_FALSE(driver()->gcm_enabled()); | |
1103 EXPECT_FALSE(syncer()->gcm_enabled()); | |
1104 EXPECT_FALSE(driver()->IsStarted()); | |
1105 } | |
1106 | |
1107 TEST_F(GCMChannelStatusSyncerTest, FirstTimePolling) { | |
1108 // Start GCM. | |
1109 CreateDriver(FakeGCMClient::NO_DELAY_START); | |
1110 AddAppHandlers(); | |
1111 | |
1112 // The 1st request should be triggered shortly without jittering. | |
1113 EXPECT_EQ(GCMChannelStatusSyncer::first_time_delay_seconds(), | |
1114 syncer()->current_request_delay_interval().InSeconds()); | |
1115 } | |
1116 | |
1117 TEST_F(GCMChannelStatusSyncerTest, SubsequentPollingWithDefaultInterval) { | |
1118 // Create GCMDriver first. GCM is not started. | |
1119 CreateDriver(FakeGCMClient::NO_DELAY_START); | |
1120 | |
1121 // Remove delay such that the request could be executed immediately. | |
1122 syncer()->set_delay_removed_for_testing(true); | |
1123 | |
1124 // Now GCM is started. | |
1125 AddAppHandlers(); | |
1126 | |
1127 // Wait until the GCM channel status request gets triggered. | |
1128 PumpUILoop(); | |
1129 | |
1130 // Keep delay such that we can find out the computed delay time. | |
1131 syncer()->set_delay_removed_for_testing(false); | |
1132 | |
1133 // Complete the request. The default interval is intact. | |
1134 CompleteGCMChannelStatusRequest(true, 0); | |
1135 | |
1136 // The next request should be scheduled at the expected default interval. | |
1137 int64 actual_delay_seconds = | |
1138 syncer()->current_request_delay_interval().InSeconds(); | |
1139 int64 expected_delay_seconds = | |
1140 GCMChannelStatusRequest::default_poll_interval_seconds(); | |
1141 EXPECT_TRUE(CompareDelaySeconds(expected_delay_seconds, actual_delay_seconds)) | |
1142 << "expected delay: " << expected_delay_seconds | |
1143 << " actual delay: " << actual_delay_seconds; | |
1144 | |
1145 // Simulate browser start by recreating GCMDriver. | |
1146 ShutdownDriver(); | |
1147 CreateDriver(FakeGCMClient::NO_DELAY_START); | |
1148 AddAppHandlers(); | |
1149 | |
1150 // After start-up, the request should still be scheduled at the expected | |
1151 // default interval. | |
1152 actual_delay_seconds = | |
1153 syncer()->current_request_delay_interval().InSeconds(); | |
1154 EXPECT_TRUE(CompareDelaySeconds(expected_delay_seconds, actual_delay_seconds)) | |
1155 << "expected delay: " << expected_delay_seconds | |
1156 << " actual delay: " << actual_delay_seconds; | |
1157 } | |
1158 | |
1159 TEST_F(GCMChannelStatusSyncerTest, SubsequentPollingWithUpdatedInterval) { | |
1160 // Create GCMDriver first. GCM is not started. | |
1161 CreateDriver(FakeGCMClient::NO_DELAY_START); | |
1162 | |
1163 // Remove delay such that the request could be executed immediately. | |
1164 syncer()->set_delay_removed_for_testing(true); | |
1165 | |
1166 // Now GCM is started. | |
1167 AddAppHandlers(); | |
1168 | |
1169 // Wait until the GCM channel status request gets triggered. | |
1170 PumpUILoop(); | |
1171 | |
1172 // Keep delay such that we can find out the computed delay time. | |
1173 syncer()->set_delay_removed_for_testing(false); | |
1174 | |
1175 // Complete the request. The interval is being changed. | |
1176 int new_poll_interval_seconds = | |
1177 GCMChannelStatusRequest::default_poll_interval_seconds() * 2; | |
1178 CompleteGCMChannelStatusRequest(true, new_poll_interval_seconds); | |
1179 | |
1180 // The next request should be scheduled at the expected updated interval. | |
1181 int64 actual_delay_seconds = | |
1182 syncer()->current_request_delay_interval().InSeconds(); | |
1183 int64 expected_delay_seconds = new_poll_interval_seconds; | |
1184 EXPECT_TRUE(CompareDelaySeconds(expected_delay_seconds, actual_delay_seconds)) | |
1185 << "expected delay: " << expected_delay_seconds | |
1186 << " actual delay: " << actual_delay_seconds; | |
1187 | |
1188 // Simulate browser start by recreating GCMDriver. | |
1189 ShutdownDriver(); | |
1190 CreateDriver(FakeGCMClient::NO_DELAY_START); | |
1191 AddAppHandlers(); | |
1192 | |
1193 // After start-up, the request should still be scheduled at the expected | |
1194 // updated interval. | |
1195 actual_delay_seconds = | |
1196 syncer()->current_request_delay_interval().InSeconds(); | |
1197 EXPECT_TRUE(CompareDelaySeconds(expected_delay_seconds, actual_delay_seconds)) | |
1198 << "expected delay: " << expected_delay_seconds | |
1199 << " actual delay: " << actual_delay_seconds; | |
1200 } | |
1201 | |
1202 } // namespace gcm | 944 } // namespace gcm |
OLD | NEW |