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 27 matching lines...) Expand all Loading... |
38 } | 38 } |
39 | 39 |
40 } // namespace | 40 } // namespace |
41 | 41 |
42 class GCMAccountTrackerTest : public testing::Test { | 42 class GCMAccountTrackerTest : public testing::Test { |
43 public: | 43 public: |
44 GCMAccountTrackerTest(); | 44 GCMAccountTrackerTest(); |
45 virtual ~GCMAccountTrackerTest(); | 45 virtual ~GCMAccountTrackerTest(); |
46 | 46 |
47 // Callback for the account tracker. | 47 // Callback for the account tracker. |
48 void UpdateAccounts(const std::map<std::string, std::string>& accounts, | 48 void UpdateAccounts(const std::map<std::string, std::string>& accounts); |
49 bool account_removed); | |
50 | 49 |
51 // Helpers to pass fake events to the tracker. Tests should have either a pair | 50 // Helpers to pass fake events to the tracker. Tests should have either a pair |
52 // of Start/FinishAccountSignIn or SignInAccount per account. Don't mix. | 51 // of Start/FinishAccountSignIn or SignInAccount per account. Don't mix. |
53 // Call to SignOutAccount is not mandatory. | 52 // Call to SignOutAccount is not mandatory. |
54 void StartAccountSignIn(const std::string& account_key); | 53 void StartAccountSignIn(const std::string& account_key); |
55 void FinishAccountSignIn(const std::string& account_key); | 54 void FinishAccountSignIn(const std::string& account_key); |
56 void SignInAccount(const std::string& account_key); | 55 void SignInAccount(const std::string& account_key); |
57 void SignOutAccount(const std::string& account_key); | 56 void SignOutAccount(const std::string& account_key); |
58 | 57 |
59 // Helpers for dealing with OAuth2 access token requests. | 58 // Helpers for dealing with OAuth2 access token requests. |
60 void IssueAccessToken(const std::string& account_key); | 59 void IssueAccessToken(const std::string& account_key); |
61 void IssueError(const std::string& account_key); | 60 void IssueError(const std::string& account_key); |
62 | 61 |
63 // Test results and helpers. | 62 // Test results and helpers. |
64 void ResetResults(); | 63 void ResetResults(); |
65 bool update_accounts_called() const { return update_accounts_called_; } | 64 bool update_accounts_called() const { return update_accounts_called_; } |
66 bool account_removed() const { return account_removed_; } | |
67 const std::map<std::string, std::string>& accounts() const { | 65 const std::map<std::string, std::string>& accounts() const { |
68 return accounts_; | 66 return accounts_; |
69 } | 67 } |
70 | 68 |
71 // Accessor to account tracker. | 69 // Accessor to account tracker. |
72 GCMAccountTracker* tracker() { return tracker_.get(); } | 70 GCMAccountTracker* tracker() { return tracker_.get(); } |
73 | 71 |
74 private: | 72 private: |
75 std::map<std::string, std::string> accounts_; | 73 std::map<std::string, std::string> accounts_; |
76 bool update_accounts_called_; | 74 bool update_accounts_called_; |
77 bool account_removed_; | |
78 | 75 |
79 base::MessageLoop message_loop_; | 76 base::MessageLoop message_loop_; |
80 net::TestURLFetcherFactory test_fetcher_factory_; | 77 net::TestURLFetcherFactory test_fetcher_factory_; |
81 scoped_ptr<FakeOAuth2TokenService> fake_token_service_; | 78 scoped_ptr<FakeOAuth2TokenService> fake_token_service_; |
82 scoped_ptr<FakeIdentityProvider> fake_identity_provider_; | 79 scoped_ptr<FakeIdentityProvider> fake_identity_provider_; |
83 scoped_ptr<GCMAccountTracker> tracker_; | 80 scoped_ptr<GCMAccountTracker> tracker_; |
84 }; | 81 }; |
85 | 82 |
86 GCMAccountTrackerTest::GCMAccountTrackerTest() | 83 GCMAccountTrackerTest::GCMAccountTrackerTest() |
87 : update_accounts_called_(false), account_removed_(false) { | 84 : update_accounts_called_(false) { |
88 fake_token_service_.reset(new FakeOAuth2TokenService()); | 85 fake_token_service_.reset(new FakeOAuth2TokenService()); |
89 | 86 |
90 fake_identity_provider_.reset( | 87 fake_identity_provider_.reset( |
91 new FakeIdentityProvider(fake_token_service_.get())); | 88 new FakeIdentityProvider(fake_token_service_.get())); |
92 | 89 |
93 scoped_ptr<gaia::AccountTracker> gaia_account_tracker( | 90 scoped_ptr<gaia::AccountTracker> gaia_account_tracker( |
94 new gaia::AccountTracker(fake_identity_provider_.get(), | 91 new gaia::AccountTracker(fake_identity_provider_.get(), |
95 new net::TestURLRequestContextGetter( | 92 new net::TestURLRequestContextGetter( |
96 message_loop_.message_loop_proxy()))); | 93 message_loop_.message_loop_proxy()))); |
97 | 94 |
98 tracker_.reset(new GCMAccountTracker( | 95 tracker_.reset(new GCMAccountTracker( |
99 gaia_account_tracker.Pass(), | 96 gaia_account_tracker.Pass(), |
100 base::Bind(&GCMAccountTrackerTest::UpdateAccounts, | 97 base::Bind(&GCMAccountTrackerTest::UpdateAccounts, |
101 base::Unretained(this)))); | 98 base::Unretained(this)))); |
102 } | 99 } |
103 | 100 |
104 GCMAccountTrackerTest::~GCMAccountTrackerTest() { | 101 GCMAccountTrackerTest::~GCMAccountTrackerTest() { |
105 if (tracker_) | 102 if (tracker_) |
106 tracker_->Shutdown(); | 103 tracker_->Shutdown(); |
107 } | 104 } |
108 | 105 |
109 void GCMAccountTrackerTest::UpdateAccounts( | 106 void GCMAccountTrackerTest::UpdateAccounts( |
110 const std::map<std::string, std::string>& accounts, | 107 const std::map<std::string, std::string>& accounts) { |
111 bool account_removed) { | |
112 update_accounts_called_ = true; | 108 update_accounts_called_ = true; |
113 accounts_ = accounts; | 109 accounts_ = accounts; |
114 account_removed_ = account_removed; | |
115 } | 110 } |
116 | 111 |
117 void GCMAccountTrackerTest::ResetResults() { | 112 void GCMAccountTrackerTest::ResetResults() { |
118 accounts_.clear(); | 113 accounts_.clear(); |
119 update_accounts_called_ = false; | 114 update_accounts_called_ = false; |
120 account_removed_ = false; | |
121 } | 115 } |
122 | 116 |
123 void GCMAccountTrackerTest::StartAccountSignIn(const std::string& account_key) { | 117 void GCMAccountTrackerTest::StartAccountSignIn(const std::string& account_key) { |
124 fake_identity_provider_->LogIn(account_key); | 118 fake_identity_provider_->LogIn(account_key); |
125 fake_token_service_->AddAccount(account_key); | 119 fake_token_service_->AddAccount(account_key); |
126 } | 120 } |
127 | 121 |
128 void GCMAccountTrackerTest::FinishAccountSignIn( | 122 void GCMAccountTrackerTest::FinishAccountSignIn( |
129 const std::string& account_key) { | 123 const std::string& account_key) { |
130 IssueAccessToken(account_key); | 124 IssueAccessToken(account_key); |
(...skipping 21 matching lines...) Expand all Loading... |
152 } | 146 } |
153 | 147 |
154 void GCMAccountTrackerTest::IssueError(const std::string& account_key) { | 148 void GCMAccountTrackerTest::IssueError(const std::string& account_key) { |
155 fake_token_service_->IssueErrorForAllPendingRequestsForAccount( | 149 fake_token_service_->IssueErrorForAllPendingRequestsForAccount( |
156 account_key, | 150 account_key, |
157 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE)); | 151 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE)); |
158 } | 152 } |
159 | 153 |
160 TEST_F(GCMAccountTrackerTest, NoAccounts) { | 154 TEST_F(GCMAccountTrackerTest, NoAccounts) { |
161 EXPECT_FALSE(update_accounts_called()); | 155 EXPECT_FALSE(update_accounts_called()); |
162 EXPECT_FALSE(account_removed()); | |
163 tracker()->Start(); | 156 tracker()->Start(); |
164 EXPECT_TRUE(update_accounts_called()); | 157 // Callback should not be called if there where no accounts provided. |
165 EXPECT_FALSE(account_removed()); | 158 EXPECT_FALSE(update_accounts_called()); |
166 EXPECT_TRUE(accounts().empty()); | 159 EXPECT_TRUE(accounts().empty()); |
167 tracker()->Stop(); | 160 tracker()->Stop(); |
168 } | 161 } |
169 | 162 |
170 // Verifies that callback is called after a token is issued for a single account | 163 // Verifies that callback is called after a token is issued for a single account |
171 // with a specific scope. In this scenario, the underlying account tracker is | 164 // with a specific scope. In this scenario, the underlying account tracker is |
172 // still working when the CompleteCollectingTokens is called for the first time. | 165 // still working when the CompleteCollectingTokens is called for the first time. |
173 TEST_F(GCMAccountTrackerTest, SingleAccount) { | 166 TEST_F(GCMAccountTrackerTest, SingleAccount) { |
174 StartAccountSignIn(kAccountId1); | 167 StartAccountSignIn(kAccountId1); |
175 | 168 |
176 tracker()->Start(); | 169 tracker()->Start(); |
177 // We don't have any accounts to report, but given the inner account tracker | 170 // We don't have any accounts to report, but given the inner account tracker |
178 // is still working we don't make a call with empty accounts list. | 171 // is still working we don't make a call with empty accounts list. |
179 EXPECT_FALSE(update_accounts_called()); | 172 EXPECT_FALSE(update_accounts_called()); |
180 | 173 |
181 // This concludes the work of inner account tracker. | 174 // This concludes the work of inner account tracker. |
182 FinishAccountSignIn(kAccountId1); | 175 FinishAccountSignIn(kAccountId1); |
183 IssueAccessToken(kAccountId1); | 176 IssueAccessToken(kAccountId1); |
184 | 177 |
185 EXPECT_TRUE(update_accounts_called()); | 178 EXPECT_TRUE(update_accounts_called()); |
186 EXPECT_FALSE(account_removed()); | |
187 | 179 |
188 std::map<std::string, std::string> expected_accounts; | 180 std::map<std::string, std::string> expected_accounts; |
189 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); | 181 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); |
190 EXPECT_EQ(expected_accounts, accounts()); | 182 EXPECT_EQ(expected_accounts, accounts()); |
191 tracker()->Stop(); | 183 tracker()->Stop(); |
192 } | 184 } |
193 | 185 |
194 TEST_F(GCMAccountTrackerTest, MultipleAccounts) { | 186 TEST_F(GCMAccountTrackerTest, MultipleAccounts) { |
195 StartAccountSignIn(kAccountId1); | 187 StartAccountSignIn(kAccountId1); |
196 StartAccountSignIn(kAccountId2); | 188 StartAccountSignIn(kAccountId2); |
197 | 189 |
198 tracker()->Start(); | 190 tracker()->Start(); |
199 EXPECT_FALSE(update_accounts_called()); | 191 EXPECT_FALSE(update_accounts_called()); |
200 | 192 |
201 FinishAccountSignIn(kAccountId1); | 193 FinishAccountSignIn(kAccountId1); |
202 IssueAccessToken(kAccountId1); | 194 IssueAccessToken(kAccountId1); |
203 EXPECT_FALSE(update_accounts_called()); | 195 EXPECT_FALSE(update_accounts_called()); |
204 EXPECT_FALSE(account_removed()); | |
205 | 196 |
206 FinishAccountSignIn(kAccountId2); | 197 FinishAccountSignIn(kAccountId2); |
207 IssueAccessToken(kAccountId2); | 198 IssueAccessToken(kAccountId2); |
208 EXPECT_TRUE(update_accounts_called()); | 199 EXPECT_TRUE(update_accounts_called()); |
209 EXPECT_FALSE(account_removed()); | |
210 | 200 |
211 std::map<std::string, std::string> expected_accounts; | 201 std::map<std::string, std::string> expected_accounts; |
212 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); | 202 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); |
213 expected_accounts[kAccountId2] = MakeAccessToken(kAccountId2); | 203 expected_accounts[kAccountId2] = MakeAccessToken(kAccountId2); |
214 EXPECT_EQ(expected_accounts, accounts()); | 204 EXPECT_EQ(expected_accounts, accounts()); |
215 | 205 |
216 tracker()->Stop(); | 206 tracker()->Stop(); |
217 } | 207 } |
218 | 208 |
219 TEST_F(GCMAccountTrackerTest, AccountAdded) { | 209 TEST_F(GCMAccountTrackerTest, AccountAdded) { |
220 tracker()->Start(); | 210 tracker()->Start(); |
221 ResetResults(); | 211 ResetResults(); |
222 | 212 |
223 SignInAccount(kAccountId1); | 213 SignInAccount(kAccountId1); |
224 EXPECT_FALSE(update_accounts_called()); | 214 EXPECT_FALSE(update_accounts_called()); |
225 | 215 |
226 IssueAccessToken(kAccountId1); | 216 IssueAccessToken(kAccountId1); |
227 EXPECT_TRUE(update_accounts_called()); | 217 EXPECT_TRUE(update_accounts_called()); |
228 EXPECT_FALSE(account_removed()); | |
229 | 218 |
230 std::map<std::string, std::string> expected_accounts; | 219 std::map<std::string, std::string> expected_accounts; |
231 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); | 220 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); |
232 EXPECT_EQ(expected_accounts, accounts()); | 221 EXPECT_EQ(expected_accounts, accounts()); |
233 | 222 |
234 tracker()->Stop(); | 223 tracker()->Stop(); |
235 } | 224 } |
236 | 225 |
237 TEST_F(GCMAccountTrackerTest, AccountRemoved) { | 226 TEST_F(GCMAccountTrackerTest, AccountRemoved) { |
238 SignInAccount(kAccountId1); | 227 SignInAccount(kAccountId1); |
239 SignInAccount(kAccountId2); | 228 SignInAccount(kAccountId2); |
240 | 229 |
241 tracker()->Start(); | 230 tracker()->Start(); |
242 IssueAccessToken(kAccountId1); | 231 IssueAccessToken(kAccountId1); |
243 IssueAccessToken(kAccountId2); | 232 IssueAccessToken(kAccountId2); |
244 EXPECT_TRUE(update_accounts_called()); | 233 EXPECT_TRUE(update_accounts_called()); |
245 | 234 |
246 ResetResults(); | 235 ResetResults(); |
247 EXPECT_FALSE(update_accounts_called()); | 236 EXPECT_FALSE(update_accounts_called()); |
248 | 237 |
249 SignOutAccount(kAccountId2); | 238 SignOutAccount(kAccountId2); |
250 EXPECT_TRUE(update_accounts_called()); | 239 EXPECT_TRUE(update_accounts_called()); |
251 EXPECT_TRUE(account_removed()); | |
252 | 240 |
253 std::map<std::string, std::string> expected_accounts; | 241 std::map<std::string, std::string> expected_accounts; |
254 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); | 242 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); |
255 EXPECT_EQ(expected_accounts, accounts()); | 243 EXPECT_EQ(expected_accounts, accounts()); |
256 | 244 |
257 tracker()->Stop(); | 245 tracker()->Stop(); |
258 } | 246 } |
259 | 247 |
260 TEST_F(GCMAccountTrackerTest, GetTokenFailed) { | 248 TEST_F(GCMAccountTrackerTest, GetTokenFailed) { |
261 SignInAccount(kAccountId1); | 249 SignInAccount(kAccountId1); |
262 SignInAccount(kAccountId2); | 250 SignInAccount(kAccountId2); |
263 | 251 |
264 tracker()->Start(); | 252 tracker()->Start(); |
265 IssueAccessToken(kAccountId1); | 253 IssueAccessToken(kAccountId1); |
266 EXPECT_FALSE(update_accounts_called()); | 254 EXPECT_FALSE(update_accounts_called()); |
267 | 255 |
268 IssueError(kAccountId2); | 256 IssueError(kAccountId2); |
269 EXPECT_TRUE(update_accounts_called()); | 257 EXPECT_TRUE(update_accounts_called()); |
270 EXPECT_FALSE(account_removed()); | |
271 | 258 |
272 std::map<std::string, std::string> expected_accounts; | 259 std::map<std::string, std::string> expected_accounts; |
273 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); | 260 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); |
274 EXPECT_EQ(expected_accounts, accounts()); | 261 EXPECT_EQ(expected_accounts, accounts()); |
275 | 262 |
276 tracker()->Stop(); | 263 tracker()->Stop(); |
277 } | 264 } |
278 | 265 |
279 TEST_F(GCMAccountTrackerTest, GetTokenFailedAccountRemoved) { | 266 TEST_F(GCMAccountTrackerTest, GetTokenFailedAccountRemoved) { |
280 SignInAccount(kAccountId1); | 267 SignInAccount(kAccountId1); |
281 SignInAccount(kAccountId2); | 268 SignInAccount(kAccountId2); |
282 | 269 |
283 tracker()->Start(); | 270 tracker()->Start(); |
284 IssueAccessToken(kAccountId1); | 271 IssueAccessToken(kAccountId1); |
285 IssueError(kAccountId2); | 272 IssueError(kAccountId2); |
286 | 273 |
287 ResetResults(); | 274 ResetResults(); |
288 SignOutAccount(kAccountId2); | 275 SignOutAccount(kAccountId2); |
289 EXPECT_TRUE(update_accounts_called()); | 276 EXPECT_TRUE(update_accounts_called()); |
290 EXPECT_TRUE(account_removed()); | |
291 | 277 |
292 std::map<std::string, std::string> expected_accounts; | 278 std::map<std::string, std::string> expected_accounts; |
293 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); | 279 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); |
294 EXPECT_EQ(expected_accounts, accounts()); | 280 EXPECT_EQ(expected_accounts, accounts()); |
295 | 281 |
296 tracker()->Stop(); | 282 tracker()->Stop(); |
297 } | 283 } |
298 | 284 |
299 TEST_F(GCMAccountTrackerTest, AccountRemovedWhileRequestsPending) { | 285 TEST_F(GCMAccountTrackerTest, AccountRemovedWhileRequestsPending) { |
300 SignInAccount(kAccountId1); | 286 SignInAccount(kAccountId1); |
301 SignInAccount(kAccountId2); | 287 SignInAccount(kAccountId2); |
302 | 288 |
303 tracker()->Start(); | 289 tracker()->Start(); |
304 IssueAccessToken(kAccountId1); | 290 IssueAccessToken(kAccountId1); |
305 EXPECT_FALSE(update_accounts_called()); | 291 EXPECT_FALSE(update_accounts_called()); |
306 | 292 |
307 SignOutAccount(kAccountId2); | 293 SignOutAccount(kAccountId2); |
308 IssueAccessToken(kAccountId2); | 294 IssueAccessToken(kAccountId2); |
309 EXPECT_TRUE(update_accounts_called()); | 295 EXPECT_TRUE(update_accounts_called()); |
310 EXPECT_TRUE(account_removed()); | |
311 | 296 |
312 std::map<std::string, std::string> expected_accounts; | 297 std::map<std::string, std::string> expected_accounts; |
313 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); | 298 expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1); |
314 EXPECT_EQ(expected_accounts, accounts()); | 299 EXPECT_EQ(expected_accounts, accounts()); |
315 | 300 |
316 tracker()->Stop(); | 301 tracker()->Stop(); |
317 } | 302 } |
318 | 303 |
319 // TODO(fgorski): Add test for adding account after removal >> make sure it does | 304 // TODO(fgorski): Add test for adding account after removal >> make sure it does |
320 // not mark removal. | 305 // not mark removal. |
321 | 306 |
322 } // namespace gcm | 307 } // namespace gcm |
OLD | NEW |