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