OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/ui/app_list/speech_auth_helper.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/prefs/pref_service_syncable.h" |
| 12 #include "chrome/browser/signin/fake_profile_oauth2_token_service.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/signin_manager.h" |
| 20 #include "components/signin/core/browser/signin_manager_base.h" |
| 21 #include "content/public/test/test_browser_thread_bundle.h" |
| 22 #include "google_apis/gaia/google_service_auth_error.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 |
| 25 namespace app_list { |
| 26 |
| 27 static const char* kTestUser = "test.user@chromium.org.test"; |
| 28 static const char* kScope = "https://www.googleapis.com/auth/webhistory"; |
| 29 static const char* kAccessToken = "fake_access_token"; |
| 30 |
| 31 class SpeechAuthHelperTest : public testing::Test { |
| 32 public: |
| 33 SpeechAuthHelperTest() |
| 34 : testing_profile_manager_(new TestingProfileManager( |
| 35 TestingBrowserProcess::GetGlobal())) { |
| 36 } |
| 37 |
| 38 void SetUp() override { |
| 39 // Set up FakeProfileOAuth2TokenService. |
| 40 TestingProfile::TestingFactories factories; |
| 41 factories.push_back(std::make_pair( |
| 42 ProfileOAuth2TokenServiceFactory::GetInstance(), |
| 43 &BuildAutoIssuingFakeProfileOAuth2TokenService)); |
| 44 |
| 45 ASSERT_TRUE(testing_profile_manager_->SetUp()); |
| 46 profile_ = testing_profile_manager_->CreateTestingProfile( |
| 47 kTestUser, |
| 48 scoped_ptr<PrefServiceSyncable>(), |
| 49 base::UTF8ToUTF16(kTestUser), |
| 50 0, |
| 51 std::string(), |
| 52 factories); |
| 53 |
| 54 // Set up the authenticated user name and ID. |
| 55 SigninManagerFactory::GetForProfile(profile_)->SetAuthenticatedUsername( |
| 56 kTestUser); |
| 57 } |
| 58 |
| 59 protected: |
| 60 void SetupRefreshToken() { |
| 61 GetFakeProfileOAuth2TokenService()->IssueRefreshTokenForUser( |
| 62 kTestUser, "fake_refresh_token"); |
| 63 } |
| 64 |
| 65 FakeProfileOAuth2TokenService* GetFakeProfileOAuth2TokenService() { |
| 66 return static_cast<FakeProfileOAuth2TokenService*>( |
| 67 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)); |
| 68 } |
| 69 |
| 70 content::TestBrowserThreadBundle thread_bundle; |
| 71 scoped_ptr<TestingProfileManager> testing_profile_manager_; |
| 72 Profile* profile_; |
| 73 scoped_ptr<SpeechAuthHelper> auth_helper_; |
| 74 }; |
| 75 |
| 76 TEST_F(SpeechAuthHelperTest, TokenFetch) { |
| 77 SetupRefreshToken(); |
| 78 SpeechAuthHelper helper(profile_); |
| 79 EXPECT_TRUE(helper.GetToken().empty()); |
| 80 |
| 81 OAuth2TokenService::ScopeSet scopes; |
| 82 scopes.insert(kScope); |
| 83 GetFakeProfileOAuth2TokenService()->IssueTokenForScope(scopes, |
| 84 kAccessToken, |
| 85 base::Time::Max()); |
| 86 |
| 87 EXPECT_EQ(kAccessToken, helper.GetToken()); |
| 88 EXPECT_EQ(kScope, helper.GetScope()); |
| 89 } |
| 90 |
| 91 TEST_F(SpeechAuthHelperTest, TokenFetchDelayedRefreshToken) { |
| 92 SpeechAuthHelper helper(profile_); |
| 93 SetupRefreshToken(); |
| 94 EXPECT_TRUE(helper.GetToken().empty()); |
| 95 |
| 96 OAuth2TokenService::ScopeSet scopes; |
| 97 scopes.insert(kScope); |
| 98 GetFakeProfileOAuth2TokenService()->IssueTokenForScope(scopes, |
| 99 kAccessToken, |
| 100 base::Time::Max()); |
| 101 |
| 102 EXPECT_EQ(kAccessToken, helper.GetToken()); |
| 103 EXPECT_EQ(kScope, helper.GetScope()); |
| 104 } |
| 105 |
| 106 TEST_F(SpeechAuthHelperTest, TokenFetchFailed) { |
| 107 SetupRefreshToken(); |
| 108 SpeechAuthHelper helper(profile_); |
| 109 EXPECT_TRUE(helper.GetToken().empty()); |
| 110 |
| 111 OAuth2TokenService::ScopeSet scopes; |
| 112 scopes.insert(kScope); |
| 113 GetFakeProfileOAuth2TokenService()->IssueErrorForScope( |
| 114 scopes, GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR)); |
| 115 |
| 116 EXPECT_TRUE(helper.GetToken().empty()); |
| 117 EXPECT_EQ(kScope, helper.GetScope()); |
| 118 } |
| 119 |
| 120 } // namespace app_list |
OLD | NEW |