1 /* Tests for Thread and SHGlobalCounter functions
3 * Copyright 2010 Detlef Riekenberg
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/test.h"
32 static HRESULT (WINAPI *pSHCreateThreadRef)(LONG*, IUnknown**);
33 static HRESULT (WINAPI *pSHGetThreadRef)(IUnknown**);
34 static HRESULT (WINAPI *pSHSetThreadRef)(IUnknown*);
36 static DWORD AddRef_called;
40 const IUnknownVtbl* lpVtbl;
44 static HRESULT WINAPI threadref_QueryInterface(IUnknown *iface, REFIID riid, LPVOID *ppvObj)
46 threadref * This = (threadref *)iface;
48 trace("unexpected QueryInterface(%p, %p, %p) called\n", This, riid, ppvObj);
53 static ULONG WINAPI threadref_AddRef(IUnknown *iface)
55 threadref * This = (threadref *)iface;
58 return InterlockedIncrement(This->ref);
61 static ULONG WINAPI threadref_Release(IUnknown *iface)
63 threadref * This = (threadref *)iface;
65 trace("unexpected Release(%p) called\n", This);
66 return InterlockedDecrement(This->ref);
70 static const IUnknownVtbl threadref_vt =
72 threadref_QueryInterface,
77 static void init_threadref(threadref* iface, LONG *refcount)
79 iface->lpVtbl = &threadref_vt;
80 iface->ref = refcount;
85 static void test_SHCreateThreadRef(void)
92 /* Not present before IE 6_XP_sp2 */
93 if (!pSHCreateThreadRef) {
94 win_skip("SHCreateThreadRef not found\n");
98 /* start with a clean state */
99 hr = pSHSetThreadRef(NULL);
100 ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
103 refcount = 0xdeadbeef;
104 hr = pSHCreateThreadRef(&refcount, &pobj);
105 ok((hr == S_OK) && pobj && (refcount == 1),
106 "got 0x%x and %p with %d (expected S_OK and '!= NULL' with 1)\n",
109 /* the object is not automatic set as ThreadRef */
111 hr = pSHGetThreadRef(&punk);
112 ok( (hr == E_NOINTERFACE) && (punk == NULL),
113 "got 0x%x and %p (expected E_NOINTERFACE and NULL)\n", hr, punk);
116 hr = pSHSetThreadRef(pobj);
117 ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
121 hr = pSHGetThreadRef(&punk);
122 ok( (hr == S_OK) && (punk == pobj) && (refcount == 2),
123 "got 0x%x and %p with %d (expected S_OK and %p with 2)\n",
124 hr, punk, refcount, pobj);
126 /* free the ref from SHGetThreadRef */
128 hr = IUnknown_Release(pobj);
129 ok((hr == 1) && (hr == refcount),
130 "got %d with %d (expected 1 with 1)\n", hr, refcount);
133 /* free the object */
135 hr = IUnknown_Release(pobj);
136 ok((hr == 0) && (hr == refcount),
137 "got %d with %d (expected 0 with 0)\n", hr, refcount);
141 /* the ThreadRef has still the pointer,
142 but the object no longer exist after the *_Release */
144 hr = pSHGetThreadRef(&punk);
145 trace("got 0x%x and %p with %d\n", hr, punk, refcount);
148 /* remove the dead object pointer */
149 hr = pSHSetThreadRef(NULL);
150 ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
152 /* parameter check */
154 /* vista: E_INVALIDARG, XP: crash */
156 hr = pSHCreateThreadRef(NULL, &pobj);
157 ok(hr == E_INVALIDARG, "got 0x%x (expected E_INVALIDARG)\n", hr);
159 refcount = 0xdeadbeef;
160 /* vista: E_INVALIDARG, XP: crash */
161 hr = pSHCreateThreadRef(&refcount, NULL);
162 ok( (hr == E_INVALIDARG) && (refcount == 0xdeadbeef),
163 "got 0x%x with 0x%x (expected E_INVALIDARG and oxdeadbeef)\n",
169 static void test_SHGetThreadRef(void)
174 /* Not present before IE 5 */
175 if (!pSHGetThreadRef) {
176 win_skip("SHGetThreadRef not found\n");
181 hr = pSHGetThreadRef(&punk);
182 ok( (hr == E_NOINTERFACE) && (punk == NULL),
183 "got 0x%x and %p (expected E_NOINTERFACE and NULL)\n", hr, punk);
186 /* this crash on Windows */
187 hr = pSHGetThreadRef(NULL);
191 static void test_SHSetThreadRef(void)
198 /* Not present before IE 5 */
199 if (!pSHSetThreadRef) {
200 win_skip("SHSetThreadRef not found\n");
204 /* start with a clean state */
205 hr = pSHSetThreadRef(NULL);
206 ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
208 /* build and set out object */
209 init_threadref(&ref, &refcount);
212 hr = pSHSetThreadRef( (IUnknown *)&ref);
213 ok( (hr == S_OK) && (refcount == 1) && (!AddRef_called),
214 "got 0x%x with %d, %d (expected S_OK with 1, 0)\n",
215 hr, refcount, AddRef_called);
217 /* read back our object */
221 hr = pSHGetThreadRef(&punk);
222 ok( (hr == S_OK) && (punk == (IUnknown *)&ref) && (refcount == 2) && (AddRef_called == 1),
223 "got 0x%x and %p with %d, %d (expected S_OK and %p with 2, 1)\n",
224 hr, punk, refcount, AddRef_called, &ref);
226 /* clear the object pointer */
227 hr = pSHSetThreadRef(NULL);
228 ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
230 /* verify, that our object is no longer known as ThreadRef */
231 hr = pSHGetThreadRef(&punk);
232 ok( (hr == E_NOINTERFACE) && (punk == NULL),
233 "got 0x%x and %p (expected E_NOINTERFACE and NULL)\n", hr, punk);
239 HMODULE hshlwapi = GetModuleHandleA("shlwapi.dll");
241 pSHCreateThreadRef = (void *) GetProcAddress(hshlwapi, "SHCreateThreadRef");
242 pSHGetThreadRef = (void *) GetProcAddress(hshlwapi, "SHGetThreadRef");
243 pSHSetThreadRef = (void *) GetProcAddress(hshlwapi, "SHSetThreadRef");
245 test_SHCreateThreadRef();
246 test_SHGetThreadRef();
247 test_SHSetThreadRef();