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

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

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

Powered by Google App Engine
This is Rietveld 408576698