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" |
(...skipping 19 matching lines...) Expand all Loading... |
30 | 30 |
31 std::string GetValidTokenInfoResponse(const std::string account_key) { | 31 std::string GetValidTokenInfoResponse(const std::string account_key) { |
32 return std::string("{ \"id\": \"") + AccountKeyToObfuscatedId(account_key) + | 32 return std::string("{ \"id\": \"") + AccountKeyToObfuscatedId(account_key) + |
33 "\" }"; | 33 "\" }"; |
34 } | 34 } |
35 | 35 |
36 std::string MakeAccessToken(const std::string& account_key) { | 36 std::string MakeAccessToken(const std::string& account_key) { |
37 return "access_token-" + account_key; | 37 return "access_token-" + account_key; |
38 } | 38 } |
39 | 39 |
| 40 GCMClient::AccountTokenInfo MakeAccountToken(const std::string& account_key) { |
| 41 GCMClient::AccountTokenInfo token_info; |
| 42 token_info.account_id = account_key; |
| 43 token_info.email = account_key; |
| 44 token_info.access_token = MakeAccessToken(account_key); |
| 45 return token_info; |
| 46 } |
| 47 |
| 48 void VerifyAccountTokens( |
| 49 const std::vector<GCMClient::AccountTokenInfo>& expected_tokens, |
| 50 const std::vector<GCMClient::AccountTokenInfo>& actual_tokens) { |
| 51 EXPECT_EQ(expected_tokens.size(), actual_tokens.size()); |
| 52 for (std::vector<GCMClient::AccountTokenInfo>::const_iterator |
| 53 expected_iter = expected_tokens.begin(), |
| 54 actual_iter = actual_tokens.begin(); |
| 55 expected_iter != expected_tokens.end() && |
| 56 actual_iter != actual_tokens.end(); |
| 57 ++expected_iter, ++actual_iter) { |
| 58 EXPECT_EQ(expected_iter->account_id, actual_iter->account_id); |
| 59 EXPECT_EQ(expected_iter->email, actual_iter->email); |
| 60 EXPECT_EQ(expected_iter->access_token, actual_iter->access_token); |
| 61 } |
| 62 } |
| 63 |
40 } // namespace | 64 } // namespace |
41 | 65 |
42 class GCMAccountTrackerTest : public testing::Test { | 66 class GCMAccountTrackerTest : public testing::Test { |
43 public: | 67 public: |
44 GCMAccountTrackerTest(); | 68 GCMAccountTrackerTest(); |
45 virtual ~GCMAccountTrackerTest(); | 69 virtual ~GCMAccountTrackerTest(); |
46 | 70 |
47 // Callback for the account tracker. | 71 // Callback for the account tracker. |
48 void UpdateAccounts(const std::map<std::string, std::string>& accounts); | 72 void UpdateAccounts(const std::vector<GCMClient::AccountTokenInfo>& accounts); |
49 | 73 |
50 // Helpers to pass fake events to the tracker. Tests should have either a pair | 74 // Helpers to pass fake events to the tracker. Tests should have either a pair |
51 // of Start/FinishAccountSignIn or SignInAccount per account. Don't mix. | 75 // of Start/FinishAccountSignIn or SignInAccount per account. Don't mix. |
52 // Call to SignOutAccount is not mandatory. | 76 // Call to SignOutAccount is not mandatory. |
53 void StartAccountSignIn(const std::string& account_key); | 77 void StartAccountSignIn(const std::string& account_key); |
54 void FinishAccountSignIn(const std::string& account_key); | 78 void FinishAccountSignIn(const std::string& account_key); |
55 void SignInAccount(const std::string& account_key); | 79 void SignInAccount(const std::string& account_key); |
56 void SignOutAccount(const std::string& account_key); | 80 void SignOutAccount(const std::string& account_key); |
57 | 81 |
58 // Helpers for dealing with OAuth2 access token requests. | 82 // Helpers for dealing with OAuth2 access token requests. |
59 void IssueAccessToken(const std::string& account_key); | 83 void IssueAccessToken(const std::string& account_key); |
60 void IssueError(const std::string& account_key); | 84 void IssueError(const std::string& account_key); |
61 | 85 |
62 // Test results and helpers. | 86 // Test results and helpers. |
63 void ResetResults(); | 87 void ResetResults(); |
64 bool update_accounts_called() const { return update_accounts_called_; } | 88 bool update_accounts_called() const { return update_accounts_called_; } |
65 const std::map<std::string, std::string>& accounts() const { | 89 const std::vector<GCMClient::AccountTokenInfo>& accounts() const { |
66 return accounts_; | 90 return accounts_; |
67 } | 91 } |
68 | 92 |
69 // Accessor to account tracker. | 93 // Accessor to account tracker. |
70 GCMAccountTracker* tracker() { return tracker_.get(); } | 94 GCMAccountTracker* tracker() { return tracker_.get(); } |
71 | 95 |
72 private: | 96 private: |
73 std::map<std::string, std::string> accounts_; | 97 std::vector<GCMClient::AccountTokenInfo> accounts_; |
74 bool update_accounts_called_; | 98 bool update_accounts_called_; |
75 | 99 |
76 base::MessageLoop message_loop_; | 100 base::MessageLoop message_loop_; |
77 net::TestURLFetcherFactory test_fetcher_factory_; | 101 net::TestURLFetcherFactory test_fetcher_factory_; |
78 scoped_ptr<FakeOAuth2TokenService> fake_token_service_; | 102 scoped_ptr<FakeOAuth2TokenService> fake_token_service_; |
79 scoped_ptr<FakeIdentityProvider> fake_identity_provider_; | 103 scoped_ptr<FakeIdentityProvider> fake_identity_provider_; |
80 scoped_ptr<GCMAccountTracker> tracker_; | 104 scoped_ptr<GCMAccountTracker> tracker_; |
81 }; | 105 }; |
82 | 106 |
83 GCMAccountTrackerTest::GCMAccountTrackerTest() | 107 GCMAccountTrackerTest::GCMAccountTrackerTest() |
(...skipping 13 matching lines...) Expand all Loading... |
97 base::Bind(&GCMAccountTrackerTest::UpdateAccounts, | 121 base::Bind(&GCMAccountTrackerTest::UpdateAccounts, |
98 base::Unretained(this)))); | 122 base::Unretained(this)))); |
99 } | 123 } |
100 | 124 |
101 GCMAccountTrackerTest::~GCMAccountTrackerTest() { | 125 GCMAccountTrackerTest::~GCMAccountTrackerTest() { |
102 if (tracker_) | 126 if (tracker_) |
103 tracker_->Shutdown(); | 127 tracker_->Shutdown(); |
104 } | 128 } |
105 | 129 |
106 void GCMAccountTrackerTest::UpdateAccounts( | 130 void GCMAccountTrackerTest::UpdateAccounts( |
107 const std::map<std::string, std::string>& accounts) { | 131 const std::vector<GCMClient::AccountTokenInfo>& accounts) { |
108 update_accounts_called_ = true; | 132 update_accounts_called_ = true; |
109 accounts_ = accounts; | 133 accounts_ = accounts; |
110 } | 134 } |
111 | 135 |
112 void GCMAccountTrackerTest::ResetResults() { | 136 void GCMAccountTrackerTest::ResetResults() { |
113 accounts_.clear(); | 137 accounts_.clear(); |
114 update_accounts_called_ = false; | 138 update_accounts_called_ = false; |
115 } | 139 } |
116 | 140 |
117 void GCMAccountTrackerTest::StartAccountSignIn(const std::string& account_key) { | 141 void GCMAccountTrackerTest::StartAccountSignIn(const std::string& account_key) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 // We don't have any accounts to report, but given the inner account tracker | 194 // We don't have any accounts to report, but given the inner account tracker |
171 // is still working we don't make a call with empty accounts list. | 195 // is still working we don't make a call with empty accounts list. |
172 EXPECT_FALSE(update_accounts_called()); | 196 EXPECT_FALSE(update_accounts_called()); |
173 | 197 |
174 // This concludes the work of inner account tracker. | 198 // This concludes the work of inner account tracker. |
175 FinishAccountSignIn(kAccountId1); | 199 FinishAccountSignIn(kAccountId1); |
176 IssueAccessToken(kAccountId1); | 200 IssueAccessToken(kAccountId1); |
177 | 201 |
178 EXPECT_TRUE(update_accounts_called()); | 202 EXPECT_TRUE(update_accounts_called()); |
179 | 203 |
180 std::map<std::string, std::string> expected_accounts; | 204 std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
181 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); | 205 expected_accounts.push_back(MakeAccountToken(kAccountId1)); |
182 EXPECT_EQ(expected_accounts, accounts()); | 206 VerifyAccountTokens(expected_accounts, accounts()); |
183 tracker()->Stop(); | 207 tracker()->Stop(); |
184 } | 208 } |
185 | 209 |
186 TEST_F(GCMAccountTrackerTest, MultipleAccounts) { | 210 TEST_F(GCMAccountTrackerTest, MultipleAccounts) { |
187 StartAccountSignIn(kAccountId1); | 211 StartAccountSignIn(kAccountId1); |
188 StartAccountSignIn(kAccountId2); | 212 StartAccountSignIn(kAccountId2); |
189 | 213 |
190 tracker()->Start(); | 214 tracker()->Start(); |
191 EXPECT_FALSE(update_accounts_called()); | 215 EXPECT_FALSE(update_accounts_called()); |
192 | 216 |
193 FinishAccountSignIn(kAccountId1); | 217 FinishAccountSignIn(kAccountId1); |
194 IssueAccessToken(kAccountId1); | 218 IssueAccessToken(kAccountId1); |
195 EXPECT_FALSE(update_accounts_called()); | 219 EXPECT_FALSE(update_accounts_called()); |
196 | 220 |
197 FinishAccountSignIn(kAccountId2); | 221 FinishAccountSignIn(kAccountId2); |
198 IssueAccessToken(kAccountId2); | 222 IssueAccessToken(kAccountId2); |
199 EXPECT_TRUE(update_accounts_called()); | 223 EXPECT_TRUE(update_accounts_called()); |
200 | 224 |
201 std::map<std::string, std::string> expected_accounts; | 225 std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
202 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); | 226 expected_accounts.push_back(MakeAccountToken(kAccountId1)); |
203 expected_accounts[kAccountId2] = MakeAccessToken(kAccountId2); | 227 expected_accounts.push_back(MakeAccountToken(kAccountId2)); |
204 EXPECT_EQ(expected_accounts, accounts()); | 228 VerifyAccountTokens(expected_accounts, accounts()); |
205 | 229 |
206 tracker()->Stop(); | 230 tracker()->Stop(); |
207 } | 231 } |
208 | 232 |
209 TEST_F(GCMAccountTrackerTest, AccountAdded) { | 233 TEST_F(GCMAccountTrackerTest, AccountAdded) { |
210 tracker()->Start(); | 234 tracker()->Start(); |
211 ResetResults(); | 235 ResetResults(); |
212 | 236 |
213 SignInAccount(kAccountId1); | 237 SignInAccount(kAccountId1); |
214 EXPECT_FALSE(update_accounts_called()); | 238 EXPECT_FALSE(update_accounts_called()); |
215 | 239 |
216 IssueAccessToken(kAccountId1); | 240 IssueAccessToken(kAccountId1); |
217 EXPECT_TRUE(update_accounts_called()); | 241 EXPECT_TRUE(update_accounts_called()); |
218 | 242 |
219 std::map<std::string, std::string> expected_accounts; | 243 std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
220 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); | 244 expected_accounts.push_back(MakeAccountToken(kAccountId1)); |
221 EXPECT_EQ(expected_accounts, accounts()); | 245 VerifyAccountTokens(expected_accounts, accounts()); |
222 | 246 |
223 tracker()->Stop(); | 247 tracker()->Stop(); |
224 } | 248 } |
225 | 249 |
226 TEST_F(GCMAccountTrackerTest, AccountRemoved) { | 250 TEST_F(GCMAccountTrackerTest, AccountRemoved) { |
227 SignInAccount(kAccountId1); | 251 SignInAccount(kAccountId1); |
228 SignInAccount(kAccountId2); | 252 SignInAccount(kAccountId2); |
229 | 253 |
230 tracker()->Start(); | 254 tracker()->Start(); |
231 IssueAccessToken(kAccountId1); | 255 IssueAccessToken(kAccountId1); |
232 IssueAccessToken(kAccountId2); | 256 IssueAccessToken(kAccountId2); |
233 EXPECT_TRUE(update_accounts_called()); | 257 EXPECT_TRUE(update_accounts_called()); |
234 | 258 |
235 ResetResults(); | 259 ResetResults(); |
236 EXPECT_FALSE(update_accounts_called()); | 260 EXPECT_FALSE(update_accounts_called()); |
237 | 261 |
238 SignOutAccount(kAccountId2); | 262 SignOutAccount(kAccountId2); |
239 EXPECT_TRUE(update_accounts_called()); | 263 EXPECT_TRUE(update_accounts_called()); |
240 | 264 |
241 std::map<std::string, std::string> expected_accounts; | 265 std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
242 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); | 266 expected_accounts.push_back(MakeAccountToken(kAccountId1)); |
243 EXPECT_EQ(expected_accounts, accounts()); | 267 VerifyAccountTokens(expected_accounts, accounts()); |
244 | 268 |
245 tracker()->Stop(); | 269 tracker()->Stop(); |
246 } | 270 } |
247 | 271 |
248 TEST_F(GCMAccountTrackerTest, GetTokenFailed) { | 272 TEST_F(GCMAccountTrackerTest, GetTokenFailed) { |
249 SignInAccount(kAccountId1); | 273 SignInAccount(kAccountId1); |
250 SignInAccount(kAccountId2); | 274 SignInAccount(kAccountId2); |
251 | 275 |
252 tracker()->Start(); | 276 tracker()->Start(); |
253 IssueAccessToken(kAccountId1); | 277 IssueAccessToken(kAccountId1); |
254 EXPECT_FALSE(update_accounts_called()); | 278 EXPECT_FALSE(update_accounts_called()); |
255 | 279 |
256 IssueError(kAccountId2); | 280 IssueError(kAccountId2); |
257 EXPECT_TRUE(update_accounts_called()); | 281 EXPECT_TRUE(update_accounts_called()); |
258 | 282 |
259 std::map<std::string, std::string> expected_accounts; | 283 std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
260 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); | 284 expected_accounts.push_back(MakeAccountToken(kAccountId1)); |
261 EXPECT_EQ(expected_accounts, accounts()); | 285 VerifyAccountTokens(expected_accounts, accounts()); |
262 | 286 |
263 tracker()->Stop(); | 287 tracker()->Stop(); |
264 } | 288 } |
265 | 289 |
266 TEST_F(GCMAccountTrackerTest, GetTokenFailedAccountRemoved) { | 290 TEST_F(GCMAccountTrackerTest, GetTokenFailedAccountRemoved) { |
267 SignInAccount(kAccountId1); | 291 SignInAccount(kAccountId1); |
268 SignInAccount(kAccountId2); | 292 SignInAccount(kAccountId2); |
269 | 293 |
270 tracker()->Start(); | 294 tracker()->Start(); |
271 IssueAccessToken(kAccountId1); | 295 IssueAccessToken(kAccountId1); |
272 IssueError(kAccountId2); | 296 IssueError(kAccountId2); |
273 | 297 |
274 ResetResults(); | 298 ResetResults(); |
275 SignOutAccount(kAccountId2); | 299 SignOutAccount(kAccountId2); |
276 EXPECT_TRUE(update_accounts_called()); | 300 EXPECT_TRUE(update_accounts_called()); |
277 | 301 |
278 std::map<std::string, std::string> expected_accounts; | 302 std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
279 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); | 303 expected_accounts.push_back(MakeAccountToken(kAccountId1)); |
280 EXPECT_EQ(expected_accounts, accounts()); | 304 VerifyAccountTokens(expected_accounts, accounts()); |
281 | 305 |
282 tracker()->Stop(); | 306 tracker()->Stop(); |
283 } | 307 } |
284 | 308 |
285 TEST_F(GCMAccountTrackerTest, AccountRemovedWhileRequestsPending) { | 309 TEST_F(GCMAccountTrackerTest, AccountRemovedWhileRequestsPending) { |
286 SignInAccount(kAccountId1); | 310 SignInAccount(kAccountId1); |
287 SignInAccount(kAccountId2); | 311 SignInAccount(kAccountId2); |
288 | 312 |
289 tracker()->Start(); | 313 tracker()->Start(); |
290 IssueAccessToken(kAccountId1); | 314 IssueAccessToken(kAccountId1); |
291 EXPECT_FALSE(update_accounts_called()); | 315 EXPECT_FALSE(update_accounts_called()); |
292 | 316 |
293 SignOutAccount(kAccountId2); | 317 SignOutAccount(kAccountId2); |
294 IssueAccessToken(kAccountId2); | 318 IssueAccessToken(kAccountId2); |
295 EXPECT_TRUE(update_accounts_called()); | 319 EXPECT_TRUE(update_accounts_called()); |
296 | 320 |
297 std::map<std::string, std::string> expected_accounts; | 321 std::vector<GCMClient::AccountTokenInfo> expected_accounts; |
298 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); | 322 expected_accounts.push_back(MakeAccountToken(kAccountId1)); |
299 EXPECT_EQ(expected_accounts, accounts()); | 323 VerifyAccountTokens(expected_accounts, accounts()); |
300 | 324 |
301 tracker()->Stop(); | 325 tracker()->Stop(); |
302 } | 326 } |
303 | 327 |
304 // TODO(fgorski): Add test for adding account after removal >> make sure it does | 328 // TODO(fgorski): Add test for adding account after removal >> make sure it does |
305 // not mark removal. | 329 // not mark removal. |
306 | 330 |
307 } // namespace gcm | 331 } // namespace gcm |
OLD | NEW |