Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: ui/gfx/screen_win_unittest.cc

Issue 1825723002: Move ScreenWin to ui/display (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added is_chromeos guard Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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.
4
5 #include "ui/gfx/screen_win.h"
6
7 #include <windows.h>
8 #include <inttypes.h>
9 #include <stddef.h>
10
11 #include <cwchar>
12 #include <memory>
13 #include <string>
14 #include <unordered_map>
15 #include <vector>
16
17 #include "base/macros.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "ui/gfx/display.h"
20 #include "ui/gfx/geometry/rect.h"
21 #include "ui/gfx/screen.h"
22 #include "ui/gfx/test/display_util.h"
23 #include "ui/gfx/win/display_info.h"
24 #include "ui/gfx/win/dpi.h"
25 #include "ui/gfx/win/screen_win_display.h"
26
27 namespace gfx {
28
29 namespace {
30
31 MONITORINFOEX CreateMonitorInfo(gfx::Rect monitor,
32 gfx::Rect work,
33 std::wstring device_name) {
34 MONITORINFOEX monitor_info;
35 ::ZeroMemory(&monitor_info, sizeof(monitor_info));
36 monitor_info.cbSize = sizeof(monitor_info);
37 monitor_info.rcMonitor = monitor.ToRECT();
38 monitor_info.rcWork = work.ToRECT();
39 size_t device_char_count = ARRAYSIZE(monitor_info.szDevice);
40 wcsncpy(monitor_info.szDevice, device_name.c_str(), device_char_count);
41 monitor_info.szDevice[device_char_count-1] = L'\0';
42 return monitor_info;
43 }
44
45 class TestScreenWin : public gfx::ScreenWin {
46 public:
47 TestScreenWin(const std::vector<gfx::win::DisplayInfo>& display_infos,
48 const std::vector<MONITORINFOEX>& monitor_infos,
49 const std::unordered_map<HWND, gfx::Rect>& hwnd_map)
50 : monitor_infos_(monitor_infos),
51 hwnd_map_(hwnd_map) {
52 UpdateFromDisplayInfos(display_infos);
53 }
54
55 ~TestScreenWin() override = default;
56
57 protected:
58 // gfx::ScreenWin:
59 HWND GetHWNDFromNativeView(NativeView window) const override {
60 // NativeView is only used as an identifier in this tests, so interchange
61 // NativeView with an HWND for convenience.
62 return reinterpret_cast<HWND>(window);
63 }
64
65 NativeWindow GetNativeWindowFromHWND(HWND hwnd) const override {
66 // NativeWindow is only used as an identifier in this tests, so interchange
67 // an HWND for a NativeWindow for convenience.
68 return reinterpret_cast<NativeWindow>(hwnd);
69 }
70
71 private:
72 void Initialize() override {}
73
74 MONITORINFOEX MonitorInfoFromScreenPoint(const gfx::Point& screen_point) const
75 override {
76 for (const MONITORINFOEX& monitor_info : monitor_infos_) {
77 if (gfx::Rect(monitor_info.rcMonitor).Contains(screen_point))
78 return monitor_info;
79 }
80 NOTREACHED();
81 return monitor_infos_[0];
82 }
83
84 MONITORINFOEX MonitorInfoFromScreenRect(const gfx::Rect& screen_rect) const
85 override {
86 MONITORINFOEX candidate = monitor_infos_[0];
87 int largest_area = 0;
88 for (const MONITORINFOEX& monitor_info : monitor_infos_) {
89 gfx::Rect bounds(monitor_info.rcMonitor);
90 if (bounds.Intersects(screen_rect)) {
91 bounds.Intersect(screen_rect);
92 int area = bounds.height() * bounds.width();
93 if (largest_area < area) {
94 candidate = monitor_info;
95 largest_area = area;
96 }
97 }
98 }
99 EXPECT_NE(largest_area, 0);
100 return candidate;
101 }
102
103 MONITORINFOEX MonitorInfoFromWindow(HWND hwnd, DWORD default_options)
104 const override {
105 auto search = hwnd_map_.find(hwnd);
106 if (search != hwnd_map_.end())
107 return MonitorInfoFromScreenRect(search->second);
108
109 EXPECT_EQ(default_options, static_cast<DWORD>(MONITOR_DEFAULTTOPRIMARY));
110 for (const auto& monitor_info : monitor_infos_) {
111 if (monitor_info.rcMonitor.left == 0 &&
112 monitor_info.rcMonitor.top == 0) {
113 return monitor_info;
114 }
115 }
116 NOTREACHED();
117 return monitor_infos_[0];
118 }
119
120 HWND GetRootWindow(HWND hwnd) const override {
121 return hwnd;
122 }
123
124 std::vector<MONITORINFOEX> monitor_infos_;
125 std::unordered_map<HWND, gfx::Rect> hwnd_map_;
126
127 DISALLOW_COPY_AND_ASSIGN(TestScreenWin);
128 };
129
130 Screen* GetScreen() {
131 return gfx::Screen::GetScreen();
132 }
133
134 } // namespace
135
136 // Allows tests to specify the screen and associated state.
137 class TestScreenWinInitializer {
138 public:
139 virtual void AddMonitor(const gfx::Rect& pixel_bounds,
140 const gfx::Rect& pixel_work,
141 const wchar_t* device_name,
142 float device_scale_factor) = 0;
143
144 virtual HWND CreateFakeHwnd(const gfx::Rect& bounds) = 0;
145 };
146
147 class TestScreenWinManager : public TestScreenWinInitializer {
148 public:
149 TestScreenWinManager() = default;
150
151 ~TestScreenWinManager() {
152 gfx::Screen::SetScreenInstance(nullptr);
153 }
154
155 void AddMonitor(const gfx::Rect& pixel_bounds,
156 const gfx::Rect& pixel_work,
157 const wchar_t* device_name,
158 float device_scale_factor) override {
159 MONITORINFOEX monitor_info = CreateMonitorInfo(pixel_bounds,
160 pixel_work,
161 device_name);
162 monitor_infos_.push_back(monitor_info);
163 display_infos_.push_back(gfx::win::DisplayInfo(monitor_info,
164 device_scale_factor,
165 gfx::Display::ROTATE_0));
166 }
167
168 HWND CreateFakeHwnd(const gfx::Rect& bounds) override {
169 EXPECT_EQ(screen_win_, nullptr);
170 hwnd_map_.insert(std::pair<HWND, gfx::Rect>(++hwndLast_, bounds));
171 return hwndLast_;
172 }
173
174 void InitializeScreenWin() {
175 ASSERT_EQ(screen_win_, nullptr);
176 screen_win_.reset(new TestScreenWin(display_infos_,
177 monitor_infos_,
178 hwnd_map_));
179 gfx::Screen::SetScreenInstance(screen_win_.get());
180 }
181
182 ScreenWin* GetScreenWin() {
183 return screen_win_.get();
184 }
185
186 private:
187 HWND hwndLast_ = nullptr;
188 scoped_ptr<ScreenWin> screen_win_;
189 std::vector<MONITORINFOEX> monitor_infos_;
190 std::vector<gfx::win::DisplayInfo> display_infos_;
191 std::unordered_map<HWND, gfx::Rect> hwnd_map_;
192
193 DISALLOW_COPY_AND_ASSIGN(TestScreenWinManager);
194 };
195
196 class ScreenWinTest : public testing::Test {
197 protected:
198 ScreenWinTest() = default;
199
200 void SetUp() override {
201 testing::Test::SetUp();
202 gfx::SetDefaultDeviceScaleFactor(1.0);
203 screen_win_initializer_.reset(new TestScreenWinManager());
204 SetUpScreen(screen_win_initializer_.get());
205 screen_win_initializer_->InitializeScreenWin();
206 }
207
208 void TearDown() override {
209 screen_win_initializer_.reset();
210 gfx::SetDefaultDeviceScaleFactor(1.0);
211 testing::Test::TearDown();
212 }
213
214 virtual void SetUpScreen(TestScreenWinInitializer* initializer) = 0;
215
216 NativeWindow GetNativeWindowFromHWND(HWND hwnd) const {
217 ScreenWin* screen_win = screen_win_initializer_->GetScreenWin();
218 return screen_win->GetNativeWindowFromHWND(hwnd);;
219 }
220
221 private:
222 scoped_ptr<TestScreenWinManager> screen_win_initializer_;
223
224 DISALLOW_COPY_AND_ASSIGN(ScreenWinTest);
225 };
226
227 // Single Display of 1.0 Device Scale Factor.
228 class ScreenWinTestSingleDisplay1x : public ScreenWinTest {
229 public:
230 ScreenWinTestSingleDisplay1x() = default;
231
232 void SetUpScreen(TestScreenWinInitializer* initializer) override {
233 initializer->AddMonitor(gfx::Rect(0, 0, 1920, 1200),
234 gfx::Rect(0, 0, 1920, 1100),
235 L"primary",
236 1.0);
237 fake_hwnd_ = initializer->CreateFakeHwnd(gfx::Rect(0, 0, 1920, 1100));
238 }
239
240 HWND GetFakeHwnd() {
241 return fake_hwnd_;
242 }
243
244 private:
245 HWND fake_hwnd_ = nullptr;
246
247 DISALLOW_COPY_AND_ASSIGN(ScreenWinTestSingleDisplay1x);
248 };
249
250 TEST_F(ScreenWinTestSingleDisplay1x, GetDisplays) {
251 std::vector<gfx::Display> displays = GetScreen()->GetAllDisplays();
252 ASSERT_EQ(1u, displays.size());
253 EXPECT_EQ(gfx::Rect(0, 0, 1920, 1200), displays[0].bounds());
254 EXPECT_EQ(gfx::Rect(0, 0, 1920, 1100), displays[0].work_area());
255 }
256
257 TEST_F(ScreenWinTestSingleDisplay1x, GetNumDisplays) {
258 EXPECT_EQ(1, GetScreen()->GetNumDisplays());
259 }
260
261 TEST_F(ScreenWinTestSingleDisplay1x, GetDisplayNearestWindowPrimaryDisplay) {
262 gfx::Screen* screen = GetScreen();
263 EXPECT_EQ(screen->GetPrimaryDisplay(),
264 screen->GetDisplayNearestWindow(nullptr));
265 }
266
267 TEST_F(ScreenWinTestSingleDisplay1x, GetDisplayNearestWindow) {
268 gfx::Screen* screen = GetScreen();
269 gfx::NativeWindow native_window = GetNativeWindowFromHWND(GetFakeHwnd());
270 EXPECT_EQ(screen->GetAllDisplays()[0],
271 screen->GetDisplayNearestWindow(native_window));
272 }
273
274 TEST_F(ScreenWinTestSingleDisplay1x, GetDisplayNearestPoint) {
275 gfx::Screen* screen = GetScreen();
276 gfx::Display display = screen->GetAllDisplays()[0];
277 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(0, 0)));
278 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(250, 952)));
279 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(1919, 1199)));
280 }
281
282 TEST_F(ScreenWinTestSingleDisplay1x, GetDisplayMatching) {
283 gfx::Screen* screen = GetScreen();
284 gfx::Display display = screen->GetAllDisplays()[0];
285 EXPECT_EQ(display, screen->GetDisplayMatching(gfx::Rect(0, 0, 100, 100)));
286 EXPECT_EQ(display,
287 screen->GetDisplayMatching(gfx::Rect(1819, 1099, 100, 100)));
288 }
289
290 TEST_F(ScreenWinTestSingleDisplay1x, GetPrimaryDisplay) {
291 gfx::Screen* screen = GetScreen();
292 EXPECT_EQ(gfx::Point(0, 0), screen->GetPrimaryDisplay().bounds().origin());
293 }
294
295 // Single Display of 1.25 Device Scale Factor.
296 class ScreenWinTestSingleDisplay1_25x : public ScreenWinTest {
297 public:
298 ScreenWinTestSingleDisplay1_25x() = default;
299
300 void SetUpScreen(TestScreenWinInitializer* initializer) override {
301 gfx::SetDefaultDeviceScaleFactor(1.25);
302 // Add Monitor of Scale Factor 1.0 since gfx::GetDPIScale performs the
303 // clamping and not ScreenWin.
304 initializer->AddMonitor(gfx::Rect(0, 0, 1920, 1200),
305 gfx::Rect(0, 0, 1920, 1100),
306 L"primary",
307 1.0);
308 fake_hwnd_ = initializer->CreateFakeHwnd(gfx::Rect(0, 0, 1920, 1100));
309 }
310
311 HWND GetFakeHwnd() {
312 return fake_hwnd_;
313 }
314
315 private:
316 HWND fake_hwnd_ = nullptr;
317
318 DISALLOW_COPY_AND_ASSIGN(ScreenWinTestSingleDisplay1_25x);
319 };
320
321 TEST_F(ScreenWinTestSingleDisplay1_25x, GetDisplays) {
322 std::vector<gfx::Display> displays = GetScreen()->GetAllDisplays();
323 ASSERT_EQ(1u, displays.size());
324 // On Windows, scale factors of 1.25 or lower are clamped to 1.0.
325 EXPECT_EQ(gfx::Rect(0, 0, 1920, 1200), displays[0].bounds());
326 EXPECT_EQ(gfx::Rect(0, 0, 1920, 1100), displays[0].work_area());
327 }
328
329 TEST_F(ScreenWinTestSingleDisplay1_25x, GetDisplayNearestWindow) {
330 gfx::Screen* screen = GetScreen();
331 gfx::NativeWindow native_window = GetNativeWindowFromHWND(GetFakeHwnd());
332 EXPECT_EQ(screen->GetAllDisplays()[0],
333 screen->GetDisplayNearestWindow(native_window));
334 }
335
336 TEST_F(ScreenWinTestSingleDisplay1_25x, GetDisplayNearestPoint) {
337 gfx::Screen* screen = GetScreen();
338 gfx::Display display = screen->GetAllDisplays()[0];
339 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(0, 0)));
340 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(250, 952)));
341 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(1919, 1199)));
342 }
343
344 TEST_F(ScreenWinTestSingleDisplay1_25x, GetDisplayMatching) {
345 gfx::Screen* screen = GetScreen();
346 gfx::Display display = screen->GetAllDisplays()[0];
347 EXPECT_EQ(display, screen->GetDisplayMatching(gfx::Rect(0, 0, 100, 100)));
348 EXPECT_EQ(display,
349 screen->GetDisplayMatching(gfx::Rect(1819, 1099, 100, 100)));
350 }
351 TEST_F(ScreenWinTestSingleDisplay1_25x, GetPrimaryDisplay) {
352 gfx::Screen* screen = GetScreen();
353 EXPECT_EQ(gfx::Point(0, 0), screen->GetPrimaryDisplay().bounds().origin());
354 }
355
356 // Single Display of 1.25 Device Scale Factor.
357 class ScreenWinTestSingleDisplay1_5x : public ScreenWinTest {
358 public:
359 ScreenWinTestSingleDisplay1_5x() = default;
360
361 void SetUpScreen(TestScreenWinInitializer* initializer) override {
362 gfx::SetDefaultDeviceScaleFactor(1.5);
363 initializer->AddMonitor(gfx::Rect(0, 0, 1920, 1200),
364 gfx::Rect(0, 0, 1920, 1100),
365 L"primary",
366 1.5);
367 fake_hwnd_ = initializer->CreateFakeHwnd(gfx::Rect(0, 0, 1920, 1100));
368 }
369
370 HWND GetFakeHwnd() {
371 return fake_hwnd_;
372 }
373
374 private:
375 HWND fake_hwnd_ = nullptr;
376
377 DISALLOW_COPY_AND_ASSIGN(ScreenWinTestSingleDisplay1_5x);
378 };
379
380 TEST_F(ScreenWinTestSingleDisplay1_5x, GetDisplays) {
381 std::vector<gfx::Display> displays = GetScreen()->GetAllDisplays();
382 ASSERT_EQ(1u, displays.size());
383 EXPECT_EQ(gfx::Rect(0, 0, 1280, 800), displays[0].bounds());
384 EXPECT_EQ(gfx::Rect(0, 0, 1280, 734), displays[0].work_area());
385 }
386
387 TEST_F(ScreenWinTestSingleDisplay1_5x, GetDisplayNearestWindow) {
388 gfx::Screen* screen = GetScreen();
389 gfx::NativeWindow native_window = GetNativeWindowFromHWND(GetFakeHwnd());
390 EXPECT_EQ(screen->GetAllDisplays()[0],
391 screen->GetDisplayNearestWindow(native_window));
392 }
393
394 TEST_F(ScreenWinTestSingleDisplay1_5x, GetDisplayNearestPoint) {
395 gfx::Screen* screen = GetScreen();
396 gfx::Display display = screen->GetAllDisplays()[0];
397 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(0, 0)));
398 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(250, 524)));
399 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(1279, 733)));
400 }
401
402 TEST_F(ScreenWinTestSingleDisplay1_5x, GetDisplayMatching) {
403 gfx::Screen* screen = GetScreen();
404 gfx::Display display = screen->GetAllDisplays()[0];
405 EXPECT_EQ(display, screen->GetDisplayMatching(gfx::Rect(0, 0, 100, 100)));
406 EXPECT_EQ(display,
407 screen->GetDisplayMatching(gfx::Rect(1819, 1099, 100, 100)));
408 }
409 TEST_F(ScreenWinTestSingleDisplay1_5x, GetPrimaryDisplay) {
410 gfx::Screen* screen = GetScreen();
411 EXPECT_EQ(gfx::Point(0, 0), screen->GetPrimaryDisplay().bounds().origin());
412 }
413
414
415 // Single Display of 2.0 Device Scale Factor.
416 class ScreenWinTestSingleDisplay2x : public ScreenWinTest {
417 public:
418 ScreenWinTestSingleDisplay2x() = default;
419
420 void SetUpScreen(TestScreenWinInitializer* initializer) override {
421 gfx::SetDefaultDeviceScaleFactor(2.0);
422 initializer->AddMonitor(gfx::Rect(0, 0, 1920, 1200),
423 gfx::Rect(0, 0, 1920, 1100),
424 L"primary",
425 2.0);
426 fake_hwnd_ = initializer->CreateFakeHwnd(gfx::Rect(0, 0, 1920, 1100));
427 }
428
429 HWND GetFakeHwnd() {
430 return fake_hwnd_;
431 }
432
433 private:
434 HWND fake_hwnd_ = nullptr;
435
436 DISALLOW_COPY_AND_ASSIGN(ScreenWinTestSingleDisplay2x);
437 };
438
439 TEST_F(ScreenWinTestSingleDisplay2x, GetDisplays) {
440 std::vector<gfx::Display> displays = GetScreen()->GetAllDisplays();
441 ASSERT_EQ(1u, displays.size());
442 EXPECT_EQ(gfx::Rect(0, 0, 960, 600), displays[0].bounds());
443 EXPECT_EQ(gfx::Rect(0, 0, 960, 550), displays[0].work_area());
444 }
445
446 TEST_F(ScreenWinTestSingleDisplay2x, GetDisplayNearestWindow) {
447 gfx::Screen* screen = GetScreen();
448 gfx::NativeWindow native_window = GetNativeWindowFromHWND(GetFakeHwnd());
449 EXPECT_EQ(screen->GetAllDisplays()[0],
450 screen->GetDisplayNearestWindow(native_window));
451 }
452
453 TEST_F(ScreenWinTestSingleDisplay2x, GetDisplayNearestPoint) {
454 gfx::Screen* screen = GetScreen();
455 gfx::Display display = screen->GetAllDisplays()[0];
456 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(0, 0)));
457 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(125, 476)));
458 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(959, 599)));
459 }
460
461 TEST_F(ScreenWinTestSingleDisplay2x, GetDisplayMatching) {
462 gfx::Screen* screen = GetScreen();
463 gfx::Display display = screen->GetAllDisplays()[0];
464 EXPECT_EQ(display, screen->GetDisplayMatching(gfx::Rect(0, 0, 100, 100)));
465 EXPECT_EQ(display,
466 screen->GetDisplayMatching(gfx::Rect(1819, 1099, 100, 100)));
467 }
468
469 // Two Displays of 1.0 Device Scale Factor.
470 class ScreenWinTestTwoDisplays1x : public ScreenWinTest {
471 public:
472 ScreenWinTestTwoDisplays1x() = default;
473
474 void SetUpScreen(TestScreenWinInitializer* initializer) override {
475 initializer->AddMonitor(gfx::Rect(0, 0, 1920, 1200),
476 gfx::Rect(0, 0, 1920, 1100),
477 L"primary",
478 1.0);
479 initializer->AddMonitor(gfx::Rect(1920, 0, 800, 600),
480 gfx::Rect(1920, 0, 800, 600),
481 L"secondary",
482 1.0);
483 fake_hwnd_left_ = initializer->CreateFakeHwnd(gfx::Rect(0, 0, 1920, 1100));
484 fake_hwnd_right_ =
485 initializer->CreateFakeHwnd(gfx::Rect(1920, 0, 800, 600));
486 }
487
488 HWND GetLeftFakeHwnd() {
489 return fake_hwnd_left_;
490 }
491
492 HWND GetRightFakeHwnd() {
493 return fake_hwnd_right_;
494 }
495
496 private:
497 HWND fake_hwnd_left_ = nullptr;
498 HWND fake_hwnd_right_ = nullptr;
499
500 DISALLOW_COPY_AND_ASSIGN(ScreenWinTestTwoDisplays1x);
501 };
502
503 TEST_F(ScreenWinTestTwoDisplays1x, GetDisplays) {
504 std::vector<gfx::Display> displays = GetScreen()->GetAllDisplays();
505 ASSERT_EQ(2u, displays.size());
506 EXPECT_EQ(gfx::Rect(0, 0, 1920, 1200), displays[0].bounds());
507 EXPECT_EQ(gfx::Rect(0, 0, 1920, 1100), displays[0].work_area());
508 EXPECT_EQ(gfx::Rect(1920, 0, 800, 600), displays[1].bounds());
509 EXPECT_EQ(gfx::Rect(1920, 0, 800, 600), displays[1].work_area());
510 }
511
512 TEST_F(ScreenWinTestTwoDisplays1x, GetNumDisplays) {
513 EXPECT_EQ(2, GetScreen()->GetNumDisplays());
514 }
515
516 TEST_F(ScreenWinTestTwoDisplays1x, GetDisplayNearestWindowPrimaryDisplay) {
517 gfx::Screen* screen = GetScreen();
518 EXPECT_EQ(screen->GetPrimaryDisplay(),
519 screen->GetDisplayNearestWindow(nullptr));
520 }
521
522 TEST_F(ScreenWinTestTwoDisplays1x, GetDisplayNearestWindow) {
523 gfx::Screen* screen = GetScreen();
524 const gfx::Display left_display = screen->GetAllDisplays()[0];
525 const gfx::Display right_display = screen->GetAllDisplays()[1];
526
527 gfx::NativeWindow left_window = GetNativeWindowFromHWND(GetLeftFakeHwnd());
528 EXPECT_EQ(left_display, screen->GetDisplayNearestWindow(left_window));
529
530 gfx::NativeWindow right_window = GetNativeWindowFromHWND(GetRightFakeHwnd());
531 EXPECT_EQ(right_display, screen->GetDisplayNearestWindow(right_window));
532 }
533
534 TEST_F(ScreenWinTestTwoDisplays1x, GetDisplayNearestPoint) {
535 gfx::Screen* screen = GetScreen();
536 const gfx::Display left_display = screen->GetAllDisplays()[0];
537 const gfx::Display right_display = screen->GetAllDisplays()[1];
538
539 EXPECT_EQ(left_display, screen->GetDisplayNearestPoint(gfx::Point(0, 0)));
540 EXPECT_EQ(left_display, screen->GetDisplayNearestPoint(gfx::Point(250, 952)));
541 EXPECT_EQ(left_display,
542 screen->GetDisplayNearestPoint(gfx::Point(1919, 1199)));
543
544 EXPECT_EQ(right_display, screen->GetDisplayNearestPoint(gfx::Point(1920, 0)));
545 EXPECT_EQ(right_display,
546 screen->GetDisplayNearestPoint(gfx::Point(2000, 400)));
547 EXPECT_EQ(right_display,
548 screen->GetDisplayNearestPoint(gfx::Point(2719, 599)));
549 }
550
551 TEST_F(ScreenWinTestTwoDisplays1x, GetDisplayMatching) {
552 gfx::Screen* screen = GetScreen();
553 const gfx::Display left_display = screen->GetAllDisplays()[0];
554 const gfx::Display right_display = screen->GetAllDisplays()[1];
555
556 EXPECT_EQ(left_display,
557 screen->GetDisplayMatching(gfx::Rect(0, 0, 100, 100)));
558 EXPECT_EQ(left_display,
559 screen->GetDisplayMatching(gfx::Rect(1819, 1099, 100, 100)));
560
561 EXPECT_EQ(right_display,
562 screen->GetDisplayMatching(gfx::Rect(1920, 0, 100, 100)));
563 EXPECT_EQ(right_display,
564 screen->GetDisplayMatching(gfx::Rect(2619, 499, 100, 100)));
565 }
566
567 TEST_F(ScreenWinTestTwoDisplays1x, GetPrimaryDisplay) {
568 gfx::Screen* screen = GetScreen();
569 gfx::Display primary = screen->GetPrimaryDisplay();
570 EXPECT_EQ(gfx::Point(0, 0), primary.bounds().origin());
571 }
572
573 // Two Displays of 2.0 Device Scale Factor.
574 class ScreenWinTestTwoDisplays2x : public ScreenWinTest {
575 public:
576 ScreenWinTestTwoDisplays2x() = default;
577
578 void SetUpScreen(TestScreenWinInitializer* initializer) override {
579 gfx::SetDefaultDeviceScaleFactor(2.0);
580 initializer->AddMonitor(gfx::Rect(0, 0, 1920, 1200),
581 gfx::Rect(0, 0, 1920, 1100),
582 L"primary",
583 2.0);
584 initializer->AddMonitor(gfx::Rect(1920, 0, 800, 600),
585 gfx::Rect(1920, 0, 800, 600),
586 L"secondary",
587 2.0);
588 fake_hwnd_left_ = initializer->CreateFakeHwnd(gfx::Rect(0, 0, 1920, 1100));
589 fake_hwnd_right_ =
590 initializer->CreateFakeHwnd(gfx::Rect(1920, 0, 800, 600));
591 }
592
593 HWND GetLeftFakeHwnd() {
594 return fake_hwnd_left_;
595 }
596
597 HWND GetRightFakeHwnd() {
598 return fake_hwnd_right_;
599 }
600
601 private:
602 HWND fake_hwnd_left_ = nullptr;
603 HWND fake_hwnd_right_ = nullptr;
604
605 DISALLOW_COPY_AND_ASSIGN(ScreenWinTestTwoDisplays2x);
606 };
607
608 TEST_F(ScreenWinTestTwoDisplays2x, GetDisplays) {
609 std::vector<gfx::Display> displays = GetScreen()->GetAllDisplays();
610 ASSERT_EQ(2u, displays.size());
611 EXPECT_EQ(gfx::Rect(0, 0, 960, 600), displays[0].bounds());
612 EXPECT_EQ(gfx::Rect(0, 0, 960, 550), displays[0].work_area());
613 EXPECT_EQ(gfx::Rect(960, 0, 400, 300), displays[1].bounds());
614 EXPECT_EQ(gfx::Rect(960, 0, 400, 300), displays[1].work_area());
615 }
616
617 TEST_F(ScreenWinTestTwoDisplays2x, GetDisplayNearestWindowPrimaryDisplay) {
618 gfx::Screen* screen = GetScreen();
619 EXPECT_EQ(screen->GetPrimaryDisplay(),
620 screen->GetDisplayNearestWindow(nullptr));
621 }
622
623 TEST_F(ScreenWinTestTwoDisplays2x, GetDisplayNearestWindow) {
624 gfx::Screen* screen = GetScreen();
625 const gfx::Display left_display = screen->GetAllDisplays()[0];
626 const gfx::Display right_display = screen->GetAllDisplays()[1];
627
628 gfx::NativeWindow left_window = GetNativeWindowFromHWND(GetLeftFakeHwnd());
629 EXPECT_EQ(left_display, screen->GetDisplayNearestWindow(left_window));
630
631 gfx::NativeWindow right_window = GetNativeWindowFromHWND(GetRightFakeHwnd());
632 EXPECT_EQ(right_display, screen->GetDisplayNearestWindow(right_window));
633 }
634
635 TEST_F(ScreenWinTestTwoDisplays2x, GetDisplayNearestPoint) {
636 gfx::Screen* screen = GetScreen();
637 const gfx::Display left_display = screen->GetAllDisplays()[0];
638 const gfx::Display right_display = screen->GetAllDisplays()[1];
639
640 EXPECT_EQ(left_display, screen->GetDisplayNearestPoint(gfx::Point(0, 0)));
641 EXPECT_EQ(left_display, screen->GetDisplayNearestPoint(gfx::Point(125, 476)));
642 EXPECT_EQ(left_display,
643 screen->GetDisplayNearestPoint(gfx::Point(959, 599)));
644
645 EXPECT_EQ(right_display, screen->GetDisplayNearestPoint(gfx::Point(960, 0)));
646 EXPECT_EQ(right_display,
647 screen->GetDisplayNearestPoint(gfx::Point(1000, 200)));
648 EXPECT_EQ(right_display,
649 screen->GetDisplayNearestPoint(gfx::Point(1359, 299)));
650 }
651
652 TEST_F(ScreenWinTestTwoDisplays2x, GetDisplayMatching) {
653 gfx::Screen* screen = GetScreen();
654 const gfx::Display left_display = screen->GetAllDisplays()[0];
655 const gfx::Display right_display = screen->GetAllDisplays()[1];
656
657 EXPECT_EQ(left_display,
658 screen->GetDisplayMatching(gfx::Rect(0, 0, 100, 100)));
659 EXPECT_EQ(left_display,
660 screen->GetDisplayMatching(gfx::Rect(1819, 1099, 100, 100)));
661
662 EXPECT_EQ(right_display,
663 screen->GetDisplayMatching(gfx::Rect(1920, 0, 100, 100)));
664 EXPECT_EQ(right_display,
665 screen->GetDisplayMatching(gfx::Rect(2619, 499, 100, 100)));
666 }
667
668 TEST_F(ScreenWinTestTwoDisplays2x, GetPrimaryDisplay) {
669 gfx::Screen* screen = GetScreen();
670 gfx::Display primary = screen->GetPrimaryDisplay();
671 EXPECT_EQ(gfx::Point(0, 0), primary.bounds().origin());
672 }
673
674 // Two Displays of 2.0 (Left) and 1.0 (Right) Device Scale Factor under
675 // Windows DPI Virtualization. Note that the displays do not form a euclidean
676 // space.
677 class ScreenWinTestTwoDisplays2x1xVirtualized : public ScreenWinTest {
678 public:
679 ScreenWinTestTwoDisplays2x1xVirtualized() = default;
680
681 void SetUpScreen(TestScreenWinInitializer* initializer) override {
682 gfx::SetDefaultDeviceScaleFactor(2.0);
683 initializer->AddMonitor(gfx::Rect(0, 0, 3200, 1600),
684 gfx::Rect(0, 0, 3200, 1500),
685 L"primary",
686 2.0);
687 initializer->AddMonitor(gfx::Rect(6400, 0, 3840, 2400),
688 gfx::Rect(6400, 0, 3840, 2400),
689 L"secondary",
690 2.0);
691 fake_hwnd_left_ = initializer->CreateFakeHwnd(gfx::Rect(0, 0, 3200, 1500));
692 fake_hwnd_right_ =
693 initializer->CreateFakeHwnd(gfx::Rect(6400, 0, 3840, 2400));
694 }
695
696 HWND GetLeftFakeHwnd() {
697 return fake_hwnd_left_;
698 }
699
700 HWND GetRightFakeHwnd() {
701 return fake_hwnd_right_;
702 }
703
704 private:
705 HWND fake_hwnd_left_ = nullptr;
706 HWND fake_hwnd_right_ = nullptr;
707
708 DISALLOW_COPY_AND_ASSIGN(ScreenWinTestTwoDisplays2x1xVirtualized);
709 };
710
711 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, GetDisplays) {
712 std::vector<gfx::Display> displays = GetScreen()->GetAllDisplays();
713 ASSERT_EQ(2u, displays.size());
714 EXPECT_EQ(gfx::Rect(0, 0, 1600, 800), displays[0].bounds());
715 EXPECT_EQ(gfx::Rect(0, 0, 1600, 750), displays[0].work_area());
716 EXPECT_EQ(gfx::Rect(3200, 0, 1920, 1200), displays[1].bounds());
717 EXPECT_EQ(gfx::Rect(3200, 0, 1920, 1200), displays[1].work_area());
718 }
719
720 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, GetNumDisplays) {
721 EXPECT_EQ(2, GetScreen()->GetNumDisplays());
722 }
723
724 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized,
725 GetDisplayNearestWindowPrimaryDisplay) {
726 gfx::Screen* screen = GetScreen();
727 EXPECT_EQ(screen->GetPrimaryDisplay(),
728 screen->GetDisplayNearestWindow(nullptr));
729 }
730
731 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, GetDisplayNearestWindow) {
732 gfx::Screen* screen = GetScreen();
733 const gfx::Display left_display = screen->GetAllDisplays()[0];
734 const gfx::Display right_display = screen->GetAllDisplays()[1];
735
736 gfx::NativeWindow left_window = GetNativeWindowFromHWND(GetLeftFakeHwnd());
737 EXPECT_EQ(left_display, screen->GetDisplayNearestWindow(left_window));
738
739 gfx::NativeWindow right_window = GetNativeWindowFromHWND(GetRightFakeHwnd());
740 EXPECT_EQ(right_display, screen->GetDisplayNearestWindow(right_window));
741 }
742
743 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, GetDisplayNearestPoint) {
744 gfx::Screen* screen = GetScreen();
745 const gfx::Display left_display = screen->GetAllDisplays()[0];
746 const gfx::Display right_display = screen->GetAllDisplays()[1];
747
748 EXPECT_EQ(left_display, screen->GetDisplayNearestPoint(gfx::Point(0, 0)));
749 EXPECT_EQ(left_display, screen->GetDisplayNearestPoint(gfx::Point(125, 476)));
750 EXPECT_EQ(left_display,
751 screen->GetDisplayNearestPoint(gfx::Point(1599, 799)));
752
753 EXPECT_EQ(right_display, screen->GetDisplayNearestPoint(gfx::Point(3200, 0)));
754 EXPECT_EQ(right_display,
755 screen->GetDisplayNearestPoint(gfx::Point(4000, 400)));
756 EXPECT_EQ(right_display,
757 screen->GetDisplayNearestPoint(gfx::Point(5119, 1199)));
758 }
759
760 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, GetDisplayMatching) {
761 gfx::Screen* screen = GetScreen();
762 const gfx::Display left_display = screen->GetAllDisplays()[0];
763 const gfx::Display right_display = screen->GetAllDisplays()[1];
764
765 EXPECT_EQ(left_display,
766 screen->GetDisplayMatching(gfx::Rect(0, 0, 100, 100)));
767 EXPECT_EQ(left_display,
768 screen->GetDisplayMatching(gfx::Rect(1819, 1099, 100, 100)));
769
770 EXPECT_EQ(right_display,
771 screen->GetDisplayMatching(gfx::Rect(6400, 0, 100, 100)));
772 EXPECT_EQ(right_display,
773 screen->GetDisplayMatching(gfx::Rect(10139, 2299, 100, 100)));
774 }
775
776 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, GetPrimaryDisplay) {
777 gfx::Screen* screen = GetScreen();
778 gfx::Display primary = screen->GetPrimaryDisplay();
779 EXPECT_EQ(gfx::Point(0, 0), primary.bounds().origin());
780 }
781
782 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698