fake_profile_oauth2_token_service.h revision 58537e28ecd584eab876aee8be7156509866d23a
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 client_id;
56    std::string client_secret;
57    ScopeSet scopes;
58    base::WeakPtr<RequestImpl> request;
59  };
60
61  FakeProfileOAuth2TokenService();
62  virtual ~FakeProfileOAuth2TokenService();
63
64  // Sets the current refresh token. If |token| is non-empty, this will invoke
65  // OnRefreshTokenAvailable() on all Observers, otherwise this will invoke
66  // OnRefreshTokenRevoked().
67  void IssueRefreshToken(const std::string& token);
68
69  // Gets a list of active requests (can be used by tests to validate that the
70  // correct request has been issued).
71  std::vector<PendingRequest> GetPendingRequests();
72
73  // Helper routines to issue tokens for pending requests.
74  void IssueTokenForScope(const ScopeSet& scopes,
75                          const std::string& access_token,
76                          const base::Time& expiration);
77
78  void IssueErrorForScope(const ScopeSet& scopes,
79                          const GoogleServiceAuthError& error);
80
81  void IssueTokenForAllPendingRequests(const std::string& access_token,
82                                       const base::Time& expiration);
83
84  void IssueErrorForAllPendingRequests(const GoogleServiceAuthError& error);
85
86  // Helper function to be used with
87  // BrowserContextKeyedService::SetTestingFactory().
88  static BrowserContextKeyedService* Build(content::BrowserContext* profile);
89
90 protected:
91  // OAuth2TokenService overrides.
92  virtual void FetchOAuth2Token(RequestImpl* request,
93                                net::URLRequestContextGetter* getter,
94                                const std::string& client_id,
95                                const std::string& client_secret,
96                                const ScopeSet& scopes) OVERRIDE;
97
98  virtual std::string GetRefreshToken() OVERRIDE;
99
100  virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE;
101
102 private:
103  // Helper function to complete pending requests - if |all_scopes| is true,
104  // then all pending requests are completed, otherwise, only those requests
105  // matching |scopes| are completed.
106  void CompleteRequests(bool all_scopes,
107                        const ScopeSet& scopes,
108                        const GoogleServiceAuthError& error,
109                        const std::string& access_token,
110                        const base::Time& expiration);
111
112  std::vector<PendingRequest> pending_requests_;
113  std::string refresh_token_;
114
115  DISALLOW_COPY_AND_ASSIGN(FakeProfileOAuth2TokenService);
116};
117
118#endif  // CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_
119