[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [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 | |
jitendra.ks | 70c5b4c | 2015-08-19 18:53:34 | [diff] [blame] | 5 | #include "components/gcm_driver/gcm_account_tracker.h" |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 6 | |
| 7 | #include <map> |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 8 | #include <memory> |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 9 | #include <string> |
dcheng | 5160635 | 2015-12-26 21:16:23 | [diff] [blame] | 10 | #include <utility> |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 11 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 12 | #include "base/macros.h" |
fdoray | e1e050c5 | 2016-07-19 21:05:54 | [diff] [blame] | 13 | #include "base/message_loop/message_loop.h" |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 14 | #include "components/gcm_driver/fake_gcm_driver.h" |
Colin Blundell | bf6d4c5f | 2018-05-16 11:12:29 | [diff] [blame] | 15 | #include "components/signin/core/browser/account_tracker_service.h" |
| 16 | #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
| 17 | #include "components/signin/core/browser/fake_signin_manager.h" |
| 18 | #include "components/signin/core/browser/test_signin_client.h" |
| 19 | #include "components/sync_preferences/testing_pref_service_syncable.h" |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 20 | #include "google_apis/gaia/fake_oauth2_token_service.h" |
| 21 | #include "google_apis/gaia/google_service_auth_error.h" |
| 22 | #include "net/http/http_status_code.h" |
| 23 | #include "net/url_request/test_url_fetcher_factory.h" |
| 24 | #include "net/url_request/url_request_test_util.h" |
| 25 | #include "testing/gtest/include/gtest/gtest.h" |
| 26 | |
| 27 | namespace gcm { |
| 28 | |
| 29 | namespace { |
| 30 | |
Colin Blundell | bf6d4c5f | 2018-05-16 11:12:29 | [diff] [blame] | 31 | #if defined(OS_CHROMEOS) |
| 32 | using SigninManagerForTest = FakeSigninManagerBase; |
| 33 | #else |
| 34 | using SigninManagerForTest = FakeSigninManager; |
| 35 | #endif // OS_CHROMEOS |
| 36 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 37 | const char kGaiaId1[] = "account_1"; |
| 38 | const char kEmail1[] = "[email protected]"; |
| 39 | const char kGaiaId2[] = "account_2"; |
| 40 | const char kEmail2[] = "[email protected]"; |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 41 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 42 | std::string AccountIdToObfuscatedId(const std::string& account_id) { |
| 43 | return "obfid-" + account_id; |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 44 | } |
| 45 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 46 | std::string GetValidTokenInfoResponse(const std::string& account_id) { |
| 47 | return std::string("{ \"id\": \"") + AccountIdToObfuscatedId(account_id) + |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 48 | "\" }"; |
| 49 | } |
| 50 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 51 | std::string MakeAccessToken(const std::string& account_id) { |
| 52 | return "access_token-" + account_id; |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 53 | } |
| 54 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 55 | GCMClient::AccountTokenInfo MakeAccountToken(const std::string& account_id) { |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 56 | GCMClient::AccountTokenInfo token_info; |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 57 | token_info.account_id = account_id; |
| 58 | |
| 59 | // TODO(https://crbug.com/856170): This *should* be expected to be the email |
| 60 | // address for the given account, but there is a bug in AccountTracker that |
| 61 | // means that |token_info.email| actually gets populated with the account ID |
| 62 | // by the production code. Hence the test expectation has to match what the |
| 63 | // production code actually does :). If/when that bug gets fixed, this |
| 64 | // function should be changed to take in the email address as well as the |
| 65 | // account ID and populate this field with the email address. |
| 66 | token_info.email = account_id; |
| 67 | token_info.access_token = MakeAccessToken(account_id); |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 68 | return token_info; |
| 69 | } |
| 70 | |
| 71 | void VerifyAccountTokens( |
| 72 | const std::vector<GCMClient::AccountTokenInfo>& expected_tokens, |
| 73 | const std::vector<GCMClient::AccountTokenInfo>& actual_tokens) { |
| 74 | EXPECT_EQ(expected_tokens.size(), actual_tokens.size()); |
| 75 | for (std::vector<GCMClient::AccountTokenInfo>::const_iterator |
| 76 | expected_iter = expected_tokens.begin(), |
| 77 | actual_iter = actual_tokens.begin(); |
| 78 | expected_iter != expected_tokens.end() && |
| 79 | actual_iter != actual_tokens.end(); |
| 80 | ++expected_iter, ++actual_iter) { |
| 81 | EXPECT_EQ(expected_iter->account_id, actual_iter->account_id); |
| 82 | EXPECT_EQ(expected_iter->email, actual_iter->email); |
| 83 | EXPECT_EQ(expected_iter->access_token, actual_iter->access_token); |
| 84 | } |
| 85 | } |
| 86 | |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 87 | // This version of FakeGCMDriver is customized around handling accounts and |
| 88 | // connection events for testing GCMAccountTracker. |
| 89 | class CustomFakeGCMDriver : public FakeGCMDriver { |
| 90 | public: |
| 91 | CustomFakeGCMDriver(); |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 92 | ~CustomFakeGCMDriver() override; |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 93 | |
| 94 | // GCMDriver overrides: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 95 | void SetAccountTokens( |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 96 | const std::vector<GCMClient::AccountTokenInfo>& account_tokens) override; |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 97 | void AddConnectionObserver(GCMConnectionObserver* observer) override; |
| 98 | void RemoveConnectionObserver(GCMConnectionObserver* observer) override; |
| 99 | bool IsConnected() const override { return connected_; } |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 100 | base::Time GetLastTokenFetchTime() override; |
| 101 | void SetLastTokenFetchTime(const base::Time& time) override; |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 102 | |
| 103 | // Test results and helpers. |
| 104 | void SetConnected(bool connected); |
| 105 | void ResetResults(); |
| 106 | bool update_accounts_called() const { return update_accounts_called_; } |
| 107 | const std::vector<GCMClient::AccountTokenInfo>& accounts() const { |
| 108 | return accounts_; |
| 109 | } |
| 110 | const GCMConnectionObserver* last_connection_observer() const { |
| 111 | return last_connection_observer_; |
| 112 | } |
| 113 | const GCMConnectionObserver* last_removed_connection_observer() const { |
| 114 | return removed_connection_observer_; |
| 115 | } |
| 116 | |
| 117 | private: |
| 118 | bool connected_; |
| 119 | std::vector<GCMClient::AccountTokenInfo> accounts_; |
| 120 | bool update_accounts_called_; |
| 121 | GCMConnectionObserver* last_connection_observer_; |
| 122 | GCMConnectionObserver* removed_connection_observer_; |
| 123 | net::IPEndPoint ip_endpoint_; |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 124 | base::Time last_token_fetch_time_; |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 125 | |
| 126 | DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver); |
| 127 | }; |
| 128 | |
| 129 | CustomFakeGCMDriver::CustomFakeGCMDriver() |
| 130 | : connected_(true), |
| 131 | update_accounts_called_(false), |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 132 | last_connection_observer_(nullptr), |
| 133 | removed_connection_observer_(nullptr) {} |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 134 | |
| 135 | CustomFakeGCMDriver::~CustomFakeGCMDriver() { |
| 136 | } |
| 137 | |
| 138 | void CustomFakeGCMDriver::SetAccountTokens( |
| 139 | const std::vector<GCMClient::AccountTokenInfo>& accounts) { |
| 140 | update_accounts_called_ = true; |
| 141 | accounts_ = accounts; |
| 142 | } |
| 143 | |
| 144 | void CustomFakeGCMDriver::AddConnectionObserver( |
| 145 | GCMConnectionObserver* observer) { |
| 146 | last_connection_observer_ = observer; |
| 147 | } |
| 148 | |
| 149 | void CustomFakeGCMDriver::RemoveConnectionObserver( |
| 150 | GCMConnectionObserver* observer) { |
| 151 | removed_connection_observer_ = observer; |
| 152 | } |
| 153 | |
| 154 | void CustomFakeGCMDriver::SetConnected(bool connected) { |
| 155 | connected_ = connected; |
| 156 | if (connected && last_connection_observer_) |
| 157 | last_connection_observer_->OnConnected(ip_endpoint_); |
| 158 | } |
| 159 | |
| 160 | void CustomFakeGCMDriver::ResetResults() { |
| 161 | accounts_.clear(); |
| 162 | update_accounts_called_ = false; |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 163 | last_connection_observer_ = nullptr; |
| 164 | removed_connection_observer_ = nullptr; |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 165 | } |
| 166 | |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 167 | |
| 168 | base::Time CustomFakeGCMDriver::GetLastTokenFetchTime() { |
| 169 | return last_token_fetch_time_; |
| 170 | } |
| 171 | |
| 172 | void CustomFakeGCMDriver::SetLastTokenFetchTime(const base::Time& time) { |
| 173 | last_token_fetch_time_ = time; |
| 174 | } |
| 175 | |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 176 | } // namespace |
| 177 | |
| 178 | class GCMAccountTrackerTest : public testing::Test { |
| 179 | public: |
| 180 | GCMAccountTrackerTest(); |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 181 | ~GCMAccountTrackerTest() override; |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 182 | |
Colin Blundell | 0472056 | 2018-05-14 09:19:40 | [diff] [blame] | 183 | // Helpers to pass fake info to the tracker. Tests should have either a pair |
Colin Blundell | bf6d4c5f | 2018-05-16 11:12:29 | [diff] [blame] | 184 | // of Start(Primary)/FinishAccountAddition or Add(Primary)Account per |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 185 | // account. Don't mix. Any methods that return an std::string are returning |
| 186 | // the account ID of the newly-added account, which can then be passed into |
| 187 | // any methods that take in an account ID. |
Colin Blundell | 0472056 | 2018-05-14 09:19:40 | [diff] [blame] | 188 | // Call to RemoveAccount is not mandatory. |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 189 | std::string StartAccountAddition(const std::string& gaia_id, |
| 190 | const std::string& email); |
| 191 | std::string StartPrimaryAccountAddition(const std::string& gaia_id, |
| 192 | const std::string& email); |
| 193 | void FinishAccountAddition(const std::string& account_id); |
| 194 | std::string AddAccount(const std::string& gaia_id, const std::string& email); |
| 195 | std::string AddPrimaryAccount(const std::string& gaia_id, |
| 196 | const std::string& email); |
| 197 | void RemoveAccount(const std::string& account_id); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 198 | |
| 199 | // Helpers for dealing with OAuth2 access token requests. |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 200 | void IssueAccessToken(const std::string& account_id); |
| 201 | void IssueExpiredAccessToken(const std::string& account_id); |
| 202 | void IssueError(const std::string& account_id); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 203 | |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 204 | // Accessors to account tracker and gcm driver. |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 205 | GCMAccountTracker* tracker() { return tracker_.get(); } |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 206 | CustomFakeGCMDriver* driver() { return &driver_; } |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 207 | |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 208 | // Accessors to private methods of account tracker. |
| 209 | bool IsFetchingRequired() const; |
| 210 | bool IsTokenReportingRequired() const; |
| 211 | base::TimeDelta GetTimeToNextTokenReporting() const; |
| 212 | |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 213 | private: |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 214 | CustomFakeGCMDriver driver_; |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 215 | |
| 216 | base::MessageLoop message_loop_; |
| 217 | net::TestURLFetcherFactory test_fetcher_factory_; |
Colin Blundell | bf6d4c5f | 2018-05-16 11:12:29 | [diff] [blame] | 218 | sync_preferences::TestingPrefServiceSyncable pref_service_; |
| 219 | AccountTrackerService account_tracker_service_; |
| 220 | std::unique_ptr<TestSigninClient> test_signin_client_; |
| 221 | std::unique_ptr<SigninManagerForTest> fake_signin_manager_; |
| 222 | std::unique_ptr<FakeProfileOAuth2TokenService> fake_token_service_; |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 223 | std::unique_ptr<GCMAccountTracker> tracker_; |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 224 | }; |
| 225 | |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 226 | GCMAccountTrackerTest::GCMAccountTrackerTest() { |
Colin Blundell | bf6d4c5f | 2018-05-16 11:12:29 | [diff] [blame] | 227 | fake_token_service_.reset(new FakeProfileOAuth2TokenService()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 228 | |
Colin Blundell | bf6d4c5f | 2018-05-16 11:12:29 | [diff] [blame] | 229 | test_signin_client_.reset(new TestSigninClient(&pref_service_)); |
| 230 | #if defined(OS_CHROMEOS) |
| 231 | fake_signin_manager_.reset(new SigninManagerForTest( |
| 232 | test_signin_client_.get(), &account_tracker_service_)); |
| 233 | #else |
| 234 | fake_signin_manager_.reset(new SigninManagerForTest( |
| 235 | test_signin_client_.get(), fake_token_service_.get(), |
| 236 | &account_tracker_service_, nullptr)); |
| 237 | #endif |
| 238 | |
| 239 | AccountTrackerService::RegisterPrefs(pref_service_.registry()); |
| 240 | SigninManagerBase::RegisterProfilePrefs(pref_service_.registry()); |
| 241 | SigninManagerBase::RegisterPrefs(pref_service_.registry()); |
| 242 | account_tracker_service_.Initialize(test_signin_client_.get()); |
| 243 | |
Colin Blundell | 021e3e4d | 2018-04-19 01:03:24 | [diff] [blame] | 244 | std::unique_ptr<AccountTracker> gaia_account_tracker(new AccountTracker( |
Colin Blundell | f878993 | 2018-05-22 11:35:56 | [diff] [blame] | 245 | fake_signin_manager_.get(), fake_token_service_.get(), |
Colin Blundell | 021e3e4d | 2018-04-19 01:03:24 | [diff] [blame] | 246 | new net::TestURLRequestContextGetter(message_loop_.task_runner()))); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 247 | |
Colin Blundell | f878993 | 2018-05-22 11:35:56 | [diff] [blame] | 248 | tracker_.reset(new GCMAccountTracker(std::move(gaia_account_tracker), |
| 249 | fake_token_service_.get(), &driver_)); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | GCMAccountTrackerTest::~GCMAccountTrackerTest() { |
| 253 | if (tracker_) |
| 254 | tracker_->Shutdown(); |
| 255 | } |
| 256 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 257 | std::string GCMAccountTrackerTest::StartAccountAddition( |
| 258 | const std::string& gaia_id, |
| 259 | const std::string& email) { |
| 260 | std::string account_id = |
| 261 | account_tracker_service_.SeedAccountInfo(gaia_id, email); |
| 262 | fake_token_service_->UpdateCredentials(account_id, "fake_refresh_token"); |
| 263 | return account_id; |
Colin Blundell | bf6d4c5f | 2018-05-16 11:12:29 | [diff] [blame] | 264 | } |
| 265 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 266 | std::string GCMAccountTrackerTest::StartPrimaryAccountAddition( |
| 267 | const std::string& gaia_id, |
| 268 | const std::string& email) { |
Colin Blundell | bf6d4c5f | 2018-05-16 11:12:29 | [diff] [blame] | 269 | // NOTE: Setting of the primary account info must be done first on ChromeOS |
| 270 | // to ensure that AccountTracker and GCMAccountTracker respond as expected |
| 271 | // when the token is added to the token service. |
| 272 | // TODO(blundell): On non-ChromeOS, it would be good to add tests wherein |
| 273 | // setting of the primary account is done afterward to check that the flow |
| 274 | // that ensues from the GoogleSigninSucceeded callback firing works as |
| 275 | // expected. |
| 276 | #if defined(OS_CHROMEOS) |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 277 | fake_signin_manager_->SignIn(email); |
Colin Blundell | bf6d4c5f | 2018-05-16 11:12:29 | [diff] [blame] | 278 | #else |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 279 | fake_signin_manager_->SignIn(gaia_id, email, "" /* password */); |
Colin Blundell | bf6d4c5f | 2018-05-16 11:12:29 | [diff] [blame] | 280 | #endif |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 281 | |
| 282 | std::string account_id = fake_signin_manager_->GetAuthenticatedAccountId(); |
| 283 | fake_token_service_->UpdateCredentials(account_id, "fake_refresh_token"); |
| 284 | |
| 285 | return account_id; |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 286 | } |
| 287 | |
Colin Blundell | 0472056 | 2018-05-14 09:19:40 | [diff] [blame] | 288 | void GCMAccountTrackerTest::FinishAccountAddition( |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 289 | const std::string& account_id) { |
| 290 | IssueAccessToken(account_id); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 291 | |
| 292 | net::TestURLFetcher* fetcher = test_fetcher_factory_.GetFetcherByID( |
| 293 | gaia::GaiaOAuthClient::kUrlFetcherId); |
| 294 | ASSERT_TRUE(fetcher); |
| 295 | fetcher->set_response_code(net::HTTP_OK); |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 296 | fetcher->SetResponseString(GetValidTokenInfoResponse(account_id)); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 297 | fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 298 | } |
| 299 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 300 | std::string GCMAccountTrackerTest::AddPrimaryAccount(const std::string& gaia_id, |
| 301 | const std::string& email) { |
| 302 | std::string account_id = StartPrimaryAccountAddition(gaia_id, email); |
| 303 | FinishAccountAddition(account_id); |
| 304 | return account_id; |
Colin Blundell | bf6d4c5f | 2018-05-16 11:12:29 | [diff] [blame] | 305 | } |
| 306 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 307 | std::string GCMAccountTrackerTest::AddAccount(const std::string& gaia_id, |
| 308 | const std::string& email) { |
| 309 | std::string account_id = StartAccountAddition(gaia_id, email); |
| 310 | FinishAccountAddition(account_id); |
| 311 | return account_id; |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 312 | } |
| 313 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 314 | void GCMAccountTrackerTest::RemoveAccount(const std::string& account_id) { |
| 315 | fake_token_service_->RevokeCredentials(account_id); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 316 | } |
| 317 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 318 | void GCMAccountTrackerTest::IssueAccessToken(const std::string& account_id) { |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 319 | fake_token_service_->IssueAllTokensForAccount( |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 320 | account_id, MakeAccessToken(account_id), base::Time::Max()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 321 | } |
| 322 | |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 323 | void GCMAccountTrackerTest::IssueExpiredAccessToken( |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 324 | const std::string& account_id) { |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 325 | fake_token_service_->IssueAllTokensForAccount( |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 326 | account_id, MakeAccessToken(account_id), base::Time::Now()); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 327 | } |
| 328 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 329 | void GCMAccountTrackerTest::IssueError(const std::string& account_id) { |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 330 | fake_token_service_->IssueErrorForAllPendingRequestsForAccount( |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 331 | account_id, |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 332 | GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE)); |
| 333 | } |
| 334 | |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 335 | bool GCMAccountTrackerTest::IsFetchingRequired() const { |
| 336 | return tracker_->IsTokenFetchingRequired(); |
| 337 | } |
| 338 | |
| 339 | bool GCMAccountTrackerTest::IsTokenReportingRequired() const { |
| 340 | return tracker_->IsTokenReportingRequired(); |
| 341 | } |
| 342 | |
| 343 | base::TimeDelta GCMAccountTrackerTest::GetTimeToNextTokenReporting() const { |
| 344 | return tracker_->GetTimeToNextTokenReporting(); |
| 345 | } |
| 346 | |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 347 | TEST_F(GCMAccountTrackerTest, NoAccounts) { |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 348 | EXPECT_FALSE(driver()->update_accounts_called()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 349 | tracker()->Start(); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 350 | // Callback should not be called if there where no accounts provided. |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 351 | EXPECT_FALSE(driver()->update_accounts_called()); |
| 352 | EXPECT_TRUE(driver()->accounts().empty()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | // Verifies that callback is called after a token is issued for a single account |
| 356 | // with a specific scope. In this scenario, the underlying account tracker is |
| 357 | // still working when the CompleteCollectingTokens is called for the first time. |
| 358 | TEST_F(GCMAccountTrackerTest, SingleAccount) { |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 359 | std::string account_id1 = StartPrimaryAccountAddition(kGaiaId1, kEmail1); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 360 | |
| 361 | tracker()->Start(); |
| 362 | // We don't have any accounts to report, but given the inner account tracker |
| 363 | // is still working we don't make a call with empty accounts list. |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 364 | EXPECT_FALSE(driver()->update_accounts_called()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 365 | |
| 366 | // This concludes the work of inner account tracker. |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 367 | FinishAccountAddition(account_id1); |
| 368 | IssueAccessToken(account_id1); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 369 | |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 370 | EXPECT_TRUE(driver()->update_accounts_called()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 371 | |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 372 | std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 373 | expected_accounts.push_back(MakeAccountToken(account_id1)); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 374 | VerifyAccountTokens(expected_accounts, driver()->accounts()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | TEST_F(GCMAccountTrackerTest, MultipleAccounts) { |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 378 | std::string account_id1 = StartPrimaryAccountAddition(kGaiaId1, kEmail1); |
Colin Blundell | bf6d4c5f | 2018-05-16 11:12:29 | [diff] [blame] | 379 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 380 | std::string account_id2 = StartAccountAddition(kGaiaId2, kEmail2); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 381 | |
| 382 | tracker()->Start(); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 383 | EXPECT_FALSE(driver()->update_accounts_called()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 384 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 385 | FinishAccountAddition(account_id1); |
| 386 | IssueAccessToken(account_id1); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 387 | EXPECT_FALSE(driver()->update_accounts_called()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 388 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 389 | FinishAccountAddition(account_id2); |
| 390 | IssueAccessToken(account_id2); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 391 | EXPECT_TRUE(driver()->update_accounts_called()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 392 | |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 393 | std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 394 | expected_accounts.push_back(MakeAccountToken(account_id1)); |
| 395 | expected_accounts.push_back(MakeAccountToken(account_id2)); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 396 | VerifyAccountTokens(expected_accounts, driver()->accounts()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | TEST_F(GCMAccountTrackerTest, AccountAdded) { |
| 400 | tracker()->Start(); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 401 | driver()->ResetResults(); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 402 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 403 | std::string account_id1 = AddPrimaryAccount(kGaiaId1, kEmail1); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 404 | EXPECT_FALSE(driver()->update_accounts_called()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 405 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 406 | IssueAccessToken(account_id1); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 407 | EXPECT_TRUE(driver()->update_accounts_called()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 408 | |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 409 | std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 410 | expected_accounts.push_back(MakeAccountToken(account_id1)); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 411 | VerifyAccountTokens(expected_accounts, driver()->accounts()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | TEST_F(GCMAccountTrackerTest, AccountRemoved) { |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 415 | std::string account_id1 = AddPrimaryAccount(kGaiaId1, kEmail1); |
| 416 | std::string account_id2 = AddAccount(kGaiaId2, kEmail2); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 417 | |
| 418 | tracker()->Start(); |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 419 | IssueAccessToken(account_id1); |
| 420 | IssueAccessToken(account_id2); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 421 | EXPECT_TRUE(driver()->update_accounts_called()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 422 | |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 423 | driver()->ResetResults(); |
| 424 | EXPECT_FALSE(driver()->update_accounts_called()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 425 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 426 | RemoveAccount(account_id2); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 427 | EXPECT_TRUE(driver()->update_accounts_called()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 428 | |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 429 | std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 430 | expected_accounts.push_back(MakeAccountToken(account_id1)); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 431 | VerifyAccountTokens(expected_accounts, driver()->accounts()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | TEST_F(GCMAccountTrackerTest, GetTokenFailed) { |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 435 | std::string account_id1 = AddPrimaryAccount(kGaiaId1, kEmail1); |
| 436 | std::string account_id2 = AddAccount(kGaiaId2, kEmail2); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 437 | |
| 438 | tracker()->Start(); |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 439 | IssueAccessToken(account_id1); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 440 | EXPECT_FALSE(driver()->update_accounts_called()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 441 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 442 | IssueError(account_id2); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 443 | |
fgorski | be981e7 | 2015-01-26 21:07:19 | [diff] [blame] | 444 | // Failed token is not retried any more. Account marked as removed. |
| 445 | EXPECT_EQ(0UL, tracker()->get_pending_token_request_count()); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 446 | EXPECT_TRUE(driver()->update_accounts_called()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 447 | |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 448 | std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 449 | expected_accounts.push_back(MakeAccountToken(account_id1)); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 450 | VerifyAccountTokens(expected_accounts, driver()->accounts()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | TEST_F(GCMAccountTrackerTest, GetTokenFailedAccountRemoved) { |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 454 | std::string account_id1 = AddPrimaryAccount(kGaiaId1, kEmail1); |
| 455 | std::string account_id2 = AddAccount(kGaiaId2, kEmail2); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 456 | |
| 457 | tracker()->Start(); |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 458 | IssueAccessToken(account_id1); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 459 | |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 460 | driver()->ResetResults(); |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 461 | RemoveAccount(account_id2); |
| 462 | IssueError(account_id2); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 463 | |
| 464 | EXPECT_TRUE(driver()->update_accounts_called()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 465 | |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 466 | std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 467 | expected_accounts.push_back(MakeAccountToken(account_id1)); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 468 | VerifyAccountTokens(expected_accounts, driver()->accounts()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | TEST_F(GCMAccountTrackerTest, AccountRemovedWhileRequestsPending) { |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 472 | std::string account_id1 = AddPrimaryAccount(kGaiaId1, kEmail1); |
| 473 | std::string account_id2 = AddAccount(kGaiaId2, kEmail2); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 474 | |
| 475 | tracker()->Start(); |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 476 | IssueAccessToken(account_id1); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 477 | EXPECT_FALSE(driver()->update_accounts_called()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 478 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 479 | RemoveAccount(account_id2); |
| 480 | IssueAccessToken(account_id2); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 481 | EXPECT_TRUE(driver()->update_accounts_called()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 482 | |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 483 | std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 484 | expected_accounts.push_back(MakeAccountToken(account_id1)); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 485 | VerifyAccountTokens(expected_accounts, driver()->accounts()); |
| 486 | } |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 487 | |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 488 | // Makes sure that tracker observes GCM connection when running. |
| 489 | TEST_F(GCMAccountTrackerTest, TrackerObservesConnection) { |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 490 | EXPECT_EQ(nullptr, driver()->last_connection_observer()); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 491 | tracker()->Start(); |
| 492 | EXPECT_EQ(tracker(), driver()->last_connection_observer()); |
| 493 | tracker()->Shutdown(); |
| 494 | EXPECT_EQ(tracker(), driver()->last_removed_connection_observer()); |
| 495 | } |
| 496 | |
| 497 | // Makes sure that token fetching happens only after connection is established. |
| 498 | TEST_F(GCMAccountTrackerTest, PostponeTokenFetchingUntilConnected) { |
| 499 | driver()->SetConnected(false); |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 500 | std::string account_id1 = StartPrimaryAccountAddition(kGaiaId1, kEmail1); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 501 | tracker()->Start(); |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 502 | FinishAccountAddition(account_id1); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 503 | |
| 504 | EXPECT_EQ(0UL, tracker()->get_pending_token_request_count()); |
| 505 | driver()->SetConnected(true); |
| 506 | |
| 507 | EXPECT_EQ(1UL, tracker()->get_pending_token_request_count()); |
| 508 | } |
| 509 | |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 510 | TEST_F(GCMAccountTrackerTest, InvalidateExpiredTokens) { |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 511 | std::string account_id1 = StartPrimaryAccountAddition(kGaiaId1, kEmail1); |
| 512 | std::string account_id2 = StartAccountAddition(kGaiaId2, kEmail2); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 513 | tracker()->Start(); |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 514 | FinishAccountAddition(account_id1); |
| 515 | FinishAccountAddition(account_id2); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 516 | |
| 517 | EXPECT_EQ(2UL, tracker()->get_pending_token_request_count()); |
| 518 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 519 | IssueExpiredAccessToken(account_id1); |
| 520 | IssueAccessToken(account_id2); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 521 | // Because the first token is expired, we expect the sanitize to kick in and |
| 522 | // clean it up before the SetAccessToken is called. This also means a new |
| 523 | // token request will be issued |
| 524 | EXPECT_FALSE(driver()->update_accounts_called()); |
| 525 | EXPECT_EQ(1UL, tracker()->get_pending_token_request_count()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 526 | } |
| 527 | |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 528 | // Testing for whether there are still more tokens to be fetched. Typically the |
| 529 | // need for token fetching triggers immediate request, unless there is no |
| 530 | // connection, that is why connection is set on and off in this test. |
| 531 | TEST_F(GCMAccountTrackerTest, IsTokenFetchingRequired) { |
| 532 | tracker()->Start(); |
| 533 | driver()->SetConnected(false); |
| 534 | EXPECT_FALSE(IsFetchingRequired()); |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 535 | std::string account_id1 = StartPrimaryAccountAddition(kGaiaId1, kEmail1); |
| 536 | FinishAccountAddition(account_id1); |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 537 | EXPECT_TRUE(IsFetchingRequired()); |
| 538 | |
| 539 | driver()->SetConnected(true); |
| 540 | EXPECT_FALSE(IsFetchingRequired()); // Indicates that fetching has started. |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 541 | IssueAccessToken(account_id1); |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 542 | EXPECT_FALSE(IsFetchingRequired()); |
| 543 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 544 | std::string account_id2 = StartAccountAddition(kGaiaId2, kEmail2); |
| 545 | FinishAccountAddition(account_id2); |
Colin Blundell | aa0f4f0 | 2018-06-28 14:41:45 | [diff] [blame] | 546 | EXPECT_FALSE(IsFetchingRequired()); // Indicates that fetching has started. |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 547 | |
Colin Blundell | aa0f4f0 | 2018-06-28 14:41:45 | [diff] [blame] | 548 | // Disconnect the driver again so that the access token request being |
| 549 | // fulfilled doesn't immediately cause another access token request (which |
| 550 | // then would cause IsFetchingRequired() to be false, preventing us from |
| 551 | // distinguishing this case from the case where IsFetchingRequired() is false |
| 552 | // because GCMAccountTracker didn't detect that a new access token needs to be |
| 553 | // fetched). |
| 554 | driver()->SetConnected(false); |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 555 | IssueExpiredAccessToken(account_id2); |
Colin Blundell | aa0f4f0 | 2018-06-28 14:41:45 | [diff] [blame] | 556 | |
| 557 | // Make sure that if the token was expired it is marked as being needed again. |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 558 | EXPECT_TRUE(IsFetchingRequired()); |
| 559 | } |
| 560 | |
| 561 | // Tests what is the expected time to the next token fetching. |
| 562 | TEST_F(GCMAccountTrackerTest, GetTimeToNextTokenReporting) { |
| 563 | tracker()->Start(); |
| 564 | // At this point the last token fetch time is never. |
| 565 | EXPECT_EQ(base::TimeDelta(), GetTimeToNextTokenReporting()); |
| 566 | |
fgorski | 702e92ed | 2015-01-29 19:22:02 | [diff] [blame] | 567 | // Regular case. The tokens have been just reported. |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 568 | driver()->SetLastTokenFetchTime(base::Time::Now()); |
| 569 | EXPECT_TRUE(GetTimeToNextTokenReporting() <= |
| 570 | base::TimeDelta::FromSeconds(12 * 60 * 60)); |
fgorski | 702e92ed | 2015-01-29 19:22:02 | [diff] [blame] | 571 | |
| 572 | // A case when gcm driver is not yet initialized. |
| 573 | driver()->SetLastTokenFetchTime(base::Time::Max()); |
| 574 | EXPECT_EQ(base::TimeDelta::FromSeconds(12 * 60 * 60), |
| 575 | GetTimeToNextTokenReporting()); |
| 576 | |
| 577 | // A case when token reporting calculation is expected to result in more than |
| 578 | // 12 hours, in which case we expect exactly 12 hours. |
| 579 | driver()->SetLastTokenFetchTime(base::Time::Now() + |
| 580 | base::TimeDelta::FromDays(2)); |
| 581 | EXPECT_EQ(base::TimeDelta::FromSeconds(12 * 60 * 60), |
| 582 | GetTimeToNextTokenReporting()); |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | // Tests conditions when token reporting is required. |
| 586 | TEST_F(GCMAccountTrackerTest, IsTokenReportingRequired) { |
| 587 | tracker()->Start(); |
| 588 | // Required because it is overdue. |
| 589 | EXPECT_TRUE(IsTokenReportingRequired()); |
| 590 | |
| 591 | // Not required because it just happened. |
| 592 | driver()->SetLastTokenFetchTime(base::Time::Now()); |
| 593 | EXPECT_FALSE(IsTokenReportingRequired()); |
| 594 | |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 595 | std::string account_id1 = AddPrimaryAccount(kGaiaId1, kEmail1); |
| 596 | IssueAccessToken(account_id1); |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 597 | driver()->ResetResults(); |
| 598 | // Reporting was triggered, which means testing for required will give false, |
| 599 | // but we have the update call. |
Colin Blundell | a1b7c90 | 2018-06-29 06:28:06 | [diff] [blame^] | 600 | RemoveAccount(account_id1); |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 601 | EXPECT_TRUE(driver()->update_accounts_called()); |
| 602 | EXPECT_FALSE(IsTokenReportingRequired()); |
| 603 | } |
| 604 | |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 605 | // TODO(fgorski): Add test for adding account after removal >> make sure it does |
| 606 | // not mark removal. |
| 607 | |
| 608 | } // namespace gcm |