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 "chrome/browser/ui/app_list/speech_auth_helper.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/test/simple_test_clock.h"
12 #include "chrome/browser/prefs/pref_service_syncable.h"
13 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
15 #include "chrome/browser/signin/signin_manager_factory.h"
16 #include "chrome/test/base/testing_browser_process.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "chrome/test/base/testing_profile_manager.h"
19 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
20 #include "components/signin/core/browser/signin_manager.h"
21 #include "components/signin/core/browser/signin_manager_base.h"
22 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "google_apis/gaia/google_service_auth_error.h"
24 #include "testing/gtest/include/gtest/gtest.h"
28 static const char* kTestGaiaId
= "gaia_id";
29 static const char* kTestUser
= "test.user@chromium.org.test";
30 static const char* kScope
= "https://www.googleapis.com/auth/webhistory";
31 static const char* kAccessToken
= "fake_access_token";
33 class SpeechAuthHelperTest
: public testing::Test
{
35 SpeechAuthHelperTest()
36 : testing_profile_manager_(new TestingProfileManager(
37 TestingBrowserProcess::GetGlobal())) {
40 void SetUp() override
{
41 // Set up FakeProfileOAuth2TokenService.
42 TestingProfile::TestingFactories factories
;
43 factories
.push_back(std::make_pair(
44 ProfileOAuth2TokenServiceFactory::GetInstance(),
45 &BuildAutoIssuingFakeProfileOAuth2TokenService
));
47 ASSERT_TRUE(testing_profile_manager_
->SetUp());
48 profile_
= testing_profile_manager_
->CreateTestingProfile(
50 scoped_ptr
<PrefServiceSyncable
>(),
51 base::UTF8ToUTF16(kTestUser
),
56 // Set up the authenticated user name and ID.
57 SigninManagerFactory::GetForProfile(profile_
)->SetAuthenticatedAccountInfo(
58 kTestGaiaId
, kTestUser
);
62 void SetupRefreshToken() {
63 std::string account_id
= SigninManagerFactory::GetForProfile(profile_
)
64 ->GetAuthenticatedAccountId();
65 GetFakeProfileOAuth2TokenService()->UpdateCredentials(account_id
,
66 "fake_refresh_token");
69 FakeProfileOAuth2TokenService
* GetFakeProfileOAuth2TokenService() {
70 return static_cast<FakeProfileOAuth2TokenService
*>(
71 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
));
74 base::SimpleTestClock test_clock_
;
75 content::TestBrowserThreadBundle thread_bundle
;
76 scoped_ptr
<TestingProfileManager
> testing_profile_manager_
;
78 scoped_ptr
<SpeechAuthHelper
> auth_helper_
;
81 TEST_F(SpeechAuthHelperTest
, TokenFetch
) {
83 SpeechAuthHelper
helper(profile_
, &test_clock_
);
84 EXPECT_TRUE(helper
.GetToken().empty());
86 OAuth2TokenService::ScopeSet scopes
;
87 scopes
.insert(kScope
);
88 GetFakeProfileOAuth2TokenService()->IssueTokenForScope(scopes
,
92 EXPECT_EQ(kAccessToken
, helper
.GetToken());
93 EXPECT_EQ(kScope
, helper
.GetScope());
96 TEST_F(SpeechAuthHelperTest
, TokenFetchDelayedRefreshToken
) {
97 SpeechAuthHelper
helper(profile_
, &test_clock_
);
99 EXPECT_TRUE(helper
.GetToken().empty());
101 OAuth2TokenService::ScopeSet scopes
;
102 scopes
.insert(kScope
);
103 GetFakeProfileOAuth2TokenService()->IssueTokenForScope(scopes
,
107 EXPECT_EQ(kAccessToken
, helper
.GetToken());
108 EXPECT_EQ(kScope
, helper
.GetScope());
111 TEST_F(SpeechAuthHelperTest
, TokenFetchFailed
) {
113 SpeechAuthHelper
helper(profile_
, &test_clock_
);
114 EXPECT_TRUE(helper
.GetToken().empty());
116 OAuth2TokenService::ScopeSet scopes
;
117 scopes
.insert(kScope
);
118 GetFakeProfileOAuth2TokenService()->IssueErrorForScope(
119 scopes
, GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR
));
121 EXPECT_TRUE(helper
.GetToken().empty());
122 EXPECT_EQ(kScope
, helper
.GetScope());
125 } // namespace app_list