ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / chromeos / login / auth / online_attempt_unittest.cc
blob8ba68a36f7d7176098c8aefb1fd39a8feffac8f8
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
3 // found in the LICENSE file.
5 #include <string>
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/run_loop.h"
11 #include "base/test/null_task_runner.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "chromeos/login/auth/auth_attempt_state.h"
14 #include "chromeos/login/auth/mock_auth_attempt_state_resolver.h"
15 #include "chromeos/login/auth/mock_url_fetchers.h"
16 #include "chromeos/login/auth/online_attempt.h"
17 #include "chromeos/login/auth/test_attempt_state.h"
18 #include "chromeos/login/auth/user_context.h"
19 #include "google_apis/gaia/gaia_auth_consumer.h"
20 #include "google_apis/gaia/mock_url_fetcher_factory.h"
21 #include "net/url_request/url_request_context.h"
22 #include "net/url_request/url_request_context_getter.h"
23 #include "net/url_request/url_request_test_util.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "url/gurl.h"
28 using ::testing::AnyNumber;
29 using ::testing::Invoke;
30 using ::testing::Return;
31 using ::testing::_;
33 namespace {
35 class TestContextURLRequestContextGetter : public net::URLRequestContextGetter {
36 public:
37 TestContextURLRequestContextGetter()
38 : null_task_runner_(new base::NullTaskRunner) {}
40 net::URLRequestContext* GetURLRequestContext() override { return &context_; }
42 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
43 const override {
44 return null_task_runner_;
47 private:
48 ~TestContextURLRequestContextGetter() override {}
50 net::TestURLRequestContext context_;
51 scoped_refptr<base::SingleThreadTaskRunner> null_task_runner_;
54 } // namespace
56 namespace chromeos {
58 class OnlineAttemptTest : public testing::Test {
59 public:
60 OnlineAttemptTest()
61 : state_(UserContext(), false),
62 attempt_(new OnlineAttempt(&state_, &resolver_)) {}
64 void SetUp() override {
65 message_loop_ = base::MessageLoopProxy::current();
66 request_context_ = new TestContextURLRequestContextGetter();
69 void RunFailureTest(const GoogleServiceAuthError& error) {
70 EXPECT_CALL(resolver_, Resolve()).Times(1).RetiresOnSaturation();
72 message_loop_->PostTask(FROM_HERE,
73 base::Bind(&OnlineAttempt::OnClientLoginFailure,
74 attempt_->weak_factory_.GetWeakPtr(),
75 error));
76 // Force UI thread to finish tasks so I can verify |state_|.
77 base::RunLoop().RunUntilIdle();
78 EXPECT_TRUE(error == state_.online_outcome().error());
81 void CancelLogin(OnlineAttempt* auth) {
82 message_loop_->PostTask(FROM_HERE,
83 base::Bind(&OnlineAttempt::CancelClientLogin,
84 auth->weak_factory_.GetWeakPtr()));
87 scoped_refptr<base::MessageLoopProxy> message_loop_;
88 scoped_refptr<net::URLRequestContextGetter> request_context_;
89 base::MessageLoop loop_;
90 TestAttemptState state_;
91 MockAuthAttemptStateResolver resolver_;
92 scoped_ptr<OnlineAttempt> attempt_;
95 TEST_F(OnlineAttemptTest, LoginSuccess) {
96 EXPECT_CALL(resolver_, Resolve()).Times(1).RetiresOnSaturation();
98 message_loop_->PostTask(FROM_HERE,
99 base::Bind(&OnlineAttempt::OnClientLoginSuccess,
100 attempt_->weak_factory_.GetWeakPtr(),
101 GaiaAuthConsumer::ClientLoginResult()));
102 // Force UI thread to finish tasks so I can verify |state_|.
103 base::RunLoop().RunUntilIdle();
106 TEST_F(OnlineAttemptTest, LoginCancelRetry) {
107 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED);
108 TestingProfile profile;
110 base::RunLoop run_loop;
111 EXPECT_CALL(resolver_, Resolve())
112 .WillOnce(Invoke(&run_loop, &base::RunLoop::Quit))
113 .RetiresOnSaturation();
115 // This is how we inject fake URLFetcher objects, with a factory.
116 // This factory creates fake URLFetchers that Start() a fake fetch attempt
117 // and then come back on the UI thread saying they've been canceled.
118 MockURLFetcherFactory<GotCanceledFetcher> factory;
120 attempt_->Initiate(request_context_.get());
122 run_loop.Run();
124 EXPECT_TRUE(error == state_.online_outcome().error());
125 EXPECT_EQ(AuthFailure::NETWORK_AUTH_FAILED, state_.online_outcome().reason());
128 TEST_F(OnlineAttemptTest, LoginTimeout) {
129 AuthFailure error(AuthFailure::LOGIN_TIMED_OUT);
130 TestingProfile profile;
132 base::RunLoop run_loop;
133 EXPECT_CALL(resolver_, Resolve())
134 .WillOnce(Invoke(&run_loop, &base::RunLoop::Quit))
135 .RetiresOnSaturation();
137 // This is how we inject fake URLFetcher objects, with a factory.
138 // This factory creates fake URLFetchers that Start() a fake fetch attempt
139 // and then come back on the UI thread saying they've been canceled.
140 MockURLFetcherFactory<ExpectCanceledFetcher> factory;
142 attempt_->Initiate(request_context_.get());
144 // Post a task to cancel the login attempt.
145 CancelLogin(attempt_.get());
147 run_loop.Run();
149 EXPECT_EQ(AuthFailure::LOGIN_TIMED_OUT, state_.online_outcome().reason());
152 TEST_F(OnlineAttemptTest, HostedLoginRejected) {
153 AuthFailure error(AuthFailure::FromNetworkAuthFailure(
154 GoogleServiceAuthError(GoogleServiceAuthError::HOSTED_NOT_ALLOWED)));
155 TestingProfile profile;
157 base::RunLoop run_loop;
158 EXPECT_CALL(resolver_, Resolve())
159 .WillOnce(Invoke(&run_loop, &base::RunLoop::Quit))
160 .RetiresOnSaturation();
162 // This is how we inject fake URLFetcher objects, with a factory.
163 MockURLFetcherFactory<HostedFetcher> factory;
165 TestAttemptState local_state(UserContext(), true);
166 attempt_.reset(new OnlineAttempt(&local_state, &resolver_));
167 attempt_->Initiate(request_context_.get());
169 run_loop.Run();
171 EXPECT_EQ(error, local_state.online_outcome());
172 EXPECT_EQ(AuthFailure::NETWORK_AUTH_FAILED,
173 local_state.online_outcome().reason());
176 TEST_F(OnlineAttemptTest, FullLogin) {
177 TestingProfile profile;
179 base::RunLoop run_loop;
180 EXPECT_CALL(resolver_, Resolve())
181 .WillOnce(Invoke(&run_loop, &base::RunLoop::Quit))
182 .RetiresOnSaturation();
184 // This is how we inject fake URLFetcher objects, with a factory.
185 MockURLFetcherFactory<SuccessFetcher> factory;
187 TestAttemptState local_state(UserContext(), true);
188 attempt_.reset(new OnlineAttempt(&local_state, &resolver_));
189 attempt_->Initiate(request_context_.get());
191 run_loop.Run();
193 EXPECT_EQ(AuthFailure::AuthFailureNone(), local_state.online_outcome());
196 TEST_F(OnlineAttemptTest, LoginNetFailure) {
197 RunFailureTest(
198 GoogleServiceAuthError::FromConnectionError(net::ERR_CONNECTION_RESET));
201 TEST_F(OnlineAttemptTest, LoginDenied) {
202 RunFailureTest(
203 GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
206 TEST_F(OnlineAttemptTest, LoginAccountDisabled) {
207 RunFailureTest(
208 GoogleServiceAuthError(GoogleServiceAuthError::ACCOUNT_DISABLED));
211 TEST_F(OnlineAttemptTest, LoginAccountDeleted) {
212 RunFailureTest(
213 GoogleServiceAuthError(GoogleServiceAuthError::ACCOUNT_DELETED));
216 TEST_F(OnlineAttemptTest, LoginServiceUnavailable) {
217 RunFailureTest(
218 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE));
221 TEST_F(OnlineAttemptTest, CaptchaErrorOutputted) {
222 GoogleServiceAuthError auth_error =
223 GoogleServiceAuthError::FromClientLoginCaptchaChallenge(
224 "CCTOKEN",
225 GURL("http://accounts.google.com/Captcha?ctoken=CCTOKEN"),
226 GURL("http://www.google.com/login/captcha"));
227 RunFailureTest(auth_error);
230 TEST_F(OnlineAttemptTest, TwoFactorSuccess) {
231 EXPECT_CALL(resolver_, Resolve()).Times(1).RetiresOnSaturation();
232 GoogleServiceAuthError error(GoogleServiceAuthError::TWO_FACTOR);
233 message_loop_->PostTask(FROM_HERE,
234 base::Bind(&OnlineAttempt::OnClientLoginFailure,
235 attempt_->weak_factory_.GetWeakPtr(),
236 error));
238 // Force UI thread to finish tasks so I can verify |state_|.
239 base::RunLoop().RunUntilIdle();
240 EXPECT_TRUE(GoogleServiceAuthError::AuthErrorNone() ==
241 state_.online_outcome().error());
244 } // namespace chromeos