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"
27 static BOOL is_win9x
= FALSE
;
29 #define test_last_error(expected_error) \
33 ok(GetLastError() == expected_error, \
34 "Last error should be set to %d, not %d\n", \
35 expected_error, GetLastError()); \
38 static void test_ClipboardOwner(void)
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)
102 UINT format_id
, format_id2
;
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
);
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
);
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
);
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());
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
);
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)
217 htext
= create_text();
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
);
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 */
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 LRESULT CALLBACK
clipboard_wnd_proc(HWND hwnd
, UINT msg
, WPARAM wp
, LPARAM lp
)
276 case WM_DRAWCLIPBOARD
:
277 EnterCriticalSection(&clipboard_cs
);
278 LeaveCriticalSection(&clipboard_cs
);
285 return DefWindowProc(hwnd
, msg
, wp
, lp
);
288 static DWORD WINAPI
clipboard_thread(void *param
)
293 EnterCriticalSection(&clipboard_cs
);
294 SetLastError(0xdeadbeef);
295 SetClipboardViewer(win
);
296 ok(GetLastError() == 0xdeadbeef, "GetLastError = %d\n", GetLastError());
297 LeaveCriticalSection(&clipboard_cs
);
299 r
= OpenClipboard(win
);
300 ok(r
, "OpenClipboard failed: %d\n", GetLastError());
302 r
= EmptyClipboard();
303 ok(r
, "EmptyClipboard failed: %d\n", GetLastError());
305 EnterCriticalSection(&clipboard_cs
);
306 r
= CloseClipboard();
307 ok(r
, "CloseClipboard failed: %d\n", GetLastError());
308 LeaveCriticalSection(&clipboard_cs
);
310 r
= PostMessage(win
, WM_USER
, 0, 0);
311 ok(r
, "PostMessage failed: %d\n", GetLastError());
315 static void test_messages(void)
323 InitializeCriticalSection(&clipboard_cs
);
325 memset(&cls
, 0, sizeof(cls
));
326 cls
.lpfnWndProc
= clipboard_wnd_proc
;
327 cls
.hInstance
= GetModuleHandle(0);
328 cls
.lpszClassName
= "clipboard_test";
331 win
= CreateWindow("clipboard_test", NULL
, 0, 0, 0, 0, 0, NULL
, 0, NULL
, 0);
332 ok(win
!= NULL
, "CreateWindow failed: %d\n", GetLastError());
334 thread
= CreateThread(NULL
, 0, clipboard_thread
, (void*)win
, 0, &tid
);
335 ok(thread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
337 while(GetMessage(&msg
, NULL
, 0, 0)) {
338 TranslateMessage(&msg
);
339 DispatchMessage(&msg
);
342 ok(WaitForSingleObject(thread
, INFINITE
) == WAIT_OBJECT_0
, "WaitForSingleObject failed\n");
345 UnregisterClass("clipboard_test", GetModuleHandle(0));
346 DeleteCriticalSection(&clipboard_cs
);
349 START_TEST(clipboard
)
351 SetLastError(0xdeadbeef);
353 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
) is_win9x
= TRUE
;
355 test_RegisterClipboardFormatA();
356 test_ClipboardOwner();