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.
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/stringprintf.h"
11 #include "chrome/browser/signin/google_auto_login_helper.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 class MockObserver
: public GoogleAutoLoginHelper::Observer
{
21 explicit MockObserver(GoogleAutoLoginHelper
* helper
) : helper_(helper
) {
22 helper_
->AddObserver(this);
26 helper_
->RemoveObserver(this);
29 MOCK_METHOD2(MergeSessionCompleted
,
30 void(const std::string
&,
31 const GoogleServiceAuthError
& ));
33 GoogleAutoLoginHelper
* helper_
;
35 DISALLOW_COPY_AND_ASSIGN(MockObserver
);
38 // Counts number of InstrumentedGoogleAutoLoginHelper created.
39 // We can EXPECT_* to be zero at the end of our unit tests
40 // to make sure everything is properly deleted.
44 class InstrumentedGoogleAutoLoginHelper
: public GoogleAutoLoginHelper
{
46 explicit InstrumentedGoogleAutoLoginHelper(Profile
* profile
) :
47 GoogleAutoLoginHelper(profile
, NULL
) {
51 virtual ~InstrumentedGoogleAutoLoginHelper() {
55 MOCK_METHOD0(StartFetching
, void());
58 DISALLOW_COPY_AND_ASSIGN(InstrumentedGoogleAutoLoginHelper
);
61 class GoogleAutoLoginHelperTest
: public testing::Test
{
63 GoogleAutoLoginHelperTest()
64 : no_error_(GoogleServiceAuthError::NONE
),
65 error_(GoogleServiceAuthError::SERVICE_ERROR
) {}
67 TestingProfile
* profile() { return &profile_
; }
69 void SimulateUbertokenFailure(UbertokenConsumer
* consumer
,
70 const GoogleServiceAuthError
& error
) {
71 consumer
->OnUbertokenFailure(error
);
74 void SimulateMergeSessionSuccess(GaiaAuthConsumer
* consumer
,
75 const std::string
& data
) {
76 consumer
->OnMergeSessionSuccess(data
);
79 void SimulateMergeSessionFailure(GaiaAuthConsumer
* consumer
,
80 const GoogleServiceAuthError
& error
) {
81 consumer
->OnMergeSessionFailure(error
);
84 const GoogleServiceAuthError
& no_error() { return no_error_
; }
85 const GoogleServiceAuthError
& error() { return error_
; }
88 content::TestBrowserThreadBundle thread_bundle_
;
89 TestingProfile profile_
;
90 GoogleServiceAuthError no_error_
;
91 GoogleServiceAuthError error_
;
98 TEST_F(GoogleAutoLoginHelperTest
, Success
) {
99 InstrumentedGoogleAutoLoginHelper
helper(profile());
100 MockObserver
observer(&helper
);
102 EXPECT_CALL(helper
, StartFetching());
103 EXPECT_CALL(observer
, MergeSessionCompleted("acc1@gmail.com", no_error()));
105 helper
.LogIn("acc1@gmail.com");
106 SimulateMergeSessionSuccess(&helper
, "token");
109 TEST_F(GoogleAutoLoginHelperTest
, FailedMergeSession
) {
110 InstrumentedGoogleAutoLoginHelper
helper(profile());
111 MockObserver
observer(&helper
);
113 EXPECT_CALL(helper
, StartFetching());
114 EXPECT_CALL(observer
, MergeSessionCompleted("acc1@gmail.com", error()));
116 helper
.LogIn("acc1@gmail.com");
117 SimulateMergeSessionFailure(&helper
, error());
120 TEST_F(GoogleAutoLoginHelperTest
, FailedUbertoken
) {
121 InstrumentedGoogleAutoLoginHelper
helper(profile());
122 MockObserver
observer(&helper
);
124 EXPECT_CALL(helper
, StartFetching());
125 EXPECT_CALL(observer
, MergeSessionCompleted("acc1@gmail.com", error()));
127 helper
.LogIn("acc1@gmail.com");
128 SimulateUbertokenFailure(&helper
, error());
131 TEST_F(GoogleAutoLoginHelperTest
, ContinueAfterSuccess
) {
132 InstrumentedGoogleAutoLoginHelper
helper(profile());
133 MockObserver
observer(&helper
);
135 EXPECT_CALL(helper
, StartFetching()).Times(2);
136 EXPECT_CALL(observer
, MergeSessionCompleted("acc1@gmail.com", no_error()));
137 EXPECT_CALL(observer
, MergeSessionCompleted("acc2@gmail.com", no_error()));
139 helper
.LogIn("acc1@gmail.com");
140 helper
.LogIn("acc2@gmail.com");
141 SimulateMergeSessionSuccess(&helper
, "token1");
142 SimulateMergeSessionSuccess(&helper
, "token2");
145 TEST_F(GoogleAutoLoginHelperTest
, ContinueAfterFailure1
) {
146 InstrumentedGoogleAutoLoginHelper
helper(profile());
147 MockObserver
observer(&helper
);
149 EXPECT_CALL(helper
, StartFetching()).Times(2);
150 EXPECT_CALL(observer
, MergeSessionCompleted("acc1@gmail.com", error()));
151 EXPECT_CALL(observer
, MergeSessionCompleted("acc2@gmail.com", no_error()));
153 helper
.LogIn("acc1@gmail.com");
154 helper
.LogIn("acc2@gmail.com");
155 SimulateMergeSessionFailure(&helper
, error());
156 SimulateMergeSessionSuccess(&helper
, "token2");
159 TEST_F(GoogleAutoLoginHelperTest
, ContinueAfterFailure2
) {
160 InstrumentedGoogleAutoLoginHelper
helper(profile());
161 MockObserver
observer(&helper
);
163 EXPECT_CALL(helper
, StartFetching()).Times(2);
164 EXPECT_CALL(observer
, MergeSessionCompleted("acc1@gmail.com", error()));
165 EXPECT_CALL(observer
, MergeSessionCompleted("acc2@gmail.com", no_error()));
167 helper
.LogIn("acc1@gmail.com");
168 helper
.LogIn("acc2@gmail.com");
169 SimulateUbertokenFailure(&helper
, error());
170 SimulateMergeSessionSuccess(&helper
, "token2");
173 TEST_F(GoogleAutoLoginHelperTest
, AllRequestsInMultipleGoes
) {
174 InstrumentedGoogleAutoLoginHelper
helper(profile());
175 MockObserver
observer(&helper
);
177 EXPECT_CALL(helper
, StartFetching()).Times(4);
178 EXPECT_CALL(observer
, MergeSessionCompleted(_
, no_error())).Times(4);
180 helper
.LogIn("acc1@gmail.com");
181 helper
.LogIn("acc2@gmail.com");
183 SimulateMergeSessionSuccess(&helper
, "token1");
185 helper
.LogIn("acc3@gmail.com");
187 SimulateMergeSessionSuccess(&helper
, "token2");
188 SimulateMergeSessionSuccess(&helper
, "token3");
190 helper
.LogIn("acc4@gmail.com");
192 SimulateMergeSessionSuccess(&helper
, "token4");