1 // Copyright 2013 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.
5 #include "base/basictypes.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "chrome/browser/ui/autofill/autofill_popup_view.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/browser_window.h"
10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11 #include "chrome/test/base/in_process_browser_test.h"
12 #include "components/autofill/content/browser/content_autofill_driver.h"
13 #include "components/autofill/content/browser/content_autofill_driver_factory.h"
14 #include "components/autofill/core/browser/autofill_manager.h"
15 #include "components/autofill/core/browser/test_autofill_external_delegate.h"
16 #include "content/public/browser/render_frame_host.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/browser/web_contents_observer.h"
19 #include "content/public/test/test_utils.h"
20 #include "ui/gfx/geometry/rect.h"
21 #include "ui/gfx/geometry/vector2d.h"
26 class TestAutofillExternalDelegate
: public AutofillExternalDelegate
{
28 TestAutofillExternalDelegate(content::WebContents
* web_contents
,
29 AutofillManager
* autofill_manager
,
30 AutofillDriver
* autofill_driver
)
31 : AutofillExternalDelegate(autofill_manager
, autofill_driver
),
32 popup_hidden_(true) {}
33 ~TestAutofillExternalDelegate() override
{}
35 void OnPopupShown() override
{
36 popup_hidden_
= false;
38 AutofillExternalDelegate::OnPopupShown();
41 void OnPopupHidden() override
{
44 if (message_loop_runner_
.get())
45 message_loop_runner_
->Quit();
48 void WaitForPopupHidden() {
52 message_loop_runner_
= new content::MessageLoopRunner
;
53 message_loop_runner_
->Run();
56 bool popup_hidden() const { return popup_hidden_
; }
60 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
62 DISALLOW_COPY_AND_ASSIGN(TestAutofillExternalDelegate
);
67 class AutofillPopupControllerBrowserTest
68 : public InProcessBrowserTest
,
69 public content::WebContentsObserver
{
71 AutofillPopupControllerBrowserTest() {}
72 ~AutofillPopupControllerBrowserTest() override
{}
74 void SetUpOnMainThread() override
{
75 content::WebContents
* web_contents
=
76 browser()->tab_strip_model()->GetActiveWebContents();
77 ASSERT_TRUE(web_contents
!= NULL
);
78 Observe(web_contents
);
80 ContentAutofillDriver
* driver
=
81 ContentAutofillDriverFactory::FromWebContents(web_contents
)
82 ->DriverForFrame(web_contents
->GetMainFrame());
83 autofill_external_delegate_
.reset(
84 new TestAutofillExternalDelegate(
86 driver
->autofill_manager(),
90 // Normally the WebContents will automatically delete the delegate, but here
91 // the delegate is owned by this test, so we have to manually destroy.
92 void RenderFrameDeleted(content::RenderFrameHost
* rfh
) override
{
93 if (!rfh
->GetParent())
94 autofill_external_delegate_
.reset();
98 scoped_ptr
<TestAutofillExternalDelegate
> autofill_external_delegate_
;
101 #if defined(OS_MACOSX)
102 // Fails on Mac OS. http://crbug.com/453256
103 #define MAYBE_PopupHidingOnWindowMove DISABLED_PopupHidingOnWindowMove
105 #define MAYBE_PopupHidingOnWindowMove PopupHidingOnWindowMove
107 IN_PROC_BROWSER_TEST_F(AutofillPopupControllerBrowserTest
,
108 MAYBE_PopupHidingOnWindowMove
) {
109 GenerateTestAutofillPopup(autofill_external_delegate_
.get());
111 EXPECT_FALSE(autofill_external_delegate_
->popup_hidden());
113 // Move the window, which should not cause the popup to hide.
114 gfx::Rect new_bounds
= browser()->window()->GetBounds() - gfx::Vector2d(1, 1);
115 browser()->window()->SetBounds(new_bounds
);
118 // Windows draws autofill popup into a separate aura window than the main
119 // browser window. Thus, the autofill popup should hide on window move.
120 // http://crbug.com/512802
121 autofill_external_delegate_
->WaitForPopupHidden();
122 EXPECT_TRUE(autofill_external_delegate_
->popup_hidden());
124 EXPECT_FALSE(autofill_external_delegate_
->popup_hidden());
128 IN_PROC_BROWSER_TEST_F(AutofillPopupControllerBrowserTest
,
129 HidePopupOnWindowResize
) {
130 GenerateTestAutofillPopup(autofill_external_delegate_
.get());
132 EXPECT_FALSE(autofill_external_delegate_
->popup_hidden());
134 // Resize the window, which should cause the popup to hide.
135 gfx::Rect new_bounds
= browser()->window()->GetBounds();
136 new_bounds
.Inset(1, 1);
137 browser()->window()->SetBounds(new_bounds
);
139 autofill_external_delegate_
->WaitForPopupHidden();
140 EXPECT_TRUE(autofill_external_delegate_
->popup_hidden());
143 // This test checks that the browser doesn't crash if the delegate is deleted
144 // before the popup is hidden.
145 IN_PROC_BROWSER_TEST_F(AutofillPopupControllerBrowserTest
,
146 DeleteDelegateBeforePopupHidden
){
147 GenerateTestAutofillPopup(autofill_external_delegate_
.get());
149 // Delete the external delegate here so that is gets deleted before popup is
150 // hidden. This can happen if the web_contents are destroyed before the popup
151 // is hidden. See http://crbug.com/232475
152 autofill_external_delegate_
.reset();
155 } // namespace autofill