Index: chrome/browser/ui/autofill/autofill_dialog_controller_browsertest.cc |
diff --git a/chrome/browser/ui/autofill/autofill_dialog_controller_browsertest.cc b/chrome/browser/ui/autofill/autofill_dialog_controller_browsertest.cc |
index 7a33c36099272768565acb38ed73d5ed97cdbb5c..32324ceb8452ce38ade1b0ca823d5850c9af0f7d 100644 |
--- a/chrome/browser/ui/autofill/autofill_dialog_controller_browsertest.cc |
+++ b/chrome/browser/ui/autofill/autofill_dialog_controller_browsertest.cc |
@@ -8,13 +8,16 @@ |
#include "base/message_loop.h" |
#include "base/strings/utf_string_conversions.h" |
#include "base/time.h" |
+#include "chrome/browser/autofill/personal_data_manager_factory.h" |
#include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h" |
#include "chrome/browser/ui/autofill/autofill_dialog_view.h" |
#include "chrome/browser/ui/autofill/data_model_wrapper.h" |
+#include "chrome/browser/ui/autofill/tab_autofill_manager_delegate.h" |
#include "chrome/browser/ui/autofill/testable_autofill_dialog_view.h" |
#include "chrome/browser/ui/browser.h" |
#include "chrome/browser/ui/tabs/tab_strip_model.h" |
#include "chrome/test/base/in_process_browser_test.h" |
+#include "chrome/test/base/ui_test_utils.h" |
#include "components/autofill/browser/autofill_common_test.h" |
#include "components/autofill/browser/autofill_metrics.h" |
#include "components/autofill/browser/test_personal_data_manager.h" |
@@ -23,8 +26,13 @@ |
#include "components/autofill/core/common/autofill_switches.h" |
#include "components/autofill/core/common/form_data.h" |
#include "components/autofill/core/common/form_field_data.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/web_contents.h" |
+#include "content/public/browser/web_contents_delegate.h" |
+#include "content/public/test/browser_test_utils.h" |
#include "content/public/test/test_utils.h" |
#include "testing/gtest/include/gtest/gtest.h" |
+#include "third_party/WebKit/public/web/WebInputEvent.h" |
namespace autofill { |
@@ -201,10 +209,84 @@ class AutofillDialogControllerTest : public InProcessBrowserTest { |
message_loop_runner_->Run(); |
} |
+ // Loads an HTML page in |GetActiveWebContents()| with markup as follows: |
+ // <form>|form_inner_html|</form>. After loading, emulates a click event on |
+ // the page as requestAutocomplete() must be in response to a user gesture. |
+ // 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.
|
+ AutofillDialogControllerImpl* SetUpHtmlAndInvoke( |
+ const std::string& form_inner_html) { |
+ content::WebContents* contents = GetActiveWebContents(); |
+ TabAutofillManagerDelegate* delegate = |
+ TabAutofillManagerDelegate::FromWebContents(contents); |
+ DCHECK(!delegate->dialog_controller()); |
+ |
+ ui_test_utils::NavigateToURL( |
+ browser(), GURL(std::string("data:text/html,") + |
+ "<!doctype html>" |
+ "<html>" |
+ "<body>" |
+ "<form>" + form_inner_html + "</form>" |
+ "<script>" |
+ "function send(msg) {" |
+ "domAutomationController.setAutomationId(0);" |
+ "domAutomationController.send(msg);" |
+ "}" |
+ "document.forms[0].onautocompleteerror = function(e) {" |
+ "send('error: ' + e.reason);" |
+ "};" |
+ "document.forms[0].onautocomplete = function() {" |
+ "send('success');" |
+ "};" |
+ "window.onclick = function() {" |
+ "document.forms[0].requestAutocomplete();" |
+ "send('clicked');" |
+ "};" |
+ "</script>" |
+ "</body>" |
+ "</html>")); |
+ content::WaitForLoadStop(contents); |
+ |
+ dom_message_queue_.reset(new content::DOMMessageQueue); |
+ |
+ // Triggers the onclick handler which invokes requestAutocomplete(). |
+ content::SimulateMouseClick(contents, 0, WebKit::WebMouseEvent::ButtonLeft); |
+ ExpectDomMessage("clicked"); |
+ |
+ AutofillDialogControllerImpl* controller = delegate->dialog_controller(); |
+ DCHECK(controller); |
+ return controller; |
+ } |
+ |
+ // Wait for a message from the DOM automation controller (from JS in the |
+ // page). Requires |SetUpHtmlAndInvoke()| be called first. |
+ void ExpectDomMessage(const std::string& expected) { |
+ std::string message; |
+ ASSERT_TRUE(dom_message_queue_->WaitForMessage(&message)); |
+ dom_message_queue_->ClearQueue(); |
+ EXPECT_EQ("\"" + expected + "\"", message); |
+ } |
+ |
+ void AddCreditcardToProfile(Profile* profile, const CreditCard& card) { |
+ PersonalDataManagerFactory::GetForProfile(profile)->AddCreditCard(card); |
+ WaitForWebDB(); |
+ } |
+ |
+ void AddAutofillProfileToProfile(Profile* profile, |
+ const AutofillProfile& autofill_profile) { |
+ PersonalDataManagerFactory::GetForProfile(profile)->AddProfile( |
+ autofill_profile); |
+ WaitForWebDB(); |
+ } |
+ |
+ void WaitForWebDB() { |
+ content::RunAllPendingInMessageLoop(content::BrowserThread::DB); |
+ } |
+ |
private: |
MockAutofillMetrics metric_logger_; |
TestAutofillDialogController* controller_; // Weak reference. |
scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
+ scoped_ptr<content::DOMMessageQueue> dom_message_queue_; |
DISALLOW_COPY_AND_ASSIGN(AutofillDialogControllerTest); |
}; |
@@ -568,6 +650,44 @@ IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest, LongNotifications) { |
EXPECT_EQ(no_notification_size.width(), |
controller()->view()->GetTestableView()->GetSize().width()); |
} |
+ |
+IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest, AutocompleteEvent) { |
+ AutofillDialogControllerImpl* controller = |
+ SetUpHtmlAndInvoke("<input autocomplete='cc-name'>"); |
+ |
+ AddCreditcardToProfile(controller->profile(), test::GetVerifiedCreditCard()); |
+ AddAutofillProfileToProfile(controller->profile(), |
+ test::GetVerifiedProfile()); |
+ |
+ controller->view()->GetTestableView()->SetCvc(ASCIIToUTF16("123")); |
+ controller->view()->GetTestableView()->SubmitForTesting(); |
+ ExpectDomMessage("success"); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest, |
+ AutocompleteErrorEventReasonInvalid) { |
+ AutofillDialogControllerImpl* controller = |
+ SetUpHtmlAndInvoke("<input autocomplete='cc-name' pattern='.*zebra.*'>"); |
+ |
+ const CreditCard& credit_card = test::GetVerifiedCreditCard(); |
+ ASSERT_TRUE( |
+ credit_card.GetRawInfo(CREDIT_CARD_NAME).find(ASCIIToUTF16("zebra")) == |
+ base::string16::npos); |
+ AddCreditcardToProfile(controller->profile(), credit_card); |
+ AddAutofillProfileToProfile(controller->profile(), |
+ test::GetVerifiedProfile()); |
+ |
+ controller->view()->GetTestableView()->SetCvc(ASCIIToUTF16("123")); |
+ controller->view()->GetTestableView()->SubmitForTesting(); |
+ ExpectDomMessage("error: invalid"); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest, |
+ AutocompleteErrorEventReasonCancel) { |
+ SetUpHtmlAndInvoke("<input autocomplete='cc-name'>")->view()-> |
+ GetTestableView()->CancelForTesting(); |
+ ExpectDomMessage("error: cancel"); |
+} |
#endif // defined(TOOLKIT_VIEWS) |
} // namespace autofill |