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