OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/constrained_window/constrained_window_views.h" | 5 #include "components/constrained_window/constrained_window_views.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
| 8 #include <vector> |
8 | 9 |
9 #include "base/macros.h" | 10 #include "base/macros.h" |
10 #include "components/constrained_window/constrained_window_views_client.h" | 11 #include "components/constrained_window/constrained_window_views_client.h" |
11 #include "components/web_modal/test_web_contents_modal_dialog_host.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" |
12 #include "ui/gfx/geometry/point.h" | 15 #include "ui/gfx/geometry/point.h" |
13 #include "ui/gfx/geometry/rect.h" | 16 #include "ui/gfx/geometry/rect.h" |
14 #include "ui/gfx/geometry/size.h" | 17 #include "ui/gfx/geometry/size.h" |
15 #include "ui/gfx/native_widget_types.h" | 18 #include "ui/gfx/native_widget_types.h" |
16 #include "ui/views/border.h" | 19 #include "ui/views/border.h" |
17 #include "ui/views/test/views_test_base.h" | 20 #include "ui/views/test/views_test_base.h" |
18 #include "ui/views/widget/widget.h" | 21 #include "ui/views/widget/widget.h" |
19 #include "ui/views/window/dialog_delegate.h" | 22 #include "ui/views/window/dialog_delegate.h" |
20 | 23 |
21 using views::Widget; | 24 using views::Widget; |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 SetConstrainedWindowViewsClient( | 224 SetConstrainedWindowViewsClient( |
222 base::MakeUnique<TestConstrainedWindowViewsClient>()); | 225 base::MakeUnique<TestConstrainedWindowViewsClient>()); |
223 DialogContents* contents = new DialogContents; | 226 DialogContents* contents = new DialogContents; |
224 contents->set_modal_type(ui::MODAL_TYPE_WINDOW); | 227 contents->set_modal_type(ui::MODAL_TYPE_WINDOW); |
225 views::Widget* widget = CreateBrowserModalDialogViews(contents, nullptr); | 228 views::Widget* widget = CreateBrowserModalDialogViews(contents, nullptr); |
226 widget->Show(); | 229 widget->Show(); |
227 EXPECT_TRUE(widget->IsVisible()); | 230 EXPECT_TRUE(widget->IsVisible()); |
228 widget->CloseNow(); | 231 widget->CloseNow(); |
229 } | 232 } |
230 | 233 |
| 234 // Make sure dialogs presented off-screen are properly clamped to the nearest |
| 235 // screen. |
| 236 TEST_F(ConstrainedWindowViewsTest, ClampDialogToNearestDisplay) { |
| 237 // Make sure the dialog will fit fully on the display |
| 238 contents()->set_preferred_size(gfx::Size(200, 100)); |
| 239 |
| 240 // First, make sure the host and dialog are sized and positioned. |
| 241 UpdateWebContentsModalDialogPosition(dialog(), dialog_host()); |
| 242 |
| 243 const display::Screen* screen = display::Screen::GetScreen(); |
| 244 const display::Display display = screen->GetPrimaryDisplay(); |
| 245 // Within the tests there is only 1 display. Error if that ever changes. |
| 246 EXPECT_EQ(screen->GetNumDisplays(), 1); |
| 247 const gfx::Rect extents = display.work_area(); |
| 248 |
| 249 // Move the host completely off the screen. |
| 250 views::Widget* host_widget = |
| 251 views::Widget::GetWidgetForNativeView(dialog_host()->GetHostView()); |
| 252 gfx::Rect host_bounds = host_widget->GetWindowBoundsInScreen(); |
| 253 host_bounds.set_origin(gfx::Point(extents.right(), extents.bottom())); |
| 254 host_widget->SetBounds(host_bounds); |
| 255 |
| 256 // Make sure the host is fully off the screen. |
| 257 EXPECT_FALSE(extents.Intersects(host_widget->GetWindowBoundsInScreen())); |
| 258 |
| 259 // Now reposition the modal dialog into the display. |
| 260 UpdateWebContentsModalDialogPosition(dialog(), dialog_host()); |
| 261 |
| 262 const gfx::Rect dialog_bounds = dialog()->GetRootView()->GetBoundsInScreen(); |
| 263 |
| 264 // The dialog should now be fully on the display. |
| 265 EXPECT_TRUE(extents.Contains(dialog_bounds)); |
| 266 } |
| 267 |
231 } // namespace constrained_window | 268 } // namespace constrained_window |
OLD | NEW |