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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "chrome/browser/autofill/personal_data_manager_factory.h" | |
11 #include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h" | 12 #include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h" |
12 #include "chrome/browser/ui/autofill/autofill_dialog_view.h" | 13 #include "chrome/browser/ui/autofill/autofill_dialog_view.h" |
13 #include "chrome/browser/ui/autofill/data_model_wrapper.h" | 14 #include "chrome/browser/ui/autofill/data_model_wrapper.h" |
15 #include "chrome/browser/ui/autofill/tab_autofill_manager_delegate.h" | |
14 #include "chrome/browser/ui/autofill/testable_autofill_dialog_view.h" | 16 #include "chrome/browser/ui/autofill/testable_autofill_dialog_view.h" |
15 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
17 #include "chrome/test/base/in_process_browser_test.h" | 19 #include "chrome/test/base/in_process_browser_test.h" |
20 #include "chrome/test/base/ui_test_utils.h" | |
18 #include "components/autofill/browser/autofill_common_test.h" | 21 #include "components/autofill/browser/autofill_common_test.h" |
19 #include "components/autofill/browser/autofill_metrics.h" | 22 #include "components/autofill/browser/autofill_metrics.h" |
20 #include "components/autofill/browser/test_personal_data_manager.h" | 23 #include "components/autofill/browser/test_personal_data_manager.h" |
21 #include "components/autofill/browser/validation.h" | 24 #include "components/autofill/browser/validation.h" |
22 #include "components/autofill/content/browser/wallet/wallet_test_util.h" | 25 #include "components/autofill/content/browser/wallet/wallet_test_util.h" |
23 #include "components/autofill/core/common/autofill_switches.h" | 26 #include "components/autofill/core/common/autofill_switches.h" |
24 #include "components/autofill/core/common/form_data.h" | 27 #include "components/autofill/core/common/form_data.h" |
25 #include "components/autofill/core/common/form_field_data.h" | 28 #include "components/autofill/core/common/form_field_data.h" |
29 #include "content/public/browser/browser_thread.h" | |
30 #include "content/public/browser/web_contents.h" | |
31 #include "content/public/browser/web_contents_delegate.h" | |
32 #include "content/public/test/browser_test_utils.h" | |
26 #include "content/public/test/test_utils.h" | 33 #include "content/public/test/test_utils.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
35 #include "third_party/WebKit/public/web/WebInputEvent.h" | |
28 | 36 |
29 namespace autofill { | 37 namespace autofill { |
30 | 38 |
31 namespace { | 39 namespace { |
32 | 40 |
33 void MockCallback(const FormStructure*, const std::string&) {} | 41 void MockCallback(const FormStructure*, const std::string&) {} |
34 | 42 |
35 class MockAutofillMetrics : public AutofillMetrics { | 43 class MockAutofillMetrics : public AutofillMetrics { |
36 public: | 44 public: |
37 MockAutofillMetrics() | 45 MockAutofillMetrics() |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 return browser()->tab_strip_model()->GetActiveWebContents(); | 202 return browser()->tab_strip_model()->GetActiveWebContents(); |
195 } | 203 } |
196 | 204 |
197 const MockAutofillMetrics& metric_logger() { return metric_logger_; } | 205 const MockAutofillMetrics& metric_logger() { return metric_logger_; } |
198 TestAutofillDialogController* controller() { return controller_; } | 206 TestAutofillDialogController* controller() { return controller_; } |
199 | 207 |
200 void RunMessageLoop() { | 208 void RunMessageLoop() { |
201 message_loop_runner_->Run(); | 209 message_loop_runner_->Run(); |
202 } | 210 } |
203 | 211 |
212 // Loads an HTML page in |GetActiveWebContents()| with markup as follows: | |
213 // <form>|form_inner_html|</form>. After loading, emulates a click event on | |
214 // the page as requestAutocomplete() must be in response to a user gesture. | |
215 // Returns the |AutofillDialogControllerImpl| that created by this invocation. | |
Ilya Sherman
2013/06/20 01:11:27
nit: "that created" -> "created" or "that was crea
Dan Beam
2013/06/20 01:47:09
Done.
| |
216 AutofillDialogControllerImpl* SetUpHtmlAndInvoke( | |
217 const std::string& form_inner_html) { | |
218 content::WebContents* contents = GetActiveWebContents(); | |
219 TabAutofillManagerDelegate* delegate = | |
220 TabAutofillManagerDelegate::FromWebContents(contents); | |
221 DCHECK(!delegate->dialog_controller()); | |
222 | |
223 ui_test_utils::NavigateToURL( | |
224 browser(), GURL(std::string("data:text/html,") + | |
225 "<!doctype html>" | |
226 "<html>" | |
227 "<body>" | |
228 "<form>" + form_inner_html + "</form>" | |
229 "<script>" | |
230 "function send(msg) {" | |
231 "domAutomationController.setAutomationId(0);" | |
232 "domAutomationController.send(msg);" | |
233 "}" | |
234 "document.forms[0].onautocompleteerror = function(e) {" | |
235 "send('error: ' + e.reason);" | |
236 "};" | |
237 "document.forms[0].onautocomplete = function() {" | |
238 "send('success');" | |
239 "};" | |
240 "window.onclick = function() {" | |
241 "document.forms[0].requestAutocomplete();" | |
242 "send('clicked');" | |
243 "};" | |
244 "</script>" | |
245 "</body>" | |
246 "</html>")); | |
247 content::WaitForLoadStop(contents); | |
248 | |
249 dom_message_queue_.reset(new content::DOMMessageQueue); | |
250 | |
251 // Triggers the onclick handler which invokes requestAutocomplete(). | |
252 content::SimulateMouseClick(contents, 0, WebKit::WebMouseEvent::ButtonLeft); | |
253 ExpectDomMessage("clicked"); | |
254 | |
255 AutofillDialogControllerImpl* controller = delegate->dialog_controller(); | |
256 DCHECK(controller); | |
257 return controller; | |
258 } | |
259 | |
260 // Wait for a message from the DOM automation controller (from JS in the | |
261 // page). Requires |SetUpHtmlAndInvoke()| be called first. | |
262 void ExpectDomMessage(const std::string& expected) { | |
263 std::string message; | |
264 ASSERT_TRUE(dom_message_queue_->WaitForMessage(&message)); | |
265 dom_message_queue_->ClearQueue(); | |
266 EXPECT_EQ("\"" + expected + "\"", message); | |
267 } | |
268 | |
269 void AddCreditcardToProfile(Profile* profile, const CreditCard& card) { | |
270 PersonalDataManagerFactory::GetForProfile(profile)->AddCreditCard(card); | |
271 WaitForWebDB(); | |
272 } | |
273 | |
274 void AddAutofillProfileToProfile(Profile* profile, | |
275 const AutofillProfile& autofill_profile) { | |
276 PersonalDataManagerFactory::GetForProfile(profile)->AddProfile( | |
277 autofill_profile); | |
278 WaitForWebDB(); | |
279 } | |
280 | |
281 void WaitForWebDB() { | |
282 content::RunAllPendingInMessageLoop(content::BrowserThread::DB); | |
283 } | |
284 | |
204 private: | 285 private: |
205 MockAutofillMetrics metric_logger_; | 286 MockAutofillMetrics metric_logger_; |
206 TestAutofillDialogController* controller_; // Weak reference. | 287 TestAutofillDialogController* controller_; // Weak reference. |
207 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | 288 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
289 scoped_ptr<content::DOMMessageQueue> dom_message_queue_; | |
208 DISALLOW_COPY_AND_ASSIGN(AutofillDialogControllerTest); | 290 DISALLOW_COPY_AND_ASSIGN(AutofillDialogControllerTest); |
209 }; | 291 }; |
210 | 292 |
211 // TODO(isherman): Enable these tests on other platforms once the UI is | 293 // TODO(isherman): Enable these tests on other platforms once the UI is |
212 // implemented on those platforms. | 294 // implemented on those platforms. |
213 #if defined(TOOLKIT_VIEWS) | 295 #if defined(TOOLKIT_VIEWS) |
214 // Submit the form data. | 296 // Submit the form data. |
215 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest, Submit) { | 297 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest, Submit) { |
216 InitializeControllerOfType(DIALOG_TYPE_REQUEST_AUTOCOMPLETE); | 298 InitializeControllerOfType(DIALOG_TYPE_REQUEST_AUTOCOMPLETE); |
217 controller()->view()->GetTestableView()->SubmitForTesting(); | 299 controller()->view()->GetTestableView()->SubmitForTesting(); |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
561 "aliquip ex ea commodo consequat. Duis aute irure dolor in " | 643 "aliquip ex ea commodo consequat. Duis aute irure dolor in " |
562 "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla " | 644 "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla " |
563 "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in " | 645 "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in " |
564 "culpa qui officia deserunt mollit anim id est laborum."))); | 646 "culpa qui officia deserunt mollit anim id est laborum."))); |
565 controller()->set_notifications(notifications); | 647 controller()->set_notifications(notifications); |
566 controller()->view()->UpdateNotificationArea(); | 648 controller()->view()->UpdateNotificationArea(); |
567 | 649 |
568 EXPECT_EQ(no_notification_size.width(), | 650 EXPECT_EQ(no_notification_size.width(), |
569 controller()->view()->GetTestableView()->GetSize().width()); | 651 controller()->view()->GetTestableView()->GetSize().width()); |
570 } | 652 } |
653 | |
654 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest, AutocompleteEvent) { | |
655 AutofillDialogControllerImpl* controller = | |
656 SetUpHtmlAndInvoke("<input autocomplete='cc-name'>"); | |
657 | |
658 AddCreditcardToProfile(controller->profile(), test::GetVerifiedCreditCard()); | |
659 AddAutofillProfileToProfile(controller->profile(), | |
660 test::GetVerifiedProfile()); | |
661 | |
662 controller->view()->GetTestableView()->SetCvc(ASCIIToUTF16("123")); | |
663 controller->view()->GetTestableView()->SubmitForTesting(); | |
664 ExpectDomMessage("success"); | |
665 } | |
666 | |
667 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest, | |
668 AutocompleteErrorEventReasonInvalid) { | |
669 AutofillDialogControllerImpl* controller = | |
670 SetUpHtmlAndInvoke("<input autocomplete='cc-name' pattern='.*zebra.*'>"); | |
671 | |
672 const CreditCard& credit_card = test::GetVerifiedCreditCard(); | |
673 ASSERT_TRUE( | |
674 credit_card.GetRawInfo(CREDIT_CARD_NAME).find(ASCIIToUTF16("zebra")) == | |
675 base::string16::npos); | |
676 AddCreditcardToProfile(controller->profile(), credit_card); | |
677 AddAutofillProfileToProfile(controller->profile(), | |
678 test::GetVerifiedProfile()); | |
679 | |
680 controller->view()->GetTestableView()->SetCvc(ASCIIToUTF16("123")); | |
681 controller->view()->GetTestableView()->SubmitForTesting(); | |
682 ExpectDomMessage("error: invalid"); | |
683 } | |
684 | |
685 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest, | |
686 AutocompleteErrorEventReasonCancel) { | |
687 SetUpHtmlAndInvoke("<input autocomplete='cc-name'>")->view()-> | |
688 GetTestableView()->CancelForTesting(); | |
689 ExpectDomMessage("error: cancel"); | |
690 } | |
571 #endif // defined(TOOLKIT_VIEWS) | 691 #endif // defined(TOOLKIT_VIEWS) |
572 | 692 |
573 } // namespace autofill | 693 } // namespace autofill |
OLD | NEW |