msctf: Use FAILED instead of !SUCCEDED.
[wine] / dlls / ddraw / tests / ddrawmodes.c
1 /*
2  * Unit tests for ddraw functions
3  *
4  *
5  * Part of this test involves changing the screen resolution. But this is
6  * really disrupting if the user is doing something else and is not very nice
7  * to CRT screens. Plus, ideally it needs someone watching it to check that
8  * each mode displays correctly.
9  * So this is only done if the test is being run in interactive mode.
10  *
11  * Copyright (C) 2003 Sami Aario
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public
15  * License as published by the Free Software Foundation; either
16  * version 2.1 of the License, or (at your option) any later version.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with this library; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26  */
27
28 #include <assert.h>
29 #include "wine/test.h"
30 #include "ddraw.h"
31
32 static LPDIRECTDRAW lpDD = NULL;
33 static LPDIRECTDRAWSURFACE lpDDSPrimary = NULL;
34 static LPDIRECTDRAWSURFACE lpDDSBack = NULL;
35 static WNDCLASS wc;
36 static HWND hwnd;
37 static int modes_cnt;
38 static int modes_size;
39 static LPDDSURFACEDESC modes;
40
41 static void createwindow(void)
42 {
43     wc.style = CS_HREDRAW | CS_VREDRAW;
44     wc.lpfnWndProc = DefWindowProcA;
45     wc.cbClsExtra = 0;
46     wc.cbWndExtra = 0;
47     wc.hInstance = GetModuleHandleA(0);
48     wc.hIcon = LoadIconA(wc.hInstance, IDI_APPLICATION);
49     wc.hCursor = LoadCursorA(NULL, IDC_ARROW);
50     wc.hbrBackground = GetStockObject(BLACK_BRUSH);
51     wc.lpszMenuName = NULL;
52     wc.lpszClassName = "TestWindowClass";
53     if(!RegisterClassA(&wc))
54         assert(0);
55     
56     hwnd = CreateWindowExA(0, "TestWindowClass", "TestWindowClass",
57         WS_POPUP, 0, 0,
58         GetSystemMetrics(SM_CXSCREEN),
59         GetSystemMetrics(SM_CYSCREEN),
60         NULL, NULL, GetModuleHandleA(0), NULL);
61     assert(hwnd != NULL);
62     
63     ShowWindow(hwnd, SW_HIDE);
64     UpdateWindow(hwnd);
65     SetFocus(hwnd);
66     
67 }
68
69 static BOOL createdirectdraw(void)
70 {
71     HRESULT rc;
72
73     rc = DirectDrawCreate(NULL, &lpDD, NULL);
74     ok(rc==DD_OK || rc==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %x\n", rc);
75     if (!lpDD) {
76         trace("DirectDrawCreateEx() failed with an error %x\n", rc);
77         return FALSE;
78     }
79     return TRUE;
80 }
81
82
83 static void releasedirectdraw(void)
84 {
85         if( lpDD != NULL )
86         {
87                 IDirectDraw_Release(lpDD);
88                 lpDD = NULL;
89         }
90 }
91
92 static void adddisplaymode(LPDDSURFACEDESC lpddsd)
93 {
94     if (!modes) 
95         modes = HeapAlloc(GetProcessHeap(), 0, (modes_size = 2) * sizeof(DDSURFACEDESC));
96     if (modes_cnt == modes_size) 
97             modes = HeapReAlloc(GetProcessHeap(), 0, modes, (modes_size *= 2) * sizeof(DDSURFACEDESC));
98     assert(modes);
99     modes[modes_cnt++] = *lpddsd;
100 }
101
102 static void flushdisplaymodes(void)
103 {
104     HeapFree(GetProcessHeap(), 0, modes);
105     modes = 0;
106     modes_cnt = modes_size = 0;
107 }
108
109 static HRESULT WINAPI enummodescallback(LPDDSURFACEDESC lpddsd, LPVOID lpContext)
110 {
111     trace("Width = %i, Height = %i, Refresh Rate = %i, Pitch = %i, flags =%02X\r\n",
112         lpddsd->dwWidth, lpddsd->dwHeight,
113           U2(*lpddsd).dwRefreshRate, U1(*lpddsd).lPitch, lpddsd->dwFlags);
114
115     /* Check that the pitch is valid if applicable */
116     if(lpddsd->dwFlags & DDSD_PITCH)
117     {
118         ok(U1(*lpddsd).lPitch != 0, "EnumDisplayModes callback with bad pitch\n");
119     }
120
121     /* Check that frequency is valid if applicable
122      *
123      * This fails on some Windows drivers or Windows versions, so it isn't important
124      * apparently
125     if(lpddsd->dwFlags & DDSD_REFRESHRATE)
126     {
127         ok(U2(*lpddsd).dwRefreshRate != 0, "EnumDisplayModes callback with bad refresh rate\n");
128     }
129      */
130
131     adddisplaymode(lpddsd);
132
133     return DDENUMRET_OK;
134 }
135
136 static void enumdisplaymodes(void)
137 {
138     DDSURFACEDESC ddsd;
139     HRESULT rc;
140
141     ZeroMemory(&ddsd, sizeof(DDSURFACEDESC));
142     ddsd.dwSize = sizeof(DDSURFACEDESC);
143     ddsd.dwFlags = DDSD_CAPS;
144     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
145
146     rc = IDirectDraw_EnumDisplayModes(lpDD,
147         DDEDM_STANDARDVGAMODES, &ddsd, 0, enummodescallback);
148     ok(rc==DD_OK || rc==E_INVALIDARG,"EnumDisplayModes returned: %x\n",rc);
149 }
150
151 static void setdisplaymode(int i)
152 {
153     HRESULT rc;
154
155     rc = IDirectDraw_SetCooperativeLevel(lpDD,
156         hwnd, DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
157     ok(rc==DD_OK,"SetCooperativeLevel returned: %x\n",rc);
158     if (modes[i].dwFlags & DDSD_PIXELFORMAT)
159     {
160         if (modes[i].ddpfPixelFormat.dwFlags & DDPF_RGB)
161         {
162             rc = IDirectDraw_SetDisplayMode(lpDD,
163                 modes[i].dwWidth, modes[i].dwHeight,
164                 U1(modes[i].ddpfPixelFormat).dwRGBBitCount);
165             ok(DD_OK==rc || DDERR_UNSUPPORTED==rc,"SetDisplayMode returned: %x\n",rc);
166             if (rc == DD_OK)
167             {
168                 RECT r, scrn, virt;
169
170                 SetRect(&virt, 0, 0, GetSystemMetrics(SM_CXVIRTUALSCREEN), GetSystemMetrics(SM_CYVIRTUALSCREEN));
171                 OffsetRect(&virt, GetSystemMetrics(SM_XVIRTUALSCREEN), GetSystemMetrics(SM_YVIRTUALSCREEN));
172                 SetRect(&scrn, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
173                 trace("Mode (%dx%d) [%dx%d] (%d %d)x(%d %d)\n", modes[i].dwWidth, modes[i].dwHeight,
174                       scrn.right, scrn.bottom, virt.left, virt.top, virt.right, virt.bottom);
175
176                 ok(GetClipCursor(&r), "GetClipCursor() failed\n");
177                 /* ddraw sets clip rect here to the screen size, even for
178                    multiple monitors */
179                 ok(EqualRect(&r, &scrn), "Invalid clip rect: (%d %d) x (%d %d)\n",
180                    r.left, r.top, r.right, r.bottom);
181
182                 ok(ClipCursor(NULL), "ClipCursor() failed\n");
183                 ok(GetClipCursor(&r), "GetClipCursor() failed\n");
184                 ok(EqualRect(&r, &virt), "Invalid clip rect: (%d %d) x (%d %d)\n",
185                    r.left, r.top, r.right, r.bottom);
186
187                 rc = IDirectDraw_RestoreDisplayMode(lpDD);
188                 ok(DD_OK==rc,"RestoreDisplayMode returned: %x\n",rc);
189             }
190         }
191     }
192 }
193
194 static void createsurface(void)
195 {
196     DDSURFACEDESC ddsd;
197     DDSCAPS ddscaps;
198     HRESULT rc;
199     
200     ddsd.dwSize = sizeof(ddsd);
201     ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
202     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
203         DDSCAPS_FLIP |
204         DDSCAPS_COMPLEX;
205     ddsd.dwBackBufferCount = 1;
206     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSPrimary, NULL );
207     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
208     ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
209     rc = IDirectDrawSurface_GetAttachedSurface(lpDDSPrimary, &ddscaps, &lpDDSBack);
210     ok(rc==DD_OK,"GetAttachedSurface returned: %x\n",rc);
211 }
212
213 static void destroysurface(void)
214 {
215     if( lpDDSPrimary != NULL )
216     {
217         IDirectDrawSurface_Release(lpDDSPrimary);
218         lpDDSPrimary = NULL;
219     }
220 }
221
222 static void testsurface(void)
223 {
224     const char* testMsg = "ddraw device context test";
225     HDC hdc;
226     HRESULT rc;
227     
228     rc = IDirectDrawSurface_GetDC(lpDDSBack, &hdc);
229     ok(rc==DD_OK, "IDirectDrawSurface_GetDC returned: %x\n",rc);
230     SetBkColor(hdc, RGB(0, 0, 255));
231     SetTextColor(hdc, RGB(255, 255, 0));
232     TextOut(hdc, 0, 0, testMsg, lstrlen(testMsg));
233     IDirectDrawSurface_ReleaseDC(lpDDSBack, hdc);
234     ok(rc==DD_OK, "IDirectDrawSurface_ReleaseDC returned: %x\n",rc);
235     
236     while (1)
237     {
238         rc = IDirectDrawSurface_Flip(lpDDSPrimary, NULL, DDFLIP_WAIT);
239         ok(rc==DD_OK || rc==DDERR_SURFACELOST, "IDirectDrawSurface_BltFast returned: %x\n",rc);
240
241         if (rc == DD_OK)
242         {
243             break;
244         }
245         else if (rc == DDERR_SURFACELOST)
246         {
247             rc = IDirectDrawSurface_Restore(lpDDSPrimary);
248             ok(rc==DD_OK, "IDirectDrawSurface_Restore returned: %x\n",rc);
249         }
250     }
251 }
252
253 static void testdisplaymodes(void)
254 {
255     int i;
256
257     for (i = 0; i < modes_cnt; ++i)
258     {
259         setdisplaymode(i);
260         createsurface();
261         testsurface();
262         destroysurface();
263     }
264 }
265
266 static void testcooperativelevels_normal(void)
267 {
268     HRESULT rc;
269     DDSURFACEDESC surfacedesc;
270     IDirectDrawSurface *surface = (IDirectDrawSurface *) 0xdeadbeef;
271
272     memset(&surfacedesc, 0, sizeof(surfacedesc));
273     surfacedesc.dwSize = sizeof(surfacedesc);
274     surfacedesc.ddpfPixelFormat.dwSize = sizeof(surfacedesc.ddpfPixelFormat);
275     surfacedesc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
276     surfacedesc.dwBackBufferCount = 1;
277     surfacedesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
278
279     /* Do some tests with DDSCL_NORMAL mode */
280
281     rc = IDirectDraw_SetCooperativeLevel(lpDD,
282         hwnd, DDSCL_NORMAL);
283     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NORMAL) returned: %x\n",rc);
284
285     /* Try creating a double buffered primary in normal mode */
286     rc = IDirectDraw_CreateSurface(lpDD, &surfacedesc, &surface, NULL);
287     if (rc == DDERR_UNSUPPORTEDMODE)
288         skip("Unsupported mode\n");
289     else
290     {
291         ok(rc == DDERR_NOEXCLUSIVEMODE, "IDirectDraw_CreateSurface returned %08x\n", rc);
292         ok(surface == NULL, "Returned surface pointer is %p\n", surface);
293     }
294     if(surface && surface != (IDirectDrawSurface *)0xdeadbeef) IDirectDrawSurface_Release(surface);
295
296     /* Set the focus window */
297     rc = IDirectDraw_SetCooperativeLevel(lpDD,
298         hwnd, DDSCL_SETFOCUSWINDOW);
299
300     if (rc == DDERR_INVALIDPARAMS)
301     {
302         win_skip("NT4/Win95 do not support cooperative levels DDSCL_SETDEVICEWINDOW and DDSCL_SETFOCUSWINDOW\n");
303         return;
304     }
305
306     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
307
308     /* Set the focus window a second time*/
309     rc = IDirectDraw_SetCooperativeLevel(lpDD,
310         hwnd, DDSCL_SETFOCUSWINDOW);
311     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) the second time returned: %x\n",rc);
312
313     /* Test DDSCL_SETFOCUSWINDOW with the other flags. They should all fail, except of DDSCL_NOWINDOWCHANGES */
314     rc = IDirectDraw_SetCooperativeLevel(lpDD,
315         hwnd, DDSCL_NORMAL | DDSCL_SETFOCUSWINDOW);
316     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
317
318     rc = IDirectDraw_SetCooperativeLevel(lpDD,
319         hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETFOCUSWINDOW);
320     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
321
322     /* This one succeeds */
323     rc = IDirectDraw_SetCooperativeLevel(lpDD,
324         hwnd, DDSCL_NOWINDOWCHANGES | DDSCL_SETFOCUSWINDOW);
325     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NOWINDOWCHANGES | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
326
327     rc = IDirectDraw_SetCooperativeLevel(lpDD,
328         hwnd, DDSCL_MULTITHREADED | DDSCL_SETFOCUSWINDOW);
329     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_MULTITHREADED | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
330
331     rc = IDirectDraw_SetCooperativeLevel(lpDD,
332         hwnd, DDSCL_FPUSETUP | DDSCL_SETFOCUSWINDOW);
333     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FPUSETUP | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
334
335     rc = IDirectDraw_SetCooperativeLevel(lpDD,
336         hwnd, DDSCL_FPUPRESERVE | DDSCL_SETFOCUSWINDOW);
337     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FPUPRESERVE | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
338
339     rc = IDirectDraw_SetCooperativeLevel(lpDD,
340         hwnd, DDSCL_ALLOWREBOOT | DDSCL_SETFOCUSWINDOW);
341     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_ALLOWREBOOT | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
342
343     rc = IDirectDraw_SetCooperativeLevel(lpDD,
344         hwnd, DDSCL_ALLOWMODEX | DDSCL_SETFOCUSWINDOW);
345     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_ALLOWMODEX | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
346
347     /* Set the device window without any other flags. Should give an error */
348     rc = IDirectDraw_SetCooperativeLevel(lpDD,
349         hwnd, DDSCL_SETDEVICEWINDOW);
350     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_SETDEVICEWINDOW) returned: %x\n",rc);
351
352     /* Set device window with DDSCL_NORMAL */
353     rc = IDirectDraw_SetCooperativeLevel(lpDD,
354         hwnd, DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW);
355     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW) returned: %x\n",rc);
356  
357     /* Also set the focus window. Should give an error */
358     rc = IDirectDraw_SetCooperativeLevel(lpDD,
359         hwnd, DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETDEVICEWINDOW | DDSCL_SETFOCUSWINDOW);
360     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
361
362     /* All done */
363 }
364
365 static void testcooperativelevels_exclusive(void)
366 {
367     HRESULT rc;
368
369     /* Do some tests with DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN mode */
370
371     /* Try to set exclusive mode only */
372     rc = IDirectDraw_SetCooperativeLevel(lpDD,
373         hwnd, DDSCL_EXCLUSIVE);
374     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_EXCLUSIVE) returned: %x\n",rc);
375
376     /* Full screen mode only */
377     rc = IDirectDraw_SetCooperativeLevel(lpDD,
378         hwnd, DDSCL_FULLSCREEN);
379     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FULLSCREEN) returned: %x\n",rc);
380
381     /* Full screen mode + exclusive mode */
382     rc = IDirectDraw_SetCooperativeLevel(lpDD,
383         hwnd, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
384     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN) returned: %x\n",rc);
385
386     /* Set the focus window. Should fail */
387     rc = IDirectDraw_SetCooperativeLevel(lpDD,
388         hwnd, DDSCL_SETFOCUSWINDOW);
389     ok(rc==DDERR_HWNDALREADYSET ||
390        broken(rc==DDERR_INVALIDPARAMS) /* NT4/Win95 */,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
391
392
393     /* All done */
394 }
395
396 static void testddraw3(void)
397 {
398     const GUID My_IID_IDirectDraw3 = {
399         0x618f8ad4,
400         0x8b7a,
401         0x11d0,
402         { 0x8f,0xcc,0x0,0xc0,0x4f,0xd9,0x18,0x9d }
403     };
404     IDirectDraw3 *dd3;
405     HRESULT hr;
406     hr = IDirectDraw_QueryInterface(lpDD, &My_IID_IDirectDraw3, (void **) &dd3);
407     ok(hr == E_NOINTERFACE, "QueryInterface for IID_IDirectDraw3 returned 0x%08x, expected E_NOINTERFACE\n", hr);
408     if(SUCCEEDED(hr) && dd3) IDirectDraw3_Release(dd3);
409 }
410
411 START_TEST(ddrawmodes)
412 {
413     createwindow();
414     if (!createdirectdraw())
415         return;
416     enumdisplaymodes();
417     if (winetest_interactive)
418         testdisplaymodes();
419     flushdisplaymodes();
420     testddraw3();
421     releasedirectdraw();
422
423     createdirectdraw();
424     testcooperativelevels_normal();
425     releasedirectdraw();
426
427     createdirectdraw();
428     testcooperativelevels_exclusive();
429     releasedirectdraw();
430 }