1 // Copyright 2013 The Chromium Authors
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"
6 #include "base/memory/raw_ptr.h"
10 #include "build/build_config.h"
11 #include "components/constrained_window/constrained_window_views_client.h"
12 #include "components/web_modal/test_web_contents_modal_dialog_host.h"
13 #include "ui/display/display.h"
14 #include "ui/display/screen.h"
15 #include "ui/gfx/geometry/point.h"
16 #include "ui/gfx/geometry/rect.h"
17 #include "ui/gfx/geometry/size.h"
18 #include "ui/gfx/native_widget_types.h"
19 #include "ui/views/test/test_views.h"
20 #include "ui/views/test/views_test_base.h"
21 #include "ui/views/widget/widget.h"
22 #include "ui/views/window/dialog_delegate.h"
26 namespace constrained_window {
29 // Dummy client that returns a null modal dialog host and host view.
30 class TestConstrainedWindowViewsClient
31 : public constrained_window::ConstrainedWindowViewsClient {
33 TestConstrainedWindowViewsClient() = default;
35 TestConstrainedWindowViewsClient(const TestConstrainedWindowViewsClient&) =
37 TestConstrainedWindowViewsClient& operator=(
38 const TestConstrainedWindowViewsClient&) = delete;
40 // ConstrainedWindowViewsClient:
41 web_modal::ModalDialogHost* GetModalDialogHost(
42 gfx::NativeWindow parent) override {
45 gfx::NativeView GetDialogHostView(gfx::NativeWindow parent) override {
50 // ViewsDelegate to provide context to dialog creation functions such as
51 // CreateBrowserModalDialogViews() which do not allow InitParams to be set, and
52 // pass a null |context| argument to DialogDelegate::CreateDialogWidget().
53 class TestViewsDelegateWithContext : public views::TestViewsDelegate {
55 TestViewsDelegateWithContext() = default;
57 TestViewsDelegateWithContext(const TestViewsDelegateWithContext&) = delete;
58 TestViewsDelegateWithContext& operator=(const TestViewsDelegateWithContext&) =
61 void set_context(gfx::NativeWindow context) { context_ = context; }
64 void OnBeforeWidgetInit(
65 views::Widget::InitParams* params,
66 views::internal::NativeWidgetDelegate* delegate) override {
68 params->context = context_;
69 TestViewsDelegate::OnBeforeWidgetInit(params, delegate);
73 gfx::NativeWindow context_ = gfx::NativeWindow();
76 class ConstrainedWindowViewsTest : public views::ViewsTestBase {
78 ConstrainedWindowViewsTest() = default;
80 ConstrainedWindowViewsTest(const ConstrainedWindowViewsTest&) = delete;
81 ConstrainedWindowViewsTest& operator=(const ConstrainedWindowViewsTest&) =
84 ~ConstrainedWindowViewsTest() override = default;
86 void SetUp() override {
87 // set_views_delegate() must be called before SetUp(), and GetContext() is
89 auto* views_delegate =
90 set_views_delegate(std::make_unique<TestViewsDelegateWithContext>());
91 views::ViewsTestBase::SetUp();
92 views_delegate->set_context(GetContext());
94 delegate_ = std::make_unique<views::DialogDelegate>();
95 auto contents = std::make_unique<views::StaticSizedView>();
96 contents_ = delegate_->SetContentsView(std::move(contents));
98 dialog_ = views::DialogDelegate::CreateDialogWidget(delegate_.get(),
99 GetContext(), nullptr);
100 dialog_host_ = std::make_unique<web_modal::TestWebContentsModalDialogHost>(
101 dialog_->GetNativeView());
102 dialog_host_->set_max_dialog_size(gfx::Size(5000, 5000));
104 // Make sure the dialog size is dominated by the preferred size of the
106 gfx::Size preferred_size = dialog()->GetRootView()->GetPreferredSize();
107 preferred_size.Enlarge(500, 500);
108 contents_->SetPreferredSize(preferred_size);
111 void TearDown() override {
113 dialog_host_.reset();
115 ViewsTestBase::TearDown();
118 gfx::Size GetDialogSize() {
119 return dialog()->GetRootView()->GetBoundsInScreen().size();
122 views::DialogDelegate* delegate() { return delegate_.get(); }
123 views::View* contents() { return contents_; }
124 web_modal::TestWebContentsModalDialogHost* dialog_host() {
125 return dialog_host_.get();
127 Widget* dialog() { return dialog_; }
130 std::unique_ptr<views::DialogDelegate> delegate_;
131 raw_ptr<views::View> contents_ = nullptr;
132 std::unique_ptr<web_modal::TestWebContentsModalDialogHost> dialog_host_;
133 raw_ptr<Widget, DanglingUntriaged> dialog_ = nullptr;
138 // Make sure a dialog that increases its preferred size grows on the next
140 TEST_F(ConstrainedWindowViewsTest, GrowModalDialogSize) {
141 UpdateWidgetModalDialogPosition(dialog(), dialog_host());
142 gfx::Size expected_size = GetDialogSize();
143 gfx::Size preferred_size = contents()->GetPreferredSize();
144 expected_size.Enlarge(50, 50);
145 preferred_size.Enlarge(50, 50);
146 contents()->SetPreferredSize(preferred_size);
147 UpdateWidgetModalDialogPosition(dialog(), dialog_host());
148 EXPECT_EQ(expected_size.ToString(), GetDialogSize().ToString());
151 // Make sure a dialog that reduces its preferred size shrinks on the next
153 TEST_F(ConstrainedWindowViewsTest, ShrinkModalDialogSize) {
154 UpdateWidgetModalDialogPosition(dialog(), dialog_host());
155 gfx::Size expected_size = GetDialogSize();
156 gfx::Size preferred_size = contents()->GetPreferredSize();
157 expected_size.Enlarge(-50, -50);
158 preferred_size.Enlarge(-50, -50);
159 contents()->SetPreferredSize(preferred_size);
160 UpdateWidgetModalDialogPosition(dialog(), dialog_host());
161 EXPECT_EQ(expected_size.ToString(), GetDialogSize().ToString());
164 // Make sure browser modal dialogs are not affected by restrictions on web
165 // content modal dialog maximum sizes.
166 TEST_F(ConstrainedWindowViewsTest, MaximumBrowserDialogSize) {
167 UpdateWidgetModalDialogPosition(dialog(), dialog_host());
168 gfx::Size dialog_size = GetDialogSize();
169 gfx::Size max_dialog_size = dialog_size;
170 max_dialog_size.Enlarge(-50, -50);
171 dialog_host()->set_max_dialog_size(max_dialog_size);
172 UpdateWidgetModalDialogPosition(dialog(), dialog_host());
173 EXPECT_EQ(dialog_size.ToString(), GetDialogSize().ToString());
176 // Web content modal dialogs should not get a size larger than what the dialog
177 // host gives as the maximum size.
178 TEST_F(ConstrainedWindowViewsTest, MaximumWebContentsDialogSize) {
179 UpdateWebContentsModalDialogPosition(dialog(), dialog_host());
180 gfx::Size full_dialog_size = GetDialogSize();
181 gfx::Size max_dialog_size = full_dialog_size;
182 max_dialog_size.Enlarge(-50, -50);
183 dialog_host()->set_max_dialog_size(max_dialog_size);
184 UpdateWebContentsModalDialogPosition(dialog(), dialog_host());
185 // The top border of the dialog is intentionally drawn outside the area
186 // specified by the dialog host, so add it to the size the dialog is expected
188 gfx::Size expected_size = max_dialog_size;
189 expected_size.Enlarge(
190 0, dialog()->non_client_view()->frame_view()->GetInsets().top());
191 EXPECT_EQ(expected_size.ToString(), GetDialogSize().ToString());
193 // Increasing the maximum dialog size should bring the dialog back to its
195 max_dialog_size.Enlarge(100, 100);
196 dialog_host()->set_max_dialog_size(max_dialog_size);
197 UpdateWebContentsModalDialogPosition(dialog(), dialog_host());
198 EXPECT_EQ(full_dialog_size.ToString(), GetDialogSize().ToString());
201 // Ensure CreateBrowserModalDialogViews() works correctly with a null parent.
202 // Flaky on Win10. https://crbug.com/1009182
203 #if BUILDFLAG(IS_WIN)
204 #define MAYBE_NullModalParent DISABLED_NullModalParent
206 #define MAYBE_NullModalParent NullModalParent
208 TEST_F(ConstrainedWindowViewsTest, MAYBE_NullModalParent) {
209 // Use desktop widgets (except on ChromeOS) for extra coverage.
210 test_views_delegate()->set_use_desktop_native_widgets(true);
212 SetConstrainedWindowViewsClient(
213 std::make_unique<TestConstrainedWindowViewsClient>());
214 auto delegate = std::make_unique<views::DialogDelegate>();
215 delegate->SetModalType(ui::MODAL_TYPE_WINDOW);
216 views::Widget* widget =
217 CreateBrowserModalDialogViews(delegate.get(), nullptr);
219 EXPECT_TRUE(widget->IsVisible());
223 // Make sure dialogs presented off-screen are properly clamped to the nearest
225 TEST_F(ConstrainedWindowViewsTest, ClampDialogToNearestDisplay) {
226 // Make sure the dialog will fit fully on the display
227 contents()->SetPreferredSize(gfx::Size(200, 100));
229 // First, make sure the host and dialog are sized and positioned.
230 UpdateWebContentsModalDialogPosition(dialog(), dialog_host());
232 const display::Screen* screen = display::Screen::GetScreen();
233 const display::Display display = screen->GetPrimaryDisplay();
234 // Within the tests there is only 1 display. Error if that ever changes.
235 EXPECT_EQ(screen->GetNumDisplays(), 1);
236 const gfx::Rect extents = display.work_area();
238 // Move the host completely off the screen.
239 views::Widget* host_widget =
240 views::Widget::GetWidgetForNativeView(dialog_host()->GetHostView());
241 gfx::Rect host_bounds = host_widget->GetWindowBoundsInScreen();
242 host_bounds.set_origin(gfx::Point(extents.right(), extents.bottom()));
243 host_widget->SetBounds(host_bounds);
245 // Make sure the host is fully off the screen.
246 EXPECT_FALSE(extents.Intersects(host_widget->GetWindowBoundsInScreen()));
248 // Now reposition the modal dialog into the display.
249 UpdateWebContentsModalDialogPosition(dialog(), dialog_host());
251 const gfx::Rect dialog_bounds = dialog()->GetRootView()->GetBoundsInScreen();
253 // The dialog should now be fully on the display.
254 EXPECT_TRUE(extents.Contains(dialog_bounds));
257 } // namespace constrained_window