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.
5 #include "base/run_loop.h"
6 #include "chrome/browser/signin/account_fetcher_service_factory.h"
7 #include "chrome/browser/signin/chrome_signin_client_factory.h"
8 #include "chrome/browser/signin/fake_account_fetcher_service.h"
9 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
10 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
11 #include "chrome/browser/signin/test_signin_client_builder.h"
12 #include "chrome/browser/sync/profile_sync_auth_provider.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
15 #include "components/signin/core/browser/profile_oauth2_token_service.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "google_apis/gaia/gaia_constants.h"
18 #include "google_apis/gaia/google_service_auth_error.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 const char kAccountId
[] = "account.id";
23 class ProfileSyncAuthProviderTest
: public ::testing::Test
{
25 ProfileSyncAuthProviderTest() {}
27 ~ProfileSyncAuthProviderTest() override
{}
29 void SetUp() override
{
30 TestingProfile::Builder builder
;
31 builder
.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
32 &BuildAutoIssuingFakeProfileOAuth2TokenService
);
33 builder
.AddTestingFactory(AccountFetcherServiceFactory::GetInstance(),
34 FakeAccountFetcherService::BuildForTests
);
35 builder
.AddTestingFactory(ChromeSigninClientFactory::GetInstance(),
36 signin::BuildTestSigninClient
);
38 profile_
= builder
.Build();
40 FakeProfileOAuth2TokenService
* token_service
=
41 (FakeProfileOAuth2TokenService
*)
42 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
.get());
43 token_service
->UpdateCredentials(kAccountId
, "fake_refresh_token");
45 auth_provider_frontend_
.reset(new ProfileSyncAuthProvider(
46 token_service
, kAccountId
, GaiaConstants::kChromeSyncOAuth2Scope
));
47 auth_provider_backend_
=
48 auth_provider_frontend_
->CreateProviderForSyncThread().Pass();
51 void RequestTokenFinished(const GoogleServiceAuthError
& error
,
52 const std::string
& token
) {
53 issued_tokens_
.push_back(token
);
54 request_token_errors_
.push_back(error
);
58 content::TestBrowserThreadBundle thread_bundle_
;
59 scoped_ptr
<Profile
> profile_
;
61 scoped_ptr
<ProfileSyncAuthProvider
> auth_provider_frontend_
;
62 scoped_ptr
<syncer::SyncAuthProvider
> auth_provider_backend_
;
64 std::vector
<std::string
> issued_tokens_
;
65 std::vector
<GoogleServiceAuthError
> request_token_errors_
;
68 TEST_F(ProfileSyncAuthProviderTest
, RequestToken
) {
69 // Request access token, make sure it gets valid response.
70 auth_provider_backend_
->RequestAccessToken(
71 base::Bind(&ProfileSyncAuthProviderTest::RequestTokenFinished
,
72 base::Unretained(this)));
73 base::RunLoop run_loop
;
74 run_loop
.RunUntilIdle();
75 EXPECT_EQ(1U, request_token_errors_
.size());
76 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_
[0]);
77 EXPECT_NE("", issued_tokens_
[0]);
80 TEST_F(ProfileSyncAuthProviderTest
, RequestTokenTwoConcurrentRequests
) {
81 // Start two requests for access token. One should be reported as canceled,
82 // the other one should succeed.
83 auth_provider_backend_
->RequestAccessToken(
84 base::Bind(&ProfileSyncAuthProviderTest::RequestTokenFinished
,
85 base::Unretained(this)));
86 auth_provider_backend_
->RequestAccessToken(
87 base::Bind(&ProfileSyncAuthProviderTest::RequestTokenFinished
,
88 base::Unretained(this)));
89 base::RunLoop run_loop
;
90 run_loop
.RunUntilIdle();
91 EXPECT_EQ(2U, request_token_errors_
.size());
93 EXPECT_EQ("", issued_tokens_
[0]);
94 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED
,
95 request_token_errors_
[0].state());
97 EXPECT_NE("", issued_tokens_
[1]);
98 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_
[1]);