user32/tests: Update some todos for changed test failure behavior.
[wine.git] / dlls / user32 / tests / clipboard.c
blob40218be4c7a1622682ec9bed9565071b25696e53
1 /*
2 * Unit test suite for clipboard functions.
4 * Copyright 2002 Dmitry Timoshkov
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.
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.
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
21 #include "wine/test.h"
22 #include "winbase.h"
23 #include "winerror.h"
24 #include "wingdi.h"
25 #include "winuser.h"
27 static BOOL is_win9x = FALSE;
29 #define test_last_error(expected_error) \
30 do \
31 { \
32 if (!is_win9x) \
33 ok(GetLastError() == expected_error, \
34 "Last error should be set to %d, not %d\n", \
35 expected_error, GetLastError()); \
36 } while (0)
38 static void test_ClipboardOwner(void)
40 HWND hWnd1, hWnd2;
41 BOOL ret;
43 SetLastError(0xdeadbeef);
44 ok(!GetClipboardOwner() && GetLastError() == 0xdeadbeef,
45 "could not perform clipboard test: clipboard already owned\n");
47 hWnd1 = CreateWindowExA(0, "static", NULL, WS_POPUP,
48 0, 0, 10, 10, 0, 0, 0, NULL);
49 ok(hWnd1 != 0, "CreateWindowExA error %d\n", GetLastError());
50 trace("hWnd1 = %p\n", hWnd1);
52 hWnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP,
53 0, 0, 10, 10, 0, 0, 0, NULL);
54 ok(hWnd2 != 0, "CreateWindowExA error %d\n", GetLastError());
55 trace("hWnd2 = %p\n", hWnd2);
57 SetLastError(0xdeadbeef);
58 ok(!CloseClipboard(), "CloseClipboard should fail if clipboard wasn't open\n");
59 ok(GetLastError() == ERROR_CLIPBOARD_NOT_OPEN || broken(GetLastError() == 0xdeadbeef), /* wow64 */
60 "wrong error %u\n", GetLastError());
62 ok(OpenClipboard(0), "OpenClipboard failed\n");
63 ok(!GetClipboardOwner(), "clipboard should still be not owned\n");
64 ok(!OpenClipboard(hWnd1), "OpenClipboard should fail since clipboard already opened\n");
65 ret = CloseClipboard();
66 ok( ret, "CloseClipboard error %d\n", GetLastError());
68 ok(OpenClipboard(hWnd1), "OpenClipboard failed\n");
69 todo_wine ok(OpenClipboard(hWnd1), "OpenClipboard second time in the same hwnd failed\n");
71 SetLastError(0xdeadbeef);
72 ret = OpenClipboard(hWnd2);
73 ok(!ret && (GetLastError() == 0xdeadbeef || GetLastError() == ERROR_ACCESS_DENIED),
74 "OpenClipboard should fail without setting last error value, or with ERROR_ACCESS_DENIED, got error %d\n", GetLastError());
76 SetLastError(0xdeadbeef);
77 ok(!GetClipboardOwner() && GetLastError() == 0xdeadbeef, "clipboard should still be not owned\n");
78 ret = EmptyClipboard();
79 ok( ret, "EmptyClipboard error %d\n", GetLastError());
80 ok(GetClipboardOwner() == hWnd1, "clipboard should be owned by %p, not by %p\n", hWnd1, GetClipboardOwner());
82 SetLastError(0xdeadbeef);
83 ret = OpenClipboard(hWnd2);
84 ok(!ret && (GetLastError() == 0xdeadbeef || GetLastError() == ERROR_ACCESS_DENIED),
85 "OpenClipboard should fail without setting last error value, or with ERROR_ACCESS_DENIED, got error %d\n", GetLastError());
87 ret = CloseClipboard();
88 ok( ret, "CloseClipboard error %d\n", GetLastError());
89 ok(GetClipboardOwner() == hWnd1, "clipboard should still be owned\n");
91 ret = DestroyWindow(hWnd1);
92 ok( ret, "DestroyWindow error %d\n", GetLastError());
93 ret = DestroyWindow(hWnd2);
94 ok( ret, "DestroyWindow error %d\n", GetLastError());
95 SetLastError(0xdeadbeef);
96 ok(!GetClipboardOwner() && GetLastError() == 0xdeadbeef, "clipboard should not be owned\n");
99 static void test_RegisterClipboardFormatA(void)
101 ATOM atom_id;
102 UINT format_id, format_id2;
103 char buf[256];
104 int len;
105 BOOL ret;
107 format_id = RegisterClipboardFormatA("my_cool_clipboard_format");
108 ok(format_id > 0xc000 && format_id < 0xffff, "invalid clipboard format id %04x\n", format_id);
110 format_id2 = RegisterClipboardFormatA("MY_COOL_CLIPBOARD_FORMAT");
111 ok(format_id2 == format_id, "invalid clipboard format id %04x\n", format_id2);
113 len = GetClipboardFormatNameA(format_id, buf, 256);
114 ok(len == lstrlenA("my_cool_clipboard_format"), "wrong format name length %d\n", len);
115 ok(!lstrcmpA(buf, "my_cool_clipboard_format"), "wrong format name \"%s\"\n", buf);
117 lstrcpyA(buf, "foo");
118 SetLastError(0xdeadbeef);
119 len = GetAtomNameA((ATOM)format_id, buf, 256);
120 ok(len == 0, "GetAtomNameA should fail\n");
121 test_last_error(ERROR_INVALID_HANDLE);
123 todo_wine
125 lstrcpyA(buf, "foo");
126 SetLastError(0xdeadbeef);
127 len = GlobalGetAtomNameA((ATOM)format_id, buf, 256);
128 ok(len == 0, "GlobalGetAtomNameA should fail\n");
129 test_last_error(ERROR_INVALID_HANDLE);
132 SetLastError(0xdeadbeef);
133 atom_id = FindAtomA("my_cool_clipboard_format");
134 ok(atom_id == 0, "FindAtomA should fail\n");
135 test_last_error(ERROR_FILE_NOT_FOUND);
137 if (0)
139 /* this relies on the clipboard and global atom table being different */
140 SetLastError(0xdeadbeef);
141 atom_id = GlobalFindAtomA("my_cool_clipboard_format");
142 ok(atom_id == 0, "GlobalFindAtomA should fail\n");
143 test_last_error(ERROR_FILE_NOT_FOUND);
146 for (format_id = 0; format_id < 0xffff; format_id++)
148 SetLastError(0xdeadbeef);
149 len = GetClipboardFormatNameA(format_id, buf, 256);
151 if (format_id < 0xc000)
152 ok(!len, "GetClipboardFormatNameA should fail, but it returned %d (%s)\n", len, buf);
153 else
154 if (len) trace("%04x: %s\n", format_id, len ? buf : "");
157 ret = OpenClipboard(0);
158 ok( ret, "OpenClipboard error %d\n", GetLastError());
160 trace("# of formats available: %d\n", CountClipboardFormats());
162 format_id = 0;
163 while ((format_id = EnumClipboardFormats(format_id)))
165 ok(IsClipboardFormatAvailable(format_id), "format %04x was listed as available\n", format_id);
166 len = GetClipboardFormatNameA(format_id, buf, 256);
167 trace("%04x: %s\n", format_id, len ? buf : "");
170 ret = EmptyClipboard();
171 ok( ret, "EmptyClipboard error %d\n", GetLastError());
172 ret =CloseClipboard();
173 ok( ret, "CloseClipboard error %d\n", GetLastError());
175 if (CountClipboardFormats())
177 SetLastError(0xdeadbeef);
178 ok(!EnumClipboardFormats(0), "EnumClipboardFormats should fail if clipboard wasn't open\n");
179 ok(GetLastError() == ERROR_CLIPBOARD_NOT_OPEN,
180 "Last error should be set to ERROR_CLIPBOARD_NOT_OPEN, not %d\n", GetLastError());
183 SetLastError(0xdeadbeef);
184 ok(!EmptyClipboard(), "EmptyClipboard should fail if clipboard wasn't open\n");
185 ok(GetLastError() == ERROR_CLIPBOARD_NOT_OPEN || broken(GetLastError() == 0xdeadbeef), /* wow64 */
186 "Wrong error %u\n", GetLastError());
188 format_id = RegisterClipboardFormatA("#1234");
189 ok(format_id == 1234, "invalid clipboard format id %04x\n", format_id);
192 static HGLOBAL create_text(void)
194 HGLOBAL h = GlobalAlloc(GMEM_DDESHARE|GMEM_MOVEABLE, 5);
195 char *p = GlobalLock(h);
196 strcpy(p, "test");
197 GlobalUnlock(h);
198 return h;
201 static HENHMETAFILE create_emf(void)
203 const RECT rect = {0, 0, 100, 100};
204 HDC hdc = CreateEnhMetaFileA(NULL, NULL, &rect, "HENHMETAFILE Ole Clipboard Test\0Test\0\0");
205 ExtTextOutA(hdc, 0, 0, ETO_OPAQUE, &rect, "Test String", strlen("Test String"), NULL);
206 return CloseEnhMetaFile(hdc);
209 static void test_synthesized(void)
211 HGLOBAL h, htext;
212 HENHMETAFILE emf;
213 BOOL r;
214 UINT cf;
215 HANDLE data;
217 htext = create_text();
218 emf = create_emf();
220 r = OpenClipboard(NULL);
221 ok(r, "gle %d\n", GetLastError());
222 r = EmptyClipboard();
223 ok(r, "gle %d\n", GetLastError());
224 h = SetClipboardData(CF_TEXT, htext);
225 ok(h == htext, "got %p\n", h);
226 h = SetClipboardData(CF_ENHMETAFILE, emf);
227 ok(h == emf, "got %p\n", h);
228 r = CloseClipboard();
229 ok(r, "gle %d\n", GetLastError());
231 r = OpenClipboard(NULL);
232 ok(r, "gle %d\n", GetLastError());
233 cf = EnumClipboardFormats(0);
234 ok(cf == CF_TEXT, "cf %08x\n", cf);
235 data = GetClipboardData(cf);
236 ok(data != NULL, "couldn't get data, cf %08x\n", cf);
238 cf = EnumClipboardFormats(cf);
239 ok(cf == CF_ENHMETAFILE, "cf %08x\n", cf);
240 data = GetClipboardData(cf);
241 ok(data != NULL, "couldn't get data, cf %08x\n", cf);
243 cf = EnumClipboardFormats(cf);
244 todo_wine ok(cf == CF_LOCALE, "cf %08x\n", cf);
245 if(cf == CF_LOCALE)
246 cf = EnumClipboardFormats(cf);
247 ok(cf == CF_OEMTEXT, "cf %08x\n", cf);
248 data = GetClipboardData(cf);
249 ok(data != NULL, "couldn't get data, cf %08x\n", cf);
251 cf = EnumClipboardFormats(cf);
252 ok(cf == CF_UNICODETEXT ||
253 broken(cf == CF_METAFILEPICT), /* win9x and winME has no CF_UNICODETEXT */
254 "cf %08x\n", cf);
256 if(cf == CF_UNICODETEXT)
257 cf = EnumClipboardFormats(cf);
258 ok(cf == CF_METAFILEPICT, "cf %08x\n", cf);
259 data = GetClipboardData(cf);
260 todo_wine ok(data != NULL, "couldn't get data, cf %08x\n", cf);
262 cf = EnumClipboardFormats(cf);
263 ok(cf == 0, "cf %08x\n", cf);
265 r = EmptyClipboard();
266 ok(r, "gle %d\n", GetLastError());
268 r = CloseClipboard();
269 ok(r, "gle %d\n", GetLastError());
272 static CRITICAL_SECTION clipboard_cs;
273 static HWND next_wnd;
274 static LRESULT CALLBACK clipboard_wnd_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
276 switch(msg) {
277 case WM_DRAWCLIPBOARD:
278 EnterCriticalSection(&clipboard_cs);
279 LeaveCriticalSection(&clipboard_cs);
280 break;
281 case WM_CHANGECBCHAIN:
282 if (next_wnd == (HWND)wp)
283 next_wnd = (HWND)lp;
284 else if (next_wnd)
285 SendMessageA(next_wnd, msg, wp, lp);
286 break;
287 case WM_USER:
288 ChangeClipboardChain(hwnd, next_wnd);
289 PostQuitMessage(0);
290 break;
293 return DefWindowProcA(hwnd, msg, wp, lp);
296 static DWORD WINAPI clipboard_thread(void *param)
298 HWND win = param;
299 BOOL r;
301 EnterCriticalSection(&clipboard_cs);
302 SetLastError(0xdeadbeef);
303 next_wnd = SetClipboardViewer(win);
304 ok(GetLastError() == 0xdeadbeef, "GetLastError = %d\n", GetLastError());
305 LeaveCriticalSection(&clipboard_cs);
307 r = OpenClipboard(win);
308 ok(r, "OpenClipboard failed: %d\n", GetLastError());
310 r = EmptyClipboard();
311 ok(r, "EmptyClipboard failed: %d\n", GetLastError());
313 EnterCriticalSection(&clipboard_cs);
314 r = CloseClipboard();
315 ok(r, "CloseClipboard failed: %d\n", GetLastError());
316 LeaveCriticalSection(&clipboard_cs);
318 r = PostMessageA(win, WM_USER, 0, 0);
319 ok(r, "PostMessage failed: %d\n", GetLastError());
320 return 0;
323 static void test_messages(void)
325 WNDCLASSA cls;
326 HWND win;
327 MSG msg;
328 HANDLE thread;
329 DWORD tid;
331 InitializeCriticalSection(&clipboard_cs);
333 memset(&cls, 0, sizeof(cls));
334 cls.lpfnWndProc = clipboard_wnd_proc;
335 cls.hInstance = GetModuleHandleA(NULL);
336 cls.lpszClassName = "clipboard_test";
337 RegisterClassA(&cls);
339 win = CreateWindowA("clipboard_test", NULL, 0, 0, 0, 0, 0, NULL, 0, NULL, 0);
340 ok(win != NULL, "CreateWindow failed: %d\n", GetLastError());
342 thread = CreateThread(NULL, 0, clipboard_thread, (void*)win, 0, &tid);
343 ok(thread != NULL, "CreateThread failed: %d\n", GetLastError());
345 while(GetMessageA(&msg, NULL, 0, 0)) {
346 TranslateMessage(&msg);
347 DispatchMessageA(&msg);
350 ok(WaitForSingleObject(thread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
351 CloseHandle(thread);
353 UnregisterClassA("clipboard_test", GetModuleHandleA(NULL));
354 DeleteCriticalSection(&clipboard_cs);
357 START_TEST(clipboard)
359 SetLastError(0xdeadbeef);
360 FindAtomW(NULL);
361 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) is_win9x = TRUE;
363 test_RegisterClipboardFormatA();
364 test_ClipboardOwner();
365 test_synthesized();
366 test_messages();