Release 1.4.1.
[wine] / dlls / ddrawex / tests / ddrawex.c
1 /*
2  * Unit tests for ddrawex specific things
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 #define COBJMACROS
19
20 #include "wine/test.h"
21 #include "windef.h"
22 #include "winbase.h"
23 #include "ddraw.h"
24 #include "ddrawex.h"
25 #include "unknwn.h"
26
27 static IDirectDrawFactory *factory;
28 static HRESULT (WINAPI *pDllGetClassObject)(REFCLSID rclsid, REFIID riid, LPVOID *ppv);
29
30 static IDirectDraw *createDD(void)
31 {
32     HRESULT hr;
33     IDirectDraw *dd;
34
35     hr = IDirectDrawFactory_CreateDirectDraw(factory, NULL, NULL, DDSCL_NORMAL, 0,
36                                              0, &dd);
37     ok(hr == DD_OK, "Failed to create an IDirectDraw interface, hr = 0x%08x\n", hr);
38     return SUCCEEDED(hr) ? dd : NULL;
39 }
40
41 static ULONG get_ref(IUnknown *o)
42 {
43     IUnknown_AddRef(o);
44     return IUnknown_Release(o);
45 }
46
47 static void RefCountTest(void)
48 {
49     IDirectDraw *dd1 = createDD();
50     IDirectDraw2 *dd2;
51     IDirectDraw3 *dd3;
52     IDirectDraw4 *dd4;
53     ULONG ref;
54
55     ref = get_ref((IUnknown *) dd1);
56     ok(ref == 1, "A new ddraw object's refcount is %u, expected 1\n", ref);
57
58     IDirectDraw_AddRef(dd1);
59     ref = get_ref((IUnknown *) dd1);
60     if (ref == 1)
61     {
62         win_skip("Refcounting is broken\n");
63         IDirectDraw_Release(dd1);
64         return;
65     }
66     ok(ref == 2, "After AddRef the refcount is %u, expected 2\n", ref);
67     IDirectDraw_Release(dd1);
68     ref = get_ref((IUnknown *) dd1);
69     ok(ref == 1, "After Release the refcount is %u, expected 1\n", ref);
70
71     IDirectDraw_QueryInterface(dd1, &IID_IDirectDraw2, (void **) &dd2);
72     ref = get_ref((IUnknown *) dd2);
73     ok(ref == 2, "IDirectDraw2 refcount is %u, expected 2\n", ref);
74
75     IDirectDraw_QueryInterface(dd1, &IID_IDirectDraw3, (void **) &dd3);
76     ref = get_ref((IUnknown *) dd3);
77     ok(ref == 3, "IDirectDraw3 refcount is %u, expected 3\n", ref);
78
79     IDirectDraw_QueryInterface(dd1, &IID_IDirectDraw4, (void **) &dd4);
80     ref = get_ref((IUnknown *) dd4);
81     ok(ref == 4, "IDirectDraw4 refcount is %u, expected 4\n", ref);
82
83     IDirectDraw_Release(dd1);
84     IDirectDraw2_Release(dd2);
85     IDirectDraw3_Release(dd3);
86
87     ref = get_ref((IUnknown *) dd4);
88     ok(ref == 1, "IDirectDraw4 refcount is %u, expected 1\n", ref);
89
90     IDirectDraw4_Release(dd4);
91 }
92
93 START_TEST(ddrawex)
94 {
95     IClassFactory *classfactory = NULL;
96     ULONG ref;
97     HRESULT hr;
98     HMODULE hmod = LoadLibrary("ddrawex.dll");
99     if(hmod == NULL) {
100         skip("Failed to load ddrawex.dll\n");
101         return;
102     }
103     pDllGetClassObject = (void*)GetProcAddress(hmod, "DllGetClassObject");
104     if(pDllGetClassObject == NULL) {
105         skip("Failed to get DllGetClassObject\n");
106         return;
107     }
108
109     hr = pDllGetClassObject(&CLSID_DirectDrawFactory, &IID_IClassFactory, (void **) &classfactory);
110     ok(hr == S_OK, "Failed to create a IClassFactory\n");
111     hr = IClassFactory_CreateInstance(classfactory, NULL, &IID_IDirectDrawFactory, (void **) &factory);
112     ok(hr == S_OK, "Failed to create a IDirectDrawFactory\n");
113
114     RefCountTest();
115
116     if(factory) {
117         ref = IDirectDrawFactory_Release(factory);
118         ok(ref == 0, "IDirectDrawFactory not cleanly released\n");
119     }
120     if(classfactory) {
121         ref = IClassFactory_Release(classfactory);
122         todo_wine ok(ref == 1, "IClassFactory refcount wrong, ref = %u\n", ref);
123     }
124 }