summaryrefslogtreecommitdiff
path: root/chromium/ui/views/widget/desktop_widget_unittest.cc
blob: 0ab25f179c209461af8aa026a6b32ed4d724a6eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/views/test/native_widget_factory.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/dialog_delegate.h"

namespace views {

typedef ViewsTestBase DesktopScreenPositionClientTest;

// Verifies setting the bounds of a dialog parented to a Widget with a
// PlatformDesktopNativeWidget is positioned correctly.
TEST_F(DesktopScreenPositionClientTest, PositionDialog) {
  Widget parent_widget;
  Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
  params.bounds = gfx::Rect(10, 11, 200, 200);
  params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
  params.native_widget = test::CreatePlatformDesktopNativeWidgetImpl(
      params, &parent_widget, nullptr);
  parent_widget.Init(params);

  // Owned by |dialog|.
  DialogDelegateView* dialog_delegate_view = new DialogDelegateView;
  // Owned by |parent_widget|.
  Widget* dialog = DialogDelegate::CreateDialogWidget(
      dialog_delegate_view, NULL, parent_widget.GetNativeView());
  dialog->SetBounds(gfx::Rect(11, 12, 200, 200));
  EXPECT_EQ(gfx::Point(11, 12), dialog->GetWindowBoundsInScreen().origin());
}

// Verifies that setting the bounds of a control parented to something other
// than the root window is positioned correctly.
TEST_F(DesktopScreenPositionClientTest, PositionControlWithNonRootParent) {
  Widget widget1;
  Widget widget2;
  Widget widget3;
  gfx::Point origin = gfx::Point(16, 16);
  gfx::Rect work_area =
      display::Screen::GetScreen()->GetDisplayNearestPoint(origin).work_area();

  // Use a custom frame type.  By default we will choose a native frame when
  // aero glass is enabled, and this complicates the logic surrounding origin
  // computation, making it difficult to compute the expected origin location.
  widget1.set_frame_type(Widget::FRAME_TYPE_FORCE_CUSTOM);
  widget2.set_frame_type(Widget::FRAME_TYPE_FORCE_CUSTOM);
  widget3.set_frame_type(Widget::FRAME_TYPE_FORCE_CUSTOM);

  // Create 3 windows.  A root window, an arbitrary window parented to the root
  // but NOT positioned at (0,0) relative to the root, and then a third window
  // parented to the second, also not positioned at (0,0).
  Widget::InitParams params1 =
      CreateParams(Widget::InitParams::TYPE_WINDOW);
  params1.bounds = gfx::Rect(
      origin + work_area.OffsetFromOrigin(),
      gfx::Size(700, work_area.height() - origin.y() - work_area.y()));
  params1.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
  params1.native_widget =
      test::CreatePlatformDesktopNativeWidgetImpl(params1, &widget1, nullptr);
  widget1.Init(params1);

  Widget::InitParams params2 =
    CreateParams(Widget::InitParams::TYPE_WINDOW);
  params2.bounds = gfx::Rect(origin, gfx::Size(600, work_area.height() - 100));
  params2.parent = widget1.GetNativeView();
  params2.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
  params2.child = true;
  widget2.Init(params2);

  Widget::InitParams params3 =
      CreateParams(Widget::InitParams::TYPE_CONTROL);
  params3.parent = widget2.GetNativeView();
  params3.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
  params3.child = true;
  params3.bounds = gfx::Rect(origin, gfx::Size(500, work_area.height() - 200));
  widget3.Init(params3);

  // The origin of the 3rd window should be the sum of all parent origins.
  gfx::Point expected_origin(origin.x() * 3 + work_area.x(),
                             origin.y() * 3 + work_area.y());
  gfx::Rect expected_bounds(expected_origin,
                            gfx::Size(500, work_area.height() - 200));
  gfx::Rect actual_bounds(widget3.GetWindowBoundsInScreen());
  EXPECT_EQ(expected_bounds, actual_bounds);
}

// Verifies that the initial bounds of the widget is fully on the screen.
TEST_F(DesktopScreenPositionClientTest, InitialBoundsConstrainedToDesktop) {
  Widget widget;
  // Use the primary display for this test.
  gfx::Rect work_area =
      display::Screen::GetScreen()->GetPrimaryDisplay().work_area();
  // Make the origin start at 75% of the width and height.
  gfx::Point origin =
      gfx::Point(work_area.width() * 3 / 4, work_area.height() * 3 / 4);

  // Use a custom frame type. See above for further explanation.
  widget.set_frame_type(Widget::FRAME_TYPE_FORCE_CUSTOM);

  // Create a window that is intentionally positioned so that it is off screen.
  Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
  params.bounds = gfx::Rect(
      origin, gfx::Size(work_area.width() / 2, work_area.height() / 2));
  params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
  params.native_widget =
      test::CreatePlatformDesktopNativeWidgetImpl(params, &widget, nullptr);
  widget.Init(params);

  // The bounds of the window should be fully on the primary display.
  gfx::Point expected_origin(work_area.right() - work_area.width() / 2,
                             work_area.bottom() - work_area.height() / 2);
  gfx::Rect expected_bounds(expected_origin, gfx::Size(work_area.width() / 2,
                                                       work_area.height() / 2));
  gfx::Rect actual_bounds(widget.GetWindowBoundsInScreen());
  EXPECT_EQ(expected_bounds, actual_bounds);
}

// Verifies that the initial bounds of the widget is fully within the bounds of
// the parent.
TEST_F(DesktopScreenPositionClientTest, InitialBoundsConstrainedToParent) {
  Widget widget1;
  Widget widget2;
  // Use the primary display for this test.
  gfx::Rect work_area =
      display::Screen::GetScreen()->GetPrimaryDisplay().work_area();
  gfx::Point origin = gfx::Point(work_area.x() + work_area.width() / 4,
                                 work_area.y() + work_area.height() / 4);

  // Use a custom frame type.  See above for further explanation
  widget1.set_frame_type(Widget::FRAME_TYPE_FORCE_CUSTOM);
  widget2.set_frame_type(Widget::FRAME_TYPE_FORCE_CUSTOM);

  // Create 2 windows.  A root window, and an arbitrary window parented to the
  // root and positioned such that it extends beyond the bounds of the root.
  Widget::InitParams params1 = CreateParams(Widget::InitParams::TYPE_WINDOW);
  params1.bounds = gfx::Rect(
      origin, gfx::Size(work_area.width() / 2, work_area.height() / 2));
  params1.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
  params1.native_widget =
      test::CreatePlatformDesktopNativeWidgetImpl(params1, &widget1, nullptr);
  widget1.Init(params1);

  gfx::Rect widget_bounds(widget1.GetWindowBoundsInScreen());

  Widget::InitParams params2 = CreateParams(Widget::InitParams::TYPE_WINDOW);
  params2.bounds =
      gfx::Rect(widget_bounds.width() * 3 / 4, widget_bounds.height() * 3 / 4,
                widget_bounds.width() / 2, widget_bounds.height() / 2);
  params2.parent = widget1.GetNativeView();
  params2.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
  params2.child = true;
  widget2.Init(params2);

  // The bounds of the child window should be fully in the parent.
  gfx::Point expected_origin(
      widget_bounds.right() - widget_bounds.width() / 2,
      widget_bounds.bottom() - widget_bounds.height() / 2);
  gfx::Rect expected_bounds(
      expected_origin,
      gfx::Size(widget_bounds.width() / 2, widget_bounds.height() / 2));
  gfx::Rect actual_bounds(widget2.GetWindowBoundsInScreen());
  EXPECT_EQ(expected_bounds, actual_bounds);
}

}  // namespace views