Index: chrome/browser/autofill/autofill_browsertest.cc |
diff --git a/chrome/browser/autofill/autofill_browsertest.cc b/chrome/browser/autofill/autofill_browsertest.cc |
index f0e8b9c32decc2347f18ae1fc429f906586a9ef0..70c8e9e3ab3037f542f0ab21578ae1a6a114fdcc 100644 |
--- a/chrome/browser/autofill/autofill_browsertest.cc |
+++ b/chrome/browser/autofill/autofill_browsertest.cc |
@@ -17,6 +17,8 @@ |
#include "chrome/browser/api/infobars/confirm_infobar_delegate.h" |
#include "chrome/browser/api/infobars/infobar_service.h" |
#include "chrome/browser/autofill/autofill_common_test.h" |
+#include "chrome/browser/autofill/autofill_external_delegate.h" |
+#include "chrome/browser/autofill/autofill_manager.h" |
#include "chrome/browser/autofill/autofill_profile.h" |
#include "chrome/browser/autofill/credit_card.h" |
#include "chrome/browser/autofill/personal_data_manager.h" |
@@ -151,6 +153,37 @@ class WindowedPersonalDataManagerObserver |
InfoBarService* infobar_service_; |
}; |
+class TestAutofillExternalDelegate : public AutofillExternalDelegate { |
+ public: |
+ TestAutofillExternalDelegate(content::WebContents* web_contents, |
+ AutofillManager* autofill_manager) |
+ : AutofillExternalDelegate(web_contents, autofill_manager), |
+ keyboard_listener_(NULL) { |
+ } |
+ virtual ~TestAutofillExternalDelegate() {} |
+ |
+ virtual void OnPopupShown(content::KeyboardListener* listener) OVERRIDE { |
+ AutofillExternalDelegate::OnPopupShown(listener); |
+ keyboard_listener_ = listener; |
+ } |
+ |
+ virtual void OnPopupHidden(content::KeyboardListener* listener) OVERRIDE { |
+ keyboard_listener_ = NULL; |
+ AutofillExternalDelegate::OnPopupHidden(listener); |
+ } |
+ |
+ content::KeyboardListener* keyboard_listener() { |
+ return keyboard_listener_; |
+ } |
+ |
+ private: |
+ // The popup that is currently registered as a keyboard listener, or NULL if |
+ // there is none. |
+ content::KeyboardListener* keyboard_listener_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TestAutofillExternalDelegate); |
+}; |
+ |
class AutofillTest : public InProcessBrowserTest { |
protected: |
AutofillTest() {} |
@@ -158,6 +191,27 @@ class AutofillTest : public InProcessBrowserTest { |
virtual void SetUpOnMainThread() OVERRIDE { |
// Don't want Keychain coming up on Mac. |
autofill_test::DisableSystemServices(browser()->profile()); |
+ |
+ // When testing the native UI, hook up a test external delegate, which |
+ // allows us to forward keyboard events to the popup directly. |
+ content::WebContents* web_contents = |
+ browser()->tab_strip_model()->GetActiveWebContents(); |
+ AutofillManager* autofill_manager = |
+ AutofillManager::FromWebContents(web_contents); |
+ if (autofill_manager->IsNativeUiEnabled()) { |
+ external_delegate_.reset( |
+ new TestAutofillExternalDelegate(web_contents, autofill_manager)); |
+ autofill_manager->SetExternalDelegate(external_delegate_.get()); |
+ } |
+ } |
+ |
+ virtual void CleanUpOnMainThread() OVERRIDE { |
+ // Make sure to close any showing popups prior to tearing down the UI. |
+ content::WebContents* web_contents = |
+ browser()->tab_strip_model()->GetActiveWebContents(); |
+ AutofillManager* autofill_manager = |
+ AutofillManager::FromWebContents(web_contents); |
+ autofill_manager->delegate()->HideAutofillPopup(); |
} |
PersonalDataManager* personal_data_manager() { |
@@ -241,12 +295,12 @@ class AutofillTest : public InProcessBrowserTest { |
std::string js("document.getElementById('" + field_id + "').focus();"); |
ASSERT_TRUE(content::ExecuteScript(render_view_host(), js)); |
- SendKeyAndWait(ui::VKEY_DOWN, |
- chrome::NOTIFICATION_AUTOFILL_DID_SHOW_SUGGESTIONS); |
- SendKeyAndWait(ui::VKEY_DOWN, |
- chrome::NOTIFICATION_AUTOFILL_DID_FILL_FORM_DATA); |
- SendKeyAndWait(ui::VKEY_RETURN, |
- chrome::NOTIFICATION_AUTOFILL_DID_FILL_FORM_DATA); |
+ SendKeyToPageAndWait(ui::VKEY_DOWN, |
+ chrome::NOTIFICATION_AUTOFILL_DID_SHOW_SUGGESTIONS); |
+ SendKeyToPopupAndWait(ui::VKEY_DOWN, |
+ chrome::NOTIFICATION_AUTOFILL_DID_FILL_FORM_DATA); |
+ SendKeyToPopupAndWait(ui::VKEY_RETURN, |
+ chrome::NOTIFICATION_AUTOFILL_DID_FILL_FORM_DATA); |
} |
// Aggregate profiles from forms into Autofill preferences. Returns the number |
@@ -376,7 +430,7 @@ class AutofillTest : public InProcessBrowserTest { |
ExpectFieldValue("phone", "5125551234"); |
} |
- void SendKeyAndWait(ui::KeyboardCode key, int notification_type) { |
+ void SendKeyToPageAndWait(ui::KeyboardCode key, int notification_type) { |
content::WindowedNotificationObserver observer( |
notification_type, content::Source<RenderViewHost>(render_view_host())); |
content::SimulateKeyPress( |
@@ -385,19 +439,38 @@ class AutofillTest : public InProcessBrowserTest { |
observer.Wait(); |
} |
+ void SendKeyToPopupAndWait(ui::KeyboardCode key, int notification_type) { |
+ // TODO(isherman): Remove this condition once the WebKit popup UI code is |
+ // removed. |
+ if (!external_delegate_) { |
+ // When testing the WebKit-based UI, route all keys to the page. |
+ SendKeyToPageAndWait(key, notification_type); |
+ return; |
+ } |
+ |
+ // When testing the native UI, route popup-targeted key presses via the |
+ // external delegate. |
+ content::WindowedNotificationObserver observer( |
+ notification_type, content::Source<RenderViewHost>(render_view_host())); |
+ content::NativeWebKeyboardEvent event; |
+ event.windowsKeyCode = key; |
+ external_delegate_->keyboard_listener()->HandleKeyPressEvent(event); |
+ observer.Wait(); |
+ } |
+ |
void TryBasicFormFill() { |
FocusFirstNameField(); |
// Start filling the first name field with "M" and wait for the popup to be |
// shown. |
LOG(WARNING) << "Typing 'M' to bring up the Autofill popup."; |
- SendKeyAndWait( |
+ SendKeyToPageAndWait( |
ui::VKEY_M, chrome::NOTIFICATION_AUTOFILL_DID_SHOW_SUGGESTIONS); |
// Press the down arrow to select the suggestion and preview the autofilled |
// form. |
LOG(WARNING) << "Simulating down arrow press to initiate Autofill preview."; |
- SendKeyAndWait( |
+ SendKeyToPopupAndWait( |
ui::VKEY_DOWN, chrome::NOTIFICATION_AUTOFILL_DID_FILL_FORM_DATA); |
// The previewed values should not be accessible to JavaScript. |
@@ -415,15 +488,20 @@ class AutofillTest : public InProcessBrowserTest { |
// Press Enter to accept the autofill suggestions. |
LOG(WARNING) << "Simulating Return press to fill the form."; |
- SendKeyAndWait( |
+ SendKeyToPopupAndWait( |
ui::VKEY_RETURN, chrome::NOTIFICATION_AUTOFILL_DID_FILL_FORM_DATA); |
// The form should be filled. |
ExpectFilledTestForm(); |
} |
+ TestAutofillExternalDelegate* external_delegate() { |
+ return external_delegate_.get(); |
+ } |
+ |
private: |
net::TestURLFetcherFactory url_fetcher_factory_; |
+ scoped_ptr<TestAutofillExternalDelegate> external_delegate_; |
}; |
// http://crbug.com/150084 |
@@ -463,16 +541,16 @@ IN_PROC_BROWSER_TEST_F(AutofillTest, MAYBE_AutofillViaDownArrow) { |
// Press the down arrow to initiate Autofill and wait for the popup to be |
// shown. |
- SendKeyAndWait( |
+ SendKeyToPageAndWait( |
ui::VKEY_DOWN, chrome::NOTIFICATION_AUTOFILL_DID_SHOW_SUGGESTIONS); |
// Press the down arrow to select the suggestion and preview the autofilled |
// form. |
- SendKeyAndWait( |
+ SendKeyToPopupAndWait( |
ui::VKEY_DOWN, chrome::NOTIFICATION_AUTOFILL_DID_FILL_FORM_DATA); |
// Press Enter to accept the autofill suggestions. |
- SendKeyAndWait( |
+ SendKeyToPopupAndWait( |
ui::VKEY_RETURN, chrome::NOTIFICATION_AUTOFILL_DID_FILL_FORM_DATA); |
// The form should be filled. |
@@ -519,16 +597,16 @@ IN_PROC_BROWSER_TEST_F(AutofillTest, MAYBE_OnChangeAfterAutofill) { |
// Start filling the first name field with "M" and wait for the popup to be |
// shown. |
- SendKeyAndWait( |
+ SendKeyToPageAndWait( |
ui::VKEY_M, chrome::NOTIFICATION_AUTOFILL_DID_SHOW_SUGGESTIONS); |
// Press the down arrow to select the suggestion and preview the autofilled |
// form. |
- SendKeyAndWait( |
+ SendKeyToPopupAndWait( |
ui::VKEY_DOWN, chrome::NOTIFICATION_AUTOFILL_DID_FILL_FORM_DATA); |
// Press Enter to accept the autofill suggestions. |
- SendKeyAndWait( |
+ SendKeyToPopupAndWait( |
ui::VKEY_RETURN, chrome::NOTIFICATION_AUTOFILL_DID_FILL_FORM_DATA); |
// The form should be filled. |
@@ -1547,7 +1625,7 @@ IN_PROC_BROWSER_TEST_F(AutofillTest, MAYBE_DisableAutocompleteWhileFilling) { |
// Invoke Autofill: Start filling the first name field with "M" and wait for |
// the popup to be shown. |
FocusFirstNameField(); |
- SendKeyAndWait( |
+ SendKeyToPageAndWait( |
ui::VKEY_M, chrome::NOTIFICATION_AUTOFILL_DID_SHOW_SUGGESTIONS); |
// Now that the popup with suggestions is showing, disable autocomplete for |
@@ -1558,9 +1636,15 @@ IN_PROC_BROWSER_TEST_F(AutofillTest, MAYBE_DisableAutocompleteWhileFilling) { |
// Press the down arrow to select the suggestion and attempt to preview the |
// autofilled form. |
- content::SimulateKeyPress( |
- browser()->tab_strip_model()->GetActiveWebContents(), |
- ui::VKEY_DOWN, false, false, false, false); |
+ if (!external_delegate()) { |
+ content::SimulateKeyPress( |
+ browser()->tab_strip_model()->GetActiveWebContents(), |
+ ui::VKEY_DOWN, false, false, false, false); |
+ } else { |
+ content::NativeWebKeyboardEvent event; |
+ event.windowsKeyCode = ui::VKEY_DOWN; |
+ external_delegate()->keyboard_listener()->HandleKeyPressEvent(event); |
+ } |
// Wait for any IPCs to complete by performing an action that generates an |
// IPC that's easy to wait for. Chrome shouldn't crash. |
@@ -1572,7 +1656,7 @@ IN_PROC_BROWSER_TEST_F(AutofillTest, MAYBE_DisableAutocompleteWhileFilling) { |
"city.focus()", |
&result)); |
ASSERT_TRUE(result); |
- SendKeyAndWait( |
+ SendKeyToPageAndWait( |
ui::VKEY_A, chrome::NOTIFICATION_AUTOFILL_DID_SHOW_SUGGESTIONS); |
} |