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

Side by Side Diff: ui/display/win/screen_win_unittest.cc

Issue 1825723002: Move ScreenWin to ui/display (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added is_chromeos guard Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/display/win/screen_win.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 #include <inttypes.h> 8 #include <inttypes.h>
9 #include <stddef.h> 9 #include <stddef.h>
10 10
11 #include <cwchar> 11 #include <cwchar>
12 #include <memory> 12 #include <memory>
13 #include <string> 13 #include <string>
14 #include <unordered_map> 14 #include <unordered_map>
15 #include <vector> 15 #include <vector>
16 16
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "ui/display/win/display_info.h"
20 #include "ui/display/win/screen_win_display.h"
19 #include "ui/gfx/display.h" 21 #include "ui/gfx/display.h"
20 #include "ui/gfx/geometry/rect.h" 22 #include "ui/gfx/geometry/rect.h"
21 #include "ui/gfx/screen.h" 23 #include "ui/gfx/screen.h"
22 #include "ui/gfx/test/display_util.h" 24 #include "ui/gfx/test/display_util.h"
23 #include "ui/gfx/win/display_info.h"
24 #include "ui/gfx/win/dpi.h" 25 #include "ui/gfx/win/dpi.h"
25 #include "ui/gfx/win/screen_win_display.h"
26 26
27 namespace gfx { 27 namespace display {
28 namespace win {
28 29
29 namespace { 30 namespace {
30 31
31 MONITORINFOEX CreateMonitorInfo(gfx::Rect monitor, 32 MONITORINFOEX CreateMonitorInfo(gfx::Rect monitor,
32 gfx::Rect work, 33 gfx::Rect work,
33 std::wstring device_name) { 34 std::wstring device_name) {
34 MONITORINFOEX monitor_info; 35 MONITORINFOEX monitor_info;
35 ::ZeroMemory(&monitor_info, sizeof(monitor_info)); 36 ::ZeroMemory(&monitor_info, sizeof(monitor_info));
36 monitor_info.cbSize = sizeof(monitor_info); 37 monitor_info.cbSize = sizeof(monitor_info);
37 monitor_info.rcMonitor = monitor.ToRECT(); 38 monitor_info.rcMonitor = monitor.ToRECT();
38 monitor_info.rcWork = work.ToRECT(); 39 monitor_info.rcWork = work.ToRECT();
39 size_t device_char_count = ARRAYSIZE(monitor_info.szDevice); 40 size_t device_char_count = ARRAYSIZE(monitor_info.szDevice);
40 wcsncpy(monitor_info.szDevice, device_name.c_str(), device_char_count); 41 wcsncpy(monitor_info.szDevice, device_name.c_str(), device_char_count);
41 monitor_info.szDevice[device_char_count-1] = L'\0'; 42 monitor_info.szDevice[device_char_count-1] = L'\0';
42 return monitor_info; 43 return monitor_info;
43 } 44 }
44 45
45 class TestScreenWin : public gfx::ScreenWin { 46 class TestScreenWin : public ScreenWin {
46 public: 47 public:
47 TestScreenWin(const std::vector<gfx::win::DisplayInfo>& display_infos, 48 TestScreenWin(const std::vector<DisplayInfo>& display_infos,
48 const std::vector<MONITORINFOEX>& monitor_infos, 49 const std::vector<MONITORINFOEX>& monitor_infos,
49 const std::unordered_map<HWND, gfx::Rect>& hwnd_map) 50 const std::unordered_map<HWND, gfx::Rect>& hwnd_map)
50 : monitor_infos_(monitor_infos), 51 : monitor_infos_(monitor_infos),
51 hwnd_map_(hwnd_map) { 52 hwnd_map_(hwnd_map) {
52 UpdateFromDisplayInfos(display_infos); 53 UpdateFromDisplayInfos(display_infos);
53 } 54 }
54 55
55 ~TestScreenWin() override = default; 56 ~TestScreenWin() override = default;
56 57
57 protected: 58 protected:
58 // gfx::ScreenWin: 59 // display::win::ScreenWin:
59 HWND GetHWNDFromNativeView(NativeView window) const override { 60 HWND GetHWNDFromNativeView(gfx::NativeView window) const override {
60 // NativeView is only used as an identifier in this tests, so interchange 61 // NativeView is only used as an identifier in this tests, so interchange
61 // NativeView with an HWND for convenience. 62 // NativeView with an HWND for convenience.
62 return reinterpret_cast<HWND>(window); 63 return reinterpret_cast<HWND>(window);
63 } 64 }
64 65
65 NativeWindow GetNativeWindowFromHWND(HWND hwnd) const override { 66 gfx::NativeWindow GetNativeWindowFromHWND(HWND hwnd) const override {
66 // NativeWindow is only used as an identifier in this tests, so interchange 67 // NativeWindow is only used as an identifier in this tests, so interchange
67 // an HWND for a NativeWindow for convenience. 68 // an HWND for a NativeWindow for convenience.
68 return reinterpret_cast<NativeWindow>(hwnd); 69 return reinterpret_cast<gfx::NativeWindow>(hwnd);
69 } 70 }
70 71
71 private: 72 private:
72 void Initialize() override {} 73 void Initialize() override {}
73 74
74 MONITORINFOEX MonitorInfoFromScreenPoint(const gfx::Point& screen_point) const 75 MONITORINFOEX MonitorInfoFromScreenPoint(const gfx::Point& screen_point) const
75 override { 76 override {
76 for (const MONITORINFOEX& monitor_info : monitor_infos_) { 77 for (const MONITORINFOEX& monitor_info : monitor_infos_) {
77 if (gfx::Rect(monitor_info.rcMonitor).Contains(screen_point)) 78 if (gfx::Rect(monitor_info.rcMonitor).Contains(screen_point))
78 return monitor_info; 79 return monitor_info;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 HWND GetRootWindow(HWND hwnd) const override { 121 HWND GetRootWindow(HWND hwnd) const override {
121 return hwnd; 122 return hwnd;
122 } 123 }
123 124
124 std::vector<MONITORINFOEX> monitor_infos_; 125 std::vector<MONITORINFOEX> monitor_infos_;
125 std::unordered_map<HWND, gfx::Rect> hwnd_map_; 126 std::unordered_map<HWND, gfx::Rect> hwnd_map_;
126 127
127 DISALLOW_COPY_AND_ASSIGN(TestScreenWin); 128 DISALLOW_COPY_AND_ASSIGN(TestScreenWin);
128 }; 129 };
129 130
130 Screen* GetScreen() { 131 gfx::Screen* GetScreen() {
131 return gfx::Screen::GetScreen(); 132 return gfx::Screen::GetScreen();
132 } 133 }
133 134
134 } // namespace
135
136 // Allows tests to specify the screen and associated state. 135 // Allows tests to specify the screen and associated state.
137 class TestScreenWinInitializer { 136 class TestScreenWinInitializer {
138 public: 137 public:
139 virtual void AddMonitor(const gfx::Rect& pixel_bounds, 138 virtual void AddMonitor(const gfx::Rect& pixel_bounds,
140 const gfx::Rect& pixel_work, 139 const gfx::Rect& pixel_work,
141 const wchar_t* device_name, 140 const wchar_t* device_name,
142 float device_scale_factor) = 0; 141 float device_scale_factor) = 0;
143 142
144 virtual HWND CreateFakeHwnd(const gfx::Rect& bounds) = 0; 143 virtual HWND CreateFakeHwnd(const gfx::Rect& bounds) = 0;
145 }; 144 };
146 145
147 class TestScreenWinManager : public TestScreenWinInitializer { 146 class TestScreenWinManager : public TestScreenWinInitializer {
148 public: 147 public:
149 TestScreenWinManager() = default; 148 TestScreenWinManager() = default;
150 149
151 ~TestScreenWinManager() { 150 ~TestScreenWinManager() {
152 gfx::Screen::SetScreenInstance(nullptr); 151 gfx::Screen::SetScreenInstance(nullptr);
153 } 152 }
154 153
155 void AddMonitor(const gfx::Rect& pixel_bounds, 154 void AddMonitor(const gfx::Rect& pixel_bounds,
156 const gfx::Rect& pixel_work, 155 const gfx::Rect& pixel_work,
157 const wchar_t* device_name, 156 const wchar_t* device_name,
158 float device_scale_factor) override { 157 float device_scale_factor) override {
159 MONITORINFOEX monitor_info = CreateMonitorInfo(pixel_bounds, 158 MONITORINFOEX monitor_info = CreateMonitorInfo(pixel_bounds,
160 pixel_work, 159 pixel_work,
161 device_name); 160 device_name);
162 monitor_infos_.push_back(monitor_info); 161 monitor_infos_.push_back(monitor_info);
163 display_infos_.push_back(gfx::win::DisplayInfo(monitor_info, 162 display_infos_.push_back(DisplayInfo(monitor_info,
164 device_scale_factor, 163 device_scale_factor,
165 gfx::Display::ROTATE_0)); 164 gfx::Display::ROTATE_0));
166 } 165 }
167 166
168 HWND CreateFakeHwnd(const gfx::Rect& bounds) override { 167 HWND CreateFakeHwnd(const gfx::Rect& bounds) override {
169 EXPECT_EQ(screen_win_, nullptr); 168 EXPECT_EQ(screen_win_, nullptr);
170 hwnd_map_.insert(std::pair<HWND, gfx::Rect>(++hwndLast_, bounds)); 169 hwnd_map_.insert(std::pair<HWND, gfx::Rect>(++hwndLast_, bounds));
171 return hwndLast_; 170 return hwndLast_;
172 } 171 }
173 172
174 void InitializeScreenWin() { 173 void InitializeScreenWin() {
175 ASSERT_EQ(screen_win_, nullptr); 174 ASSERT_EQ(screen_win_, nullptr);
176 screen_win_.reset(new TestScreenWin(display_infos_, 175 screen_win_.reset(new TestScreenWin(display_infos_,
177 monitor_infos_, 176 monitor_infos_,
178 hwnd_map_)); 177 hwnd_map_));
179 gfx::Screen::SetScreenInstance(screen_win_.get()); 178 gfx::Screen::SetScreenInstance(screen_win_.get());
180 } 179 }
181 180
182 ScreenWin* GetScreenWin() { 181 ScreenWin* GetScreenWin() {
183 return screen_win_.get(); 182 return screen_win_.get();
184 } 183 }
185 184
186 private: 185 private:
187 HWND hwndLast_ = nullptr; 186 HWND hwndLast_ = nullptr;
188 scoped_ptr<ScreenWin> screen_win_; 187 scoped_ptr<ScreenWin> screen_win_;
189 std::vector<MONITORINFOEX> monitor_infos_; 188 std::vector<MONITORINFOEX> monitor_infos_;
190 std::vector<gfx::win::DisplayInfo> display_infos_; 189 std::vector<DisplayInfo> display_infos_;
191 std::unordered_map<HWND, gfx::Rect> hwnd_map_; 190 std::unordered_map<HWND, gfx::Rect> hwnd_map_;
192 191
193 DISALLOW_COPY_AND_ASSIGN(TestScreenWinManager); 192 DISALLOW_COPY_AND_ASSIGN(TestScreenWinManager);
194 }; 193 };
195 194
196 class ScreenWinTest : public testing::Test { 195 class ScreenWinTest : public testing::Test {
197 protected: 196 protected:
198 ScreenWinTest() = default; 197 ScreenWinTest() = default;
199 198
200 void SetUp() override { 199 void SetUp() override {
201 testing::Test::SetUp(); 200 testing::Test::SetUp();
202 gfx::SetDefaultDeviceScaleFactor(1.0); 201 gfx::SetDefaultDeviceScaleFactor(1.0);
203 screen_win_initializer_.reset(new TestScreenWinManager()); 202 screen_win_initializer_.reset(new TestScreenWinManager());
204 SetUpScreen(screen_win_initializer_.get()); 203 SetUpScreen(screen_win_initializer_.get());
205 screen_win_initializer_->InitializeScreenWin(); 204 screen_win_initializer_->InitializeScreenWin();
206 } 205 }
207 206
208 void TearDown() override { 207 void TearDown() override {
209 screen_win_initializer_.reset(); 208 screen_win_initializer_.reset();
210 gfx::SetDefaultDeviceScaleFactor(1.0); 209 gfx::SetDefaultDeviceScaleFactor(1.0);
211 testing::Test::TearDown(); 210 testing::Test::TearDown();
212 } 211 }
213 212
214 virtual void SetUpScreen(TestScreenWinInitializer* initializer) = 0; 213 virtual void SetUpScreen(TestScreenWinInitializer* initializer) = 0;
215 214
216 NativeWindow GetNativeWindowFromHWND(HWND hwnd) const { 215 gfx::NativeWindow GetNativeWindowFromHWND(HWND hwnd) const {
217 ScreenWin* screen_win = screen_win_initializer_->GetScreenWin(); 216 ScreenWin* screen_win = screen_win_initializer_->GetScreenWin();
218 return screen_win->GetNativeWindowFromHWND(hwnd);; 217 return screen_win->GetNativeWindowFromHWND(hwnd);;
219 } 218 }
220 219
221 private: 220 private:
222 scoped_ptr<TestScreenWinManager> screen_win_initializer_; 221 scoped_ptr<TestScreenWinManager> screen_win_initializer_;
223 222
224 DISALLOW_COPY_AND_ASSIGN(ScreenWinTest); 223 DISALLOW_COPY_AND_ASSIGN(ScreenWinTest);
225 }; 224 };
226 225
(...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after
772 EXPECT_EQ(right_display, 771 EXPECT_EQ(right_display,
773 screen->GetDisplayMatching(gfx::Rect(10139, 2299, 100, 100))); 772 screen->GetDisplayMatching(gfx::Rect(10139, 2299, 100, 100)));
774 } 773 }
775 774
776 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, GetPrimaryDisplay) { 775 TEST_F(ScreenWinTestTwoDisplays2x1xVirtualized, GetPrimaryDisplay) {
777 gfx::Screen* screen = GetScreen(); 776 gfx::Screen* screen = GetScreen();
778 gfx::Display primary = screen->GetPrimaryDisplay(); 777 gfx::Display primary = screen->GetPrimaryDisplay();
779 EXPECT_EQ(gfx::Point(0, 0), primary.bounds().origin()); 778 EXPECT_EQ(gfx::Point(0, 0), primary.bounds().origin());
780 } 779 }
781 780
782 } // namespace gfx 781 } // namespace
782
783 } // namespace win
784 } // namespace display
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698