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 test_last_error(ERROR_CLIPBOARD_NOT_OPEN);
61 ok(OpenClipboard(0), "OpenClipboard failed\n");
62 ok(!GetClipboardOwner(), "clipboard should still be not owned\n");
63 ok(!OpenClipboard(hWnd1), "OpenClipboard should fail since clipboard already opened\n");
64 ret = CloseClipboard();
65 ok( ret, "CloseClipboard error %d\n", GetLastError());
67 ok(OpenClipboard(hWnd1), "OpenClipboard failed\n");
69 SetLastError(0xdeadbeef);
70 ok(!OpenClipboard(hWnd2) &&
71 (GetLastError() == 0xdeadbeef || GetLastError() == ERROR_ACCESS_DENIED),
72 "OpenClipboard should fail without setting last error value, or with ERROR_ACCESS_DENIED, got error %d\n", GetLastError());
74 SetLastError(0xdeadbeef);
75 ok(!GetClipboardOwner() && GetLastError() == 0xdeadbeef, "clipboard should still be not owned\n");
76 ret = EmptyClipboard();
77 ok( ret, "EmptyClipboard error %d\n", GetLastError());
78 ok(GetClipboardOwner() == hWnd1, "clipboard should be owned by %p, not by %p\n", hWnd1, GetClipboardOwner());
80 SetLastError(0xdeadbeef);
81 ok(!OpenClipboard(hWnd2) &&
82 (GetLastError() == 0xdeadbeef || GetLastError() == ERROR_ACCESS_DENIED),
83 "OpenClipboard should fail without setting last error valuei, or with ERROR_ACCESS_DENIED, got error %d\n", GetLastError());
85 ret = CloseClipboard();
86 ok( ret, "CloseClipboard error %d\n", GetLastError());
87 ok(GetClipboardOwner() == hWnd1, "clipboard should still be owned\n");
89 ret = DestroyWindow(hWnd1);
90 ok( ret, "DestroyWindow error %d\n", GetLastError());
91 ret = DestroyWindow(hWnd2);
92 ok( ret, "DestroyWindow error %d\n", GetLastError());
93 SetLastError(0xdeadbeef);
94 ok(!GetClipboardOwner() && GetLastError() == 0xdeadbeef, "clipboard should not be owned\n");
97 static void test_RegisterClipboardFormatA(void)
100 UINT format_id, format_id2;
105 format_id = RegisterClipboardFormatA("my_cool_clipboard_format");
106 ok(format_id > 0xc000 && format_id < 0xffff, "invalid clipboard format id %04x\n", format_id);
108 format_id2 = RegisterClipboardFormatA("MY_COOL_CLIPBOARD_FORMAT");
109 ok(format_id2 == format_id, "invalid clipboard format id %04x\n", format_id2);
111 len = GetClipboardFormatNameA(format_id, buf, 256);
112 ok(len == lstrlenA("my_cool_clipboard_format"), "wrong format name length %d\n", len);
113 ok(!lstrcmpA(buf, "my_cool_clipboard_format"), "wrong format name \"%s\"\n", buf);
115 lstrcpyA(buf, "foo");
116 SetLastError(0xdeadbeef);
117 len = GetAtomNameA((ATOM)format_id, buf, 256);
118 ok(len == 0, "GetAtomNameA should fail\n");
119 test_last_error(ERROR_INVALID_HANDLE);
123 lstrcpyA(buf, "foo");
124 SetLastError(0xdeadbeef);
125 len = GlobalGetAtomNameA((ATOM)format_id, buf, 256);
126 ok(len == 0, "GlobalGetAtomNameA should fail\n");
127 test_last_error(ERROR_INVALID_HANDLE);
130 SetLastError(0xdeadbeef);
131 atom_id = FindAtomA("my_cool_clipboard_format");
132 ok(atom_id == 0, "FindAtomA should fail\n");
133 test_last_error(ERROR_FILE_NOT_FOUND);
137 /* this relies on the clipboard and global atom table being different */
138 SetLastError(0xdeadbeef);
139 atom_id = GlobalFindAtomA("my_cool_clipboard_format");
140 ok(atom_id == 0, "GlobalFindAtomA should fail\n");
141 test_last_error(ERROR_FILE_NOT_FOUND);
143 for (format_id = 0; format_id < 0xffff; format_id++)
145 SetLastError(0xdeadbeef);
146 len = GetClipboardFormatNameA(format_id, buf, 256);
148 if (format_id < 0xc000)
150 ok(!len, "GetClipboardFormatNameA should fail, but it returned %d (%s)\n", len, buf);
151 test_last_error(ERROR_INVALID_PARAMETER);
156 trace("%04x: %s\n", format_id, len ? buf : "");
158 test_last_error(ERROR_INVALID_HANDLE);
163 ret = OpenClipboard(0);
164 ok( ret, "OpenClipboard error %d\n", GetLastError());
166 trace("# of formats available: %d\n", CountClipboardFormats());
169 while ((format_id = EnumClipboardFormats(format_id)))
171 ok(IsClipboardFormatAvailable(format_id), "format %04x was listed as available\n", format_id);
172 len = GetClipboardFormatNameA(format_id, buf, 256);
173 trace("%04x: %s\n", format_id, len ? buf : "");
176 ret = EmptyClipboard();
177 ok( ret, "EmptyClipboard error %d\n", GetLastError());
178 ret =CloseClipboard();
179 ok( ret, "CloseClipboard error %d\n", GetLastError());
181 if (CountClipboardFormats())
183 SetLastError(0xdeadbeef);
184 ok(!EnumClipboardFormats(0), "EnumClipboardFormats should fail if clipboard wasn't open\n");
185 ok(GetLastError() == ERROR_CLIPBOARD_NOT_OPEN,
186 "Last error should be set to ERROR_CLIPBOARD_NOT_OPEN, not %d\n", GetLastError());
189 SetLastError(0xdeadbeef);
190 ok(!EmptyClipboard(), "EmptyClipboard should fail if clipboard wasn't open\n");
191 test_last_error(ERROR_CLIPBOARD_NOT_OPEN);
194 static HGLOBAL create_text(void)
196 HGLOBAL h = GlobalAlloc(GMEM_DDESHARE|GMEM_MOVEABLE, 5);
197 char *p = GlobalLock(h);
203 static HENHMETAFILE create_emf(void)
205 const RECT rect = {0, 0, 100, 100};
206 HDC hdc = CreateEnhMetaFileA(NULL, NULL, &rect, "HENHMETAFILE Ole Clipboard Test\0Test\0\0");
207 ExtTextOutA(hdc, 0, 0, ETO_OPAQUE, &rect, "Test String", strlen("Test String"), NULL);
208 return CloseEnhMetaFile(hdc);
211 static void test_synthesized(void)
218 htext = create_text();
221 r = OpenClipboard(NULL);
222 ok(r, "gle %d\n", GetLastError());
223 r = EmptyClipboard();
224 ok(r, "gle %d\n", GetLastError());
225 h = SetClipboardData(CF_TEXT, htext);
226 ok(h == htext, "got %p\n", h);
227 h = SetClipboardData(CF_ENHMETAFILE, emf);
228 ok(h == emf, "got %p\n", h);
229 r = CloseClipboard();
230 ok(r, "gle %d\n", GetLastError());
232 r = OpenClipboard(NULL);
233 ok(r, "gle %d\n", GetLastError());
234 cf = EnumClipboardFormats(0);
235 ok(cf == CF_TEXT, "cf %08x\n", cf);
237 cf = EnumClipboardFormats(cf);
238 ok(cf == CF_ENHMETAFILE, "cf %08x\n", cf);
240 cf = EnumClipboardFormats(cf);
241 todo_wine ok(cf == CF_LOCALE, "cf %08x\n", cf);
243 cf = EnumClipboardFormats(cf);
244 ok(cf == CF_OEMTEXT, "cf %08x\n", cf);
246 cf = EnumClipboardFormats(cf);
247 ok(cf == CF_UNICODETEXT ||
248 broken(cf == CF_METAFILEPICT), /* win9x and winME has no CF_UNICODETEXT */
251 if(cf == CF_UNICODETEXT)
252 cf = EnumClipboardFormats(cf);
253 ok(cf == CF_METAFILEPICT, "cf %08x\n", cf);
255 cf = EnumClipboardFormats(cf);
256 ok(cf == 0, "cf %08x\n", cf);
258 r = EmptyClipboard();
259 ok(r, "gle %d\n", GetLastError());
261 r = CloseClipboard();
262 ok(r, "gle %d\n", GetLastError());
265 START_TEST(clipboard)
267 SetLastError(0xdeadbeef);
269 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) is_win9x = TRUE;
271 test_RegisterClipboardFormatA();
272 test_ClipboardOwner();