html_dialog_view_browsertest.cc revision dc0f95d653279beabeb9817299e2902918ba123e
1// Copyright (c) 2011 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 "chrome/test/ui/ui_test.h"
6
7#include "base/file_path.h"
8#include "base/message_loop.h"
9#include "base/singleton.h"
10#include "chrome/browser/ui/views/html_dialog_view.h"
11#include "chrome/browser/ui/webui/html_dialog_ui.h"
12#include "chrome/common/url_constants.h"
13#include "chrome/test/in_process_browser_test.h"
14#include "chrome/test/ui_test_utils.h"
15#include "content/browser/renderer_host/render_widget_host_view.h"
16#include "content/browser/tab_contents/tab_contents.h"
17#include "testing/gmock/include/gmock/gmock.h"
18#include "testing/gtest/include/gtest/gtest.h"
19#include "views/widget/widget.h"
20#include "views/window/window.h"
21
22using testing::Eq;
23
24namespace {
25
26// Window non-client-area means that the minimum size for the window
27// won't be the actual minimum size - our layout and resizing code
28// makes sure the chrome is always visible.
29const int kMinimumWidthToTestFor = 20;
30const int kMinimumHeightToTestFor = 30;
31
32class TestHtmlDialogUIDelegate : public HtmlDialogUIDelegate {
33 public:
34  TestHtmlDialogUIDelegate() {}
35  virtual ~TestHtmlDialogUIDelegate() {}
36
37  // HTMLDialogUIDelegate implementation:
38  virtual bool IsDialogModal() const {
39    return true;
40  }
41  virtual std::wstring GetDialogTitle() const {
42    return std::wstring(L"Test");
43  }
44  virtual GURL GetDialogContentURL() const {
45    return GURL(chrome::kAboutBlankURL);
46  }
47  virtual void GetWebUIMessageHandlers(
48      std::vector<WebUIMessageHandler*>* handlers) const { }
49  virtual void GetDialogSize(gfx::Size* size) const {
50    size->set_width(40);
51    size->set_height(40);
52  }
53  virtual std::string GetDialogArgs() const {
54    return std::string();
55  }
56  virtual void OnDialogClosed(const std::string& json_retval) { }
57  virtual void OnCloseContents(TabContents* source, bool* out_close_dialog) {
58    if (out_close_dialog)
59      *out_close_dialog = true;
60  }
61  virtual bool ShouldShowDialogTitle() const { return true; }
62};
63
64}  // namespace
65
66class HtmlDialogBrowserTest : public InProcessBrowserTest {
67 public:
68  HtmlDialogBrowserTest() {}
69
70#if defined(OS_WIN)
71  class WindowChangedObserver : public base::MessagePumpForUI::Observer {
72   public:
73    WindowChangedObserver() {}
74
75    static WindowChangedObserver* GetInstance() {
76      return Singleton<WindowChangedObserver>::get();
77    }
78
79    // This method is called before processing a message.
80    virtual void WillProcessMessage(const MSG& msg) {}
81
82    // This method is called after processing a message.
83    virtual void DidProcessMessage(const MSG& msg) {
84      // Either WM_PAINT or WM_TIMER indicates the actual work of
85      // pushing through the window resizing messages is done since
86      // they are lower priority (we don't get to see the
87      // WM_WINDOWPOSCHANGED message here).
88      if (msg.message == WM_PAINT || msg.message == WM_TIMER)
89        MessageLoop::current()->Quit();
90    }
91  };
92#elif !defined(OS_MACOSX)
93  class WindowChangedObserver : public base::MessagePumpForUI::Observer {
94   public:
95    WindowChangedObserver() {}
96
97    static WindowChangedObserver* GetInstance() {
98      return Singleton<WindowChangedObserver>::get();
99    }
100
101    // This method is called before processing a message.
102    virtual void WillProcessEvent(GdkEvent* event) {}
103
104    // This method is called after processing a message.
105    virtual void DidProcessEvent(GdkEvent* event) {
106      // Quit once the GDK_CONFIGURE event has been processed - seeing
107      // this means the window sizing request that was made actually
108      // happened.
109      if (event->type == GDK_CONFIGURE)
110        MessageLoop::current()->Quit();
111    }
112  };
113#endif
114};
115
116#if defined(OS_LINUX)
117#define MAYBE_SizeWindow SizeWindow
118#else
119// http://code.google.com/p/chromium/issues/detail?id=52602
120// Windows has some issues resizing windows- an off by one problem,
121// and a minimum size that seems too big.  This file isn't included in
122// Mac builds yet.
123#define MAYBE_SizeWindow DISABLED_SizeWindow
124#endif
125
126IN_PROC_BROWSER_TEST_F(HtmlDialogBrowserTest, MAYBE_SizeWindow) {
127  HtmlDialogUIDelegate* delegate = new TestHtmlDialogUIDelegate();
128
129  HtmlDialogView* html_view =
130      new HtmlDialogView(browser()->profile(), delegate);
131  TabContents* tab_contents = browser()->GetSelectedTabContents();
132  ASSERT_TRUE(tab_contents != NULL);
133  views::Window::CreateChromeWindow(tab_contents->GetMessageBoxRootWindow(),
134                                    gfx::Rect(), html_view);
135  html_view->InitDialog();
136  html_view->window()->Show();
137
138  MessageLoopForUI::current()->AddObserver(
139      WindowChangedObserver::GetInstance());
140
141  gfx::Rect bounds = html_view->GetWidget()->GetClientAreaScreenBounds();
142
143  gfx::Rect set_bounds = bounds;
144  gfx::Rect actual_bounds, rwhv_bounds;
145
146  // Bigger than the default in both dimensions.
147  set_bounds.set_width(400);
148  set_bounds.set_height(300);
149
150  html_view->MoveContents(tab_contents, set_bounds);
151  ui_test_utils::RunMessageLoop();
152  actual_bounds = html_view->GetWidget()->GetClientAreaScreenBounds();
153  EXPECT_EQ(set_bounds, actual_bounds);
154
155  rwhv_bounds =
156      html_view->tab_contents()->GetRenderWidgetHostView()->GetViewBounds();
157  EXPECT_LT(0, rwhv_bounds.width());
158  EXPECT_LT(0, rwhv_bounds.height());
159  EXPECT_GE(set_bounds.width(), rwhv_bounds.width());
160  EXPECT_GE(set_bounds.height(), rwhv_bounds.height());
161
162  // Larger in one dimension and smaller in the other.
163  set_bounds.set_width(550);
164  set_bounds.set_height(250);
165
166  html_view->MoveContents(tab_contents, set_bounds);
167  ui_test_utils::RunMessageLoop();
168  actual_bounds = html_view->GetWidget()->GetClientAreaScreenBounds();
169  EXPECT_EQ(set_bounds, actual_bounds);
170
171  rwhv_bounds =
172      html_view->tab_contents()->GetRenderWidgetHostView()->GetViewBounds();
173  EXPECT_LT(0, rwhv_bounds.width());
174  EXPECT_LT(0, rwhv_bounds.height());
175  EXPECT_GE(set_bounds.width(), rwhv_bounds.width());
176  EXPECT_GE(set_bounds.height(), rwhv_bounds.height());
177
178  // Get very small.
179  set_bounds.set_width(kMinimumWidthToTestFor);
180  set_bounds.set_height(kMinimumHeightToTestFor);
181
182  html_view->MoveContents(tab_contents, set_bounds);
183  ui_test_utils::RunMessageLoop();
184  actual_bounds = html_view->GetWidget()->GetClientAreaScreenBounds();
185  EXPECT_EQ(set_bounds, actual_bounds);
186
187  rwhv_bounds =
188      html_view->tab_contents()->GetRenderWidgetHostView()->GetViewBounds();
189  EXPECT_LT(0, rwhv_bounds.width());
190  EXPECT_LT(0, rwhv_bounds.height());
191  EXPECT_GE(set_bounds.width(), rwhv_bounds.width());
192  EXPECT_GE(set_bounds.height(), rwhv_bounds.height());
193
194  // Check to make sure we can't get to 0x0
195  set_bounds.set_width(0);
196  set_bounds.set_height(0);
197
198  html_view->MoveContents(tab_contents, set_bounds);
199  ui_test_utils::RunMessageLoop();
200  actual_bounds = html_view->GetWidget()->GetClientAreaScreenBounds();
201  EXPECT_LT(0, actual_bounds.width());
202  EXPECT_LT(0, actual_bounds.height());
203
204  MessageLoopForUI::current()->RemoveObserver(
205      WindowChangedObserver::GetInstance());
206}
207