user32: Increase the message timeout.
[wine] / dlls / user32 / tests / monitor.c
1 /*
2  * Unit tests for monitor APIs
3  *
4  * Copyright 2005 Huw Davies
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "wine/test.h"
22 #include "winbase.h"
23 #include "wingdi.h"
24 #include "winuser.h"
25
26 static HMODULE hdll;
27 static LONG (WINAPI *pChangeDisplaySettingsExA)(LPCSTR, LPDEVMODEA, HWND, DWORD, LPVOID);
28 static BOOL (WINAPI *pEnumDisplayDevicesA)(LPCSTR,DWORD,LPDISPLAY_DEVICEA,DWORD);
29 static BOOL (WINAPI *pEnumDisplayMonitors)(HDC,LPRECT,MONITORENUMPROC,LPARAM);
30 static BOOL (WINAPI *pGetMonitorInfoA)(HMONITOR,LPMONITORINFO);
31 static HMONITOR (WINAPI *pMonitorFromPoint)(POINT,DWORD);
32 static HMONITOR (WINAPI *pMonitorFromWindow)(HWND,DWORD);
33
34 static void init_function_pointers(void)
35 {
36     hdll = GetModuleHandleA("user32.dll");
37
38 #define GET_PROC(func) \
39     p ## func = (void*)GetProcAddress(hdll, #func); \
40     if(!p ## func) \
41       trace("GetProcAddress(%s) failed\n", #func);
42
43     GET_PROC(ChangeDisplaySettingsExA)
44     GET_PROC(EnumDisplayDevicesA)
45     GET_PROC(EnumDisplayMonitors)
46     GET_PROC(GetMonitorInfoA)
47     GET_PROC(MonitorFromPoint)
48     GET_PROC(MonitorFromWindow)
49
50 #undef GET_PROC
51 }
52
53 static BOOL CALLBACK monitor_enum_proc(HMONITOR hmon, HDC hdc, LPRECT lprc,
54                                        LPARAM lparam)
55 {
56     MONITORINFOEXA mi;
57     char *primary = (char *)lparam;
58
59     mi.cbSize = sizeof(mi);
60
61     ok(pGetMonitorInfoA(hmon, (MONITORINFO*)&mi), "GetMonitorInfo failed\n");
62     if(mi.dwFlags == MONITORINFOF_PRIMARY)
63         strcpy(primary, mi.szDevice);
64
65     return TRUE;
66 }
67
68 static void test_enumdisplaydevices(void)
69 {
70     DISPLAY_DEVICEA dd;
71     char primary_device_name[32];
72     char primary_monitor_device_name[32];
73     DWORD primary_num = -1, num = 0;
74
75     dd.cb = sizeof(dd);
76     if(pEnumDisplayDevicesA == NULL) return;
77     while(1)
78     {
79         BOOL ret;
80         HDC dc;
81         ret = pEnumDisplayDevicesA(NULL, num, &dd, 0);
82         ok(ret || num != 0, "EnumDisplayDevices fails with num == 0\n");
83         if(!ret) break;
84         if(dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
85         {
86             strcpy(primary_device_name, dd.DeviceName);
87             primary_num = num;
88         }
89         if(dd.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)
90         {
91             /* test creating DC */
92             dc = CreateDCA(dd.DeviceName, NULL, NULL, NULL);
93             ok(dc != NULL, "Failed to CreateDC(\"%s\") err=%d\n", dd.DeviceName, GetLastError());
94             DeleteDC(dc);
95         }
96         num++;
97     }
98     ok(primary_num != -1, "Didn't get the primary device\n");
99
100     if(pEnumDisplayMonitors && pGetMonitorInfoA) {
101         ok(pEnumDisplayMonitors(NULL, NULL, monitor_enum_proc, (LPARAM)primary_monitor_device_name),
102            "EnumDisplayMonitors failed\n");
103
104         ok(!strcmp(primary_monitor_device_name, primary_device_name),
105            "monitor device name %s, device name %s\n", primary_monitor_device_name,
106            primary_device_name);
107     }
108 }
109
110 struct vid_mode
111 {
112     DWORD w, h, bpp, freq, fields;
113     LONG success;
114 };
115
116 static struct vid_mode vid_modes_test[] = {
117     {640, 480, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY, 1},
118     {640, 480, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT |                 DM_DISPLAYFREQUENCY, 1},
119     {640, 480, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL                      , 1},
120     {640, 480, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT                                      , 1},
121     {640, 480, 0, 0,                                DM_BITSPERPEL                      , 1},
122     {640, 480, 0, 0,                                                DM_DISPLAYFREQUENCY, 1},
123
124     {0, 0, 0, 0, DM_PELSWIDTH, 1},
125     {0, 0, 0, 0, DM_PELSHEIGHT, 1},
126
127     {640, 480, 0, 0, DM_PELSWIDTH, 0},
128     {640, 480, 0, 0, DM_PELSHEIGHT, 0},
129     {  0, 480, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT, 0},
130     {640,   0, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT, 0},
131
132     {0, 0, 0, 0, DM_DISPLAYFREQUENCY, 0},
133 };
134 #define vid_modes_cnt (sizeof(vid_modes_test) / sizeof(vid_modes_test[0]))
135
136 static void test_ChangeDisplaySettingsEx(void)
137 {
138     DEVMODE dm;
139     LONG res;
140     int i;
141
142     if (!pChangeDisplaySettingsExA)
143     {
144         skip("ChangeDisplaySettingsExA is not available\n");
145         return;
146     }
147
148     memset(&dm, 0, sizeof(dm));
149     dm.dmSize = sizeof(dm);
150
151     for (i = 0; i < vid_modes_cnt; i++)
152     {
153         dm.dmPelsWidth        = vid_modes_test[i].w;
154         dm.dmPelsHeight       = vid_modes_test[i].h;
155         dm.dmBitsPerPel       = vid_modes_test[i].bpp;
156         dm.dmDisplayFrequency = vid_modes_test[i].freq;
157         dm.dmFields           = vid_modes_test[i].fields;
158         res = pChangeDisplaySettingsExA(NULL, &dm, NULL, CDS_FULLSCREEN, NULL);
159         ok(vid_modes_test[i].success ?
160            (res == DISP_CHANGE_SUCCESSFUL) :
161            (res == DISP_CHANGE_BADMODE || res == DISP_CHANGE_BADPARAM),
162            "Unexpected ChangeDisplaySettingsEx() return code for resolution[%d]: %d\n", i, res);
163
164         if (res == DISP_CHANGE_SUCCESSFUL)
165         {
166             RECT r, r1, virt;
167
168             SetRect(&virt, 0, 0, GetSystemMetrics(SM_CXVIRTUALSCREEN), GetSystemMetrics(SM_CYVIRTUALSCREEN));
169             OffsetRect(&virt, GetSystemMetrics(SM_XVIRTUALSCREEN), GetSystemMetrics(SM_YVIRTUALSCREEN));
170
171             /* Resolution change resets clip rect */
172             ok(GetClipCursor(&r), "GetClipCursor() failed\n");
173             ok(EqualRect(&r, &virt), "Invalid clip rect: (%d %d) x (%d %d)\n", r.left, r.top, r.right, r.bottom);
174
175             ok(ClipCursor(NULL), "ClipCursor() failed\n");
176             ok(GetClipCursor(&r), "GetClipCursor() failed\n");
177             ok(EqualRect(&r, &virt), "Invalid clip rect: (%d %d) x (%d %d)\n", r.left, r.top, r.right, r.bottom);
178
179             /* This should always work. Primary monitor is at (0,0) */
180             SetRect(&r1, 10, 10, 20, 20);
181             ok(ClipCursor(&r1), "ClipCursor() failed\n");
182             ok(GetClipCursor(&r), "GetClipCursor() failed\n");
183             ok(EqualRect(&r, &r1), "Invalid clip rect: (%d %d) x (%d %d)\n", r.left, r.top, r.right, r.bottom);
184
185             SetRect(&r1, virt.left - 10, virt.top - 10, virt.right + 20, virt.bottom + 20);
186             ok(ClipCursor(&r1), "ClipCursor() failed\n");
187             ok(GetClipCursor(&r), "GetClipCursor() failed\n");
188             ok(EqualRect(&r, &virt), "Invalid clip rect: (%d %d) x (%d %d)\n", r.left, r.top, r.right, r.bottom);
189         }
190     }
191     res = pChangeDisplaySettingsExA(NULL, NULL, NULL, CDS_RESET, NULL);
192     ok(res == DISP_CHANGE_SUCCESSFUL, "Failed to reset default resolution: %d\n", res);
193 }
194
195 static void test_monitors(void)
196 {
197     HMONITOR monitor, primary;
198     POINT pt;
199
200     pt.x = pt.y = 0;
201     primary = pMonitorFromPoint( pt, MONITOR_DEFAULTTOPRIMARY );
202     ok( primary != 0, "couldn't get primary monitor\n" );
203
204     monitor = pMonitorFromWindow( 0, MONITOR_DEFAULTTONULL );
205     ok( !monitor, "got %p, should not get a monitor for an invalid window\n", monitor );
206     monitor = pMonitorFromWindow( 0, MONITOR_DEFAULTTOPRIMARY );
207     ok( monitor == primary, "got %p, should get primary %p for MONITOR_DEFAULTTOPRIMARY\n", monitor, primary );
208     monitor = pMonitorFromWindow( 0, MONITOR_DEFAULTTONEAREST );
209     ok( monitor == primary, "got %p, should get primary %p for MONITOR_DEFAULTTONEAREST\n", monitor, primary );
210 }
211
212
213 START_TEST(monitor)
214 {
215     init_function_pointers();
216     test_enumdisplaydevices();
217     if (winetest_interactive)
218         test_ChangeDisplaySettingsEx();
219     if (pMonitorFromPoint && pMonitorFromWindow)
220         test_monitors();
221     else
222         skip("MonitorFromPoint and/or MonitorFromWindow are not available\n");
223 }