| 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_account_mapper.h" | 5 #include "components/gcm_driver/gcm_account_mapper.h" |
| 6 | 6 |
| 7 #include "base/test/simple_test_clock.h" | 7 #include "base/test/simple_test_clock.h" |
| 8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
| 9 #include "components/gcm_driver/fake_gcm_driver.h" | 9 #include "components/gcm_driver/fake_gcm_driver.h" |
| 10 #include "google_apis/gcm/engine/account_mapping.h" | 10 #include "google_apis/gcm/engine/account_mapping.h" |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 | 80 |
| 81 CustomFakeGCMDriver(); | 81 CustomFakeGCMDriver(); |
| 82 virtual ~CustomFakeGCMDriver(); | 82 virtual ~CustomFakeGCMDriver(); |
| 83 | 83 |
| 84 virtual void UpdateAccountMapping( | 84 virtual void UpdateAccountMapping( |
| 85 const AccountMapping& account_mapping) OVERRIDE; | 85 const AccountMapping& account_mapping) OVERRIDE; |
| 86 virtual void RemoveAccountMapping(const std::string& account_id) OVERRIDE; | 86 virtual void RemoveAccountMapping(const std::string& account_id) OVERRIDE; |
| 87 virtual void AddAppHandler(const std::string& app_id, | 87 virtual void AddAppHandler(const std::string& app_id, |
| 88 GCMAppHandler* handler) OVERRIDE; | 88 GCMAppHandler* handler) OVERRIDE; |
| 89 virtual void RemoveAppHandler(const std::string& app_id) OVERRIDE; | 89 virtual void RemoveAppHandler(const std::string& app_id) OVERRIDE; |
| 90 virtual void RegisterImpl( |
| 91 const std::string& app_id, |
| 92 const std::vector<std::string>& sender_ids) OVERRIDE; |
| 90 | 93 |
| 94 void CompleteRegister(const std::string& registration_id, |
| 95 GCMClient::Result result); |
| 91 void CompleteSend(const std::string& message_id, GCMClient::Result result); | 96 void CompleteSend(const std::string& message_id, GCMClient::Result result); |
| 92 void AcknowledgeSend(const std::string& message_id); | 97 void AcknowledgeSend(const std::string& message_id); |
| 93 void MessageSendError(const std::string& message_id); | 98 void MessageSendError(const std::string& message_id); |
| 94 | 99 |
| 95 void CompleteSendAllMessages(); | 100 void CompleteSendAllMessages(); |
| 96 void AcknowledgeSendAllMessages(); | 101 void AcknowledgeSendAllMessages(); |
| 97 | 102 |
| 98 void SetLastMessageAction(const std::string& message_id, | 103 void SetLastMessageAction(const std::string& message_id, |
| 99 LastMessageAction action); | 104 LastMessageAction action); |
| 100 void Clear(); | 105 void Clear(); |
| 101 | 106 |
| 102 const AccountMapping& last_account_mapping() const { | 107 const AccountMapping& last_account_mapping() const { |
| 103 return account_mapping_; | 108 return account_mapping_; |
| 104 } | 109 } |
| 105 const std::string& last_message_id() const { return last_message_id_; } | 110 const std::string& last_message_id() const { return last_message_id_; } |
| 106 const std::string& last_removed_account_id() const { | 111 const std::string& last_removed_account_id() const { |
| 107 return last_removed_account_id_; | 112 return last_removed_account_id_; |
| 108 } | 113 } |
| 109 LastMessageAction last_action() const { return last_action_; } | 114 LastMessageAction last_action() const { return last_action_; } |
| 115 bool registration_id_requested() const { return registration_id_requested_; } |
| 110 | 116 |
| 111 protected: | 117 protected: |
| 112 virtual void SendImpl(const std::string& app_id, | 118 virtual void SendImpl(const std::string& app_id, |
| 113 const std::string& receiver_id, | 119 const std::string& receiver_id, |
| 114 const GCMClient::OutgoingMessage& message) OVERRIDE; | 120 const GCMClient::OutgoingMessage& message) OVERRIDE; |
| 115 | 121 |
| 116 private: | 122 private: |
| 117 AccountMapping account_mapping_; | 123 AccountMapping account_mapping_; |
| 118 std::string last_message_id_; | 124 std::string last_message_id_; |
| 119 std::string last_removed_account_id_; | 125 std::string last_removed_account_id_; |
| 120 LastMessageAction last_action_; | 126 LastMessageAction last_action_; |
| 121 std::map<std::string, LastMessageAction> all_messages_; | 127 std::map<std::string, LastMessageAction> all_messages_; |
| 128 bool registration_id_requested_; |
| 122 }; | 129 }; |
| 123 | 130 |
| 124 CustomFakeGCMDriver::CustomFakeGCMDriver() : last_action_(NONE) { | 131 CustomFakeGCMDriver::CustomFakeGCMDriver() |
| 132 : last_action_(NONE), registration_id_requested_(false) { |
| 125 } | 133 } |
| 126 | 134 |
| 127 CustomFakeGCMDriver::~CustomFakeGCMDriver() { | 135 CustomFakeGCMDriver::~CustomFakeGCMDriver() { |
| 128 } | 136 } |
| 129 | 137 |
| 130 void CustomFakeGCMDriver::UpdateAccountMapping( | 138 void CustomFakeGCMDriver::UpdateAccountMapping( |
| 131 const AccountMapping& account_mapping) { | 139 const AccountMapping& account_mapping) { |
| 132 account_mapping_.email = account_mapping.email; | 140 account_mapping_.email = account_mapping.email; |
| 133 account_mapping_.account_id = account_mapping.account_id; | 141 account_mapping_.account_id = account_mapping.account_id; |
| 134 account_mapping_.access_token = account_mapping.access_token; | 142 account_mapping_.access_token = account_mapping.access_token; |
| 135 account_mapping_.status = account_mapping.status; | 143 account_mapping_.status = account_mapping.status; |
| 136 account_mapping_.status_change_timestamp = | 144 account_mapping_.status_change_timestamp = |
| 137 account_mapping.status_change_timestamp; | 145 account_mapping.status_change_timestamp; |
| 138 account_mapping_.last_message_id = account_mapping.last_message_id; | 146 account_mapping_.last_message_id = account_mapping.last_message_id; |
| 139 } | 147 } |
| 140 | 148 |
| 141 void CustomFakeGCMDriver::RemoveAccountMapping(const std::string& account_id) { | 149 void CustomFakeGCMDriver::RemoveAccountMapping(const std::string& account_id) { |
| 142 last_removed_account_id_ = account_id; | 150 last_removed_account_id_ = account_id; |
| 143 } | 151 } |
| 144 | 152 |
| 145 void CustomFakeGCMDriver::AddAppHandler(const std::string& app_id, | 153 void CustomFakeGCMDriver::AddAppHandler(const std::string& app_id, |
| 146 GCMAppHandler* handler) { | 154 GCMAppHandler* handler) { |
| 147 GCMDriver::AddAppHandler(app_id, handler); | 155 GCMDriver::AddAppHandler(app_id, handler); |
| 148 } | 156 } |
| 149 | 157 |
| 150 void CustomFakeGCMDriver::RemoveAppHandler(const std::string& app_id) { | 158 void CustomFakeGCMDriver::RemoveAppHandler(const std::string& app_id) { |
| 151 GCMDriver::RemoveAppHandler(app_id); | 159 GCMDriver::RemoveAppHandler(app_id); |
| 152 } | 160 } |
| 153 | 161 |
| 162 void CustomFakeGCMDriver::RegisterImpl( |
| 163 const std::string& app_id, |
| 164 const std::vector<std::string>& sender_ids) { |
| 165 DCHECK_EQ(kGCMAccountMapperAppId, app_id); |
| 166 DCHECK_EQ(1u, sender_ids.size()); |
| 167 DCHECK_EQ(kGCMAccountMapperSenderId, sender_ids[0]); |
| 168 registration_id_requested_ = true; |
| 169 } |
| 170 |
| 171 void CustomFakeGCMDriver::CompleteRegister(const std::string& registration_id, |
| 172 GCMClient::Result result) { |
| 173 RegisterFinished(kGCMAccountMapperAppId, registration_id, result); |
| 174 } |
| 175 |
| 154 void CustomFakeGCMDriver::CompleteSend(const std::string& message_id, | 176 void CustomFakeGCMDriver::CompleteSend(const std::string& message_id, |
| 155 GCMClient::Result result) { | 177 GCMClient::Result result) { |
| 156 SendFinished(kGCMAccountMapperAppId, message_id, result); | 178 SendFinished(kGCMAccountMapperAppId, message_id, result); |
| 157 SetLastMessageAction(message_id, SEND_FINISHED); | 179 SetLastMessageAction(message_id, SEND_FINISHED); |
| 158 } | 180 } |
| 159 | 181 |
| 160 void CustomFakeGCMDriver::AcknowledgeSend(const std::string& message_id) { | 182 void CustomFakeGCMDriver::AcknowledgeSend(const std::string& message_id) { |
| 161 GetAppHandler(kGCMAccountMapperAppId) | 183 GetAppHandler(kGCMAccountMapperAppId) |
| 162 ->OnSendAcknowledged(kGCMAccountMapperAppId, message_id); | 184 ->OnSendAcknowledged(kGCMAccountMapperAppId, message_id); |
| 163 SetLastMessageAction(message_id, SEND_ACKNOWLEDGED); | 185 SetLastMessageAction(message_id, SEND_ACKNOWLEDGED); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 if (iter->second == SEND_FINISHED) | 220 if (iter->second == SEND_FINISHED) |
| 199 AcknowledgeSend(iter->first); | 221 AcknowledgeSend(iter->first); |
| 200 } | 222 } |
| 201 } | 223 } |
| 202 | 224 |
| 203 void CustomFakeGCMDriver::Clear() { | 225 void CustomFakeGCMDriver::Clear() { |
| 204 account_mapping_ = AccountMapping(); | 226 account_mapping_ = AccountMapping(); |
| 205 last_message_id_.clear(); | 227 last_message_id_.clear(); |
| 206 last_removed_account_id_.clear(); | 228 last_removed_account_id_.clear(); |
| 207 last_action_ = NONE; | 229 last_action_ = NONE; |
| 230 registration_id_requested_ = false; |
| 208 } | 231 } |
| 209 | 232 |
| 210 void CustomFakeGCMDriver::SetLastMessageAction(const std::string& message_id, | 233 void CustomFakeGCMDriver::SetLastMessageAction(const std::string& message_id, |
| 211 LastMessageAction action) { | 234 LastMessageAction action) { |
| 212 last_action_ = action; | 235 last_action_ = action; |
| 213 last_message_id_ = message_id; | 236 last_message_id_ = message_id; |
| 214 all_messages_[message_id] = action; | 237 all_messages_[message_id] = action; |
| 215 } | 238 } |
| 216 | 239 |
| 217 } // namespace | 240 } // namespace |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 void GCMAccountMapperTest::Restart() { | 272 void GCMAccountMapperTest::Restart() { |
| 250 if (account_mapper_) | 273 if (account_mapper_) |
| 251 account_mapper_->ShutdownHandler(); | 274 account_mapper_->ShutdownHandler(); |
| 252 account_mapper_.reset(new GCMAccountMapper(&gcm_driver_)); | 275 account_mapper_.reset(new GCMAccountMapper(&gcm_driver_)); |
| 253 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); | 276 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); |
| 254 clock_ = clock.get(); | 277 clock_ = clock.get(); |
| 255 account_mapper_->SetClockForTesting(clock.PassAs<base::Clock>()); | 278 account_mapper_->SetClockForTesting(clock.PassAs<base::Clock>()); |
| 256 } | 279 } |
| 257 | 280 |
| 258 // Tests the initialization of account mappings (from the store) when empty. | 281 // Tests the initialization of account mappings (from the store) when empty. |
| 282 // It also checks that initialization triggers registration ID request. |
| 259 TEST_F(GCMAccountMapperTest, InitializeAccountMappingsEmpty) { | 283 TEST_F(GCMAccountMapperTest, InitializeAccountMappingsEmpty) { |
| 260 GCMAccountMapper::AccountMappings account_mappings; | 284 EXPECT_FALSE(gcm_driver().registration_id_requested()); |
| 261 mapper()->Initialize(account_mappings, ""); | 285 mapper()->Initialize(GCMAccountMapper::AccountMappings()); |
| 262 EXPECT_TRUE(GetAccounts().empty()); | 286 EXPECT_TRUE(GetAccounts().empty()); |
| 287 EXPECT_TRUE(gcm_driver().registration_id_requested()); |
| 288 } |
| 289 |
| 290 // Tests that registration is retried, when new tokens are delivered and in no |
| 291 // other circumstances. |
| 292 TEST_F(GCMAccountMapperTest, RegistrationRetryUponFailure) { |
| 293 mapper()->Initialize(GCMAccountMapper::AccountMappings()); |
| 294 EXPECT_TRUE(gcm_driver().registration_id_requested()); |
| 295 gcm_driver().Clear(); |
| 296 |
| 297 gcm_driver().CompleteRegister(kRegistrationId, GCMClient::UNKNOWN_ERROR); |
| 298 EXPECT_FALSE(gcm_driver().registration_id_requested()); |
| 299 gcm_driver().Clear(); |
| 300 |
| 301 std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 302 account_tokens.push_back(MakeAccountTokenInfo("acc_id2")); |
| 303 mapper()->SetAccountTokens(account_tokens); |
| 304 EXPECT_TRUE(gcm_driver().registration_id_requested()); |
| 305 gcm_driver().Clear(); |
| 306 |
| 307 gcm_driver().CompleteRegister(kRegistrationId, |
| 308 GCMClient::ASYNC_OPERATION_PENDING); |
| 309 EXPECT_FALSE(gcm_driver().registration_id_requested()); |
| 263 } | 310 } |
| 264 | 311 |
| 265 // Tests the initialization of account mappings (from the store). | 312 // Tests the initialization of account mappings (from the store). |
| 266 TEST_F(GCMAccountMapperTest, InitializeAccountMappings) { | 313 TEST_F(GCMAccountMapperTest, InitializeAccountMappings) { |
| 267 GCMAccountMapper::AccountMappings account_mappings; | 314 GCMAccountMapper::AccountMappings account_mappings; |
| 268 AccountMapping account_mapping1 = MakeAccountMapping("acc_id1", | 315 AccountMapping account_mapping1 = MakeAccountMapping("acc_id1", |
| 269 AccountMapping::MAPPED, | 316 AccountMapping::MAPPED, |
| 270 base::Time::Now(), | 317 base::Time::Now(), |
| 271 std::string()); | 318 std::string()); |
| 272 AccountMapping account_mapping2 = MakeAccountMapping("acc_id2", | 319 AccountMapping account_mapping2 = MakeAccountMapping("acc_id2", |
| 273 AccountMapping::ADDING, | 320 AccountMapping::ADDING, |
| 274 base::Time::Now(), | 321 base::Time::Now(), |
| 275 "add_message_1"); | 322 "add_message_1"); |
| 276 account_mappings.push_back(account_mapping1); | 323 account_mappings.push_back(account_mapping1); |
| 277 account_mappings.push_back(account_mapping2); | 324 account_mappings.push_back(account_mapping2); |
| 278 | 325 |
| 279 mapper()->Initialize(account_mappings, ""); | 326 mapper()->Initialize(account_mappings); |
| 280 | 327 |
| 281 GCMAccountMapper::AccountMappings mappings = GetAccounts(); | 328 GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
| 282 EXPECT_EQ(2UL, mappings.size()); | 329 EXPECT_EQ(2UL, mappings.size()); |
| 283 GCMAccountMapper::AccountMappings::const_iterator iter = mappings.begin(); | 330 GCMAccountMapper::AccountMappings::const_iterator iter = mappings.begin(); |
| 284 | 331 |
| 285 EXPECT_EQ(account_mapping1.account_id, iter->account_id); | 332 EXPECT_EQ(account_mapping1.account_id, iter->account_id); |
| 286 EXPECT_EQ(account_mapping1.email, iter->email); | 333 EXPECT_EQ(account_mapping1.email, iter->email); |
| 287 EXPECT_TRUE(account_mapping1.access_token.empty()); | 334 EXPECT_TRUE(account_mapping1.access_token.empty()); |
| 288 EXPECT_EQ(account_mapping1.status, iter->status); | 335 EXPECT_EQ(account_mapping1.status, iter->status); |
| 289 EXPECT_EQ(account_mapping1.status_change_timestamp, | 336 EXPECT_EQ(account_mapping1.status_change_timestamp, |
| 290 iter->status_change_timestamp); | 337 iter->status_change_timestamp); |
| 291 EXPECT_TRUE(account_mapping1.last_message_id.empty()); | 338 EXPECT_TRUE(account_mapping1.last_message_id.empty()); |
| 292 | 339 |
| 293 ++iter; | 340 ++iter; |
| 294 EXPECT_EQ(account_mapping2.account_id, iter->account_id); | 341 EXPECT_EQ(account_mapping2.account_id, iter->account_id); |
| 295 EXPECT_EQ(account_mapping2.email, iter->email); | 342 EXPECT_EQ(account_mapping2.email, iter->email); |
| 296 EXPECT_TRUE(account_mapping2.access_token.empty()); | 343 EXPECT_TRUE(account_mapping2.access_token.empty()); |
| 297 EXPECT_EQ(account_mapping2.status, iter->status); | 344 EXPECT_EQ(account_mapping2.status, iter->status); |
| 298 EXPECT_EQ(account_mapping2.status_change_timestamp, | 345 EXPECT_EQ(account_mapping2.status_change_timestamp, |
| 299 iter->status_change_timestamp); | 346 iter->status_change_timestamp); |
| 300 EXPECT_EQ(account_mapping2.last_message_id, iter->last_message_id); | 347 EXPECT_EQ(account_mapping2.last_message_id, iter->last_message_id); |
| 301 } | 348 } |
| 302 | 349 |
| 350 // Tests that account tokens are not processed until registration ID is |
| 351 // available. |
| 352 TEST_F(GCMAccountMapperTest, SetAccountTokensOnlyWorksWithRegisterationId) { |
| 353 // Start with empty list. |
| 354 mapper()->Initialize(GCMAccountMapper::AccountMappings()); |
| 355 |
| 356 std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 357 account_tokens.push_back(MakeAccountTokenInfo("acc_id")); |
| 358 mapper()->SetAccountTokens(account_tokens); |
| 359 |
| 360 EXPECT_TRUE(GetAccounts().empty()); |
| 361 |
| 362 account_tokens.clear(); |
| 363 account_tokens.push_back(MakeAccountTokenInfo("acc_id1")); |
| 364 account_tokens.push_back(MakeAccountTokenInfo("acc_id2")); |
| 365 mapper()->SetAccountTokens(account_tokens); |
| 366 |
| 367 EXPECT_TRUE(GetAccounts().empty()); |
| 368 |
| 369 gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
| 370 |
| 371 GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
| 372 EXPECT_EQ(2UL, mappings.size()); |
| 373 EXPECT_EQ("acc_id1", mappings[0].account_id); |
| 374 EXPECT_EQ("acc_id2", mappings[1].account_id); |
| 375 } |
| 376 |
| 303 // Tests the part where a new account is added with a token, to the point when | 377 // Tests the part where a new account is added with a token, to the point when |
| 304 // GCM message is sent. | 378 // GCM message is sent. |
| 305 TEST_F(GCMAccountMapperTest, AddMappingToMessageSent) { | 379 TEST_F(GCMAccountMapperTest, AddMappingToMessageSent) { |
| 306 mapper()->Initialize(GCMAccountMapper::AccountMappings(), kRegistrationId); | 380 mapper()->Initialize(GCMAccountMapper::AccountMappings()); |
| 381 gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
| 307 | 382 |
| 308 std::vector<GCMClient::AccountTokenInfo> account_tokens; | 383 std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 309 GCMClient::AccountTokenInfo account_token = MakeAccountTokenInfo("acc_id"); | 384 GCMClient::AccountTokenInfo account_token = MakeAccountTokenInfo("acc_id"); |
| 310 account_tokens.push_back(account_token); | 385 account_tokens.push_back(account_token); |
| 311 mapper()->SetAccountTokens(account_tokens); | 386 mapper()->SetAccountTokens(account_tokens); |
| 312 | 387 |
| 313 GCMAccountMapper::AccountMappings mappings = GetAccounts(); | 388 GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
| 314 EXPECT_EQ(1UL, mappings.size()); | 389 EXPECT_EQ(1UL, mappings.size()); |
| 315 GCMAccountMapper::AccountMappings::const_iterator iter = mappings.begin(); | 390 GCMAccountMapper::AccountMappings::const_iterator iter = mappings.begin(); |
| 316 EXPECT_EQ("acc_id", iter->account_id); | 391 EXPECT_EQ("acc_id", iter->account_id); |
| 317 EXPECT_EQ("acc_id@gmail.com", iter->email); | 392 EXPECT_EQ("acc_id@gmail.com", iter->email); |
| 318 EXPECT_EQ("acc_id_token", iter->access_token); | 393 EXPECT_EQ("acc_id_token", iter->access_token); |
| 319 EXPECT_EQ(AccountMapping::NEW, iter->status); | 394 EXPECT_EQ(AccountMapping::NEW, iter->status); |
| 320 EXPECT_EQ(base::Time(), iter->status_change_timestamp); | 395 EXPECT_EQ(base::Time(), iter->status_change_timestamp); |
| 321 | 396 |
| 322 EXPECT_TRUE(!gcm_driver().last_message_id().empty()); | 397 EXPECT_TRUE(!gcm_driver().last_message_id().empty()); |
| 323 } | 398 } |
| 324 | 399 |
| 325 // Tests the part where GCM message is successfully queued. | 400 // Tests the part where GCM message is successfully queued. |
| 326 TEST_F(GCMAccountMapperTest, AddMappingMessageQueued) { | 401 TEST_F(GCMAccountMapperTest, AddMappingMessageQueued) { |
| 327 mapper()->Initialize(GCMAccountMapper::AccountMappings(), kRegistrationId); | 402 mapper()->Initialize(GCMAccountMapper::AccountMappings()); |
| 403 gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
| 328 | 404 |
| 329 std::vector<GCMClient::AccountTokenInfo> account_tokens; | 405 std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 330 GCMClient::AccountTokenInfo account_token = MakeAccountTokenInfo("acc_id"); | 406 GCMClient::AccountTokenInfo account_token = MakeAccountTokenInfo("acc_id"); |
| 331 account_tokens.push_back(account_token); | 407 account_tokens.push_back(account_token); |
| 332 mapper()->SetAccountTokens(account_tokens); | 408 mapper()->SetAccountTokens(account_tokens); |
| 333 | 409 |
| 334 clock()->SetNow(base::Time::Now()); | 410 clock()->SetNow(base::Time::Now()); |
| 335 gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); | 411 gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); |
| 336 | 412 |
| 337 EXPECT_EQ(account_token.email, gcm_driver().last_account_mapping().email); | 413 EXPECT_EQ(account_token.email, gcm_driver().last_account_mapping().email); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 350 EXPECT_EQ(account_token.email, iter->email); | 426 EXPECT_EQ(account_token.email, iter->email); |
| 351 EXPECT_EQ(account_token.account_id, iter->account_id); | 427 EXPECT_EQ(account_token.account_id, iter->account_id); |
| 352 EXPECT_EQ(account_token.access_token, iter->access_token); | 428 EXPECT_EQ(account_token.access_token, iter->access_token); |
| 353 EXPECT_EQ(AccountMapping::ADDING, iter->status); | 429 EXPECT_EQ(AccountMapping::ADDING, iter->status); |
| 354 EXPECT_EQ(clock()->Now(), iter->status_change_timestamp); | 430 EXPECT_EQ(clock()->Now(), iter->status_change_timestamp); |
| 355 EXPECT_EQ(gcm_driver().last_message_id(), iter->last_message_id); | 431 EXPECT_EQ(gcm_driver().last_message_id(), iter->last_message_id); |
| 356 } | 432 } |
| 357 | 433 |
| 358 // Tests status change from ADDING to MAPPED (Message is acknowledged). | 434 // Tests status change from ADDING to MAPPED (Message is acknowledged). |
| 359 TEST_F(GCMAccountMapperTest, AddMappingMessageAcknowledged) { | 435 TEST_F(GCMAccountMapperTest, AddMappingMessageAcknowledged) { |
| 360 mapper()->Initialize(GCMAccountMapper::AccountMappings(), kRegistrationId); | 436 mapper()->Initialize(GCMAccountMapper::AccountMappings()); |
| 437 gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
| 361 | 438 |
| 362 std::vector<GCMClient::AccountTokenInfo> account_tokens; | 439 std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 363 GCMClient::AccountTokenInfo account_token = MakeAccountTokenInfo("acc_id"); | 440 GCMClient::AccountTokenInfo account_token = MakeAccountTokenInfo("acc_id"); |
| 364 account_tokens.push_back(account_token); | 441 account_tokens.push_back(account_token); |
| 365 mapper()->SetAccountTokens(account_tokens); | 442 mapper()->SetAccountTokens(account_tokens); |
| 366 | 443 |
| 367 clock()->SetNow(base::Time::Now()); | 444 clock()->SetNow(base::Time::Now()); |
| 368 gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); | 445 gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); |
| 369 clock()->SetNow(base::Time::Now()); | 446 clock()->SetNow(base::Time::Now()); |
| 370 gcm_driver().AcknowledgeSend(gcm_driver().last_message_id()); | 447 gcm_driver().AcknowledgeSend(gcm_driver().last_message_id()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 385 EXPECT_EQ(account_token.account_id, iter->account_id); | 462 EXPECT_EQ(account_token.account_id, iter->account_id); |
| 386 EXPECT_EQ(account_token.access_token, iter->access_token); | 463 EXPECT_EQ(account_token.access_token, iter->access_token); |
| 387 EXPECT_EQ(AccountMapping::MAPPED, iter->status); | 464 EXPECT_EQ(AccountMapping::MAPPED, iter->status); |
| 388 EXPECT_EQ(clock()->Now(), iter->status_change_timestamp); | 465 EXPECT_EQ(clock()->Now(), iter->status_change_timestamp); |
| 389 EXPECT_TRUE(iter->last_message_id.empty()); | 466 EXPECT_TRUE(iter->last_message_id.empty()); |
| 390 } | 467 } |
| 391 | 468 |
| 392 // Tests status change form ADDING to MAPPED (When message was acknowledged, | 469 // Tests status change form ADDING to MAPPED (When message was acknowledged, |
| 393 // after Chrome was restarted). | 470 // after Chrome was restarted). |
| 394 TEST_F(GCMAccountMapperTest, AddMappingMessageAckedAfterRestart) { | 471 TEST_F(GCMAccountMapperTest, AddMappingMessageAckedAfterRestart) { |
| 395 mapper()->Initialize(GCMAccountMapper::AccountMappings(), kRegistrationId); | 472 mapper()->Initialize(GCMAccountMapper::AccountMappings()); |
| 473 gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
| 396 | 474 |
| 397 std::vector<GCMClient::AccountTokenInfo> account_tokens; | 475 std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 398 GCMClient::AccountTokenInfo account_token = MakeAccountTokenInfo("acc_id"); | 476 GCMClient::AccountTokenInfo account_token = MakeAccountTokenInfo("acc_id"); |
| 399 account_tokens.push_back(account_token); | 477 account_tokens.push_back(account_token); |
| 400 mapper()->SetAccountTokens(account_tokens); | 478 mapper()->SetAccountTokens(account_tokens); |
| 401 | 479 |
| 402 clock()->SetNow(base::Time::Now()); | 480 clock()->SetNow(base::Time::Now()); |
| 403 gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); | 481 gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); |
| 404 | 482 |
| 405 Restart(); | 483 Restart(); |
| 406 GCMAccountMapper::AccountMappings stored_mappings; | 484 GCMAccountMapper::AccountMappings stored_mappings; |
| 407 stored_mappings.push_back(gcm_driver().last_account_mapping()); | 485 stored_mappings.push_back(gcm_driver().last_account_mapping()); |
| 408 mapper()->Initialize(stored_mappings, kRegistrationId); | 486 mapper()->Initialize(stored_mappings); |
| 409 | 487 |
| 410 clock()->SetNow(base::Time::Now()); | 488 clock()->SetNow(base::Time::Now()); |
| 411 gcm_driver().AcknowledgeSend(gcm_driver().last_message_id()); | 489 gcm_driver().AcknowledgeSend(gcm_driver().last_message_id()); |
| 412 | 490 |
| 413 EXPECT_EQ(account_token.email, gcm_driver().last_account_mapping().email); | 491 EXPECT_EQ(account_token.email, gcm_driver().last_account_mapping().email); |
| 414 EXPECT_EQ(account_token.account_id, | 492 EXPECT_EQ(account_token.account_id, |
| 415 gcm_driver().last_account_mapping().account_id); | 493 gcm_driver().last_account_mapping().account_id); |
| 416 EXPECT_EQ(account_token.access_token, | 494 EXPECT_EQ(account_token.access_token, |
| 417 gcm_driver().last_account_mapping().access_token); | 495 gcm_driver().last_account_mapping().access_token); |
| 418 EXPECT_EQ(AccountMapping::MAPPED, gcm_driver().last_account_mapping().status); | 496 EXPECT_EQ(AccountMapping::MAPPED, gcm_driver().last_account_mapping().status); |
| 419 EXPECT_EQ(clock()->Now(), | 497 EXPECT_EQ(clock()->Now(), |
| 420 gcm_driver().last_account_mapping().status_change_timestamp); | 498 gcm_driver().last_account_mapping().status_change_timestamp); |
| 421 EXPECT_TRUE(gcm_driver().last_account_mapping().last_message_id.empty()); | 499 EXPECT_TRUE(gcm_driver().last_account_mapping().last_message_id.empty()); |
| 422 | 500 |
| 423 GCMAccountMapper::AccountMappings mappings = GetAccounts(); | 501 GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
| 424 GCMAccountMapper::AccountMappings::const_iterator iter = mappings.begin(); | 502 GCMAccountMapper::AccountMappings::const_iterator iter = mappings.begin(); |
| 425 EXPECT_EQ(account_token.email, iter->email); | 503 EXPECT_EQ(account_token.email, iter->email); |
| 426 EXPECT_EQ(account_token.account_id, iter->account_id); | 504 EXPECT_EQ(account_token.account_id, iter->account_id); |
| 427 EXPECT_EQ(account_token.access_token, iter->access_token); | 505 EXPECT_EQ(account_token.access_token, iter->access_token); |
| 428 EXPECT_EQ(AccountMapping::MAPPED, iter->status); | 506 EXPECT_EQ(AccountMapping::MAPPED, iter->status); |
| 429 EXPECT_EQ(clock()->Now(), iter->status_change_timestamp); | 507 EXPECT_EQ(clock()->Now(), iter->status_change_timestamp); |
| 430 EXPECT_TRUE(iter->last_message_id.empty()); | 508 EXPECT_TRUE(iter->last_message_id.empty()); |
| 431 } | 509 } |
| 432 | 510 |
| 433 // Tests a case when ADD message times out for a new account. | 511 // Tests a case when ADD message times out for a new account. |
| 434 TEST_F(GCMAccountMapperTest, AddMappingMessageSendErrorForNewAccount) { | 512 TEST_F(GCMAccountMapperTest, AddMappingMessageSendErrorForNewAccount) { |
| 435 mapper()->Initialize(GCMAccountMapper::AccountMappings(), kRegistrationId); | 513 mapper()->Initialize(GCMAccountMapper::AccountMappings()); |
| 514 gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
| 436 | 515 |
| 437 std::vector<GCMClient::AccountTokenInfo> account_tokens; | 516 std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 438 GCMClient::AccountTokenInfo account_token = MakeAccountTokenInfo("acc_id"); | 517 GCMClient::AccountTokenInfo account_token = MakeAccountTokenInfo("acc_id"); |
| 439 account_tokens.push_back(account_token); | 518 account_tokens.push_back(account_token); |
| 440 mapper()->SetAccountTokens(account_tokens); | 519 mapper()->SetAccountTokens(account_tokens); |
| 441 | 520 |
| 442 clock()->SetNow(base::Time::Now()); | 521 clock()->SetNow(base::Time::Now()); |
| 443 gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); | 522 gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); |
| 444 | 523 |
| 445 clock()->SetNow(base::Time::Now()); | 524 clock()->SetNow(base::Time::Now()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 457 TEST_F(GCMAccountMapperTest, AddMappingMessageSendErrorForMappedAccount) { | 536 TEST_F(GCMAccountMapperTest, AddMappingMessageSendErrorForMappedAccount) { |
| 458 // Start with one account that is mapped. | 537 // Start with one account that is mapped. |
| 459 base::Time status_change_timestamp = base::Time::Now(); | 538 base::Time status_change_timestamp = base::Time::Now(); |
| 460 AccountMapping mapping = MakeAccountMapping("acc_id", | 539 AccountMapping mapping = MakeAccountMapping("acc_id", |
| 461 AccountMapping::MAPPED, | 540 AccountMapping::MAPPED, |
| 462 status_change_timestamp, | 541 status_change_timestamp, |
| 463 "add_message_id"); | 542 "add_message_id"); |
| 464 | 543 |
| 465 GCMAccountMapper::AccountMappings stored_mappings; | 544 GCMAccountMapper::AccountMappings stored_mappings; |
| 466 stored_mappings.push_back(mapping); | 545 stored_mappings.push_back(mapping); |
| 467 mapper()->Initialize(stored_mappings, kRegistrationId); | 546 mapper()->Initialize(stored_mappings); |
| 547 gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
| 468 | 548 |
| 469 clock()->SetNow(base::Time::Now()); | 549 clock()->SetNow(base::Time::Now()); |
| 470 gcm_driver().MessageSendError("add_message_id"); | 550 gcm_driver().MessageSendError("add_message_id"); |
| 471 | 551 |
| 472 // No new message is sent because of the send error, as the token is stale. | 552 // No new message is sent because of the send error, as the token is stale. |
| 473 // Because the account was new, the entry should be deleted. | 553 // Because the account was new, the entry should be deleted. |
| 474 EXPECT_TRUE(gcm_driver().last_message_id().empty()); | 554 EXPECT_TRUE(gcm_driver().last_message_id().empty()); |
| 475 | 555 |
| 476 GCMAccountMapper::AccountMappings mappings = GetAccounts(); | 556 GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
| 477 GCMAccountMapper::AccountMappings::const_iterator iter = mappings.begin(); | 557 GCMAccountMapper::AccountMappings::const_iterator iter = mappings.begin(); |
| 478 EXPECT_EQ(mapping.email, iter->email); | 558 EXPECT_EQ(mapping.email, iter->email); |
| 479 EXPECT_EQ(mapping.account_id, iter->account_id); | 559 EXPECT_EQ(mapping.account_id, iter->account_id); |
| 480 EXPECT_EQ(mapping.access_token, iter->access_token); | 560 EXPECT_EQ(mapping.access_token, iter->access_token); |
| 481 EXPECT_EQ(AccountMapping::MAPPED, iter->status); | 561 EXPECT_EQ(AccountMapping::MAPPED, iter->status); |
| 482 EXPECT_EQ(status_change_timestamp, iter->status_change_timestamp); | 562 EXPECT_EQ(status_change_timestamp, iter->status_change_timestamp); |
| 483 EXPECT_TRUE(iter->last_message_id.empty()); | 563 EXPECT_TRUE(iter->last_message_id.empty()); |
| 484 } | 564 } |
| 485 | 565 |
| 486 // Tests that a missing token for an account will trigger removing of that | 566 // Tests that a missing token for an account will trigger removing of that |
| 487 // account. This test goes only until the message is passed to GCM. | 567 // account. This test goes only until the message is passed to GCM. |
| 488 TEST_F(GCMAccountMapperTest, RemoveMappingToMessageSent) { | 568 TEST_F(GCMAccountMapperTest, RemoveMappingToMessageSent) { |
| 489 // Start with one account that is mapped. | 569 // Start with one account that is mapped. |
| 490 AccountMapping mapping = MakeAccountMapping("acc_id", | 570 AccountMapping mapping = MakeAccountMapping("acc_id", |
| 491 AccountMapping::MAPPED, | 571 AccountMapping::MAPPED, |
| 492 base::Time::Now(), | 572 base::Time::Now(), |
| 493 std::string()); | 573 std::string()); |
| 494 | 574 |
| 495 GCMAccountMapper::AccountMappings stored_mappings; | 575 GCMAccountMapper::AccountMappings stored_mappings; |
| 496 stored_mappings.push_back(mapping); | 576 stored_mappings.push_back(mapping); |
| 497 mapper()->Initialize(stored_mappings, kRegistrationId); | 577 mapper()->Initialize(stored_mappings); |
| 578 gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
| 498 clock()->SetNow(base::Time::Now()); | 579 clock()->SetNow(base::Time::Now()); |
| 499 | 580 |
| 500 mapper()->SetAccountTokens(std::vector<GCMClient::AccountTokenInfo>()); | 581 mapper()->SetAccountTokens(std::vector<GCMClient::AccountTokenInfo>()); |
| 501 | 582 |
| 502 EXPECT_EQ(mapping.account_id, gcm_driver().last_account_mapping().account_id); | 583 EXPECT_EQ(mapping.account_id, gcm_driver().last_account_mapping().account_id); |
| 503 EXPECT_EQ(mapping.email, gcm_driver().last_account_mapping().email); | 584 EXPECT_EQ(mapping.email, gcm_driver().last_account_mapping().email); |
| 504 EXPECT_EQ(AccountMapping::REMOVING, | 585 EXPECT_EQ(AccountMapping::REMOVING, |
| 505 gcm_driver().last_account_mapping().status); | 586 gcm_driver().last_account_mapping().status); |
| 506 EXPECT_EQ(clock()->Now(), | 587 EXPECT_EQ(clock()->Now(), |
| 507 gcm_driver().last_account_mapping().status_change_timestamp); | 588 gcm_driver().last_account_mapping().status_change_timestamp); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 521 // account. This test goes until the message is queued by GCM. | 602 // account. This test goes until the message is queued by GCM. |
| 522 TEST_F(GCMAccountMapperTest, RemoveMappingMessageQueued) { | 603 TEST_F(GCMAccountMapperTest, RemoveMappingMessageQueued) { |
| 523 // Start with one account that is mapped. | 604 // Start with one account that is mapped. |
| 524 AccountMapping mapping = MakeAccountMapping("acc_id", | 605 AccountMapping mapping = MakeAccountMapping("acc_id", |
| 525 AccountMapping::MAPPED, | 606 AccountMapping::MAPPED, |
| 526 base::Time::Now(), | 607 base::Time::Now(), |
| 527 std::string()); | 608 std::string()); |
| 528 | 609 |
| 529 GCMAccountMapper::AccountMappings stored_mappings; | 610 GCMAccountMapper::AccountMappings stored_mappings; |
| 530 stored_mappings.push_back(mapping); | 611 stored_mappings.push_back(mapping); |
| 531 mapper()->Initialize(stored_mappings, kRegistrationId); | 612 mapper()->Initialize(stored_mappings); |
| 613 gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
| 532 clock()->SetNow(base::Time::Now()); | 614 clock()->SetNow(base::Time::Now()); |
| 533 base::Time status_change_timestamp = clock()->Now(); | 615 base::Time status_change_timestamp = clock()->Now(); |
| 534 | 616 |
| 535 mapper()->SetAccountTokens(std::vector<GCMClient::AccountTokenInfo>()); | 617 mapper()->SetAccountTokens(std::vector<GCMClient::AccountTokenInfo>()); |
| 536 clock()->SetNow(base::Time::Now()); | 618 clock()->SetNow(base::Time::Now()); |
| 537 gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); | 619 gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); |
| 538 | 620 |
| 539 EXPECT_EQ(mapping.account_id, gcm_driver().last_account_mapping().account_id); | 621 EXPECT_EQ(mapping.account_id, gcm_driver().last_account_mapping().account_id); |
| 540 EXPECT_EQ(mapping.email, gcm_driver().last_account_mapping().email); | 622 EXPECT_EQ(mapping.email, gcm_driver().last_account_mapping().email); |
| 541 EXPECT_EQ(AccountMapping::REMOVING, | 623 EXPECT_EQ(AccountMapping::REMOVING, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 561 // account mapping being completely gone. | 643 // account mapping being completely gone. |
| 562 TEST_F(GCMAccountMapperTest, RemoveMappingMessageAcknowledged) { | 644 TEST_F(GCMAccountMapperTest, RemoveMappingMessageAcknowledged) { |
| 563 // Start with one account that is mapped. | 645 // Start with one account that is mapped. |
| 564 AccountMapping mapping = MakeAccountMapping("acc_id", | 646 AccountMapping mapping = MakeAccountMapping("acc_id", |
| 565 AccountMapping::MAPPED, | 647 AccountMapping::MAPPED, |
| 566 base::Time::Now(), | 648 base::Time::Now(), |
| 567 std::string()); | 649 std::string()); |
| 568 | 650 |
| 569 GCMAccountMapper::AccountMappings stored_mappings; | 651 GCMAccountMapper::AccountMappings stored_mappings; |
| 570 stored_mappings.push_back(mapping); | 652 stored_mappings.push_back(mapping); |
| 571 mapper()->Initialize(stored_mappings, kRegistrationId); | 653 mapper()->Initialize(stored_mappings); |
| 654 gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
| 572 clock()->SetNow(base::Time::Now()); | 655 clock()->SetNow(base::Time::Now()); |
| 573 | 656 |
| 574 mapper()->SetAccountTokens(std::vector<GCMClient::AccountTokenInfo>()); | 657 mapper()->SetAccountTokens(std::vector<GCMClient::AccountTokenInfo>()); |
| 575 gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); | 658 gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); |
| 576 gcm_driver().AcknowledgeSend(gcm_driver().last_message_id()); | 659 gcm_driver().AcknowledgeSend(gcm_driver().last_message_id()); |
| 577 | 660 |
| 578 EXPECT_EQ(mapping.account_id, gcm_driver().last_removed_account_id()); | 661 EXPECT_EQ(mapping.account_id, gcm_driver().last_removed_account_id()); |
| 579 | 662 |
| 580 GCMAccountMapper::AccountMappings mappings = GetAccounts(); | 663 GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
| 581 EXPECT_TRUE(mappings.empty()); | 664 EXPECT_TRUE(mappings.empty()); |
| 582 } | 665 } |
| 583 | 666 |
| 584 // Tests that account removing proceeds, when a removing message is acked after | 667 // Tests that account removing proceeds, when a removing message is acked after |
| 585 // Chrome was restarted. | 668 // Chrome was restarted. |
| 586 TEST_F(GCMAccountMapperTest, RemoveMappingMessageAckedAfterRestart) { | 669 TEST_F(GCMAccountMapperTest, RemoveMappingMessageAckedAfterRestart) { |
| 587 // Start with one account that is mapped. | 670 // Start with one account that is mapped. |
| 588 AccountMapping mapping = MakeAccountMapping("acc_id", | 671 AccountMapping mapping = MakeAccountMapping("acc_id", |
| 589 AccountMapping::REMOVING, | 672 AccountMapping::REMOVING, |
| 590 base::Time::Now(), | 673 base::Time::Now(), |
| 591 "remove_message_id"); | 674 "remove_message_id"); |
| 592 | 675 |
| 593 GCMAccountMapper::AccountMappings stored_mappings; | 676 GCMAccountMapper::AccountMappings stored_mappings; |
| 594 stored_mappings.push_back(mapping); | 677 stored_mappings.push_back(mapping); |
| 595 mapper()->Initialize(stored_mappings, kRegistrationId); | 678 mapper()->Initialize(stored_mappings); |
| 596 | 679 |
| 597 gcm_driver().AcknowledgeSend("remove_message_id"); | 680 gcm_driver().AcknowledgeSend("remove_message_id"); |
| 598 | 681 |
| 599 EXPECT_EQ(mapping.account_id, gcm_driver().last_removed_account_id()); | 682 EXPECT_EQ(mapping.account_id, gcm_driver().last_removed_account_id()); |
| 600 | 683 |
| 601 GCMAccountMapper::AccountMappings mappings = GetAccounts(); | 684 GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
| 602 EXPECT_TRUE(mappings.empty()); | 685 EXPECT_TRUE(mappings.empty()); |
| 603 } | 686 } |
| 604 | 687 |
| 605 // Tests that account removing proceeds, when a removing message is acked after | 688 // Tests that account removing proceeds, when a removing message is acked after |
| 606 // Chrome was restarted. | 689 // Chrome was restarted. |
| 607 TEST_F(GCMAccountMapperTest, RemoveMappingMessageSendError) { | 690 TEST_F(GCMAccountMapperTest, RemoveMappingMessageSendError) { |
| 608 // Start with one account that is mapped. | 691 // Start with one account that is mapped. |
| 609 base::Time status_change_timestamp = base::Time::Now(); | 692 base::Time status_change_timestamp = base::Time::Now(); |
| 610 AccountMapping mapping = MakeAccountMapping("acc_id", | 693 AccountMapping mapping = MakeAccountMapping("acc_id", |
| 611 AccountMapping::REMOVING, | 694 AccountMapping::REMOVING, |
| 612 status_change_timestamp, | 695 status_change_timestamp, |
| 613 "remove_message_id"); | 696 "remove_message_id"); |
| 614 | 697 |
| 615 GCMAccountMapper::AccountMappings stored_mappings; | 698 GCMAccountMapper::AccountMappings stored_mappings; |
| 616 stored_mappings.push_back(mapping); | 699 stored_mappings.push_back(mapping); |
| 617 mapper()->Initialize(stored_mappings, kRegistrationId); | 700 mapper()->Initialize(stored_mappings); |
| 618 | 701 |
| 619 clock()->SetNow(base::Time::Now()); | 702 clock()->SetNow(base::Time::Now()); |
| 620 gcm_driver().MessageSendError("remove_message_id"); | 703 gcm_driver().MessageSendError("remove_message_id"); |
| 621 | 704 |
| 622 EXPECT_TRUE(gcm_driver().last_removed_account_id().empty()); | 705 EXPECT_TRUE(gcm_driver().last_removed_account_id().empty()); |
| 623 | 706 |
| 624 EXPECT_EQ(mapping.account_id, gcm_driver().last_account_mapping().account_id); | 707 EXPECT_EQ(mapping.account_id, gcm_driver().last_account_mapping().account_id); |
| 625 EXPECT_EQ(mapping.email, gcm_driver().last_account_mapping().email); | 708 EXPECT_EQ(mapping.email, gcm_driver().last_account_mapping().email); |
| 626 EXPECT_EQ(AccountMapping::REMOVING, | 709 EXPECT_EQ(AccountMapping::REMOVING, |
| 627 gcm_driver().last_account_mapping().status); | 710 gcm_driver().last_account_mapping().status); |
| 628 EXPECT_EQ(status_change_timestamp, | 711 EXPECT_EQ(status_change_timestamp, |
| 629 gcm_driver().last_account_mapping().status_change_timestamp); | 712 gcm_driver().last_account_mapping().status_change_timestamp); |
| 630 // Message is not persisted, until send is completed. | 713 // Message is not persisted, until send is completed. |
| 631 EXPECT_TRUE(gcm_driver().last_account_mapping().last_message_id.empty()); | 714 EXPECT_TRUE(gcm_driver().last_account_mapping().last_message_id.empty()); |
| 632 | 715 |
| 633 GCMAccountMapper::AccountMappings mappings = GetAccounts(); | 716 GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
| 634 GCMAccountMapper::AccountMappings::const_iterator iter = mappings.begin(); | 717 GCMAccountMapper::AccountMappings::const_iterator iter = mappings.begin(); |
| 635 EXPECT_EQ(mapping.email, iter->email); | 718 EXPECT_EQ(mapping.email, iter->email); |
| 636 EXPECT_EQ(mapping.account_id, iter->account_id); | 719 EXPECT_EQ(mapping.account_id, iter->account_id); |
| 637 EXPECT_TRUE(iter->access_token.empty()); | 720 EXPECT_TRUE(iter->access_token.empty()); |
| 638 EXPECT_EQ(AccountMapping::REMOVING, iter->status); | 721 EXPECT_EQ(AccountMapping::REMOVING, iter->status); |
| 639 EXPECT_EQ(status_change_timestamp, iter->status_change_timestamp); | 722 EXPECT_EQ(status_change_timestamp, iter->status_change_timestamp); |
| 640 EXPECT_TRUE(iter->last_message_id.empty()); | 723 EXPECT_TRUE(iter->last_message_id.empty()); |
| 641 } | 724 } |
| 642 | 725 |
| 643 // Tests that, if a new token arrives when the adding message is in progress | 726 // Tests that, if a new token arrives when the adding message is in progress |
| 644 // no new message is sent and account mapper still waits for the first one to | 727 // no new message is sent and account mapper still waits for the first one to |
| 645 // complete. | 728 // complete. |
| 646 TEST_F(GCMAccountMapperTest, TokenIsRefreshedWhenAdding) { | 729 TEST_F(GCMAccountMapperTest, TokenIsRefreshedWhenAdding) { |
| 647 mapper()->Initialize(GCMAccountMapper::AccountMappings(), kRegistrationId); | 730 mapper()->Initialize(GCMAccountMapper::AccountMappings()); |
| 731 gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
| 648 | 732 |
| 649 clock()->SetNow(base::Time::Now()); | 733 clock()->SetNow(base::Time::Now()); |
| 650 std::vector<GCMClient::AccountTokenInfo> account_tokens; | 734 std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 651 GCMClient::AccountTokenInfo account_token = MakeAccountTokenInfo("acc_id"); | 735 GCMClient::AccountTokenInfo account_token = MakeAccountTokenInfo("acc_id"); |
| 652 account_tokens.push_back(account_token); | 736 account_tokens.push_back(account_token); |
| 653 mapper()->SetAccountTokens(account_tokens); | 737 mapper()->SetAccountTokens(account_tokens); |
| 654 DCHECK_EQ(CustomFakeGCMDriver::SEND_STARTED, gcm_driver().last_action()); | 738 DCHECK_EQ(CustomFakeGCMDriver::SEND_STARTED, gcm_driver().last_action()); |
| 655 | 739 |
| 656 clock()->SetNow(base::Time::Now()); | 740 clock()->SetNow(base::Time::Now()); |
| 657 gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); | 741 gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); |
| 658 DCHECK_EQ(CustomFakeGCMDriver::SEND_FINISHED, gcm_driver().last_action()); | 742 DCHECK_EQ(CustomFakeGCMDriver::SEND_FINISHED, gcm_driver().last_action()); |
| 659 | 743 |
| 660 // Providing another token and clearing status. | 744 // Providing another token and clearing status. |
| 661 gcm_driver().Clear(); | 745 gcm_driver().Clear(); |
| 662 mapper()->SetAccountTokens(account_tokens); | 746 mapper()->SetAccountTokens(account_tokens); |
| 663 DCHECK_EQ(CustomFakeGCMDriver::NONE, gcm_driver().last_action()); | 747 DCHECK_EQ(CustomFakeGCMDriver::NONE, gcm_driver().last_action()); |
| 664 } | 748 } |
| 665 | 749 |
| 666 // Tests that, if a new token arrives when a removing message is in progress | 750 // Tests that, if a new token arrives when a removing message is in progress |
| 667 // a new adding message is sent and while account mapping status is changed to | 751 // a new adding message is sent and while account mapping status is changed to |
| 668 // mapped. If the original Removing message arrives it is discarded. | 752 // mapped. If the original Removing message arrives it is discarded. |
| 669 TEST_F(GCMAccountMapperTest, TokenIsRefreshedWhenRemoving) { | 753 TEST_F(GCMAccountMapperTest, TokenIsRefreshedWhenRemoving) { |
| 670 // Start with one account that is mapped. | 754 // Start with one account that is mapped. |
| 671 AccountMapping mapping = MakeAccountMapping( | 755 AccountMapping mapping = MakeAccountMapping( |
| 672 "acc_id", AccountMapping::MAPPED, base::Time::Now(), std::string()); | 756 "acc_id", AccountMapping::MAPPED, base::Time::Now(), std::string()); |
| 673 | 757 |
| 674 GCMAccountMapper::AccountMappings stored_mappings; | 758 GCMAccountMapper::AccountMappings stored_mappings; |
| 675 stored_mappings.push_back(mapping); | 759 stored_mappings.push_back(mapping); |
| 676 mapper()->Initialize(stored_mappings, kRegistrationId); | 760 mapper()->Initialize(stored_mappings); |
| 761 gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
| 677 clock()->SetNow(base::Time::Now()); | 762 clock()->SetNow(base::Time::Now()); |
| 678 | 763 |
| 679 // Remove the token to trigger a remove message to be sent | 764 // Remove the token to trigger a remove message to be sent |
| 680 mapper()->SetAccountTokens(std::vector<GCMClient::AccountTokenInfo>()); | 765 mapper()->SetAccountTokens(std::vector<GCMClient::AccountTokenInfo>()); |
| 681 EXPECT_EQ(CustomFakeGCMDriver::SEND_STARTED, gcm_driver().last_action()); | 766 EXPECT_EQ(CustomFakeGCMDriver::SEND_STARTED, gcm_driver().last_action()); |
| 682 gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); | 767 gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); |
| 683 EXPECT_EQ(CustomFakeGCMDriver::SEND_FINISHED, gcm_driver().last_action()); | 768 EXPECT_EQ(CustomFakeGCMDriver::SEND_FINISHED, gcm_driver().last_action()); |
| 684 | 769 |
| 685 std::string remove_message_id = gcm_driver().last_message_id(); | 770 std::string remove_message_id = gcm_driver().last_message_id(); |
| 686 gcm_driver().Clear(); | 771 gcm_driver().Clear(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 719 clock()->SetNow(base::Time::Now()); | 804 clock()->SetNow(base::Time::Now()); |
| 720 base::Time half_hour_ago = clock()->Now() - base::TimeDelta::FromMinutes(30); | 805 base::Time half_hour_ago = clock()->Now() - base::TimeDelta::FromMinutes(30); |
| 721 GCMAccountMapper::AccountMappings stored_mappings; | 806 GCMAccountMapper::AccountMappings stored_mappings; |
| 722 stored_mappings.push_back(MakeAccountMapping( | 807 stored_mappings.push_back(MakeAccountMapping( |
| 723 "acc_id_0", AccountMapping::ADDING, half_hour_ago, "acc_id_0_msg")); | 808 "acc_id_0", AccountMapping::ADDING, half_hour_ago, "acc_id_0_msg")); |
| 724 stored_mappings.push_back(MakeAccountMapping( | 809 stored_mappings.push_back(MakeAccountMapping( |
| 725 "acc_id_1", AccountMapping::MAPPED, half_hour_ago, "acc_id_1_msg")); | 810 "acc_id_1", AccountMapping::MAPPED, half_hour_ago, "acc_id_1_msg")); |
| 726 stored_mappings.push_back(MakeAccountMapping( | 811 stored_mappings.push_back(MakeAccountMapping( |
| 727 "acc_id_2", AccountMapping::REMOVING, half_hour_ago, "acc_id_2_msg")); | 812 "acc_id_2", AccountMapping::REMOVING, half_hour_ago, "acc_id_2_msg")); |
| 728 | 813 |
| 729 mapper()->Initialize(stored_mappings, kRegistrationId); | 814 mapper()->Initialize(stored_mappings); |
| 815 gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
| 730 | 816 |
| 731 GCMAccountMapper::AccountMappings expected_mappings(stored_mappings); | 817 GCMAccountMapper::AccountMappings expected_mappings(stored_mappings); |
| 732 | 818 |
| 733 // Finish messages after a restart. | 819 // Finish messages after a restart. |
| 734 clock()->SetNow(base::Time::Now()); | 820 clock()->SetNow(base::Time::Now()); |
| 735 gcm_driver().AcknowledgeSend(expected_mappings[0].last_message_id); | 821 gcm_driver().AcknowledgeSend(expected_mappings[0].last_message_id); |
| 736 expected_mappings[0].status_change_timestamp = clock()->Now(); | 822 expected_mappings[0].status_change_timestamp = clock()->Now(); |
| 737 expected_mappings[0].status = AccountMapping::MAPPED; | 823 expected_mappings[0].status = AccountMapping::MAPPED; |
| 738 expected_mappings[0].last_message_id.clear(); | 824 expected_mappings[0].last_message_id.clear(); |
| 739 | 825 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 811 expected_mappings[1].status_change_timestamp = clock()->Now(); | 897 expected_mappings[1].status_change_timestamp = clock()->Now(); |
| 812 expected_mappings[1].status = AccountMapping::MAPPED; | 898 expected_mappings[1].status = AccountMapping::MAPPED; |
| 813 expected_mappings[2].status_change_timestamp = clock()->Now(); | 899 expected_mappings[2].status_change_timestamp = clock()->Now(); |
| 814 expected_mappings[2].status = AccountMapping::MAPPED; | 900 expected_mappings[2].status = AccountMapping::MAPPED; |
| 815 | 901 |
| 816 VerifyMappings( | 902 VerifyMappings( |
| 817 expected_mappings, GetAccounts(), "Step 6, Three mapped accounts"); | 903 expected_mappings, GetAccounts(), "Step 6, Three mapped accounts"); |
| 818 } | 904 } |
| 819 | 905 |
| 820 } // namespace gcm | 906 } // namespace gcm |
| OLD | NEW |