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/geometry/point.h"
9 #include "ui/gfx/geometry/rect.h"
10 #include "ui/gfx/geometry/size.h"
11 #include "ui/gfx/native_widget_types.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_(nullptr), dialog_(nullptr) {}
45 ~ConstrainedWindowViewsTest() override
{}
47 void SetUp() override
{
48 views::ViewsTestBase::SetUp();
49 contents_
= new DialogContents
;
50 dialog_
= views::DialogDelegate::CreateDialogWidget(
51 contents_
, GetContext(), nullptr);
52 dialog_host_
.reset(new web_modal::TestWebContentsModalDialogHost(
53 dialog_
->GetNativeView()));
54 dialog_host_
->set_max_dialog_size(gfx::Size(5000, 5000));
56 // Make sure the dialog size is dominated by the preferred size of the
58 gfx::Size preferred_size
= dialog()->GetRootView()->GetPreferredSize();
59 preferred_size
.Enlarge(500, 500);
60 contents()->set_preferred_size(preferred_size
);
63 void TearDown() override
{
67 ViewsTestBase::TearDown();
70 gfx::Size
GetDialogSize() {
71 return dialog()->GetRootView()->GetBoundsInScreen().size();
74 DialogContents
* contents() { return contents_
; }
75 web_modal::TestWebContentsModalDialogHost
* dialog_host() {
76 return dialog_host_
.get();
78 Widget
* dialog() { return dialog_
; }
81 DialogContents
* contents_
;
82 scoped_ptr
<web_modal::TestWebContentsModalDialogHost
> dialog_host_
;
85 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 UpdateWidgetModalDialogPosition(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 UpdateWidgetModalDialogPosition(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 UpdateWidgetModalDialogPosition(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 UpdateWidgetModalDialogPosition(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 UpdateWidgetModalDialogPosition(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 UpdateWidgetModalDialogPosition(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 views::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());
154 } // namespace constrained_window