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