1 // Copyright (c) 2012 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.
6 #include "base/bind_helpers.h"
7 #include "base/files/file_path.h"
8 #include "base/memory/singleton.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/browser/ui/webui/chrome_web_contents_handler.h"
15 #include "chrome/common/url_constants.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "content/public/browser/browser_context.h"
18 #include "content/public/browser/render_widget_host_view.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/test/test_utils.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "ui/views/controls/webview/web_dialog_view.h"
24 #include "ui/views/widget/widget.h"
25 #include "ui/web_dialogs/test/test_web_dialog_delegate.h"
27 using content::BrowserContext
;
28 using content::WebContents
;
30 using ui::WebDialogDelegate
;
34 // Initial size of WebDialog for SizeWindow test case.
35 const int kInitialWidth
= 40;
36 const int kInitialHeight
= 40;
38 class TestWebDialogView
: public views::WebDialogView
{
40 TestWebDialogView(content::BrowserContext
* context
,
41 WebDialogDelegate
* delegate
)
42 : views::WebDialogView(context
, delegate
, new ChromeWebContentsHandler
),
43 should_quit_on_size_change_(false) {
44 delegate
->GetDialogSize(&last_size_
);
47 void set_should_quit_on_size_change(bool should_quit
) {
48 should_quit_on_size_change_
= should_quit
;
52 // TODO(xiyuan): Update this when WidgetDelegate has bounds change hook.
53 void SaveWindowPlacement(const gfx::Rect
& bounds
,
54 ui::WindowShowState show_state
) override
{
55 if (should_quit_on_size_change_
&& last_size_
!= bounds
.size()) {
56 // Schedule message loop quit because we could be called while
57 // the bounds change call is on the stack and not in the nested message
59 base::MessageLoop::current()->PostTask(
61 base::Bind(&base::MessageLoop::Quit
,
62 base::Unretained(base::MessageLoop::current())));
65 last_size_
= bounds
.size();
68 void OnDialogClosed(const std::string
& json_retval
) override
{
69 should_quit_on_size_change_
= false; // No quit when we are closing.
70 views::WebDialogView::OnDialogClosed(json_retval
);
73 // Whether we should quit message loop when size change is detected.
74 bool should_quit_on_size_change_
;
77 DISALLOW_COPY_AND_ASSIGN(TestWebDialogView
);
82 class WebDialogBrowserTest
: public InProcessBrowserTest
{
84 WebDialogBrowserTest() {}
87 // Windows has some issues resizing windows. An off by one problem, and a
88 // minimum size that seems too big. See http://crbug.com/52602.
89 // On Mac with toolkit_views, this test compiles but crashes at
90 // CreateWindowWithParent. See http://crbug.com/447086.
91 #if defined(OS_WIN) || defined(OS_MACOSX)
92 #define MAYBE_SizeWindow DISABLED_SizeWindow
94 #define MAYBE_SizeWindow SizeWindow
96 IN_PROC_BROWSER_TEST_F(WebDialogBrowserTest
, MAYBE_SizeWindow
) {
97 ui::test::TestWebDialogDelegate
delegate(
98 (GURL(chrome::kChromeUIChromeURLsURL
)));
99 delegate
.set_size(kInitialWidth
, kInitialHeight
);
101 TestWebDialogView
* view
=
102 new TestWebDialogView(browser()->profile(), &delegate
);
103 WebContents
* web_contents
=
104 browser()->tab_strip_model()->GetActiveWebContents();
105 ASSERT_TRUE(web_contents
!= NULL
);
106 views::Widget::CreateWindowWithParent(view
, web_contents
->GetNativeView());
107 view
->GetWidget()->Show();
109 // TestWebDialogView should quit current message loop on size change.
110 view
->set_should_quit_on_size_change(true);
112 gfx::Rect bounds
= view
->GetWidget()->GetClientAreaBoundsInScreen();
114 gfx::Rect set_bounds
= bounds
;
115 gfx::Rect actual_bounds
, rwhv_bounds
;
117 // Bigger than the default in both dimensions.
118 set_bounds
.set_width(400);
119 set_bounds
.set_height(300);
121 view
->MoveContents(web_contents
, set_bounds
);
122 content::RunMessageLoop(); // TestWebDialogView will quit.
123 actual_bounds
= view
->GetWidget()->GetClientAreaBoundsInScreen();
124 EXPECT_EQ(set_bounds
, actual_bounds
);
127 view
->web_contents()->GetRenderWidgetHostView()->GetViewBounds();
128 EXPECT_LT(0, rwhv_bounds
.width());
129 EXPECT_LT(0, rwhv_bounds
.height());
130 EXPECT_GE(set_bounds
.width(), rwhv_bounds
.width());
131 EXPECT_GE(set_bounds
.height(), rwhv_bounds
.height());
133 // Larger in one dimension and smaller in the other.
134 set_bounds
.set_width(550);
135 set_bounds
.set_height(250);
137 view
->MoveContents(web_contents
, set_bounds
);
138 content::RunMessageLoop(); // TestWebDialogView will quit.
139 actual_bounds
= view
->GetWidget()->GetClientAreaBoundsInScreen();
140 EXPECT_EQ(set_bounds
, actual_bounds
);
143 view
->web_contents()->GetRenderWidgetHostView()->GetViewBounds();
144 EXPECT_LT(0, rwhv_bounds
.width());
145 EXPECT_LT(0, rwhv_bounds
.height());
146 EXPECT_GE(set_bounds
.width(), rwhv_bounds
.width());
147 EXPECT_GE(set_bounds
.height(), rwhv_bounds
.height());
150 const gfx::Size min_size
= view
->GetWidget()->GetMinimumSize();
151 EXPECT_LT(0, min_size
.width());
152 EXPECT_LT(0, min_size
.height());
154 set_bounds
.set_size(min_size
);
156 view
->MoveContents(web_contents
, set_bounds
);
157 content::RunMessageLoop(); // TestWebDialogView will quit.
158 actual_bounds
= view
->GetWidget()->GetClientAreaBoundsInScreen();
159 EXPECT_EQ(set_bounds
, actual_bounds
);
162 view
->web_contents()->GetRenderWidgetHostView()->GetViewBounds();
163 EXPECT_LT(0, rwhv_bounds
.width());
164 EXPECT_LT(0, rwhv_bounds
.height());
165 EXPECT_GE(set_bounds
.width(), rwhv_bounds
.width());
166 EXPECT_GE(set_bounds
.height(), rwhv_bounds
.height());
168 // Check to make sure we can't get to 0x0. First expand beyond the minimum
169 // size that was set above so that TestWebDialogView has a change to pick up.
170 set_bounds
.set_height(250);
171 view
->MoveContents(web_contents
, set_bounds
);
172 content::RunMessageLoop(); // TestWebDialogView will quit.
173 actual_bounds
= view
->GetWidget()->GetClientAreaBoundsInScreen();
174 EXPECT_EQ(set_bounds
, actual_bounds
);
176 // Now verify that attempts to re-size to 0x0 enforces the minimum size.
177 set_bounds
.set_width(0);
178 set_bounds
.set_height(0);
180 view
->MoveContents(web_contents
, set_bounds
);
181 content::RunMessageLoop(); // TestWebDialogView will quit.
182 actual_bounds
= view
->GetWidget()->GetClientAreaBoundsInScreen();
183 EXPECT_EQ(min_size
, actual_bounds
.size());
185 // And that the render view is also non-zero.
187 view
->web_contents()->GetRenderWidgetHostView()->GetViewBounds();
188 EXPECT_LT(0, rwhv_bounds
.width());
189 EXPECT_LT(0, rwhv_bounds
.height());
191 view
->GetWidget()->CloseNow();