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_builder.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/test/base/testing_profile.h"
13 #include "components/signin/core/browser/fake_account_fetcher_service.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 "components/sync_driver/profile_sync_auth_provider.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "google_apis/gaia/gaia_constants.h"
19 #include "google_apis/gaia/google_service_auth_error.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 const char kAccountId
[] = "account.id";
24 class ProfileSyncAuthProviderTest
: public ::testing::Test
{
26 ProfileSyncAuthProviderTest() {}
28 ~ProfileSyncAuthProviderTest() override
{}
30 void SetUp() override
{
31 TestingProfile::Builder builder
;
32 builder
.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
33 &BuildAutoIssuingFakeProfileOAuth2TokenService
);
34 builder
.AddTestingFactory(AccountFetcherServiceFactory::GetInstance(),
35 FakeAccountFetcherServiceBuilder::BuildForTests
);
36 builder
.AddTestingFactory(ChromeSigninClientFactory::GetInstance(),
37 signin::BuildTestSigninClient
);
39 profile_
= builder
.Build();
41 FakeProfileOAuth2TokenService
* token_service
=
42 (FakeProfileOAuth2TokenService
*)
43 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
.get());
44 token_service
->UpdateCredentials(kAccountId
, "fake_refresh_token");
46 auth_provider_frontend_
.reset(new ProfileSyncAuthProvider(
47 token_service
, kAccountId
, GaiaConstants::kChromeSyncOAuth2Scope
));
48 auth_provider_backend_
=
49 auth_provider_frontend_
->CreateProviderForSyncThread().Pass();
52 void RequestTokenFinished(const GoogleServiceAuthError
& error
,
53 const std::string
& token
) {
54 issued_tokens_
.push_back(token
);
55 request_token_errors_
.push_back(error
);
59 content::TestBrowserThreadBundle thread_bundle_
;
60 scoped_ptr
<Profile
> profile_
;
62 scoped_ptr
<ProfileSyncAuthProvider
> auth_provider_frontend_
;
63 scoped_ptr
<syncer::SyncAuthProvider
> auth_provider_backend_
;
65 std::vector
<std::string
> issued_tokens_
;
66 std::vector
<GoogleServiceAuthError
> request_token_errors_
;
69 TEST_F(ProfileSyncAuthProviderTest
, RequestToken
) {
70 // Request access token, make sure it gets valid response.
71 auth_provider_backend_
->RequestAccessToken(
72 base::Bind(&ProfileSyncAuthProviderTest::RequestTokenFinished
,
73 base::Unretained(this)));
74 base::RunLoop run_loop
;
75 run_loop
.RunUntilIdle();
76 EXPECT_EQ(1U, request_token_errors_
.size());
77 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_
[0]);
78 EXPECT_NE("", issued_tokens_
[0]);
81 TEST_F(ProfileSyncAuthProviderTest
, RequestTokenTwoConcurrentRequests
) {
82 // Start two requests for access token. One should be reported as canceled,
83 // the other one should succeed.
84 auth_provider_backend_
->RequestAccessToken(
85 base::Bind(&ProfileSyncAuthProviderTest::RequestTokenFinished
,
86 base::Unretained(this)));
87 auth_provider_backend_
->RequestAccessToken(
88 base::Bind(&ProfileSyncAuthProviderTest::RequestTokenFinished
,
89 base::Unretained(this)));
90 base::RunLoop run_loop
;
91 run_loop
.RunUntilIdle();
92 EXPECT_EQ(2U, request_token_errors_
.size());
94 EXPECT_EQ("", issued_tokens_
[0]);
95 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED
,
96 request_token_errors_
[0].state());
98 EXPECT_NE("", issued_tokens_
[1]);
99 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_
[1]);