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