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 // First, make sure the host and dialog are sized and positioned. | |
238 UpdateWebContentsModalDialogPosition(dialog(), dialog_host()); | |
239 | |
240 // Calculate the largest screen rectangle that will contain all the displays. | |
241 const display::Screen* screen = display::Screen::GetScreen(); | |
242 const std::vector<display::Display>& displays = screen->GetAllDisplays(); | |
243 gfx::Rect extents = gfx::Rect(); | |
244 for (auto display : displays) { | |
245 const gfx::Rect bounds = display.bounds(); | |
msw
2016/12/15 20:46:09
use Rect::Union (or skip and just assert that ther
kylix_rd
2016/12/15 21:30:15
Done.
| |
246 extents.set_x(std::min(bounds.x(), extents.x())); | |
247 extents.set_y(std::min(bounds.y(), extents.y())); | |
248 extents.set_width(std::max(bounds.right(), | |
249 extents.right()) - extents.x()); | |
250 extents.set_height(std::max(bounds.bottom(), | |
251 extents.bottom()) - extents.y()); | |
252 } | |
253 | |
254 // Move the host completely off the screen. | |
255 views::Widget* host_widget = | |
256 views::Widget::GetWidgetForNativeView(dialog_host()->GetHostView()); | |
257 gfx::Rect host_bounds = host_widget->GetWindowBoundsInScreen(); | |
258 host_bounds.set_origin(gfx::Point(extents.right(), extents.bottom())); | |
259 host_widget->SetBounds(host_bounds); | |
260 | |
261 // Precalculate the expected location of the dialog. | |
msw
2016/12/15 20:46:09
just check that the one display's bounds contains
kylix_rd
2016/12/15 21:30:16
Yes, that code seemed a little silly... I've rewor
| |
262 const display::Display display = screen->GetDisplayNearestWindow( | |
263 dialog_host()->GetHostView()); | |
264 gfx::Rect expected_bounds = dialog()->GetRootView()->GetBoundsInScreen(); | |
265 expected_bounds.AdjustToFit(display.work_area()); | |
266 | |
267 // Now make sure the modal dialog is repositioned into one of the displays. | |
268 // This should be on the display calculated above. | |
269 UpdateWebContentsModalDialogPosition(dialog(), dialog_host()); | |
270 | |
271 const gfx::Rect dialog_bounds = dialog()->GetRootView()->GetBoundsInScreen(); | |
272 | |
273 // expected_bounds.origin() should be where the dialog is now located. | |
274 // NOTE: Don't test the size since it is constrained by preferred size. | |
275 EXPECT_EQ(expected_bounds.origin(), dialog_bounds.origin()); | |
276 } | |
277 | |
231 } // namespace constrained_window | 278 } // namespace constrained_window |
OLD | NEW |