user32/tests: Fix a test failure on Win9x, WinMe and NT4.
[wine] / dlls / user32 / tests / monitor.c
1 /*
2  * Unit tests for monitor APIs
3  *
4  * Copyright 2005 Huw Davies
5  * Copyright 2008 Dmitry Timoshkov
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "wine/test.h"
23 #include "winbase.h"
24 #include "wingdi.h"
25 #include "winuser.h"
26
27 static HMODULE hdll;
28 static LONG (WINAPI *pChangeDisplaySettingsExA)(LPCSTR, LPDEVMODEA, HWND, DWORD, LPVOID);
29 static LONG (WINAPI *pChangeDisplaySettingsExW)(LPCWSTR, LPDEVMODEW, HWND, DWORD, LPVOID);
30 static BOOL (WINAPI *pEnumDisplayDevicesA)(LPCSTR,DWORD,LPDISPLAY_DEVICEA,DWORD);
31 static BOOL (WINAPI *pEnumDisplayMonitors)(HDC,LPRECT,MONITORENUMPROC,LPARAM);
32 static BOOL (WINAPI *pGetMonitorInfoA)(HMONITOR,LPMONITORINFO);
33 static HMONITOR (WINAPI *pMonitorFromPoint)(POINT,DWORD);
34 static HMONITOR (WINAPI *pMonitorFromWindow)(HWND,DWORD);
35
36 static void init_function_pointers(void)
37 {
38     hdll = GetModuleHandleA("user32.dll");
39
40 #define GET_PROC(func) \
41     p ## func = (void*)GetProcAddress(hdll, #func); \
42     if(!p ## func) \
43       trace("GetProcAddress(%s) failed\n", #func);
44
45     GET_PROC(ChangeDisplaySettingsExA)
46     GET_PROC(ChangeDisplaySettingsExW)
47     GET_PROC(EnumDisplayDevicesA)
48     GET_PROC(EnumDisplayMonitors)
49     GET_PROC(GetMonitorInfoA)
50     GET_PROC(MonitorFromPoint)
51     GET_PROC(MonitorFromWindow)
52
53 #undef GET_PROC
54 }
55
56 static BOOL CALLBACK monitor_enum_proc(HMONITOR hmon, HDC hdc, LPRECT lprc,
57                                        LPARAM lparam)
58 {
59     MONITORINFOEXA mi;
60     char *primary = (char *)lparam;
61
62     mi.cbSize = sizeof(mi);
63
64     ok(pGetMonitorInfoA(hmon, (MONITORINFO*)&mi), "GetMonitorInfo failed\n");
65     if (mi.dwFlags & MONITORINFOF_PRIMARY)
66         strcpy(primary, mi.szDevice);
67
68     return TRUE;
69 }
70
71 static void test_enumdisplaydevices(void)
72 {
73     DISPLAY_DEVICEA dd;
74     char primary_device_name[32];
75     char primary_monitor_device_name[32];
76     DWORD primary_num = -1, num = 0;
77     BOOL ret;
78
79     if (!pEnumDisplayDevicesA)
80     {
81         skip("EnumDisplayDevicesA is not available\n");
82         return;
83     }
84
85     dd.cb = sizeof(dd);
86     while(1)
87     {
88         BOOL ret;
89         HDC dc;
90         ret = pEnumDisplayDevicesA(NULL, num, &dd, 0);
91         ok(ret || num != 0, "EnumDisplayDevices fails with num == 0\n");
92         if(!ret) break;
93         if(dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
94         {
95             strcpy(primary_device_name, dd.DeviceName);
96             primary_num = num;
97         }
98         if(dd.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)
99         {
100             /* test creating DC */
101             dc = CreateDCA(dd.DeviceName, NULL, NULL, NULL);
102             ok(dc != NULL, "Failed to CreateDC(\"%s\") err=%d\n", dd.DeviceName, GetLastError());
103             DeleteDC(dc);
104         }
105         num++;
106     }
107     ok(primary_num != -1, "Didn't get the primary device\n");
108
109     if (!pEnumDisplayMonitors || !pGetMonitorInfoA)
110     {
111         skip("EnumDisplayMonitors or GetMonitorInfoA are not available\n");
112         return;
113     }
114
115     primary_monitor_device_name[0] = 0;
116     ret = pEnumDisplayMonitors(NULL, NULL, monitor_enum_proc, (LPARAM)primary_monitor_device_name);
117     ok(ret, "EnumDisplayMonitors failed\n");
118     ok(!strcmp(primary_monitor_device_name, primary_device_name),
119        "monitor device name %s, device name %s\n", primary_monitor_device_name,
120        primary_device_name);
121 }
122
123 struct vid_mode
124 {
125     DWORD w, h, bpp, freq, fields;
126     BOOL must_succeed;
127 };
128
129 static const struct vid_mode vid_modes_test[] = {
130     {640, 480, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY, 1},
131     {640, 480, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT |                 DM_DISPLAYFREQUENCY, 1},
132     {640, 480, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL                      , 1},
133     {640, 480, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT                                      , 1},
134     {640, 480, 0, 0,                                DM_BITSPERPEL                      , 0},
135     {640, 480, 0, 0,                                                DM_DISPLAYFREQUENCY, 0},
136
137     {0, 0, 0, 0, DM_PELSWIDTH, 0},
138     {0, 0, 0, 0, DM_PELSHEIGHT, 0},
139
140     {640, 480, 0, 0, DM_PELSWIDTH, 0},
141     {640, 480, 0, 0, DM_PELSHEIGHT, 0},
142     {  0, 480, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT, 0},
143     {640,   0, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT, 0},
144
145     /* the following test succeeds under XP SP3
146     {0, 0, 0, 0, DM_DISPLAYFREQUENCY, 0}
147     */
148 };
149 #define vid_modes_cnt (sizeof(vid_modes_test) / sizeof(vid_modes_test[0]))
150
151 static void test_ChangeDisplaySettingsEx(void)
152 {
153     DEVMODEA dm;
154     DEVMODEW dmW;
155     DWORD width;
156     LONG res;
157     int i;
158
159     if (!pChangeDisplaySettingsExA)
160     {
161         skip("ChangeDisplaySettingsExA is not available\n");
162         return;
163     }
164
165     SetLastError(0xdeadbeef);
166     res = EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm);
167     ok(res, "EnumDisplaySettings error %u\n", GetLastError());
168
169     width = dm.dmPelsWidth;
170
171     dm.dmDriverExtra = 1;
172     res = ChangeDisplaySettingsA(&dm, CDS_TEST);
173     ok(res == DISP_CHANGE_SUCCESSFUL,
174        "ChangeDisplaySettingsA returned %d, expected DISP_CHANGE_SUCCESSFUL\n", res);
175     ok(dm.dmDriverExtra == 0, "ChangeDisplaySettingsA didn't reset dmDriverExtra to 0\n");
176
177     /* crashes under XP SP3 for large dmDriverExtra values */
178     dm.dmDriverExtra = 1;
179     res = pChangeDisplaySettingsExA(NULL, &dm, NULL, CDS_TEST, NULL);
180     ok(res == DISP_CHANGE_SUCCESSFUL,
181        "ChangeDisplaySettingsExW returned %d, expected DISP_CHANGE_BADMODE\n", res);
182     ok(dm.dmDriverExtra == 1, "ChangeDisplaySettingsExA shouldn't reset dmDriverExtra to 0\n");
183
184     memset(&dmW, 0, sizeof(dmW));
185     dmW.dmSize = sizeof(dmW);
186     dmW.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
187     dmW.dmPelsWidth = dm.dmPelsWidth;
188     dmW.dmPelsHeight = dm.dmPelsHeight;
189     dmW.dmDriverExtra = 1;
190     SetLastError(0xdeadbeef);
191     res = ChangeDisplaySettingsW(&dmW, CDS_TEST);
192     if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
193     {
194         ok(res == DISP_CHANGE_SUCCESSFUL,
195            "ChangeDisplaySettingsW returned %d, expected DISP_CHANGE_SUCCESSFUL\n", res);
196         ok(dmW.dmDriverExtra == 0, "ChangeDisplaySettingsW didn't reset dmDriverExtra to 0\n");
197     }
198
199     /* Apparently XP treats dmDriverExtra being != 0 as an error */
200     dmW.dmDriverExtra = 1;
201     res = pChangeDisplaySettingsExW(NULL, &dmW, NULL, CDS_TEST, NULL);
202     if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
203     {
204         ok(res == DISP_CHANGE_SUCCESSFUL,
205            "ChangeDisplaySettingsExW returned %d, expected DISP_CHANGE_BADMODE\n", res);
206         ok(dmW.dmDriverExtra == 1, "ChangeDisplaySettingsExW shouldn't reset dmDriverExtra to 0\n");
207     }
208
209     /* the following 2 tests show that dm.dmSize being 0 is invalid, but
210      * ChangeDisplaySettingsExA still reports success.
211      */
212     memset(&dm, 0, sizeof(dm));
213     dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
214     dm.dmPelsWidth = width;
215     res = pChangeDisplaySettingsExA(NULL, &dm, NULL, CDS_TEST, NULL);
216     ok(res == DISP_CHANGE_SUCCESSFUL ||
217        res == DISP_CHANGE_BADMODE || /* Win98, WinMe */
218        res == DISP_CHANGE_FAILED, /* NT4 */
219        "ChangeDisplaySettingsExA returned unexpected %d\n", res);
220
221     memset(&dmW, 0, sizeof(dmW));
222     dmW.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
223     dmW.dmPelsWidth = width;
224     SetLastError(0xdeadbeef);
225     res = pChangeDisplaySettingsExW(NULL, &dmW, NULL, CDS_TEST, NULL);
226     if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
227         ok(res == DISP_CHANGE_FAILED ||
228            res == DISP_CHANGE_BADMODE /* XP SP3 */,
229            "ChangeDisplaySettingsExW returned %d, expected DISP_CHANGE_FAILED or DISP_CHANGE_BADMODE\n", res);
230
231     memset(&dm, 0, sizeof(dm));
232     dm.dmSize = sizeof(dm);
233
234     for (i = 0; i < vid_modes_cnt; i++)
235     {
236         dm.dmPelsWidth        = vid_modes_test[i].w;
237         dm.dmPelsHeight       = vid_modes_test[i].h;
238         dm.dmBitsPerPel       = vid_modes_test[i].bpp;
239         dm.dmDisplayFrequency = vid_modes_test[i].freq;
240         dm.dmFields           = vid_modes_test[i].fields;
241         res = pChangeDisplaySettingsExA(NULL, &dm, NULL, CDS_TEST, NULL);
242         ok(vid_modes_test[i].must_succeed ?
243            (res == DISP_CHANGE_SUCCESSFUL) :
244            (res == DISP_CHANGE_SUCCESSFUL || res == DISP_CHANGE_BADMODE || res == DISP_CHANGE_BADPARAM),
245            "Unexpected ChangeDisplaySettingsEx() return code for resolution[%d]: %d\n", i, res);
246
247         if (res == DISP_CHANGE_SUCCESSFUL)
248         {
249             RECT r, r1, virt;
250
251             SetRect(&virt, 0, 0, GetSystemMetrics(SM_CXVIRTUALSCREEN), GetSystemMetrics(SM_CYVIRTUALSCREEN));
252             OffsetRect(&virt, GetSystemMetrics(SM_XVIRTUALSCREEN), GetSystemMetrics(SM_YVIRTUALSCREEN));
253
254             /* Resolution change resets clip rect */
255             ok(GetClipCursor(&r), "GetClipCursor() failed\n");
256             ok(EqualRect(&r, &virt), "Invalid clip rect: (%d %d) x (%d %d)\n", r.left, r.top, r.right, r.bottom);
257
258             ok(ClipCursor(NULL), "ClipCursor() failed\n");
259             ok(GetClipCursor(&r), "GetClipCursor() failed\n");
260             ok(EqualRect(&r, &virt), "Invalid clip rect: (%d %d) x (%d %d)\n", r.left, r.top, r.right, r.bottom);
261
262             /* This should always work. Primary monitor is at (0,0) */
263             SetRect(&r1, 10, 10, 20, 20);
264             ok(ClipCursor(&r1), "ClipCursor() failed\n");
265             ok(GetClipCursor(&r), "GetClipCursor() failed\n");
266             ok(EqualRect(&r, &r1), "Invalid clip rect: (%d %d) x (%d %d)\n", r.left, r.top, r.right, r.bottom);
267
268             SetRect(&r1, virt.left - 10, virt.top - 10, virt.right + 20, virt.bottom + 20);
269             ok(ClipCursor(&r1), "ClipCursor() failed\n");
270             ok(GetClipCursor(&r), "GetClipCursor() failed\n");
271             ok(EqualRect(&r, &virt) ||
272                broken(EqualRect(&r, &r1)) /* win9x */,
273                "Invalid clip rect: (%d %d) x (%d %d)\n", r.left, r.top, r.right, r.bottom);
274             ClipCursor(&virt);
275         }
276     }
277     res = pChangeDisplaySettingsExA(NULL, NULL, NULL, CDS_RESET, NULL);
278     ok(res == DISP_CHANGE_SUCCESSFUL, "Failed to reset default resolution: %d\n", res);
279 }
280
281 static void test_monitors(void)
282 {
283     HMONITOR monitor, primary;
284     POINT pt;
285
286     if (!pMonitorFromPoint || !pMonitorFromWindow)
287     {
288         skip("MonitorFromPoint or MonitorFromWindow are not available\n");
289         return;
290     }
291
292     pt.x = pt.y = 0;
293     primary = pMonitorFromPoint( pt, MONITOR_DEFAULTTOPRIMARY );
294     ok( primary != 0, "couldn't get primary monitor\n" );
295
296     monitor = pMonitorFromWindow( 0, MONITOR_DEFAULTTONULL );
297     ok( !monitor, "got %p, should not get a monitor for an invalid window\n", monitor );
298     monitor = pMonitorFromWindow( 0, MONITOR_DEFAULTTOPRIMARY );
299     ok( monitor == primary, "got %p, should get primary %p for MONITOR_DEFAULTTOPRIMARY\n", monitor, primary );
300     monitor = pMonitorFromWindow( 0, MONITOR_DEFAULTTONEAREST );
301     ok( monitor == primary, "got %p, should get primary %p for MONITOR_DEFAULTTONEAREST\n", monitor, primary );
302 }
303
304 static BOOL CALLBACK find_primary_mon(HMONITOR hmon, HDC hdc, LPRECT rc, LPARAM lp)
305 {
306     MONITORINFO mi;
307     BOOL ret;
308
309     mi.cbSize = sizeof(mi);
310     ret = pGetMonitorInfoA(hmon, &mi);
311     ok(ret, "GetMonitorInfo failed\n");
312     if (mi.dwFlags & MONITORINFOF_PRIMARY)
313     {
314         *(HMONITOR *)lp = hmon;
315         return FALSE;
316     }
317     return TRUE;
318 }
319
320 static void test_work_area(void)
321 {
322     HMONITOR hmon;
323     MONITORINFO mi;
324     RECT rc_work, rc_normal;
325     HWND hwnd;
326     WINDOWPLACEMENT wp;
327     BOOL ret;
328
329     if (!pEnumDisplayMonitors || !pGetMonitorInfoA)
330     {
331         skip("EnumDisplayMonitors or GetMonitorInfoA are not available\n");
332         return;
333     }
334
335     hmon = 0;
336     ret = pEnumDisplayMonitors(NULL, NULL, find_primary_mon, (LPARAM)&hmon);
337     ok(!ret && hmon != 0, "Failed to find primary monitor\n");
338
339     mi.cbSize = sizeof(mi);
340     SetLastError(0xdeadbeef);
341     ret = pGetMonitorInfoA(hmon, &mi);
342     ok(ret, "GetMonitorInfo error %u\n", GetLastError());
343     ok(mi.dwFlags & MONITORINFOF_PRIMARY, "not a primary monitor\n");
344     trace("primary monitor (%d,%d-%d,%d)\n",
345         mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom);
346
347     SetLastError(0xdeadbeef);
348     ret = SystemParametersInfo(SPI_GETWORKAREA, 0, &rc_work, 0);
349     ok(ret, "SystemParametersInfo error %u\n", GetLastError());
350     trace("work area (%d,%d-%d,%d)\n", rc_work.left, rc_work.top, rc_work.right, rc_work.bottom);
351     ok(EqualRect(&rc_work, &mi.rcWork), "work area is different\n");
352
353     hwnd = CreateWindowEx(0, "static", NULL, WS_OVERLAPPEDWINDOW|WS_VISIBLE,100,100,10,10,0,0,0,NULL);
354     ok(hwnd != 0, "CreateWindowEx failed\n");
355
356     ret = GetWindowRect(hwnd, &rc_normal);
357     ok(ret, "GetWindowRect failed\n");
358     trace("normal (%d,%d-%d,%d)\n", rc_normal.left, rc_normal.top, rc_normal.right, rc_normal.bottom);
359
360     wp.length = sizeof(wp);
361     ret = GetWindowPlacement(hwnd, &wp);
362     ok(ret, "GetWindowPlacement failed\n");
363     trace("min: %d,%d max %d,%d normal %d,%d-%d,%d\n",
364           wp.ptMinPosition.x, wp.ptMinPosition.y,
365           wp.ptMaxPosition.x, wp.ptMaxPosition.y,
366           wp.rcNormalPosition.left, wp.rcNormalPosition.top,
367           wp.rcNormalPosition.right, wp.rcNormalPosition.bottom);
368     OffsetRect(&wp.rcNormalPosition, rc_work.left, rc_work.top);
369     if (!EqualRect(&mi.rcMonitor, &mi.rcWork)) /* FIXME: remove once Wine is fixed */
370         todo_wine ok(EqualRect(&rc_normal, &wp.rcNormalPosition), "normal pos is different\n");
371     else
372         ok(EqualRect(&rc_normal, &wp.rcNormalPosition), "normal pos is different\n");
373
374     SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_TOOLWINDOW);
375
376     wp.length = sizeof(wp);
377     ret = GetWindowPlacement(hwnd, &wp);
378     ok(ret, "GetWindowPlacement failed\n");
379     trace("min: %d,%d max %d,%d normal %d,%d-%d,%d\n",
380           wp.ptMinPosition.x, wp.ptMinPosition.y,
381           wp.ptMaxPosition.x, wp.ptMaxPosition.y,
382           wp.rcNormalPosition.left, wp.rcNormalPosition.top,
383           wp.rcNormalPosition.right, wp.rcNormalPosition.bottom);
384     ok(EqualRect(&rc_normal, &wp.rcNormalPosition), "normal pos is different\n");
385
386     DestroyWindow(hwnd);
387 }
388
389 START_TEST(monitor)
390 {
391     init_function_pointers();
392     test_enumdisplaydevices();
393     test_ChangeDisplaySettingsEx();
394     test_monitors();
395     test_work_area();
396 }