OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/run_loop.h" |
10 #include "chrome/browser/chromeos/cros/network_library.h" | 10 #include "chrome/browser/chromeos/cros/network_library.h" |
11 #include "chrome/browser/chromeos/login/auth_attempt_state.h" | 11 #include "chrome/browser/chromeos/login/auth_attempt_state.h" |
12 #include "chrome/browser/chromeos/login/mock_auth_attempt_state_resolver.h" | 12 #include "chrome/browser/chromeos/login/mock_auth_attempt_state_resolver.h" |
13 #include "chrome/browser/chromeos/login/mock_url_fetchers.h" | 13 #include "chrome/browser/chromeos/login/mock_url_fetchers.h" |
14 #include "chrome/browser/chromeos/login/online_attempt.h" | 14 #include "chrome/browser/chromeos/login/online_attempt.h" |
15 #include "chrome/browser/chromeos/login/test_attempt_state.h" | 15 #include "chrome/browser/chromeos/login/test_attempt_state.h" |
16 #include "chrome/browser/chromeos/login/user.h" | 16 #include "chrome/browser/chromeos/login/user.h" |
17 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
18 #include "content/public/test/test_browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/test/test_browser_thread_bundle.h" |
19 #include "google_apis/gaia/gaia_auth_consumer.h" | 20 #include "google_apis/gaia/gaia_auth_consumer.h" |
20 #include "google_apis/gaia/mock_url_fetcher_factory.h" | 21 #include "google_apis/gaia/mock_url_fetcher_factory.h" |
21 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
23 #include "url/gurl.h" | 24 #include "url/gurl.h" |
24 | 25 |
25 using ::testing::AnyNumber; | 26 using ::testing::AnyNumber; |
26 using ::testing::Invoke; | 27 using ::testing::Invoke; |
27 using ::testing::Return; | 28 using ::testing::Return; |
28 using ::testing::_; | 29 using ::testing::_; |
29 using content::BrowserThread; | 30 using content::BrowserThread; |
30 | 31 |
31 namespace chromeos { | 32 namespace chromeos { |
32 | 33 |
33 class OnlineAttemptTest : public testing::Test { | 34 class OnlineAttemptTest : public testing::Test { |
34 public: | 35 public: |
35 OnlineAttemptTest() | 36 OnlineAttemptTest() |
36 : message_loop_(base::MessageLoop::TYPE_UI), | 37 : state_(UserContext(), "", "", "", User::USER_TYPE_REGULAR, false), |
37 ui_thread_(BrowserThread::UI, &message_loop_), | 38 attempt_(new OnlineAttempt(&state_, &resolver_)) { |
38 state_(UserContext(), "", "", "", User::USER_TYPE_REGULAR, false), | |
39 resolver_(new MockAuthAttemptStateResolver) { | |
40 } | |
41 | |
42 virtual ~OnlineAttemptTest() {} | |
43 | |
44 virtual void SetUp() { | |
45 attempt_.reset(new OnlineAttempt(&state_, resolver_.get())); | |
46 } | |
47 | |
48 virtual void TearDown() { | |
49 } | 39 } |
50 | 40 |
51 void RunFailureTest(const GoogleServiceAuthError& error) { | 41 void RunFailureTest(const GoogleServiceAuthError& error) { |
52 EXPECT_CALL(*(resolver_.get()), Resolve()) | 42 EXPECT_CALL(resolver_, Resolve()) |
53 .Times(1) | 43 .Times(1) |
54 .RetiresOnSaturation(); | 44 .RetiresOnSaturation(); |
55 | 45 |
56 BrowserThread::PostTask( | 46 BrowserThread::PostTask( |
57 BrowserThread::UI, FROM_HERE, | 47 BrowserThread::UI, FROM_HERE, |
58 base::Bind(&OnlineAttempt::OnClientLoginFailure, | 48 base::Bind(&OnlineAttempt::OnClientLoginFailure, |
59 attempt_->weak_factory_.GetWeakPtr(), | 49 attempt_->weak_factory_.GetWeakPtr(), |
60 error)); | 50 error)); |
61 // Force UI thread to finish tasks so I can verify |state_|. | 51 // Force UI thread to finish tasks so I can verify |state_|. |
62 message_loop_.RunUntilIdle(); | 52 base::RunLoop().RunUntilIdle(); |
63 EXPECT_TRUE(error == state_.online_outcome().error()); | 53 EXPECT_TRUE(error == state_.online_outcome().error()); |
64 } | 54 } |
65 | 55 |
66 void CancelLogin(OnlineAttempt* auth) { | 56 void CancelLogin(OnlineAttempt* auth) { |
67 BrowserThread::PostTask( | 57 BrowserThread::PostTask( |
68 BrowserThread::UI, FROM_HERE, | 58 BrowserThread::UI, FROM_HERE, |
69 base::Bind(&OnlineAttempt::CancelClientLogin, | 59 base::Bind(&OnlineAttempt::CancelClientLogin, |
70 auth->weak_factory_.GetWeakPtr())); | 60 auth->weak_factory_.GetWeakPtr())); |
71 } | 61 } |
72 | 62 |
73 static void Quit() { | 63 content::TestBrowserThreadBundle thread_bundle_; |
74 BrowserThread::PostTask( | |
75 BrowserThread::UI, FROM_HERE, base::MessageLoop::QuitClosure()); | |
76 } | |
77 | |
78 static void RunThreadTest() { | |
79 base::MessageLoop::current()->RunUntilIdle(); | |
80 } | |
81 | |
82 base::MessageLoop message_loop_; | |
83 content::TestBrowserThread ui_thread_; | |
84 TestAttemptState state_; | 64 TestAttemptState state_; |
85 scoped_ptr<MockAuthAttemptStateResolver> resolver_; | 65 MockAuthAttemptStateResolver resolver_; |
86 scoped_ptr<OnlineAttempt> attempt_; | 66 scoped_ptr<OnlineAttempt> attempt_; |
87 | 67 |
88 // Initializes / shuts down a stub NetworkLibrary. | 68 // Initializes / shuts down a stub NetworkLibrary. |
89 ScopedStubNetworkLibraryEnabler stub_network_library_enabler_; | 69 ScopedStubNetworkLibraryEnabler stub_network_library_enabler_; |
90 }; | 70 }; |
91 | 71 |
92 TEST_F(OnlineAttemptTest, LoginSuccess) { | 72 TEST_F(OnlineAttemptTest, LoginSuccess) { |
93 EXPECT_CALL(*(resolver_.get()), Resolve()) | 73 EXPECT_CALL(resolver_, Resolve()) |
94 .Times(1) | 74 .Times(1) |
95 .RetiresOnSaturation(); | 75 .RetiresOnSaturation(); |
96 | 76 |
97 BrowserThread::PostTask( | 77 BrowserThread::PostTask( |
98 BrowserThread::UI, FROM_HERE, | 78 BrowserThread::UI, FROM_HERE, |
99 base::Bind(&OnlineAttempt::OnClientLoginSuccess, | 79 base::Bind(&OnlineAttempt::OnClientLoginSuccess, |
100 attempt_->weak_factory_.GetWeakPtr(), | 80 attempt_->weak_factory_.GetWeakPtr(), |
101 GaiaAuthConsumer::ClientLoginResult())); | 81 GaiaAuthConsumer::ClientLoginResult())); |
102 // Force UI thread to finish tasks so I can verify |state_|. | 82 // Force UI thread to finish tasks so I can verify |state_|. |
103 message_loop_.RunUntilIdle(); | 83 base::RunLoop().RunUntilIdle(); |
104 } | 84 } |
105 | 85 |
106 TEST_F(OnlineAttemptTest, LoginCancelRetry) { | 86 TEST_F(OnlineAttemptTest, LoginCancelRetry) { |
107 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); | 87 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); |
108 TestingProfile profile; | 88 TestingProfile profile; |
109 | 89 |
110 EXPECT_CALL(*(resolver_.get()), Resolve()) | 90 base::RunLoop run_loop; |
111 .WillOnce(Invoke(OnlineAttemptTest::Quit)) | 91 EXPECT_CALL(resolver_, Resolve()) |
| 92 .WillOnce(Invoke(&run_loop, &base::RunLoop::Quit)) |
112 .RetiresOnSaturation(); | 93 .RetiresOnSaturation(); |
113 | 94 |
114 // This is how we inject fake URLFetcher objects, with a factory. | 95 // This is how we inject fake URLFetcher objects, with a factory. |
115 // This factory creates fake URLFetchers that Start() a fake fetch attempt | 96 // This factory creates fake URLFetchers that Start() a fake fetch attempt |
116 // and then come back on the UI thread saying they've been canceled. | 97 // and then come back on the UI thread saying they've been canceled. |
117 MockURLFetcherFactory<GotCanceledFetcher> factory; | 98 MockURLFetcherFactory<GotCanceledFetcher> factory; |
118 | 99 |
119 attempt_->Initiate(&profile); | 100 attempt_->Initiate(&profile); |
120 BrowserThread::PostTask( | |
121 BrowserThread::UI, FROM_HERE, | |
122 base::Bind(&OnlineAttemptTest::RunThreadTest)); | |
123 | 101 |
124 base::MessageLoop::current()->Run(); | 102 run_loop.Run(); |
125 | 103 |
126 EXPECT_TRUE(error == state_.online_outcome().error()); | 104 EXPECT_TRUE(error == state_.online_outcome().error()); |
127 EXPECT_EQ(LoginFailure::NETWORK_AUTH_FAILED, | 105 EXPECT_EQ(LoginFailure::NETWORK_AUTH_FAILED, |
128 state_.online_outcome().reason()); | 106 state_.online_outcome().reason()); |
129 } | 107 } |
130 | 108 |
131 TEST_F(OnlineAttemptTest, LoginTimeout) { | 109 TEST_F(OnlineAttemptTest, LoginTimeout) { |
132 LoginFailure error(LoginFailure::LOGIN_TIMED_OUT); | 110 LoginFailure error(LoginFailure::LOGIN_TIMED_OUT); |
133 TestingProfile profile; | 111 TestingProfile profile; |
134 | 112 |
135 EXPECT_CALL(*(resolver_.get()), Resolve()) | 113 base::RunLoop run_loop; |
136 .WillOnce(Invoke(OnlineAttemptTest::Quit)) | 114 EXPECT_CALL(resolver_, Resolve()) |
| 115 .WillOnce(Invoke(&run_loop, &base::RunLoop::Quit)) |
137 .RetiresOnSaturation(); | 116 .RetiresOnSaturation(); |
138 | 117 |
139 // This is how we inject fake URLFetcher objects, with a factory. | 118 // This is how we inject fake URLFetcher objects, with a factory. |
140 // This factory creates fake URLFetchers that Start() a fake fetch attempt | 119 // This factory creates fake URLFetchers that Start() a fake fetch attempt |
141 // and then come back on the UI thread saying they've been canceled. | 120 // and then come back on the UI thread saying they've been canceled. |
142 MockURLFetcherFactory<ExpectCanceledFetcher> factory; | 121 MockURLFetcherFactory<ExpectCanceledFetcher> factory; |
143 | 122 |
144 attempt_->Initiate(&profile); | 123 attempt_->Initiate(&profile); |
145 BrowserThread::PostTask( | |
146 BrowserThread::UI, FROM_HERE, | |
147 base::Bind(&OnlineAttemptTest::RunThreadTest)); | |
148 | 124 |
149 // Post a task to cancel the login attempt. | 125 // Post a task to cancel the login attempt. |
150 CancelLogin(attempt_.get()); | 126 CancelLogin(attempt_.get()); |
151 | 127 |
152 base::MessageLoop::current()->Run(); | 128 run_loop.Run(); |
153 | 129 |
154 EXPECT_EQ(LoginFailure::LOGIN_TIMED_OUT, state_.online_outcome().reason()); | 130 EXPECT_EQ(LoginFailure::LOGIN_TIMED_OUT, state_.online_outcome().reason()); |
155 } | 131 } |
156 | 132 |
157 TEST_F(OnlineAttemptTest, HostedLoginRejected) { | 133 TEST_F(OnlineAttemptTest, HostedLoginRejected) { |
158 LoginFailure error( | 134 LoginFailure error( |
159 LoginFailure::FromNetworkAuthFailure( | 135 LoginFailure::FromNetworkAuthFailure( |
160 GoogleServiceAuthError( | 136 GoogleServiceAuthError( |
161 GoogleServiceAuthError::HOSTED_NOT_ALLOWED))); | 137 GoogleServiceAuthError::HOSTED_NOT_ALLOWED))); |
162 TestingProfile profile; | 138 TestingProfile profile; |
163 | 139 |
164 EXPECT_CALL(*(resolver_.get()), Resolve()) | 140 base::RunLoop run_loop; |
165 .WillOnce(Invoke(OnlineAttemptTest::Quit)) | 141 EXPECT_CALL(resolver_, Resolve()) |
| 142 .WillOnce(Invoke(&run_loop, &base::RunLoop::Quit)) |
166 .RetiresOnSaturation(); | 143 .RetiresOnSaturation(); |
167 | 144 |
168 // This is how we inject fake URLFetcher objects, with a factory. | 145 // This is how we inject fake URLFetcher objects, with a factory. |
169 MockURLFetcherFactory<HostedFetcher> factory; | 146 MockURLFetcherFactory<HostedFetcher> factory; |
170 | 147 |
171 TestAttemptState local_state(UserContext(), "", "", "", | 148 TestAttemptState local_state(UserContext(), "", "", "", |
172 User::USER_TYPE_REGULAR, true); | 149 User::USER_TYPE_REGULAR, true); |
173 attempt_.reset(new OnlineAttempt(&local_state, resolver_.get())); | 150 attempt_.reset(new OnlineAttempt(&local_state, &resolver_)); |
174 attempt_->Initiate(&profile); | 151 attempt_->Initiate(&profile); |
175 BrowserThread::PostTask( | |
176 BrowserThread::UI, FROM_HERE, | |
177 base::Bind(&OnlineAttemptTest::RunThreadTest)); | |
178 | 152 |
179 base::MessageLoop::current()->Run(); | 153 run_loop.Run(); |
180 | 154 |
181 EXPECT_EQ(error, local_state.online_outcome()); | 155 EXPECT_EQ(error, local_state.online_outcome()); |
182 EXPECT_EQ(LoginFailure::NETWORK_AUTH_FAILED, | 156 EXPECT_EQ(LoginFailure::NETWORK_AUTH_FAILED, |
183 local_state.online_outcome().reason()); | 157 local_state.online_outcome().reason()); |
184 } | 158 } |
185 | 159 |
186 TEST_F(OnlineAttemptTest, FullLogin) { | 160 TEST_F(OnlineAttemptTest, FullLogin) { |
187 TestingProfile profile; | 161 TestingProfile profile; |
188 | 162 |
189 EXPECT_CALL(*(resolver_.get()), Resolve()) | 163 base::RunLoop run_loop; |
190 .WillOnce(Invoke(OnlineAttemptTest::Quit)) | 164 EXPECT_CALL(resolver_, Resolve()) |
| 165 .WillOnce(Invoke(&run_loop, &base::RunLoop::Quit)) |
191 .RetiresOnSaturation(); | 166 .RetiresOnSaturation(); |
192 | 167 |
193 // This is how we inject fake URLFetcher objects, with a factory. | 168 // This is how we inject fake URLFetcher objects, with a factory. |
194 MockURLFetcherFactory<SuccessFetcher> factory; | 169 MockURLFetcherFactory<SuccessFetcher> factory; |
195 | 170 |
196 TestAttemptState local_state(UserContext(), "", "", "", | 171 TestAttemptState local_state(UserContext(), "", "", "", |
197 User::USER_TYPE_REGULAR, true); | 172 User::USER_TYPE_REGULAR, true); |
198 attempt_.reset(new OnlineAttempt(&local_state, resolver_.get())); | 173 attempt_.reset(new OnlineAttempt(&local_state, &resolver_)); |
199 attempt_->Initiate(&profile); | 174 attempt_->Initiate(&profile); |
200 BrowserThread::PostTask( | |
201 BrowserThread::UI, FROM_HERE, | |
202 base::Bind(&OnlineAttemptTest::RunThreadTest)); | |
203 | 175 |
204 base::MessageLoop::current()->Run(); | 176 run_loop.Run(); |
205 | 177 |
206 EXPECT_EQ(LoginFailure::LoginFailureNone(), local_state.online_outcome()); | 178 EXPECT_EQ(LoginFailure::LoginFailureNone(), local_state.online_outcome()); |
207 } | 179 } |
208 | 180 |
209 TEST_F(OnlineAttemptTest, LoginNetFailure) { | 181 TEST_F(OnlineAttemptTest, LoginNetFailure) { |
210 RunFailureTest( | 182 RunFailureTest( |
211 GoogleServiceAuthError::FromConnectionError(net::ERR_CONNECTION_RESET)); | 183 GoogleServiceAuthError::FromConnectionError(net::ERR_CONNECTION_RESET)); |
212 } | 184 } |
213 | 185 |
214 TEST_F(OnlineAttemptTest, LoginDenied) { | 186 TEST_F(OnlineAttemptTest, LoginDenied) { |
(...skipping 19 matching lines...) Expand all Loading... |
234 TEST_F(OnlineAttemptTest, CaptchaErrorOutputted) { | 206 TEST_F(OnlineAttemptTest, CaptchaErrorOutputted) { |
235 GoogleServiceAuthError auth_error = | 207 GoogleServiceAuthError auth_error = |
236 GoogleServiceAuthError::FromClientLoginCaptchaChallenge( | 208 GoogleServiceAuthError::FromClientLoginCaptchaChallenge( |
237 "CCTOKEN", | 209 "CCTOKEN", |
238 GURL("http://accounts.google.com/Captcha?ctoken=CCTOKEN"), | 210 GURL("http://accounts.google.com/Captcha?ctoken=CCTOKEN"), |
239 GURL("http://www.google.com/login/captcha")); | 211 GURL("http://www.google.com/login/captcha")); |
240 RunFailureTest(auth_error); | 212 RunFailureTest(auth_error); |
241 } | 213 } |
242 | 214 |
243 TEST_F(OnlineAttemptTest, TwoFactorSuccess) { | 215 TEST_F(OnlineAttemptTest, TwoFactorSuccess) { |
244 EXPECT_CALL(*(resolver_.get()), Resolve()) | 216 EXPECT_CALL(resolver_, Resolve()) |
245 .Times(1) | 217 .Times(1) |
246 .RetiresOnSaturation(); | 218 .RetiresOnSaturation(); |
247 GoogleServiceAuthError error(GoogleServiceAuthError::TWO_FACTOR); | 219 GoogleServiceAuthError error(GoogleServiceAuthError::TWO_FACTOR); |
248 BrowserThread::PostTask( | 220 BrowserThread::PostTask( |
249 BrowserThread::UI, FROM_HERE, | 221 BrowserThread::UI, FROM_HERE, |
250 base::Bind(&OnlineAttempt::OnClientLoginFailure, | 222 base::Bind(&OnlineAttempt::OnClientLoginFailure, |
251 attempt_->weak_factory_.GetWeakPtr(), | 223 attempt_->weak_factory_.GetWeakPtr(), |
252 error)); | 224 error)); |
253 | 225 |
254 // Force UI thread to finish tasks so I can verify |state_|. | 226 // Force UI thread to finish tasks so I can verify |state_|. |
255 message_loop_.RunUntilIdle(); | 227 base::RunLoop().RunUntilIdle(); |
256 EXPECT_TRUE(GoogleServiceAuthError::AuthErrorNone() == | 228 EXPECT_TRUE(GoogleServiceAuthError::AuthErrorNone() == |
257 state_.online_outcome().error()); | 229 state_.online_outcome().error()); |
258 } | 230 } |
259 | 231 |
260 } // namespace chromeos | 232 } // namespace chromeos |
OLD | NEW |