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