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.
9 #include "base/location.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/prefs/pref_registry_simple.h"
14 #include "base/prefs/testing_pref_service.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/test/histogram_tester.h"
18 #include "base/thread_task_runner_handle.h"
19 #include "components/signin/core/browser/gaia_cookie_manager_service.h"
20 #include "components/signin/core/browser/test_signin_client.h"
21 #include "components/signin/core/common/signin_pref_names.h"
22 #include "google_apis/gaia/fake_oauth2_token_service.h"
23 #include "google_apis/gaia/gaia_constants.h"
24 #include "google_apis/gaia/gaia_urls.h"
25 #include "net/url_request/test_url_fetcher_factory.h"
26 #include "net/url_request/url_request_test_util.h"
27 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h"
32 class MockObserver
: public GaiaCookieManagerService::Observer
{
34 explicit MockObserver(GaiaCookieManagerService
* helper
) : helper_(helper
) {
35 helper_
->AddObserver(this);
38 ~MockObserver() { helper_
->RemoveObserver(this); }
40 MOCK_METHOD2(OnAddAccountToCookieCompleted
,
41 void(const std::string
&, const GoogleServiceAuthError
&));
42 MOCK_METHOD2(OnGaiaAccountsInCookieUpdated
,
43 void(const std::vector
<gaia::ListedAccount
>&,
44 const GoogleServiceAuthError
&));
46 GaiaCookieManagerService
* helper_
;
48 DISALLOW_COPY_AND_ASSIGN(MockObserver
);
51 // Counts number of InstrumentedGaiaCookieManagerService created.
52 // We can EXPECT_* to be zero at the end of our unit tests
53 // to make sure everything is properly deleted.
57 class InstrumentedGaiaCookieManagerService
: public GaiaCookieManagerService
{
59 InstrumentedGaiaCookieManagerService(
60 OAuth2TokenService
* token_service
,
61 SigninClient
* signin_client
)
62 : GaiaCookieManagerService(token_service
,
63 GaiaConstants::kChromeSource
,
68 virtual ~InstrumentedGaiaCookieManagerService() { total
--; }
70 MOCK_METHOD0(StartFetchingUbertoken
, void());
71 MOCK_METHOD0(StartFetchingListAccounts
, void());
72 MOCK_METHOD0(StartFetchingLogOut
, void());
73 MOCK_METHOD0(StartFetchingMergeSession
, void());
76 DISALLOW_COPY_AND_ASSIGN(InstrumentedGaiaCookieManagerService
);
79 class GaiaCookieManagerServiceTest
: public testing::Test
{
81 GaiaCookieManagerServiceTest()
82 : no_error_(GoogleServiceAuthError::NONE
),
83 error_(GoogleServiceAuthError::SERVICE_ERROR
),
84 canceled_(GoogleServiceAuthError::REQUEST_CANCELED
) {}
86 void SetUp() override
{
87 pref_service_
.registry()->RegisterIntegerPref(
88 prefs::kAccountIdMigrationState
,
89 AccountTrackerService::MIGRATION_NOT_STARTED
);
90 signin_client_
.reset(new TestSigninClient(&pref_service_
));
93 OAuth2TokenService
* token_service() { return &token_service_
; }
94 TestSigninClient
* signin_client() { return signin_client_
.get(); }
96 void SimulateUbertokenSuccess(UbertokenConsumer
* consumer
,
97 const std::string
& uber_token
) {
98 consumer
->OnUbertokenSuccess(uber_token
);
101 void SimulateUbertokenFailure(UbertokenConsumer
* consumer
,
102 const GoogleServiceAuthError
& error
) {
103 consumer
->OnUbertokenFailure(error
);
106 void SimulateMergeSessionSuccess(GaiaAuthConsumer
* consumer
,
107 const std::string
& data
) {
108 consumer
->OnMergeSessionSuccess(data
);
111 void SimulateMergeSessionFailure(GaiaAuthConsumer
* consumer
,
112 const GoogleServiceAuthError
& error
) {
113 consumer
->OnMergeSessionFailure(error
);
116 void SimulateListAccountsSuccess(GaiaAuthConsumer
* consumer
,
117 const std::string
& data
) {
118 consumer
->OnListAccountsSuccess(data
);
121 void SimulateLogOutSuccess(GaiaAuthConsumer
* consumer
) {
122 consumer
->OnLogOutSuccess();
125 void SimulateLogOutFailure(GaiaAuthConsumer
* consumer
,
126 const GoogleServiceAuthError
& error
) {
127 consumer
->OnLogOutFailure(error
);
130 void SimulateGetCheckConnctionInfoSuccess(net::TestURLFetcher
* fetcher
,
131 const std::string
& data
) {
132 fetcher
->set_status(net::URLRequestStatus());
133 fetcher
->set_response_code(200);
134 fetcher
->SetResponseString(data
);
135 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
138 void SimulateGetCheckConnctionInfoResult(net::URLFetcher
* fetcher
,
139 const std::string
& result
) {
140 net::TestURLFetcher
* test_fetcher
=
141 static_cast<net::TestURLFetcher
*>(fetcher
);
142 test_fetcher
->set_status(net::URLRequestStatus());
143 test_fetcher
->set_response_code(200);
144 test_fetcher
->SetResponseString(result
);
145 test_fetcher
->delegate()->OnURLFetchComplete(fetcher
);
148 const GoogleServiceAuthError
& no_error() { return no_error_
; }
149 const GoogleServiceAuthError
& error() { return error_
; }
150 const GoogleServiceAuthError
& canceled() { return canceled_
; }
152 net::TestURLFetcherFactory
* factory() { return &factory_
; }
155 base::MessageLoop message_loop_
;
156 net::TestURLFetcherFactory factory_
;
157 FakeOAuth2TokenService token_service_
;
158 GoogleServiceAuthError no_error_
;
159 GoogleServiceAuthError error_
;
160 GoogleServiceAuthError canceled_
;
161 TestingPrefServiceSimple pref_service_
;
162 scoped_ptr
<TestSigninClient
> signin_client_
;
169 TEST_F(GaiaCookieManagerServiceTest
, Success
) {
170 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
171 MockObserver
observer(&helper
);
173 EXPECT_CALL(helper
, StartFetchingUbertoken());
174 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc1@gmail.com",
177 helper
.AddAccountToCookie("acc1@gmail.com");
178 SimulateMergeSessionSuccess(&helper
, "token");
181 TEST_F(GaiaCookieManagerServiceTest
, FailedMergeSession
) {
182 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
183 MockObserver
observer(&helper
);
184 base::HistogramTester histograms
;
186 EXPECT_CALL(helper
, StartFetchingUbertoken());
187 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc1@gmail.com",
190 helper
.AddAccountToCookie("acc1@gmail.com");
191 SimulateMergeSessionFailure(&helper
, error());
192 // Persistent error incurs no further retries.
193 DCHECK(!helper
.is_running());
194 histograms
.ExpectUniqueSample("OAuth2Login.MergeSessionFailure",
195 GoogleServiceAuthError::SERVICE_ERROR
, 1);
198 TEST_F(GaiaCookieManagerServiceTest
, AddAccountCookiesDisabled
) {
199 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
200 MockObserver
observer(&helper
);
201 signin_client()->set_are_signin_cookies_allowed(false);
203 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc1@gmail.com",
206 helper
.AddAccountToCookie("acc1@gmail.com");
209 TEST_F(GaiaCookieManagerServiceTest
, MergeSessionRetried
) {
210 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
211 MockObserver
observer(&helper
);
213 EXPECT_CALL(helper
, StartFetchingUbertoken());
214 EXPECT_CALL(helper
, StartFetchingMergeSession());
215 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc1@gmail.com",
218 helper
.AddAccountToCookie("acc1@gmail.com");
219 SimulateMergeSessionFailure(&helper
, canceled());
220 DCHECK(helper
.is_running());
221 // Transient error incurs a retry after 1 second.
222 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
223 FROM_HERE
, base::MessageLoop::QuitClosure(),
224 base::TimeDelta::FromMilliseconds(1100));
225 base::MessageLoop::current()->Run();
226 SimulateMergeSessionSuccess(&helper
, "token");
227 DCHECK(!helper
.is_running());
230 TEST_F(GaiaCookieManagerServiceTest
, MergeSessionRetriedTwice
) {
231 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
232 MockObserver
observer(&helper
);
233 base::HistogramTester histograms
;
235 EXPECT_CALL(helper
, StartFetchingUbertoken());
236 EXPECT_CALL(helper
, StartFetchingMergeSession()).Times(2);
237 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc1@gmail.com",
240 helper
.AddAccountToCookie("acc1@gmail.com");
241 SimulateMergeSessionFailure(&helper
, canceled());
242 DCHECK(helper
.is_running());
243 // Transient error incurs a retry after 1 second.
244 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
245 FROM_HERE
, base::MessageLoop::QuitClosure(),
246 base::TimeDelta::FromMilliseconds(1100));
247 base::MessageLoop::current()->Run();
248 SimulateMergeSessionFailure(&helper
, canceled());
249 DCHECK(helper
.is_running());
250 // Next transient error incurs a retry after 3 seconds.
251 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
252 FROM_HERE
, base::MessageLoop::QuitClosure(),
253 base::TimeDelta::FromMilliseconds(3100));
254 base::MessageLoop::current()->Run();
255 SimulateMergeSessionSuccess(&helper
, "token");
256 DCHECK(!helper
.is_running());
257 histograms
.ExpectUniqueSample("OAuth2Login.MergeSessionRetry",
258 GoogleServiceAuthError::REQUEST_CANCELED
, 2);
261 TEST_F(GaiaCookieManagerServiceTest
, FailedUbertoken
) {
262 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
263 MockObserver
observer(&helper
);
265 EXPECT_CALL(helper
, StartFetchingUbertoken());
266 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc1@gmail.com",
269 helper
.AddAccountToCookie("acc1@gmail.com");
270 SimulateUbertokenFailure(&helper
, error());
273 TEST_F(GaiaCookieManagerServiceTest
, ContinueAfterSuccess
) {
274 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
275 MockObserver
observer(&helper
);
277 EXPECT_CALL(helper
, StartFetchingUbertoken()).Times(2);
278 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc1@gmail.com",
280 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc2@gmail.com",
283 helper
.AddAccountToCookie("acc1@gmail.com");
284 helper
.AddAccountToCookie("acc2@gmail.com");
285 SimulateMergeSessionSuccess(&helper
, "token1");
286 SimulateMergeSessionSuccess(&helper
, "token2");
289 TEST_F(GaiaCookieManagerServiceTest
, ContinueAfterFailure1
) {
290 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
291 MockObserver
observer(&helper
);
293 EXPECT_CALL(helper
, StartFetchingUbertoken()).Times(2);
294 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc1@gmail.com",
296 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc2@gmail.com",
299 helper
.AddAccountToCookie("acc1@gmail.com");
300 helper
.AddAccountToCookie("acc2@gmail.com");
301 SimulateMergeSessionFailure(&helper
, error());
302 SimulateMergeSessionSuccess(&helper
, "token2");
305 TEST_F(GaiaCookieManagerServiceTest
, ContinueAfterFailure2
) {
306 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
307 MockObserver
observer(&helper
);
309 EXPECT_CALL(helper
, StartFetchingUbertoken()).Times(2);
310 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc1@gmail.com",
312 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc2@gmail.com",
315 helper
.AddAccountToCookie("acc1@gmail.com");
316 helper
.AddAccountToCookie("acc2@gmail.com");
317 SimulateUbertokenFailure(&helper
, error());
318 SimulateMergeSessionSuccess(&helper
, "token2");
321 TEST_F(GaiaCookieManagerServiceTest
, AllRequestsInMultipleGoes
) {
322 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
323 MockObserver
observer(&helper
);
325 EXPECT_CALL(helper
, StartFetchingUbertoken()).Times(4);
326 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted(_
, no_error())).Times(4);
328 helper
.AddAccountToCookie("acc1@gmail.com");
329 helper
.AddAccountToCookie("acc2@gmail.com");
331 SimulateMergeSessionSuccess(&helper
, "token1");
333 helper
.AddAccountToCookie("acc3@gmail.com");
335 SimulateMergeSessionSuccess(&helper
, "token2");
336 SimulateMergeSessionSuccess(&helper
, "token3");
338 helper
.AddAccountToCookie("acc4@gmail.com");
340 SimulateMergeSessionSuccess(&helper
, "token4");
343 TEST_F(GaiaCookieManagerServiceTest
, LogOutAllAccountsNoQueue
) {
344 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
345 MockObserver
observer(&helper
);
347 EXPECT_CALL(helper
, StartFetchingUbertoken());
348 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc2@gmail.com",
350 EXPECT_CALL(helper
, StartFetchingLogOut());
352 helper
.AddAccountToCookie("acc2@gmail.com");
353 SimulateMergeSessionSuccess(&helper
, "token1");
355 helper
.LogOutAllAccounts();
356 SimulateLogOutSuccess(&helper
);
357 ASSERT_FALSE(helper
.is_running());
360 TEST_F(GaiaCookieManagerServiceTest
, LogOutAllAccountsFails
) {
361 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
362 MockObserver
observer(&helper
);
364 EXPECT_CALL(helper
, StartFetchingUbertoken());
365 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc2@gmail.com",
367 EXPECT_CALL(helper
, StartFetchingLogOut());
369 helper
.AddAccountToCookie("acc2@gmail.com");
370 SimulateMergeSessionSuccess(&helper
, "token1");
372 helper
.LogOutAllAccounts();
373 SimulateLogOutFailure(&helper
, error());
374 // CookieManagerService is still running; it is retrying the failed logout.
375 ASSERT_TRUE(helper
.is_running());
378 TEST_F(GaiaCookieManagerServiceTest
, LogOutAllAccountsAfterOneAddInQueue
) {
379 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
380 MockObserver
observer(&helper
);
382 EXPECT_CALL(helper
, StartFetchingUbertoken());
383 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc2@gmail.com",
385 EXPECT_CALL(helper
, StartFetchingLogOut());
387 helper
.AddAccountToCookie("acc2@gmail.com");
388 helper
.LogOutAllAccounts();
390 SimulateMergeSessionSuccess(&helper
, "token1");
391 SimulateLogOutSuccess(&helper
);
394 TEST_F(GaiaCookieManagerServiceTest
, LogOutAllAccountsAfterTwoAddsInQueue
) {
395 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
396 MockObserver
observer(&helper
);
398 EXPECT_CALL(helper
, StartFetchingUbertoken());
399 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc1@gmail.com",
401 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc2@gmail.com",
403 EXPECT_CALL(helper
, StartFetchingLogOut());
405 helper
.AddAccountToCookie("acc1@gmail.com");
406 // The Log Out should prevent this AddAccount from being fetched.
407 helper
.AddAccountToCookie("acc2@gmail.com");
408 helper
.LogOutAllAccounts();
410 SimulateMergeSessionSuccess(&helper
, "token1");
411 SimulateLogOutSuccess(&helper
);
414 TEST_F(GaiaCookieManagerServiceTest
, LogOutAllAccountsTwice
) {
415 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
416 MockObserver
observer(&helper
);
418 EXPECT_CALL(helper
, StartFetchingUbertoken());
419 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc2@gmail.com",
421 EXPECT_CALL(helper
, StartFetchingLogOut());
423 helper
.AddAccountToCookie("acc2@gmail.com");
424 SimulateMergeSessionSuccess(&helper
, "token1");
426 helper
.LogOutAllAccounts();
427 // Only one LogOut will be fetched.
428 helper
.LogOutAllAccounts();
429 SimulateLogOutSuccess(&helper
);
432 TEST_F(GaiaCookieManagerServiceTest
, LogOutAllAccountsBeforeAdd
) {
433 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
434 MockObserver
observer(&helper
);
436 EXPECT_CALL(helper
, StartFetchingUbertoken()).Times(2);
437 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc2@gmail.com",
439 EXPECT_CALL(helper
, StartFetchingLogOut());
440 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc3@gmail.com",
442 helper
.AddAccountToCookie("acc2@gmail.com");
443 SimulateMergeSessionSuccess(&helper
, "token1");
445 helper
.LogOutAllAccounts();
446 helper
.AddAccountToCookie("acc3@gmail.com");
448 SimulateLogOutSuccess(&helper
);
449 // After LogOut the MergeSession should be fetched.
450 SimulateMergeSessionSuccess(&helper
, "token2");
453 TEST_F(GaiaCookieManagerServiceTest
, LogOutAllAccountsBeforeLogoutAndAdd
) {
454 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
455 MockObserver
observer(&helper
);
458 EXPECT_CALL(helper
, StartFetchingUbertoken()).Times(2);
459 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc2@gmail.com",
461 EXPECT_CALL(helper
, StartFetchingLogOut());
462 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc3@gmail.com",
465 helper
.AddAccountToCookie("acc2@gmail.com");
466 SimulateMergeSessionSuccess(&helper
, "token1");
468 helper
.LogOutAllAccounts();
469 // Second LogOut will never be fetched.
470 helper
.LogOutAllAccounts();
471 helper
.AddAccountToCookie("acc3@gmail.com");
473 SimulateLogOutSuccess(&helper
);
474 // After LogOut the MergeSession should be fetched.
475 SimulateMergeSessionSuccess(&helper
, "token2");
478 TEST_F(GaiaCookieManagerServiceTest
, PendingSigninThenSignout
) {
479 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
480 MockObserver
observer(&helper
);
482 // From the first Signin.
483 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc1@gmail.com",
486 // From the sign out and then re-sign in.
487 EXPECT_CALL(helper
, StartFetchingLogOut());
488 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc3@gmail.com",
491 // Total sign in 2 times, not enforcing ordered sequences.
492 EXPECT_CALL(helper
, StartFetchingUbertoken()).Times(2);
494 helper
.AddAccountToCookie("acc1@gmail.com");
495 helper
.LogOutAllAccounts();
497 SimulateMergeSessionSuccess(&helper
, "token1");
498 SimulateLogOutSuccess(&helper
);
500 helper
.AddAccountToCookie("acc3@gmail.com");
501 SimulateMergeSessionSuccess(&helper
, "token3");
504 TEST_F(GaiaCookieManagerServiceTest
, CancelSignIn
) {
505 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
506 MockObserver
observer(&helper
);
508 EXPECT_CALL(helper
, StartFetchingUbertoken());
509 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc2@gmail.com",
511 EXPECT_CALL(observer
, OnAddAccountToCookieCompleted("acc1@gmail.com",
513 EXPECT_CALL(helper
, StartFetchingLogOut());
515 helper
.AddAccountToCookie("acc1@gmail.com");
516 helper
.AddAccountToCookie("acc2@gmail.com");
517 helper
.LogOutAllAccounts();
519 SimulateMergeSessionSuccess(&helper
, "token1");
520 SimulateLogOutSuccess(&helper
);
523 TEST_F(GaiaCookieManagerServiceTest
, ListAccountsFirstReturnsEmpty
) {
524 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
525 MockObserver
observer(&helper
);
527 std::vector
<gaia::ListedAccount
> list_accounts
;
529 EXPECT_CALL(helper
, StartFetchingListAccounts());
531 ASSERT_FALSE(helper
.ListAccounts(&list_accounts
));
532 ASSERT_TRUE(list_accounts
.empty());
535 TEST_F(GaiaCookieManagerServiceTest
, ListAccountsFindsOneAccount
) {
536 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
537 MockObserver
observer(&helper
);
539 std::vector
<gaia::ListedAccount
> list_accounts
;
540 std::vector
<gaia::ListedAccount
> expected_accounts
;
541 gaia::ListedAccount listed_account
;
542 listed_account
.email
= "a@b.com";
543 listed_account
.raw_email
= "a@b.com";
544 listed_account
.gaia_id
= "8";
545 listed_account
.valid
= true;
546 expected_accounts
.push_back(listed_account
);
548 EXPECT_CALL(helper
, StartFetchingListAccounts());
549 EXPECT_CALL(observer
, OnGaiaAccountsInCookieUpdated(expected_accounts
,
552 ASSERT_FALSE(helper
.ListAccounts(&list_accounts
));
554 SimulateListAccountsSuccess(&helper
,
555 "[\"f\", [[\"b\", 0, \"n\", \"a@b.com\", \"p\", 0, 0, 0, 0, 1, \"8\"]]]");
558 TEST_F(GaiaCookieManagerServiceTest
, ExternalCcResultFetcher
) {
559 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
560 GaiaCookieManagerService::ExternalCcResultFetcher
result_fetcher(&helper
);
561 EXPECT_CALL(helper
, StartFetchingMergeSession());
562 result_fetcher
.Start();
564 // Simulate a successful completion of GetCheckConnctionInfo.
565 net::TestURLFetcher
* fetcher
= factory()->GetFetcherByID(0);
566 ASSERT_TRUE(NULL
!= fetcher
);
567 SimulateGetCheckConnctionInfoSuccess(
569 "[{\"carryBackToken\": \"yt\", \"url\": \"http://www.yt.com\"},"
570 " {\"carryBackToken\": \"bl\", \"url\": \"http://www.bl.com\"}]");
572 // Simulate responses for the two connection URLs.
573 GaiaCookieManagerService::ExternalCcResultFetcher::URLToTokenAndFetcher
574 fetchers
= result_fetcher
.get_fetcher_map_for_testing();
575 ASSERT_EQ(2u, fetchers
.size());
576 ASSERT_EQ(1u, fetchers
.count(GURL("http://www.yt.com")));
577 ASSERT_EQ(1u, fetchers
.count(GURL("http://www.bl.com")));
579 ASSERT_EQ("bl:null,yt:null", result_fetcher
.GetExternalCcResult());
580 SimulateGetCheckConnctionInfoResult(
581 fetchers
[GURL("http://www.yt.com")].second
, "yt_result");
582 ASSERT_EQ("bl:null,yt:yt_result", result_fetcher
.GetExternalCcResult());
583 SimulateGetCheckConnctionInfoResult(
584 fetchers
[GURL("http://www.bl.com")].second
, "bl_result");
585 ASSERT_EQ("bl:bl_result,yt:yt_result", result_fetcher
.GetExternalCcResult());
588 TEST_F(GaiaCookieManagerServiceTest
, ExternalCcResultFetcherTimeout
) {
589 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
590 GaiaCookieManagerService::ExternalCcResultFetcher
result_fetcher(&helper
);
591 EXPECT_CALL(helper
, StartFetchingMergeSession());
592 result_fetcher
.Start();
594 // Simulate a successful completion of GetCheckConnctionInfo.
595 net::TestURLFetcher
* fetcher
= factory()->GetFetcherByID(0);
596 ASSERT_TRUE(NULL
!= fetcher
);
597 SimulateGetCheckConnctionInfoSuccess(
599 "[{\"carryBackToken\": \"yt\", \"url\": \"http://www.yt.com\"},"
600 " {\"carryBackToken\": \"bl\", \"url\": \"http://www.bl.com\"}]");
602 GaiaCookieManagerService::ExternalCcResultFetcher::URLToTokenAndFetcher
603 fetchers
= result_fetcher
.get_fetcher_map_for_testing();
604 ASSERT_EQ(2u, fetchers
.size());
605 ASSERT_EQ(1u, fetchers
.count(GURL("http://www.yt.com")));
606 ASSERT_EQ(1u, fetchers
.count(GURL("http://www.bl.com")));
608 // Simulate response only for "yt".
609 ASSERT_EQ("bl:null,yt:null", result_fetcher
.GetExternalCcResult());
610 SimulateGetCheckConnctionInfoResult(
611 fetchers
[GURL("http://www.yt.com")].second
, "yt_result");
612 ASSERT_EQ("bl:null,yt:yt_result", result_fetcher
.GetExternalCcResult());
615 result_fetcher
.TimeoutForTests();
616 ASSERT_EQ("bl:null,yt:yt_result", result_fetcher
.GetExternalCcResult());
617 fetchers
= result_fetcher
.get_fetcher_map_for_testing();
618 ASSERT_EQ(0u, fetchers
.size());
621 TEST_F(GaiaCookieManagerServiceTest
, ExternalCcResultFetcherTruncate
) {
622 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
623 GaiaCookieManagerService::ExternalCcResultFetcher
result_fetcher(&helper
);
624 EXPECT_CALL(helper
, StartFetchingMergeSession());
625 result_fetcher
.Start();
627 // Simulate a successful completion of GetCheckConnctionInfo.
628 net::TestURLFetcher
* fetcher
= factory()->GetFetcherByID(0);
629 ASSERT_TRUE(NULL
!= fetcher
);
630 SimulateGetCheckConnctionInfoSuccess(
632 "[{\"carryBackToken\": \"yt\", \"url\": \"http://www.yt.com\"}]");
634 GaiaCookieManagerService::ExternalCcResultFetcher::URLToTokenAndFetcher
635 fetchers
= result_fetcher
.get_fetcher_map_for_testing();
636 ASSERT_EQ(1u, fetchers
.size());
637 ASSERT_EQ(1u, fetchers
.count(GURL("http://www.yt.com")));
639 // Simulate response for "yt" with a string that is too long.
640 SimulateGetCheckConnctionInfoResult(
641 fetchers
[GURL("http://www.yt.com")].second
, "1234567890123456trunc");
642 ASSERT_EQ("yt:1234567890123456", result_fetcher
.GetExternalCcResult());
645 TEST_F(GaiaCookieManagerServiceTest
, UbertokenSuccessFetchesExternalCC
) {
646 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
648 EXPECT_CALL(helper
, StartFetchingUbertoken());
649 helper
.AddAccountToCookie("acc1@gmail.com");
651 ASSERT_FALSE(factory()->GetFetcherByID(0));
652 SimulateUbertokenSuccess(&helper
, "token");
654 // Check there is now a fetcher that belongs to the ExternalCCResultFetcher.
655 net::TestURLFetcher
* fetcher
= factory()->GetFetcherByID(0);
656 ASSERT_TRUE(NULL
!= fetcher
);
657 SimulateGetCheckConnctionInfoSuccess(
659 "[{\"carryBackToken\": \"bl\", \"url\": \"http://www.bl.com\"}]");
660 GaiaCookieManagerService::ExternalCcResultFetcher
* result_fetcher
=
661 helper
.external_cc_result_fetcher_for_testing();
662 GaiaCookieManagerService::ExternalCcResultFetcher::URLToTokenAndFetcher
663 fetchers
= result_fetcher
->get_fetcher_map_for_testing();
664 ASSERT_EQ(1u, fetchers
.size());
665 ASSERT_EQ(1u, fetchers
.count(GURL("http://www.bl.com")));
668 TEST_F(GaiaCookieManagerServiceTest
, UbertokenSuccessFetchesExternalCCOnce
) {
669 InstrumentedGaiaCookieManagerService
helper(token_service(), signin_client());
671 helper
.external_cc_result_fetcher_for_testing()->Start();
673 EXPECT_CALL(helper
, StartFetchingUbertoken());
674 helper
.AddAccountToCookie("acc2@gmail.com");
675 // There is already a ExternalCCResultFetch underway. This will trigger
676 // StartFetchingMergeSession.
677 EXPECT_CALL(helper
, StartFetchingMergeSession());
678 SimulateUbertokenSuccess(&helper
, "token3");