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 506 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 registration->sender_ids = sender_ids; | 517 registration->sender_ids = sender_ids; |
518 gcm_client_->registrations_[registration] = registration_id; | 518 gcm_client_->registrations_[registration] = registration_id; |
519 } | 519 } |
520 | 520 |
521 void GCMClientImplTest::InitializeGCMClient() { | 521 void GCMClientImplTest::InitializeGCMClient() { |
522 clock()->Advance(base::TimeDelta::FromMilliseconds(1)); | 522 clock()->Advance(base::TimeDelta::FromMilliseconds(1)); |
523 | 523 |
524 // Actual initialization. | 524 // Actual initialization. |
525 GCMClient::ChromeBuildInfo chrome_build_info; | 525 GCMClient::ChromeBuildInfo chrome_build_info; |
526 chrome_build_info.version = kChromeVersion; | 526 chrome_build_info.version = kChromeVersion; |
527 gcm_client_->Initialize(chrome_build_info, temp_directory_.path(), | 527 gcm_client_->Initialize( |
528 message_loop_.task_runner(), | 528 chrome_build_info, |
529 url_request_context_getter_, | 529 // Pass an non-existent directory as store path to match the exact |
530 make_scoped_ptr<Encryptor>(new FakeEncryptor), this); | 530 // behavior in the production code. Currently GCMStoreImpl checks if |
| 531 // the directory exist or not to determine the store existence. |
| 532 temp_directory_.path().Append(FILE_PATH_LITERAL("GCM Store")), |
| 533 message_loop_.task_runner(), |
| 534 url_request_context_getter_, |
| 535 make_scoped_ptr<Encryptor>(new FakeEncryptor), |
| 536 this); |
531 } | 537 } |
532 | 538 |
533 void GCMClientImplTest::StartGCMClient() { | 539 void GCMClientImplTest::StartGCMClient() { |
534 // Start loading and check-in. | 540 // Start loading and check-in. |
535 gcm_client_->Start(GCMClient::IMMEDIATE_START); | 541 gcm_client_->Start(GCMClient::IMMEDIATE_START); |
536 | 542 |
537 PumpLoopUntilIdle(); | 543 PumpLoopUntilIdle(); |
538 } | 544 } |
539 | 545 |
540 void GCMClientImplTest::Register(const std::string& app_id, | 546 void GCMClientImplTest::Register(const std::string& app_id, |
(...skipping 559 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1100 PumpLoopUntilIdle(); | 1106 PumpLoopUntilIdle(); |
1101 } | 1107 } |
1102 | 1108 |
1103 TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestart) { | 1109 TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestart) { |
1104 // GCMClientImpl should be in INITIALIZED state at first. | 1110 // GCMClientImpl should be in INITIALIZED state at first. |
1105 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); | 1111 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
1106 | 1112 |
1107 // Delay start the GCM. | 1113 // Delay start the GCM. |
1108 gcm_client()->Start(GCMClient::DELAYED_START); | 1114 gcm_client()->Start(GCMClient::DELAYED_START); |
1109 PumpLoopUntilIdle(); | 1115 PumpLoopUntilIdle(); |
1110 EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state()); | 1116 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
1111 | 1117 |
1112 // Stop the GCM. | 1118 // Stop the GCM. |
1113 gcm_client()->Stop(); | 1119 gcm_client()->Stop(); |
1114 PumpLoopUntilIdle(); | 1120 PumpLoopUntilIdle(); |
1115 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); | 1121 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
1116 | 1122 |
1117 // Restart the GCM without delay. | 1123 // Restart the GCM without delay. |
1118 gcm_client()->Start(GCMClient::IMMEDIATE_START); | 1124 gcm_client()->Start(GCMClient::IMMEDIATE_START); |
1119 PumpLoopUntilIdle(); | 1125 PumpLoopUntilIdle(); |
1120 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); | 1126 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); |
1121 } | 1127 } |
1122 | 1128 |
1123 TEST_F(GCMClientImplStartAndStopTest, StartAndStopImmediately) { | 1129 TEST_F(GCMClientImplStartAndStopTest, DelayedStartAndStopImmediately) { |
1124 // GCMClientImpl should be in INITIALIZED state at first. | 1130 // GCMClientImpl should be in INITIALIZED state at first. |
1125 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); | 1131 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
1126 | 1132 |
1127 // Delay start the GCM and then stop it immediately. | 1133 // Delay start the GCM and then stop it immediately. |
1128 gcm_client()->Start(GCMClient::DELAYED_START); | 1134 gcm_client()->Start(GCMClient::DELAYED_START); |
1129 gcm_client()->Stop(); | 1135 gcm_client()->Stop(); |
1130 PumpLoopUntilIdle(); | 1136 PumpLoopUntilIdle(); |
1131 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); | 1137 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1138 } |
| 1139 |
| 1140 TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndStopImmediately) { |
| 1141 // GCMClientImpl should be in INITIALIZED state at first. |
| 1142 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
1132 | 1143 |
1133 // Start the GCM and then stop it immediately. | 1144 // Start the GCM and then stop it immediately. |
1134 gcm_client()->Start(GCMClient::IMMEDIATE_START); | 1145 gcm_client()->Start(GCMClient::IMMEDIATE_START); |
1135 gcm_client()->Stop(); | 1146 gcm_client()->Stop(); |
1136 PumpLoopUntilIdle(); | 1147 PumpLoopUntilIdle(); |
1137 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); | 1148 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
1138 } | 1149 } |
1139 | 1150 |
1140 TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestartImmediately) { | 1151 TEST_F(GCMClientImplStartAndStopTest, DelayedStartStopAndRestart) { |
1141 // GCMClientImpl should be in INITIALIZED state at first. | 1152 // GCMClientImpl should be in INITIALIZED state at first. |
1142 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); | 1153 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
1143 | 1154 |
1144 // Delay start the GCM and then stop and restart it immediately. | 1155 // Delay start the GCM and then stop and restart it immediately. |
1145 gcm_client()->Start(GCMClient::DELAYED_START); | 1156 gcm_client()->Start(GCMClient::DELAYED_START); |
1146 gcm_client()->Stop(); | 1157 gcm_client()->Stop(); |
1147 gcm_client()->Start(GCMClient::DELAYED_START); | 1158 gcm_client()->Start(GCMClient::DELAYED_START); |
1148 PumpLoopUntilIdle(); | 1159 PumpLoopUntilIdle(); |
1149 EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state()); | 1160 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1161 } |
| 1162 |
| 1163 TEST_F(GCMClientImplStartAndStopTest, ImmediateStartStopAndRestart) { |
| 1164 // GCMClientImpl should be in INITIALIZED state at first. |
| 1165 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
1150 | 1166 |
1151 // Start the GCM and then stop and restart it immediately. | 1167 // Start the GCM and then stop and restart it immediately. |
1152 gcm_client()->Start(GCMClient::IMMEDIATE_START); | 1168 gcm_client()->Start(GCMClient::IMMEDIATE_START); |
1153 gcm_client()->Stop(); | 1169 gcm_client()->Stop(); |
1154 gcm_client()->Start(GCMClient::IMMEDIATE_START); | 1170 gcm_client()->Start(GCMClient::IMMEDIATE_START); |
1155 PumpLoopUntilIdle(); | 1171 PumpLoopUntilIdle(); |
1156 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); | 1172 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); |
1157 } | 1173 } |
1158 | 1174 |
1159 TEST_F(GCMClientImplStartAndStopTest, DelayStart) { | 1175 TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndThenImmediateStart) { |
1160 // GCMClientImpl should be in INITIALIZED state at first. | 1176 // GCMClientImpl should be in INITIALIZED state at first. |
1161 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); | 1177 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
1162 | 1178 |
1163 // Delay start the GCM. | 1179 // Start the GCM immediately and complete the checkin. |
| 1180 gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1181 PumpLoopUntilIdle(); |
| 1182 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); |
| 1183 DefaultCompleteCheckin(); |
| 1184 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
| 1185 |
| 1186 // Stop the GCM. |
| 1187 gcm_client()->Stop(); |
| 1188 PumpLoopUntilIdle(); |
| 1189 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1190 |
| 1191 // Start the GCM immediately. GCMClientImpl should be in READY state. |
| 1192 BuildGCMClient(base::TimeDelta()); |
| 1193 InitializeGCMClient(); |
| 1194 gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1195 PumpLoopUntilIdle(); |
| 1196 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
| 1197 } |
| 1198 |
| 1199 TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndThenDelayStart) { |
| 1200 // GCMClientImpl should be in INITIALIZED state at first. |
| 1201 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1202 |
| 1203 // Start the GCM immediately and complete the checkin. |
| 1204 gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1205 PumpLoopUntilIdle(); |
| 1206 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); |
| 1207 DefaultCompleteCheckin(); |
| 1208 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
| 1209 |
| 1210 // Stop the GCM. |
| 1211 gcm_client()->Stop(); |
| 1212 PumpLoopUntilIdle(); |
| 1213 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1214 |
| 1215 // Delay start the GCM. GCMClientImpl should be in LOADED state. |
| 1216 BuildGCMClient(base::TimeDelta()); |
| 1217 InitializeGCMClient(); |
1164 gcm_client()->Start(GCMClient::DELAYED_START); | 1218 gcm_client()->Start(GCMClient::DELAYED_START); |
1165 PumpLoopUntilIdle(); | 1219 PumpLoopUntilIdle(); |
1166 EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state()); | 1220 EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state()); |
| 1221 } |
| 1222 |
| 1223 TEST_F(GCMClientImplStartAndStopTest, DelayedStart) { |
| 1224 // GCMClientImpl should be in INITIALIZED state at first. |
| 1225 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1226 |
| 1227 // Delay start the GCM. The store will not be loaded and GCMClientImpl should |
| 1228 // still be in INITIALIZED state. |
| 1229 gcm_client()->Start(GCMClient::DELAYED_START); |
| 1230 PumpLoopUntilIdle(); |
| 1231 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
1167 | 1232 |
1168 // Start the GCM immediately and complete the checkin. | 1233 // Start the GCM immediately and complete the checkin. |
1169 gcm_client()->Start(GCMClient::IMMEDIATE_START); | 1234 gcm_client()->Start(GCMClient::IMMEDIATE_START); |
1170 PumpLoopUntilIdle(); | 1235 PumpLoopUntilIdle(); |
1171 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); | 1236 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); |
1172 DefaultCompleteCheckin(); | 1237 DefaultCompleteCheckin(); |
1173 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); | 1238 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
1174 | 1239 |
1175 // Registration. | 1240 // Registration. |
1176 std::vector<std::string> senders; | 1241 std::vector<std::string> senders; |
1177 senders.push_back("sender"); | 1242 senders.push_back("sender"); |
1178 Register(kAppId, senders); | 1243 Register(kAppId, senders); |
1179 CompleteRegistration("reg_id"); | 1244 CompleteRegistration("reg_id"); |
1180 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); | 1245 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
1181 | 1246 |
1182 // Stop the GCM. | 1247 // Stop the GCM. |
1183 gcm_client()->Stop(); | 1248 gcm_client()->Stop(); |
1184 PumpLoopUntilIdle(); | 1249 PumpLoopUntilIdle(); |
1185 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); | 1250 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
1186 | 1251 |
1187 // Delay start the GCM. GCM is indeed started without delay because the | 1252 // Delay start the GCM. GCM is indeed started without delay because the |
1188 // registration record has been found. | 1253 // registration record has been found. |
| 1254 BuildGCMClient(base::TimeDelta()); |
| 1255 InitializeGCMClient(); |
1189 gcm_client()->Start(GCMClient::DELAYED_START); | 1256 gcm_client()->Start(GCMClient::DELAYED_START); |
1190 PumpLoopUntilIdle(); | 1257 PumpLoopUntilIdle(); |
1191 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); | 1258 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
1192 } | 1259 } |
1193 | 1260 |
1194 // Test for known account mappings and last token fetching time being passed | 1261 // Test for known account mappings and last token fetching time being passed |
1195 // to OnGCMReady. | 1262 // to OnGCMReady. |
1196 TEST_F(GCMClientImplStartAndStopTest, OnGCMReadyAccountsAndTokenFetchingTime) { | 1263 TEST_F(GCMClientImplStartAndStopTest, OnGCMReadyAccountsAndTokenFetchingTime) { |
1197 // Start the GCM and wait until it is ready. | 1264 // Start the GCM and wait until it is ready. |
1198 gcm_client()->Start(GCMClient::IMMEDIATE_START); | 1265 gcm_client()->Start(GCMClient::IMMEDIATE_START); |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1417 | 1484 |
1418 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); | 1485 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
1419 EXPECT_EQ(kAppId, last_app_id()); | 1486 EXPECT_EQ(kAppId, last_app_id()); |
1420 EXPECT_EQ(GCMClient::SUCCESS, last_result()); | 1487 EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
1421 // All tokens are gone now. | 1488 // All tokens are gone now. |
1422 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); | 1489 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); |
1423 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); | 1490 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); |
1424 } | 1491 } |
1425 | 1492 |
1426 } // namespace gcm | 1493 } // namespace gcm |
OLD | NEW |