Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / autofill / autofill_popup_controller_interactive_uitest.cc
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.
4
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/core/browser/autofill_manager.h"
14 #include "components/autofill/core/browser/test_autofill_external_delegate.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_observer.h"
17 #include "content/public/test/test_utils.h"
18 #include "ui/gfx/rect.h"
19 #include "ui/gfx/vector2d.h"
20
21 namespace autofill {
22 namespace {
23
24 class TestAutofillExternalDelegate : public AutofillExternalDelegate {
25  public:
26   TestAutofillExternalDelegate(content::WebContents* web_contents,
27                                AutofillManager* autofill_manager,
28                                AutofillDriver* autofill_driver)
29       : AutofillExternalDelegate(autofill_manager, autofill_driver),
30         popup_hidden_(true) {}
31   ~TestAutofillExternalDelegate() override {}
32
33   void OnPopupShown() override {
34     popup_hidden_ = false;
35
36     AutofillExternalDelegate::OnPopupShown();
37   }
38
39   void OnPopupHidden() override {
40     popup_hidden_ = true;
41
42     if (message_loop_runner_.get())
43       message_loop_runner_->Quit();
44
45     AutofillExternalDelegate::OnPopupHidden();
46   }
47
48   void WaitForPopupHidden() {
49     if (popup_hidden_)
50       return;
51
52     message_loop_runner_ = new content::MessageLoopRunner;
53     message_loop_runner_->Run();
54   }
55
56   bool popup_hidden() const { return popup_hidden_; }
57
58  private:
59   bool popup_hidden_;
60   scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
61
62   DISALLOW_COPY_AND_ASSIGN(TestAutofillExternalDelegate);
63 };
64
65 }  // namespace
66
67 class AutofillPopupControllerBrowserTest
68     : public InProcessBrowserTest,
69       public content::WebContentsObserver {
70  public:
71   AutofillPopupControllerBrowserTest() {}
72   ~AutofillPopupControllerBrowserTest() override {}
73
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);
79
80     ContentAutofillDriver* driver =
81         ContentAutofillDriver::FromWebContents(web_contents);
82     autofill_external_delegate_.reset(
83        new TestAutofillExternalDelegate(
84            web_contents,
85            driver->autofill_manager(),
86            driver));
87   }
88
89   // Normally the WebContents will automatically delete the delegate, but here
90   // the delegate is owned by this test, so we have to manually destroy.
91   void WebContentsDestroyed() override { autofill_external_delegate_.reset(); }
92
93  protected:
94   scoped_ptr<TestAutofillExternalDelegate> autofill_external_delegate_;
95 };
96
97 // Autofill UI isn't currently hidden on window move on Mac.
98 // http://crbug.com/180566
99 #if !defined(OS_MACOSX)
100 IN_PROC_BROWSER_TEST_F(AutofillPopupControllerBrowserTest,
101                        HidePopupOnWindowConfiguration) {
102   GenerateTestAutofillPopup(autofill_external_delegate_.get());
103
104   EXPECT_FALSE(autofill_external_delegate_->popup_hidden());
105
106   // Resize the window, which should cause the popup to hide.
107   gfx::Rect new_bounds = browser()->window()->GetBounds() - gfx::Vector2d(1, 1);
108   browser()->window()->SetBounds(new_bounds);
109
110   autofill_external_delegate_->WaitForPopupHidden();
111   EXPECT_TRUE(autofill_external_delegate_->popup_hidden());
112 }
113 #endif // !defined(OS_MACOSX)
114
115 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
116 // TODO(erg): linux_aura bringup: http://crbug.com/163931
117 #define MAYBE_DeleteDelegateBeforePopupHidden \
118   DISABLED_DeleteDelegateBeforePopupHidden
119 #else
120 #define MAYBE_DeleteDelegateBeforePopupHidden DeleteDelegateBeforePopupHidden
121 #endif
122
123 // This test checks that the browser doesn't crash if the delegate is deleted
124 // before the popup is hidden.
125 IN_PROC_BROWSER_TEST_F(AutofillPopupControllerBrowserTest,
126                        MAYBE_DeleteDelegateBeforePopupHidden){
127   GenerateTestAutofillPopup(autofill_external_delegate_.get());
128
129   // Delete the external delegate here so that is gets deleted before popup is
130   // hidden. This can happen if the web_contents are destroyed before the popup
131   // is hidden. See http://crbug.com/232475
132   autofill_external_delegate_.reset();
133 }
134
135 }  // namespace autofill