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/fake_profile_oauth2_token_service.h"
7 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
8 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
9 #include "chrome/browser/sync/profile_sync_auth_provider.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "components/signin/core/browser/profile_oauth2_token_service.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "google_apis/gaia/gaia_constants.h"
14 #include "google_apis/gaia/google_service_auth_error.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 const char kAccountId
[] = "account.id";
19 class ProfileSyncAuthProviderTest
: public ::testing::Test
{
21 ProfileSyncAuthProviderTest() {}
23 virtual ~ProfileSyncAuthProviderTest() {}
25 virtual void SetUp() override
{
26 TestingProfile::Builder builder
;
27 builder
.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
28 &BuildAutoIssuingFakeProfileOAuth2TokenService
);
30 profile_
= builder
.Build();
32 FakeProfileOAuth2TokenService
* token_service
=
33 (FakeProfileOAuth2TokenService
*)
34 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
.get());
35 token_service
->IssueRefreshTokenForUser(kAccountId
, "fake_refresh_token");
37 auth_provider_frontend_
.reset(new ProfileSyncAuthProvider(
38 token_service
, kAccountId
, GaiaConstants::kChromeSyncOAuth2Scope
));
39 auth_provider_backend_
=
40 auth_provider_frontend_
->CreateProviderForSyncThread().Pass();
43 void RequestTokenFinished(const GoogleServiceAuthError
& error
,
44 const std::string
& token
) {
45 issued_tokens_
.push_back(token
);
46 request_token_errors_
.push_back(error
);
50 content::TestBrowserThreadBundle thread_bundle_
;
51 scoped_ptr
<Profile
> profile_
;
53 scoped_ptr
<ProfileSyncAuthProvider
> auth_provider_frontend_
;
54 scoped_ptr
<syncer::SyncAuthProvider
> auth_provider_backend_
;
56 std::vector
<std::string
> issued_tokens_
;
57 std::vector
<GoogleServiceAuthError
> request_token_errors_
;
60 TEST_F(ProfileSyncAuthProviderTest
, RequestToken
) {
61 // Request access token, make sure it gets valid response.
62 auth_provider_backend_
->RequestAccessToken(
63 base::Bind(&ProfileSyncAuthProviderTest::RequestTokenFinished
,
64 base::Unretained(this)));
65 base::RunLoop run_loop
;
66 run_loop
.RunUntilIdle();
67 EXPECT_EQ(1U, request_token_errors_
.size());
68 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_
[0]);
69 EXPECT_NE("", issued_tokens_
[0]);
72 TEST_F(ProfileSyncAuthProviderTest
, RequestTokenTwoConcurrentRequests
) {
73 // Start two requests for access token. One should be reported as canceled,
74 // the other one should succeed.
75 auth_provider_backend_
->RequestAccessToken(
76 base::Bind(&ProfileSyncAuthProviderTest::RequestTokenFinished
,
77 base::Unretained(this)));
78 auth_provider_backend_
->RequestAccessToken(
79 base::Bind(&ProfileSyncAuthProviderTest::RequestTokenFinished
,
80 base::Unretained(this)));
81 base::RunLoop run_loop
;
82 run_loop
.RunUntilIdle();
83 EXPECT_EQ(2U, request_token_errors_
.size());
85 EXPECT_EQ("", issued_tokens_
[0]);
86 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED
,
87 request_token_errors_
[0].state());
89 EXPECT_NE("", issued_tokens_
[1]);
90 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_
[1]);