OLD | NEW |
(Empty) | |
| 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 "chrome/browser/signin/profile_oauth2_token_service.h" |
| 9 |
| 10 #include <string> |
| 11 #include <vector> |
| 12 |
| 13 #include "base/compiler_specific.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 |
| 16 namespace content { |
| 17 class BrowserContext; |
| 18 } |
| 19 |
| 20 // Helper class to simplify writing unittests that depend on an instance of |
| 21 // ProfileOAuth2TokenService. |
| 22 // |
| 23 // Tests would typically do something like the following: |
| 24 // |
| 25 // FakeProfileOAuth2TokenService service; |
| 26 // ... |
| 27 // service.IssueRefreshToken("token"); // Issue refresh token/notify observers |
| 28 // ... |
| 29 // // Confirm that there is at least one active request. |
| 30 // EXPECT_GT(0U, service.GetPendingRequests().size()); |
| 31 // ... |
| 32 // // Make any pending token fetches for a given scope succeed. |
| 33 // ScopeSet scopes; |
| 34 // scopes.insert(GaiaConstants::kYourServiceScope); |
| 35 // IssueTokenForScope(scopes, "access_token", base::Time()::Max()); |
| 36 // ... |
| 37 // // ...or make them fail... |
| 38 // IssueErrorForScope(scopes, GoogleServiceAuthError(INVALID_GAIA_CREDENTIALS)); |
| 39 // |
| 40 class FakeProfileOAuth2TokenService : public ProfileOAuth2TokenService { |
| 41 public: |
| 42 struct PendingRequest { |
| 43 PendingRequest(); |
| 44 ~PendingRequest(); |
| 45 |
| 46 std::string client_id; |
| 47 std::string client_secret; |
| 48 ScopeSet scopes; |
| 49 base::WeakPtr<RequestImpl> request; |
| 50 }; |
| 51 |
| 52 FakeProfileOAuth2TokenService(); |
| 53 virtual ~FakeProfileOAuth2TokenService(); |
| 54 |
| 55 // Sets the current refresh token. If |token| is non-empty, this will invoke |
| 56 // OnRefreshTokenAvailable() on all Observers, otherwise this will invoke |
| 57 // OnRefreshTokenRevoked(). |
| 58 void IssueRefreshToken(const std::string& token); |
| 59 |
| 60 // Gets a list of active requests (can be used by tests to validate that the |
| 61 // correct request has been issued). |
| 62 std::vector<PendingRequest> GetPendingRequests(); |
| 63 |
| 64 // Helper routines to issue tokens for pending requests. |
| 65 void IssueTokenForScope(const ScopeSet& scopes, |
| 66 const std::string& access_token, |
| 67 const base::Time& expiration); |
| 68 |
| 69 void IssueErrorForScope(const ScopeSet& scopes, |
| 70 const GoogleServiceAuthError& error); |
| 71 |
| 72 void IssueTokenForAllPendingRequests(const std::string& access_token, |
| 73 const base::Time& expiration); |
| 74 |
| 75 void IssueErrorForAllPendingRequests(const GoogleServiceAuthError& error); |
| 76 |
| 77 virtual void Shutdown() OVERRIDE; |
| 78 |
| 79 // Helper function to be used with |
| 80 // BrowserContextKeyedService::SetTestingFactory(). |
| 81 static BrowserContextKeyedService* Build(content::BrowserContext* profile); |
| 82 |
| 83 protected: |
| 84 // OAuth2TokenService overrides. |
| 85 virtual void FetchOAuth2Token(RequestImpl* request, |
| 86 net::URLRequestContextGetter* getter, |
| 87 const std::string& client_id, |
| 88 const std::string& client_secret, |
| 89 const ScopeSet& scopes) OVERRIDE; |
| 90 |
| 91 virtual std::string GetRefreshToken() OVERRIDE; |
| 92 |
| 93 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE; |
| 94 |
| 95 private: |
| 96 // Helper function to complete pending requests - if |all_scopes| is true, |
| 97 // then all pending requests are completed, otherwise, only those requests |
| 98 // matching |scopes| are completed. |
| 99 void CompleteRequests(bool all_scopes, |
| 100 const ScopeSet& scopes, |
| 101 const GoogleServiceAuthError& error, |
| 102 const std::string& access_token, |
| 103 const base::Time& expiration); |
| 104 |
| 105 std::vector<PendingRequest> pending_requests_; |
| 106 std::string refresh_token_; |
| 107 |
| 108 DISALLOW_COPY_AND_ASSIGN(FakeProfileOAuth2TokenService); |
| 109 }; |
| 110 |
| 111 #endif // CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_ |
OLD | NEW |