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");
70 SetLastError(0xdeadbeef);
71 ok(!OpenClipboard(hWnd2
) &&
72 (GetLastError() == 0xdeadbeef || GetLastError() == ERROR_ACCESS_DENIED
),
73 "OpenClipboard should fail without setting last error value, or with ERROR_ACCESS_DENIED, got error %d\n", GetLastError());
75 SetLastError(0xdeadbeef);
76 ok(!GetClipboardOwner() && GetLastError() == 0xdeadbeef, "clipboard should still be not owned\n");
77 ret
= EmptyClipboard();
78 ok( ret
, "EmptyClipboard error %d\n", GetLastError());
79 ok(GetClipboardOwner() == hWnd1
, "clipboard should be owned by %p, not by %p\n", hWnd1
, GetClipboardOwner());
81 SetLastError(0xdeadbeef);
82 ok(!OpenClipboard(hWnd2
) &&
83 (GetLastError() == 0xdeadbeef || GetLastError() == ERROR_ACCESS_DENIED
),
84 "OpenClipboard should fail without setting last error valuei, or with ERROR_ACCESS_DENIED, got error %d\n", GetLastError());
86 ret
= CloseClipboard();
87 ok( ret
, "CloseClipboard error %d\n", GetLastError());
88 ok(GetClipboardOwner() == hWnd1
, "clipboard should still be owned\n");
90 ret
= DestroyWindow(hWnd1
);
91 ok( ret
, "DestroyWindow error %d\n", GetLastError());
92 ret
= DestroyWindow(hWnd2
);
93 ok( ret
, "DestroyWindow error %d\n", GetLastError());
94 SetLastError(0xdeadbeef);
95 ok(!GetClipboardOwner() && GetLastError() == 0xdeadbeef, "clipboard should not be owned\n");
98 static void test_RegisterClipboardFormatA(void)
101 UINT format_id
, format_id2
;
106 format_id
= RegisterClipboardFormatA("my_cool_clipboard_format");
107 ok(format_id
> 0xc000 && format_id
< 0xffff, "invalid clipboard format id %04x\n", format_id
);
109 format_id2
= RegisterClipboardFormatA("MY_COOL_CLIPBOARD_FORMAT");
110 ok(format_id2
== format_id
, "invalid clipboard format id %04x\n", format_id2
);
112 len
= GetClipboardFormatNameA(format_id
, buf
, 256);
113 ok(len
== lstrlenA("my_cool_clipboard_format"), "wrong format name length %d\n", len
);
114 ok(!lstrcmpA(buf
, "my_cool_clipboard_format"), "wrong format name \"%s\"\n", buf
);
116 lstrcpyA(buf
, "foo");
117 SetLastError(0xdeadbeef);
118 len
= GetAtomNameA((ATOM
)format_id
, buf
, 256);
119 ok(len
== 0, "GetAtomNameA should fail\n");
120 test_last_error(ERROR_INVALID_HANDLE
);
124 lstrcpyA(buf
, "foo");
125 SetLastError(0xdeadbeef);
126 len
= GlobalGetAtomNameA((ATOM
)format_id
, buf
, 256);
127 ok(len
== 0, "GlobalGetAtomNameA should fail\n");
128 test_last_error(ERROR_INVALID_HANDLE
);
131 SetLastError(0xdeadbeef);
132 atom_id
= FindAtomA("my_cool_clipboard_format");
133 ok(atom_id
== 0, "FindAtomA should fail\n");
134 test_last_error(ERROR_FILE_NOT_FOUND
);
138 /* this relies on the clipboard and global atom table being different */
139 SetLastError(0xdeadbeef);
140 atom_id
= GlobalFindAtomA("my_cool_clipboard_format");
141 ok(atom_id
== 0, "GlobalFindAtomA should fail\n");
142 test_last_error(ERROR_FILE_NOT_FOUND
);
144 for (format_id
= 0; format_id
< 0xffff; format_id
++)
146 SetLastError(0xdeadbeef);
147 len
= GetClipboardFormatNameA(format_id
, buf
, 256);
149 if (format_id
< 0xc000)
151 ok(!len
, "GetClipboardFormatNameA should fail, but it returned %d (%s)\n", len
, buf
);
152 test_last_error(ERROR_INVALID_PARAMETER
);
157 trace("%04x: %s\n", format_id
, len
? buf
: "");
159 test_last_error(ERROR_INVALID_HANDLE
);
164 ret
= OpenClipboard(0);
165 ok( ret
, "OpenClipboard error %d\n", GetLastError());
167 trace("# of formats available: %d\n", CountClipboardFormats());
170 while ((format_id
= EnumClipboardFormats(format_id
)))
172 ok(IsClipboardFormatAvailable(format_id
), "format %04x was listed as available\n", format_id
);
173 len
= GetClipboardFormatNameA(format_id
, buf
, 256);
174 trace("%04x: %s\n", format_id
, len
? buf
: "");
177 ret
= EmptyClipboard();
178 ok( ret
, "EmptyClipboard error %d\n", GetLastError());
179 ret
=CloseClipboard();
180 ok( ret
, "CloseClipboard error %d\n", GetLastError());
182 if (CountClipboardFormats())
184 SetLastError(0xdeadbeef);
185 ok(!EnumClipboardFormats(0), "EnumClipboardFormats should fail if clipboard wasn't open\n");
186 ok(GetLastError() == ERROR_CLIPBOARD_NOT_OPEN
,
187 "Last error should be set to ERROR_CLIPBOARD_NOT_OPEN, not %d\n", GetLastError());
190 SetLastError(0xdeadbeef);
191 ok(!EmptyClipboard(), "EmptyClipboard should fail if clipboard wasn't open\n");
192 ok(GetLastError() == ERROR_CLIPBOARD_NOT_OPEN
|| broken(GetLastError() == 0xdeadbeef), /* wow64 */
193 "Wrong error %u\n", GetLastError());
196 static HGLOBAL
create_text(void)
198 HGLOBAL h
= GlobalAlloc(GMEM_DDESHARE
|GMEM_MOVEABLE
, 5);
199 char *p
= GlobalLock(h
);
205 static HENHMETAFILE
create_emf(void)
207 const RECT rect
= {0, 0, 100, 100};
208 HDC hdc
= CreateEnhMetaFileA(NULL
, NULL
, &rect
, "HENHMETAFILE Ole Clipboard Test\0Test\0\0");
209 ExtTextOutA(hdc
, 0, 0, ETO_OPAQUE
, &rect
, "Test String", strlen("Test String"), NULL
);
210 return CloseEnhMetaFile(hdc
);
213 static void test_synthesized(void)
220 htext
= create_text();
223 r
= OpenClipboard(NULL
);
224 ok(r
, "gle %d\n", GetLastError());
225 r
= EmptyClipboard();
226 ok(r
, "gle %d\n", GetLastError());
227 h
= SetClipboardData(CF_TEXT
, htext
);
228 ok(h
== htext
, "got %p\n", h
);
229 h
= SetClipboardData(CF_ENHMETAFILE
, emf
);
230 ok(h
== emf
, "got %p\n", h
);
231 r
= CloseClipboard();
232 ok(r
, "gle %d\n", GetLastError());
234 r
= OpenClipboard(NULL
);
235 ok(r
, "gle %d\n", GetLastError());
236 cf
= EnumClipboardFormats(0);
237 ok(cf
== CF_TEXT
, "cf %08x\n", cf
);
239 cf
= EnumClipboardFormats(cf
);
240 ok(cf
== CF_ENHMETAFILE
, "cf %08x\n", cf
);
242 cf
= EnumClipboardFormats(cf
);
243 todo_wine
ok(cf
== CF_LOCALE
, "cf %08x\n", cf
);
245 cf
= EnumClipboardFormats(cf
);
246 ok(cf
== CF_OEMTEXT
, "cf %08x\n", cf
);
248 cf
= EnumClipboardFormats(cf
);
249 ok(cf
== CF_UNICODETEXT
||
250 broken(cf
== CF_METAFILEPICT
), /* win9x and winME has no CF_UNICODETEXT */
253 if(cf
== CF_UNICODETEXT
)
254 cf
= EnumClipboardFormats(cf
);
255 ok(cf
== CF_METAFILEPICT
, "cf %08x\n", cf
);
257 cf
= EnumClipboardFormats(cf
);
258 ok(cf
== 0, "cf %08x\n", cf
);
260 r
= EmptyClipboard();
261 ok(r
, "gle %d\n", GetLastError());
263 r
= CloseClipboard();
264 ok(r
, "gle %d\n", GetLastError());
267 START_TEST(clipboard
)
269 SetLastError(0xdeadbeef);
271 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
) is_win9x
= TRUE
;
273 test_RegisterClipboardFormatA();
274 test_ClipboardOwner();