Chromium Code Reviews| 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" | |
| 13 #include "chromeos/login/auth/auth_attempt_state.h" | 10 #include "chromeos/login/auth/auth_attempt_state.h" |
| 14 #include "chromeos/login/auth/mock_auth_attempt_state_resolver.h" | 11 #include "chromeos/login/auth/mock_auth_attempt_state_resolver.h" |
| 12 #include "chromeos/login/auth/mock_url_fetchers.h" | |
| 13 #include "chromeos/login/auth/online_attempt.h" | |
| 15 #include "chromeos/login/auth/test_attempt_state.h" | 14 #include "chromeos/login/auth/test_attempt_state.h" |
| 16 #include "chromeos/login/auth/user_context.h" | 15 #include "chromeos/login/auth/user_context.h" |
| 17 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/test/test_browser_context.h" | |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
|
stevenjb
2014/07/22 16:35:37
Layering violation.
Denis Kuznetsov (DE-MUC)
2014/07/24 15:22:13
Done.
| |
| 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 using content::TestBrowserContext; | |
| 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 : state_(UserContext(), false), | 37 : state_(UserContext(), false), |
| 37 attempt_(new OnlineAttempt(&state_, &resolver_)) { | 38 attempt_(new OnlineAttempt(&state_, &resolver_)) {} |
| 38 } | |
| 39 | 39 |
| 40 void RunFailureTest(const GoogleServiceAuthError& error) { | 40 void RunFailureTest(const GoogleServiceAuthError& error) { |
| 41 EXPECT_CALL(resolver_, Resolve()) | 41 EXPECT_CALL(resolver_, Resolve()).Times(1).RetiresOnSaturation(); |
| 42 .Times(1) | |
| 43 .RetiresOnSaturation(); | |
| 44 | 42 |
| 45 BrowserThread::PostTask( | 43 BrowserThread::PostTask(BrowserThread::UI, |
| 46 BrowserThread::UI, FROM_HERE, | 44 FROM_HERE, |
| 47 base::Bind(&OnlineAttempt::OnClientLoginFailure, | 45 base::Bind(&OnlineAttempt::OnClientLoginFailure, |
| 48 attempt_->weak_factory_.GetWeakPtr(), | 46 attempt_->weak_factory_.GetWeakPtr(), |
| 49 error)); | 47 error)); |
| 50 // Force UI thread to finish tasks so I can verify |state_|. | 48 // Force UI thread to finish tasks so I can verify |state_|. |
| 51 base::RunLoop().RunUntilIdle(); | 49 base::RunLoop().RunUntilIdle(); |
| 52 EXPECT_TRUE(error == state_.online_outcome().error()); | 50 EXPECT_TRUE(error == state_.online_outcome().error()); |
| 53 } | 51 } |
| 54 | 52 |
| 55 void CancelLogin(OnlineAttempt* auth) { | 53 void CancelLogin(OnlineAttempt* auth) { |
| 56 BrowserThread::PostTask( | 54 BrowserThread::PostTask(BrowserThread::UI, |
| 57 BrowserThread::UI, FROM_HERE, | 55 FROM_HERE, |
| 58 base::Bind(&OnlineAttempt::CancelClientLogin, | 56 base::Bind(&OnlineAttempt::CancelClientLogin, |
| 59 auth->weak_factory_.GetWeakPtr())); | 57 auth->weak_factory_.GetWeakPtr())); |
| 60 } | 58 } |
| 61 | 59 |
| 62 content::TestBrowserThreadBundle thread_bundle_; | 60 content::TestBrowserThreadBundle thread_bundle_; |
| 63 TestAttemptState state_; | 61 TestAttemptState state_; |
| 64 MockAuthAttemptStateResolver resolver_; | 62 MockAuthAttemptStateResolver resolver_; |
| 65 scoped_ptr<OnlineAttempt> attempt_; | 63 scoped_ptr<OnlineAttempt> attempt_; |
| 66 }; | 64 }; |
| 67 | 65 |
| 68 TEST_F(OnlineAttemptTest, LoginSuccess) { | 66 TEST_F(OnlineAttemptTest, LoginSuccess) { |
| 69 EXPECT_CALL(resolver_, Resolve()) | 67 EXPECT_CALL(resolver_, Resolve()).Times(1).RetiresOnSaturation(); |
| 70 .Times(1) | |
| 71 .RetiresOnSaturation(); | |
| 72 | 68 |
| 73 BrowserThread::PostTask( | 69 BrowserThread::PostTask(BrowserThread::UI, |
| 74 BrowserThread::UI, FROM_HERE, | 70 FROM_HERE, |
| 75 base::Bind(&OnlineAttempt::OnClientLoginSuccess, | 71 base::Bind(&OnlineAttempt::OnClientLoginSuccess, |
| 76 attempt_->weak_factory_.GetWeakPtr(), | 72 attempt_->weak_factory_.GetWeakPtr(), |
| 77 GaiaAuthConsumer::ClientLoginResult())); | 73 GaiaAuthConsumer::ClientLoginResult())); |
| 78 // Force UI thread to finish tasks so I can verify |state_|. | 74 // Force UI thread to finish tasks so I can verify |state_|. |
| 79 base::RunLoop().RunUntilIdle(); | 75 base::RunLoop().RunUntilIdle(); |
| 80 } | 76 } |
| 81 | 77 |
| 82 TEST_F(OnlineAttemptTest, LoginCancelRetry) { | 78 TEST_F(OnlineAttemptTest, LoginCancelRetry) { |
| 83 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); | 79 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); |
| 84 TestingProfile profile; | 80 TestBrowserContext context; |
| 85 | 81 |
| 86 base::RunLoop run_loop; | 82 base::RunLoop run_loop; |
| 87 EXPECT_CALL(resolver_, Resolve()) | 83 EXPECT_CALL(resolver_, Resolve()) |
| 88 .WillOnce(Invoke(&run_loop, &base::RunLoop::Quit)) | 84 .WillOnce(Invoke(&run_loop, &base::RunLoop::Quit)) |
| 89 .RetiresOnSaturation(); | 85 .RetiresOnSaturation(); |
| 90 | 86 |
| 91 // This is how we inject fake URLFetcher objects, with a factory. | 87 // This is how we inject fake URLFetcher objects, with a factory. |
| 92 // This factory creates fake URLFetchers that Start() a fake fetch attempt | 88 // This factory creates fake URLFetchers that Start() a fake fetch attempt |
| 93 // and then come back on the UI thread saying they've been canceled. | 89 // and then come back on the UI thread saying they've been canceled. |
| 94 MockURLFetcherFactory<GotCanceledFetcher> factory; | 90 MockURLFetcherFactory<GotCanceledFetcher> factory; |
| 95 | 91 |
| 96 attempt_->Initiate(&profile); | 92 attempt_->Initiate(&context); |
| 97 | 93 |
| 98 run_loop.Run(); | 94 run_loop.Run(); |
| 99 | 95 |
| 100 EXPECT_TRUE(error == state_.online_outcome().error()); | 96 EXPECT_TRUE(error == state_.online_outcome().error()); |
| 101 EXPECT_EQ(AuthFailure::NETWORK_AUTH_FAILED, state_.online_outcome().reason()); | 97 EXPECT_EQ(AuthFailure::NETWORK_AUTH_FAILED, state_.online_outcome().reason()); |
| 102 } | 98 } |
| 103 | 99 |
| 104 TEST_F(OnlineAttemptTest, LoginTimeout) { | 100 TEST_F(OnlineAttemptTest, LoginTimeout) { |
| 105 AuthFailure error(AuthFailure::LOGIN_TIMED_OUT); | 101 AuthFailure error(AuthFailure::LOGIN_TIMED_OUT); |
| 106 TestingProfile profile; | 102 TestBrowserContext context; |
| 107 | 103 |
| 108 base::RunLoop run_loop; | 104 base::RunLoop run_loop; |
| 109 EXPECT_CALL(resolver_, Resolve()) | 105 EXPECT_CALL(resolver_, Resolve()) |
| 110 .WillOnce(Invoke(&run_loop, &base::RunLoop::Quit)) | 106 .WillOnce(Invoke(&run_loop, &base::RunLoop::Quit)) |
| 111 .RetiresOnSaturation(); | 107 .RetiresOnSaturation(); |
| 112 | 108 |
| 113 // This is how we inject fake URLFetcher objects, with a factory. | 109 // This is how we inject fake URLFetcher objects, with a factory. |
| 114 // This factory creates fake URLFetchers that Start() a fake fetch attempt | 110 // This factory creates fake URLFetchers that Start() a fake fetch attempt |
| 115 // and then come back on the UI thread saying they've been canceled. | 111 // and then come back on the UI thread saying they've been canceled. |
| 116 MockURLFetcherFactory<ExpectCanceledFetcher> factory; | 112 MockURLFetcherFactory<ExpectCanceledFetcher> factory; |
| 117 | 113 |
| 118 attempt_->Initiate(&profile); | 114 attempt_->Initiate(&context); |
| 119 | 115 |
| 120 // Post a task to cancel the login attempt. | 116 // Post a task to cancel the login attempt. |
| 121 CancelLogin(attempt_.get()); | 117 CancelLogin(attempt_.get()); |
| 122 | 118 |
| 123 run_loop.Run(); | 119 run_loop.Run(); |
| 124 | 120 |
| 125 EXPECT_EQ(AuthFailure::LOGIN_TIMED_OUT, state_.online_outcome().reason()); | 121 EXPECT_EQ(AuthFailure::LOGIN_TIMED_OUT, state_.online_outcome().reason()); |
| 126 } | 122 } |
| 127 | 123 |
| 128 TEST_F(OnlineAttemptTest, HostedLoginRejected) { | 124 TEST_F(OnlineAttemptTest, HostedLoginRejected) { |
| 129 AuthFailure error(AuthFailure::FromNetworkAuthFailure( | 125 AuthFailure error(AuthFailure::FromNetworkAuthFailure( |
| 130 GoogleServiceAuthError(GoogleServiceAuthError::HOSTED_NOT_ALLOWED))); | 126 GoogleServiceAuthError(GoogleServiceAuthError::HOSTED_NOT_ALLOWED))); |
| 131 TestingProfile profile; | 127 TestBrowserContext context; |
| 132 | 128 |
| 133 base::RunLoop run_loop; | 129 base::RunLoop run_loop; |
| 134 EXPECT_CALL(resolver_, Resolve()) | 130 EXPECT_CALL(resolver_, Resolve()) |
| 135 .WillOnce(Invoke(&run_loop, &base::RunLoop::Quit)) | 131 .WillOnce(Invoke(&run_loop, &base::RunLoop::Quit)) |
| 136 .RetiresOnSaturation(); | 132 .RetiresOnSaturation(); |
| 137 | 133 |
| 138 // This is how we inject fake URLFetcher objects, with a factory. | 134 // This is how we inject fake URLFetcher objects, with a factory. |
| 139 MockURLFetcherFactory<HostedFetcher> factory; | 135 MockURLFetcherFactory<HostedFetcher> factory; |
| 140 | 136 |
| 141 TestAttemptState local_state(UserContext(), true); | 137 TestAttemptState local_state(UserContext(), true); |
| 142 attempt_.reset(new OnlineAttempt(&local_state, &resolver_)); | 138 attempt_.reset(new OnlineAttempt(&local_state, &resolver_)); |
| 143 attempt_->Initiate(&profile); | 139 attempt_->Initiate(&context); |
| 144 | 140 |
| 145 run_loop.Run(); | 141 run_loop.Run(); |
| 146 | 142 |
| 147 EXPECT_EQ(error, local_state.online_outcome()); | 143 EXPECT_EQ(error, local_state.online_outcome()); |
| 148 EXPECT_EQ(AuthFailure::NETWORK_AUTH_FAILED, | 144 EXPECT_EQ(AuthFailure::NETWORK_AUTH_FAILED, |
| 149 local_state.online_outcome().reason()); | 145 local_state.online_outcome().reason()); |
| 150 } | 146 } |
| 151 | 147 |
| 152 TEST_F(OnlineAttemptTest, FullLogin) { | 148 TEST_F(OnlineAttemptTest, FullLogin) { |
| 153 TestingProfile profile; | 149 TestBrowserContext context; |
| 154 | 150 |
| 155 base::RunLoop run_loop; | 151 base::RunLoop run_loop; |
| 156 EXPECT_CALL(resolver_, Resolve()) | 152 EXPECT_CALL(resolver_, Resolve()) |
| 157 .WillOnce(Invoke(&run_loop, &base::RunLoop::Quit)) | 153 .WillOnce(Invoke(&run_loop, &base::RunLoop::Quit)) |
| 158 .RetiresOnSaturation(); | 154 .RetiresOnSaturation(); |
| 159 | 155 |
| 160 // This is how we inject fake URLFetcher objects, with a factory. | 156 // This is how we inject fake URLFetcher objects, with a factory. |
| 161 MockURLFetcherFactory<SuccessFetcher> factory; | 157 MockURLFetcherFactory<SuccessFetcher> factory; |
| 162 | 158 |
| 163 TestAttemptState local_state(UserContext(), true); | 159 TestAttemptState local_state(UserContext(), true); |
| 164 attempt_.reset(new OnlineAttempt(&local_state, &resolver_)); | 160 attempt_.reset(new OnlineAttempt(&local_state, &resolver_)); |
| 165 attempt_->Initiate(&profile); | 161 attempt_->Initiate(&context); |
| 166 | 162 |
| 167 run_loop.Run(); | 163 run_loop.Run(); |
| 168 | 164 |
| 169 EXPECT_EQ(AuthFailure::AuthFailureNone(), local_state.online_outcome()); | 165 EXPECT_EQ(AuthFailure::AuthFailureNone(), local_state.online_outcome()); |
| 170 } | 166 } |
| 171 | 167 |
| 172 TEST_F(OnlineAttemptTest, LoginNetFailure) { | 168 TEST_F(OnlineAttemptTest, LoginNetFailure) { |
| 173 RunFailureTest( | 169 RunFailureTest( |
| 174 GoogleServiceAuthError::FromConnectionError(net::ERR_CONNECTION_RESET)); | 170 GoogleServiceAuthError::FromConnectionError(net::ERR_CONNECTION_RESET)); |
| 175 } | 171 } |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 197 TEST_F(OnlineAttemptTest, CaptchaErrorOutputted) { | 193 TEST_F(OnlineAttemptTest, CaptchaErrorOutputted) { |
| 198 GoogleServiceAuthError auth_error = | 194 GoogleServiceAuthError auth_error = |
| 199 GoogleServiceAuthError::FromClientLoginCaptchaChallenge( | 195 GoogleServiceAuthError::FromClientLoginCaptchaChallenge( |
| 200 "CCTOKEN", | 196 "CCTOKEN", |
| 201 GURL("http://accounts.google.com/Captcha?ctoken=CCTOKEN"), | 197 GURL("http://accounts.google.com/Captcha?ctoken=CCTOKEN"), |
| 202 GURL("http://www.google.com/login/captcha")); | 198 GURL("http://www.google.com/login/captcha")); |
| 203 RunFailureTest(auth_error); | 199 RunFailureTest(auth_error); |
| 204 } | 200 } |
| 205 | 201 |
| 206 TEST_F(OnlineAttemptTest, TwoFactorSuccess) { | 202 TEST_F(OnlineAttemptTest, TwoFactorSuccess) { |
| 207 EXPECT_CALL(resolver_, Resolve()) | 203 EXPECT_CALL(resolver_, Resolve()).Times(1).RetiresOnSaturation(); |
| 208 .Times(1) | |
| 209 .RetiresOnSaturation(); | |
| 210 GoogleServiceAuthError error(GoogleServiceAuthError::TWO_FACTOR); | 204 GoogleServiceAuthError error(GoogleServiceAuthError::TWO_FACTOR); |
| 211 BrowserThread::PostTask( | 205 BrowserThread::PostTask(BrowserThread::UI, |
| 212 BrowserThread::UI, FROM_HERE, | 206 FROM_HERE, |
| 213 base::Bind(&OnlineAttempt::OnClientLoginFailure, | 207 base::Bind(&OnlineAttempt::OnClientLoginFailure, |
| 214 attempt_->weak_factory_.GetWeakPtr(), | 208 attempt_->weak_factory_.GetWeakPtr(), |
| 215 error)); | 209 error)); |
| 216 | 210 |
| 217 // Force UI thread to finish tasks so I can verify |state_|. | 211 // Force UI thread to finish tasks so I can verify |state_|. |
| 218 base::RunLoop().RunUntilIdle(); | 212 base::RunLoop().RunUntilIdle(); |
| 219 EXPECT_TRUE(GoogleServiceAuthError::AuthErrorNone() == | 213 EXPECT_TRUE(GoogleServiceAuthError::AuthErrorNone() == |
| 220 state_.online_outcome().error()); | 214 state_.online_outcome().error()); |
| 221 } | 215 } |
| 222 | 216 |
| 223 } // namespace chromeos | 217 } // namespace chromeos |
| OLD | NEW |