user32/tests: Define the required Windows version before including anything.
[wine] / dlls / user32 / tests / clipboard.c
1 /*
2  * Unit test suite for clipboard functions.
3  *
4  * Copyright 2002 Dmitry Timoshkov
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "wine/test.h"
22 #include "winbase.h"
23 #include "winerror.h"
24 #include "wingdi.h"
25 #include "winuser.h"
26
27 static BOOL is_win9x = FALSE;
28
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)
37
38 static void test_ClipboardOwner(void)
39 {
40     HWND hWnd1, hWnd2;
41     BOOL ret;
42
43     SetLastError(0xdeadbeef);
44     ok(!GetClipboardOwner() && GetLastError() == 0xdeadbeef,
45        "could not perform clipboard test: clipboard already owned\n");
46
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);
51
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);
56
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());
61
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());
67
68     ok(OpenClipboard(hWnd1), "OpenClipboard failed\n");
69     todo_wine ok(OpenClipboard(hWnd1), "OpenClipboard second time in the same hwnd failed\n");
70
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());
75
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());
81
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());
86
87     ret = CloseClipboard();
88     ok( ret, "CloseClipboard error %d\n", GetLastError());
89     ok(GetClipboardOwner() == hWnd1, "clipboard should still be owned\n");
90
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");
97 }
98
99 static void test_RegisterClipboardFormatA(void)
100 {
101     ATOM atom_id;
102     UINT format_id, format_id2;
103     char buf[256];
104     int len;
105     BOOL ret;
106
107     format_id = RegisterClipboardFormatA("my_cool_clipboard_format");
108     ok(format_id > 0xc000 && format_id < 0xffff, "invalid clipboard format id %04x\n", format_id);
109
110     format_id2 = RegisterClipboardFormatA("MY_COOL_CLIPBOARD_FORMAT");
111     ok(format_id2 == format_id, "invalid clipboard format id %04x\n", format_id2);
112
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);
116
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);
122
123 todo_wine
124 {
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);
130 }
131
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);
136
137     if (0)
138     {
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);
144     }
145
146     for (format_id = 0; format_id < 0xffff; format_id++)
147     {
148         SetLastError(0xdeadbeef);
149         len = GetClipboardFormatNameA(format_id, buf, 256);
150
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 : "");
155     }
156
157     ret = OpenClipboard(0);
158     ok( ret, "OpenClipboard error %d\n", GetLastError());
159
160     trace("# of formats available: %d\n", CountClipboardFormats());
161
162     format_id = 0;
163     while ((format_id = EnumClipboardFormats(format_id)))
164     {
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 : "");
168     }
169
170     ret = EmptyClipboard();
171     ok( ret, "EmptyClipboard error %d\n", GetLastError());
172     ret =CloseClipboard();
173     ok( ret, "CloseClipboard error %d\n", GetLastError());
174
175     if (CountClipboardFormats())
176     {
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());
181     }
182
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());
187
188     format_id = RegisterClipboardFormatA("#1234");
189     ok(format_id == 1234, "invalid clipboard format id %04x\n", format_id);
190 }
191
192 static HGLOBAL create_text(void)
193 {
194     HGLOBAL h = GlobalAlloc(GMEM_DDESHARE|GMEM_MOVEABLE, 5);
195     char *p = GlobalLock(h);
196     strcpy(p, "test");
197     GlobalUnlock(h);
198     return h;
199 }
200
201 static HENHMETAFILE create_emf(void)
202 {
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);
207 }
208
209 static void test_synthesized(void)
210 {
211     HGLOBAL h, htext;
212     HENHMETAFILE emf;
213     BOOL r;
214     UINT cf;
215     HANDLE data;
216
217     htext = create_text();
218     emf = create_emf();
219
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());
230
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);
237
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);
242
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);
250
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);
255
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);
261
262     cf = EnumClipboardFormats(cf);
263     ok(cf == 0, "cf %08x\n", cf);
264
265     r = EmptyClipboard();
266     ok(r, "gle %d\n", GetLastError());
267
268     r = CloseClipboard();
269     ok(r, "gle %d\n", GetLastError());
270 }
271
272 START_TEST(clipboard)
273 {
274     SetLastError(0xdeadbeef);
275     FindAtomW(NULL);
276     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) is_win9x = TRUE;
277
278     test_RegisterClipboardFormatA();
279     test_ClipboardOwner();
280     test_synthesized();
281 }