[M120 Migration][VD] Enable direct rendering for TVPlus
[platform/framework/web/chromium-efl.git] / components / constrained_window / constrained_window_views_unittest.cc
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.
4
5 #include "components/constrained_window/constrained_window_views.h"
6 #include "base/memory/raw_ptr.h"
7
8 #include <memory>
9
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"
23
24 using views::Widget;
25
26 namespace constrained_window {
27 namespace {
28
29 // Dummy client that returns a null modal dialog host and host view.
30 class TestConstrainedWindowViewsClient
31     : public constrained_window::ConstrainedWindowViewsClient {
32  public:
33   TestConstrainedWindowViewsClient() = default;
34
35   TestConstrainedWindowViewsClient(const TestConstrainedWindowViewsClient&) =
36       delete;
37   TestConstrainedWindowViewsClient& operator=(
38       const TestConstrainedWindowViewsClient&) = delete;
39
40   // ConstrainedWindowViewsClient:
41   web_modal::ModalDialogHost* GetModalDialogHost(
42       gfx::NativeWindow parent) override {
43     return nullptr;
44   }
45   gfx::NativeView GetDialogHostView(gfx::NativeWindow parent) override {
46     return nullptr;
47   }
48 };
49
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 {
54  public:
55   TestViewsDelegateWithContext() = default;
56
57   TestViewsDelegateWithContext(const TestViewsDelegateWithContext&) = delete;
58   TestViewsDelegateWithContext& operator=(const TestViewsDelegateWithContext&) =
59       delete;
60
61   void set_context(gfx::NativeWindow context) { context_ = context; }
62
63   // ViewsDelegate:
64   void OnBeforeWidgetInit(
65       views::Widget::InitParams* params,
66       views::internal::NativeWidgetDelegate* delegate) override {
67     if (!params->context)
68       params->context = context_;
69     TestViewsDelegate::OnBeforeWidgetInit(params, delegate);
70   }
71
72  private:
73   gfx::NativeWindow context_ = gfx::NativeWindow();
74 };
75
76 class ConstrainedWindowViewsTest : public views::ViewsTestBase {
77  public:
78   ConstrainedWindowViewsTest() = default;
79
80   ConstrainedWindowViewsTest(const ConstrainedWindowViewsTest&) = delete;
81   ConstrainedWindowViewsTest& operator=(const ConstrainedWindowViewsTest&) =
82       delete;
83
84   ~ConstrainedWindowViewsTest() override = default;
85
86   void SetUp() override {
87     // set_views_delegate() must be called before SetUp(), and GetContext() is
88     // null before that.
89     auto* views_delegate =
90         set_views_delegate(std::make_unique<TestViewsDelegateWithContext>());
91     views::ViewsTestBase::SetUp();
92     views_delegate->set_context(GetContext());
93
94     delegate_ = std::make_unique<views::DialogDelegate>();
95     auto contents = std::make_unique<views::StaticSizedView>();
96     contents_ = delegate_->SetContentsView(std::move(contents));
97
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));
103
104     // Make sure the dialog size is dominated by the preferred size of the
105     // contents.
106     gfx::Size preferred_size = dialog()->GetRootView()->GetPreferredSize();
107     preferred_size.Enlarge(500, 500);
108     contents_->SetPreferredSize(preferred_size);
109   }
110
111   void TearDown() override {
112     contents_ = nullptr;
113     dialog_host_.reset();
114     dialog_->CloseNow();
115     ViewsTestBase::TearDown();
116   }
117
118   gfx::Size GetDialogSize() {
119     return dialog()->GetRootView()->GetBoundsInScreen().size();
120   }
121
122   views::DialogDelegate* delegate() { return delegate_.get(); }
123   views::View* contents() { return contents_; }
124   web_modal::TestWebContentsModalDialogHost* dialog_host() {
125     return dialog_host_.get();
126   }
127   Widget* dialog() { return dialog_; }
128
129  private:
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;
134 };
135
136 }  // namespace
137
138 // Make sure a dialog that increases its preferred size grows on the next
139 // position update.
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());
149 }
150
151 // Make sure a dialog that reduces its preferred size shrinks on the next
152 // position update.
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());
162 }
163
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());
174 }
175
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
187   // to occupy.
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());
192
193   // Increasing the maximum dialog size should bring the dialog back to its
194   // original size.
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());
199 }
200
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
205 #else
206 #define MAYBE_NullModalParent NullModalParent
207 #endif
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);
211
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);
218   widget->Show();
219   EXPECT_TRUE(widget->IsVisible());
220   widget->CloseNow();
221 }
222
223 // Make sure dialogs presented off-screen are properly clamped to the nearest
224 // screen.
225 TEST_F(ConstrainedWindowViewsTest, ClampDialogToNearestDisplay) {
226   // Make sure the dialog will fit fully on the display
227   contents()->SetPreferredSize(gfx::Size(200, 100));
228
229   // First, make sure the host and dialog are sized and positioned.
230   UpdateWebContentsModalDialogPosition(dialog(), dialog_host());
231
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();
237
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);
244
245   // Make sure the host is fully off the screen.
246   EXPECT_FALSE(extents.Intersects(host_widget->GetWindowBoundsInScreen()));
247
248   // Now reposition the modal dialog into the display.
249   UpdateWebContentsModalDialogPosition(dialog(), dialog_host());
250
251   const gfx::Rect dialog_bounds = dialog()->GetRootView()->GetBoundsInScreen();
252
253   // The dialog should now be fully on the display.
254   EXPECT_TRUE(extents.Contains(dialog_bounds));
255 }
256
257 }  // namespace constrained_window