| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "chrome/browser/chromeos/login/auth/mock_url_fetchers.h" | |
| 11 #include "chrome/browser/chromeos/login/auth/online_attempt.h" | |
| 12 #include "chrome/test/base/testing_profile.h" | 10 #include "chrome/test/base/testing_profile.h" |
| 13 #include "chromeos/login/auth/auth_attempt_state.h" | 11 #include "chromeos/login/auth/auth_attempt_state.h" |
| 14 #include "chromeos/login/auth/mock_auth_attempt_state_resolver.h" | 12 #include "chromeos/login/auth/mock_auth_attempt_state_resolver.h" |
| 13 #include "chromeos/login/auth/mock_url_fetchers.h" |
| 14 #include "chromeos/login/auth/online_attempt.h" |
| 15 #include "chromeos/login/auth/test_attempt_state.h" | 15 #include "chromeos/login/auth/test_attempt_state.h" |
| 16 #include "chromeos/login/auth/user_context.h" | 16 #include "chromeos/login/auth/user_context.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "google_apis/gaia/gaia_auth_consumer.h" | 19 #include "google_apis/gaia/gaia_auth_consumer.h" |
| 20 #include "google_apis/gaia/mock_url_fetcher_factory.h" | 20 #include "google_apis/gaia/mock_url_fetcher_factory.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 24 | 24 |
| 25 using ::testing::AnyNumber; | 25 using ::testing::AnyNumber; |
| 26 using ::testing::Invoke; | 26 using ::testing::Invoke; |
| 27 using ::testing::Return; | 27 using ::testing::Return; |
| 28 using ::testing::_; | 28 using ::testing::_; |
| 29 using content::BrowserThread; | 29 using content::BrowserThread; |
| 30 | 30 |
| 31 namespace chromeos { | 31 namespace chromeos { |
| 32 | 32 |
| 33 class OnlineAttemptTest : public testing::Test { | 33 class OnlineAttemptTest : public testing::Test { |
| 34 public: | 34 public: |
| 35 OnlineAttemptTest() | 35 OnlineAttemptTest() |
| 36 : state_(UserContext(), false), | 36 : state_(UserContext(), false), |
| 37 attempt_(new OnlineAttempt(&state_, &resolver_)) { | 37 attempt_(new OnlineAttempt(&state_, &resolver_)) {} |
| 38 } | |
| 39 | 38 |
| 40 void RunFailureTest(const GoogleServiceAuthError& error) { | 39 void RunFailureTest(const GoogleServiceAuthError& error) { |
| 41 EXPECT_CALL(resolver_, Resolve()) | 40 EXPECT_CALL(resolver_, Resolve()).Times(1).RetiresOnSaturation(); |
| 42 .Times(1) | |
| 43 .RetiresOnSaturation(); | |
| 44 | 41 |
| 45 BrowserThread::PostTask( | 42 BrowserThread::PostTask(BrowserThread::UI, |
| 46 BrowserThread::UI, FROM_HERE, | 43 FROM_HERE, |
| 47 base::Bind(&OnlineAttempt::OnClientLoginFailure, | 44 base::Bind(&OnlineAttempt::OnClientLoginFailure, |
| 48 attempt_->weak_factory_.GetWeakPtr(), | 45 attempt_->weak_factory_.GetWeakPtr(), |
| 49 error)); | 46 error)); |
| 50 // Force UI thread to finish tasks so I can verify |state_|. | 47 // Force UI thread to finish tasks so I can verify |state_|. |
| 51 base::RunLoop().RunUntilIdle(); | 48 base::RunLoop().RunUntilIdle(); |
| 52 EXPECT_TRUE(error == state_.online_outcome().error()); | 49 EXPECT_TRUE(error == state_.online_outcome().error()); |
| 53 } | 50 } |
| 54 | 51 |
| 55 void CancelLogin(OnlineAttempt* auth) { | 52 void CancelLogin(OnlineAttempt* auth) { |
| 56 BrowserThread::PostTask( | 53 BrowserThread::PostTask(BrowserThread::UI, |
| 57 BrowserThread::UI, FROM_HERE, | 54 FROM_HERE, |
| 58 base::Bind(&OnlineAttempt::CancelClientLogin, | 55 base::Bind(&OnlineAttempt::CancelClientLogin, |
| 59 auth->weak_factory_.GetWeakPtr())); | 56 auth->weak_factory_.GetWeakPtr())); |
| 60 } | 57 } |
| 61 | 58 |
| 62 content::TestBrowserThreadBundle thread_bundle_; | 59 content::TestBrowserThreadBundle thread_bundle_; |
| 63 TestAttemptState state_; | 60 TestAttemptState state_; |
| 64 MockAuthAttemptStateResolver resolver_; | 61 MockAuthAttemptStateResolver resolver_; |
| 65 scoped_ptr<OnlineAttempt> attempt_; | 62 scoped_ptr<OnlineAttempt> attempt_; |
| 66 }; | 63 }; |
| 67 | 64 |
| 68 TEST_F(OnlineAttemptTest, LoginSuccess) { | 65 TEST_F(OnlineAttemptTest, LoginSuccess) { |
| 69 EXPECT_CALL(resolver_, Resolve()) | 66 EXPECT_CALL(resolver_, Resolve()).Times(1).RetiresOnSaturation(); |
| 70 .Times(1) | |
| 71 .RetiresOnSaturation(); | |
| 72 | 67 |
| 73 BrowserThread::PostTask( | 68 BrowserThread::PostTask(BrowserThread::UI, |
| 74 BrowserThread::UI, FROM_HERE, | 69 FROM_HERE, |
| 75 base::Bind(&OnlineAttempt::OnClientLoginSuccess, | 70 base::Bind(&OnlineAttempt::OnClientLoginSuccess, |
| 76 attempt_->weak_factory_.GetWeakPtr(), | 71 attempt_->weak_factory_.GetWeakPtr(), |
| 77 GaiaAuthConsumer::ClientLoginResult())); | 72 GaiaAuthConsumer::ClientLoginResult())); |
| 78 // Force UI thread to finish tasks so I can verify |state_|. | 73 // Force UI thread to finish tasks so I can verify |state_|. |
| 79 base::RunLoop().RunUntilIdle(); | 74 base::RunLoop().RunUntilIdle(); |
| 80 } | 75 } |
| 81 | 76 |
| 82 TEST_F(OnlineAttemptTest, LoginCancelRetry) { | 77 TEST_F(OnlineAttemptTest, LoginCancelRetry) { |
| 83 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); | 78 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); |
| 84 TestingProfile profile; | 79 TestingProfile profile; |
| 85 | 80 |
| 86 base::RunLoop run_loop; | 81 base::RunLoop run_loop; |
| 87 EXPECT_CALL(resolver_, Resolve()) | 82 EXPECT_CALL(resolver_, Resolve()) |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 TEST_F(OnlineAttemptTest, CaptchaErrorOutputted) { | 192 TEST_F(OnlineAttemptTest, CaptchaErrorOutputted) { |
| 198 GoogleServiceAuthError auth_error = | 193 GoogleServiceAuthError auth_error = |
| 199 GoogleServiceAuthError::FromClientLoginCaptchaChallenge( | 194 GoogleServiceAuthError::FromClientLoginCaptchaChallenge( |
| 200 "CCTOKEN", | 195 "CCTOKEN", |
| 201 GURL("http://accounts.google.com/Captcha?ctoken=CCTOKEN"), | 196 GURL("http://accounts.google.com/Captcha?ctoken=CCTOKEN"), |
| 202 GURL("http://www.google.com/login/captcha")); | 197 GURL("http://www.google.com/login/captcha")); |
| 203 RunFailureTest(auth_error); | 198 RunFailureTest(auth_error); |
| 204 } | 199 } |
| 205 | 200 |
| 206 TEST_F(OnlineAttemptTest, TwoFactorSuccess) { | 201 TEST_F(OnlineAttemptTest, TwoFactorSuccess) { |
| 207 EXPECT_CALL(resolver_, Resolve()) | 202 EXPECT_CALL(resolver_, Resolve()).Times(1).RetiresOnSaturation(); |
| 208 .Times(1) | |
| 209 .RetiresOnSaturation(); | |
| 210 GoogleServiceAuthError error(GoogleServiceAuthError::TWO_FACTOR); | 203 GoogleServiceAuthError error(GoogleServiceAuthError::TWO_FACTOR); |
| 211 BrowserThread::PostTask( | 204 BrowserThread::PostTask(BrowserThread::UI, |
| 212 BrowserThread::UI, FROM_HERE, | 205 FROM_HERE, |
| 213 base::Bind(&OnlineAttempt::OnClientLoginFailure, | 206 base::Bind(&OnlineAttempt::OnClientLoginFailure, |
| 214 attempt_->weak_factory_.GetWeakPtr(), | 207 attempt_->weak_factory_.GetWeakPtr(), |
| 215 error)); | 208 error)); |
| 216 | 209 |
| 217 // Force UI thread to finish tasks so I can verify |state_|. | 210 // Force UI thread to finish tasks so I can verify |state_|. |
| 218 base::RunLoop().RunUntilIdle(); | 211 base::RunLoop().RunUntilIdle(); |
| 219 EXPECT_TRUE(GoogleServiceAuthError::AuthErrorNone() == | 212 EXPECT_TRUE(GoogleServiceAuthError::AuthErrorNone() == |
| 220 state_.online_outcome().error()); | 213 state_.online_outcome().error()); |
| 221 } | 214 } |
| 222 | 215 |
| 223 } // namespace chromeos | 216 } // namespace chromeos |
| OLD | NEW |