autofill_popup_controller_interactive_uitest.cc revision f2477e01787aa58f445919b809d89e252beef54f
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
21namespace autofill {
22namespace {
23
24class 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  virtual ~TestAutofillExternalDelegate() {}
32
33  virtual void OnPopupShown() OVERRIDE {
34    popup_hidden_ = false;
35
36    AutofillExternalDelegate::OnPopupShown();
37  }
38
39  virtual 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
67class AutofillPopupControllerBrowserTest
68    : public InProcessBrowserTest,
69      public content::WebContentsObserver {
70 public:
71  AutofillPopupControllerBrowserTest() {}
72  virtual ~AutofillPopupControllerBrowserTest() {}
73
74  virtual void SetUpOnMainThread() OVERRIDE {
75    web_contents_ = browser()->tab_strip_model()->GetActiveWebContents();
76    ASSERT_TRUE(web_contents_ != NULL);
77    Observe(web_contents_);
78
79    AutofillDriverImpl* driver =
80        AutofillDriverImpl::FromWebContents(web_contents_);
81    autofill_external_delegate_.reset(
82       new TestAutofillExternalDelegate(
83           web_contents_,
84           driver->autofill_manager(),
85           driver));
86  }
87
88  // Normally the WebContents will automatically delete the delegate, but here
89  // the delegate is owned by this test, so we have to manually destroy.
90  virtual void WebContentsDestroyed(content::WebContents* web_contents)
91      OVERRIDE {
92    DCHECK_EQ(web_contents_, web_contents);
93
94    autofill_external_delegate_.reset();
95  }
96
97 protected:
98  content::WebContents* web_contents_;
99
100  scoped_ptr<TestAutofillExternalDelegate> autofill_external_delegate_;
101};
102
103// Autofill UI isn't currently hidden on window move on Mac.
104// http://crbug.com/180566
105#if !defined(OS_MACOSX)
106IN_PROC_BROWSER_TEST_F(AutofillPopupControllerBrowserTest,
107                       HidePopupOnWindowConfiguration) {
108  GenerateTestAutofillPopup(autofill_external_delegate_.get());
109
110  EXPECT_FALSE(autofill_external_delegate_->popup_hidden());
111
112  // Resize the window, which should cause the popup to hide.
113  gfx::Rect new_bounds = browser()->window()->GetBounds() - gfx::Vector2d(1, 1);
114  browser()->window()->SetBounds(new_bounds);
115
116  autofill_external_delegate_->WaitForPopupHidden();
117  EXPECT_TRUE(autofill_external_delegate_->popup_hidden());
118}
119#endif // !defined(OS_MACOSX)
120
121#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
122// TODO(erg): linux_aura bringup: http://crbug.com/163931
123#define MAYBE_DeleteDelegateBeforePopupHidden \
124  DISABLED_DeleteDelegateBeforePopupHidden
125#else
126#define MAYBE_DeleteDelegateBeforePopupHidden DeleteDelegateBeforePopupHidden
127#endif
128
129// This test checks that the browser doesn't crash if the delegate is deleted
130// before the popup is hidden.
131IN_PROC_BROWSER_TEST_F(AutofillPopupControllerBrowserTest,
132                       MAYBE_DeleteDelegateBeforePopupHidden){
133  GenerateTestAutofillPopup(autofill_external_delegate_.get());
134
135  // Delete the external delegate here so that is gets deleted before popup is
136  // hidden. This can happen if the web_contents are destroyed before the popup
137  // is hidden. See http://crbug.com/232475
138  autofill_external_delegate_.reset();
139}
140
141}  // namespace autofill
142