fake_profile_oauth2_token_service.h revision f2477e01787aa58f445919b809d89e252beef54f
1// Copyright 2013 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#ifndef CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_ 6#define CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_ 7 8#include <string> 9#include <vector> 10 11#include "base/compiler_specific.h" 12#include "base/memory/weak_ptr.h" 13 14#if defined(OS_ANDROID) 15#include "chrome/browser/signin/android_profile_oauth2_token_service.h" 16#else 17#include "chrome/browser/signin/profile_oauth2_token_service.h" 18#endif 19 20namespace content { 21class BrowserContext; 22} 23 24// Helper class to simplify writing unittests that depend on an instance of 25// ProfileOAuth2TokenService. 26// 27// Tests would typically do something like the following: 28// 29// FakeProfileOAuth2TokenService service; 30// ... 31// service.IssueRefreshToken("token"); // Issue refresh token/notify observers 32// ... 33// // Confirm that there is at least one active request. 34// EXPECT_GT(0U, service.GetPendingRequests().size()); 35// ... 36// // Make any pending token fetches for a given scope succeed. 37// ScopeSet scopes; 38// scopes.insert(GaiaConstants::kYourServiceScope); 39// IssueTokenForScope(scopes, "access_token", base::Time()::Max()); 40// ... 41// // ...or make them fail... 42// IssueErrorForScope(scopes, GoogleServiceAuthError(INVALID_GAIA_CREDENTIALS)); 43// 44class FakeProfileOAuth2TokenService 45#if defined(OS_ANDROID) 46 : public AndroidProfileOAuth2TokenService { 47#else 48 : public ProfileOAuth2TokenService { 49#endif 50 public: 51 struct PendingRequest { 52 PendingRequest(); 53 ~PendingRequest(); 54 55 std::string account_id; 56 std::string client_id; 57 std::string client_secret; 58 ScopeSet scopes; 59 base::WeakPtr<RequestImpl> request; 60 }; 61 62 FakeProfileOAuth2TokenService(); 63 virtual ~FakeProfileOAuth2TokenService(); 64 65 // Overriden to make sure it works on Android. 66 virtual bool RefreshTokenIsAvailable( 67 const std::string& account_id) OVERRIDE; 68 69 // Overriden to make sure it works on Android. Simply calls 70 // IssueRefreshToken(). 71 virtual void UpdateCredentials(const std::string& account_id, 72 const std::string& refresh_token) OVERRIDE; 73 74 // Sets the current refresh token. If |token| is non-empty, this will invoke 75 // OnRefreshTokenAvailable() on all Observers, otherwise this will invoke 76 // OnRefreshTokenRevoked(). 77 void IssueRefreshToken(const std::string& token); 78 79 // TODO(fgorski,rogerta): Merge with UpdateCredentials when this class fully 80 // supports multiple accounts. 81 void IssueRefreshTokenForUser(const std::string& account_id, 82 const std::string& token); 83 84 // Gets a list of active requests (can be used by tests to validate that the 85 // correct request has been issued). 86 std::vector<PendingRequest> GetPendingRequests(); 87 88 // Helper routines to issue tokens for pending requests. 89 // TODO(fgorski): Add account IDs as parameters. 90 void IssueTokenForScope(const ScopeSet& scopes, 91 const std::string& access_token, 92 const base::Time& expiration); 93 94 void IssueErrorForScope(const ScopeSet& scopes, 95 const GoogleServiceAuthError& error); 96 97 void IssueTokenForAllPendingRequests(const std::string& access_token, 98 const base::Time& expiration); 99 100 void IssueErrorForAllPendingRequests(const GoogleServiceAuthError& error); 101 102 // Helper function to be used with 103 // BrowserContextKeyedService::SetTestingFactory(). 104 static BrowserContextKeyedService* Build(content::BrowserContext* profile); 105 106 protected: 107 // OAuth2TokenService overrides. 108 virtual void FetchOAuth2Token(RequestImpl* request, 109 const std::string& account_id, 110 net::URLRequestContextGetter* getter, 111 const std::string& client_id, 112 const std::string& client_secret, 113 const ScopeSet& scopes) OVERRIDE; 114 115 virtual void InvalidateOAuth2Token(const std::string& account_id, 116 const std::string& client_id, 117 const ScopeSet& scopes, 118 const std::string& access_token) OVERRIDE; 119 120 virtual std::string GetRefreshToken(const std::string& account_id) OVERRIDE; 121 122 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE; 123 124 virtual void RevokeCredentialsOnServer( 125 const std::string& refresh_token) OVERRIDE; 126 127 private: 128 // Helper function to complete pending requests - if |all_scopes| is true, 129 // then all pending requests are completed, otherwise, only those requests 130 // matching |scopes| are completed. 131 void CompleteRequests(bool all_scopes, 132 const ScopeSet& scopes, 133 const GoogleServiceAuthError& error, 134 const std::string& access_token, 135 const base::Time& expiration); 136 137 std::vector<PendingRequest> pending_requests_; 138 std::string refresh_token_; 139 140 DISALLOW_COPY_AND_ASSIGN(FakeProfileOAuth2TokenService); 141}; 142 143#endif // CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_ 144