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.
5 #ifndef CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_
6 #define CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_
11 #include "base/compiler_specific.h"
12 #include "base/memory/weak_ptr.h"
14 #if defined(OS_ANDROID)
15 #include "chrome/browser/signin/android_profile_oauth2_token_service.h"
17 #include "chrome/browser/signin/profile_oauth2_token_service.h"
24 // Helper class to simplify writing unittests that depend on an instance of
25 // ProfileOAuth2TokenService.
27 // Tests would typically do something like the following:
29 // FakeProfileOAuth2TokenService service;
31 // service.IssueRefreshToken("token"); // Issue refresh token/notify observers
33 // // Confirm that there is at least one active request.
34 // EXPECT_GT(0U, service.GetPendingRequests().size());
36 // // Make any pending token fetches for a given scope succeed.
38 // scopes.insert(GaiaConstants::kYourServiceScope);
39 // IssueTokenForScope(scopes, "access_token", base::Time()::Max());
41 // // ...or make them fail...
42 // IssueErrorForScope(scopes, GoogleServiceAuthError(INVALID_GAIA_CREDENTIALS));
44 class FakeProfileOAuth2TokenService
45 #if defined(OS_ANDROID)
46 : public AndroidProfileOAuth2TokenService {
48 : public ProfileOAuth2TokenService {
51 struct PendingRequest {
55 std::string account_id;
56 std::string client_id;
57 std::string client_secret;
59 base::WeakPtr<RequestImpl> request;
62 FakeProfileOAuth2TokenService();
63 virtual ~FakeProfileOAuth2TokenService();
65 // Overriden to make sure it works on Android.
66 virtual bool RefreshTokenIsAvailable(
67 const std::string& account_id) OVERRIDE;
69 // Sets the current refresh token. If |token| is non-empty, this will invoke
70 // OnRefreshTokenAvailable() on all Observers, otherwise this will invoke
71 // OnRefreshTokenRevoked().
72 void IssueRefreshToken(const std::string& token);
74 // TODO(fgorski,rogerta): Merge with UpdateCredentials when this class fully
75 // supports multiple accounts.
76 void IssueRefreshTokenForUser(const std::string& account_id,
77 const std::string& token);
79 // Gets a list of active requests (can be used by tests to validate that the
80 // correct request has been issued).
81 std::vector<PendingRequest> GetPendingRequests();
83 // Helper routines to issue tokens for pending requests.
84 // TODO(fgorski): Add account IDs as parameters.
85 void IssueTokenForScope(const ScopeSet& scopes,
86 const std::string& access_token,
87 const base::Time& expiration);
89 void IssueErrorForScope(const ScopeSet& scopes,
90 const GoogleServiceAuthError& error);
92 void IssueTokenForAllPendingRequests(const std::string& access_token,
93 const base::Time& expiration);
95 void IssueErrorForAllPendingRequests(const GoogleServiceAuthError& error);
97 // Helper function to be used with
98 // BrowserContextKeyedService::SetTestingFactory().
99 static BrowserContextKeyedService* Build(content::BrowserContext* profile);
102 // OAuth2TokenService overrides.
103 virtual void FetchOAuth2Token(RequestImpl* request,
104 const std::string& account_id,
105 net::URLRequestContextGetter* getter,
106 const std::string& client_id,
107 const std::string& client_secret,
108 const ScopeSet& scopes) OVERRIDE;
110 virtual std::string GetRefreshToken(const std::string& account_id) OVERRIDE;
112 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE;
115 // Helper function to complete pending requests - if |all_scopes| is true,
116 // then all pending requests are completed, otherwise, only those requests
117 // matching |scopes| are completed.
118 void CompleteRequests(bool all_scopes,
119 const ScopeSet& scopes,
120 const GoogleServiceAuthError& error,
121 const std::string& access_token,
122 const base::Time& expiration);
124 std::vector<PendingRequest> pending_requests_;
125 std::string refresh_token_;
127 DISALLOW_COPY_AND_ASSIGN(FakeProfileOAuth2TokenService);
130 #endif // CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_