user32/tests: Skip some tests if not privileged enough to call CreateWindowStation().
[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
70     SetLastError(0xdeadbeef);
71     ret = OpenClipboard(hWnd2);
72     ok(!ret && (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());
74
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());
80
81     SetLastError(0xdeadbeef);
82     ret = OpenClipboard(hWnd2);
83     ok(!ret && (GetLastError() == 0xdeadbeef || GetLastError() == ERROR_ACCESS_DENIED),
84        "OpenClipboard should fail without setting last error value, or with ERROR_ACCESS_DENIED, got error %d\n", GetLastError());
85
86     ret = CloseClipboard();
87     ok( ret, "CloseClipboard error %d\n", GetLastError());
88     ok(GetClipboardOwner() == hWnd1, "clipboard should still be owned\n");
89
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");
96 }
97
98 static void test_RegisterClipboardFormatA(void)
99 {
100     ATOM atom_id;
101     UINT format_id, format_id2;
102     char buf[256];
103     int len;
104     BOOL ret;
105
106     format_id = RegisterClipboardFormatA("my_cool_clipboard_format");
107     ok(format_id > 0xc000 && format_id < 0xffff, "invalid clipboard format id %04x\n", format_id);
108
109     format_id2 = RegisterClipboardFormatA("MY_COOL_CLIPBOARD_FORMAT");
110     ok(format_id2 == format_id, "invalid clipboard format id %04x\n", format_id2);
111
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);
115
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);
121
122 todo_wine
123 {
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);
129 }
130
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);
135
136     if (0)
137     {
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);
143     }
144
145     for (format_id = 0; format_id < 0xffff; format_id++)
146     {
147         SetLastError(0xdeadbeef);
148         len = GetClipboardFormatNameA(format_id, buf, 256);
149
150         if (format_id < 0xc000)
151             ok(!len, "GetClipboardFormatNameA should fail, but it returned %d (%s)\n", len, buf);
152         else
153             if (len) trace("%04x: %s\n", format_id, len ? buf : "");
154     }
155
156     ret = OpenClipboard(0);
157     ok( ret, "OpenClipboard error %d\n", GetLastError());
158
159     trace("# of formats available: %d\n", CountClipboardFormats());
160
161     format_id = 0;
162     while ((format_id = EnumClipboardFormats(format_id)))
163     {
164         ok(IsClipboardFormatAvailable(format_id), "format %04x was listed as available\n", format_id);
165         len = GetClipboardFormatNameA(format_id, buf, 256);
166         trace("%04x: %s\n", format_id, len ? buf : "");
167     }
168
169     ret = EmptyClipboard();
170     ok( ret, "EmptyClipboard error %d\n", GetLastError());
171     ret =CloseClipboard();
172     ok( ret, "CloseClipboard error %d\n", GetLastError());
173
174     if (CountClipboardFormats())
175     {
176         SetLastError(0xdeadbeef);
177         ok(!EnumClipboardFormats(0), "EnumClipboardFormats should fail if clipboard wasn't open\n");
178         ok(GetLastError() == ERROR_CLIPBOARD_NOT_OPEN,
179            "Last error should be set to ERROR_CLIPBOARD_NOT_OPEN, not %d\n", GetLastError());
180     }
181
182     SetLastError(0xdeadbeef);
183     ok(!EmptyClipboard(), "EmptyClipboard should fail if clipboard wasn't open\n");
184     ok(GetLastError() == ERROR_CLIPBOARD_NOT_OPEN || broken(GetLastError() == 0xdeadbeef), /* wow64 */
185        "Wrong error %u\n", GetLastError());
186
187     format_id = RegisterClipboardFormatA("#1234");
188     ok(format_id == 1234, "invalid clipboard format id %04x\n", format_id);
189 }
190
191 static HGLOBAL create_text(void)
192 {
193     HGLOBAL h = GlobalAlloc(GMEM_DDESHARE|GMEM_MOVEABLE, 5);
194     char *p = GlobalLock(h);
195     strcpy(p, "test");
196     GlobalUnlock(h);
197     return h;
198 }
199
200 static HENHMETAFILE create_emf(void)
201 {
202     const RECT rect = {0, 0, 100, 100};
203     HDC hdc = CreateEnhMetaFileA(NULL, NULL, &rect, "HENHMETAFILE Ole Clipboard Test\0Test\0\0");
204     ExtTextOutA(hdc, 0, 0, ETO_OPAQUE, &rect, "Test String", strlen("Test String"), NULL);
205     return CloseEnhMetaFile(hdc);
206 }
207
208 static void test_synthesized(void)
209 {
210     HGLOBAL h, htext;
211     HENHMETAFILE emf;
212     BOOL r;
213     UINT cf;
214     HANDLE data;
215
216     htext = create_text();
217     emf = create_emf();
218
219     r = OpenClipboard(NULL);
220     ok(r, "gle %d\n", GetLastError());
221     r = EmptyClipboard();
222     ok(r, "gle %d\n", GetLastError());
223     h = SetClipboardData(CF_TEXT, htext);
224     ok(h == htext, "got %p\n", h);
225     h = SetClipboardData(CF_ENHMETAFILE, emf);
226     ok(h == emf, "got %p\n", h);
227     r = CloseClipboard();
228     ok(r, "gle %d\n", GetLastError());
229
230     r = OpenClipboard(NULL);
231     ok(r, "gle %d\n", GetLastError());
232     cf = EnumClipboardFormats(0);
233     ok(cf == CF_TEXT, "cf %08x\n", cf);
234     data = GetClipboardData(cf);
235     ok(data != NULL, "couldn't get data, cf %08x\n", cf);
236
237     cf = EnumClipboardFormats(cf);
238     ok(cf == CF_ENHMETAFILE, "cf %08x\n", cf);
239     data = GetClipboardData(cf);
240     ok(data != NULL, "couldn't get data, cf %08x\n", cf);
241
242     cf = EnumClipboardFormats(cf);
243     todo_wine ok(cf == CF_LOCALE, "cf %08x\n", cf);
244     if(cf == CF_LOCALE)
245         cf = EnumClipboardFormats(cf);
246     ok(cf == CF_OEMTEXT, "cf %08x\n", cf);
247     data = GetClipboardData(cf);
248     ok(data != NULL, "couldn't get data, cf %08x\n", cf);
249
250     cf = EnumClipboardFormats(cf);
251     ok(cf == CF_UNICODETEXT ||
252        broken(cf == CF_METAFILEPICT), /* win9x and winME has no CF_UNICODETEXT */
253        "cf %08x\n", cf);
254
255     if(cf == CF_UNICODETEXT)
256         cf = EnumClipboardFormats(cf);
257     ok(cf == CF_METAFILEPICT, "cf %08x\n", cf);
258     data = GetClipboardData(cf);
259     todo_wine ok(data != NULL, "couldn't get data, cf %08x\n", cf);
260
261     cf = EnumClipboardFormats(cf);
262     ok(cf == 0, "cf %08x\n", cf);
263
264     r = EmptyClipboard();
265     ok(r, "gle %d\n", GetLastError());
266
267     r = CloseClipboard();
268     ok(r, "gle %d\n", GetLastError());
269 }
270
271 START_TEST(clipboard)
272 {
273     SetLastError(0xdeadbeef);
274     FindAtomW(NULL);
275     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) is_win9x = TRUE;
276
277     test_RegisterClipboardFormatA();
278     test_ClipboardOwner();
279     test_synthesized();
280 }