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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
// Copyright (c) 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/views/widget/desktop_aura/desktop_screen_x11.h"
#include "base/memory/scoped_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/window.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/base/hit_test.h"
#include "ui/base/x/x11_util.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/display_observer.h"
#include "ui/gfx/x/x11_types.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
#include "ui/views/widget/desktop_aura/desktop_window_tree_host_x11.h"
namespace {
// Class which allows for the designation of non-client component targets of
// hit tests.
class TestDesktopNativeWidgetAura : public views::DesktopNativeWidgetAura {
public:
explicit TestDesktopNativeWidgetAura(
views::internal::NativeWidgetDelegate* delegate)
: views::DesktopNativeWidgetAura(delegate) {}
virtual ~TestDesktopNativeWidgetAura() {}
void set_window_component(int window_component) {
window_component_ = window_component;
}
// DesktopNativeWidgetAura:
virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
return window_component_;
}
private:
int window_component_;
DISALLOW_COPY_AND_ASSIGN(TestDesktopNativeWidgetAura);
};
} // namespace
namespace views {
const int64 kFirstDisplay = 5321829;
const int64 kSecondDisplay = 928310;
class DesktopScreenX11Test : public views::ViewsTestBase,
public gfx::DisplayObserver {
public:
DesktopScreenX11Test() {}
virtual ~DesktopScreenX11Test() {}
// Overridden from testing::Test:
virtual void SetUp() OVERRIDE {
ViewsTestBase::SetUp();
// Initialize the world to the single monitor case.
std::vector<gfx::Display> displays;
displays.push_back(gfx::Display(kFirstDisplay, gfx::Rect(0, 0, 640, 480)));
screen_.reset(new DesktopScreenX11(displays));
screen_->AddObserver(this);
}
virtual void TearDown() OVERRIDE {
screen_.reset();
ViewsTestBase::TearDown();
}
protected:
std::vector<gfx::Display> changed_display_;
std::vector<gfx::Display> added_display_;
std::vector<gfx::Display> removed_display_;
DesktopScreenX11* screen() { return screen_.get(); }
void NotifyDisplaysChanged(const std::vector<gfx::Display>& displays) {
DesktopScreenX11* screen = screen_.get();
screen->change_notifier_.NotifyDisplaysChanged(screen->displays_, displays);
screen->displays_ = displays;
}
void ResetDisplayChanges() {
changed_display_.clear();
added_display_.clear();
removed_display_.clear();
}
Widget* BuildTopLevelDesktopWidget(const gfx::Rect& bounds,
bool use_test_native_widget) {
Widget* toplevel = new Widget;
Widget::InitParams toplevel_params =
CreateParams(Widget::InitParams::TYPE_WINDOW);
if (use_test_native_widget) {
toplevel_params.native_widget =
new TestDesktopNativeWidgetAura(toplevel);
} else {
toplevel_params.native_widget =
new views::DesktopNativeWidgetAura(toplevel);
}
toplevel_params.bounds = bounds;
toplevel_params.remove_standard_frame = true;
toplevel->Init(toplevel_params);
return toplevel;
}
private:
// Overridden from gfx::DisplayObserver:
virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE {
added_display_.push_back(new_display);
}
virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE {
removed_display_.push_back(old_display);
}
virtual void OnDisplayMetricsChanged(const gfx::Display& display,
uint32_t metrics) OVERRIDE {
changed_display_.push_back(display);
}
scoped_ptr<DesktopScreenX11> screen_;
DISALLOW_COPY_AND_ASSIGN(DesktopScreenX11Test);
};
TEST_F(DesktopScreenX11Test, BoundsChangeSingleMonitor) {
std::vector<gfx::Display> displays;
displays.push_back(gfx::Display(kFirstDisplay, gfx::Rect(0, 0, 1024, 768)));
NotifyDisplaysChanged(displays);
EXPECT_EQ(1u, changed_display_.size());
EXPECT_EQ(0u, added_display_.size());
EXPECT_EQ(0u, removed_display_.size());
}
TEST_F(DesktopScreenX11Test, AddMonitorToTheRight) {
std::vector<gfx::Display> displays;
displays.push_back(gfx::Display(kFirstDisplay, gfx::Rect(0, 0, 640, 480)));
displays.push_back(gfx::Display(kSecondDisplay,
gfx::Rect(640, 0, 1024, 768)));
NotifyDisplaysChanged(displays);
EXPECT_EQ(0u, changed_display_.size());
EXPECT_EQ(1u, added_display_.size());
EXPECT_EQ(0u, removed_display_.size());
}
TEST_F(DesktopScreenX11Test, AddMonitorToTheLeft) {
std::vector<gfx::Display> displays;
displays.push_back(gfx::Display(kSecondDisplay, gfx::Rect(0, 0, 1024, 768)));
displays.push_back(gfx::Display(kFirstDisplay, gfx::Rect(1024, 0, 640, 480)));
NotifyDisplaysChanged(displays);
EXPECT_EQ(1u, changed_display_.size());
EXPECT_EQ(1u, added_display_.size());
EXPECT_EQ(0u, removed_display_.size());
}
TEST_F(DesktopScreenX11Test, RemoveMonitorOnRight) {
std::vector<gfx::Display> displays;
displays.push_back(gfx::Display(kFirstDisplay, gfx::Rect(0, 0, 640, 480)));
displays.push_back(gfx::Display(kSecondDisplay,
gfx::Rect(640, 0, 1024, 768)));
NotifyDisplaysChanged(displays);
ResetDisplayChanges();
displays.clear();
displays.push_back(gfx::Display(kFirstDisplay, gfx::Rect(0, 0, 640, 480)));
NotifyDisplaysChanged(displays);
EXPECT_EQ(0u, changed_display_.size());
EXPECT_EQ(0u, added_display_.size());
EXPECT_EQ(1u, removed_display_.size());
}
TEST_F(DesktopScreenX11Test, RemoveMonitorOnLeft) {
std::vector<gfx::Display> displays;
displays.push_back(gfx::Display(kFirstDisplay, gfx::Rect(0, 0, 640, 480)));
displays.push_back(gfx::Display(kSecondDisplay,
gfx::Rect(640, 0, 1024, 768)));
NotifyDisplaysChanged(displays);
ResetDisplayChanges();
displays.clear();
displays.push_back(gfx::Display(kSecondDisplay, gfx::Rect(0, 0, 1024, 768)));
NotifyDisplaysChanged(displays);
EXPECT_EQ(1u, changed_display_.size());
EXPECT_EQ(0u, added_display_.size());
EXPECT_EQ(1u, removed_display_.size());
}
TEST_F(DesktopScreenX11Test, GetDisplayNearestPoint) {
std::vector<gfx::Display> displays;
displays.push_back(gfx::Display(kFirstDisplay, gfx::Rect(0, 0, 640, 480)));
displays.push_back(gfx::Display(kSecondDisplay,
gfx::Rect(640, 0, 1024, 768)));
NotifyDisplaysChanged(displays);
EXPECT_EQ(kSecondDisplay,
screen()->GetDisplayNearestPoint(gfx::Point(650, 10)).id());
EXPECT_EQ(kFirstDisplay,
screen()->GetDisplayNearestPoint(gfx::Point(10, 10)).id());
EXPECT_EQ(kFirstDisplay,
screen()->GetDisplayNearestPoint(gfx::Point(10000, 10000)).id());
}
TEST_F(DesktopScreenX11Test, GetDisplayMatchingBasic) {
std::vector<gfx::Display> displays;
displays.push_back(gfx::Display(kFirstDisplay, gfx::Rect(0, 0, 640, 480)));
displays.push_back(gfx::Display(kSecondDisplay,
gfx::Rect(640, 0, 1024, 768)));
NotifyDisplaysChanged(displays);
EXPECT_EQ(kSecondDisplay,
screen()->GetDisplayMatching(gfx::Rect(700, 20, 100, 100)).id());
}
TEST_F(DesktopScreenX11Test, GetDisplayMatchingOverlap) {
std::vector<gfx::Display> displays;
displays.push_back(gfx::Display(kFirstDisplay, gfx::Rect(0, 0, 640, 480)));
displays.push_back(gfx::Display(kSecondDisplay,
gfx::Rect(640, 0, 1024, 768)));
NotifyDisplaysChanged(displays);
EXPECT_EQ(kSecondDisplay,
screen()->GetDisplayMatching(gfx::Rect(630, 20, 100, 100)).id());
}
TEST_F(DesktopScreenX11Test, GetPrimaryDisplay) {
std::vector<gfx::Display> displays;
displays.push_back(gfx::Display(kFirstDisplay,
gfx::Rect(640, 0, 1024, 768)));
displays.push_back(gfx::Display(kSecondDisplay, gfx::Rect(0, 0, 640, 480)));
NotifyDisplaysChanged(displays);
// The first display in the list is always the primary, even if other
// displays are to the left in screen layout.
EXPECT_EQ(kFirstDisplay, screen()->GetPrimaryDisplay().id());
}
TEST_F(DesktopScreenX11Test, GetDisplayNearestWindow) {
// Set up a two monitor situation.
std::vector<gfx::Display> displays;
displays.push_back(gfx::Display(kFirstDisplay, gfx::Rect(0, 0, 640, 480)));
displays.push_back(gfx::Display(kSecondDisplay,
gfx::Rect(640, 0, 1024, 768)));
NotifyDisplaysChanged(displays);
Widget* window_one = BuildTopLevelDesktopWidget(gfx::Rect(10, 10, 10, 10),
false);
Widget* window_two = BuildTopLevelDesktopWidget(gfx::Rect(650, 50, 10, 10),
false);
EXPECT_EQ(
kFirstDisplay,
screen()->GetDisplayNearestWindow(window_one->GetNativeWindow()).id());
EXPECT_EQ(
kSecondDisplay,
screen()->GetDisplayNearestWindow(window_two->GetNativeWindow()).id());
window_one->CloseNow();
window_two->CloseNow();
}
// Tests that the window is maximized in response to a double click event.
TEST_F(DesktopScreenX11Test, DoubleClickHeaderMaximizes) {
if (!ui::WmSupportsHint(ui::GetAtom("_NET_WM_STATE_MAXIMIZED_VERT")))
return;
Widget* widget = BuildTopLevelDesktopWidget(gfx::Rect(0, 0, 100, 100), true);
widget->Show();
TestDesktopNativeWidgetAura* native_widget =
static_cast<TestDesktopNativeWidgetAura*>(widget->native_widget());
native_widget->set_window_component(HTCAPTION);
aura::Window* window = widget->GetNativeWindow();
window->SetProperty(aura::client::kCanMaximizeKey, true);
// Cast to superclass as DesktopWindowTreeHostX11 hide IsMaximized
DesktopWindowTreeHost* rwh =
DesktopWindowTreeHostX11::GetHostForXID(window->GetHost()->
GetAcceleratedWidget());
ui::test::EventGenerator generator(window);
generator.ClickLeftButton();
generator.DoubleClickLeftButton();
RunPendingMessages();
EXPECT_TRUE(rwh->IsMaximized());
widget->CloseNow();
}
// Tests that the window does not maximize in response to a double click event,
// if the first click was to a different target component than that of the
// second click.
TEST_F(DesktopScreenX11Test, DoubleClickTwoDifferentTargetsDoesntMaximizes) {
Widget* widget = BuildTopLevelDesktopWidget(gfx::Rect(0, 0, 100, 100), true);
widget->Show();
TestDesktopNativeWidgetAura* native_widget =
static_cast<TestDesktopNativeWidgetAura*>(widget->native_widget());
aura::Window* window = widget->GetNativeWindow();
window->SetProperty(aura::client::kCanMaximizeKey, true);
// Cast to superclass as DesktopWindowTreeHostX11 hide IsMaximized
DesktopWindowTreeHost* rwh =
DesktopWindowTreeHostX11::GetHostForXID(window->GetHost()->
GetAcceleratedWidget());
ui::test::EventGenerator generator(window);
native_widget->set_window_component(HTCLIENT);
generator.ClickLeftButton();
native_widget->set_window_component(HTCAPTION);
generator.DoubleClickLeftButton();
RunPendingMessages();
EXPECT_FALSE(rwh->IsMaximized());
widget->CloseNow();
}
// Tests that the window does not maximize in response to a double click event,
// if the double click was interrupted by a right click.
TEST_F(DesktopScreenX11Test, RightClickDuringDoubleClickDoesntMaximize) {
Widget* widget = BuildTopLevelDesktopWidget(gfx::Rect(0, 0, 100, 100), true);
widget->Show();
TestDesktopNativeWidgetAura* native_widget =
static_cast<TestDesktopNativeWidgetAura*>(widget->native_widget());
aura::Window* window = widget->GetNativeWindow();
window->SetProperty(aura::client::kCanMaximizeKey, true);
// Cast to superclass as DesktopWindowTreeHostX11 hide IsMaximized
DesktopWindowTreeHost* rwh = static_cast<DesktopWindowTreeHost*>(
DesktopWindowTreeHostX11::GetHostForXID(window->GetHost()->
GetAcceleratedWidget()));
ui::test::EventGenerator generator(window);
native_widget->set_window_component(HTCLIENT);
generator.ClickLeftButton();
native_widget->set_window_component(HTCAPTION);
generator.PressRightButton();
generator.ReleaseRightButton();
EXPECT_FALSE(rwh->IsMaximized());
generator.DoubleClickLeftButton();
RunPendingMessages();
EXPECT_FALSE(rwh->IsMaximized());
widget->CloseNow();
}
// Test that rotating the displays notifies the DisplayObservers.
TEST_F(DesktopScreenX11Test, RotationChange) {
std::vector<gfx::Display> displays;
displays.push_back(gfx::Display(kFirstDisplay, gfx::Rect(0, 0, 640, 480)));
displays.push_back(
gfx::Display(kSecondDisplay, gfx::Rect(640, 0, 1024, 768)));
NotifyDisplaysChanged(displays);
ResetDisplayChanges();
displays[0].set_rotation(gfx::Display::ROTATE_90);
NotifyDisplaysChanged(displays);
EXPECT_EQ(1u, changed_display_.size());
displays[1].set_rotation(gfx::Display::ROTATE_90);
NotifyDisplaysChanged(displays);
EXPECT_EQ(2u, changed_display_.size());
displays[0].set_rotation(gfx::Display::ROTATE_270);
NotifyDisplaysChanged(displays);
EXPECT_EQ(3u, changed_display_.size());
displays[0].set_rotation(gfx::Display::ROTATE_270);
NotifyDisplaysChanged(displays);
EXPECT_EQ(3u, changed_display_.size());
displays[0].set_rotation(gfx::Display::ROTATE_0);
displays[1].set_rotation(gfx::Display::ROTATE_0);
NotifyDisplaysChanged(displays);
EXPECT_EQ(5u, changed_display_.size());
}
// Test that changing the displays workarea notifies the DisplayObservers.
TEST_F(DesktopScreenX11Test, WorkareaChange) {
std::vector<gfx::Display> displays;
displays.push_back(gfx::Display(kFirstDisplay, gfx::Rect(0, 0, 640, 480)));
displays.push_back(
gfx::Display(kSecondDisplay, gfx::Rect(640, 0, 1024, 768)));
NotifyDisplaysChanged(displays);
ResetDisplayChanges();
displays[0].set_work_area(gfx::Rect(0, 0, 300, 300));
NotifyDisplaysChanged(displays);
EXPECT_EQ(1u, changed_display_.size());
displays[1].set_work_area(gfx::Rect(0, 0, 300, 300));
NotifyDisplaysChanged(displays);
EXPECT_EQ(2u, changed_display_.size());
displays[0].set_work_area(gfx::Rect(0, 0, 300, 300));
NotifyDisplaysChanged(displays);
EXPECT_EQ(2u, changed_display_.size());
displays[1].set_work_area(gfx::Rect(0, 0, 300, 300));
NotifyDisplaysChanged(displays);
EXPECT_EQ(2u, changed_display_.size());
displays[0].set_work_area(gfx::Rect(0, 0, 640, 480));
displays[1].set_work_area(gfx::Rect(640, 0, 1024, 768));
NotifyDisplaysChanged(displays);
EXPECT_EQ(4u, changed_display_.size());
}
// Test that changing the device scale factor notifies the DisplayObservers.
TEST_F(DesktopScreenX11Test, DeviceScaleFactorChange) {
std::vector<gfx::Display> displays;
displays.push_back(gfx::Display(kFirstDisplay, gfx::Rect(0, 0, 640, 480)));
displays.push_back(
gfx::Display(kSecondDisplay, gfx::Rect(640, 0, 1024, 768)));
NotifyDisplaysChanged(displays);
ResetDisplayChanges();
displays[0].set_device_scale_factor(2.5f);
NotifyDisplaysChanged(displays);
EXPECT_EQ(1u, changed_display_.size());
displays[1].set_device_scale_factor(2.5f);
NotifyDisplaysChanged(displays);
EXPECT_EQ(2u, changed_display_.size());
displays[0].set_device_scale_factor(2.5f);
NotifyDisplaysChanged(displays);
EXPECT_EQ(2u, changed_display_.size());
displays[1].set_device_scale_factor(2.5f);
NotifyDisplaysChanged(displays);
EXPECT_EQ(2u, changed_display_.size());
displays[0].set_device_scale_factor(1.f);
displays[1].set_device_scale_factor(1.f);
NotifyDisplaysChanged(displays);
EXPECT_EQ(4u, changed_display_.size());
}
} // namespace views
|