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

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

Powered by Google App Engine
This is Rietveld 408576698