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(kSecondDisplay
,
209 screen()->GetDisplayNearestPoint(gfx::Point(650, 10)).id());
210 EXPECT_EQ(kFirstDisplay
,
211 screen()->GetDisplayNearestPoint(gfx::Point(10, 10)).id());
212 EXPECT_EQ(kFirstDisplay
,
213 screen()->GetDisplayNearestPoint(gfx::Point(10000, 10000)).id());
216 TEST_F(DesktopScreenX11Test
, GetDisplayMatchingBasic
) {
217 std::vector
<gfx::Display
> displays
;
218 displays
.push_back(gfx::Display(kFirstDisplay
, gfx::Rect(0, 0, 640, 480)));
219 displays
.push_back(gfx::Display(kSecondDisplay
,
220 gfx::Rect(640, 0, 1024, 768)));
221 NotifyDisplaysChanged(displays
);
223 EXPECT_EQ(kSecondDisplay
,
224 screen()->GetDisplayMatching(gfx::Rect(700, 20, 100, 100)).id());
227 TEST_F(DesktopScreenX11Test
, GetDisplayMatchingOverlap
) {
228 std::vector
<gfx::Display
> displays
;
229 displays
.push_back(gfx::Display(kFirstDisplay
, gfx::Rect(0, 0, 640, 480)));
230 displays
.push_back(gfx::Display(kSecondDisplay
,
231 gfx::Rect(640, 0, 1024, 768)));
232 NotifyDisplaysChanged(displays
);
234 EXPECT_EQ(kSecondDisplay
,
235 screen()->GetDisplayMatching(gfx::Rect(630, 20, 100, 100)).id());
238 TEST_F(DesktopScreenX11Test
, GetPrimaryDisplay
) {
239 std::vector
<gfx::Display
> displays
;
240 displays
.push_back(gfx::Display(kFirstDisplay
,
241 gfx::Rect(640, 0, 1024, 768)));
242 displays
.push_back(gfx::Display(kSecondDisplay
, gfx::Rect(0, 0, 640, 480)));
243 NotifyDisplaysChanged(displays
);
245 // The first display in the list is always the primary, even if other
246 // displays are to the left in screen layout.
247 EXPECT_EQ(kFirstDisplay
, screen()->GetPrimaryDisplay().id());
250 TEST_F(DesktopScreenX11Test
, GetDisplayNearestWindow
) {
251 // Set up a two monitor situation.
252 std::vector
<gfx::Display
> displays
;
253 displays
.push_back(gfx::Display(kFirstDisplay
, gfx::Rect(0, 0, 640, 480)));
254 displays
.push_back(gfx::Display(kSecondDisplay
,
255 gfx::Rect(640, 0, 1024, 768)));
256 NotifyDisplaysChanged(displays
);
258 Widget
* window_one
= BuildTopLevelDesktopWidget(gfx::Rect(10, 10, 10, 10),
260 Widget
* window_two
= BuildTopLevelDesktopWidget(gfx::Rect(650, 50, 10, 10),
265 screen()->GetDisplayNearestWindow(window_one
->GetNativeWindow()).id());
268 screen()->GetDisplayNearestWindow(window_two
->GetNativeWindow()).id());
270 window_one
->CloseNow();
271 window_two
->CloseNow();
274 // Tests that the window is maximized in response to a double click event.
275 TEST_F(DesktopScreenX11Test
, DoubleClickHeaderMaximizes
) {
276 if (!ui::WmSupportsHint(ui::GetAtom("_NET_WM_STATE_MAXIMIZED_VERT")))
279 Widget
* widget
= BuildTopLevelDesktopWidget(gfx::Rect(0, 0, 100, 100), true);
281 TestDesktopNativeWidgetAura
* native_widget
=
282 static_cast<TestDesktopNativeWidgetAura
*>(widget
->native_widget());
283 native_widget
->set_window_component(HTCAPTION
);
285 aura::Window
* window
= widget
->GetNativeWindow();
286 window
->SetProperty(aura::client::kCanMaximizeKey
, true);
288 // Cast to superclass as DesktopWindowTreeHostX11 hide IsMaximized
289 DesktopWindowTreeHost
* rwh
=
290 DesktopWindowTreeHostX11::GetHostForXID(window
->GetHost()->
291 GetAcceleratedWidget());
293 ui::test::EventGenerator
generator(window
);
294 generator
.DoubleClickLeftButton();
295 RunPendingMessages();
296 EXPECT_TRUE(rwh
->IsMaximized());
301 // Tests that the window does not maximize in response to a double click event,
302 // if the first click was to a different target component than that of the
304 TEST_F(DesktopScreenX11Test
, DoubleClickTwoDifferentTargetsDoesntMaximizes
) {
305 Widget
* widget
= BuildTopLevelDesktopWidget(gfx::Rect(0, 0, 100, 100), true);
307 TestDesktopNativeWidgetAura
* native_widget
=
308 static_cast<TestDesktopNativeWidgetAura
*>(widget
->native_widget());
310 aura::Window
* window
= widget
->GetNativeWindow();
311 window
->SetProperty(aura::client::kCanMaximizeKey
, true);
313 // Cast to superclass as DesktopWindowTreeHostX11 hide IsMaximized
314 DesktopWindowTreeHost
* rwh
=
315 DesktopWindowTreeHostX11::GetHostForXID(window
->GetHost()->
316 GetAcceleratedWidget());
318 ui::test::EventGenerator
generator(window
);
319 native_widget
->set_window_component(HTCLIENT
);
320 generator
.ClickLeftButton();
321 native_widget
->set_window_component(HTCAPTION
);
322 generator
.set_flags(ui::EF_IS_DOUBLE_CLICK
);
323 generator
.ClickLeftButton();
324 generator
.set_flags(ui::EF_NONE
);
325 RunPendingMessages();
326 EXPECT_FALSE(rwh
->IsMaximized());
331 // Tests that the window does not maximize in response to a double click event,
332 // if the double click was interrupted by a right click.
333 TEST_F(DesktopScreenX11Test
, RightClickDuringDoubleClickDoesntMaximize
) {
334 Widget
* widget
= BuildTopLevelDesktopWidget(gfx::Rect(0, 0, 100, 100), true);
336 TestDesktopNativeWidgetAura
* native_widget
=
337 static_cast<TestDesktopNativeWidgetAura
*>(widget
->native_widget());
339 aura::Window
* window
= widget
->GetNativeWindow();
340 window
->SetProperty(aura::client::kCanMaximizeKey
, true);
342 // Cast to superclass as DesktopWindowTreeHostX11 hide IsMaximized
343 DesktopWindowTreeHost
* rwh
= static_cast<DesktopWindowTreeHost
*>(
344 DesktopWindowTreeHostX11::GetHostForXID(window
->GetHost()->
345 GetAcceleratedWidget()));
347 ui::test::EventGenerator
generator(window
);
348 native_widget
->set_window_component(HTCLIENT
);
349 generator
.ClickLeftButton();
350 native_widget
->set_window_component(HTCAPTION
);
351 generator
.PressRightButton();
352 generator
.ReleaseRightButton();
353 EXPECT_FALSE(rwh
->IsMaximized());
354 generator
.set_flags(ui::EF_IS_DOUBLE_CLICK
);
355 generator
.ClickLeftButton();
356 generator
.set_flags(ui::EF_NONE
);
357 RunPendingMessages();
358 EXPECT_FALSE(rwh
->IsMaximized());
363 // Test that rotating the displays notifies the DisplayObservers.
364 TEST_F(DesktopScreenX11Test
, RotationChange
) {
365 std::vector
<gfx::Display
> displays
;
366 displays
.push_back(gfx::Display(kFirstDisplay
, gfx::Rect(0, 0, 640, 480)));
368 gfx::Display(kSecondDisplay
, gfx::Rect(640, 0, 1024, 768)));
369 NotifyDisplaysChanged(displays
);
370 ResetDisplayChanges();
372 displays
[0].set_rotation(gfx::Display::ROTATE_90
);
373 NotifyDisplaysChanged(displays
);
374 EXPECT_EQ(1u, changed_display_
.size());
376 displays
[1].set_rotation(gfx::Display::ROTATE_90
);
377 NotifyDisplaysChanged(displays
);
378 EXPECT_EQ(2u, changed_display_
.size());
380 displays
[0].set_rotation(gfx::Display::ROTATE_270
);
381 NotifyDisplaysChanged(displays
);
382 EXPECT_EQ(3u, changed_display_
.size());
384 displays
[0].set_rotation(gfx::Display::ROTATE_270
);
385 NotifyDisplaysChanged(displays
);
386 EXPECT_EQ(3u, changed_display_
.size());
388 displays
[0].set_rotation(gfx::Display::ROTATE_0
);
389 displays
[1].set_rotation(gfx::Display::ROTATE_0
);
390 NotifyDisplaysChanged(displays
);
391 EXPECT_EQ(5u, changed_display_
.size());
394 // Test that changing the displays workarea notifies the DisplayObservers.
395 TEST_F(DesktopScreenX11Test
, WorkareaChange
) {
396 std::vector
<gfx::Display
> displays
;
397 displays
.push_back(gfx::Display(kFirstDisplay
, gfx::Rect(0, 0, 640, 480)));
399 gfx::Display(kSecondDisplay
, gfx::Rect(640, 0, 1024, 768)));
400 NotifyDisplaysChanged(displays
);
401 ResetDisplayChanges();
403 displays
[0].set_work_area(gfx::Rect(0, 0, 300, 300));
404 NotifyDisplaysChanged(displays
);
405 EXPECT_EQ(1u, changed_display_
.size());
407 displays
[1].set_work_area(gfx::Rect(0, 0, 300, 300));
408 NotifyDisplaysChanged(displays
);
409 EXPECT_EQ(2u, changed_display_
.size());
411 displays
[0].set_work_area(gfx::Rect(0, 0, 300, 300));
412 NotifyDisplaysChanged(displays
);
413 EXPECT_EQ(2u, changed_display_
.size());
415 displays
[1].set_work_area(gfx::Rect(0, 0, 300, 300));
416 NotifyDisplaysChanged(displays
);
417 EXPECT_EQ(2u, changed_display_
.size());
419 displays
[0].set_work_area(gfx::Rect(0, 0, 640, 480));
420 displays
[1].set_work_area(gfx::Rect(640, 0, 1024, 768));
421 NotifyDisplaysChanged(displays
);
422 EXPECT_EQ(4u, changed_display_
.size());
425 // Test that changing the device scale factor notifies the DisplayObservers.
426 TEST_F(DesktopScreenX11Test
, DeviceScaleFactorChange
) {
427 std::vector
<gfx::Display
> displays
;
428 displays
.push_back(gfx::Display(kFirstDisplay
, gfx::Rect(0, 0, 640, 480)));
430 gfx::Display(kSecondDisplay
, gfx::Rect(640, 0, 1024, 768)));
431 NotifyDisplaysChanged(displays
);
432 ResetDisplayChanges();
434 displays
[0].set_device_scale_factor(2.5f
);
435 NotifyDisplaysChanged(displays
);
436 EXPECT_EQ(1u, changed_display_
.size());
438 displays
[1].set_device_scale_factor(2.5f
);
439 NotifyDisplaysChanged(displays
);
440 EXPECT_EQ(2u, changed_display_
.size());
442 displays
[0].set_device_scale_factor(2.5f
);
443 NotifyDisplaysChanged(displays
);
444 EXPECT_EQ(2u, changed_display_
.size());
446 displays
[1].set_device_scale_factor(2.5f
);
447 NotifyDisplaysChanged(displays
);
448 EXPECT_EQ(2u, changed_display_
.size());
450 displays
[0].set_device_scale_factor(1.f
);
451 displays
[1].set_device_scale_factor(1.f
);
452 NotifyDisplaysChanged(displays
);
453 EXPECT_EQ(4u, changed_display_
.size());