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.
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/strings/stringprintf.h"
13 #include "google_apis/gaia/fake_oauth2_token_service.h"
14 #include "google_apis/gaia/gaia_constants.h"
15 #include "google_apis/gaia/merge_session_helper.h"
16 #include "net/url_request/test_url_fetcher_factory.h"
17 #include "net/url_request/url_request_test_util.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
23 class MockObserver
: public MergeSessionHelper::Observer
{
25 explicit MockObserver(MergeSessionHelper
* helper
) : helper_(helper
) {
26 helper_
->AddObserver(this);
30 helper_
->RemoveObserver(this);
33 MOCK_METHOD2(MergeSessionCompleted
,
34 void(const std::string
&,
35 const GoogleServiceAuthError
& ));
37 MergeSessionHelper
* helper_
;
39 DISALLOW_COPY_AND_ASSIGN(MockObserver
);
42 // Counts number of InstrumentedMergeSessionHelper created.
43 // We can EXPECT_* to be zero at the end of our unit tests
44 // to make sure everything is properly deleted.
48 class InstrumentedMergeSessionHelper
: public MergeSessionHelper
{
50 InstrumentedMergeSessionHelper(
51 OAuth2TokenService
* token_service
,
52 net::URLRequestContextGetter
* request_context
) :
53 MergeSessionHelper(token_service
, request_context
, NULL
) {
57 virtual ~InstrumentedMergeSessionHelper() {
61 MOCK_METHOD0(StartFetching
, void());
62 MOCK_METHOD0(StartLogOutUrlFetch
, void());
65 DISALLOW_COPY_AND_ASSIGN(InstrumentedMergeSessionHelper
);
68 class MergeSessionHelperTest
: public testing::Test
{
70 MergeSessionHelperTest()
71 : no_error_(GoogleServiceAuthError::NONE
),
72 error_(GoogleServiceAuthError::SERVICE_ERROR
),
73 canceled_(GoogleServiceAuthError::REQUEST_CANCELED
),
74 request_context_getter_(new net::TestURLRequestContextGetter(
75 base::MessageLoopProxy::current())) {}
77 OAuth2TokenService
* token_service() { return &token_service_
; }
78 net::URLRequestContextGetter
* request_context() {
79 return request_context_getter_
;
82 void SimulateUbertokenFailure(UbertokenConsumer
* consumer
,
83 const GoogleServiceAuthError
& error
) {
84 consumer
->OnUbertokenFailure(error
);
87 void SimulateMergeSessionSuccess(GaiaAuthConsumer
* consumer
,
88 const std::string
& data
) {
89 consumer
->OnMergeSessionSuccess(data
);
92 void SimulateMergeSessionFailure(GaiaAuthConsumer
* consumer
,
93 const GoogleServiceAuthError
& error
) {
94 consumer
->OnMergeSessionFailure(error
);
97 void SimulateLogoutSuccess(net::URLFetcherDelegate
* consumer
) {
98 consumer
->OnURLFetchComplete(NULL
);
101 const GoogleServiceAuthError
& no_error() { return no_error_
; }
102 const GoogleServiceAuthError
& error() { return error_
; }
103 const GoogleServiceAuthError
& canceled() { return canceled_
; }
106 base::MessageLoop message_loop_
;
107 net::TestURLFetcherFactory factory_
;
108 FakeOAuth2TokenService token_service_
;
109 GoogleServiceAuthError no_error_
;
110 GoogleServiceAuthError error_
;
111 GoogleServiceAuthError canceled_
;
112 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
119 TEST_F(MergeSessionHelperTest
, Success
) {
120 InstrumentedMergeSessionHelper
helper(token_service(), request_context());
121 MockObserver
observer(&helper
);
123 EXPECT_CALL(helper
, StartFetching());
124 EXPECT_CALL(observer
, MergeSessionCompleted("acc1@gmail.com", no_error()));
126 helper
.LogIn("acc1@gmail.com");
127 SimulateMergeSessionSuccess(&helper
, "token");
130 TEST_F(MergeSessionHelperTest
, FailedMergeSession
) {
131 InstrumentedMergeSessionHelper
helper(token_service(), request_context());
132 MockObserver
observer(&helper
);
134 EXPECT_CALL(helper
, StartFetching());
135 EXPECT_CALL(observer
, MergeSessionCompleted("acc1@gmail.com", error()));
137 helper
.LogIn("acc1@gmail.com");
138 SimulateMergeSessionFailure(&helper
, error());
141 TEST_F(MergeSessionHelperTest
, FailedUbertoken
) {
142 InstrumentedMergeSessionHelper
helper(token_service(), request_context());
143 MockObserver
observer(&helper
);
145 EXPECT_CALL(helper
, StartFetching());
146 EXPECT_CALL(observer
, MergeSessionCompleted("acc1@gmail.com", error()));
148 helper
.LogIn("acc1@gmail.com");
149 SimulateUbertokenFailure(&helper
, error());
152 TEST_F(MergeSessionHelperTest
, ContinueAfterSuccess
) {
153 InstrumentedMergeSessionHelper
helper(token_service(), request_context());
154 MockObserver
observer(&helper
);
156 EXPECT_CALL(helper
, StartFetching()).Times(2);
157 EXPECT_CALL(observer
, MergeSessionCompleted("acc1@gmail.com", no_error()));
158 EXPECT_CALL(observer
, MergeSessionCompleted("acc2@gmail.com", no_error()));
160 helper
.LogIn("acc1@gmail.com");
161 helper
.LogIn("acc2@gmail.com");
162 SimulateMergeSessionSuccess(&helper
, "token1");
163 SimulateMergeSessionSuccess(&helper
, "token2");
166 TEST_F(MergeSessionHelperTest
, ContinueAfterFailure1
) {
167 InstrumentedMergeSessionHelper
helper(token_service(), request_context());
168 MockObserver
observer(&helper
);
170 EXPECT_CALL(helper
, StartFetching()).Times(2);
171 EXPECT_CALL(observer
, MergeSessionCompleted("acc1@gmail.com", error()));
172 EXPECT_CALL(observer
, MergeSessionCompleted("acc2@gmail.com", no_error()));
174 helper
.LogIn("acc1@gmail.com");
175 helper
.LogIn("acc2@gmail.com");
176 SimulateMergeSessionFailure(&helper
, error());
177 SimulateMergeSessionSuccess(&helper
, "token2");
180 TEST_F(MergeSessionHelperTest
, ContinueAfterFailure2
) {
181 InstrumentedMergeSessionHelper
helper(token_service(), request_context());
182 MockObserver
observer(&helper
);
184 EXPECT_CALL(helper
, StartFetching()).Times(2);
185 EXPECT_CALL(observer
, MergeSessionCompleted("acc1@gmail.com", error()));
186 EXPECT_CALL(observer
, MergeSessionCompleted("acc2@gmail.com", no_error()));
188 helper
.LogIn("acc1@gmail.com");
189 helper
.LogIn("acc2@gmail.com");
190 SimulateUbertokenFailure(&helper
, error());
191 SimulateMergeSessionSuccess(&helper
, "token2");
194 TEST_F(MergeSessionHelperTest
, AllRequestsInMultipleGoes
) {
195 InstrumentedMergeSessionHelper
helper(token_service(), request_context());
196 MockObserver
observer(&helper
);
198 EXPECT_CALL(helper
, StartFetching()).Times(4);
199 EXPECT_CALL(observer
, MergeSessionCompleted(_
, no_error())).Times(4);
201 helper
.LogIn("acc1@gmail.com");
202 helper
.LogIn("acc2@gmail.com");
204 SimulateMergeSessionSuccess(&helper
, "token1");
206 helper
.LogIn("acc3@gmail.com");
208 SimulateMergeSessionSuccess(&helper
, "token2");
209 SimulateMergeSessionSuccess(&helper
, "token3");
211 helper
.LogIn("acc4@gmail.com");
213 SimulateMergeSessionSuccess(&helper
, "token4");
216 TEST_F(MergeSessionHelperTest
, LogOut
) {
217 InstrumentedMergeSessionHelper
helper(token_service(), request_context());
218 MockObserver
observer(&helper
);
220 std::vector
<std::string
> current_accounts
;
221 current_accounts
.push_back("acc1@gmail.com");
222 current_accounts
.push_back("acc2@gmail.com");
223 current_accounts
.push_back("acc3@gmail.com");
225 EXPECT_CALL(helper
, StartLogOutUrlFetch());
226 EXPECT_CALL(helper
, StartFetching()).Times(2);
227 EXPECT_CALL(observer
, MergeSessionCompleted("acc1@gmail.com", no_error()));
228 EXPECT_CALL(observer
, MergeSessionCompleted("acc3@gmail.com", no_error()));
230 helper
.LogOut("acc2@gmail.com", current_accounts
);
231 SimulateLogoutSuccess(&helper
);
232 SimulateMergeSessionSuccess(&helper
, "token1");
233 SimulateMergeSessionSuccess(&helper
, "token3");
236 TEST_F(MergeSessionHelperTest
, PendingSigninThenSignout
) {
237 InstrumentedMergeSessionHelper
helper(token_service(), request_context());
238 MockObserver
observer(&helper
);
240 std::vector
<std::string
> current_accounts
;
241 current_accounts
.push_back("acc2@gmail.com");
242 current_accounts
.push_back("acc3@gmail.com");
244 // From the first Signin.
245 EXPECT_CALL(observer
, MergeSessionCompleted("acc1@gmail.com", no_error()));
247 // From the sign out and then re-sign in.
248 EXPECT_CALL(helper
, StartLogOutUrlFetch());
249 EXPECT_CALL(observer
, MergeSessionCompleted("acc3@gmail.com", no_error()));
251 // Total sign in 2 times, not enforcing ordered sequences.
252 EXPECT_CALL(helper
, StartFetching()).Times(2);
254 helper
.LogIn("acc1@gmail.com");
255 helper
.LogOut("acc2@gmail.com", current_accounts
);
257 SimulateMergeSessionSuccess(&helper
, "token1");
258 SimulateLogoutSuccess(&helper
);
259 SimulateMergeSessionSuccess(&helper
, "token3");
262 TEST_F(MergeSessionHelperTest
, CancelSignIn
) {
263 InstrumentedMergeSessionHelper
helper(token_service(), request_context());
264 MockObserver
observer(&helper
);
266 std::vector
<std::string
> current_accounts
;
268 EXPECT_CALL(helper
, StartFetching());
269 EXPECT_CALL(observer
, MergeSessionCompleted("acc2@gmail.com", canceled()));
270 EXPECT_CALL(observer
, MergeSessionCompleted("acc1@gmail.com", no_error()));
271 EXPECT_CALL(helper
, StartLogOutUrlFetch());
273 helper
.LogIn("acc1@gmail.com");
274 helper
.LogIn("acc2@gmail.com");
275 helper
.LogOut("acc2@gmail.com", current_accounts
);
277 SimulateMergeSessionSuccess(&helper
, "token1");
278 SimulateLogoutSuccess(&helper
);
281 TEST_F(MergeSessionHelperTest
, DoubleSignout
) {
282 InstrumentedMergeSessionHelper
helper(token_service(), request_context());
283 MockObserver
observer(&helper
);
285 std::vector
<std::string
> current_accounts1
;
286 current_accounts1
.push_back("acc1@gmail.com");
287 current_accounts1
.push_back("acc2@gmail.com");
288 current_accounts1
.push_back("acc3@gmail.com");
290 std::vector
<std::string
> current_accounts2
;
291 current_accounts2
.push_back("acc1@gmail.com");
292 current_accounts2
.push_back("acc3@gmail.com");
294 EXPECT_CALL(helper
, StartFetching()).Times(2);
295 EXPECT_CALL(observer
, MergeSessionCompleted("acc3@gmail.com", canceled()));
296 EXPECT_CALL(observer
,
297 MergeSessionCompleted("acc1@gmail.com", no_error())).Times(2);
298 EXPECT_CALL(helper
, StartLogOutUrlFetch());
300 helper
.LogIn("acc1@gmail.com");
301 helper
.LogOut("acc2@gmail.com", current_accounts1
);
302 helper
.LogOut("acc3@gmail.com", current_accounts2
);
304 SimulateMergeSessionSuccess(&helper
, "token1");
305 SimulateLogoutSuccess(&helper
);
306 SimulateMergeSessionSuccess(&helper
, "token1");