2 * Unit tests for monitor APIs
4 * Copyright 2005 Huw Davies
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/test.h"
27 static LONG (WINAPI
*pChangeDisplaySettingsExA
)(LPCSTR
, LPDEVMODEA
, HWND
, DWORD
, LPVOID
);
28 static LONG (WINAPI
*pChangeDisplaySettingsExW
)(LPCWSTR
, LPDEVMODEW
, HWND
, DWORD
, LPVOID
);
29 static BOOL (WINAPI
*pEnumDisplayDevicesA
)(LPCSTR
,DWORD
,LPDISPLAY_DEVICEA
,DWORD
);
30 static BOOL (WINAPI
*pEnumDisplayMonitors
)(HDC
,LPRECT
,MONITORENUMPROC
,LPARAM
);
31 static BOOL (WINAPI
*pGetMonitorInfoA
)(HMONITOR
,LPMONITORINFO
);
32 static HMONITOR (WINAPI
*pMonitorFromPoint
)(POINT
,DWORD
);
33 static HMONITOR (WINAPI
*pMonitorFromWindow
)(HWND
,DWORD
);
35 static void init_function_pointers(void)
37 hdll
= GetModuleHandleA("user32.dll");
39 #define GET_PROC(func) \
40 p ## func = (void*)GetProcAddress(hdll, #func); \
42 trace("GetProcAddress(%s) failed\n", #func);
44 GET_PROC(ChangeDisplaySettingsExA
)
45 GET_PROC(ChangeDisplaySettingsExW
)
46 GET_PROC(EnumDisplayDevicesA
)
47 GET_PROC(EnumDisplayMonitors
)
48 GET_PROC(GetMonitorInfoA
)
49 GET_PROC(MonitorFromPoint
)
50 GET_PROC(MonitorFromWindow
)
55 static BOOL CALLBACK
monitor_enum_proc(HMONITOR hmon
, HDC hdc
, LPRECT lprc
,
59 char *primary
= (char *)lparam
;
61 mi
.cbSize
= sizeof(mi
);
63 ok(pGetMonitorInfoA(hmon
, (MONITORINFO
*)&mi
), "GetMonitorInfo failed\n");
64 if(mi
.dwFlags
== MONITORINFOF_PRIMARY
)
65 strcpy(primary
, mi
.szDevice
);
70 static void test_enumdisplaydevices(void)
73 char primary_device_name
[32];
74 char primary_monitor_device_name
[32];
75 DWORD primary_num
= -1, num
= 0;
78 if(pEnumDisplayDevicesA
== NULL
) return;
83 ret
= pEnumDisplayDevicesA(NULL
, num
, &dd
, 0);
84 ok(ret
|| num
!= 0, "EnumDisplayDevices fails with num == 0\n");
86 if(dd
.StateFlags
& DISPLAY_DEVICE_PRIMARY_DEVICE
)
88 strcpy(primary_device_name
, dd
.DeviceName
);
91 if(dd
.StateFlags
& DISPLAY_DEVICE_ATTACHED_TO_DESKTOP
)
93 /* test creating DC */
94 dc
= CreateDCA(dd
.DeviceName
, NULL
, NULL
, NULL
);
95 ok(dc
!= NULL
, "Failed to CreateDC(\"%s\") err=%d\n", dd
.DeviceName
, GetLastError());
100 ok(primary_num
!= -1, "Didn't get the primary device\n");
102 if(pEnumDisplayMonitors
&& pGetMonitorInfoA
) {
103 ok(pEnumDisplayMonitors(NULL
, NULL
, monitor_enum_proc
, (LPARAM
)primary_monitor_device_name
),
104 "EnumDisplayMonitors failed\n");
106 ok(!strcmp(primary_monitor_device_name
, primary_device_name
),
107 "monitor device name %s, device name %s\n", primary_monitor_device_name
,
108 primary_device_name
);
114 DWORD w
, h
, bpp
, freq
, fields
;
118 static const struct vid_mode vid_modes_test
[] = {
119 {640, 480, 0, 0, DM_PELSWIDTH
| DM_PELSHEIGHT
| DM_BITSPERPEL
| DM_DISPLAYFREQUENCY
, 1},
120 {640, 480, 0, 0, DM_PELSWIDTH
| DM_PELSHEIGHT
| DM_DISPLAYFREQUENCY
, 1},
121 {640, 480, 0, 0, DM_PELSWIDTH
| DM_PELSHEIGHT
| DM_BITSPERPEL
, 1},
122 {640, 480, 0, 0, DM_PELSWIDTH
| DM_PELSHEIGHT
, 1},
123 {640, 480, 0, 0, DM_BITSPERPEL
, 1},
124 {640, 480, 0, 0, DM_DISPLAYFREQUENCY
, 1},
126 {0, 0, 0, 0, DM_PELSWIDTH
, 1},
127 {0, 0, 0, 0, DM_PELSHEIGHT
, 1},
129 {640, 480, 0, 0, DM_PELSWIDTH
, 0},
130 {640, 480, 0, 0, DM_PELSHEIGHT
, 0},
131 { 0, 480, 0, 0, DM_PELSWIDTH
| DM_PELSHEIGHT
, 0},
132 {640, 0, 0, 0, DM_PELSWIDTH
| DM_PELSHEIGHT
, 0},
134 {0, 0, 0, 0, DM_DISPLAYFREQUENCY
, 0},
136 #define vid_modes_cnt (sizeof(vid_modes_test) / sizeof(vid_modes_test[0]))
138 static void test_ChangeDisplaySettingsEx(void)
146 if (!pChangeDisplaySettingsExA
)
148 skip("ChangeDisplaySettingsExA is not available\n");
152 SetLastError(0xdeadbeef);
153 res
= EnumDisplaySettings(NULL
, ENUM_CURRENT_SETTINGS
, &dm
);
154 ok(res
, "EnumDisplaySettings error %u\n", GetLastError());
156 width
= dm
.dmPelsWidth
;
158 /* the following 2 tests show that dm.dmSize being 0 is invalid, but
159 * ChangeDisplaySettingsExA still reports success.
161 memset(&dm
, 0, sizeof(dm
));
162 dm
.dmFields
= DM_PELSWIDTH
| DM_PELSHEIGHT
;
163 dm
.dmPelsWidth
= width
;
164 res
= pChangeDisplaySettingsExA(NULL
, &dm
, NULL
, CDS_TEST
, NULL
);
165 ok(res
== DISP_CHANGE_SUCCESSFUL
,
166 "ChangeDisplaySettingsExA returned %d, expected DISP_CHANGE_SUCCESSFUL\n", res
);
168 memset(&dmW
, 0, sizeof(dmW
));
169 dmW
.dmFields
= DM_PELSWIDTH
| DM_PELSHEIGHT
;
170 dmW
.dmPelsWidth
= width
;
171 SetLastError(0xdeadbeef);
172 res
= pChangeDisplaySettingsExW(NULL
, &dmW
, NULL
, CDS_TEST
, NULL
);
173 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED
)
174 ok(res
== DISP_CHANGE_FAILED
,
175 "ChangeDisplaySettingsExW returned %d, expected DISP_CHANGE_FAILED\n", res
);
177 memset(&dm
, 0, sizeof(dm
));
178 dm
.dmSize
= sizeof(dm
);
180 for (i
= 0; i
< vid_modes_cnt
; i
++)
182 dm
.dmPelsWidth
= vid_modes_test
[i
].w
;
183 dm
.dmPelsHeight
= vid_modes_test
[i
].h
;
184 dm
.dmBitsPerPel
= vid_modes_test
[i
].bpp
;
185 dm
.dmDisplayFrequency
= vid_modes_test
[i
].freq
;
186 dm
.dmFields
= vid_modes_test
[i
].fields
;
187 res
= pChangeDisplaySettingsExA(NULL
, &dm
, NULL
, CDS_FULLSCREEN
, NULL
);
188 ok(vid_modes_test
[i
].success
?
189 (res
== DISP_CHANGE_SUCCESSFUL
) :
190 (res
== DISP_CHANGE_BADMODE
|| res
== DISP_CHANGE_BADPARAM
),
191 "Unexpected ChangeDisplaySettingsEx() return code for resolution[%d]: %d\n", i
, res
);
193 if (res
== DISP_CHANGE_SUCCESSFUL
)
197 SetRect(&virt
, 0, 0, GetSystemMetrics(SM_CXVIRTUALSCREEN
), GetSystemMetrics(SM_CYVIRTUALSCREEN
));
198 OffsetRect(&virt
, GetSystemMetrics(SM_XVIRTUALSCREEN
), GetSystemMetrics(SM_YVIRTUALSCREEN
));
200 /* Resolution change resets clip rect */
201 ok(GetClipCursor(&r
), "GetClipCursor() failed\n");
202 ok(EqualRect(&r
, &virt
), "Invalid clip rect: (%d %d) x (%d %d)\n", r
.left
, r
.top
, r
.right
, r
.bottom
);
204 ok(ClipCursor(NULL
), "ClipCursor() failed\n");
205 ok(GetClipCursor(&r
), "GetClipCursor() failed\n");
206 ok(EqualRect(&r
, &virt
), "Invalid clip rect: (%d %d) x (%d %d)\n", r
.left
, r
.top
, r
.right
, r
.bottom
);
208 /* This should always work. Primary monitor is at (0,0) */
209 SetRect(&r1
, 10, 10, 20, 20);
210 ok(ClipCursor(&r1
), "ClipCursor() failed\n");
211 ok(GetClipCursor(&r
), "GetClipCursor() failed\n");
212 ok(EqualRect(&r
, &r1
), "Invalid clip rect: (%d %d) x (%d %d)\n", r
.left
, r
.top
, r
.right
, r
.bottom
);
214 SetRect(&r1
, virt
.left
- 10, virt
.top
- 10, virt
.right
+ 20, virt
.bottom
+ 20);
215 ok(ClipCursor(&r1
), "ClipCursor() failed\n");
216 ok(GetClipCursor(&r
), "GetClipCursor() failed\n");
217 ok(EqualRect(&r
, &virt
), "Invalid clip rect: (%d %d) x (%d %d)\n", r
.left
, r
.top
, r
.right
, r
.bottom
);
220 res
= pChangeDisplaySettingsExA(NULL
, NULL
, NULL
, CDS_RESET
, NULL
);
221 ok(res
== DISP_CHANGE_SUCCESSFUL
, "Failed to reset default resolution: %d\n", res
);
224 static void test_monitors(void)
226 HMONITOR monitor
, primary
;
230 primary
= pMonitorFromPoint( pt
, MONITOR_DEFAULTTOPRIMARY
);
231 ok( primary
!= 0, "couldn't get primary monitor\n" );
233 monitor
= pMonitorFromWindow( 0, MONITOR_DEFAULTTONULL
);
234 ok( !monitor
, "got %p, should not get a monitor for an invalid window\n", monitor
);
235 monitor
= pMonitorFromWindow( 0, MONITOR_DEFAULTTOPRIMARY
);
236 ok( monitor
== primary
, "got %p, should get primary %p for MONITOR_DEFAULTTOPRIMARY\n", monitor
, primary
);
237 monitor
= pMonitorFromWindow( 0, MONITOR_DEFAULTTONEAREST
);
238 ok( monitor
== primary
, "got %p, should get primary %p for MONITOR_DEFAULTTONEAREST\n", monitor
, primary
);
244 init_function_pointers();
245 test_enumdisplaydevices();
246 if (winetest_interactive
)
247 test_ChangeDisplaySettingsEx();
248 if (pMonitorFromPoint
&& pMonitorFromWindow
)
251 skip("MonitorFromPoint and/or MonitorFromWindow are not available\n");