- add sources.
[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/autofill_driver_impl.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(web_contents, autofill_manager,
30                                  autofill_driver),
31         popup_hidden_(true) {}
32   virtual ~TestAutofillExternalDelegate() {}
33
34   virtual void OnPopupShown() OVERRIDE {
35     popup_hidden_ = false;
36
37     AutofillExternalDelegate::OnPopupShown();
38   }
39
40   virtual void OnPopupHidden() OVERRIDE {
41     popup_hidden_ = true;
42
43     if (message_loop_runner_.get())
44       message_loop_runner_->Quit();
45
46     AutofillExternalDelegate::OnPopupHidden();
47   }
48
49   void WaitForPopupHidden() {
50     if (popup_hidden_)
51       return;
52
53     message_loop_runner_ = new content::MessageLoopRunner;
54     message_loop_runner_->Run();
55   }
56
57   bool popup_hidden() const { return popup_hidden_; }
58
59  private:
60   bool popup_hidden_;
61   scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
62
63   DISALLOW_COPY_AND_ASSIGN(TestAutofillExternalDelegate);
64 };
65
66 }  // namespace
67
68 class AutofillPopupControllerBrowserTest
69     : public InProcessBrowserTest,
70       public content::WebContentsObserver {
71  public:
72   AutofillPopupControllerBrowserTest() {}
73   virtual ~AutofillPopupControllerBrowserTest() {}
74
75   virtual void SetUpOnMainThread() OVERRIDE {
76     web_contents_ = browser()->tab_strip_model()->GetActiveWebContents();
77     ASSERT_TRUE(web_contents_ != NULL);
78     Observe(web_contents_);
79
80     AutofillDriverImpl* driver =
81         AutofillDriverImpl::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   virtual void WebContentsDestroyed(content::WebContents* web_contents)
92       OVERRIDE {
93     DCHECK_EQ(web_contents_, web_contents);
94
95     autofill_external_delegate_.reset();
96   }
97
98  protected:
99   content::WebContents* web_contents_;
100
101   scoped_ptr<TestAutofillExternalDelegate> autofill_external_delegate_;
102 };
103
104 // Autofill UI isn't currently hidden on window move on Mac.
105 // http://crbug.com/180566
106 #if !defined(OS_MACOSX)
107 IN_PROC_BROWSER_TEST_F(AutofillPopupControllerBrowserTest,
108                        HidePopupOnWindowConfiguration) {
109   GenerateTestAutofillPopup(autofill_external_delegate_.get());
110
111   EXPECT_FALSE(autofill_external_delegate_->popup_hidden());
112
113   // Resize the window, which should cause the popup to hide.
114   gfx::Rect new_bounds = browser()->window()->GetBounds() - gfx::Vector2d(1, 1);
115   browser()->window()->SetBounds(new_bounds);
116
117   autofill_external_delegate_->WaitForPopupHidden();
118   EXPECT_TRUE(autofill_external_delegate_->popup_hidden());
119 }
120 #endif // !defined(OS_MACOSX)
121
122 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
123 // TODO(erg): linux_aura bringup: http://crbug.com/163931
124 #define MAYBE_DeleteDelegateBeforePopupHidden \
125   DISABLED_DeleteDelegateBeforePopupHidden
126 #else
127 #define MAYBE_DeleteDelegateBeforePopupHidden DeleteDelegateBeforePopupHidden
128 #endif
129
130 // This test checks that the browser doesn't crash if the delegate is deleted
131 // before the popup is hidden.
132 IN_PROC_BROWSER_TEST_F(AutofillPopupControllerBrowserTest,
133                        MAYBE_DeleteDelegateBeforePopupHidden){
134   GenerateTestAutofillPopup(autofill_external_delegate_.get());
135
136   // Delete the external delegate here so that is gets deleted before popup is
137   // hidden. This can happen if the web_contents are destroyed before the popup
138   // is hidden. See http://crbug.com/232475
139   autofill_external_delegate_.reset();
140 }
141
142 }  // namespace autofill