| 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 <cwchar> |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 7 #include <windows.h> | 11 #include <windows.h> |
| 8 #include <inttypes.h> | |
| 9 #include <stddef.h> | 12 #include <stddef.h> |
| 10 | 13 |
| 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" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "ui/gfx/display.h" | 15 #include "ui/gfx/display.h" |
| 20 #include "ui/gfx/geometry/rect.h" | 16 #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" | 17 #include "ui/gfx/win/dpi.h" |
| 25 #include "ui/gfx/win/screen_win_display.h" | |
| 26 | 18 |
| 27 namespace gfx { | 19 namespace gfx { |
| 28 | 20 |
| 29 namespace { | 21 namespace { |
| 30 | 22 |
| 31 MONITORINFOEX CreateMonitorInfo(gfx::Rect monitor, | 23 MONITORINFOEX CreateMonitorInfo(gfx::Rect monitor, |
| 32 gfx::Rect work, | 24 gfx::Rect work, |
| 33 std::wstring device_name) { | 25 std::wstring device_name) { |
| 34 MONITORINFOEX monitor_info; | 26 MONITORINFOEX monitor_info; |
| 35 ::ZeroMemory(&monitor_info, sizeof(monitor_info)); | 27 ::ZeroMemory(&monitor_info, sizeof(monitor_info)); |
| 36 monitor_info.cbSize = sizeof(monitor_info); | 28 monitor_info.cbSize = sizeof(monitor_info); |
| 37 monitor_info.rcMonitor = monitor.ToRECT(); | 29 monitor_info.rcMonitor = monitor.ToRECT(); |
| 38 monitor_info.rcWork = work.ToRECT(); | 30 monitor_info.rcWork = work.ToRECT(); |
| 39 size_t device_char_count = ARRAYSIZE(monitor_info.szDevice); | 31 size_t device_char_count = ARRAYSIZE(monitor_info.szDevice); |
| 40 wcsncpy(monitor_info.szDevice, device_name.c_str(), device_char_count); | 32 wcsncpy(monitor_info.szDevice, device_name.c_str(), device_char_count); |
| 41 monitor_info.szDevice[device_char_count-1] = L'\0'; | 33 monitor_info.szDevice[device_char_count-1] = L'\0'; |
| 42 return monitor_info; | 34 return monitor_info; |
| 43 } | 35 } |
| 44 | 36 |
| 45 class TestScreenWin : public gfx::ScreenWin { | |
| 46 public: | |
| 47 TestScreenWin(const std::vector<const 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() = 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, 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 | 37 } // namespace |
| 135 | 38 |
| 136 // Allows tests to specify the screen and associated state. | 39 class ScreenWinTest : public testing::Test { |
| 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: | 40 private: |
| 187 HWND hwndLast_ = nullptr; | |
| 188 scoped_ptr<ScreenWin> screen_win_; | |
| 189 std::vector<MONITORINFOEX> monitor_infos_; | |
| 190 std::vector<const 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 { | 41 void SetUp() override { |
| 201 testing::Test::SetUp(); | 42 testing::Test::SetUp(); |
| 202 gfx::SetDefaultDeviceScaleFactor(1.0); | 43 gfx::SetDefaultDeviceScaleFactor(1.0); |
| 203 screen_win_initializer_.reset(new TestScreenWinManager()); | |
| 204 SetUpScreen(screen_win_initializer_.get()); | |
| 205 screen_win_initializer_->InitializeScreenWin(); | |
| 206 } | 44 } |
| 207 | 45 |
| 208 void TearDown() override { | 46 void TearDown() override { |
| 209 screen_win_initializer_.reset(); | |
| 210 gfx::SetDefaultDeviceScaleFactor(1.0); | 47 gfx::SetDefaultDeviceScaleFactor(1.0); |
| 211 testing::Test::TearDown(); | 48 testing::Test::TearDown(); |
| 212 } | 49 } |
| 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 }; | 50 }; |
| 226 | 51 |
| 227 // Single Display of 1.0 Device Scale Factor. | 52 TEST_F(ScreenWinTest, SingleDisplay1x) { |
| 228 class ScreenWinTestSingleDisplay1x : public ScreenWinTest { | 53 std::vector<MONITORINFOEX> monitor_infos; |
| 229 public: | 54 monitor_infos.push_back(CreateMonitorInfo(gfx::Rect(0, 0, 1920, 1200), |
| 230 ScreenWinTestSingleDisplay1x() = default; | 55 gfx::Rect(0, 0, 1920, 1100), |
| 56 L"primary")); |
| 57 std::vector<gfx::Display> displays = |
| 58 ScreenWin::GetDisplaysForMonitorInfos(monitor_infos); |
| 231 | 59 |
| 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 | |
| 248 TEST_F(ScreenWinTestSingleDisplay1x, GetDisplays) { | |
| 249 std::vector<gfx::Display> displays = GetScreen()->GetAllDisplays(); | |
| 250 ASSERT_EQ(1u, displays.size()); | 60 ASSERT_EQ(1u, displays.size()); |
| 251 EXPECT_EQ(gfx::Rect(0, 0, 1920, 1200), displays[0].bounds()); | 61 EXPECT_EQ(gfx::Rect(0, 0, 1920, 1200), displays[0].bounds()); |
| 252 EXPECT_EQ(gfx::Rect(0, 0, 1920, 1100), displays[0].work_area()); | 62 EXPECT_EQ(gfx::Rect(0, 0, 1920, 1100), displays[0].work_area()); |
| 253 } | 63 } |
| 254 | 64 |
| 255 TEST_F(ScreenWinTestSingleDisplay1x, GetNumDisplays) { | 65 TEST_F(ScreenWinTest, SingleDisplay2x) { |
| 256 EXPECT_EQ(1, GetScreen()->GetNumDisplays()); | 66 gfx::SetDefaultDeviceScaleFactor(2.0); |
| 257 } | |
| 258 | 67 |
| 259 TEST_F(ScreenWinTestSingleDisplay1x, GetDisplayNearestWindowPrimaryDisplay) { | 68 std::vector<MONITORINFOEX> monitor_infos; |
| 260 gfx::Screen* screen = GetScreen(); | 69 monitor_infos.push_back(CreateMonitorInfo(gfx::Rect(0, 0, 1920, 1200), |
| 261 EXPECT_EQ(screen->GetPrimaryDisplay(), | 70 gfx::Rect(0, 0, 1920, 1100), |
| 262 screen->GetDisplayNearestWindow(nullptr)); | 71 L"primary")); |
| 263 } | 72 std::vector<gfx::Display> displays = |
| 73 ScreenWin::GetDisplaysForMonitorInfos(monitor_infos); |
| 264 | 74 |
| 265 TEST_F(ScreenWinTestSingleDisplay1x, GetDisplayNearestWindow) { | |
| 266 gfx::Screen* screen = GetScreen(); | |
| 267 gfx::NativeWindow native_window = GetNativeWindowFromHWND(GetFakeHwnd()); | |
| 268 EXPECT_EQ(screen->GetAllDisplays()[0], | |
| 269 screen->GetDisplayNearestWindow(native_window)); | |
| 270 } | |
| 271 | |
| 272 TEST_F(ScreenWinTestSingleDisplay1x, GetDisplayNearestPoint) { | |
| 273 gfx::Screen* screen = GetScreen(); | |
| 274 gfx::Display display = screen->GetAllDisplays()[0]; | |
| 275 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(0, 0))); | |
| 276 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(250, 952))); | |
| 277 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(1919, 1199))); | |
| 278 } | |
| 279 | |
| 280 TEST_F(ScreenWinTestSingleDisplay1x, GetDisplayMatching) { | |
| 281 gfx::Screen* screen = GetScreen(); | |
| 282 gfx::Display display = screen->GetAllDisplays()[0]; | |
| 283 EXPECT_EQ(display, screen->GetDisplayMatching(gfx::Rect(0, 0, 100, 100))); | |
| 284 EXPECT_EQ(display, | |
| 285 screen->GetDisplayMatching(gfx::Rect(1819, 1099, 100, 100))); | |
| 286 } | |
| 287 | |
| 288 TEST_F(ScreenWinTestSingleDisplay1x, GetPrimaryDisplay) { | |
| 289 gfx::Screen* screen = GetScreen(); | |
| 290 EXPECT_EQ(gfx::Point(0, 0), screen->GetPrimaryDisplay().bounds().origin()); | |
| 291 } | |
| 292 | |
| 293 // Single Display of 2.0 Device Scale Factor. | |
| 294 class ScreenWinTestSingleDisplay2x : public ScreenWinTest { | |
| 295 public: | |
| 296 ScreenWinTestSingleDisplay2x() = default; | |
| 297 | |
| 298 void SetUpScreen(TestScreenWinInitializer* initializer) override { | |
| 299 gfx::SetDefaultDeviceScaleFactor(2.0); | |
| 300 initializer->AddMonitor(gfx::Rect(0, 0, 1920, 1200), | |
| 301 gfx::Rect(0, 0, 1920, 1100), | |
| 302 L"primary", | |
| 303 2.0); | |
| 304 fake_hwnd_ = initializer->CreateFakeHwnd(gfx::Rect(0, 0, 1920, 1100)); | |
| 305 } | |
| 306 | |
| 307 HWND GetFakeHwnd() { | |
| 308 return fake_hwnd_; | |
| 309 } | |
| 310 | |
| 311 private: | |
| 312 HWND fake_hwnd_ = nullptr; | |
| 313 | |
| 314 DISALLOW_COPY_AND_ASSIGN(ScreenWinTestSingleDisplay2x); | |
| 315 }; | |
| 316 | |
| 317 TEST_F(ScreenWinTestSingleDisplay2x, GetDisplays) { | |
| 318 std::vector<gfx::Display> displays = GetScreen()->GetAllDisplays(); | |
| 319 ASSERT_EQ(1u, displays.size()); | 75 ASSERT_EQ(1u, displays.size()); |
| 320 EXPECT_EQ(gfx::Rect(0, 0, 960, 600), displays[0].bounds()); | 76 EXPECT_EQ(gfx::Rect(0, 0, 960, 600), displays[0].bounds()); |
| 321 EXPECT_EQ(gfx::Rect(0, 0, 960, 550), displays[0].work_area()); | 77 EXPECT_EQ(gfx::Rect(0, 0, 960, 550), displays[0].work_area()); |
| 322 } | 78 } |
| 323 | 79 |
| 324 TEST_F(ScreenWinTestSingleDisplay2x, GetDisplayNearestPoint) { | |
| 325 gfx::Screen* screen = GetScreen(); | |
| 326 gfx::Display display = screen->GetAllDisplays()[0]; | |
| 327 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(0, 0))); | |
| 328 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(125, 476))); | |
| 329 EXPECT_EQ(display, screen->GetDisplayNearestPoint(gfx::Point(959, 599))); | |
| 330 } | |
| 331 | |
| 332 TEST_F(ScreenWinTestSingleDisplay2x, GetDisplayMatching) { | |
| 333 gfx::Screen* screen = GetScreen(); | |
| 334 gfx::Display display = screen->GetAllDisplays()[0]; | |
| 335 EXPECT_EQ(display, screen->GetDisplayMatching(gfx::Rect(0, 0, 100, 100))); | |
| 336 EXPECT_EQ(display, | |
| 337 screen->GetDisplayMatching(gfx::Rect(1819, 1099, 100, 100))); | |
| 338 } | |
| 339 | |
| 340 // Two Displays of 1.0 Device Scale Factor. | |
| 341 class ScreenWinTestTwoDisplays1x : public ScreenWinTest { | |
| 342 public: | |
| 343 ScreenWinTestTwoDisplays1x() = default; | |
| 344 | |
| 345 void SetUpScreen(TestScreenWinInitializer* initializer) override { | |
| 346 initializer->AddMonitor(gfx::Rect(0, 0, 1920, 1200), | |
| 347 gfx::Rect(0, 0, 1920, 1100), | |
| 348 L"primary", | |
| 349 1.0); | |
| 350 initializer->AddMonitor(gfx::Rect(1920, 0, 800, 600), | |
| 351 gfx::Rect(1920, 0, 800, 600), | |
| 352 L"secondary", | |
| 353 1.0); | |
| 354 fake_hwnd_left_ = initializer->CreateFakeHwnd(gfx::Rect(0, 0, 1920, 1100)); | |
| 355 fake_hwnd_right_ = | |
| 356 initializer->CreateFakeHwnd(gfx::Rect(1920, 0, 800, 600)); | |
| 357 } | |
| 358 | |
| 359 HWND GetLeftFakeHwnd() { | |
| 360 return fake_hwnd_left_; | |
| 361 } | |
| 362 | |
| 363 HWND GetRightFakeHwnd() { | |
| 364 return fake_hwnd_right_; | |
| 365 } | |
| 366 | |
| 367 private: | |
| 368 HWND fake_hwnd_left_ = nullptr; | |
| 369 HWND fake_hwnd_right_ = nullptr; | |
| 370 | |
| 371 DISALLOW_COPY_AND_ASSIGN(ScreenWinTestTwoDisplays1x); | |
| 372 }; | |
| 373 | |
| 374 TEST_F(ScreenWinTestTwoDisplays1x, GetDisplays) { | |
| 375 std::vector<gfx::Display> displays = GetScreen()->GetAllDisplays(); | |
| 376 ASSERT_EQ(2u, displays.size()); | |
| 377 EXPECT_EQ(gfx::Rect(0, 0, 1920, 1200), displays[0].bounds()); | |
| 378 EXPECT_EQ(gfx::Rect(0, 0, 1920, 1100), displays[0].work_area()); | |
| 379 EXPECT_EQ(gfx::Rect(1920, 0, 800, 600), displays[1].bounds()); | |
| 380 EXPECT_EQ(gfx::Rect(1920, 0, 800, 600), displays[1].work_area()); | |
| 381 } | |
| 382 | |
| 383 TEST_F(ScreenWinTestTwoDisplays1x, GetNumDisplays) { | |
| 384 EXPECT_EQ(2, GetScreen()->GetNumDisplays()); | |
| 385 } | |
| 386 | |
| 387 TEST_F(ScreenWinTestTwoDisplays1x, GetDisplayNearestWindowPrimaryDisplay) { | |
| 388 gfx::Screen* screen = GetScreen(); | |
| 389 EXPECT_EQ(screen->GetPrimaryDisplay(), | |
| 390 screen->GetDisplayNearestWindow(nullptr)); | |
| 391 } | |
| 392 | |
| 393 TEST_F(ScreenWinTestTwoDisplays1x, GetDisplayNearestWindow) { | |
| 394 gfx::Screen* screen = GetScreen(); | |
| 395 const gfx::Display left_display = screen->GetAllDisplays()[0]; | |
| 396 const gfx::Display right_display = screen->GetAllDisplays()[1]; | |
| 397 | |
| 398 gfx::NativeWindow left_window = GetNativeWindowFromHWND(GetLeftFakeHwnd()); | |
| 399 EXPECT_EQ(left_display, screen->GetDisplayNearestWindow(left_window)); | |
| 400 | |
| 401 gfx::NativeWindow right_window = GetNativeWindowFromHWND(GetRightFakeHwnd()); | |
| 402 EXPECT_EQ(right_display, screen->GetDisplayNearestWindow(right_window)); | |
| 403 } | |
| 404 | |
| 405 TEST_F(ScreenWinTestTwoDisplays1x, GetDisplayNearestPoint) { | |
| 406 gfx::Screen* screen = GetScreen(); | |
| 407 const gfx::Display left_display = screen->GetAllDisplays()[0]; | |
| 408 const gfx::Display right_display = screen->GetAllDisplays()[1]; | |
| 409 | |
| 410 EXPECT_EQ(left_display, screen->GetDisplayNearestPoint(gfx::Point(0, 0))); | |
| 411 EXPECT_EQ(left_display, screen->GetDisplayNearestPoint(gfx::Point(250, 952))); | |
| 412 EXPECT_EQ(left_display, | |
| 413 screen->GetDisplayNearestPoint(gfx::Point(1919, 1199))); | |
| 414 | |
| 415 EXPECT_EQ(right_display, screen->GetDisplayNearestPoint(gfx::Point(1920, 0))); | |
| 416 EXPECT_EQ(right_display, | |
| 417 screen->GetDisplayNearestPoint(gfx::Point(2000, 400))); | |
| 418 EXPECT_EQ(right_display, | |
| 419 screen->GetDisplayNearestPoint(gfx::Point(2719, 599))); | |
| 420 } | |
| 421 | |
| 422 TEST_F(ScreenWinTestTwoDisplays1x, GetDisplayMatching) { | |
| 423 gfx::Screen* screen = GetScreen(); | |
| 424 const gfx::Display left_display = screen->GetAllDisplays()[0]; | |
| 425 const gfx::Display right_display = screen->GetAllDisplays()[1]; | |
| 426 | |
| 427 EXPECT_EQ(left_display, | |
| 428 screen->GetDisplayMatching(gfx::Rect(0, 0, 100, 100))); | |
| 429 EXPECT_EQ(left_display, | |
| 430 screen->GetDisplayMatching(gfx::Rect(1819, 1099, 100, 100))); | |
| 431 | |
| 432 EXPECT_EQ(right_display, | |
| 433 screen->GetDisplayMatching(gfx::Rect(1920, 0, 100, 100))); | |
| 434 EXPECT_EQ(right_display, | |
| 435 screen->GetDisplayMatching(gfx::Rect(2619, 499, 100, 100))); | |
| 436 } | |
| 437 | |
| 438 TEST_F(ScreenWinTestTwoDisplays1x, GetPrimaryDisplay) { | |
| 439 gfx::Screen* screen = GetScreen(); | |
| 440 gfx::Display primary = screen->GetPrimaryDisplay(); | |
| 441 EXPECT_EQ(gfx::Point(0, 0), primary.bounds().origin()); | |
| 442 } | |
| 443 | |
| 444 // Two Displays of 2.0 Device Scale Factor. | |
| 445 class ScreenWinTestTwoDisplays2x : public ScreenWinTest { | |
| 446 public: | |
| 447 ScreenWinTestTwoDisplays2x() = default; | |
| 448 | |
| 449 void SetUpScreen(TestScreenWinInitializer* initializer) override { | |
| 450 gfx::SetDefaultDeviceScaleFactor(2.0); | |
| 451 initializer->AddMonitor(gfx::Rect(0, 0, 1920, 1200), | |
| 452 gfx::Rect(0, 0, 1920, 1100), | |
| 453 L"primary", | |
| 454 2.0); | |
| 455 initializer->AddMonitor(gfx::Rect(1920, 0, 800, 600), | |
| 456 gfx::Rect(1920, 0, 800, 600), | |
| 457 L"secondary", | |
| 458 2.0); | |
| 459 fake_hwnd_left_ = initializer->CreateFakeHwnd(gfx::Rect(0, 0, 1920, 1100)); | |
| 460 fake_hwnd_right_ = | |
| 461 initializer->CreateFakeHwnd(gfx::Rect(1920, 0, 800, 600)); | |
| 462 } | |
| 463 | |
| 464 HWND GetLeftFakeHwnd() { | |
| 465 return fake_hwnd_left_; | |
| 466 } | |
| 467 | |
| 468 HWND GetRightFakeHwnd() { | |
| 469 return fake_hwnd_right_; | |
| 470 } | |
| 471 | |
| 472 private: | |
| 473 HWND fake_hwnd_left_ = nullptr; | |
| 474 HWND fake_hwnd_right_ = nullptr; | |
| 475 | |
| 476 DISALLOW_COPY_AND_ASSIGN(ScreenWinTestTwoDisplays2x); | |
| 477 }; | |
| 478 | |
| 479 TEST_F(ScreenWinTestTwoDisplays2x, GetDisplays) { | |
| 480 std::vector<gfx::Display> displays = GetScreen()->GetAllDisplays(); | |
| 481 ASSERT_EQ(2u, displays.size()); | |
| 482 EXPECT_EQ(gfx::Rect(0, 0, 960, 600), displays[0].bounds()); | |
| 483 EXPECT_EQ(gfx::Rect(0, 0, 960, 550), displays[0].work_area()); | |
| 484 EXPECT_EQ(gfx::Rect(960, 0, 400, 300), displays[1].bounds()); | |
| 485 EXPECT_EQ(gfx::Rect(960, 0, 400, 300), displays[1].work_area()); | |
| 486 } | |
| 487 | |
| 488 TEST_F(ScreenWinTestTwoDisplays2x, GetDisplayNearestWindowPrimaryDisplay) { | |
| 489 gfx::Screen* screen = GetScreen(); | |
| 490 EXPECT_EQ(screen->GetPrimaryDisplay(), | |
| 491 screen->GetDisplayNearestWindow(nullptr)); | |
| 492 } | |
| 493 | |
| 494 TEST_F(ScreenWinTestTwoDisplays2x, GetDisplayNearestWindow) { | |
| 495 gfx::Screen* screen = GetScreen(); | |
| 496 const gfx::Display left_display = screen->GetAllDisplays()[0]; | |
| 497 const gfx::Display right_display = screen->GetAllDisplays()[1]; | |
| 498 | |
| 499 gfx::NativeWindow left_window = GetNativeWindowFromHWND(GetLeftFakeHwnd()); | |
| 500 EXPECT_EQ(left_display, screen->GetDisplayNearestWindow(left_window)); | |
| 501 | |
| 502 gfx::NativeWindow right_window = GetNativeWindowFromHWND(GetRightFakeHwnd()); | |
| 503 EXPECT_EQ(right_display, screen->GetDisplayNearestWindow(right_window)); | |
| 504 } | |
| 505 | |
| 506 TEST_F(ScreenWinTestTwoDisplays2x, GetDisplayNearestPoint) { | |
| 507 gfx::Screen* screen = GetScreen(); | |
| 508 const gfx::Display left_display = screen->GetAllDisplays()[0]; | |
| 509 const gfx::Display right_display = screen->GetAllDisplays()[1]; | |
| 510 | |
| 511 EXPECT_EQ(left_display, screen->GetDisplayNearestPoint(gfx::Point(0, 0))); | |
| 512 EXPECT_EQ(left_display, screen->GetDisplayNearestPoint(gfx::Point(125, 476))); | |
| 513 EXPECT_EQ(left_display, | |
| 514 screen->GetDisplayNearestPoint(gfx::Point(959, 599))); | |
| 515 | |
| 516 EXPECT_EQ(right_display, screen->GetDisplayNearestPoint(gfx::Point(960, 0))); | |
| 517 EXPECT_EQ(right_display, | |
| 518 screen->GetDisplayNearestPoint(gfx::Point(1000, 200))); | |
| 519 EXPECT_EQ(right_display, | |
| 520 screen->GetDisplayNearestPoint(gfx::Point(1359, 299))); | |
| 521 } | |
| 522 | |
| 523 TEST_F(ScreenWinTestTwoDisplays2x, GetDisplayMatching) { | |
| 524 gfx::Screen* screen = GetScreen(); | |
| 525 const gfx::Display left_display = screen->GetAllDisplays()[0]; | |
| 526 const gfx::Display right_display = screen->GetAllDisplays()[1]; | |
| 527 | |
| 528 EXPECT_EQ(left_display, | |
| 529 screen->GetDisplayMatching(gfx::Rect(0, 0, 100, 100))); | |
| 530 EXPECT_EQ(left_display, | |
| 531 screen->GetDisplayMatching(gfx::Rect(1819, 1099, 100, 100))); | |
| 532 | |
| 533 EXPECT_EQ(right_display, | |
| 534 screen->GetDisplayMatching(gfx::Rect(1920, 0, 100, 100))); | |
| 535 EXPECT_EQ(right_display, | |
| 536 screen->GetDisplayMatching(gfx::Rect(2619, 499, 100, 100))); | |
| 537 } | |
| 538 | |
| 539 TEST_F(ScreenWinTestTwoDisplays2x, GetPrimaryDisplay) { | |
| 540 gfx::Screen* screen = GetScreen(); | |
| 541 gfx::Display primary = screen->GetPrimaryDisplay(); | |
| 542 EXPECT_EQ(gfx::Point(0, 0), primary.bounds().origin()); | |
| 543 } | |
| 544 | |
| 545 // Two Displays of 2.0 (Left) and 1.0 (Right) Device Scale Factor under | |
| 546 // Windows DPI Virtualization. Note that the displays do not form a euclidean | |
| 547 // space. | |
| 548 class ScreenWinTestTwoDisplays2x1xVirtualized : public ScreenWinTest { | |
| 549 public: | |
| 550 ScreenWinTestTwoDisplays2x1xVirtualized() = default; | |
| 551 | |
| 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_ = nullptr; | |
| 577 HWND fake_hwnd_right_ = nullptr; | |
| 578 | |
| 579 DISALLOW_COPY_AND_ASSIGN(ScreenWinTestTwoDisplays2x1xVirtualized); | |
| 580 }; | |
| 581 | |
| 582 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, GetDisplays) { | |
| 583 std::vector<gfx::Display> displays = GetScreen()->GetAllDisplays(); | |
| 584 ASSERT_EQ(2u, displays.size()); | |
| 585 EXPECT_EQ(gfx::Rect(0, 0, 1600, 800), displays[0].bounds()); | |
| 586 EXPECT_EQ(gfx::Rect(0, 0, 1600, 750), displays[0].work_area()); | |
| 587 EXPECT_EQ(gfx::Rect(3200, 0, 1920, 1200), displays[1].bounds()); | |
| 588 EXPECT_EQ(gfx::Rect(3200, 0, 1920, 1200), displays[1].work_area()); | |
| 589 } | |
| 590 | |
| 591 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, GetNumDisplays) { | |
| 592 EXPECT_EQ(2, GetScreen()->GetNumDisplays()); | |
| 593 } | |
| 594 | |
| 595 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, | |
| 596 GetDisplayNearestWindowPrimaryDisplay) { | |
| 597 gfx::Screen* screen = GetScreen(); | |
| 598 EXPECT_EQ(screen->GetPrimaryDisplay(), | |
| 599 screen->GetDisplayNearestWindow(nullptr)); | |
| 600 } | |
| 601 | |
| 602 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, GetDisplayNearestWindow) { | |
| 603 gfx::Screen* screen = GetScreen(); | |
| 604 const gfx::Display left_display = screen->GetAllDisplays()[0]; | |
| 605 const gfx::Display right_display = screen->GetAllDisplays()[1]; | |
| 606 | |
| 607 gfx::NativeWindow left_window = GetNativeWindowFromHWND(GetLeftFakeHwnd()); | |
| 608 EXPECT_EQ(left_display, screen->GetDisplayNearestWindow(left_window)); | |
| 609 | |
| 610 gfx::NativeWindow right_window = GetNativeWindowFromHWND(GetRightFakeHwnd()); | |
| 611 EXPECT_EQ(right_display, screen->GetDisplayNearestWindow(right_window)); | |
| 612 } | |
| 613 | |
| 614 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, GetDisplayNearestPoint) { | |
| 615 gfx::Screen* screen = GetScreen(); | |
| 616 const gfx::Display left_display = screen->GetAllDisplays()[0]; | |
| 617 const gfx::Display right_display = screen->GetAllDisplays()[1]; | |
| 618 | |
| 619 EXPECT_EQ(left_display, screen->GetDisplayNearestPoint(gfx::Point(0, 0))); | |
| 620 EXPECT_EQ(left_display, screen->GetDisplayNearestPoint(gfx::Point(125, 476))); | |
| 621 EXPECT_EQ(left_display, | |
| 622 screen->GetDisplayNearestPoint(gfx::Point(1599, 799))); | |
| 623 | |
| 624 EXPECT_EQ(right_display, screen->GetDisplayNearestPoint(gfx::Point(3200, 0))); | |
| 625 EXPECT_EQ(right_display, | |
| 626 screen->GetDisplayNearestPoint(gfx::Point(4000, 400))); | |
| 627 EXPECT_EQ(right_display, | |
| 628 screen->GetDisplayNearestPoint(gfx::Point(5119, 1199))); | |
| 629 } | |
| 630 | |
| 631 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, GetDisplayMatching) { | |
| 632 gfx::Screen* screen = GetScreen(); | |
| 633 const gfx::Display left_display = screen->GetAllDisplays()[0]; | |
| 634 const gfx::Display right_display = screen->GetAllDisplays()[1]; | |
| 635 | |
| 636 EXPECT_EQ(left_display, | |
| 637 screen->GetDisplayMatching(gfx::Rect(0, 0, 100, 100))); | |
| 638 EXPECT_EQ(left_display, | |
| 639 screen->GetDisplayMatching(gfx::Rect(1819, 1099, 100, 100))); | |
| 640 | |
| 641 EXPECT_EQ(right_display, | |
| 642 screen->GetDisplayMatching(gfx::Rect(6400, 0, 100, 100))); | |
| 643 EXPECT_EQ(right_display, | |
| 644 screen->GetDisplayMatching(gfx::Rect(10139, 2299, 100, 100))); | |
| 645 } | |
| 646 | |
| 647 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, GetPrimaryDisplay) { | |
| 648 gfx::Screen* screen = GetScreen(); | |
| 649 gfx::Display primary = screen->GetPrimaryDisplay(); | |
| 650 EXPECT_EQ(gfx::Point(0, 0), primary.bounds().origin()); | |
| 651 } | |
| 652 | |
| 653 } // namespace gfx | 80 } // namespace gfx |
| OLD | NEW |