Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2012)

Side by Side Diff: google_apis/gaia/oauth2_token_service_proxy_unittest.cc

Issue 299943003: Refactor ProfileOAuth2TokenServiceRequest into OAuth2TokenServiceRequest (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Allow ProfileOAuth2TokenServiceRequestTest to free UI thread resources. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 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 #include "google_apis/gaia/oauth2_token_service_proxy.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "google_apis/gaia/fake_oauth2_token_service.h"
13 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace {
17
18 const char kRequesterId[] = "unittest";
19 const char kAccountId[] = "validaccountid";
20
21 } // namespace
22
23 class MockOAuth2TokenService : public FakeOAuth2TokenService {
24 public:
25 MockOAuth2TokenService();
26 virtual ~MockOAuth2TokenService();
27
28 // Set the error to return in response to a RequestToken call.
29 void SetRequestTokenError(const GoogleServiceAuthError& error) {
30 error_ = error;
31 }
32
33 // Return the number of times InvalidateOAuth2Token was called.
34 int GetNumInvalidateCalls() const { return num_invalidate_calls_; }
35
36 protected:
37 virtual void FetchOAuth2Token(RequestImpl* request,
38 const std::string& account_id,
39 net::URLRequestContextGetter* getter,
40 const std::string& client_id,
41 const std::string& client_secret,
42 const ScopeSet& scopes) OVERRIDE;
43
44 virtual void InvalidateOAuth2Token(const std::string& account_id,
45 const std::string& client_id,
46 const ScopeSet& scopes,
47 const std::string& access_token) OVERRIDE;
48
49 private:
50 GoogleServiceAuthError error_;
51 int num_invalidate_calls_;
52 };
53
54 MockOAuth2TokenService::MockOAuth2TokenService()
55 : error_(GoogleServiceAuthError::NONE), num_invalidate_calls_(0) {
56 }
57
58 MockOAuth2TokenService::~MockOAuth2TokenService() {
59 }
60
61 void MockOAuth2TokenService::FetchOAuth2Token(
62 RequestImpl* request,
63 const std::string& account_id,
64 net::URLRequestContextGetter* getter,
65 const std::string& client_id,
66 const std::string& client_secret,
67 const ScopeSet& scopes) {
68 base::MessageLoop::current()->PostTask(
69 FROM_HERE,
70 base::Bind(&OAuth2TokenService::RequestImpl::InformConsumer,
71 request->AsWeakPtr(),
72 error_,
73 account_id,
74 base::Time()));
75 }
76
77 void MockOAuth2TokenService::InvalidateOAuth2Token(
78 const std::string& account_id,
79 const std::string& client_id,
80 const ScopeSet& scopes,
81 const std::string& access_token) {
82 ++num_invalidate_calls_;
83 }
84
85 class OAuth2TokenServiceProxyTest : public testing::Test {
86 public:
87 virtual void SetUp() OVERRIDE;
88
89 virtual void TearDown() OVERRIDE;
90
91 void OnRequestTokenDone(const GoogleServiceAuthError& error,
92 const std::string& access_token,
93 const base::Time& expiration_time);
94
95 struct CallbackArguments {
96 GoogleServiceAuthError error;
97 std::string access_token;
98 base::Time expiration_time;
99 };
100
101 base::MessageLoop message_loop_;
102
103 OAuth2TokenServiceProxy::RequestTokenCallback callback;
104 std::vector<CallbackArguments> callback_invocations;
105 OAuth2TokenService::ScopeSet scopes;
106 MockOAuth2TokenService token_service;
107 scoped_ptr<OAuth2TokenServiceProxy> proxy;
108 };
109
110 void OAuth2TokenServiceProxyTest::SetUp() {
111 token_service.AddAccount(kAccountId);
112 callback = base::Bind(&OAuth2TokenServiceProxyTest::OnRequestTokenDone,
113 base::Unretained(this));
114 proxy.reset(new OAuth2TokenServiceProxy(base::MessageLoopProxy::current(),
115 &token_service));
116 }
117
118 void OAuth2TokenServiceProxyTest::TearDown() {
119 // Ensure that any tasks posted by OAuth2TokenServiceProxy's destructor get a
120 // chance to run so we don't leak memory.
121 proxy.reset();
122 base::RunLoop().RunUntilIdle();
123 }
124
125 void OAuth2TokenServiceProxyTest::OnRequestTokenDone(
126 const GoogleServiceAuthError& error,
127 const std::string& access_token,
128 const base::Time& expiration_time) {
129 CallbackArguments args = {error, access_token, expiration_time};
130 callback_invocations.push_back(args);
131 }
132
133 // See that a call to RequestToken is proxied and that an success response is
134 // proxied back.
135 TEST_F(OAuth2TokenServiceProxyTest, RequestToken_Succeeds) {
136 proxy->RequestToken(kAccountId, scopes, callback, kRequesterId);
137 base::RunLoop().RunUntilIdle();
138 ASSERT_EQ(1U, callback_invocations.size());
139 EXPECT_EQ(GoogleServiceAuthError::NONE,
140 callback_invocations[0].error.state());
141 }
142
143 // See that a call to RequestToken is proxied and that an error response is
144 // proxied back.
145 TEST_F(OAuth2TokenServiceProxyTest, RequestToken_Fails) {
146 token_service.SetRequestTokenError(
147 GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED));
148 proxy->RequestToken(kAccountId, scopes, callback, kRequesterId);
149 base::RunLoop().RunUntilIdle();
150 ASSERT_EQ(1U, callback_invocations.size());
151 EXPECT_EQ(GoogleServiceAuthError::CONNECTION_FAILED,
152 callback_invocations[0].error.state());
153 }
154
155 // See that destroying the proxy before the request has completed does not
156 // cancel the request.
157 TEST_F(OAuth2TokenServiceProxyTest, RequestToken_DestroyProxy) {
158 proxy->RequestToken(kAccountId, scopes, callback, kRequesterId);
159 proxy.reset();
160 // Proxy has been destroyed, but its Core may live on in the token service
161 // task runner thread.
162 base::RunLoop().RunUntilIdle();
163 ASSERT_EQ(1U, callback_invocations.size());
164 }
165
166 // See that calls to InvalidateToken are proxied.
167 TEST_F(OAuth2TokenServiceProxyTest, InvalidateToken) {
168 proxy->InvalidateToken(kAccountId, scopes, "sometoken");
169 base::RunLoop().RunUntilIdle();
170 ASSERT_EQ(1, token_service.GetNumInvalidateCalls());
171 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698