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