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 "components/password_manager/content/browser/content_credential_manager
_dispatcher.h" | 5 #include "components/password_manager/content/browser/content_credential_manager
_dispatcher.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "base/command_line.h" | 8 #include "base/command_line.h" |
8 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
9 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
11 #include "components/password_manager/content/browser/credential_manager_passwor
d_form_manager.h" | 12 #include "components/password_manager/content/browser/credential_manager_passwor
d_form_manager.h" |
12 #include "components/password_manager/content/common/credential_manager_messages
.h" | 13 #include "components/password_manager/content/common/credential_manager_messages
.h" |
13 #include "components/password_manager/content/common/credential_manager_types.h" | 14 #include "components/password_manager/content/common/credential_manager_types.h" |
14 #include "components/password_manager/core/browser/stub_password_manager_client.
h" | 15 #include "components/password_manager/core/browser/stub_password_manager_client.
h" |
15 #include "components/password_manager/core/browser/stub_password_manager_driver.
h" | 16 #include "components/password_manager/core/browser/stub_password_manager_driver.
h" |
16 #include "components/password_manager/core/browser/test_password_store.h" | 17 #include "components/password_manager/core/browser/test_password_store.h" |
17 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
18 #include "content/public/test/mock_render_process_host.h" | 19 #include "content/public/test/mock_render_process_host.h" |
19 #include "content/public/test/test_renderer_host.h" | 20 #include "content/public/test/test_renderer_host.h" |
20 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
22 | 23 |
23 using content::BrowserContext; | 24 using content::BrowserContext; |
24 using content::WebContents; | 25 using content::WebContents; |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
28 // Chosen by fair dice roll. Guaranteed to be random. | 29 // Chosen by fair dice roll. Guaranteed to be random. |
29 const int kRequestId = 4; | 30 const int kRequestId = 4; |
30 | 31 |
31 class TestPasswordManagerClient | 32 class TestPasswordManagerClient |
32 : public password_manager::StubPasswordManagerClient { | 33 : public password_manager::StubPasswordManagerClient { |
33 public: | 34 public: |
34 TestPasswordManagerClient(password_manager::PasswordStore* store) | 35 TestPasswordManagerClient(password_manager::PasswordStore* store) |
35 : did_prompt_user_to_save_(false), store_(store) {} | 36 : did_prompt_user_to_save_(false), |
| 37 did_prompt_user_to_choose_(false), |
| 38 store_(store) {} |
36 ~TestPasswordManagerClient() override {} | 39 ~TestPasswordManagerClient() override {} |
37 | 40 |
38 password_manager::PasswordStore* GetPasswordStore() override { | 41 password_manager::PasswordStore* GetPasswordStore() override { |
39 return store_; | 42 return store_; |
40 } | 43 } |
41 | 44 |
42 password_manager::PasswordManagerDriver* GetDriver() override { | 45 password_manager::PasswordManagerDriver* GetDriver() override { |
43 return &driver_; | 46 return &driver_; |
44 } | 47 } |
45 | 48 |
46 bool PromptUserToSavePassword( | 49 bool PromptUserToSavePassword( |
47 scoped_ptr<password_manager::PasswordFormManager> manager) override { | 50 scoped_ptr<password_manager::PasswordFormManager> manager) override { |
48 did_prompt_user_to_save_ = true; | 51 did_prompt_user_to_save_ = true; |
49 manager_.reset(manager.release()); | 52 manager_.reset(manager.release()); |
50 return true; | 53 return true; |
51 } | 54 } |
52 | 55 |
| 56 bool PromptUserToChooseCredentials( |
| 57 const std::vector<autofill::PasswordForm*>& forms, |
| 58 base::Callback<void(const password_manager::CredentialInfo&)> |
| 59 callback) override { |
| 60 EXPECT_FALSE(forms.empty()); |
| 61 did_prompt_user_to_choose_ = true; |
| 62 ScopedVector<autofill::PasswordForm> entries; |
| 63 entries.assign(forms.begin(), forms.end()); |
| 64 password_manager::CredentialInfo info(*entries[0]); |
| 65 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, |
| 66 info)); |
| 67 return true; |
| 68 } |
| 69 |
53 bool did_prompt_user_to_save() const { return did_prompt_user_to_save_; } | 70 bool did_prompt_user_to_save() const { return did_prompt_user_to_save_; } |
| 71 bool did_prompt_user_to_choose() const { return did_prompt_user_to_choose_; } |
54 | 72 |
55 password_manager::PasswordFormManager* pending_manager() const { | 73 password_manager::PasswordFormManager* pending_manager() const { |
56 return manager_.get(); | 74 return manager_.get(); |
57 } | 75 } |
58 | 76 |
59 private: | 77 private: |
60 bool did_prompt_user_to_save_; | 78 bool did_prompt_user_to_save_; |
| 79 bool did_prompt_user_to_choose_; |
61 password_manager::PasswordStore* store_; | 80 password_manager::PasswordStore* store_; |
62 password_manager::StubPasswordManagerDriver driver_; | 81 password_manager::StubPasswordManagerDriver driver_; |
63 scoped_ptr<password_manager::PasswordFormManager> manager_; | 82 scoped_ptr<password_manager::PasswordFormManager> manager_; |
64 }; | 83 }; |
65 | 84 |
66 void RunAllPendingTasks() { | 85 void RunAllPendingTasks() { |
67 base::RunLoop run_loop; | 86 base::RunLoop run_loop; |
68 base::MessageLoop::current()->PostTask( | 87 base::MessageLoop::current()->PostTask( |
69 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 88 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
70 run_loop.Run(); | 89 run_loop.Run(); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 RunAllPendingTasks(); | 192 RunAllPendingTasks(); |
174 | 193 |
175 const uint32 kMsgID = CredentialManagerMsg_SendCredential::ID; | 194 const uint32 kMsgID = CredentialManagerMsg_SendCredential::ID; |
176 const IPC::Message* message = | 195 const IPC::Message* message = |
177 process()->sink().GetFirstMessageMatching(kMsgID); | 196 process()->sink().GetFirstMessageMatching(kMsgID); |
178 EXPECT_TRUE(message); | 197 EXPECT_TRUE(message); |
179 CredentialManagerMsg_SendCredential::Param param; | 198 CredentialManagerMsg_SendCredential::Param param; |
180 CredentialManagerMsg_SendCredential::Read(message, ¶m); | 199 CredentialManagerMsg_SendCredential::Read(message, ¶m); |
181 EXPECT_EQ(CREDENTIAL_TYPE_EMPTY, param.b.type); | 200 EXPECT_EQ(CREDENTIAL_TYPE_EMPTY, param.b.type); |
182 process()->sink().ClearMessages(); | 201 process()->sink().ClearMessages(); |
| 202 EXPECT_FALSE(client_->did_prompt_user_to_choose()); |
183 } | 203 } |
184 | 204 |
185 TEST_F(ContentCredentialManagerDispatcherTest, | 205 TEST_F(ContentCredentialManagerDispatcherTest, |
186 CredentialManagerOnRequestCredentialWithFullPasswordStore) { | 206 CredentialManagerOnRequestCredentialWithFullPasswordStore) { |
187 store_->AddLogin(form_); | 207 store_->AddLogin(form_); |
188 | 208 |
189 std::vector<GURL> federations; | 209 std::vector<GURL> federations; |
190 dispatcher()->OnRequestCredential(kRequestId, false, federations); | 210 dispatcher()->OnRequestCredential(kRequestId, false, federations); |
191 | 211 |
192 RunAllPendingTasks(); | 212 RunAllPendingTasks(); |
193 | 213 |
194 const uint32 kMsgID = CredentialManagerMsg_SendCredential::ID; | 214 const uint32 kMsgID = CredentialManagerMsg_SendCredential::ID; |
195 const IPC::Message* message = | 215 const IPC::Message* message = |
196 process()->sink().GetFirstMessageMatching(kMsgID); | 216 process()->sink().GetFirstMessageMatching(kMsgID); |
197 EXPECT_TRUE(message); | 217 EXPECT_TRUE(message); |
198 process()->sink().ClearMessages(); | 218 EXPECT_TRUE(client_->did_prompt_user_to_choose()); |
199 } | 219 } |
200 | 220 |
201 TEST_F(ContentCredentialManagerDispatcherTest, | 221 TEST_F(ContentCredentialManagerDispatcherTest, |
202 CredentialManagerOnRequestCredentialWhileRequestPending) { | 222 CredentialManagerOnRequestCredentialWhileRequestPending) { |
203 store_->AddLogin(form_); | 223 store_->AddLogin(form_); |
204 | 224 |
205 std::vector<GURL> federations; | 225 std::vector<GURL> federations; |
206 dispatcher()->OnRequestCredential(kRequestId, false, federations); | 226 dispatcher()->OnRequestCredential(kRequestId, false, federations); |
207 dispatcher()->OnRequestCredential(kRequestId, false, federations); | 227 dispatcher()->OnRequestCredential(kRequestId, false, federations); |
208 | 228 |
209 // Check that the second request triggered a rejection. | 229 // Check that the second request triggered a rejection. |
210 uint32 kMsgID = CredentialManagerMsg_RejectCredentialRequest::ID; | 230 uint32 kMsgID = CredentialManagerMsg_RejectCredentialRequest::ID; |
211 const IPC::Message* message = | 231 const IPC::Message* message = |
212 process()->sink().GetFirstMessageMatching(kMsgID); | 232 process()->sink().GetFirstMessageMatching(kMsgID); |
213 EXPECT_TRUE(message); | 233 EXPECT_TRUE(message); |
214 CredentialManagerMsg_RejectCredentialRequest::Param reject_param; | 234 CredentialManagerMsg_RejectCredentialRequest::Param reject_param; |
215 CredentialManagerMsg_RejectCredentialRequest::Read(message, &reject_param); | 235 CredentialManagerMsg_RejectCredentialRequest::Read(message, &reject_param); |
216 EXPECT_EQ(blink::WebCredentialManagerError::ErrorTypePendingRequest, | 236 EXPECT_EQ(blink::WebCredentialManagerError::ErrorTypePendingRequest, |
217 reject_param.b); | 237 reject_param.b); |
| 238 EXPECT_FALSE(client_->did_prompt_user_to_choose()); |
218 | 239 |
219 process()->sink().ClearMessages(); | 240 process()->sink().ClearMessages(); |
220 | 241 |
221 // Execute the PasswordStore asynchronousness. | 242 // Execute the PasswordStore asynchronousness. |
222 RunAllPendingTasks(); | 243 RunAllPendingTasks(); |
223 | 244 |
224 // Check that the first request resolves. | 245 // Check that the first request resolves. |
225 kMsgID = CredentialManagerMsg_SendCredential::ID; | 246 kMsgID = CredentialManagerMsg_SendCredential::ID; |
226 message = process()->sink().GetFirstMessageMatching(kMsgID); | 247 message = process()->sink().GetFirstMessageMatching(kMsgID); |
227 EXPECT_TRUE(message); | 248 EXPECT_TRUE(message); |
228 CredentialManagerMsg_SendCredential::Param send_param; | 249 CredentialManagerMsg_SendCredential::Param send_param; |
229 CredentialManagerMsg_SendCredential::Read(message, &send_param); | 250 CredentialManagerMsg_SendCredential::Read(message, &send_param); |
230 CredentialManagerMsg_SendCredential::Read(message, &send_param); | 251 CredentialManagerMsg_SendCredential::Read(message, &send_param); |
231 EXPECT_NE(CREDENTIAL_TYPE_EMPTY, send_param.b.type); | 252 EXPECT_NE(CREDENTIAL_TYPE_EMPTY, send_param.b.type); |
232 process()->sink().ClearMessages(); | 253 process()->sink().ClearMessages(); |
| 254 EXPECT_TRUE(client_->did_prompt_user_to_choose()); |
233 } | 255 } |
234 | 256 |
235 } // namespace password_manager | 257 } // namespace password_manager |
OLD | NEW |