OLD | NEW |
---|---|
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 | |
37 } // namespace | 151 } // namespace |
38 | 152 |
153 // Allows tests to specify the screen and associated state. | |
154 class TestScreenWinInitializer { | |
155 public: | |
156 virtual void AddMonitor(const gfx::Rect& pixel_bounds, | |
157 const gfx::Rect& pixel_work, | |
158 const wchar_t* device_name, | |
159 float device_scale_factor) = 0; | |
160 | |
161 virtual HWND CreateFakeHwnd(const gfx::Rect& bounds) = 0; | |
162 }; | |
163 | |
164 class TestScreenWinInitializerImpl : public TestScreenWinInitializer { | |
165 public: | |
166 TestScreenWinInitializerImpl() = default; | |
167 | |
168 ~TestScreenWinInitializerImpl() { | |
169 gfx::Screen::SetScreenInstance(nullptr); | |
170 } | |
171 | |
172 void AddMonitor(const gfx::Rect& pixel_bounds, | |
173 const gfx::Rect& pixel_work, | |
174 const wchar_t* device_name, | |
175 float device_scale_factor) override { | |
176 MONITORINFOEX monitor_info = CreateMonitorInfo(pixel_bounds, | |
177 pixel_work, | |
178 device_name); | |
179 monitor_infos_.push_back(monitor_info); | |
180 display_infos_.push_back(gfx::win::DisplayInfo(monitor_info, | |
181 device_scale_factor, | |
182 gfx::Display::ROTATE_0)); | |
183 } | |
184 | |
185 HWND CreateFakeHwnd(const gfx::Rect& bounds) override { | |
186 EXPECT_EQ(screen_win_, nullptr); | |
187 hwnd_map_.insert(std::pair<HWND, gfx::Rect>(++hwndLast_, bounds)); | |
188 return hwndLast_; | |
189 } | |
190 | |
191 void InitializeScreenWin() { | |
192 ASSERT_EQ(screen_win_, nullptr); | |
193 TestDisplayManager::InitializeDisplayManager(display_infos_, | |
194 monitor_infos_, | |
195 hwnd_map_); | |
196 screen_win_.reset(new TestScreenWin()); | |
197 gfx::Screen::SetScreenInstance(screen_win_.get()); | |
198 } | |
199 | |
200 ScreenWin* GetScreenWin() { | |
201 return screen_win_.get(); | |
202 } | |
203 | |
204 private: | |
205 HWND hwndLast_ = nullptr; | |
206 scoped_ptr<ScreenWin> screen_win_; | |
207 std::vector<MONITORINFOEX> monitor_infos_; | |
208 std::vector<gfx::win::DisplayInfo> display_infos_; | |
209 std::unordered_map<HWND, gfx::Rect> hwnd_map_; | |
210 DISALLOW_COPY_AND_ASSIGN(TestScreenWinInitializerImpl); | |
211 }; | |
212 | |
39 class ScreenWinTest : public testing::Test { | 213 class ScreenWinTest : public testing::Test { |
40 private: | 214 protected: |
215 ScreenWinTest() = default; | |
216 | |
41 void SetUp() override { | 217 void SetUp() override { |
42 testing::Test::SetUp(); | 218 testing::Test::SetUp(); |
43 gfx::SetDefaultDeviceScaleFactor(1.0); | 219 gfx::SetDefaultDeviceScaleFactor(1.0); |
220 screen_win_initializer_.reset(new TestScreenWinInitializerImpl()); | |
221 SetUpScreen(screen_win_initializer_.get()); | |
222 screen_win_initializer_->InitializeScreenWin(); | |
44 } | 223 } |
45 | 224 |
46 void TearDown() override { | 225 void TearDown() override { |
226 screen_win_initializer_.reset(); | |
47 gfx::SetDefaultDeviceScaleFactor(1.0); | 227 gfx::SetDefaultDeviceScaleFactor(1.0); |
48 testing::Test::TearDown(); | 228 testing::Test::TearDown(); |
49 } | 229 } |
50 }; | 230 |
51 | 231 virtual void SetUpScreen(TestScreenWinInitializer* initializer) = 0; |
52 TEST_F(ScreenWinTest, SingleDisplay1x) { | 232 |
53 std::vector<MONITORINFOEX> monitor_infos; | 233 Screen* GetScreen() const { |
54 monitor_infos.push_back(CreateMonitorInfo(gfx::Rect(0, 0, 1920, 1200), | 234 return screen_win_initializer_->GetScreenWin(); |
55 gfx::Rect(0, 0, 1920, 1100), | 235 } |
56 L"primary")); | 236 |
57 std::vector<gfx::Display> displays = | 237 NativeWindow GetNativeWindowFromHWND(HWND hwnd) const { |
58 ScreenWin::GetDisplaysForMonitorInfos(monitor_infos); | 238 ScreenWin* screen_win = screen_win_initializer_->GetScreenWin(); |
59 | 239 return screen_win->GetNativeWindowFromHWND(hwnd);; |
240 } | |
241 | |
242 private: | |
243 scoped_ptr<TestScreenWinInitializerImpl> screen_win_initializer_; | |
244 DISALLOW_COPY_AND_ASSIGN(ScreenWinTest); | |
245 }; | |
246 | |
247 // Single Display of 1.0 Device Scale Factor. | |
248 class ScreenWinTestSingleDisplay1x : public ScreenWinTest { | |
249 public: | |
250 void SetUpScreen(TestScreenWinInitializer* initializer) override { | |
251 initializer->AddMonitor(gfx::Rect(0, 0, 1920, 1200), | |
252 gfx::Rect(0, 0, 1920, 1100), | |
253 L"primary", | |
254 1.0); | |
255 fake_hwnd_ = initializer->CreateFakeHwnd(gfx::Rect(0, 0, 1920, 1100)); | |
256 } | |
257 | |
258 HWND GetFakeHwnd() { | |
259 return fake_hwnd_; | |
260 } | |
261 | |
262 private: | |
263 HWND fake_hwnd_ = nullptr; | |
264 }; | |
265 | |
oshima
2016/02/01 19:07:37
DISALLOW_COPY_AND_ASSIGN
robliao
2016/02/01 21:35:04
Done.
| |
266 TEST_F(ScreenWinTestSingleDisplay1x, GetDisplays) { | |
267 std::vector<gfx::Display> displays = GetScreen()->GetAllDisplays(); | |
60 ASSERT_EQ(1u, displays.size()); | 268 ASSERT_EQ(1u, displays.size()); |
61 EXPECT_EQ(gfx::Rect(0, 0, 1920, 1200), displays[0].bounds()); | 269 EXPECT_EQ(gfx::Rect(0, 0, 1920, 1200), displays[0].bounds()); |
62 EXPECT_EQ(gfx::Rect(0, 0, 1920, 1100), displays[0].work_area()); | 270 EXPECT_EQ(gfx::Rect(0, 0, 1920, 1100), displays[0].work_area()); |
63 } | 271 } |
64 | 272 |
65 TEST_F(ScreenWinTest, SingleDisplay2x) { | 273 TEST_F(ScreenWinTestSingleDisplay1x, GetNumDisplays) { |
66 gfx::SetDefaultDeviceScaleFactor(2.0); | 274 EXPECT_EQ(1, GetScreen()->GetNumDisplays()); |
67 | 275 } |
68 std::vector<MONITORINFOEX> monitor_infos; | 276 |
69 monitor_infos.push_back(CreateMonitorInfo(gfx::Rect(0, 0, 1920, 1200), | 277 TEST_F(ScreenWinTestSingleDisplay1x, GetDisplayNearestWindowPrimaryDisplay) { |
70 gfx::Rect(0, 0, 1920, 1100), | 278 gfx::Screen* screen = GetScreen(); |
71 L"primary")); | 279 EXPECT_EQ(screen->GetPrimaryDisplay(), |
72 std::vector<gfx::Display> displays = | 280 screen->GetDisplayNearestWindow(nullptr)); |
73 ScreenWin::GetDisplaysForMonitorInfos(monitor_infos); | 281 } |
74 | 282 |
283 TEST_F(ScreenWinTestSingleDisplay1x, GetDisplayNearestWindow) { | |
284 gfx::Screen* screen = GetScreen(); | |
285 gfx::NativeWindow native_window = GetNativeWindowFromHWND(GetFakeHwnd()); | |
286 EXPECT_EQ(screen->GetAllDisplays()[0], | |
287 screen->GetDisplayNearestWindow(native_window)); | |
288 } | |
289 | |
290 TEST_F(ScreenWinTestSingleDisplay1x, GetDisplayNearestPoint) { | |
291 gfx::Screen* screen = GetScreen(); | |
292 gfx::Display display = screen->GetAllDisplays()[0]; | |
293 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(0, 0))); | |
294 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(250, 952))); | |
295 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(1919, 1199))); | |
oshima
2016/02/01 19:07:37
what happens for the out of bounds point?
robliao
2016/02/01 21:35:05
The test will crash since the test harness doesn't
| |
296 } | |
297 | |
298 TEST_F(ScreenWinTestSingleDisplay1x, GetDisplayMatching) { | |
299 gfx::Screen* screen = GetScreen(); | |
300 gfx::Display display = screen->GetAllDisplays()[0]; | |
301 EXPECT_EQ(display, screen->GetDisplayMatching(gfx::Rect(0, 0, 100, 100))); | |
302 EXPECT_EQ(display, | |
303 screen->GetDisplayMatching(gfx::Rect(1819, 1099, 100, 100))); | |
oshima
2016/02/01 19:07:37
same here
robliao
2016/02/01 21:35:05
See above. The test here is a bit more lenient in
| |
304 } | |
305 | |
306 TEST_F(ScreenWinTestSingleDisplay1x, GetPrimaryDisplay) { | |
307 gfx::Screen* screen = GetScreen(); | |
308 EXPECT_EQ(gfx::Point(0, 0), screen->GetPrimaryDisplay().bounds().origin()); | |
309 } | |
310 | |
311 // Single Display of 2.0 Device Scale Factor. | |
312 class ScreenWinTestSingleDisplay2x : public ScreenWinTest { | |
313 public: | |
314 void SetUpScreen(TestScreenWinInitializer* initializer) override { | |
315 gfx::SetDefaultDeviceScaleFactor(2.0); | |
316 initializer->AddMonitor(gfx::Rect(0, 0, 1920, 1200), | |
317 gfx::Rect(0, 0, 1920, 1100), | |
318 L"primary", | |
319 2.0); | |
320 fake_hwnd_ = initializer->CreateFakeHwnd(gfx::Rect(0, 0, 1920, 1100)); | |
321 } | |
322 | |
323 HWND GetFakeHwnd() { | |
324 return fake_hwnd_; | |
325 } | |
326 | |
327 private: | |
328 HWND fake_hwnd_ = nullptr; | |
329 }; | |
330 | |
331 TEST_F(ScreenWinTestSingleDisplay2x, GetDisplays) { | |
332 std::vector<gfx::Display> displays = GetScreen()->GetAllDisplays(); | |
75 ASSERT_EQ(1u, displays.size()); | 333 ASSERT_EQ(1u, displays.size()); |
76 EXPECT_EQ(gfx::Rect(0, 0, 960, 600), displays[0].bounds()); | 334 EXPECT_EQ(gfx::Rect(0, 0, 960, 600), displays[0].bounds()); |
77 EXPECT_EQ(gfx::Rect(0, 0, 960, 550), displays[0].work_area()); | 335 EXPECT_EQ(gfx::Rect(0, 0, 960, 550), displays[0].work_area()); |
78 } | 336 } |
79 | 337 |
338 TEST_F(ScreenWinTestSingleDisplay2x, GetDisplayNearestPoint) { | |
339 gfx::Screen* screen = GetScreen(); | |
340 gfx::Display display = screen->GetAllDisplays()[0]; | |
341 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(0, 0))); | |
342 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(125, 476))); | |
343 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(959, 599))); | |
344 } | |
345 | |
346 TEST_F(ScreenWinTestSingleDisplay2x, GetDisplayMatching) { | |
347 gfx::Screen* screen = GetScreen(); | |
348 gfx::Display display = screen->GetAllDisplays()[0]; | |
349 EXPECT_EQ(display, screen->GetDisplayMatching(gfx::Rect(0, 0, 100, 100))); | |
350 EXPECT_EQ(display, | |
351 screen->GetDisplayMatching(gfx::Rect(1819, 1099, 100, 100))); | |
352 } | |
353 | |
354 // Two Displays of 1.0 Device Scale Factor. | |
355 class ScreenWinTestTwoDisplays1x : public ScreenWinTest { | |
356 public: | |
357 void SetUpScreen(TestScreenWinInitializer* initializer) override { | |
358 initializer->AddMonitor(gfx::Rect(0, 0, 1920, 1200), | |
359 gfx::Rect(0, 0, 1920, 1100), | |
360 L"primary", | |
361 1.0); | |
362 initializer->AddMonitor(gfx::Rect(1920, 0, 800, 600), | |
363 gfx::Rect(1920, 0, 800, 600), | |
364 L"secondary", | |
365 1.0); | |
366 fake_hwnd_left_ = initializer->CreateFakeHwnd(gfx::Rect(0, 0, 1920, 1100)); | |
367 fake_hwnd_right_ = | |
368 initializer->CreateFakeHwnd(gfx::Rect(1920, 0, 800, 600)); | |
369 } | |
370 | |
371 HWND GetLeftFakeHwnd() { | |
372 return fake_hwnd_left_; | |
373 } | |
374 | |
375 HWND GetRightFakeHwnd() { | |
376 return fake_hwnd_right_; | |
377 } | |
378 | |
379 private: | |
380 HWND fake_hwnd_left_ = nullptr; | |
381 HWND fake_hwnd_right_ = nullptr; | |
382 }; | |
383 | |
384 TEST_F(ScreenWinTestTwoDisplays1x, GetDisplays) { | |
385 std::vector<gfx::Display> displays = GetScreen()->GetAllDisplays(); | |
386 ASSERT_EQ(2u, displays.size()); | |
387 EXPECT_EQ(gfx::Rect(0, 0, 1920, 1200), displays[0].bounds()); | |
388 EXPECT_EQ(gfx::Rect(0, 0, 1920, 1100), displays[0].work_area()); | |
389 EXPECT_EQ(gfx::Rect(1920, 0, 800, 600), displays[1].bounds()); | |
390 EXPECT_EQ(gfx::Rect(1920, 0, 800, 600), displays[1].work_area()); | |
391 } | |
392 | |
393 TEST_F(ScreenWinTestTwoDisplays1x, GetNumDisplays) { | |
394 EXPECT_EQ(2, GetScreen()->GetNumDisplays()); | |
395 } | |
396 | |
397 TEST_F(ScreenWinTestTwoDisplays1x, GetDisplayNearestWindowPrimaryDisplay) { | |
398 gfx::Screen* screen = GetScreen(); | |
399 EXPECT_EQ(screen->GetPrimaryDisplay(), | |
400 screen->GetDisplayNearestWindow(nullptr)); | |
401 } | |
402 | |
403 TEST_F(ScreenWinTestTwoDisplays1x, GetDisplayNearestWindow) { | |
404 gfx::Screen* screen = GetScreen(); | |
405 const gfx::Display left_display = screen->GetAllDisplays()[0]; | |
406 const gfx::Display right_display = screen->GetAllDisplays()[1]; | |
407 | |
408 gfx::NativeWindow left_window = GetNativeWindowFromHWND(GetLeftFakeHwnd()); | |
409 EXPECT_EQ(left_display, screen->GetDisplayNearestWindow(left_window)); | |
410 | |
411 gfx::NativeWindow right_window = GetNativeWindowFromHWND(GetRightFakeHwnd()); | |
412 EXPECT_EQ(right_display, screen->GetDisplayNearestWindow(right_window)); | |
413 } | |
414 | |
415 TEST_F(ScreenWinTestTwoDisplays1x, GetDisplayNearestPoint) { | |
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 EXPECT_EQ(left_display, screen->GetDisplayNearestPoint(gfx::Point(0, 0))); | |
421 EXPECT_EQ(left_display, screen->GetDisplayNearestPoint(gfx::Point(250, 952))); | |
422 EXPECT_EQ(left_display, | |
423 screen->GetDisplayNearestPoint(gfx::Point(1919, 1199))); | |
424 | |
425 EXPECT_EQ(right_display, screen->GetDisplayNearestPoint(gfx::Point(1920, 0))); | |
426 EXPECT_EQ(right_display, | |
427 screen->GetDisplayNearestPoint(gfx::Point(2000, 400))); | |
428 EXPECT_EQ(right_display, | |
429 screen->GetDisplayNearestPoint(gfx::Point(2719, 599))); | |
430 } | |
431 | |
432 TEST_F(ScreenWinTestTwoDisplays1x, GetDisplayMatching) { | |
433 gfx::Screen* screen = GetScreen(); | |
434 const gfx::Display left_display = screen->GetAllDisplays()[0]; | |
435 const gfx::Display right_display = screen->GetAllDisplays()[1]; | |
436 | |
437 EXPECT_EQ(left_display, | |
438 screen->GetDisplayMatching(gfx::Rect(0, 0, 100, 100))); | |
439 EXPECT_EQ(left_display, | |
440 screen->GetDisplayMatching(gfx::Rect(1819, 1099, 100, 100))); | |
441 | |
442 EXPECT_EQ(right_display, | |
443 screen->GetDisplayMatching(gfx::Rect(1920, 0, 100, 100))); | |
444 EXPECT_EQ(right_display, | |
445 screen->GetDisplayMatching(gfx::Rect(2619, 499, 100, 100))); | |
446 } | |
447 | |
448 TEST_F(ScreenWinTestTwoDisplays1x, GetPrimaryDisplay) { | |
449 gfx::Screen* screen = GetScreen(); | |
450 gfx::Display primary = screen->GetPrimaryDisplay(); | |
451 EXPECT_EQ(gfx::Point(0, 0), primary.bounds().origin()); | |
452 } | |
453 | |
454 // Two Displays of 2.0 Device Scale Factor. | |
455 class ScreenWinTestTwoDisplays2x : public ScreenWinTest { | |
456 public: | |
457 void SetUpScreen(TestScreenWinInitializer* initializer) override { | |
458 gfx::SetDefaultDeviceScaleFactor(2.0); | |
459 initializer->AddMonitor(gfx::Rect(0, 0, 1920, 1200), | |
460 gfx::Rect(0, 0, 1920, 1100), | |
461 L"primary", | |
462 2.0); | |
463 initializer->AddMonitor(gfx::Rect(1920, 0, 800, 600), | |
464 gfx::Rect(1920, 0, 800, 600), | |
465 L"secondary", | |
466 2.0); | |
467 fake_hwnd_left_ = initializer->CreateFakeHwnd(gfx::Rect(0, 0, 1920, 1100)); | |
468 fake_hwnd_right_ = | |
469 initializer->CreateFakeHwnd(gfx::Rect(1920, 0, 800, 600)); | |
470 } | |
471 | |
472 HWND GetLeftFakeHwnd() { | |
473 return fake_hwnd_left_; | |
474 } | |
475 | |
476 HWND GetRightFakeHwnd() { | |
477 return fake_hwnd_right_; | |
478 } | |
479 | |
480 private: | |
481 HWND fake_hwnd_left_ = nullptr; | |
482 HWND fake_hwnd_right_ = nullptr; | |
483 }; | |
484 | |
485 TEST_F(ScreenWinTestTwoDisplays2x, GetDisplays) { | |
486 std::vector<gfx::Display> displays = GetScreen()->GetAllDisplays(); | |
487 ASSERT_EQ(2u, displays.size()); | |
488 EXPECT_EQ(gfx::Rect(0, 0, 960, 600), displays[0].bounds()); | |
489 EXPECT_EQ(gfx::Rect(0, 0, 960, 550), displays[0].work_area()); | |
490 EXPECT_EQ(gfx::Rect(960, 0, 400, 300), displays[1].bounds()); | |
491 EXPECT_EQ(gfx::Rect(960, 0, 400, 300), displays[1].work_area()); | |
492 } | |
493 | |
494 TEST_F(ScreenWinTestTwoDisplays2x, GetDisplayNearestWindowPrimaryDisplay) { | |
495 gfx::Screen* screen = GetScreen(); | |
496 EXPECT_EQ(screen->GetPrimaryDisplay(), | |
497 screen->GetDisplayNearestWindow(nullptr)); | |
498 } | |
499 | |
500 TEST_F(ScreenWinTestTwoDisplays2x, GetDisplayNearestWindow) { | |
501 gfx::Screen* screen = GetScreen(); | |
502 const gfx::Display left_display = screen->GetAllDisplays()[0]; | |
503 const gfx::Display right_display = screen->GetAllDisplays()[1]; | |
504 | |
505 gfx::NativeWindow left_window = GetNativeWindowFromHWND(GetLeftFakeHwnd()); | |
506 EXPECT_EQ(left_display, screen->GetDisplayNearestWindow(left_window)); | |
507 | |
508 gfx::NativeWindow right_window = GetNativeWindowFromHWND(GetRightFakeHwnd()); | |
509 EXPECT_EQ(right_display, screen->GetDisplayNearestWindow(right_window)); | |
510 } | |
511 | |
512 TEST_F(ScreenWinTestTwoDisplays2x, GetDisplayNearestPoint) { | |
513 gfx::Screen* screen = GetScreen(); | |
514 const gfx::Display left_display = screen->GetAllDisplays()[0]; | |
515 const gfx::Display right_display = screen->GetAllDisplays()[1]; | |
516 | |
517 EXPECT_EQ(left_display, screen->GetDisplayNearestPoint(gfx::Point(0, 0))); | |
518 EXPECT_EQ(left_display, screen->GetDisplayNearestPoint(gfx::Point(125, 476))); | |
519 EXPECT_EQ(left_display, | |
520 screen->GetDisplayNearestPoint(gfx::Point(959, 599))); | |
521 | |
522 EXPECT_EQ(right_display, screen->GetDisplayNearestPoint(gfx::Point(960, 0))); | |
523 EXPECT_EQ(right_display, | |
524 screen->GetDisplayNearestPoint(gfx::Point(1000, 200))); | |
525 EXPECT_EQ(right_display, | |
526 screen->GetDisplayNearestPoint(gfx::Point(1359, 299))); | |
527 } | |
528 | |
529 TEST_F(ScreenWinTestTwoDisplays2x, GetDisplayMatching) { | |
530 gfx::Screen* screen = GetScreen(); | |
531 const gfx::Display left_display = screen->GetAllDisplays()[0]; | |
532 const gfx::Display right_display = screen->GetAllDisplays()[1]; | |
533 | |
534 EXPECT_EQ(left_display, | |
535 screen->GetDisplayMatching(gfx::Rect(0, 0, 100, 100))); | |
536 EXPECT_EQ(left_display, | |
537 screen->GetDisplayMatching(gfx::Rect(1819, 1099, 100, 100))); | |
538 | |
539 EXPECT_EQ(right_display, | |
540 screen->GetDisplayMatching(gfx::Rect(1920, 0, 100, 100))); | |
541 EXPECT_EQ(right_display, | |
542 screen->GetDisplayMatching(gfx::Rect(2619, 499, 100, 100))); | |
543 } | |
544 | |
545 TEST_F(ScreenWinTestTwoDisplays2x, GetPrimaryDisplay) { | |
546 gfx::Screen* screen = GetScreen(); | |
547 gfx::Display primary = screen->GetPrimaryDisplay(); | |
548 EXPECT_EQ(gfx::Point(0, 0), primary.bounds().origin()); | |
549 } | |
550 | |
551 // Two Displays of 2.0 (Left) and 1.0 (Right) Device Scale Factor under | |
552 // Windows DPI Virtualization. Note that the displays do not form a euclidean | |
553 // space. | |
554 class ScreenWinTestTwoDisplays2x1xVirtualized : public ScreenWinTest { | |
555 public: | |
556 void SetUpScreen(TestScreenWinInitializer* initializer) override { | |
557 gfx::SetDefaultDeviceScaleFactor(2.0); | |
558 initializer->AddMonitor(gfx::Rect(0, 0, 3200, 1600), | |
559 gfx::Rect(0, 0, 3200, 1500), | |
560 L"primary", | |
561 2.0); | |
562 initializer->AddMonitor(gfx::Rect(6400, 0, 3840, 2400), | |
563 gfx::Rect(6400, 0, 3840, 2400), | |
564 L"secondary", | |
565 2.0); | |
566 fake_hwnd_left_ = initializer->CreateFakeHwnd(gfx::Rect(0, 0, 3200, 1500)); | |
567 fake_hwnd_right_ = | |
568 initializer->CreateFakeHwnd(gfx::Rect(6400, 0, 3840, 2400)); | |
569 } | |
570 | |
571 HWND GetLeftFakeHwnd() { | |
572 return fake_hwnd_left_; | |
573 } | |
574 | |
575 HWND GetRightFakeHwnd() { | |
576 return fake_hwnd_right_; | |
577 } | |
578 | |
579 private: | |
580 HWND fake_hwnd_left_ = nullptr; | |
581 HWND fake_hwnd_right_ = nullptr; | |
582 }; | |
583 | |
584 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, GetDisplays) { | |
585 std::vector<gfx::Display> displays = GetScreen()->GetAllDisplays(); | |
586 ASSERT_EQ(2u, displays.size()); | |
587 EXPECT_EQ(gfx::Rect(0, 0, 1600, 800), displays[0].bounds()); | |
588 EXPECT_EQ(gfx::Rect(0, 0, 1600, 750), displays[0].work_area()); | |
589 EXPECT_EQ(gfx::Rect(3200, 0, 1920, 1200), displays[1].bounds()); | |
590 EXPECT_EQ(gfx::Rect(3200, 0, 1920, 1200), displays[1].work_area()); | |
591 } | |
592 | |
593 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, GetNumDisplays) { | |
594 EXPECT_EQ(2, GetScreen()->GetNumDisplays()); | |
595 } | |
596 | |
597 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, | |
598 GetDisplayNearestWindowPrimaryDisplay) { | |
599 gfx::Screen* screen = GetScreen(); | |
600 EXPECT_EQ(screen->GetPrimaryDisplay(), | |
601 screen->GetDisplayNearestWindow(nullptr)); | |
602 } | |
603 | |
604 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, GetDisplayNearestWindow) { | |
605 gfx::Screen* screen = GetScreen(); | |
606 const gfx::Display left_display = screen->GetAllDisplays()[0]; | |
607 const gfx::Display right_display = screen->GetAllDisplays()[1]; | |
608 | |
609 gfx::NativeWindow left_window = GetNativeWindowFromHWND(GetLeftFakeHwnd()); | |
610 EXPECT_EQ(left_display, screen->GetDisplayNearestWindow(left_window)); | |
611 | |
612 gfx::NativeWindow right_window = GetNativeWindowFromHWND(GetRightFakeHwnd()); | |
613 EXPECT_EQ(right_display, screen->GetDisplayNearestWindow(right_window)); | |
614 } | |
615 | |
616 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, GetDisplayNearestPoint) { | |
617 gfx::Screen* screen = GetScreen(); | |
618 const gfx::Display left_display = screen->GetAllDisplays()[0]; | |
619 const gfx::Display right_display = screen->GetAllDisplays()[1]; | |
620 | |
621 EXPECT_EQ(left_display, screen->GetDisplayNearestPoint(gfx::Point(0, 0))); | |
622 EXPECT_EQ(left_display, screen->GetDisplayNearestPoint(gfx::Point(125, 476))); | |
623 EXPECT_EQ(left_display, | |
624 screen->GetDisplayNearestPoint(gfx::Point(1599, 799))); | |
625 | |
626 EXPECT_EQ(right_display, screen->GetDisplayNearestPoint(gfx::Point(3200, 0))); | |
627 EXPECT_EQ(right_display, | |
628 screen->GetDisplayNearestPoint(gfx::Point(4000, 400))); | |
629 EXPECT_EQ(right_display, | |
630 screen->GetDisplayNearestPoint(gfx::Point(5119, 1199))); | |
631 } | |
632 | |
633 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, GetDisplayMatching) { | |
634 gfx::Screen* screen = GetScreen(); | |
635 const gfx::Display left_display = screen->GetAllDisplays()[0]; | |
636 const gfx::Display right_display = screen->GetAllDisplays()[1]; | |
637 | |
638 EXPECT_EQ(left_display, | |
639 screen->GetDisplayMatching(gfx::Rect(0, 0, 100, 100))); | |
640 EXPECT_EQ(left_display, | |
641 screen->GetDisplayMatching(gfx::Rect(1819, 1099, 100, 100))); | |
642 | |
643 EXPECT_EQ(right_display, | |
644 screen->GetDisplayMatching(gfx::Rect(6400, 0, 100, 100))); | |
645 EXPECT_EQ(right_display, | |
646 screen->GetDisplayMatching(gfx::Rect(10139, 2299, 100, 100))); | |
647 } | |
648 | |
649 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, GetPrimaryDisplay) { | |
650 gfx::Screen* screen = GetScreen(); | |
651 gfx::Display primary = screen->GetPrimaryDisplay(); | |
652 EXPECT_EQ(gfx::Point(0, 0), primary.bounds().origin()); | |
653 } | |
654 | |
80 } // namespace gfx | 655 } // namespace gfx |
OLD | NEW |