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.
5 #include "chrome/browser/ui/views/constrained_window_views.h"
7 #include "components/web_modal/test_web_contents_modal_dialog_host.h"
8 #include "ui/gfx/native_widget_types.h"
9 #include "ui/gfx/point.h"
10 #include "ui/gfx/rect.h"
11 #include "ui/gfx/size.h"
12 #include "ui/views/border.h"
13 #include "ui/views/test/views_test_base.h"
14 #include "ui/views/widget/widget.h"
15 #include "ui/views/window/dialog_delegate.h"
19 class DialogContents
: public DialogDelegateView
{
22 virtual ~DialogContents() {}
24 void set_preferred_size(const gfx::Size
& preferred_size
) {
25 preferred_size_
= preferred_size
;
28 // Overriden from DialogDelegateView:
29 virtual View
* GetContentsView() OVERRIDE
{ return this; }
30 virtual gfx::Size
GetPreferredSize() const OVERRIDE
{
31 return preferred_size_
;
33 virtual gfx::Size
GetMinimumSize() const OVERRIDE
{ return gfx::Size(); }
36 gfx::Size preferred_size_
;
38 DISALLOW_COPY_AND_ASSIGN(DialogContents
);
41 class ConstrainedWindowViewsTest
: public ViewsTestBase
{
43 ConstrainedWindowViewsTest() : contents_(NULL
) {}
44 virtual ~ConstrainedWindowViewsTest() {}
46 virtual void SetUp() OVERRIDE
{
47 ViewsTestBase::SetUp();
48 contents_
= new DialogContents
;
49 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_WINDOW
);
50 params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
51 params
.delegate
= contents_
;
52 dialog_
.reset(new Widget
);
53 dialog_
->Init(params
);
54 dialog_host_
.reset(new web_modal::TestWebContentsModalDialogHost(
55 dialog_
->GetNativeView()));
56 dialog_host_
->set_max_dialog_size(gfx::Size(5000, 5000));
58 // Make sure the dialog size is dominated by the preferred size of the
60 gfx::Size preferred_size
= dialog()->GetRootView()->GetPreferredSize();
61 preferred_size
.Enlarge(500, 500);
62 contents()->set_preferred_size(preferred_size
);
65 virtual void TearDown() OVERRIDE
{
66 ViewsTestBase::TearDown();
72 gfx::Size
GetDialogSize() {
73 return dialog()->GetRootView()->GetBoundsInScreen().size();
76 DialogContents
* contents() { return contents_
; }
77 web_modal::TestWebContentsModalDialogHost
* dialog_host() {
78 return dialog_host_
.get();
80 Widget
* dialog() { return dialog_
.get(); }
83 DialogContents
* contents_
;
84 scoped_ptr
<web_modal::TestWebContentsModalDialogHost
> dialog_host_
;
85 scoped_ptr
<Widget
> dialog_
;
87 DISALLOW_COPY_AND_ASSIGN(ConstrainedWindowViewsTest
);
90 // Make sure a dialog that increases its preferred size grows on the next
92 TEST_F(ConstrainedWindowViewsTest
, GrowModalDialogSize
) {
93 UpdateBrowserModalDialogPosition(dialog(), dialog_host());
94 gfx::Size expected_size
= GetDialogSize();
95 gfx::Size preferred_size
= contents()->GetPreferredSize();
96 expected_size
.Enlarge(50, 50);
97 preferred_size
.Enlarge(50, 50);
98 contents()->set_preferred_size(preferred_size
);
99 UpdateBrowserModalDialogPosition(dialog(), dialog_host());
100 EXPECT_EQ(expected_size
.ToString(), GetDialogSize().ToString());
103 // Make sure a dialog that reduces its preferred size shrinks on the next
105 TEST_F(ConstrainedWindowViewsTest
, ShrinkModalDialogSize
) {
106 UpdateBrowserModalDialogPosition(dialog(), dialog_host());
107 gfx::Size expected_size
= GetDialogSize();
108 gfx::Size preferred_size
= contents()->GetPreferredSize();
109 expected_size
.Enlarge(-50, -50);
110 preferred_size
.Enlarge(-50, -50);
111 contents()->set_preferred_size(preferred_size
);
112 UpdateBrowserModalDialogPosition(dialog(), dialog_host());
113 EXPECT_EQ(expected_size
.ToString(), GetDialogSize().ToString());
116 // Make sure browser modal dialogs are not affected by restrictions on web
117 // content modal dialog maximum sizes.
118 TEST_F(ConstrainedWindowViewsTest
, MaximumBrowserDialogSize
) {
119 UpdateBrowserModalDialogPosition(dialog(), dialog_host());
120 gfx::Size dialog_size
= GetDialogSize();
121 gfx::Size max_dialog_size
= dialog_size
;
122 max_dialog_size
.Enlarge(-50, -50);
123 dialog_host()->set_max_dialog_size(max_dialog_size
);
124 UpdateBrowserModalDialogPosition(dialog(), dialog_host());
125 EXPECT_EQ(dialog_size
.ToString(), GetDialogSize().ToString());
128 // Web content modal dialogs should not get a size larger than what the dialog
129 // host gives as the maximum size.
130 TEST_F(ConstrainedWindowViewsTest
, MaximumWebContentsDialogSize
) {
131 UpdateWebContentsModalDialogPosition(dialog(), dialog_host());
132 gfx::Size full_dialog_size
= GetDialogSize();
133 gfx::Size max_dialog_size
= full_dialog_size
;
134 max_dialog_size
.Enlarge(-50, -50);
135 dialog_host()->set_max_dialog_size(max_dialog_size
);
136 UpdateWebContentsModalDialogPosition(dialog(), dialog_host());
137 // The top border of the dialog is intentionally drawn outside the area
138 // specified by the dialog host, so add it to the size the dialog is expected
140 gfx::Size expected_size
= max_dialog_size
;
141 Border
* border
= dialog()->non_client_view()->frame_view()->border();
143 expected_size
.Enlarge(0, border
->GetInsets().top());
144 EXPECT_EQ(expected_size
.ToString(), GetDialogSize().ToString());
146 // Increasing the maximum dialog size should bring the dialog back to its
148 max_dialog_size
.Enlarge(100, 100);
149 dialog_host()->set_max_dialog_size(max_dialog_size
);
150 UpdateWebContentsModalDialogPosition(dialog(), dialog_host());
151 EXPECT_EQ(full_dialog_size
.ToString(), GetDialogSize().ToString());