fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "components/gcm_driver/gcm_account_mapper.h" |
| 6 | |
| 7 | #include "base/test/simple_test_clock.h" |
| 8 | #include "base/time/time.h" |
| 9 | #include "components/gcm_driver/fake_gcm_driver.h" |
| 10 | #include "google_apis/gcm/engine/account_mapping.h" |
| 11 | #include "google_apis/gcm/engine/gcm_store.h" |
| 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
| 14 | namespace gcm { |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | const char kGCMAccountMapperSenderId[] = "745476177629"; |
fgorski | ec85dd4 | 2015-02-13 00:15:43 | [diff] [blame^] | 19 | const char kGCMAccountMapperSendTo[] = "google.com"; |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 20 | const char kRegistrationId[] = "reg_id"; |
| 21 | |
| 22 | AccountMapping MakeAccountMapping(const std::string& account_id, |
| 23 | AccountMapping::MappingStatus status, |
| 24 | const base::Time& status_change_timestamp, |
| 25 | const std::string& last_message_id) { |
| 26 | AccountMapping account_mapping; |
| 27 | account_mapping.account_id = account_id; |
| 28 | account_mapping.email = account_id + "@gmail.com"; |
| 29 | // account_mapping.access_token intentionally left empty. |
| 30 | account_mapping.status = status; |
| 31 | account_mapping.status_change_timestamp = status_change_timestamp; |
| 32 | account_mapping.last_message_id = last_message_id; |
| 33 | return account_mapping; |
| 34 | } |
| 35 | |
| 36 | GCMClient::AccountTokenInfo MakeAccountTokenInfo( |
| 37 | const std::string& account_id) { |
| 38 | GCMClient::AccountTokenInfo account_token; |
| 39 | account_token.account_id = account_id; |
| 40 | account_token.email = account_id + "@gmail.com"; |
| 41 | account_token.access_token = account_id + "_token"; |
| 42 | return account_token; |
| 43 | } |
| 44 | |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 45 | void VerifyMappings(const GCMAccountMapper::AccountMappings& expected_mappings, |
| 46 | const GCMAccountMapper::AccountMappings& actual_mappings, |
| 47 | const std::string& verification_info) { |
| 48 | EXPECT_EQ(expected_mappings.size(), actual_mappings.size()) |
| 49 | << "Verification Info: " << verification_info; |
| 50 | GCMAccountMapper::AccountMappings::const_iterator expected_iter = |
| 51 | expected_mappings.begin(); |
| 52 | GCMAccountMapper::AccountMappings::const_iterator actual_iter = |
| 53 | actual_mappings.begin(); |
| 54 | for (; expected_iter != expected_mappings.end() && |
| 55 | actual_iter != actual_mappings.end(); |
| 56 | ++expected_iter, ++actual_iter) { |
| 57 | EXPECT_EQ(expected_iter->email, actual_iter->email) |
| 58 | << "Verification Info: " << verification_info |
| 59 | << "; Account ID of expected: " << expected_iter->account_id; |
| 60 | EXPECT_EQ(expected_iter->account_id, actual_iter->account_id) |
| 61 | << "Verification Info: " << verification_info; |
| 62 | EXPECT_EQ(expected_iter->status, actual_iter->status) |
| 63 | << "Verification Info: " << verification_info |
| 64 | << "; Account ID of expected: " << expected_iter->account_id; |
| 65 | EXPECT_EQ(expected_iter->status_change_timestamp, |
| 66 | actual_iter->status_change_timestamp) |
| 67 | << "Verification Info: " << verification_info |
| 68 | << "; Account ID of expected: " << expected_iter->account_id; |
| 69 | } |
| 70 | } |
| 71 | |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 72 | class CustomFakeGCMDriver : public FakeGCMDriver { |
| 73 | public: |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 74 | enum LastMessageAction { |
| 75 | NONE, |
| 76 | SEND_STARTED, |
| 77 | SEND_FINISHED, |
| 78 | SEND_ACKNOWLEDGED |
| 79 | }; |
| 80 | |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 81 | CustomFakeGCMDriver(); |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 82 | ~CustomFakeGCMDriver() override; |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 83 | |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 84 | void UpdateAccountMapping(const AccountMapping& account_mapping) override; |
| 85 | void RemoveAccountMapping(const std::string& account_id) override; |
| 86 | void AddAppHandler(const std::string& app_id, |
| 87 | GCMAppHandler* handler) override; |
| 88 | void RemoveAppHandler(const std::string& app_id) override; |
| 89 | void RegisterImpl(const std::string& app_id, |
| 90 | const std::vector<std::string>& sender_ids) override; |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 91 | |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 92 | void CompleteRegister(const std::string& registration_id, |
| 93 | GCMClient::Result result); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 94 | void CompleteSend(const std::string& message_id, GCMClient::Result result); |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 95 | void AcknowledgeSend(const std::string& message_id); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 96 | void MessageSendError(const std::string& message_id); |
| 97 | |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 98 | void CompleteSendAllMessages(); |
| 99 | void AcknowledgeSendAllMessages(); |
| 100 | |
| 101 | void SetLastMessageAction(const std::string& message_id, |
| 102 | LastMessageAction action); |
| 103 | void Clear(); |
| 104 | |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 105 | const AccountMapping& last_account_mapping() const { |
| 106 | return account_mapping_; |
| 107 | } |
| 108 | const std::string& last_message_id() const { return last_message_id_; } |
| 109 | const std::string& last_removed_account_id() const { |
| 110 | return last_removed_account_id_; |
| 111 | } |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 112 | LastMessageAction last_action() const { return last_action_; } |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 113 | bool registration_id_requested() const { return registration_id_requested_; } |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 114 | |
| 115 | protected: |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 116 | void SendImpl(const std::string& app_id, |
| 117 | const std::string& receiver_id, |
| 118 | const GCMClient::OutgoingMessage& message) override; |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 119 | |
| 120 | private: |
| 121 | AccountMapping account_mapping_; |
| 122 | std::string last_message_id_; |
| 123 | std::string last_removed_account_id_; |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 124 | LastMessageAction last_action_; |
| 125 | std::map<std::string, LastMessageAction> all_messages_; |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 126 | bool registration_id_requested_; |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 127 | }; |
| 128 | |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 129 | CustomFakeGCMDriver::CustomFakeGCMDriver() |
| 130 | : last_action_(NONE), registration_id_requested_(false) { |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | CustomFakeGCMDriver::~CustomFakeGCMDriver() { |
| 134 | } |
| 135 | |
| 136 | void CustomFakeGCMDriver::UpdateAccountMapping( |
| 137 | const AccountMapping& account_mapping) { |
| 138 | account_mapping_.email = account_mapping.email; |
| 139 | account_mapping_.account_id = account_mapping.account_id; |
| 140 | account_mapping_.access_token = account_mapping.access_token; |
| 141 | account_mapping_.status = account_mapping.status; |
| 142 | account_mapping_.status_change_timestamp = |
| 143 | account_mapping.status_change_timestamp; |
| 144 | account_mapping_.last_message_id = account_mapping.last_message_id; |
| 145 | } |
| 146 | |
| 147 | void CustomFakeGCMDriver::RemoveAccountMapping(const std::string& account_id) { |
| 148 | last_removed_account_id_ = account_id; |
| 149 | } |
| 150 | |
| 151 | void CustomFakeGCMDriver::AddAppHandler(const std::string& app_id, |
| 152 | GCMAppHandler* handler) { |
| 153 | GCMDriver::AddAppHandler(app_id, handler); |
| 154 | } |
| 155 | |
| 156 | void CustomFakeGCMDriver::RemoveAppHandler(const std::string& app_id) { |
| 157 | GCMDriver::RemoveAppHandler(app_id); |
| 158 | } |
| 159 | |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 160 | void CustomFakeGCMDriver::RegisterImpl( |
| 161 | const std::string& app_id, |
| 162 | const std::vector<std::string>& sender_ids) { |
| 163 | DCHECK_EQ(kGCMAccountMapperAppId, app_id); |
| 164 | DCHECK_EQ(1u, sender_ids.size()); |
| 165 | DCHECK_EQ(kGCMAccountMapperSenderId, sender_ids[0]); |
| 166 | registration_id_requested_ = true; |
| 167 | } |
| 168 | |
| 169 | void CustomFakeGCMDriver::CompleteRegister(const std::string& registration_id, |
| 170 | GCMClient::Result result) { |
| 171 | RegisterFinished(kGCMAccountMapperAppId, registration_id, result); |
| 172 | } |
| 173 | |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 174 | void CustomFakeGCMDriver::CompleteSend(const std::string& message_id, |
| 175 | GCMClient::Result result) { |
| 176 | SendFinished(kGCMAccountMapperAppId, message_id, result); |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 177 | SetLastMessageAction(message_id, SEND_FINISHED); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 178 | } |
| 179 | |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 180 | void CustomFakeGCMDriver::AcknowledgeSend(const std::string& message_id) { |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 181 | GetAppHandler(kGCMAccountMapperAppId) |
| 182 | ->OnSendAcknowledged(kGCMAccountMapperAppId, message_id); |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 183 | SetLastMessageAction(message_id, SEND_ACKNOWLEDGED); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void CustomFakeGCMDriver::MessageSendError(const std::string& message_id) { |
| 187 | GCMClient::SendErrorDetails send_error; |
| 188 | send_error.message_id = message_id; |
| 189 | send_error.result = GCMClient::TTL_EXCEEDED; |
| 190 | GetAppHandler(kGCMAccountMapperAppId) |
| 191 | ->OnSendError(kGCMAccountMapperAppId, send_error); |
| 192 | } |
| 193 | |
| 194 | void CustomFakeGCMDriver::SendImpl(const std::string& app_id, |
| 195 | const std::string& receiver_id, |
| 196 | const GCMClient::OutgoingMessage& message) { |
| 197 | DCHECK_EQ(kGCMAccountMapperAppId, app_id); |
fgorski | ec85dd4 | 2015-02-13 00:15:43 | [diff] [blame^] | 198 | DCHECK_EQ(kGCMAccountMapperSendTo, receiver_id); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 199 | |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 200 | SetLastMessageAction(message.id, SEND_STARTED); |
| 201 | } |
| 202 | |
| 203 | void CustomFakeGCMDriver::CompleteSendAllMessages() { |
| 204 | for (std::map<std::string, LastMessageAction>::const_iterator iter = |
| 205 | all_messages_.begin(); |
| 206 | iter != all_messages_.end(); |
| 207 | ++iter) { |
| 208 | if (iter->second == SEND_STARTED) |
| 209 | CompleteSend(iter->first, GCMClient::SUCCESS); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void CustomFakeGCMDriver::AcknowledgeSendAllMessages() { |
| 214 | for (std::map<std::string, LastMessageAction>::const_iterator iter = |
| 215 | all_messages_.begin(); |
| 216 | iter != all_messages_.end(); |
| 217 | ++iter) { |
| 218 | if (iter->second == SEND_FINISHED) |
| 219 | AcknowledgeSend(iter->first); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | void CustomFakeGCMDriver::Clear() { |
| 224 | account_mapping_ = AccountMapping(); |
| 225 | last_message_id_.clear(); |
| 226 | last_removed_account_id_.clear(); |
| 227 | last_action_ = NONE; |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 228 | registration_id_requested_ = false; |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void CustomFakeGCMDriver::SetLastMessageAction(const std::string& message_id, |
| 232 | LastMessageAction action) { |
| 233 | last_action_ = action; |
| 234 | last_message_id_ = message_id; |
| 235 | all_messages_[message_id] = action; |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | } // namespace |
| 239 | |
| 240 | class GCMAccountMapperTest : public testing::Test { |
| 241 | public: |
| 242 | GCMAccountMapperTest(); |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 243 | ~GCMAccountMapperTest() override; |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 244 | |
| 245 | void Restart(); |
| 246 | |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 247 | const GCMAccountMapper::AccountMappings& GetAccounts() const { |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 248 | return account_mapper_->accounts_; |
| 249 | } |
| 250 | |
| 251 | GCMAccountMapper* mapper() { return account_mapper_.get(); } |
| 252 | |
| 253 | CustomFakeGCMDriver& gcm_driver() { return gcm_driver_; } |
| 254 | |
| 255 | base::SimpleTestClock* clock() { return clock_; } |
| 256 | |
| 257 | private: |
| 258 | CustomFakeGCMDriver gcm_driver_; |
| 259 | scoped_ptr<GCMAccountMapper> account_mapper_; |
| 260 | base::SimpleTestClock* clock_; |
| 261 | }; |
| 262 | |
| 263 | GCMAccountMapperTest::GCMAccountMapperTest() { |
| 264 | Restart(); |
| 265 | } |
| 266 | |
| 267 | GCMAccountMapperTest::~GCMAccountMapperTest() { |
| 268 | } |
| 269 | |
| 270 | void GCMAccountMapperTest::Restart() { |
| 271 | if (account_mapper_) |
| 272 | account_mapper_->ShutdownHandler(); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 273 | gcm_driver_.RemoveAppHandler(kGCMAccountMapperAppId); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 274 | account_mapper_.reset(new GCMAccountMapper(&gcm_driver_)); |
| 275 | scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); |
| 276 | clock_ = clock.get(); |
dcheng | 1d86a70 | 2014-10-15 17:26:56 | [diff] [blame] | 277 | account_mapper_->SetClockForTesting(clock.Pass()); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // Tests the initialization of account mappings (from the store) when empty. |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 281 | // It also checks that initialization triggers registration ID request. |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 282 | TEST_F(GCMAccountMapperTest, InitializeAccountMappingsEmpty) { |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 283 | EXPECT_FALSE(gcm_driver().registration_id_requested()); |
| 284 | mapper()->Initialize(GCMAccountMapper::AccountMappings()); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 285 | EXPECT_TRUE(GetAccounts().empty()); |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 286 | EXPECT_TRUE(gcm_driver().registration_id_requested()); |
| 287 | } |
| 288 | |
| 289 | // Tests that registration is retried, when new tokens are delivered and in no |
| 290 | // other circumstances. |
| 291 | TEST_F(GCMAccountMapperTest, RegistrationRetryUponFailure) { |
| 292 | mapper()->Initialize(GCMAccountMapper::AccountMappings()); |
| 293 | EXPECT_TRUE(gcm_driver().registration_id_requested()); |
| 294 | gcm_driver().Clear(); |
| 295 | |
| 296 | gcm_driver().CompleteRegister(kRegistrationId, GCMClient::UNKNOWN_ERROR); |
| 297 | EXPECT_FALSE(gcm_driver().registration_id_requested()); |
| 298 | gcm_driver().Clear(); |
| 299 | |
| 300 | std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 301 | account_tokens.push_back(MakeAccountTokenInfo("acc_id2")); |
| 302 | mapper()->SetAccountTokens(account_tokens); |
| 303 | EXPECT_TRUE(gcm_driver().registration_id_requested()); |
| 304 | gcm_driver().Clear(); |
| 305 | |
| 306 | gcm_driver().CompleteRegister(kRegistrationId, |
| 307 | GCMClient::ASYNC_OPERATION_PENDING); |
| 308 | EXPECT_FALSE(gcm_driver().registration_id_requested()); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | // Tests the initialization of account mappings (from the store). |
| 312 | TEST_F(GCMAccountMapperTest, InitializeAccountMappings) { |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 313 | GCMAccountMapper::AccountMappings account_mappings; |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 314 | AccountMapping account_mapping1 = MakeAccountMapping("acc_id1", |
| 315 | AccountMapping::MAPPED, |
| 316 | base::Time::Now(), |
| 317 | std::string()); |
| 318 | AccountMapping account_mapping2 = MakeAccountMapping("acc_id2", |
| 319 | AccountMapping::ADDING, |
| 320 | base::Time::Now(), |
| 321 | "add_message_1"); |
| 322 | account_mappings.push_back(account_mapping1); |
| 323 | account_mappings.push_back(account_mapping2); |
| 324 | |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 325 | mapper()->Initialize(account_mappings); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 326 | |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 327 | GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 328 | EXPECT_EQ(2UL, mappings.size()); |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 329 | GCMAccountMapper::AccountMappings::const_iterator iter = mappings.begin(); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 330 | |
| 331 | EXPECT_EQ(account_mapping1.account_id, iter->account_id); |
| 332 | EXPECT_EQ(account_mapping1.email, iter->email); |
| 333 | EXPECT_TRUE(account_mapping1.access_token.empty()); |
| 334 | EXPECT_EQ(account_mapping1.status, iter->status); |
| 335 | EXPECT_EQ(account_mapping1.status_change_timestamp, |
| 336 | iter->status_change_timestamp); |
| 337 | EXPECT_TRUE(account_mapping1.last_message_id.empty()); |
| 338 | |
| 339 | ++iter; |
| 340 | EXPECT_EQ(account_mapping2.account_id, iter->account_id); |
| 341 | EXPECT_EQ(account_mapping2.email, iter->email); |
| 342 | EXPECT_TRUE(account_mapping2.access_token.empty()); |
| 343 | EXPECT_EQ(account_mapping2.status, iter->status); |
| 344 | EXPECT_EQ(account_mapping2.status_change_timestamp, |
| 345 | iter->status_change_timestamp); |
| 346 | EXPECT_EQ(account_mapping2.last_message_id, iter->last_message_id); |
| 347 | } |
| 348 | |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 349 | // Tests that account tokens are not processed until registration ID is |
| 350 | // available. |
| 351 | TEST_F(GCMAccountMapperTest, SetAccountTokensOnlyWorksWithRegisterationId) { |
| 352 | // Start with empty list. |
| 353 | mapper()->Initialize(GCMAccountMapper::AccountMappings()); |
| 354 | |
| 355 | std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 356 | account_tokens.push_back(MakeAccountTokenInfo("acc_id")); |
| 357 | mapper()->SetAccountTokens(account_tokens); |
| 358 | |
| 359 | EXPECT_TRUE(GetAccounts().empty()); |
| 360 | |
| 361 | account_tokens.clear(); |
| 362 | account_tokens.push_back(MakeAccountTokenInfo("acc_id1")); |
| 363 | account_tokens.push_back(MakeAccountTokenInfo("acc_id2")); |
| 364 | mapper()->SetAccountTokens(account_tokens); |
| 365 | |
| 366 | EXPECT_TRUE(GetAccounts().empty()); |
| 367 | |
| 368 | gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
| 369 | |
| 370 | GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
| 371 | EXPECT_EQ(2UL, mappings.size()); |
| 372 | EXPECT_EQ("acc_id1", mappings[0].account_id); |
| 373 | EXPECT_EQ("acc_id2", mappings[1].account_id); |
| 374 | } |
| 375 | |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 376 | // Tests the part where a new account is added with a token, to the point when |
| 377 | // GCM message is sent. |
| 378 | TEST_F(GCMAccountMapperTest, AddMappingToMessageSent) { |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 379 | mapper()->Initialize(GCMAccountMapper::AccountMappings()); |
| 380 | gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 381 | |
| 382 | std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 383 | GCMClient::AccountTokenInfo account_token = MakeAccountTokenInfo("acc_id"); |
| 384 | account_tokens.push_back(account_token); |
| 385 | mapper()->SetAccountTokens(account_tokens); |
| 386 | |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 387 | GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 388 | EXPECT_EQ(1UL, mappings.size()); |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 389 | GCMAccountMapper::AccountMappings::const_iterator iter = mappings.begin(); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 390 | EXPECT_EQ("acc_id", iter->account_id); |
| 391 | EXPECT_EQ("[email protected]", iter->email); |
| 392 | EXPECT_EQ("acc_id_token", iter->access_token); |
| 393 | EXPECT_EQ(AccountMapping::NEW, iter->status); |
| 394 | EXPECT_EQ(base::Time(), iter->status_change_timestamp); |
| 395 | |
| 396 | EXPECT_TRUE(!gcm_driver().last_message_id().empty()); |
| 397 | } |
| 398 | |
| 399 | // Tests the part where GCM message is successfully queued. |
| 400 | TEST_F(GCMAccountMapperTest, AddMappingMessageQueued) { |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 401 | mapper()->Initialize(GCMAccountMapper::AccountMappings()); |
| 402 | gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 403 | |
| 404 | std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 405 | GCMClient::AccountTokenInfo account_token = MakeAccountTokenInfo("acc_id"); |
| 406 | account_tokens.push_back(account_token); |
| 407 | mapper()->SetAccountTokens(account_tokens); |
| 408 | |
| 409 | clock()->SetNow(base::Time::Now()); |
| 410 | gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); |
| 411 | |
| 412 | EXPECT_EQ(account_token.email, gcm_driver().last_account_mapping().email); |
| 413 | EXPECT_EQ(account_token.account_id, |
| 414 | gcm_driver().last_account_mapping().account_id); |
| 415 | EXPECT_EQ(account_token.access_token, |
| 416 | gcm_driver().last_account_mapping().access_token); |
| 417 | EXPECT_EQ(AccountMapping::ADDING, gcm_driver().last_account_mapping().status); |
| 418 | EXPECT_EQ(clock()->Now(), |
| 419 | gcm_driver().last_account_mapping().status_change_timestamp); |
| 420 | EXPECT_EQ(gcm_driver().last_message_id(), |
| 421 | gcm_driver().last_account_mapping().last_message_id); |
| 422 | |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 423 | GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
| 424 | GCMAccountMapper::AccountMappings::const_iterator iter = mappings.begin(); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 425 | EXPECT_EQ(account_token.email, iter->email); |
| 426 | EXPECT_EQ(account_token.account_id, iter->account_id); |
| 427 | EXPECT_EQ(account_token.access_token, iter->access_token); |
| 428 | EXPECT_EQ(AccountMapping::ADDING, iter->status); |
| 429 | EXPECT_EQ(clock()->Now(), iter->status_change_timestamp); |
| 430 | EXPECT_EQ(gcm_driver().last_message_id(), iter->last_message_id); |
| 431 | } |
| 432 | |
| 433 | // Tests status change from ADDING to MAPPED (Message is acknowledged). |
| 434 | TEST_F(GCMAccountMapperTest, AddMappingMessageAcknowledged) { |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 435 | mapper()->Initialize(GCMAccountMapper::AccountMappings()); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 436 | gcm_driver().AddAppHandler(kGCMAccountMapperAppId, mapper()); |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 437 | gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 438 | |
| 439 | std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 440 | GCMClient::AccountTokenInfo account_token = MakeAccountTokenInfo("acc_id"); |
| 441 | account_tokens.push_back(account_token); |
| 442 | mapper()->SetAccountTokens(account_tokens); |
| 443 | |
| 444 | clock()->SetNow(base::Time::Now()); |
| 445 | gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); |
| 446 | clock()->SetNow(base::Time::Now()); |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 447 | gcm_driver().AcknowledgeSend(gcm_driver().last_message_id()); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 448 | |
| 449 | EXPECT_EQ(account_token.email, gcm_driver().last_account_mapping().email); |
| 450 | EXPECT_EQ(account_token.account_id, |
| 451 | gcm_driver().last_account_mapping().account_id); |
| 452 | EXPECT_EQ(account_token.access_token, |
| 453 | gcm_driver().last_account_mapping().access_token); |
| 454 | EXPECT_EQ(AccountMapping::MAPPED, gcm_driver().last_account_mapping().status); |
| 455 | EXPECT_EQ(clock()->Now(), |
| 456 | gcm_driver().last_account_mapping().status_change_timestamp); |
| 457 | EXPECT_TRUE(gcm_driver().last_account_mapping().last_message_id.empty()); |
| 458 | |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 459 | GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
| 460 | GCMAccountMapper::AccountMappings::const_iterator iter = mappings.begin(); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 461 | EXPECT_EQ(account_token.email, iter->email); |
| 462 | EXPECT_EQ(account_token.account_id, iter->account_id); |
| 463 | EXPECT_EQ(account_token.access_token, iter->access_token); |
| 464 | EXPECT_EQ(AccountMapping::MAPPED, iter->status); |
| 465 | EXPECT_EQ(clock()->Now(), iter->status_change_timestamp); |
| 466 | EXPECT_TRUE(iter->last_message_id.empty()); |
| 467 | } |
| 468 | |
| 469 | // Tests status change form ADDING to MAPPED (When message was acknowledged, |
| 470 | // after Chrome was restarted). |
| 471 | TEST_F(GCMAccountMapperTest, AddMappingMessageAckedAfterRestart) { |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 472 | mapper()->Initialize(GCMAccountMapper::AccountMappings()); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 473 | gcm_driver().AddAppHandler(kGCMAccountMapperAppId, mapper()); |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 474 | gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 475 | |
| 476 | std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 477 | GCMClient::AccountTokenInfo account_token = MakeAccountTokenInfo("acc_id"); |
| 478 | account_tokens.push_back(account_token); |
| 479 | mapper()->SetAccountTokens(account_tokens); |
| 480 | |
| 481 | clock()->SetNow(base::Time::Now()); |
| 482 | gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); |
| 483 | |
| 484 | Restart(); |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 485 | GCMAccountMapper::AccountMappings stored_mappings; |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 486 | stored_mappings.push_back(gcm_driver().last_account_mapping()); |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 487 | mapper()->Initialize(stored_mappings); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 488 | gcm_driver().AddAppHandler(kGCMAccountMapperAppId, mapper()); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 489 | |
| 490 | clock()->SetNow(base::Time::Now()); |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 491 | gcm_driver().AcknowledgeSend(gcm_driver().last_message_id()); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 492 | |
| 493 | EXPECT_EQ(account_token.email, gcm_driver().last_account_mapping().email); |
| 494 | EXPECT_EQ(account_token.account_id, |
| 495 | gcm_driver().last_account_mapping().account_id); |
| 496 | EXPECT_EQ(account_token.access_token, |
| 497 | gcm_driver().last_account_mapping().access_token); |
| 498 | EXPECT_EQ(AccountMapping::MAPPED, gcm_driver().last_account_mapping().status); |
| 499 | EXPECT_EQ(clock()->Now(), |
| 500 | gcm_driver().last_account_mapping().status_change_timestamp); |
| 501 | EXPECT_TRUE(gcm_driver().last_account_mapping().last_message_id.empty()); |
| 502 | |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 503 | GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
| 504 | GCMAccountMapper::AccountMappings::const_iterator iter = mappings.begin(); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 505 | EXPECT_EQ(account_token.email, iter->email); |
| 506 | EXPECT_EQ(account_token.account_id, iter->account_id); |
| 507 | EXPECT_EQ(account_token.access_token, iter->access_token); |
| 508 | EXPECT_EQ(AccountMapping::MAPPED, iter->status); |
| 509 | EXPECT_EQ(clock()->Now(), iter->status_change_timestamp); |
| 510 | EXPECT_TRUE(iter->last_message_id.empty()); |
| 511 | } |
| 512 | |
| 513 | // Tests a case when ADD message times out for a new account. |
| 514 | TEST_F(GCMAccountMapperTest, AddMappingMessageSendErrorForNewAccount) { |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 515 | mapper()->Initialize(GCMAccountMapper::AccountMappings()); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 516 | gcm_driver().AddAppHandler(kGCMAccountMapperAppId, mapper()); |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 517 | gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 518 | |
| 519 | std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 520 | GCMClient::AccountTokenInfo account_token = MakeAccountTokenInfo("acc_id"); |
| 521 | account_tokens.push_back(account_token); |
| 522 | mapper()->SetAccountTokens(account_tokens); |
| 523 | |
| 524 | clock()->SetNow(base::Time::Now()); |
| 525 | gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); |
| 526 | |
| 527 | clock()->SetNow(base::Time::Now()); |
| 528 | std::string old_message_id = gcm_driver().last_message_id(); |
| 529 | gcm_driver().MessageSendError(old_message_id); |
| 530 | |
| 531 | // No new message is sent because of the send error, as the token is stale. |
| 532 | // Because the account was new, the entry should be deleted. |
| 533 | EXPECT_EQ(old_message_id, gcm_driver().last_message_id()); |
| 534 | EXPECT_EQ(account_token.account_id, gcm_driver().last_removed_account_id()); |
| 535 | EXPECT_TRUE(GetAccounts().empty()); |
| 536 | } |
| 537 | |
| 538 | /// Tests a case when ADD message times out for a MAPPED account. |
| 539 | TEST_F(GCMAccountMapperTest, AddMappingMessageSendErrorForMappedAccount) { |
| 540 | // Start with one account that is mapped. |
| 541 | base::Time status_change_timestamp = base::Time::Now(); |
| 542 | AccountMapping mapping = MakeAccountMapping("acc_id", |
| 543 | AccountMapping::MAPPED, |
| 544 | status_change_timestamp, |
| 545 | "add_message_id"); |
| 546 | |
| 547 | GCMAccountMapper::AccountMappings stored_mappings; |
| 548 | stored_mappings.push_back(mapping); |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 549 | mapper()->Initialize(stored_mappings); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 550 | gcm_driver().AddAppHandler(kGCMAccountMapperAppId, mapper()); |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 551 | gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 552 | |
| 553 | clock()->SetNow(base::Time::Now()); |
| 554 | gcm_driver().MessageSendError("add_message_id"); |
| 555 | |
| 556 | // No new message is sent because of the send error, as the token is stale. |
| 557 | // Because the account was new, the entry should be deleted. |
| 558 | EXPECT_TRUE(gcm_driver().last_message_id().empty()); |
| 559 | |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 560 | GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
| 561 | GCMAccountMapper::AccountMappings::const_iterator iter = mappings.begin(); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 562 | EXPECT_EQ(mapping.email, iter->email); |
| 563 | EXPECT_EQ(mapping.account_id, iter->account_id); |
| 564 | EXPECT_EQ(mapping.access_token, iter->access_token); |
| 565 | EXPECT_EQ(AccountMapping::MAPPED, iter->status); |
| 566 | EXPECT_EQ(status_change_timestamp, iter->status_change_timestamp); |
| 567 | EXPECT_TRUE(iter->last_message_id.empty()); |
| 568 | } |
| 569 | |
| 570 | // Tests that a missing token for an account will trigger removing of that |
| 571 | // account. This test goes only until the message is passed to GCM. |
| 572 | TEST_F(GCMAccountMapperTest, RemoveMappingToMessageSent) { |
| 573 | // Start with one account that is mapped. |
| 574 | AccountMapping mapping = MakeAccountMapping("acc_id", |
| 575 | AccountMapping::MAPPED, |
| 576 | base::Time::Now(), |
| 577 | std::string()); |
| 578 | |
| 579 | GCMAccountMapper::AccountMappings stored_mappings; |
| 580 | stored_mappings.push_back(mapping); |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 581 | mapper()->Initialize(stored_mappings); |
| 582 | gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 583 | clock()->SetNow(base::Time::Now()); |
| 584 | |
| 585 | mapper()->SetAccountTokens(std::vector<GCMClient::AccountTokenInfo>()); |
| 586 | |
| 587 | EXPECT_EQ(mapping.account_id, gcm_driver().last_account_mapping().account_id); |
| 588 | EXPECT_EQ(mapping.email, gcm_driver().last_account_mapping().email); |
| 589 | EXPECT_EQ(AccountMapping::REMOVING, |
| 590 | gcm_driver().last_account_mapping().status); |
| 591 | EXPECT_EQ(clock()->Now(), |
| 592 | gcm_driver().last_account_mapping().status_change_timestamp); |
| 593 | EXPECT_TRUE(gcm_driver().last_account_mapping().last_message_id.empty()); |
| 594 | |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 595 | GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
| 596 | GCMAccountMapper::AccountMappings::const_iterator iter = mappings.begin(); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 597 | EXPECT_EQ(mapping.email, iter->email); |
| 598 | EXPECT_EQ(mapping.account_id, iter->account_id); |
| 599 | EXPECT_EQ(mapping.access_token, iter->access_token); |
| 600 | EXPECT_EQ(AccountMapping::REMOVING, iter->status); |
| 601 | EXPECT_EQ(clock()->Now(), iter->status_change_timestamp); |
| 602 | EXPECT_TRUE(iter->last_message_id.empty()); |
| 603 | } |
| 604 | |
| 605 | // Tests that a missing token for an account will trigger removing of that |
| 606 | // account. This test goes until the message is queued by GCM. |
| 607 | TEST_F(GCMAccountMapperTest, RemoveMappingMessageQueued) { |
| 608 | // Start with one account that is mapped. |
| 609 | AccountMapping mapping = MakeAccountMapping("acc_id", |
| 610 | AccountMapping::MAPPED, |
| 611 | base::Time::Now(), |
| 612 | std::string()); |
| 613 | |
| 614 | GCMAccountMapper::AccountMappings stored_mappings; |
| 615 | stored_mappings.push_back(mapping); |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 616 | mapper()->Initialize(stored_mappings); |
| 617 | gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 618 | clock()->SetNow(base::Time::Now()); |
| 619 | base::Time status_change_timestamp = clock()->Now(); |
| 620 | |
| 621 | mapper()->SetAccountTokens(std::vector<GCMClient::AccountTokenInfo>()); |
| 622 | clock()->SetNow(base::Time::Now()); |
| 623 | gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); |
| 624 | |
| 625 | EXPECT_EQ(mapping.account_id, gcm_driver().last_account_mapping().account_id); |
| 626 | EXPECT_EQ(mapping.email, gcm_driver().last_account_mapping().email); |
| 627 | EXPECT_EQ(AccountMapping::REMOVING, |
| 628 | gcm_driver().last_account_mapping().status); |
| 629 | EXPECT_EQ(status_change_timestamp, |
| 630 | gcm_driver().last_account_mapping().status_change_timestamp); |
| 631 | EXPECT_TRUE(!gcm_driver().last_account_mapping().last_message_id.empty()); |
| 632 | |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 633 | GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
| 634 | GCMAccountMapper::AccountMappings::const_iterator iter = mappings.begin(); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 635 | EXPECT_EQ(mapping.email, iter->email); |
| 636 | EXPECT_EQ(mapping.account_id, iter->account_id); |
| 637 | EXPECT_EQ(mapping.access_token, iter->access_token); |
| 638 | EXPECT_EQ(AccountMapping::REMOVING, iter->status); |
| 639 | EXPECT_EQ(status_change_timestamp, iter->status_change_timestamp); |
| 640 | EXPECT_EQ(gcm_driver().last_account_mapping().last_message_id, |
| 641 | iter->last_message_id); |
| 642 | } |
| 643 | |
| 644 | // Tests that a missing token for an account will trigger removing of that |
| 645 | // account. This test goes until the message is acknowledged by GCM. |
| 646 | // This is a complete success scenario for account removal, and it end with |
| 647 | // account mapping being completely gone. |
| 648 | TEST_F(GCMAccountMapperTest, RemoveMappingMessageAcknowledged) { |
| 649 | // Start with one account that is mapped. |
| 650 | AccountMapping mapping = MakeAccountMapping("acc_id", |
| 651 | AccountMapping::MAPPED, |
| 652 | base::Time::Now(), |
| 653 | std::string()); |
| 654 | |
| 655 | GCMAccountMapper::AccountMappings stored_mappings; |
| 656 | stored_mappings.push_back(mapping); |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 657 | mapper()->Initialize(stored_mappings); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 658 | gcm_driver().AddAppHandler(kGCMAccountMapperAppId, mapper()); |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 659 | gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 660 | clock()->SetNow(base::Time::Now()); |
| 661 | |
| 662 | mapper()->SetAccountTokens(std::vector<GCMClient::AccountTokenInfo>()); |
| 663 | gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 664 | gcm_driver().AcknowledgeSend(gcm_driver().last_message_id()); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 665 | |
| 666 | EXPECT_EQ(mapping.account_id, gcm_driver().last_removed_account_id()); |
| 667 | |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 668 | GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 669 | EXPECT_TRUE(mappings.empty()); |
| 670 | } |
| 671 | |
| 672 | // Tests that account removing proceeds, when a removing message is acked after |
| 673 | // Chrome was restarted. |
| 674 | TEST_F(GCMAccountMapperTest, RemoveMappingMessageAckedAfterRestart) { |
| 675 | // Start with one account that is mapped. |
| 676 | AccountMapping mapping = MakeAccountMapping("acc_id", |
| 677 | AccountMapping::REMOVING, |
| 678 | base::Time::Now(), |
| 679 | "remove_message_id"); |
| 680 | |
| 681 | GCMAccountMapper::AccountMappings stored_mappings; |
| 682 | stored_mappings.push_back(mapping); |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 683 | mapper()->Initialize(stored_mappings); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 684 | gcm_driver().AddAppHandler(kGCMAccountMapperAppId, mapper()); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 685 | |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 686 | gcm_driver().AcknowledgeSend("remove_message_id"); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 687 | |
| 688 | EXPECT_EQ(mapping.account_id, gcm_driver().last_removed_account_id()); |
| 689 | |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 690 | GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 691 | EXPECT_TRUE(mappings.empty()); |
| 692 | } |
| 693 | |
| 694 | // Tests that account removing proceeds, when a removing message is acked after |
| 695 | // Chrome was restarted. |
| 696 | TEST_F(GCMAccountMapperTest, RemoveMappingMessageSendError) { |
| 697 | // Start with one account that is mapped. |
| 698 | base::Time status_change_timestamp = base::Time::Now(); |
| 699 | AccountMapping mapping = MakeAccountMapping("acc_id", |
| 700 | AccountMapping::REMOVING, |
| 701 | status_change_timestamp, |
| 702 | "remove_message_id"); |
| 703 | |
| 704 | GCMAccountMapper::AccountMappings stored_mappings; |
| 705 | stored_mappings.push_back(mapping); |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 706 | mapper()->Initialize(stored_mappings); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 707 | gcm_driver().AddAppHandler(kGCMAccountMapperAppId, mapper()); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 708 | |
| 709 | clock()->SetNow(base::Time::Now()); |
| 710 | gcm_driver().MessageSendError("remove_message_id"); |
| 711 | |
| 712 | EXPECT_TRUE(gcm_driver().last_removed_account_id().empty()); |
| 713 | |
| 714 | EXPECT_EQ(mapping.account_id, gcm_driver().last_account_mapping().account_id); |
| 715 | EXPECT_EQ(mapping.email, gcm_driver().last_account_mapping().email); |
| 716 | EXPECT_EQ(AccountMapping::REMOVING, |
| 717 | gcm_driver().last_account_mapping().status); |
| 718 | EXPECT_EQ(status_change_timestamp, |
| 719 | gcm_driver().last_account_mapping().status_change_timestamp); |
| 720 | // Message is not persisted, until send is completed. |
| 721 | EXPECT_TRUE(gcm_driver().last_account_mapping().last_message_id.empty()); |
| 722 | |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 723 | GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
| 724 | GCMAccountMapper::AccountMappings::const_iterator iter = mappings.begin(); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 725 | EXPECT_EQ(mapping.email, iter->email); |
| 726 | EXPECT_EQ(mapping.account_id, iter->account_id); |
| 727 | EXPECT_TRUE(iter->access_token.empty()); |
| 728 | EXPECT_EQ(AccountMapping::REMOVING, iter->status); |
| 729 | EXPECT_EQ(status_change_timestamp, iter->status_change_timestamp); |
| 730 | EXPECT_TRUE(iter->last_message_id.empty()); |
| 731 | } |
| 732 | |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 733 | // Tests that, if a new token arrives when the adding message is in progress |
| 734 | // no new message is sent and account mapper still waits for the first one to |
| 735 | // complete. |
| 736 | TEST_F(GCMAccountMapperTest, TokenIsRefreshedWhenAdding) { |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 737 | mapper()->Initialize(GCMAccountMapper::AccountMappings()); |
| 738 | gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 739 | |
| 740 | clock()->SetNow(base::Time::Now()); |
| 741 | std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 742 | GCMClient::AccountTokenInfo account_token = MakeAccountTokenInfo("acc_id"); |
| 743 | account_tokens.push_back(account_token); |
| 744 | mapper()->SetAccountTokens(account_tokens); |
| 745 | DCHECK_EQ(CustomFakeGCMDriver::SEND_STARTED, gcm_driver().last_action()); |
| 746 | |
| 747 | clock()->SetNow(base::Time::Now()); |
| 748 | gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); |
| 749 | DCHECK_EQ(CustomFakeGCMDriver::SEND_FINISHED, gcm_driver().last_action()); |
| 750 | |
| 751 | // Providing another token and clearing status. |
| 752 | gcm_driver().Clear(); |
| 753 | mapper()->SetAccountTokens(account_tokens); |
| 754 | DCHECK_EQ(CustomFakeGCMDriver::NONE, gcm_driver().last_action()); |
| 755 | } |
| 756 | |
| 757 | // Tests that, if a new token arrives when a removing message is in progress |
| 758 | // a new adding message is sent and while account mapping status is changed to |
| 759 | // mapped. If the original Removing message arrives it is discarded. |
| 760 | TEST_F(GCMAccountMapperTest, TokenIsRefreshedWhenRemoving) { |
| 761 | // Start with one account that is mapped. |
| 762 | AccountMapping mapping = MakeAccountMapping( |
| 763 | "acc_id", AccountMapping::MAPPED, base::Time::Now(), std::string()); |
| 764 | |
| 765 | GCMAccountMapper::AccountMappings stored_mappings; |
| 766 | stored_mappings.push_back(mapping); |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 767 | mapper()->Initialize(stored_mappings); |
| 768 | gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 769 | clock()->SetNow(base::Time::Now()); |
| 770 | |
| 771 | // Remove the token to trigger a remove message to be sent |
| 772 | mapper()->SetAccountTokens(std::vector<GCMClient::AccountTokenInfo>()); |
| 773 | EXPECT_EQ(CustomFakeGCMDriver::SEND_STARTED, gcm_driver().last_action()); |
| 774 | gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); |
| 775 | EXPECT_EQ(CustomFakeGCMDriver::SEND_FINISHED, gcm_driver().last_action()); |
| 776 | |
| 777 | std::string remove_message_id = gcm_driver().last_message_id(); |
| 778 | gcm_driver().Clear(); |
| 779 | |
| 780 | // The account mapping for acc_id is now in status REMOVING. |
| 781 | // Adding the token for that account. |
| 782 | clock()->SetNow(base::Time::Now()); |
| 783 | std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 784 | GCMClient::AccountTokenInfo account_token = MakeAccountTokenInfo("acc_id"); |
| 785 | account_tokens.push_back(account_token); |
| 786 | mapper()->SetAccountTokens(account_tokens); |
| 787 | DCHECK_EQ(CustomFakeGCMDriver::SEND_STARTED, gcm_driver().last_action()); |
| 788 | gcm_driver().CompleteSend(gcm_driver().last_message_id(), GCMClient::SUCCESS); |
| 789 | EXPECT_EQ(CustomFakeGCMDriver::SEND_FINISHED, gcm_driver().last_action()); |
| 790 | |
| 791 | std::string add_message_id = gcm_driver().last_message_id(); |
| 792 | |
| 793 | // A remove message confirmation arrives now, but should be ignored. |
| 794 | gcm_driver().AcknowledgeSend(remove_message_id); |
| 795 | |
| 796 | GCMAccountMapper::AccountMappings mappings = GetAccounts(); |
| 797 | GCMAccountMapper::AccountMappings::const_iterator iter = mappings.begin(); |
| 798 | EXPECT_EQ(mapping.email, iter->email); |
| 799 | EXPECT_EQ(mapping.account_id, iter->account_id); |
| 800 | EXPECT_FALSE(iter->access_token.empty()); |
| 801 | EXPECT_EQ(AccountMapping::MAPPED, iter->status); |
| 802 | // Status change timestamp is set to very long time ago, to make sure the next |
| 803 | // round of mapping picks it up. |
| 804 | EXPECT_EQ(base::Time(), iter->status_change_timestamp); |
| 805 | EXPECT_EQ(add_message_id, iter->last_message_id); |
| 806 | } |
| 807 | |
| 808 | // Tests adding/removing works for multiple accounts, after a restart and when |
| 809 | // tokens are periodically delierverd. |
| 810 | TEST_F(GCMAccountMapperTest, MultipleAccountMappings) { |
| 811 | clock()->SetNow(base::Time::Now()); |
| 812 | base::Time half_hour_ago = clock()->Now() - base::TimeDelta::FromMinutes(30); |
| 813 | GCMAccountMapper::AccountMappings stored_mappings; |
| 814 | stored_mappings.push_back(MakeAccountMapping( |
| 815 | "acc_id_0", AccountMapping::ADDING, half_hour_ago, "acc_id_0_msg")); |
| 816 | stored_mappings.push_back(MakeAccountMapping( |
| 817 | "acc_id_1", AccountMapping::MAPPED, half_hour_ago, "acc_id_1_msg")); |
| 818 | stored_mappings.push_back(MakeAccountMapping( |
| 819 | "acc_id_2", AccountMapping::REMOVING, half_hour_ago, "acc_id_2_msg")); |
| 820 | |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 821 | mapper()->Initialize(stored_mappings); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 822 | gcm_driver().AddAppHandler(kGCMAccountMapperAppId, mapper()); |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 823 | gcm_driver().CompleteRegister(kRegistrationId, GCMClient::SUCCESS); |
fgorski | 93f4864 | 2014-09-10 23:32:53 | [diff] [blame] | 824 | |
| 825 | GCMAccountMapper::AccountMappings expected_mappings(stored_mappings); |
| 826 | |
| 827 | // Finish messages after a restart. |
| 828 | clock()->SetNow(base::Time::Now()); |
| 829 | gcm_driver().AcknowledgeSend(expected_mappings[0].last_message_id); |
| 830 | expected_mappings[0].status_change_timestamp = clock()->Now(); |
| 831 | expected_mappings[0].status = AccountMapping::MAPPED; |
| 832 | expected_mappings[0].last_message_id.clear(); |
| 833 | |
| 834 | clock()->SetNow(base::Time::Now()); |
| 835 | gcm_driver().AcknowledgeSend(expected_mappings[1].last_message_id); |
| 836 | expected_mappings[1].status_change_timestamp = clock()->Now(); |
| 837 | expected_mappings[1].status = AccountMapping::MAPPED; |
| 838 | expected_mappings[1].last_message_id.clear(); |
| 839 | |
| 840 | // Upon success last element is removed. |
| 841 | clock()->SetNow(base::Time::Now()); |
| 842 | gcm_driver().AcknowledgeSend(expected_mappings[2].last_message_id); |
| 843 | expected_mappings.pop_back(); |
| 844 | |
| 845 | VerifyMappings(expected_mappings, GetAccounts(), "Step 1, After restart"); |
| 846 | |
| 847 | // One of accounts gets removed. |
| 848 | std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 849 | account_tokens.push_back(MakeAccountTokenInfo("acc_id_0")); |
| 850 | |
| 851 | // Advance a day to make sure existing mappings will be reported. |
| 852 | clock()->SetNow(clock()->Now() + base::TimeDelta::FromDays(1)); |
| 853 | mapper()->SetAccountTokens(account_tokens); |
| 854 | |
| 855 | expected_mappings[0].status = AccountMapping::MAPPED; |
| 856 | expected_mappings[1].status = AccountMapping::REMOVING; |
| 857 | expected_mappings[1].status_change_timestamp = clock()->Now(); |
| 858 | |
| 859 | gcm_driver().CompleteSendAllMessages(); |
| 860 | |
| 861 | VerifyMappings( |
| 862 | expected_mappings, GetAccounts(), "Step 2, One account is being removed"); |
| 863 | |
| 864 | clock()->SetNow(clock()->Now() + base::TimeDelta::FromSeconds(5)); |
| 865 | gcm_driver().AcknowledgeSendAllMessages(); |
| 866 | |
| 867 | expected_mappings[0].status_change_timestamp = clock()->Now(); |
| 868 | expected_mappings.pop_back(); |
| 869 | |
| 870 | VerifyMappings( |
| 871 | expected_mappings, GetAccounts(), "Step 3, Removing completed"); |
| 872 | |
| 873 | account_tokens.clear(); |
| 874 | account_tokens.push_back(MakeAccountTokenInfo("acc_id_0")); |
| 875 | account_tokens.push_back(MakeAccountTokenInfo("acc_id_3")); |
| 876 | account_tokens.push_back(MakeAccountTokenInfo("acc_id_4")); |
| 877 | |
| 878 | // Advance a day to make sure existing mappings will be reported. |
| 879 | clock()->SetNow(clock()->Now() + base::TimeDelta::FromDays(1)); |
| 880 | mapper()->SetAccountTokens(account_tokens); |
| 881 | |
| 882 | // Mapping from acc_id_0 still in position 0 |
| 883 | expected_mappings.push_back(MakeAccountMapping( |
| 884 | "acc_id_3", AccountMapping::NEW, base::Time(), std::string())); |
| 885 | expected_mappings.push_back(MakeAccountMapping( |
| 886 | "acc_id_4", AccountMapping::NEW, base::Time(), std::string())); |
| 887 | |
| 888 | VerifyMappings(expected_mappings, GetAccounts(), "Step 4, Two new accounts"); |
| 889 | |
| 890 | clock()->SetNow(clock()->Now() + base::TimeDelta::FromSeconds(1)); |
| 891 | gcm_driver().CompleteSendAllMessages(); |
| 892 | |
| 893 | expected_mappings[1].status = AccountMapping::ADDING; |
| 894 | expected_mappings[1].status_change_timestamp = clock()->Now(); |
| 895 | expected_mappings[2].status = AccountMapping::ADDING; |
| 896 | expected_mappings[2].status_change_timestamp = clock()->Now(); |
| 897 | |
| 898 | VerifyMappings( |
| 899 | expected_mappings, GetAccounts(), "Step 5, Two accounts being added"); |
| 900 | |
| 901 | clock()->SetNow(clock()->Now() + base::TimeDelta::FromSeconds(5)); |
| 902 | gcm_driver().AcknowledgeSendAllMessages(); |
| 903 | |
| 904 | expected_mappings[0].status_change_timestamp = clock()->Now(); |
| 905 | expected_mappings[1].status_change_timestamp = clock()->Now(); |
| 906 | expected_mappings[1].status = AccountMapping::MAPPED; |
| 907 | expected_mappings[2].status_change_timestamp = clock()->Now(); |
| 908 | expected_mappings[2].status = AccountMapping::MAPPED; |
| 909 | |
| 910 | VerifyMappings( |
| 911 | expected_mappings, GetAccounts(), "Step 6, Three mapped accounts"); |
| 912 | } |
| 913 | |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 914 | } // namespace gcm |