1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/views/widget/desktop_aura/desktop_screen_x11.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/aura/client/aura_constants.h"
10 #include "ui/aura/window.h"
11 #include "ui/aura/window_event_dispatcher.h"
12 #include "ui/base/hit_test.h"
13 #include "ui/base/x/x11_util.h"
14 #include "ui/events/test/event_generator.h"
15 #include "ui/gfx/display_observer.h"
16 #include "ui/gfx/x/x11_types.h"
17 #include "ui/views/test/views_test_base.h"
18 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
19 #include "ui/views/widget/desktop_aura/desktop_window_tree_host_x11.h"
23 // Class which allows for the designation of non-client component targets of
25 class TestDesktopNativeWidgetAura
: public views::DesktopNativeWidgetAura
{
27 explicit TestDesktopNativeWidgetAura(
28 views::internal::NativeWidgetDelegate
* delegate
)
29 : views::DesktopNativeWidgetAura(delegate
) {}
30 ~TestDesktopNativeWidgetAura() override
{}
32 void set_window_component(int window_component
) {
33 window_component_
= window_component
;
36 // DesktopNativeWidgetAura:
37 int GetNonClientComponent(const gfx::Point
& point
) const override
{
38 return window_component_
;
42 int window_component_
;
44 DISALLOW_COPY_AND_ASSIGN(TestDesktopNativeWidgetAura
);
51 const int64 kFirstDisplay
= 5321829;
52 const int64 kSecondDisplay
= 928310;
54 class DesktopScreenX11Test
: public views::ViewsTestBase
,
55 public gfx::DisplayObserver
{
57 DesktopScreenX11Test() {}
58 ~DesktopScreenX11Test() override
{}
60 // Overridden from testing::Test:
61 void SetUp() override
{
62 ViewsTestBase::SetUp();
63 // Initialize the world to the single monitor case.
64 std::vector
<gfx::Display
> displays
;
65 displays
.push_back(gfx::Display(kFirstDisplay
, gfx::Rect(0, 0, 640, 480)));
66 screen_
.reset(new DesktopScreenX11(displays
));
67 screen_
->AddObserver(this);
70 void TearDown() override
{
72 ViewsTestBase::TearDown();
76 std::vector
<gfx::Display
> changed_display_
;
77 std::vector
<gfx::Display
> added_display_
;
78 std::vector
<gfx::Display
> removed_display_
;
80 DesktopScreenX11
* screen() { return screen_
.get(); }
82 void NotifyDisplaysChanged(const std::vector
<gfx::Display
>& displays
) {
83 DesktopScreenX11
* screen
= screen_
.get();
84 screen
->change_notifier_
.NotifyDisplaysChanged(screen
->displays_
, displays
);
85 screen
->displays_
= displays
;
88 void ResetDisplayChanges() {
89 changed_display_
.clear();
90 added_display_
.clear();
91 removed_display_
.clear();
94 Widget
* BuildTopLevelDesktopWidget(const gfx::Rect
& bounds
,
95 bool use_test_native_widget
) {
96 Widget
* toplevel
= new Widget
;
97 Widget::InitParams toplevel_params
=
98 CreateParams(Widget::InitParams::TYPE_WINDOW
);
99 if (use_test_native_widget
) {
100 toplevel_params
.native_widget
=
101 new TestDesktopNativeWidgetAura(toplevel
);
103 toplevel_params
.native_widget
=
104 new views::DesktopNativeWidgetAura(toplevel
);
106 toplevel_params
.bounds
= bounds
;
107 toplevel_params
.remove_standard_frame
= true;
108 toplevel
->Init(toplevel_params
);
113 // Overridden from gfx::DisplayObserver:
114 void OnDisplayAdded(const gfx::Display
& new_display
) override
{
115 added_display_
.push_back(new_display
);
118 void OnDisplayRemoved(const gfx::Display
& old_display
) override
{
119 removed_display_
.push_back(old_display
);
122 void OnDisplayMetricsChanged(const gfx::Display
& display
,
123 uint32_t metrics
) override
{
124 changed_display_
.push_back(display
);
127 scoped_ptr
<DesktopScreenX11
> screen_
;
129 DISALLOW_COPY_AND_ASSIGN(DesktopScreenX11Test
);
132 TEST_F(DesktopScreenX11Test
, BoundsChangeSingleMonitor
) {
133 std::vector
<gfx::Display
> displays
;
134 displays
.push_back(gfx::Display(kFirstDisplay
, gfx::Rect(0, 0, 1024, 768)));
135 NotifyDisplaysChanged(displays
);
137 EXPECT_EQ(1u, changed_display_
.size());
138 EXPECT_EQ(0u, added_display_
.size());
139 EXPECT_EQ(0u, removed_display_
.size());
142 TEST_F(DesktopScreenX11Test
, AddMonitorToTheRight
) {
143 std::vector
<gfx::Display
> displays
;
144 displays
.push_back(gfx::Display(kFirstDisplay
, gfx::Rect(0, 0, 640, 480)));
145 displays
.push_back(gfx::Display(kSecondDisplay
,
146 gfx::Rect(640, 0, 1024, 768)));
147 NotifyDisplaysChanged(displays
);
149 EXPECT_EQ(0u, changed_display_
.size());
150 EXPECT_EQ(1u, added_display_
.size());
151 EXPECT_EQ(0u, removed_display_
.size());
154 TEST_F(DesktopScreenX11Test
, AddMonitorToTheLeft
) {
155 std::vector
<gfx::Display
> displays
;
156 displays
.push_back(gfx::Display(kSecondDisplay
, gfx::Rect(0, 0, 1024, 768)));
157 displays
.push_back(gfx::Display(kFirstDisplay
, gfx::Rect(1024, 0, 640, 480)));
158 NotifyDisplaysChanged(displays
);
160 EXPECT_EQ(1u, changed_display_
.size());
161 EXPECT_EQ(1u, added_display_
.size());
162 EXPECT_EQ(0u, removed_display_
.size());
165 TEST_F(DesktopScreenX11Test
, RemoveMonitorOnRight
) {
166 std::vector
<gfx::Display
> displays
;
167 displays
.push_back(gfx::Display(kFirstDisplay
, gfx::Rect(0, 0, 640, 480)));
168 displays
.push_back(gfx::Display(kSecondDisplay
,
169 gfx::Rect(640, 0, 1024, 768)));
170 NotifyDisplaysChanged(displays
);
172 ResetDisplayChanges();
175 displays
.push_back(gfx::Display(kFirstDisplay
, gfx::Rect(0, 0, 640, 480)));
176 NotifyDisplaysChanged(displays
);
178 EXPECT_EQ(0u, changed_display_
.size());
179 EXPECT_EQ(0u, added_display_
.size());
180 EXPECT_EQ(1u, removed_display_
.size());
183 TEST_F(DesktopScreenX11Test
, RemoveMonitorOnLeft
) {
184 std::vector
<gfx::Display
> displays
;
185 displays
.push_back(gfx::Display(kFirstDisplay
, gfx::Rect(0, 0, 640, 480)));
186 displays
.push_back(gfx::Display(kSecondDisplay
,
187 gfx::Rect(640, 0, 1024, 768)));
188 NotifyDisplaysChanged(displays
);
190 ResetDisplayChanges();
193 displays
.push_back(gfx::Display(kSecondDisplay
, gfx::Rect(0, 0, 1024, 768)));
194 NotifyDisplaysChanged(displays
);
196 EXPECT_EQ(1u, changed_display_
.size());
197 EXPECT_EQ(0u, added_display_
.size());
198 EXPECT_EQ(1u, removed_display_
.size());
201 TEST_F(DesktopScreenX11Test
, GetDisplayNearestPoint
) {
202 std::vector
<gfx::Display
> displays
;
203 displays
.push_back(gfx::Display(kFirstDisplay
, gfx::Rect(0, 0, 640, 480)));
204 displays
.push_back(gfx::Display(kSecondDisplay
,
205 gfx::Rect(640, 0, 1024, 768)));
206 NotifyDisplaysChanged(displays
);
208 EXPECT_EQ(kFirstDisplay
,
209 screen()->GetDisplayNearestPoint(gfx::Point(630, 10)).id());
210 EXPECT_EQ(kSecondDisplay
,
211 screen()->GetDisplayNearestPoint(gfx::Point(650, 10)).id());
212 EXPECT_EQ(kFirstDisplay
,
213 screen()->GetDisplayNearestPoint(gfx::Point(10, 10)).id());
214 EXPECT_EQ(kFirstDisplay
,
215 screen()->GetDisplayNearestPoint(gfx::Point(10000, 10000)).id());
218 TEST_F(DesktopScreenX11Test
, GetDisplayMatchingBasic
) {
219 std::vector
<gfx::Display
> displays
;
220 displays
.push_back(gfx::Display(kFirstDisplay
, gfx::Rect(0, 0, 640, 480)));
221 displays
.push_back(gfx::Display(kSecondDisplay
,
222 gfx::Rect(640, 0, 1024, 768)));
223 NotifyDisplaysChanged(displays
);
225 EXPECT_EQ(kSecondDisplay
,
226 screen()->GetDisplayMatching(gfx::Rect(700, 20, 100, 100)).id());
229 TEST_F(DesktopScreenX11Test
, GetDisplayMatchingOverlap
) {
230 std::vector
<gfx::Display
> displays
;
231 displays
.push_back(gfx::Display(kFirstDisplay
, gfx::Rect(0, 0, 640, 480)));
232 displays
.push_back(gfx::Display(kSecondDisplay
,
233 gfx::Rect(640, 0, 1024, 768)));
234 NotifyDisplaysChanged(displays
);
236 EXPECT_EQ(kSecondDisplay
,
237 screen()->GetDisplayMatching(gfx::Rect(630, 20, 100, 100)).id());
240 TEST_F(DesktopScreenX11Test
, GetPrimaryDisplay
) {
241 std::vector
<gfx::Display
> displays
;
242 displays
.push_back(gfx::Display(kFirstDisplay
,
243 gfx::Rect(640, 0, 1024, 768)));
244 displays
.push_back(gfx::Display(kSecondDisplay
, gfx::Rect(0, 0, 640, 480)));
245 NotifyDisplaysChanged(displays
);
247 // The first display in the list is always the primary, even if other
248 // displays are to the left in screen layout.
249 EXPECT_EQ(kFirstDisplay
, screen()->GetPrimaryDisplay().id());
252 TEST_F(DesktopScreenX11Test
, GetDisplayNearestWindow
) {
253 // Set up a two monitor situation.
254 std::vector
<gfx::Display
> displays
;
255 displays
.push_back(gfx::Display(kFirstDisplay
, gfx::Rect(0, 0, 640, 480)));
256 displays
.push_back(gfx::Display(kSecondDisplay
,
257 gfx::Rect(640, 0, 1024, 768)));
258 NotifyDisplaysChanged(displays
);
260 Widget
* window_one
= BuildTopLevelDesktopWidget(gfx::Rect(10, 10, 10, 10),
262 Widget
* window_two
= BuildTopLevelDesktopWidget(gfx::Rect(650, 50, 10, 10),
267 screen()->GetDisplayNearestWindow(window_one
->GetNativeWindow()).id());
270 screen()->GetDisplayNearestWindow(window_two
->GetNativeWindow()).id());
272 window_one
->CloseNow();
273 window_two
->CloseNow();
276 // Tests that the window is maximized in response to a double click event.
277 TEST_F(DesktopScreenX11Test
, DoubleClickHeaderMaximizes
) {
278 if (!ui::WmSupportsHint(ui::GetAtom("_NET_WM_STATE_MAXIMIZED_VERT")))
281 Widget
* widget
= BuildTopLevelDesktopWidget(gfx::Rect(0, 0, 100, 100), true);
283 TestDesktopNativeWidgetAura
* native_widget
=
284 static_cast<TestDesktopNativeWidgetAura
*>(widget
->native_widget());
285 native_widget
->set_window_component(HTCAPTION
);
287 aura::Window
* window
= widget
->GetNativeWindow();
288 window
->SetProperty(aura::client::kCanMaximizeKey
, true);
290 // Cast to superclass as DesktopWindowTreeHostX11 hide IsMaximized
291 DesktopWindowTreeHost
* rwh
=
292 DesktopWindowTreeHostX11::GetHostForXID(window
->GetHost()->
293 GetAcceleratedWidget());
295 ui::test::EventGenerator
generator(window
);
296 generator
.DoubleClickLeftButton();
297 RunPendingMessages();
298 EXPECT_TRUE(rwh
->IsMaximized());
303 // Tests that the window does not maximize in response to a double click event,
304 // if the first click was to a different target component than that of the
306 TEST_F(DesktopScreenX11Test
, DoubleClickTwoDifferentTargetsDoesntMaximizes
) {
307 Widget
* widget
= BuildTopLevelDesktopWidget(gfx::Rect(0, 0, 100, 100), true);
309 TestDesktopNativeWidgetAura
* native_widget
=
310 static_cast<TestDesktopNativeWidgetAura
*>(widget
->native_widget());
312 aura::Window
* window
= widget
->GetNativeWindow();
313 window
->SetProperty(aura::client::kCanMaximizeKey
, true);
315 // Cast to superclass as DesktopWindowTreeHostX11 hide IsMaximized
316 DesktopWindowTreeHost
* rwh
=
317 DesktopWindowTreeHostX11::GetHostForXID(window
->GetHost()->
318 GetAcceleratedWidget());
320 ui::test::EventGenerator
generator(window
);
321 native_widget
->set_window_component(HTCLIENT
);
322 generator
.ClickLeftButton();
323 native_widget
->set_window_component(HTCAPTION
);
324 generator
.set_flags(ui::EF_IS_DOUBLE_CLICK
);
325 generator
.ClickLeftButton();
326 generator
.set_flags(ui::EF_NONE
);
327 RunPendingMessages();
328 EXPECT_FALSE(rwh
->IsMaximized());
333 // Tests that the window does not maximize in response to a double click event,
334 // if the double click was interrupted by a right click.
335 TEST_F(DesktopScreenX11Test
, RightClickDuringDoubleClickDoesntMaximize
) {
336 Widget
* widget
= BuildTopLevelDesktopWidget(gfx::Rect(0, 0, 100, 100), true);
338 TestDesktopNativeWidgetAura
* native_widget
=
339 static_cast<TestDesktopNativeWidgetAura
*>(widget
->native_widget());
341 aura::Window
* window
= widget
->GetNativeWindow();
342 window
->SetProperty(aura::client::kCanMaximizeKey
, true);
344 // Cast to superclass as DesktopWindowTreeHostX11 hide IsMaximized
345 DesktopWindowTreeHost
* rwh
= static_cast<DesktopWindowTreeHost
*>(
346 DesktopWindowTreeHostX11::GetHostForXID(window
->GetHost()->
347 GetAcceleratedWidget()));
349 ui::test::EventGenerator
generator(window
);
350 native_widget
->set_window_component(HTCLIENT
);
351 generator
.ClickLeftButton();
352 native_widget
->set_window_component(HTCAPTION
);
353 generator
.PressRightButton();
354 generator
.ReleaseRightButton();
355 EXPECT_FALSE(rwh
->IsMaximized());
356 generator
.set_flags(ui::EF_IS_DOUBLE_CLICK
);
357 generator
.ClickLeftButton();
358 generator
.set_flags(ui::EF_NONE
);
359 RunPendingMessages();
360 EXPECT_FALSE(rwh
->IsMaximized());
365 // Test that rotating the displays notifies the DisplayObservers.
366 TEST_F(DesktopScreenX11Test
, RotationChange
) {
367 std::vector
<gfx::Display
> displays
;
368 displays
.push_back(gfx::Display(kFirstDisplay
, gfx::Rect(0, 0, 640, 480)));
370 gfx::Display(kSecondDisplay
, gfx::Rect(640, 0, 1024, 768)));
371 NotifyDisplaysChanged(displays
);
372 ResetDisplayChanges();
374 displays
[0].set_rotation(gfx::Display::ROTATE_90
);
375 NotifyDisplaysChanged(displays
);
376 EXPECT_EQ(1u, changed_display_
.size());
378 displays
[1].set_rotation(gfx::Display::ROTATE_90
);
379 NotifyDisplaysChanged(displays
);
380 EXPECT_EQ(2u, changed_display_
.size());
382 displays
[0].set_rotation(gfx::Display::ROTATE_270
);
383 NotifyDisplaysChanged(displays
);
384 EXPECT_EQ(3u, changed_display_
.size());
386 displays
[0].set_rotation(gfx::Display::ROTATE_270
);
387 NotifyDisplaysChanged(displays
);
388 EXPECT_EQ(3u, changed_display_
.size());
390 displays
[0].set_rotation(gfx::Display::ROTATE_0
);
391 displays
[1].set_rotation(gfx::Display::ROTATE_0
);
392 NotifyDisplaysChanged(displays
);
393 EXPECT_EQ(5u, changed_display_
.size());
396 // Test that changing the displays workarea notifies the DisplayObservers.
397 TEST_F(DesktopScreenX11Test
, WorkareaChange
) {
398 std::vector
<gfx::Display
> displays
;
399 displays
.push_back(gfx::Display(kFirstDisplay
, gfx::Rect(0, 0, 640, 480)));
401 gfx::Display(kSecondDisplay
, gfx::Rect(640, 0, 1024, 768)));
402 NotifyDisplaysChanged(displays
);
403 ResetDisplayChanges();
405 displays
[0].set_work_area(gfx::Rect(0, 0, 300, 300));
406 NotifyDisplaysChanged(displays
);
407 EXPECT_EQ(1u, changed_display_
.size());
409 displays
[1].set_work_area(gfx::Rect(0, 0, 300, 300));
410 NotifyDisplaysChanged(displays
);
411 EXPECT_EQ(2u, changed_display_
.size());
413 displays
[0].set_work_area(gfx::Rect(0, 0, 300, 300));
414 NotifyDisplaysChanged(displays
);
415 EXPECT_EQ(2u, changed_display_
.size());
417 displays
[1].set_work_area(gfx::Rect(0, 0, 300, 300));
418 NotifyDisplaysChanged(displays
);
419 EXPECT_EQ(2u, changed_display_
.size());
421 displays
[0].set_work_area(gfx::Rect(0, 0, 640, 480));
422 displays
[1].set_work_area(gfx::Rect(640, 0, 1024, 768));
423 NotifyDisplaysChanged(displays
);
424 EXPECT_EQ(4u, changed_display_
.size());
427 // Test that changing the device scale factor notifies the DisplayObservers.
428 TEST_F(DesktopScreenX11Test
, DeviceScaleFactorChange
) {
429 std::vector
<gfx::Display
> displays
;
430 displays
.push_back(gfx::Display(kFirstDisplay
, gfx::Rect(0, 0, 640, 480)));
432 gfx::Display(kSecondDisplay
, gfx::Rect(640, 0, 1024, 768)));
433 NotifyDisplaysChanged(displays
);
434 ResetDisplayChanges();
436 displays
[0].set_device_scale_factor(2.5f
);
437 NotifyDisplaysChanged(displays
);
438 EXPECT_EQ(1u, changed_display_
.size());
440 displays
[1].set_device_scale_factor(2.5f
);
441 NotifyDisplaysChanged(displays
);
442 EXPECT_EQ(2u, changed_display_
.size());
444 displays
[0].set_device_scale_factor(2.5f
);
445 NotifyDisplaysChanged(displays
);
446 EXPECT_EQ(2u, changed_display_
.size());
448 displays
[1].set_device_scale_factor(2.5f
);
449 NotifyDisplaysChanged(displays
);
450 EXPECT_EQ(2u, changed_display_
.size());
452 displays
[0].set_device_scale_factor(1.f
);
453 displays
[1].set_device_scale_factor(1.f
);
454 NotifyDisplaysChanged(displays
);
455 EXPECT_EQ(4u, changed_display_
.size());