OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/services/gcm/gcm_account_tracker.h" | 5 #include "chrome/browser/services/gcm/gcm_account_tracker.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/run_loop.h" | |
13 #include "components/gcm_driver/fake_gcm_driver.h" | |
12 #include "google_apis/gaia/fake_identity_provider.h" | 14 #include "google_apis/gaia/fake_identity_provider.h" |
13 #include "google_apis/gaia/fake_oauth2_token_service.h" | 15 #include "google_apis/gaia/fake_oauth2_token_service.h" |
14 #include "google_apis/gaia/google_service_auth_error.h" | 16 #include "google_apis/gaia/google_service_auth_error.h" |
15 #include "net/http/http_status_code.h" | 17 #include "net/http/http_status_code.h" |
16 #include "net/url_request/test_url_fetcher_factory.h" | 18 #include "net/url_request/test_url_fetcher_factory.h" |
17 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
19 | 21 |
20 namespace gcm { | 22 namespace gcm { |
21 | 23 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
54 actual_iter = actual_tokens.begin(); | 56 actual_iter = actual_tokens.begin(); |
55 expected_iter != expected_tokens.end() && | 57 expected_iter != expected_tokens.end() && |
56 actual_iter != actual_tokens.end(); | 58 actual_iter != actual_tokens.end(); |
57 ++expected_iter, ++actual_iter) { | 59 ++expected_iter, ++actual_iter) { |
58 EXPECT_EQ(expected_iter->account_id, actual_iter->account_id); | 60 EXPECT_EQ(expected_iter->account_id, actual_iter->account_id); |
59 EXPECT_EQ(expected_iter->email, actual_iter->email); | 61 EXPECT_EQ(expected_iter->email, actual_iter->email); |
60 EXPECT_EQ(expected_iter->access_token, actual_iter->access_token); | 62 EXPECT_EQ(expected_iter->access_token, actual_iter->access_token); |
61 } | 63 } |
62 } | 64 } |
63 | 65 |
66 class CustomFakeGCMDriver : public FakeGCMDriver { | |
Nicolas Zea
2014/10/08 00:36:35
nit: comment what it does differently from the nor
fgorski
2014/10/08 18:04:27
Done.
| |
67 public: | |
68 CustomFakeGCMDriver(); | |
69 virtual ~CustomFakeGCMDriver(); | |
70 | |
71 virtual void SetAccountTokens( | |
72 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) OVERRIDE; | |
Nicolas Zea
2014/10/08 00:36:35
nit: OVERRIDE -> override
fgorski
2014/10/08 18:04:27
Done.
| |
73 virtual void AddConnectionObserver(GCMConnectionObserver* observer) OVERRIDE; | |
74 virtual void RemoveConnectionObserver( | |
75 GCMConnectionObserver* observer) OVERRIDE; | |
76 virtual bool IsConnected() const OVERRIDE { return connected_; } | |
77 | |
78 void set_connected(bool connected) { | |
Nicolas Zea
2014/10/08 00:36:35
uber nit: the hacker_style inline method is genera
fgorski
2014/10/08 18:04:27
Uber done.
| |
79 connected_ = connected; | |
80 if (connected && last_connection_observer_) | |
81 last_connection_observer_->OnConnected(ip_endpoint_); | |
82 } | |
83 | |
84 // Test results and helpers. | |
85 void ResetResults(); | |
86 bool update_accounts_called() const { return update_accounts_called_; } | |
87 const std::vector<GCMClient::AccountTokenInfo>& accounts() const { | |
88 return accounts_; | |
89 } | |
90 const GCMConnectionObserver* last_connection_observer() const { | |
91 return last_connection_observer_; | |
92 } | |
93 const GCMConnectionObserver* last_removed_connection_observer() const { | |
94 return removed_connection_observer_; | |
95 } | |
96 | |
97 private: | |
98 bool connected_; | |
99 std::vector<GCMClient::AccountTokenInfo> accounts_; | |
100 bool update_accounts_called_; | |
101 GCMConnectionObserver* last_connection_observer_; | |
102 GCMConnectionObserver* removed_connection_observer_; | |
103 net::IPEndPoint ip_endpoint_; | |
104 | |
105 DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver); | |
106 }; | |
107 | |
108 CustomFakeGCMDriver::CustomFakeGCMDriver() | |
109 : connected_(true), | |
110 update_accounts_called_(false), | |
111 last_connection_observer_(NULL), | |
112 removed_connection_observer_(NULL) { | |
113 } | |
114 | |
115 CustomFakeGCMDriver::~CustomFakeGCMDriver() { | |
116 } | |
117 | |
118 void CustomFakeGCMDriver::SetAccountTokens( | |
119 const std::vector<GCMClient::AccountTokenInfo>& accounts) { | |
120 update_accounts_called_ = true; | |
121 accounts_ = accounts; | |
122 } | |
123 | |
124 void CustomFakeGCMDriver::AddConnectionObserver( | |
125 GCMConnectionObserver* observer) { | |
126 last_connection_observer_ = observer; | |
127 } | |
128 | |
129 void CustomFakeGCMDriver::RemoveConnectionObserver( | |
130 GCMConnectionObserver* observer) { | |
131 removed_connection_observer_ = observer; | |
132 } | |
133 | |
134 void CustomFakeGCMDriver::ResetResults() { | |
135 accounts_.clear(); | |
136 update_accounts_called_ = false; | |
137 last_connection_observer_ = NULL; | |
138 removed_connection_observer_ = NULL; | |
139 } | |
140 | |
64 } // namespace | 141 } // namespace |
65 | 142 |
66 class GCMAccountTrackerTest : public testing::Test { | 143 class GCMAccountTrackerTest : public testing::Test { |
67 public: | 144 public: |
68 GCMAccountTrackerTest(); | 145 GCMAccountTrackerTest(); |
69 virtual ~GCMAccountTrackerTest(); | 146 virtual ~GCMAccountTrackerTest(); |
70 | 147 |
71 // Callback for the account tracker. | |
72 void UpdateAccounts(const std::vector<GCMClient::AccountTokenInfo>& accounts); | |
73 | |
74 // Helpers to pass fake events to the tracker. Tests should have either a pair | 148 // Helpers to pass fake events to the tracker. Tests should have either a pair |
75 // of Start/FinishAccountSignIn or SignInAccount per account. Don't mix. | 149 // of Start/FinishAccountSignIn or SignInAccount per account. Don't mix. |
76 // Call to SignOutAccount is not mandatory. | 150 // Call to SignOutAccount is not mandatory. |
77 void StartAccountSignIn(const std::string& account_key); | 151 void StartAccountSignIn(const std::string& account_key); |
78 void FinishAccountSignIn(const std::string& account_key); | 152 void FinishAccountSignIn(const std::string& account_key); |
79 void SignInAccount(const std::string& account_key); | 153 void SignInAccount(const std::string& account_key); |
80 void SignOutAccount(const std::string& account_key); | 154 void SignOutAccount(const std::string& account_key); |
81 | 155 |
82 // Helpers for dealing with OAuth2 access token requests. | 156 // Helpers for dealing with OAuth2 access token requests. |
83 void IssueAccessToken(const std::string& account_key); | 157 void IssueAccessToken(const std::string& account_key); |
158 void IssueExpiredAccessToken(const std::string& account_key); | |
84 void IssueError(const std::string& account_key); | 159 void IssueError(const std::string& account_key); |
85 | 160 |
86 // Test results and helpers. | 161 // Accessors to account tracker and gcm driver. |
87 void ResetResults(); | |
88 bool update_accounts_called() const { return update_accounts_called_; } | |
89 const std::vector<GCMClient::AccountTokenInfo>& accounts() const { | |
90 return accounts_; | |
91 } | |
92 | |
93 // Accessor to account tracker. | |
94 GCMAccountTracker* tracker() { return tracker_.get(); } | 162 GCMAccountTracker* tracker() { return tracker_.get(); } |
163 CustomFakeGCMDriver* driver() { return &driver_; } | |
95 | 164 |
96 private: | 165 private: |
97 std::vector<GCMClient::AccountTokenInfo> accounts_; | 166 CustomFakeGCMDriver driver_; |
98 bool update_accounts_called_; | |
99 | 167 |
100 base::MessageLoop message_loop_; | 168 base::MessageLoop message_loop_; |
101 net::TestURLFetcherFactory test_fetcher_factory_; | 169 net::TestURLFetcherFactory test_fetcher_factory_; |
102 scoped_ptr<FakeOAuth2TokenService> fake_token_service_; | 170 scoped_ptr<FakeOAuth2TokenService> fake_token_service_; |
103 scoped_ptr<FakeIdentityProvider> fake_identity_provider_; | 171 scoped_ptr<FakeIdentityProvider> fake_identity_provider_; |
104 scoped_ptr<GCMAccountTracker> tracker_; | 172 scoped_ptr<GCMAccountTracker> tracker_; |
105 }; | 173 }; |
106 | 174 |
107 GCMAccountTrackerTest::GCMAccountTrackerTest() | 175 GCMAccountTrackerTest::GCMAccountTrackerTest() { |
108 : update_accounts_called_(false) { | |
109 fake_token_service_.reset(new FakeOAuth2TokenService()); | 176 fake_token_service_.reset(new FakeOAuth2TokenService()); |
110 | 177 |
111 fake_identity_provider_.reset( | 178 fake_identity_provider_.reset( |
112 new FakeIdentityProvider(fake_token_service_.get())); | 179 new FakeIdentityProvider(fake_token_service_.get())); |
113 | 180 |
114 scoped_ptr<gaia::AccountTracker> gaia_account_tracker( | 181 scoped_ptr<gaia::AccountTracker> gaia_account_tracker( |
115 new gaia::AccountTracker(fake_identity_provider_.get(), | 182 new gaia::AccountTracker(fake_identity_provider_.get(), |
116 new net::TestURLRequestContextGetter( | 183 new net::TestURLRequestContextGetter( |
117 message_loop_.message_loop_proxy()))); | 184 message_loop_.message_loop_proxy()))); |
118 | 185 |
119 tracker_.reset(new GCMAccountTracker( | 186 tracker_.reset(new GCMAccountTracker(gaia_account_tracker.Pass(), &driver_)); |
120 gaia_account_tracker.Pass(), | |
121 base::Bind(&GCMAccountTrackerTest::UpdateAccounts, | |
122 base::Unretained(this)))); | |
123 } | 187 } |
124 | 188 |
125 GCMAccountTrackerTest::~GCMAccountTrackerTest() { | 189 GCMAccountTrackerTest::~GCMAccountTrackerTest() { |
126 if (tracker_) | 190 if (tracker_) |
127 tracker_->Shutdown(); | 191 tracker_->Shutdown(); |
128 } | 192 } |
129 | 193 |
130 void GCMAccountTrackerTest::UpdateAccounts( | |
131 const std::vector<GCMClient::AccountTokenInfo>& accounts) { | |
132 update_accounts_called_ = true; | |
133 accounts_ = accounts; | |
134 } | |
135 | |
136 void GCMAccountTrackerTest::ResetResults() { | |
137 accounts_.clear(); | |
138 update_accounts_called_ = false; | |
139 } | |
140 | |
141 void GCMAccountTrackerTest::StartAccountSignIn(const std::string& account_key) { | 194 void GCMAccountTrackerTest::StartAccountSignIn(const std::string& account_key) { |
142 fake_identity_provider_->LogIn(account_key); | 195 fake_identity_provider_->LogIn(account_key); |
143 fake_token_service_->AddAccount(account_key); | 196 fake_token_service_->AddAccount(account_key); |
144 } | 197 } |
145 | 198 |
146 void GCMAccountTrackerTest::FinishAccountSignIn( | 199 void GCMAccountTrackerTest::FinishAccountSignIn( |
147 const std::string& account_key) { | 200 const std::string& account_key) { |
148 IssueAccessToken(account_key); | 201 IssueAccessToken(account_key); |
149 | 202 |
150 net::TestURLFetcher* fetcher = test_fetcher_factory_.GetFetcherByID( | 203 net::TestURLFetcher* fetcher = test_fetcher_factory_.GetFetcherByID( |
(...skipping 11 matching lines...) Expand all Loading... | |
162 | 215 |
163 void GCMAccountTrackerTest::SignOutAccount(const std::string& account_key) { | 216 void GCMAccountTrackerTest::SignOutAccount(const std::string& account_key) { |
164 fake_token_service_->RemoveAccount(account_key); | 217 fake_token_service_->RemoveAccount(account_key); |
165 } | 218 } |
166 | 219 |
167 void GCMAccountTrackerTest::IssueAccessToken(const std::string& account_key) { | 220 void GCMAccountTrackerTest::IssueAccessToken(const std::string& account_key) { |
168 fake_token_service_->IssueAllTokensForAccount( | 221 fake_token_service_->IssueAllTokensForAccount( |
169 account_key, MakeAccessToken(account_key), base::Time::Max()); | 222 account_key, MakeAccessToken(account_key), base::Time::Max()); |
170 } | 223 } |
171 | 224 |
225 void GCMAccountTrackerTest::IssueExpiredAccessToken( | |
226 const std::string& account_key) { | |
227 fake_token_service_->IssueAllTokensForAccount( | |
228 account_key, MakeAccessToken(account_key), base::Time::Now()); | |
229 } | |
230 | |
172 void GCMAccountTrackerTest::IssueError(const std::string& account_key) { | 231 void GCMAccountTrackerTest::IssueError(const std::string& account_key) { |
173 fake_token_service_->IssueErrorForAllPendingRequestsForAccount( | 232 fake_token_service_->IssueErrorForAllPendingRequestsForAccount( |
174 account_key, | 233 account_key, |
175 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE)); | 234 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE)); |
176 } | 235 } |
177 | 236 |
237 // TODO(fgorsk): This actually need fixing. | |
Nicolas Zea
2014/10/08 00:36:35
Is this comment still relevant?
fgorski
2014/10/08 18:04:27
Removing.
| |
178 TEST_F(GCMAccountTrackerTest, NoAccounts) { | 238 TEST_F(GCMAccountTrackerTest, NoAccounts) { |
179 EXPECT_FALSE(update_accounts_called()); | 239 EXPECT_FALSE(driver()->update_accounts_called()); |
180 tracker()->Start(); | 240 tracker()->Start(); |
181 // Callback should not be called if there where no accounts provided. | 241 // Callback should not be called if there where no accounts provided. |
182 EXPECT_FALSE(update_accounts_called()); | 242 EXPECT_FALSE(driver()->update_accounts_called()); |
183 EXPECT_TRUE(accounts().empty()); | 243 EXPECT_TRUE(driver()->accounts().empty()); |
184 tracker()->Stop(); | |
185 } | 244 } |
186 | 245 |
187 // Verifies that callback is called after a token is issued for a single account | 246 // Verifies that callback is called after a token is issued for a single account |
188 // with a specific scope. In this scenario, the underlying account tracker is | 247 // with a specific scope. In this scenario, the underlying account tracker is |
189 // still working when the CompleteCollectingTokens is called for the first time. | 248 // still working when the CompleteCollectingTokens is called for the first time. |
190 TEST_F(GCMAccountTrackerTest, SingleAccount) { | 249 TEST_F(GCMAccountTrackerTest, SingleAccount) { |
191 StartAccountSignIn(kAccountId1); | 250 StartAccountSignIn(kAccountId1); |
192 | 251 |
193 tracker()->Start(); | 252 tracker()->Start(); |
194 // We don't have any accounts to report, but given the inner account tracker | 253 // We don't have any accounts to report, but given the inner account tracker |
195 // is still working we don't make a call with empty accounts list. | 254 // is still working we don't make a call with empty accounts list. |
196 EXPECT_FALSE(update_accounts_called()); | 255 EXPECT_FALSE(driver()->update_accounts_called()); |
197 | 256 |
198 // This concludes the work of inner account tracker. | 257 // This concludes the work of inner account tracker. |
199 FinishAccountSignIn(kAccountId1); | 258 FinishAccountSignIn(kAccountId1); |
200 IssueAccessToken(kAccountId1); | 259 IssueAccessToken(kAccountId1); |
201 | 260 |
202 EXPECT_TRUE(update_accounts_called()); | 261 EXPECT_TRUE(driver()->update_accounts_called()); |
203 | 262 |
204 std::vector<GCMClient::AccountTokenInfo> expected_accounts; | 263 std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
205 expected_accounts.push_back(MakeAccountToken(kAccountId1)); | 264 expected_accounts.push_back(MakeAccountToken(kAccountId1)); |
206 VerifyAccountTokens(expected_accounts, accounts()); | 265 VerifyAccountTokens(expected_accounts, driver()->accounts()); |
207 tracker()->Stop(); | |
208 } | 266 } |
209 | 267 |
210 TEST_F(GCMAccountTrackerTest, MultipleAccounts) { | 268 TEST_F(GCMAccountTrackerTest, MultipleAccounts) { |
211 StartAccountSignIn(kAccountId1); | 269 StartAccountSignIn(kAccountId1); |
212 StartAccountSignIn(kAccountId2); | 270 StartAccountSignIn(kAccountId2); |
213 | 271 |
214 tracker()->Start(); | 272 tracker()->Start(); |
215 EXPECT_FALSE(update_accounts_called()); | 273 EXPECT_FALSE(driver()->update_accounts_called()); |
216 | 274 |
217 FinishAccountSignIn(kAccountId1); | 275 FinishAccountSignIn(kAccountId1); |
218 IssueAccessToken(kAccountId1); | 276 IssueAccessToken(kAccountId1); |
219 EXPECT_FALSE(update_accounts_called()); | 277 EXPECT_FALSE(driver()->update_accounts_called()); |
220 | 278 |
221 FinishAccountSignIn(kAccountId2); | 279 FinishAccountSignIn(kAccountId2); |
222 IssueAccessToken(kAccountId2); | 280 IssueAccessToken(kAccountId2); |
223 EXPECT_TRUE(update_accounts_called()); | 281 EXPECT_TRUE(driver()->update_accounts_called()); |
224 | 282 |
225 std::vector<GCMClient::AccountTokenInfo> expected_accounts; | 283 std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
226 expected_accounts.push_back(MakeAccountToken(kAccountId1)); | 284 expected_accounts.push_back(MakeAccountToken(kAccountId1)); |
227 expected_accounts.push_back(MakeAccountToken(kAccountId2)); | 285 expected_accounts.push_back(MakeAccountToken(kAccountId2)); |
228 VerifyAccountTokens(expected_accounts, accounts()); | 286 VerifyAccountTokens(expected_accounts, driver()->accounts()); |
229 | |
230 tracker()->Stop(); | |
231 } | 287 } |
232 | 288 |
233 TEST_F(GCMAccountTrackerTest, AccountAdded) { | 289 TEST_F(GCMAccountTrackerTest, AccountAdded) { |
234 tracker()->Start(); | 290 tracker()->Start(); |
235 ResetResults(); | 291 driver()->ResetResults(); |
236 | 292 |
237 SignInAccount(kAccountId1); | 293 SignInAccount(kAccountId1); |
238 EXPECT_FALSE(update_accounts_called()); | 294 EXPECT_FALSE(driver()->update_accounts_called()); |
239 | 295 |
240 IssueAccessToken(kAccountId1); | 296 IssueAccessToken(kAccountId1); |
241 EXPECT_TRUE(update_accounts_called()); | 297 EXPECT_TRUE(driver()->update_accounts_called()); |
242 | 298 |
243 std::vector<GCMClient::AccountTokenInfo> expected_accounts; | 299 std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
244 expected_accounts.push_back(MakeAccountToken(kAccountId1)); | 300 expected_accounts.push_back(MakeAccountToken(kAccountId1)); |
245 VerifyAccountTokens(expected_accounts, accounts()); | 301 VerifyAccountTokens(expected_accounts, driver()->accounts()); |
246 | |
247 tracker()->Stop(); | |
248 } | 302 } |
249 | 303 |
250 TEST_F(GCMAccountTrackerTest, AccountRemoved) { | 304 TEST_F(GCMAccountTrackerTest, AccountRemoved) { |
251 SignInAccount(kAccountId1); | 305 SignInAccount(kAccountId1); |
252 SignInAccount(kAccountId2); | 306 SignInAccount(kAccountId2); |
253 | 307 |
254 tracker()->Start(); | 308 tracker()->Start(); |
255 IssueAccessToken(kAccountId1); | 309 IssueAccessToken(kAccountId1); |
256 IssueAccessToken(kAccountId2); | 310 IssueAccessToken(kAccountId2); |
257 EXPECT_TRUE(update_accounts_called()); | 311 EXPECT_TRUE(driver()->update_accounts_called()); |
258 | 312 |
259 ResetResults(); | 313 driver()->ResetResults(); |
260 EXPECT_FALSE(update_accounts_called()); | 314 EXPECT_FALSE(driver()->update_accounts_called()); |
261 | 315 |
262 SignOutAccount(kAccountId2); | 316 SignOutAccount(kAccountId2); |
263 EXPECT_TRUE(update_accounts_called()); | 317 EXPECT_TRUE(driver()->update_accounts_called()); |
264 | 318 |
265 std::vector<GCMClient::AccountTokenInfo> expected_accounts; | 319 std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
266 expected_accounts.push_back(MakeAccountToken(kAccountId1)); | 320 expected_accounts.push_back(MakeAccountToken(kAccountId1)); |
267 VerifyAccountTokens(expected_accounts, accounts()); | 321 VerifyAccountTokens(expected_accounts, driver()->accounts()); |
268 | |
269 tracker()->Stop(); | |
270 } | 322 } |
271 | 323 |
272 TEST_F(GCMAccountTrackerTest, GetTokenFailed) { | 324 TEST_F(GCMAccountTrackerTest, GetTokenFailed) { |
273 SignInAccount(kAccountId1); | 325 SignInAccount(kAccountId1); |
274 SignInAccount(kAccountId2); | 326 SignInAccount(kAccountId2); |
275 | 327 |
276 tracker()->Start(); | 328 tracker()->Start(); |
277 IssueAccessToken(kAccountId1); | 329 IssueAccessToken(kAccountId1); |
278 EXPECT_FALSE(update_accounts_called()); | 330 EXPECT_FALSE(driver()->update_accounts_called()); |
279 | 331 |
280 IssueError(kAccountId2); | 332 IssueError(kAccountId2); |
281 EXPECT_TRUE(update_accounts_called()); | 333 EXPECT_FALSE(driver()->update_accounts_called()); |
334 | |
335 EXPECT_EQ(1UL, tracker()->get_pending_token_request_count()); | |
336 | |
337 IssueAccessToken(kAccountId2); | |
338 EXPECT_TRUE(driver()->update_accounts_called()); | |
282 | 339 |
283 std::vector<GCMClient::AccountTokenInfo> expected_accounts; | 340 std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
284 expected_accounts.push_back(MakeAccountToken(kAccountId1)); | 341 expected_accounts.push_back(MakeAccountToken(kAccountId1)); |
285 VerifyAccountTokens(expected_accounts, accounts()); | 342 expected_accounts.push_back(MakeAccountToken(kAccountId2)); |
286 | 343 VerifyAccountTokens(expected_accounts, driver()->accounts()); |
287 tracker()->Stop(); | |
288 } | 344 } |
289 | 345 |
290 TEST_F(GCMAccountTrackerTest, GetTokenFailedAccountRemoved) { | 346 TEST_F(GCMAccountTrackerTest, GetTokenFailedAccountRemoved) { |
291 SignInAccount(kAccountId1); | 347 SignInAccount(kAccountId1); |
292 SignInAccount(kAccountId2); | 348 SignInAccount(kAccountId2); |
293 | 349 |
294 tracker()->Start(); | 350 tracker()->Start(); |
295 IssueAccessToken(kAccountId1); | 351 IssueAccessToken(kAccountId1); |
296 IssueError(kAccountId2); | 352 IssueError(kAccountId2); |
297 | 353 |
298 ResetResults(); | 354 driver()->ResetResults(); |
299 SignOutAccount(kAccountId2); | 355 SignOutAccount(kAccountId2); |
300 EXPECT_TRUE(update_accounts_called()); | 356 IssueError(kAccountId2); |
357 | |
358 EXPECT_TRUE(driver()->update_accounts_called()); | |
301 | 359 |
302 std::vector<GCMClient::AccountTokenInfo> expected_accounts; | 360 std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
303 expected_accounts.push_back(MakeAccountToken(kAccountId1)); | 361 expected_accounts.push_back(MakeAccountToken(kAccountId1)); |
304 VerifyAccountTokens(expected_accounts, accounts()); | 362 VerifyAccountTokens(expected_accounts, driver()->accounts()); |
305 | |
306 tracker()->Stop(); | |
307 } | 363 } |
308 | 364 |
309 TEST_F(GCMAccountTrackerTest, AccountRemovedWhileRequestsPending) { | 365 TEST_F(GCMAccountTrackerTest, AccountRemovedWhileRequestsPending) { |
310 SignInAccount(kAccountId1); | 366 SignInAccount(kAccountId1); |
311 SignInAccount(kAccountId2); | 367 SignInAccount(kAccountId2); |
312 | 368 |
313 tracker()->Start(); | 369 tracker()->Start(); |
314 IssueAccessToken(kAccountId1); | 370 IssueAccessToken(kAccountId1); |
315 EXPECT_FALSE(update_accounts_called()); | 371 EXPECT_FALSE(driver()->update_accounts_called()); |
316 | 372 |
317 SignOutAccount(kAccountId2); | 373 SignOutAccount(kAccountId2); |
318 IssueAccessToken(kAccountId2); | 374 IssueAccessToken(kAccountId2); |
319 EXPECT_TRUE(update_accounts_called()); | 375 EXPECT_TRUE(driver()->update_accounts_called()); |
320 | 376 |
321 std::vector<GCMClient::AccountTokenInfo> expected_accounts; | 377 std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
322 expected_accounts.push_back(MakeAccountToken(kAccountId1)); | 378 expected_accounts.push_back(MakeAccountToken(kAccountId1)); |
323 VerifyAccountTokens(expected_accounts, accounts()); | 379 VerifyAccountTokens(expected_accounts, driver()->accounts()); |
380 } | |
324 | 381 |
325 tracker()->Stop(); | 382 // Makes sure that tracker observes GCM connection when running. |
383 TEST_F(GCMAccountTrackerTest, TrackerObservesConnection) { | |
384 EXPECT_EQ(NULL, driver()->last_connection_observer()); | |
385 tracker()->Start(); | |
386 EXPECT_EQ(tracker(), driver()->last_connection_observer()); | |
387 tracker()->Shutdown(); | |
388 EXPECT_EQ(tracker(), driver()->last_removed_connection_observer()); | |
389 } | |
390 | |
391 // Makes sure that token fetching happens only after connection is established. | |
392 TEST_F(GCMAccountTrackerTest, PostponeTokenFetchingUntilConnected) { | |
393 driver()->set_connected(false); | |
394 StartAccountSignIn(kAccountId1); | |
395 tracker()->Start(); | |
396 FinishAccountSignIn(kAccountId1); | |
397 | |
398 EXPECT_EQ(0UL, tracker()->get_pending_token_request_count()); | |
399 driver()->set_connected(true); | |
400 | |
401 EXPECT_EQ(1UL, tracker()->get_pending_token_request_count()); | |
402 } | |
403 | |
404 TEST_F(GCMAccountTrackerTest, IvalidateExpiredTokens) { | |
405 StartAccountSignIn(kAccountId1); | |
406 StartAccountSignIn(kAccountId2); | |
407 tracker()->Start(); | |
408 FinishAccountSignIn(kAccountId1); | |
409 FinishAccountSignIn(kAccountId2); | |
410 | |
411 EXPECT_EQ(2UL, tracker()->get_pending_token_request_count()); | |
412 | |
413 IssueExpiredAccessToken(kAccountId1); | |
414 IssueAccessToken(kAccountId2); | |
415 // Because the first token is expired, we expect the sanitize to kick in and | |
416 // clean it up before the SetAccessToken is called. This also means a new | |
417 // token request will be issued | |
418 EXPECT_FALSE(driver()->update_accounts_called()); | |
419 EXPECT_EQ(1UL, tracker()->get_pending_token_request_count()); | |
326 } | 420 } |
327 | 421 |
328 // TODO(fgorski): Add test for adding account after removal >> make sure it does | 422 // TODO(fgorski): Add test for adding account after removal >> make sure it does |
329 // not mark removal. | 423 // not mark removal. |
330 | 424 |
331 } // namespace gcm | 425 } // namespace gcm |
OLD | NEW |