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 "chrome/browser/chromeos/cros/cros_library.h" | 10 #include "chrome/browser/chromeos/cros/cros_library.h" |
(...skipping 15 matching lines...) Expand all Loading... |
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 : message_loop_(MessageLoop::TYPE_UI), | 36 : message_loop_(base::MessageLoop::TYPE_UI), |
37 ui_thread_(BrowserThread::UI, &message_loop_), | 37 ui_thread_(BrowserThread::UI, &message_loop_), |
38 state_(UserContext(), "", "", "", User::USER_TYPE_REGULAR, false), | 38 state_(UserContext(), "", "", "", User::USER_TYPE_REGULAR, false), |
39 resolver_(new MockAuthAttemptStateResolver) { | 39 resolver_(new MockAuthAttemptStateResolver) {} |
40 } | |
41 | 40 |
42 virtual ~OnlineAttemptTest() {} | 41 virtual ~OnlineAttemptTest() {} |
43 | 42 |
44 virtual void SetUp() { | 43 virtual void SetUp() { |
45 attempt_.reset(new OnlineAttempt(&state_, resolver_.get())); | 44 attempt_.reset(new OnlineAttempt(&state_, resolver_.get())); |
46 } | 45 } |
47 | 46 |
48 virtual void TearDown() { | 47 virtual void TearDown() { |
49 } | 48 } |
50 | 49 |
(...skipping 14 matching lines...) Expand all Loading... |
65 | 64 |
66 void CancelLogin(OnlineAttempt* auth) { | 65 void CancelLogin(OnlineAttempt* auth) { |
67 BrowserThread::PostTask( | 66 BrowserThread::PostTask( |
68 BrowserThread::UI, FROM_HERE, | 67 BrowserThread::UI, FROM_HERE, |
69 base::Bind(&OnlineAttempt::CancelClientLogin, | 68 base::Bind(&OnlineAttempt::CancelClientLogin, |
70 auth->weak_factory_.GetWeakPtr())); | 69 auth->weak_factory_.GetWeakPtr())); |
71 } | 70 } |
72 | 71 |
73 static void Quit() { | 72 static void Quit() { |
74 BrowserThread::PostTask( | 73 BrowserThread::PostTask( |
75 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); | 74 BrowserThread::UI, FROM_HERE, base::MessageLoop::QuitClosure()); |
76 } | 75 } |
77 | 76 |
78 static void RunThreadTest() { | 77 static void RunThreadTest() { |
79 MessageLoop::current()->RunUntilIdle(); | 78 base::MessageLoop::current()->RunUntilIdle(); |
80 } | 79 } |
81 | 80 |
82 MessageLoop message_loop_; | 81 base::MessageLoop message_loop_; |
83 content::TestBrowserThread ui_thread_; | 82 content::TestBrowserThread ui_thread_; |
84 TestAttemptState state_; | 83 TestAttemptState state_; |
85 scoped_ptr<MockAuthAttemptStateResolver> resolver_; | 84 scoped_ptr<MockAuthAttemptStateResolver> resolver_; |
86 scoped_ptr<OnlineAttempt> attempt_; | 85 scoped_ptr<OnlineAttempt> attempt_; |
87 | 86 |
88 // Initializes / shuts down a stub CrosLibrary. | 87 // Initializes / shuts down a stub CrosLibrary. |
89 chromeos::ScopedStubCrosEnabler stub_cros_enabler_; | 88 chromeos::ScopedStubCrosEnabler stub_cros_enabler_; |
90 }; | 89 }; |
91 | 90 |
92 TEST_F(OnlineAttemptTest, LoginSuccess) { | 91 TEST_F(OnlineAttemptTest, LoginSuccess) { |
(...skipping 21 matching lines...) Expand all Loading... |
114 // This is how we inject fake URLFetcher objects, with a factory. | 113 // This is how we inject fake URLFetcher objects, with a factory. |
115 // This factory creates fake URLFetchers that Start() a fake fetch attempt | 114 // 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. | 115 // and then come back on the UI thread saying they've been canceled. |
117 MockURLFetcherFactory<GotCanceledFetcher> factory; | 116 MockURLFetcherFactory<GotCanceledFetcher> factory; |
118 | 117 |
119 attempt_->Initiate(&profile); | 118 attempt_->Initiate(&profile); |
120 BrowserThread::PostTask( | 119 BrowserThread::PostTask( |
121 BrowserThread::UI, FROM_HERE, | 120 BrowserThread::UI, FROM_HERE, |
122 base::Bind(&OnlineAttemptTest::RunThreadTest)); | 121 base::Bind(&OnlineAttemptTest::RunThreadTest)); |
123 | 122 |
124 MessageLoop::current()->Run(); | 123 base::MessageLoop::current()->Run(); |
125 | 124 |
126 EXPECT_TRUE(error == state_.online_outcome().error()); | 125 EXPECT_TRUE(error == state_.online_outcome().error()); |
127 EXPECT_EQ(LoginFailure::NETWORK_AUTH_FAILED, | 126 EXPECT_EQ(LoginFailure::NETWORK_AUTH_FAILED, |
128 state_.online_outcome().reason()); | 127 state_.online_outcome().reason()); |
129 } | 128 } |
130 | 129 |
131 TEST_F(OnlineAttemptTest, LoginTimeout) { | 130 TEST_F(OnlineAttemptTest, LoginTimeout) { |
132 LoginFailure error(LoginFailure::LOGIN_TIMED_OUT); | 131 LoginFailure error(LoginFailure::LOGIN_TIMED_OUT); |
133 TestingProfile profile; | 132 TestingProfile profile; |
134 | 133 |
135 EXPECT_CALL(*(resolver_.get()), Resolve()) | 134 EXPECT_CALL(*(resolver_.get()), Resolve()) |
136 .WillOnce(Invoke(OnlineAttemptTest::Quit)) | 135 .WillOnce(Invoke(OnlineAttemptTest::Quit)) |
137 .RetiresOnSaturation(); | 136 .RetiresOnSaturation(); |
138 | 137 |
139 // This is how we inject fake URLFetcher objects, with a factory. | 138 // This is how we inject fake URLFetcher objects, with a factory. |
140 // This factory creates fake URLFetchers that Start() a fake fetch attempt | 139 // 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. | 140 // and then come back on the UI thread saying they've been canceled. |
142 MockURLFetcherFactory<ExpectCanceledFetcher> factory; | 141 MockURLFetcherFactory<ExpectCanceledFetcher> factory; |
143 | 142 |
144 attempt_->Initiate(&profile); | 143 attempt_->Initiate(&profile); |
145 BrowserThread::PostTask( | 144 BrowserThread::PostTask( |
146 BrowserThread::UI, FROM_HERE, | 145 BrowserThread::UI, FROM_HERE, |
147 base::Bind(&OnlineAttemptTest::RunThreadTest)); | 146 base::Bind(&OnlineAttemptTest::RunThreadTest)); |
148 | 147 |
149 // Post a task to cancel the login attempt. | 148 // Post a task to cancel the login attempt. |
150 CancelLogin(attempt_.get()); | 149 CancelLogin(attempt_.get()); |
151 | 150 |
152 MessageLoop::current()->Run(); | 151 base::MessageLoop::current()->Run(); |
153 | 152 |
154 EXPECT_EQ(LoginFailure::LOGIN_TIMED_OUT, state_.online_outcome().reason()); | 153 EXPECT_EQ(LoginFailure::LOGIN_TIMED_OUT, state_.online_outcome().reason()); |
155 } | 154 } |
156 | 155 |
157 TEST_F(OnlineAttemptTest, HostedLoginRejected) { | 156 TEST_F(OnlineAttemptTest, HostedLoginRejected) { |
158 LoginFailure error( | 157 LoginFailure error( |
159 LoginFailure::FromNetworkAuthFailure( | 158 LoginFailure::FromNetworkAuthFailure( |
160 GoogleServiceAuthError( | 159 GoogleServiceAuthError( |
161 GoogleServiceAuthError::HOSTED_NOT_ALLOWED))); | 160 GoogleServiceAuthError::HOSTED_NOT_ALLOWED))); |
162 TestingProfile profile; | 161 TestingProfile profile; |
163 | 162 |
164 EXPECT_CALL(*(resolver_.get()), Resolve()) | 163 EXPECT_CALL(*(resolver_.get()), Resolve()) |
165 .WillOnce(Invoke(OnlineAttemptTest::Quit)) | 164 .WillOnce(Invoke(OnlineAttemptTest::Quit)) |
166 .RetiresOnSaturation(); | 165 .RetiresOnSaturation(); |
167 | 166 |
168 // This is how we inject fake URLFetcher objects, with a factory. | 167 // This is how we inject fake URLFetcher objects, with a factory. |
169 MockURLFetcherFactory<HostedFetcher> factory; | 168 MockURLFetcherFactory<HostedFetcher> factory; |
170 | 169 |
171 TestAttemptState local_state(UserContext(), "", "", "", | 170 TestAttemptState local_state(UserContext(), "", "", "", |
172 User::USER_TYPE_REGULAR, true); | 171 User::USER_TYPE_REGULAR, true); |
173 attempt_.reset(new OnlineAttempt(&local_state, resolver_.get())); | 172 attempt_.reset(new OnlineAttempt(&local_state, resolver_.get())); |
174 attempt_->Initiate(&profile); | 173 attempt_->Initiate(&profile); |
175 BrowserThread::PostTask( | 174 BrowserThread::PostTask( |
176 BrowserThread::UI, FROM_HERE, | 175 BrowserThread::UI, FROM_HERE, |
177 base::Bind(&OnlineAttemptTest::RunThreadTest)); | 176 base::Bind(&OnlineAttemptTest::RunThreadTest)); |
178 | 177 |
179 MessageLoop::current()->Run(); | 178 base::MessageLoop::current()->Run(); |
180 | 179 |
181 EXPECT_EQ(error, local_state.online_outcome()); | 180 EXPECT_EQ(error, local_state.online_outcome()); |
182 EXPECT_EQ(LoginFailure::NETWORK_AUTH_FAILED, | 181 EXPECT_EQ(LoginFailure::NETWORK_AUTH_FAILED, |
183 local_state.online_outcome().reason()); | 182 local_state.online_outcome().reason()); |
184 } | 183 } |
185 | 184 |
186 TEST_F(OnlineAttemptTest, FullLogin) { | 185 TEST_F(OnlineAttemptTest, FullLogin) { |
187 TestingProfile profile; | 186 TestingProfile profile; |
188 | 187 |
189 EXPECT_CALL(*(resolver_.get()), Resolve()) | 188 EXPECT_CALL(*(resolver_.get()), Resolve()) |
190 .WillOnce(Invoke(OnlineAttemptTest::Quit)) | 189 .WillOnce(Invoke(OnlineAttemptTest::Quit)) |
191 .RetiresOnSaturation(); | 190 .RetiresOnSaturation(); |
192 | 191 |
193 // This is how we inject fake URLFetcher objects, with a factory. | 192 // This is how we inject fake URLFetcher objects, with a factory. |
194 MockURLFetcherFactory<SuccessFetcher> factory; | 193 MockURLFetcherFactory<SuccessFetcher> factory; |
195 | 194 |
196 TestAttemptState local_state(UserContext(), "", "", "", | 195 TestAttemptState local_state(UserContext(), "", "", "", |
197 User::USER_TYPE_REGULAR, true); | 196 User::USER_TYPE_REGULAR, true); |
198 attempt_.reset(new OnlineAttempt(&local_state, resolver_.get())); | 197 attempt_.reset(new OnlineAttempt(&local_state, resolver_.get())); |
199 attempt_->Initiate(&profile); | 198 attempt_->Initiate(&profile); |
200 BrowserThread::PostTask( | 199 BrowserThread::PostTask( |
201 BrowserThread::UI, FROM_HERE, | 200 BrowserThread::UI, FROM_HERE, |
202 base::Bind(&OnlineAttemptTest::RunThreadTest)); | 201 base::Bind(&OnlineAttemptTest::RunThreadTest)); |
203 | 202 |
204 MessageLoop::current()->Run(); | 203 base::MessageLoop::current()->Run(); |
205 | 204 |
206 EXPECT_EQ(LoginFailure::LoginFailureNone(), local_state.online_outcome()); | 205 EXPECT_EQ(LoginFailure::LoginFailureNone(), local_state.online_outcome()); |
207 } | 206 } |
208 | 207 |
209 TEST_F(OnlineAttemptTest, LoginNetFailure) { | 208 TEST_F(OnlineAttemptTest, LoginNetFailure) { |
210 RunFailureTest( | 209 RunFailureTest( |
211 GoogleServiceAuthError::FromConnectionError(net::ERR_CONNECTION_RESET)); | 210 GoogleServiceAuthError::FromConnectionError(net::ERR_CONNECTION_RESET)); |
212 } | 211 } |
213 | 212 |
214 TEST_F(OnlineAttemptTest, LoginDenied) { | 213 TEST_F(OnlineAttemptTest, LoginDenied) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 attempt_->weak_factory_.GetWeakPtr(), | 250 attempt_->weak_factory_.GetWeakPtr(), |
252 error)); | 251 error)); |
253 | 252 |
254 // Force UI thread to finish tasks so I can verify |state_|. | 253 // Force UI thread to finish tasks so I can verify |state_|. |
255 message_loop_.RunUntilIdle(); | 254 message_loop_.RunUntilIdle(); |
256 EXPECT_TRUE(GoogleServiceAuthError::AuthErrorNone() == | 255 EXPECT_TRUE(GoogleServiceAuthError::AuthErrorNone() == |
257 state_.online_outcome().error()); | 256 state_.online_outcome().error()); |
258 } | 257 } |
259 | 258 |
260 } // namespace chromeos | 259 } // namespace chromeos |
OLD | NEW |