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_client_impl.h" | 5 #include "components/gcm_driver/gcm_client_impl.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 } // namespace | 233 } // namespace |
234 | 234 |
235 class GCMClientImplTest : public testing::Test, | 235 class GCMClientImplTest : public testing::Test, |
236 public GCMClient::Delegate { | 236 public GCMClient::Delegate { |
237 public: | 237 public: |
238 GCMClientImplTest(); | 238 GCMClientImplTest(); |
239 virtual ~GCMClientImplTest(); | 239 virtual ~GCMClientImplTest(); |
240 | 240 |
241 virtual void SetUp() override; | 241 virtual void SetUp() override; |
242 | 242 |
| 243 void SetUpUrlFetcherFactory(); |
| 244 |
243 void BuildGCMClient(base::TimeDelta clock_step); | 245 void BuildGCMClient(base::TimeDelta clock_step); |
244 void InitializeGCMClient(); | 246 void InitializeGCMClient(); |
245 void StartGCMClient(); | 247 void StartGCMClient(); |
246 void ReceiveMessageFromMCS(const MCSMessage& message); | 248 void ReceiveMessageFromMCS(const MCSMessage& message); |
247 void ReceiveOnMessageSentToMCS( | 249 void ReceiveOnMessageSentToMCS( |
248 const std::string& app_id, | 250 const std::string& app_id, |
249 const std::string& message_id, | 251 const std::string& message_id, |
250 const MCSClient::MessageSendStatus status); | 252 const MCSClient::MessageSendStatus status); |
251 void CompleteCheckin(uint64 android_id, | 253 void CompleteCheckin(uint64 android_id, |
252 uint64 security_token, | 254 uint64 security_token, |
(...skipping 18 matching lines...) Expand all Loading... |
271 const std::string& message_id, | 273 const std::string& message_id, |
272 GCMClient::Result result) override {} | 274 GCMClient::Result result) override {} |
273 void OnMessageReceived(const std::string& registration_id, | 275 void OnMessageReceived(const std::string& registration_id, |
274 const GCMClient::IncomingMessage& message) override; | 276 const GCMClient::IncomingMessage& message) override; |
275 void OnMessagesDeleted(const std::string& app_id) override; | 277 void OnMessagesDeleted(const std::string& app_id) override; |
276 void OnMessageSendError( | 278 void OnMessageSendError( |
277 const std::string& app_id, | 279 const std::string& app_id, |
278 const gcm::GCMClient::SendErrorDetails& send_error_details) override; | 280 const gcm::GCMClient::SendErrorDetails& send_error_details) override; |
279 void OnSendAcknowledged(const std::string& app_id, | 281 void OnSendAcknowledged(const std::string& app_id, |
280 const std::string& message_id) override; | 282 const std::string& message_id) override; |
281 void OnGCMReady(const std::vector<AccountMapping>& account_mappings) override; | 283 void OnGCMReady(const std::vector<AccountMapping>& account_mappings, |
| 284 const base::Time& last_token_fetch_time) override; |
282 void OnActivityRecorded() override {} | 285 void OnActivityRecorded() override {} |
283 void OnConnected(const net::IPEndPoint& ip_endpoint) override {} | 286 void OnConnected(const net::IPEndPoint& ip_endpoint) override {} |
284 void OnDisconnected() override {} | 287 void OnDisconnected() override {} |
285 | 288 |
286 GCMClientImpl* gcm_client() const { return gcm_client_.get(); } | 289 GCMClientImpl* gcm_client() const { return gcm_client_.get(); } |
287 FakeMCSClient* mcs_client() const { | 290 FakeMCSClient* mcs_client() const { |
288 return reinterpret_cast<FakeMCSClient*>(gcm_client_->mcs_client_.get()); | 291 return reinterpret_cast<FakeMCSClient*>(gcm_client_->mcs_client_.get()); |
289 } | 292 } |
290 ConnectionFactory* connection_factory() const { | 293 ConnectionFactory* connection_factory() const { |
291 return gcm_client_->connection_factory_.get(); | 294 return gcm_client_->connection_factory_.get(); |
292 } | 295 } |
293 | 296 |
294 const GCMClientImpl::CheckinInfo& device_checkin_info() const { | 297 const GCMClientImpl::CheckinInfo& device_checkin_info() const { |
295 return gcm_client_->device_checkin_info_; | 298 return gcm_client_->device_checkin_info_; |
296 } | 299 } |
297 | 300 |
298 void reset_last_event() { | 301 void reset_last_event() { |
299 last_event_ = NONE; | 302 last_event_ = NONE; |
300 last_app_id_.clear(); | 303 last_app_id_.clear(); |
301 last_registration_id_.clear(); | 304 last_registration_id_.clear(); |
302 last_message_id_.clear(); | 305 last_message_id_.clear(); |
303 last_result_ = GCMClient::UNKNOWN_ERROR; | 306 last_result_ = GCMClient::UNKNOWN_ERROR; |
| 307 last_account_mappings_.clear(); |
| 308 last_token_fetch_time_ = base::Time(); |
304 } | 309 } |
305 | 310 |
306 LastEvent last_event() const { return last_event_; } | 311 LastEvent last_event() const { return last_event_; } |
307 const std::string& last_app_id() const { return last_app_id_; } | 312 const std::string& last_app_id() const { return last_app_id_; } |
308 const std::string& last_registration_id() const { | 313 const std::string& last_registration_id() const { |
309 return last_registration_id_; | 314 return last_registration_id_; |
310 } | 315 } |
311 const std::string& last_message_id() const { return last_message_id_; } | 316 const std::string& last_message_id() const { return last_message_id_; } |
312 GCMClient::Result last_result() const { return last_result_; } | 317 GCMClient::Result last_result() const { return last_result_; } |
313 const GCMClient::IncomingMessage& last_message() const { | 318 const GCMClient::IncomingMessage& last_message() const { |
314 return last_message_; | 319 return last_message_; |
315 } | 320 } |
316 const GCMClient::SendErrorDetails& last_error_details() const { | 321 const GCMClient::SendErrorDetails& last_error_details() const { |
317 return last_error_details_; | 322 return last_error_details_; |
318 } | 323 } |
| 324 const base::Time& last_token_fetch_time() const { |
| 325 return last_token_fetch_time_; |
| 326 } |
| 327 const std::vector<AccountMapping>& last_account_mappings() { |
| 328 return last_account_mappings_; |
| 329 } |
319 | 330 |
320 const GServicesSettings& gservices_settings() const { | 331 const GServicesSettings& gservices_settings() const { |
321 return gcm_client_->gservices_settings_; | 332 return gcm_client_->gservices_settings_; |
322 } | 333 } |
323 | 334 |
324 int64 CurrentTime(); | 335 int64 CurrentTime(); |
325 | 336 |
326 // Tooling. | 337 // Tooling. |
327 void PumpLoop(); | 338 void PumpLoop(); |
328 void PumpLoopUntilIdle(); | 339 void PumpLoopUntilIdle(); |
329 void QuitLoop(); | 340 void QuitLoop(); |
330 void InitializeLoop(); | 341 void InitializeLoop(); |
331 bool CreateUniqueTempDir(); | 342 bool CreateUniqueTempDir(); |
332 AutoAdvancingTestClock* clock() const { | 343 AutoAdvancingTestClock* clock() const { |
333 return reinterpret_cast<AutoAdvancingTestClock*>(gcm_client_->clock_.get()); | 344 return reinterpret_cast<AutoAdvancingTestClock*>(gcm_client_->clock_.get()); |
334 } | 345 } |
335 | 346 |
336 private: | 347 private: |
337 // Variables used for verification. | 348 // Variables used for verification. |
338 LastEvent last_event_; | 349 LastEvent last_event_; |
339 std::string last_app_id_; | 350 std::string last_app_id_; |
340 std::string last_registration_id_; | 351 std::string last_registration_id_; |
341 std::string last_message_id_; | 352 std::string last_message_id_; |
342 GCMClient::Result last_result_; | 353 GCMClient::Result last_result_; |
343 GCMClient::IncomingMessage last_message_; | 354 GCMClient::IncomingMessage last_message_; |
344 GCMClient::SendErrorDetails last_error_details_; | 355 GCMClient::SendErrorDetails last_error_details_; |
| 356 base::Time last_token_fetch_time_; |
| 357 std::vector<AccountMapping> last_account_mappings_; |
345 | 358 |
346 scoped_ptr<GCMClientImpl> gcm_client_; | 359 scoped_ptr<GCMClientImpl> gcm_client_; |
347 | 360 |
348 base::MessageLoop message_loop_; | 361 base::MessageLoop message_loop_; |
349 scoped_ptr<base::RunLoop> run_loop_; | 362 scoped_ptr<base::RunLoop> run_loop_; |
350 net::TestURLFetcherFactory url_fetcher_factory_; | 363 net::TestURLFetcherFactory url_fetcher_factory_; |
351 | 364 |
352 // Injected to GCM client: | 365 // Injected to GCM client: |
353 base::ScopedTempDir temp_directory_; | 366 base::ScopedTempDir temp_directory_; |
354 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; | 367 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; |
355 }; | 368 }; |
356 | 369 |
357 GCMClientImplTest::GCMClientImplTest() | 370 GCMClientImplTest::GCMClientImplTest() |
358 : last_event_(NONE), | 371 : last_event_(NONE), |
359 last_result_(GCMClient::UNKNOWN_ERROR), | 372 last_result_(GCMClient::UNKNOWN_ERROR), |
360 url_request_context_getter_(new net::TestURLRequestContextGetter( | 373 url_request_context_getter_(new net::TestURLRequestContextGetter( |
361 message_loop_.message_loop_proxy())) { | 374 message_loop_.message_loop_proxy())) { |
362 } | 375 } |
363 | 376 |
364 GCMClientImplTest::~GCMClientImplTest() {} | 377 GCMClientImplTest::~GCMClientImplTest() {} |
365 | 378 |
366 void GCMClientImplTest::SetUp() { | 379 void GCMClientImplTest::SetUp() { |
367 testing::Test::SetUp(); | 380 testing::Test::SetUp(); |
368 ASSERT_TRUE(CreateUniqueTempDir()); | 381 ASSERT_TRUE(CreateUniqueTempDir()); |
369 InitializeLoop(); | 382 InitializeLoop(); |
370 BuildGCMClient(base::TimeDelta()); | 383 BuildGCMClient(base::TimeDelta()); |
371 InitializeGCMClient(); | 384 InitializeGCMClient(); |
372 StartGCMClient(); | 385 StartGCMClient(); |
373 url_fetcher_factory_.set_remove_fetcher_on_delete(true); | 386 SetUpUrlFetcherFactory(); |
374 CompleteCheckin(kDeviceAndroidId, | 387 CompleteCheckin(kDeviceAndroidId, |
375 kDeviceSecurityToken, | 388 kDeviceSecurityToken, |
376 std::string(), | 389 std::string(), |
377 std::map<std::string, std::string>()); | 390 std::map<std::string, std::string>()); |
378 } | 391 } |
379 | 392 |
| 393 void GCMClientImplTest::SetUpUrlFetcherFactory() { |
| 394 url_fetcher_factory_.set_remove_fetcher_on_delete(true); |
| 395 } |
| 396 |
380 void GCMClientImplTest::PumpLoop() { | 397 void GCMClientImplTest::PumpLoop() { |
381 run_loop_->Run(); | 398 run_loop_->Run(); |
382 run_loop_.reset(new base::RunLoop()); | 399 run_loop_.reset(new base::RunLoop()); |
383 } | 400 } |
384 | 401 |
385 void GCMClientImplTest::PumpLoopUntilIdle() { | 402 void GCMClientImplTest::PumpLoopUntilIdle() { |
386 run_loop_->RunUntilIdle(); | 403 run_loop_->RunUntilIdle(); |
387 run_loop_.reset(new base::RunLoop()); | 404 run_loop_.reset(new base::RunLoop()); |
388 } | 405 } |
389 | 406 |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
507 } | 524 } |
508 | 525 |
509 void GCMClientImplTest::ReceiveOnMessageSentToMCS( | 526 void GCMClientImplTest::ReceiveOnMessageSentToMCS( |
510 const std::string& app_id, | 527 const std::string& app_id, |
511 const std::string& message_id, | 528 const std::string& message_id, |
512 const MCSClient::MessageSendStatus status) { | 529 const MCSClient::MessageSendStatus status) { |
513 gcm_client_->OnMessageSentToMCS(0LL, app_id, message_id, status); | 530 gcm_client_->OnMessageSentToMCS(0LL, app_id, message_id, status); |
514 } | 531 } |
515 | 532 |
516 void GCMClientImplTest::OnGCMReady( | 533 void GCMClientImplTest::OnGCMReady( |
517 const std::vector<AccountMapping>& account_mappings) { | 534 const std::vector<AccountMapping>& account_mappings, |
| 535 const base::Time& last_token_fetch_time) { |
518 last_event_ = LOADING_COMPLETED; | 536 last_event_ = LOADING_COMPLETED; |
| 537 last_account_mappings_ = account_mappings; |
| 538 last_token_fetch_time_ = last_token_fetch_time; |
519 QuitLoop(); | 539 QuitLoop(); |
520 // TODO(fgorski): Add scenario verifying contents of account_mappings, when | |
521 // the list is not empty. | |
522 } | 540 } |
523 | 541 |
524 void GCMClientImplTest::OnMessageReceived( | 542 void GCMClientImplTest::OnMessageReceived( |
525 const std::string& registration_id, | 543 const std::string& registration_id, |
526 const GCMClient::IncomingMessage& message) { | 544 const GCMClient::IncomingMessage& message) { |
527 last_event_ = MESSAGE_RECEIVED; | 545 last_event_ = MESSAGE_RECEIVED; |
528 last_app_id_ = registration_id; | 546 last_app_id_ = registration_id; |
529 last_message_ = message; | 547 last_message_ = message; |
530 QuitLoop(); | 548 QuitLoop(); |
531 } | 549 } |
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
993 EXPECT_EQ(MakeEmailToTokenMap(account_tokens), | 1011 EXPECT_EQ(MakeEmailToTokenMap(account_tokens), |
994 device_checkin_info().account_tokens); | 1012 device_checkin_info().account_tokens); |
995 } | 1013 } |
996 | 1014 |
997 class GCMClientImplStartAndStopTest : public GCMClientImplTest { | 1015 class GCMClientImplStartAndStopTest : public GCMClientImplTest { |
998 public: | 1016 public: |
999 GCMClientImplStartAndStopTest(); | 1017 GCMClientImplStartAndStopTest(); |
1000 virtual ~GCMClientImplStartAndStopTest(); | 1018 virtual ~GCMClientImplStartAndStopTest(); |
1001 | 1019 |
1002 virtual void SetUp() override; | 1020 virtual void SetUp() override; |
| 1021 |
| 1022 void DefaultCompleteCheckin(); |
1003 }; | 1023 }; |
1004 | 1024 |
1005 GCMClientImplStartAndStopTest::GCMClientImplStartAndStopTest() { | 1025 GCMClientImplStartAndStopTest::GCMClientImplStartAndStopTest() { |
1006 } | 1026 } |
1007 | 1027 |
1008 GCMClientImplStartAndStopTest::~GCMClientImplStartAndStopTest() { | 1028 GCMClientImplStartAndStopTest::~GCMClientImplStartAndStopTest() { |
1009 } | 1029 } |
1010 | 1030 |
1011 void GCMClientImplStartAndStopTest::SetUp() { | 1031 void GCMClientImplStartAndStopTest::SetUp() { |
1012 testing::Test::SetUp(); | 1032 testing::Test::SetUp(); |
1013 ASSERT_TRUE(CreateUniqueTempDir()); | 1033 ASSERT_TRUE(CreateUniqueTempDir()); |
1014 InitializeLoop(); | 1034 InitializeLoop(); |
1015 BuildGCMClient(base::TimeDelta()); | 1035 BuildGCMClient(base::TimeDelta()); |
1016 InitializeGCMClient(); | 1036 InitializeGCMClient(); |
1017 } | 1037 } |
1018 | 1038 |
| 1039 void GCMClientImplStartAndStopTest::DefaultCompleteCheckin() { |
| 1040 SetUpUrlFetcherFactory(); |
| 1041 CompleteCheckin(kDeviceAndroidId, |
| 1042 kDeviceSecurityToken, |
| 1043 std::string(), |
| 1044 std::map<std::string, std::string>()); |
| 1045 PumpLoopUntilIdle(); |
| 1046 } |
| 1047 |
1019 TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestart) { | 1048 TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestart) { |
1020 // Start the GCM and wait until it is ready. | 1049 // Start the GCM and wait until it is ready. |
1021 gcm_client()->Start(); | 1050 gcm_client()->Start(); |
1022 PumpLoopUntilIdle(); | 1051 PumpLoopUntilIdle(); |
1023 | 1052 |
1024 // Stop the GCM. | 1053 // Stop the GCM. |
1025 gcm_client()->Stop(); | 1054 gcm_client()->Stop(); |
1026 PumpLoopUntilIdle(); | 1055 PumpLoopUntilIdle(); |
1027 | 1056 |
1028 // Restart the GCM. | 1057 // Restart the GCM. |
(...skipping 11 matching lines...) Expand all Loading... |
1040 | 1069 |
1041 TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestartImmediately) { | 1070 TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestartImmediately) { |
1042 // Start the GCM and then stop and restart it immediately. | 1071 // Start the GCM and then stop and restart it immediately. |
1043 gcm_client()->Start(); | 1072 gcm_client()->Start(); |
1044 gcm_client()->Stop(); | 1073 gcm_client()->Stop(); |
1045 gcm_client()->Start(); | 1074 gcm_client()->Start(); |
1046 | 1075 |
1047 PumpLoopUntilIdle(); | 1076 PumpLoopUntilIdle(); |
1048 } | 1077 } |
1049 | 1078 |
| 1079 // Test for known account mappings and last token fetching time being passed |
| 1080 // to OnGCMReady. |
| 1081 TEST_F(GCMClientImplStartAndStopTest, OnGCMReadyAccountsAndTokenFetchingTime) { |
| 1082 // Start the GCM and wait until it is ready. |
| 1083 gcm_client()->Start(); |
| 1084 PumpLoopUntilIdle(); |
| 1085 DefaultCompleteCheckin(); |
| 1086 |
| 1087 base::Time expected_time = base::Time::Now(); |
| 1088 gcm_client()->SetLastTokenFetchTime(expected_time); |
| 1089 AccountMapping expected_mapping; |
| 1090 expected_mapping.account_id = "accId"; |
| 1091 expected_mapping.email = "email@gmail.com"; |
| 1092 expected_mapping.status = AccountMapping::MAPPED; |
| 1093 expected_mapping.status_change_timestamp = expected_time; |
| 1094 gcm_client()->UpdateAccountMapping(expected_mapping); |
| 1095 PumpLoopUntilIdle(); |
| 1096 |
| 1097 // Stop the GCM. |
| 1098 gcm_client()->Stop(); |
| 1099 PumpLoopUntilIdle(); |
| 1100 |
| 1101 // Restart the GCM. |
| 1102 gcm_client()->Start(); |
| 1103 PumpLoopUntilIdle(); |
| 1104 |
| 1105 EXPECT_EQ(LOADING_COMPLETED, last_event()); |
| 1106 EXPECT_EQ(expected_time, last_token_fetch_time()); |
| 1107 ASSERT_EQ(1UL, last_account_mappings().size()); |
| 1108 const AccountMapping& actual_mapping = last_account_mappings()[0]; |
| 1109 EXPECT_EQ(expected_mapping.account_id, actual_mapping.account_id); |
| 1110 EXPECT_EQ(expected_mapping.email, actual_mapping.email); |
| 1111 EXPECT_EQ(expected_mapping.status, actual_mapping.status); |
| 1112 EXPECT_EQ(expected_mapping.status_change_timestamp, |
| 1113 actual_mapping.status_change_timestamp); |
| 1114 } |
| 1115 |
1050 } // namespace gcm | 1116 } // namespace gcm |
OLD | NEW |