1 // Copyright 2018 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_AUTOFILL_AUTOFILL_UITEST_H_ 6 #define CHROME_BROWSER_AUTOFILL_AUTOFILL_UITEST_H_ 7 8 #include <string> 9 10 #include "chrome/test/base/in_process_browser_test.h" 11 #include "chrome/test/base/interactive_test_utils.h" 12 #include "components/autofill/core/browser/autofill_manager.h" 13 #include "components/autofill/core/browser/autofill_manager_test_delegate.h" 14 #include "components/autofill/core/browser/test_event_waiter.h" 15 #include "content/public/browser/render_widget_host.h" 16 #include "content/public/browser/web_contents_observer.h" 17 #include "content/public/test/test_utils.h" 18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "ui/compositor/scoped_animation_duration_scale_mode.h" 20 #include "ui/events/keycodes/dom/dom_key.h" 21 #include "ui/events/keycodes/keyboard_code_conversion.h" 22 #include "ui/events/keycodes/keyboard_codes.h" 23 24 namespace autofill { 25 26 enum class ObservedUiEvents { 27 kPreviewFormData, 28 kFormDataFilled, 29 kSuggestionShown, 30 kNoEvent, 31 }; 32 33 class AutofillManagerTestDelegateImpl 34 : public autofill::AutofillManagerTestDelegate { 35 public: 36 AutofillManagerTestDelegateImpl(); 37 ~AutofillManagerTestDelegateImpl() override; 38 39 // autofill::AutofillManagerTestDelegate: 40 void DidPreviewFormData() override; 41 void DidFillFormData() override; 42 void DidShowSuggestions() override; 43 void OnTextFieldChanged() override; 44 45 void SetExpectations( 46 std::list<ObservedUiEvents> expected_events, 47 base::TimeDelta timeout = base::TimeDelta::FromSeconds(0)); 48 bool Wait(); 49 SetIsExpectingDynamicRefill(bool expect_refill)50 void SetIsExpectingDynamicRefill(bool expect_refill) { 51 is_expecting_dynamic_refill_ = expect_refill; 52 } 53 54 private: 55 bool is_expecting_dynamic_refill_; 56 std::unique_ptr<EventWaiter<ObservedUiEvents>> event_waiter_; 57 58 DISALLOW_COPY_AND_ASSIGN(AutofillManagerTestDelegateImpl); 59 }; 60 61 class AutofillUiTest : public InProcessBrowserTest, 62 public content::WebContentsObserver { 63 protected: 64 AutofillUiTest(); 65 ~AutofillUiTest() override; 66 67 // InProcessBrowserTest: 68 void SetUpOnMainThread() override; 69 void TearDownOnMainThread() override; 70 71 void SendKeyToPage(content::WebContents* web_contents, const ui::DomKey key); 72 void SendKeyToPageAndWait(ui::DomKey key, 73 std::list<ObservedUiEvents> expected_events); 74 void SendKeyToPageAndWait(ui::DomKey key, 75 ui::DomCode code, 76 ui::KeyboardCode key_code, 77 std::list<ObservedUiEvents> expected_events); 78 void DoNothingAndWait(unsigned seconds); 79 void SendKeyToPopup(content::RenderFrameHost* render_frame_host, 80 const ui::DomKey key); 81 // Send key to the render host view's widget if |widget| is null. 82 void SendKeyToPopupAndWait(ui::DomKey key, 83 std::list<ObservedUiEvents> expected_events, 84 content::RenderWidgetHost* widget = nullptr); 85 void SendKeyToPopupAndWait(ui::DomKey key, 86 ui::DomCode code, 87 ui::KeyboardCode key_code, 88 std::list<ObservedUiEvents> expected_events, 89 content::RenderWidgetHost* widget); 90 void SendKeyToDataListPopup(ui::DomKey key); 91 void SendKeyToDataListPopup(ui::DomKey key, 92 ui::DomCode code, 93 ui::KeyboardCode key_code); 94 bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent& event); 95 96 content::WebContents* GetWebContents(); 97 content::RenderViewHost* GetRenderViewHost(); 98 AutofillManager* GetAutofillManager(); 99 test_delegate()100 AutofillManagerTestDelegateImpl* test_delegate() { return &test_delegate_; } 101 content::RenderWidgetHost::KeyPressEventCallback key_press_event_sink(); 102 103 private: 104 // WebContentsObserver override: 105 void RenderFrameHostChanged(content::RenderFrameHost* old_host, 106 content::RenderFrameHost* new_host) override; 107 content::RenderFrameHost* current_main_rfh_ = nullptr; 108 AutofillManagerTestDelegateImpl test_delegate_; 109 110 // KeyPressEventCallback that serves as a sink to ensure that every key press 111 // event the tests create and have the WebContents forward is handled by some 112 // key press event callback. It is necessary to have this sink because if no 113 // key press event callback handles the event (at least on Mac), a DCHECK 114 // ends up going off that the |event| doesn't have an |os_event| associated 115 // with it. 116 content::RenderWidgetHost::KeyPressEventCallback key_press_event_sink_; 117 118 std::unique_ptr<ui::ScopedAnimationDurationScaleMode> disable_animation_; 119 120 DISALLOW_COPY_AND_ASSIGN(AutofillUiTest); 121 }; 122 123 } // namespace autofill 124 125 #endif // CHROME_BROWSER_AUTOFILL_AUTOFILL_UITEST_H_ 126