Chromium Code Reviews| Index: chrome/browser/signin/fake_profile_oauth2_token_service.h |
| diff --git a/chrome/browser/signin/fake_profile_oauth2_token_service.h b/chrome/browser/signin/fake_profile_oauth2_token_service.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..04d59cc432a674d2c818cc19195960f7ad76089b |
| --- /dev/null |
| +++ b/chrome/browser/signin/fake_profile_oauth2_token_service.h |
| @@ -0,0 +1,103 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
|
Mattias Nissler (ping if slow)
2013/08/19 14:03:22
newline
Andrew T Wilson (Slow)
2013/08/20 09:28:35
Done.
|
| +#ifndef CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_ |
| +#define CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_ |
| + |
| +#include "chrome/browser/signin/profile_oauth2_token_service.h" |
| + |
| +namespace content { |
| +class BrowserContext; |
| +} |
| + |
| +// Helper class to simplify writing unittests that depend on an instance of |
| +// ProfileOAuth2TokenService. |
| +// |
| +// Tests would typically do something like the following: |
| +// |
| +// FakeProfileOAuth2TokenService service; |
| +// ... |
| +// service.IssueRefreshToken("token"); // Issue refresh token/notify observers |
| +// ... |
| +// // Confirm that there is at least one active request. |
| +// EXPECT_GT(0U, service.GetPendingRequests().size()); |
| +// ... |
| +// // Make any pending token fetches for a given scope succeed. |
| +// ScopeSet scopes; |
| +// scopes.insert(GaiaConstants::kYourServiceScope); |
| +// IssueTokenForScope(scopes, "access_token", base::Time()::Max()); |
| +// ... |
| +// // ...or make them fail... |
| +// IssueErrorForScope(scopes, GoogleServiceAuthError(INVALID_GAIA_CREDENTIALS)); |
| +// |
| +class FakeProfileOAuth2TokenService : public ProfileOAuth2TokenService { |
| + public: |
| + class PendingRequest { |
| + public: |
| + PendingRequest(); |
| + ~PendingRequest(); |
| + std::string client_id; |
|
Bernhard Bauer
2013/08/19 14:00:06
Make the member variables private?
Andrew T Wilson (Slow)
2013/08/20 09:28:35
Made this a struct instead.
|
| + std::string client_secret; |
| + ScopeSet scopes; |
| + base::WeakPtr<RequestImpl> request; |
|
Mattias Nissler (ping if slow)
2013/08/19 14:03:22
#include "base/memory/weak_ptr.h"
Andrew T Wilson (Slow)
2013/08/20 09:28:35
Done.
|
| + }; |
| + |
| + FakeProfileOAuth2TokenService(); |
| + virtual ~FakeProfileOAuth2TokenService(); |
| + |
| + // Sets the current refresh token. This method allows tests to mimic |
| + // what happens when the ProfileOAuth2TokenService has finished loading |
| + // its refresh token from disk, so OnRefreshTokensLoaded() will be fired |
| + // on all Observers. Additionally, if |token| is non-empty, this will also |
| + // fire OnRefreshTokenAvailable() on the Observers. |
| + void IssueRefreshToken(const std::string& token); |
| + |
| + // Gets a list of active requests (can be used by tests to validate that the |
| + // correct request has been issued). |
| + std::vector<PendingRequest> GetPendingRequests(); |
| + |
| + // Helper routines to issue tokens for pending requests. |
| + void IssueTokenForScope(const ScopeSet& scopes, |
| + const std::string& access_token, |
| + const base::Time& expiration); |
| + void IssueErrorForScope(const ScopeSet& scopes, |
| + const GoogleServiceAuthError& error); |
| + |
| + void IssueTokenForAllPendingRequests(const std::string& access_token, |
| + const base::Time& expiration); |
| + |
|
Mattias Nissler (ping if slow)
2013/08/19 14:03:22
nit: if no newline on line #63, then none here eit
Andrew T Wilson (Slow)
2013/08/20 09:28:35
Done.
|
| + void IssueErrorForAllPendingRequests(const GoogleServiceAuthError& error); |
| + |
| + virtual void Shutdown() OVERRIDE; |
| + |
| + // Helper function to be used with |
| + // BrowserContextKeyedService::SetTestingFactory(). |
| + static BrowserContextKeyedService* Build(content::BrowserContext* profile); |
| + |
| + protected: |
| + // OAuth2TokenService overrides. |
| + virtual void FetchOAuth2Token(RequestImpl* request, |
| + net::URLRequestContextGetter* getter, |
| + const std::string& client_id, |
| + const std::string& client_secret, |
| + const ScopeSet& scopes) OVERRIDE; |
| + |
| + virtual std::string GetRefreshToken() OVERRIDE; |
| + |
| + virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE; |
|
Mattias Nissler (ping if slow)
2013/08/19 14:03:22
#include "base/compiler_specific.h"
Andrew T Wilson (Slow)
2013/08/20 09:28:35
Done.
|
| + |
| + private: |
| + // Helper function to complete pending requests - if |all_scopes| is true, |
| + // then all pending requests are completed, otherwise, only those requests |
| + // matching |scopes| are completed. |
| + void CompleteRequests(bool all_scopes, |
| + const ScopeSet& scopes, |
| + const GoogleServiceAuthError& error, |
| + const std::string& access_token, |
| + const base::Time& expiration); |
| + |
| + std::vector<PendingRequest> pending_requests_; |
|
Mattias Nissler (ping if slow)
2013/08/19 14:03:22
#include <vector>
Andrew T Wilson (Slow)
2013/08/20 09:28:35
Done.
|
| + std::string refresh_token_; |
|
Mattias Nissler (ping if slow)
2013/08/19 14:03:22
#include <string>
Andrew T Wilson (Slow)
2013/08/20 09:28:35
Done.
|
| +}; |
| + |
| +#endif // CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_ |