| 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/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 } | 343 } |
| 344 | 344 |
| 345 const GServicesSettings& gservices_settings() const { | 345 const GServicesSettings& gservices_settings() const { |
| 346 return gcm_client_->gservices_settings_; | 346 return gcm_client_->gservices_settings_; |
| 347 } | 347 } |
| 348 | 348 |
| 349 const base::FilePath& temp_directory_path() const { | 349 const base::FilePath& temp_directory_path() const { |
| 350 return temp_directory_.path(); | 350 return temp_directory_.path(); |
| 351 } | 351 } |
| 352 | 352 |
| 353 base::FilePath gcm_store_path() const { |
| 354 // Pass an non-existent directory as store path to match the exact |
| 355 // behavior in the production code. Currently GCMStoreImpl checks if |
| 356 // the directory exist or not to determine the store existence. |
| 357 return temp_directory_.path().Append(FILE_PATH_LITERAL("GCM Store")); |
| 358 } |
| 359 |
| 353 int64 CurrentTime(); | 360 int64 CurrentTime(); |
| 354 | 361 |
| 355 // Tooling. | 362 // Tooling. |
| 356 void PumpLoop(); | 363 void PumpLoop(); |
| 357 void PumpLoopUntilIdle(); | 364 void PumpLoopUntilIdle(); |
| 358 void QuitLoop(); | 365 void QuitLoop(); |
| 359 void InitializeLoop(); | 366 void InitializeLoop(); |
| 360 bool CreateUniqueTempDir(); | 367 bool CreateUniqueTempDir(); |
| 361 AutoAdvancingTestClock* clock() const { | 368 AutoAdvancingTestClock* clock() const { |
| 362 return reinterpret_cast<AutoAdvancingTestClock*>(gcm_client_->clock_.get()); | 369 return reinterpret_cast<AutoAdvancingTestClock*>(gcm_client_->clock_.get()); |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 registration->sender_ids = sender_ids; | 524 registration->sender_ids = sender_ids; |
| 518 gcm_client_->registrations_[registration] = registration_id; | 525 gcm_client_->registrations_[registration] = registration_id; |
| 519 } | 526 } |
| 520 | 527 |
| 521 void GCMClientImplTest::InitializeGCMClient() { | 528 void GCMClientImplTest::InitializeGCMClient() { |
| 522 clock()->Advance(base::TimeDelta::FromMilliseconds(1)); | 529 clock()->Advance(base::TimeDelta::FromMilliseconds(1)); |
| 523 | 530 |
| 524 // Actual initialization. | 531 // Actual initialization. |
| 525 GCMClient::ChromeBuildInfo chrome_build_info; | 532 GCMClient::ChromeBuildInfo chrome_build_info; |
| 526 chrome_build_info.version = kChromeVersion; | 533 chrome_build_info.version = kChromeVersion; |
| 527 gcm_client_->Initialize(chrome_build_info, temp_directory_.path(), | 534 gcm_client_->Initialize( |
| 528 message_loop_.task_runner(), | 535 chrome_build_info, |
| 529 url_request_context_getter_, | 536 gcm_store_path(), |
| 530 make_scoped_ptr<Encryptor>(new FakeEncryptor), this); | 537 message_loop_.task_runner(), |
| 538 url_request_context_getter_, |
| 539 make_scoped_ptr<Encryptor>(new FakeEncryptor), |
| 540 this); |
| 531 } | 541 } |
| 532 | 542 |
| 533 void GCMClientImplTest::StartGCMClient() { | 543 void GCMClientImplTest::StartGCMClient() { |
| 534 // Start loading and check-in. | 544 // Start loading and check-in. |
| 535 gcm_client_->Start(GCMClient::IMMEDIATE_START); | 545 gcm_client_->Start(GCMClient::IMMEDIATE_START); |
| 536 | 546 |
| 537 PumpLoopUntilIdle(); | 547 PumpLoopUntilIdle(); |
| 538 } | 548 } |
| 539 | 549 |
| 540 void GCMClientImplTest::Register(const std::string& app_id, | 550 void GCMClientImplTest::Register(const std::string& app_id, |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 638 EXPECT_TRUE(device_checkin_info().account_tokens.empty()); | 648 EXPECT_TRUE(device_checkin_info().account_tokens.empty()); |
| 639 } | 649 } |
| 640 | 650 |
| 641 TEST_F(GCMClientImplTest, LoadingBusted) { | 651 TEST_F(GCMClientImplTest, LoadingBusted) { |
| 642 // Close the GCM store. | 652 // Close the GCM store. |
| 643 gcm_client()->Stop(); | 653 gcm_client()->Stop(); |
| 644 PumpLoopUntilIdle(); | 654 PumpLoopUntilIdle(); |
| 645 | 655 |
| 646 // Mess up the store. | 656 // Mess up the store. |
| 647 base::FilePath store_file_path = | 657 base::FilePath store_file_path = |
| 648 temp_directory_path().Append(FILE_PATH_LITERAL("CURRENT")); | 658 gcm_store_path().Append(FILE_PATH_LITERAL("CURRENT")); |
| 649 ASSERT_TRUE(base::AppendToFile(store_file_path, "A", 1)); | 659 ASSERT_TRUE(base::AppendToFile(store_file_path, "A", 1)); |
| 650 | 660 |
| 651 // Restart GCM client. The store should be reset and the loading should | 661 // Restart GCM client. The store should be reset and the loading should |
| 652 // complete successfully. | 662 // complete successfully. |
| 653 reset_last_event(); | 663 reset_last_event(); |
| 654 BuildGCMClient(base::TimeDelta()); | 664 BuildGCMClient(base::TimeDelta()); |
| 655 InitializeGCMClient(); | 665 InitializeGCMClient(); |
| 656 StartGCMClient(); | 666 StartGCMClient(); |
| 657 CompleteCheckin(kDeviceAndroidId2, | 667 CompleteCheckin(kDeviceAndroidId2, |
| 658 kDeviceSecurityToken2, | 668 kDeviceSecurityToken2, |
| (...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1100 PumpLoopUntilIdle(); | 1110 PumpLoopUntilIdle(); |
| 1101 } | 1111 } |
| 1102 | 1112 |
| 1103 TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestart) { | 1113 TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestart) { |
| 1104 // GCMClientImpl should be in INITIALIZED state at first. | 1114 // GCMClientImpl should be in INITIALIZED state at first. |
| 1105 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); | 1115 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1106 | 1116 |
| 1107 // Delay start the GCM. | 1117 // Delay start the GCM. |
| 1108 gcm_client()->Start(GCMClient::DELAYED_START); | 1118 gcm_client()->Start(GCMClient::DELAYED_START); |
| 1109 PumpLoopUntilIdle(); | 1119 PumpLoopUntilIdle(); |
| 1110 EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state()); | 1120 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1111 | 1121 |
| 1112 // Stop the GCM. | 1122 // Stop the GCM. |
| 1113 gcm_client()->Stop(); | 1123 gcm_client()->Stop(); |
| 1114 PumpLoopUntilIdle(); | 1124 PumpLoopUntilIdle(); |
| 1115 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); | 1125 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1116 | 1126 |
| 1117 // Restart the GCM without delay. | 1127 // Restart the GCM without delay. |
| 1118 gcm_client()->Start(GCMClient::IMMEDIATE_START); | 1128 gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1119 PumpLoopUntilIdle(); | 1129 PumpLoopUntilIdle(); |
| 1120 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); | 1130 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); |
| 1121 } | 1131 } |
| 1122 | 1132 |
| 1123 TEST_F(GCMClientImplStartAndStopTest, StartAndStopImmediately) { | 1133 TEST_F(GCMClientImplStartAndStopTest, DelayedStartAndStopImmediately) { |
| 1124 // GCMClientImpl should be in INITIALIZED state at first. | 1134 // GCMClientImpl should be in INITIALIZED state at first. |
| 1125 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); | 1135 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1126 | 1136 |
| 1127 // Delay start the GCM and then stop it immediately. | 1137 // Delay start the GCM and then stop it immediately. |
| 1128 gcm_client()->Start(GCMClient::DELAYED_START); | 1138 gcm_client()->Start(GCMClient::DELAYED_START); |
| 1129 gcm_client()->Stop(); | 1139 gcm_client()->Stop(); |
| 1130 PumpLoopUntilIdle(); | 1140 PumpLoopUntilIdle(); |
| 1131 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); | 1141 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1142 } |
| 1143 |
| 1144 TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndStopImmediately) { |
| 1145 // GCMClientImpl should be in INITIALIZED state at first. |
| 1146 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1132 | 1147 |
| 1133 // Start the GCM and then stop it immediately. | 1148 // Start the GCM and then stop it immediately. |
| 1134 gcm_client()->Start(GCMClient::IMMEDIATE_START); | 1149 gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1135 gcm_client()->Stop(); | 1150 gcm_client()->Stop(); |
| 1136 PumpLoopUntilIdle(); | 1151 PumpLoopUntilIdle(); |
| 1137 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); | 1152 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1138 } | 1153 } |
| 1139 | 1154 |
| 1140 TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestartImmediately) { | 1155 TEST_F(GCMClientImplStartAndStopTest, DelayedStartStopAndRestart) { |
| 1141 // GCMClientImpl should be in INITIALIZED state at first. | 1156 // GCMClientImpl should be in INITIALIZED state at first. |
| 1142 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); | 1157 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1143 | 1158 |
| 1144 // Delay start the GCM and then stop and restart it immediately. | 1159 // Delay start the GCM and then stop and restart it immediately. |
| 1145 gcm_client()->Start(GCMClient::DELAYED_START); | 1160 gcm_client()->Start(GCMClient::DELAYED_START); |
| 1146 gcm_client()->Stop(); | 1161 gcm_client()->Stop(); |
| 1147 gcm_client()->Start(GCMClient::DELAYED_START); | 1162 gcm_client()->Start(GCMClient::DELAYED_START); |
| 1148 PumpLoopUntilIdle(); | 1163 PumpLoopUntilIdle(); |
| 1149 EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state()); | 1164 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1165 } |
| 1166 |
| 1167 TEST_F(GCMClientImplStartAndStopTest, ImmediateStartStopAndRestart) { |
| 1168 // GCMClientImpl should be in INITIALIZED state at first. |
| 1169 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1150 | 1170 |
| 1151 // Start the GCM and then stop and restart it immediately. | 1171 // Start the GCM and then stop and restart it immediately. |
| 1152 gcm_client()->Start(GCMClient::IMMEDIATE_START); | 1172 gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1153 gcm_client()->Stop(); | 1173 gcm_client()->Stop(); |
| 1154 gcm_client()->Start(GCMClient::IMMEDIATE_START); | 1174 gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1155 PumpLoopUntilIdle(); | 1175 PumpLoopUntilIdle(); |
| 1156 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); | 1176 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); |
| 1157 } | 1177 } |
| 1158 | 1178 |
| 1159 TEST_F(GCMClientImplStartAndStopTest, DelayStart) { | 1179 TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndThenImmediateStart) { |
| 1160 // GCMClientImpl should be in INITIALIZED state at first. | 1180 // GCMClientImpl should be in INITIALIZED state at first. |
| 1161 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); | 1181 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1162 | 1182 |
| 1163 // Delay start the GCM. | 1183 // Start the GCM immediately and complete the checkin. |
| 1184 gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1185 PumpLoopUntilIdle(); |
| 1186 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); |
| 1187 DefaultCompleteCheckin(); |
| 1188 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
| 1189 |
| 1190 // Stop the GCM. |
| 1191 gcm_client()->Stop(); |
| 1192 PumpLoopUntilIdle(); |
| 1193 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1194 |
| 1195 // Start the GCM immediately. GCMClientImpl should be in READY state. |
| 1196 BuildGCMClient(base::TimeDelta()); |
| 1197 InitializeGCMClient(); |
| 1198 gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1199 PumpLoopUntilIdle(); |
| 1200 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
| 1201 } |
| 1202 |
| 1203 TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndThenDelayStart) { |
| 1204 // GCMClientImpl should be in INITIALIZED state at first. |
| 1205 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1206 |
| 1207 // Start the GCM immediately and complete the checkin. |
| 1208 gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1209 PumpLoopUntilIdle(); |
| 1210 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); |
| 1211 DefaultCompleteCheckin(); |
| 1212 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
| 1213 |
| 1214 // Stop the GCM. |
| 1215 gcm_client()->Stop(); |
| 1216 PumpLoopUntilIdle(); |
| 1217 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1218 |
| 1219 // Delay start the GCM. GCMClientImpl should be in LOADED state. |
| 1220 BuildGCMClient(base::TimeDelta()); |
| 1221 InitializeGCMClient(); |
| 1164 gcm_client()->Start(GCMClient::DELAYED_START); | 1222 gcm_client()->Start(GCMClient::DELAYED_START); |
| 1165 PumpLoopUntilIdle(); | 1223 PumpLoopUntilIdle(); |
| 1166 EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state()); | 1224 EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state()); |
| 1225 } |
| 1226 |
| 1227 TEST_F(GCMClientImplStartAndStopTest, DelayedStart) { |
| 1228 // GCMClientImpl should be in INITIALIZED state at first. |
| 1229 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1230 |
| 1231 // Delay start the GCM. The store will not be loaded and GCMClientImpl should |
| 1232 // still be in INITIALIZED state. |
| 1233 gcm_client()->Start(GCMClient::DELAYED_START); |
| 1234 PumpLoopUntilIdle(); |
| 1235 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1167 | 1236 |
| 1168 // Start the GCM immediately and complete the checkin. | 1237 // Start the GCM immediately and complete the checkin. |
| 1169 gcm_client()->Start(GCMClient::IMMEDIATE_START); | 1238 gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1170 PumpLoopUntilIdle(); | 1239 PumpLoopUntilIdle(); |
| 1171 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); | 1240 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); |
| 1172 DefaultCompleteCheckin(); | 1241 DefaultCompleteCheckin(); |
| 1173 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); | 1242 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
| 1174 | 1243 |
| 1175 // Registration. | 1244 // Registration. |
| 1176 std::vector<std::string> senders; | 1245 std::vector<std::string> senders; |
| 1177 senders.push_back("sender"); | 1246 senders.push_back("sender"); |
| 1178 Register(kAppId, senders); | 1247 Register(kAppId, senders); |
| 1179 CompleteRegistration("reg_id"); | 1248 CompleteRegistration("reg_id"); |
| 1180 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); | 1249 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
| 1181 | 1250 |
| 1182 // Stop the GCM. | 1251 // Stop the GCM. |
| 1183 gcm_client()->Stop(); | 1252 gcm_client()->Stop(); |
| 1184 PumpLoopUntilIdle(); | 1253 PumpLoopUntilIdle(); |
| 1185 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); | 1254 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1186 | 1255 |
| 1187 // Delay start the GCM. GCM is indeed started without delay because the | 1256 // Delay start the GCM. GCM is indeed started without delay because the |
| 1188 // registration record has been found. | 1257 // registration record has been found. |
| 1258 BuildGCMClient(base::TimeDelta()); |
| 1259 InitializeGCMClient(); |
| 1189 gcm_client()->Start(GCMClient::DELAYED_START); | 1260 gcm_client()->Start(GCMClient::DELAYED_START); |
| 1190 PumpLoopUntilIdle(); | 1261 PumpLoopUntilIdle(); |
| 1191 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); | 1262 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
| 1192 } | 1263 } |
| 1193 | 1264 |
| 1194 // Test for known account mappings and last token fetching time being passed | 1265 // Test for known account mappings and last token fetching time being passed |
| 1195 // to OnGCMReady. | 1266 // to OnGCMReady. |
| 1196 TEST_F(GCMClientImplStartAndStopTest, OnGCMReadyAccountsAndTokenFetchingTime) { | 1267 TEST_F(GCMClientImplStartAndStopTest, OnGCMReadyAccountsAndTokenFetchingTime) { |
| 1197 // Start the GCM and wait until it is ready. | 1268 // Start the GCM and wait until it is ready. |
| 1198 gcm_client()->Start(GCMClient::IMMEDIATE_START); | 1269 gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1417 | 1488 |
| 1418 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); | 1489 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
| 1419 EXPECT_EQ(kAppId, last_app_id()); | 1490 EXPECT_EQ(kAppId, last_app_id()); |
| 1420 EXPECT_EQ(GCMClient::SUCCESS, last_result()); | 1491 EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1421 // All tokens are gone now. | 1492 // All tokens are gone now. |
| 1422 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); | 1493 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); |
| 1423 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); | 1494 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); |
| 1424 } | 1495 } |
| 1425 | 1496 |
| 1426 } // namespace gcm | 1497 } // namespace gcm |
| OLD | NEW |