user32/tests: Fix some last error checks for WoW64.
[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     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());
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     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());
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     for (format_id = 0; format_id < 0xffff; format_id++)
145     {
146         SetLastError(0xdeadbeef);
147         len = GetClipboardFormatNameA(format_id, buf, 256);
148
149         if (format_id < 0xc000)
150         {
151             ok(!len, "GetClipboardFormatNameA should fail, but it returned %d (%s)\n", len, buf);
152             test_last_error(ERROR_INVALID_PARAMETER);
153         }
154         else
155         {
156             if (len)
157                 trace("%04x: %s\n", format_id, len ? buf : "");
158             else
159                 test_last_error(ERROR_INVALID_HANDLE);
160         }
161     }
162     }
163
164     ret = OpenClipboard(0);
165     ok( ret, "OpenClipboard error %d\n", GetLastError());
166
167     trace("# of formats available: %d\n", CountClipboardFormats());
168
169     format_id = 0;
170     while ((format_id = EnumClipboardFormats(format_id)))
171     {
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 : "");
175     }
176
177     ret = EmptyClipboard();
178     ok( ret, "EmptyClipboard error %d\n", GetLastError());
179     ret =CloseClipboard();
180     ok( ret, "CloseClipboard error %d\n", GetLastError());
181
182     if (CountClipboardFormats())
183     {
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());
188     }
189
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());
194 }
195
196 static HGLOBAL create_text(void)
197 {
198     HGLOBAL h = GlobalAlloc(GMEM_DDESHARE|GMEM_MOVEABLE, 5);
199     char *p = GlobalLock(h);
200     strcpy(p, "test");
201     GlobalUnlock(h);
202     return h;
203 }
204
205 static HENHMETAFILE create_emf(void)
206 {
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);
211 }
212
213 static void test_synthesized(void)
214 {
215     HGLOBAL h, htext;
216     HENHMETAFILE emf;
217     BOOL r;
218     UINT cf;
219
220     htext = create_text();
221     emf = create_emf();
222
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());
233
234     r = OpenClipboard(NULL);
235     ok(r, "gle %d\n", GetLastError());
236     cf = EnumClipboardFormats(0);
237     ok(cf == CF_TEXT, "cf %08x\n", cf);
238
239     cf = EnumClipboardFormats(cf);
240     ok(cf == CF_ENHMETAFILE, "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
248     cf = EnumClipboardFormats(cf);
249     ok(cf == CF_UNICODETEXT ||
250        broken(cf == CF_METAFILEPICT), /* win9x and winME has no CF_UNICODETEXT */
251        "cf %08x\n", cf);
252
253     if(cf == CF_UNICODETEXT)
254         cf = EnumClipboardFormats(cf);
255     ok(cf == CF_METAFILEPICT, "cf %08x\n", cf);
256
257     cf = EnumClipboardFormats(cf);
258     ok(cf == 0, "cf %08x\n", cf);
259
260     r = EmptyClipboard();
261     ok(r, "gle %d\n", GetLastError());
262
263     r = CloseClipboard();
264     ok(r, "gle %d\n", GetLastError());
265 }
266
267 START_TEST(clipboard)
268 {
269     SetLastError(0xdeadbeef);
270     FindAtomW(NULL);
271     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) is_win9x = TRUE;
272
273     test_RegisterClipboardFormatA();
274     test_ClipboardOwner();
275     test_synthesized();
276 }