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 "components/constrained_window/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 namespace constrained_window
{
22 class DialogContents
: public views::DialogDelegateView
{
25 ~DialogContents() override
{}
27 void set_preferred_size(const gfx::Size
& preferred_size
) {
28 preferred_size_
= preferred_size
;
31 // Overriden from DialogDelegateView:
32 views::View
* GetContentsView() override
{ return this; }
33 gfx::Size
GetPreferredSize() const override
{ return preferred_size_
; }
34 gfx::Size
GetMinimumSize() const override
{ return gfx::Size(); }
37 gfx::Size preferred_size_
;
39 DISALLOW_COPY_AND_ASSIGN(DialogContents
);
42 class ConstrainedWindowViewsTest
: public views::ViewsTestBase
{
44 ConstrainedWindowViewsTest() : contents_(NULL
) {}
45 ~ConstrainedWindowViewsTest() override
{}
47 void SetUp() override
{
48 views::ViewsTestBase::SetUp();
49 contents_
= new DialogContents
;
50 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_WINDOW
);
51 params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
52 params
.delegate
= contents_
;
53 dialog_
.reset(new Widget
);
54 dialog_
->Init(params
);
55 dialog_host_
.reset(new web_modal::TestWebContentsModalDialogHost(
56 dialog_
->GetNativeView()));
57 dialog_host_
->set_max_dialog_size(gfx::Size(5000, 5000));
59 // Make sure the dialog size is dominated by the preferred size of the
61 gfx::Size preferred_size
= dialog()->GetRootView()->GetPreferredSize();
62 preferred_size
.Enlarge(500, 500);
63 contents()->set_preferred_size(preferred_size
);
66 void TearDown() override
{
67 ViewsTestBase::TearDown();
73 gfx::Size
GetDialogSize() {
74 return dialog()->GetRootView()->GetBoundsInScreen().size();
77 DialogContents
* contents() { return contents_
; }
78 web_modal::TestWebContentsModalDialogHost
* dialog_host() {
79 return dialog_host_
.get();
81 Widget
* dialog() { return dialog_
.get(); }
84 DialogContents
* contents_
;
85 scoped_ptr
<web_modal::TestWebContentsModalDialogHost
> dialog_host_
;
86 scoped_ptr
<Widget
> dialog_
;
88 DISALLOW_COPY_AND_ASSIGN(ConstrainedWindowViewsTest
);
93 // Make sure a dialog that increases its preferred size grows on the next
95 TEST_F(ConstrainedWindowViewsTest
, GrowModalDialogSize
) {
96 UpdateWidgetModalDialogPosition(dialog(), dialog_host());
97 gfx::Size expected_size
= GetDialogSize();
98 gfx::Size preferred_size
= contents()->GetPreferredSize();
99 expected_size
.Enlarge(50, 50);
100 preferred_size
.Enlarge(50, 50);
101 contents()->set_preferred_size(preferred_size
);
102 UpdateWidgetModalDialogPosition(dialog(), dialog_host());
103 EXPECT_EQ(expected_size
.ToString(), GetDialogSize().ToString());
106 // Make sure a dialog that reduces its preferred size shrinks on the next
108 TEST_F(ConstrainedWindowViewsTest
, ShrinkModalDialogSize
) {
109 UpdateWidgetModalDialogPosition(dialog(), dialog_host());
110 gfx::Size expected_size
= GetDialogSize();
111 gfx::Size preferred_size
= contents()->GetPreferredSize();
112 expected_size
.Enlarge(-50, -50);
113 preferred_size
.Enlarge(-50, -50);
114 contents()->set_preferred_size(preferred_size
);
115 UpdateWidgetModalDialogPosition(dialog(), dialog_host());
116 EXPECT_EQ(expected_size
.ToString(), GetDialogSize().ToString());
119 // Make sure browser modal dialogs are not affected by restrictions on web
120 // content modal dialog maximum sizes.
121 TEST_F(ConstrainedWindowViewsTest
, MaximumBrowserDialogSize
) {
122 UpdateWidgetModalDialogPosition(dialog(), dialog_host());
123 gfx::Size dialog_size
= GetDialogSize();
124 gfx::Size max_dialog_size
= dialog_size
;
125 max_dialog_size
.Enlarge(-50, -50);
126 dialog_host()->set_max_dialog_size(max_dialog_size
);
127 UpdateWidgetModalDialogPosition(dialog(), dialog_host());
128 EXPECT_EQ(dialog_size
.ToString(), GetDialogSize().ToString());
131 // Web content modal dialogs should not get a size larger than what the dialog
132 // host gives as the maximum size.
133 TEST_F(ConstrainedWindowViewsTest
, MaximumWebContentsDialogSize
) {
134 UpdateWebContentsModalDialogPosition(dialog(), dialog_host());
135 gfx::Size full_dialog_size
= GetDialogSize();
136 gfx::Size max_dialog_size
= full_dialog_size
;
137 max_dialog_size
.Enlarge(-50, -50);
138 dialog_host()->set_max_dialog_size(max_dialog_size
);
139 UpdateWebContentsModalDialogPosition(dialog(), dialog_host());
140 // The top border of the dialog is intentionally drawn outside the area
141 // specified by the dialog host, so add it to the size the dialog is expected
143 gfx::Size expected_size
= max_dialog_size
;
144 views::Border
* border
= dialog()->non_client_view()->frame_view()->border();
146 expected_size
.Enlarge(0, border
->GetInsets().top());
147 EXPECT_EQ(expected_size
.ToString(), GetDialogSize().ToString());
149 // Increasing the maximum dialog size should bring the dialog back to its
151 max_dialog_size
.Enlarge(100, 100);
152 dialog_host()->set_max_dialog_size(max_dialog_size
);
153 UpdateWebContentsModalDialogPosition(dialog(), dialog_host());
154 EXPECT_EQ(full_dialog_size
.ToString(), GetDialogSize().ToString());
157 } // namespace constrained_window