ntdll: Free old memory block when reallocating to a large block.
[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     test_last_error(ERROR_CLIPBOARD_NOT_OPEN);
60
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());
66
67     ok(OpenClipboard(hWnd1), "OpenClipboard failed\n");
68
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());
73
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());
79
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());
84
85     ret = CloseClipboard();
86     ok( ret, "CloseClipboard error %d\n", GetLastError());
87     ok(GetClipboardOwner() == hWnd1, "clipboard should still be owned\n");
88
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");
95 }
96
97 static void test_RegisterClipboardFormatA(void)
98 {
99     ATOM atom_id;
100     UINT format_id, format_id2;
101     char buf[256];
102     int len;
103     BOOL ret;
104
105     format_id = RegisterClipboardFormatA("my_cool_clipboard_format");
106     ok(format_id > 0xc000 && format_id < 0xffff, "invalid clipboard format id %04x\n", format_id);
107
108     format_id2 = RegisterClipboardFormatA("MY_COOL_CLIPBOARD_FORMAT");
109     ok(format_id2 == format_id, "invalid clipboard format id %04x\n", format_id2);
110
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);
114
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);
120
121 todo_wine
122 {
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);
128 }
129
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);
134
135     if (0)
136     {
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);
142
143     for (format_id = 0; format_id < 0xffff; format_id++)
144     {
145         SetLastError(0xdeadbeef);
146         len = GetClipboardFormatNameA(format_id, buf, 256);
147
148         if (format_id < 0xc000)
149         {
150             ok(!len, "GetClipboardFormatNameA should fail, but it returned %d (%s)\n", len, buf);
151             test_last_error(ERROR_INVALID_PARAMETER);
152         }
153         else
154         {
155             if (len)
156                 trace("%04x: %s\n", format_id, len ? buf : "");
157             else
158                 test_last_error(ERROR_INVALID_HANDLE);
159         }
160     }
161     }
162
163     ret = OpenClipboard(0);
164     ok( ret, "OpenClipboard error %d\n", GetLastError());
165
166     trace("# of formats available: %d\n", CountClipboardFormats());
167
168     format_id = 0;
169     while ((format_id = EnumClipboardFormats(format_id)))
170     {
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 : "");
174     }
175
176     ret = EmptyClipboard();
177     ok( ret, "EmptyClipboard error %d\n", GetLastError());
178     ret =CloseClipboard();
179     ok( ret, "CloseClipboard error %d\n", GetLastError());
180
181     if (CountClipboardFormats())
182     {
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());
187     }
188
189     SetLastError(0xdeadbeef);
190     ok(!EmptyClipboard(), "EmptyClipboard should fail if clipboard wasn't open\n");
191     test_last_error(ERROR_CLIPBOARD_NOT_OPEN);
192 }
193
194 static HGLOBAL create_text(void)
195 {
196     HGLOBAL h = GlobalAlloc(GMEM_DDESHARE|GMEM_MOVEABLE, 5);
197     char *p = GlobalLock(h);
198     strcpy(p, "test");
199     GlobalUnlock(h);
200     return h;
201 }
202
203 static HENHMETAFILE create_emf(void)
204 {
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);
209 }
210
211 static void test_synthesized(void)
212 {
213     HGLOBAL h, htext;
214     HENHMETAFILE emf;
215     BOOL r;
216     UINT cf;
217
218     htext = create_text();
219     emf = create_emf();
220
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());
231
232     r = OpenClipboard(NULL);
233     ok(r, "gle %d\n", GetLastError());
234     cf = EnumClipboardFormats(0);
235     ok(cf == CF_TEXT, "cf %08x\n", cf);
236
237     cf = EnumClipboardFormats(cf);
238     ok(cf == CF_ENHMETAFILE, "cf %08x\n", cf);
239
240     cf = EnumClipboardFormats(cf);
241     todo_wine ok(cf == CF_LOCALE, "cf %08x\n", cf);
242     if(cf == CF_LOCALE)
243         cf = EnumClipboardFormats(cf);
244     ok(cf == CF_OEMTEXT, "cf %08x\n", cf);
245
246     cf = EnumClipboardFormats(cf);
247     ok(cf == CF_UNICODETEXT ||
248        broken(cf == CF_METAFILEPICT), /* win9x and winME has no CF_UNICODETEXT */
249        "cf %08x\n", cf);
250
251     if(cf == CF_UNICODETEXT)
252         cf = EnumClipboardFormats(cf);
253     ok(cf == CF_METAFILEPICT, "cf %08x\n", cf);
254
255     cf = EnumClipboardFormats(cf);
256     ok(cf == 0, "cf %08x\n", cf);
257
258     r = EmptyClipboard();
259     ok(r, "gle %d\n", GetLastError());
260
261     r = CloseClipboard();
262     ok(r, "gle %d\n", GetLastError());
263 }
264
265 START_TEST(clipboard)
266 {
267     SetLastError(0xdeadbeef);
268     FindAtomW(NULL);
269     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) is_win9x = TRUE;
270
271     test_RegisterClipboardFormatA();
272     test_ClipboardOwner();
273     test_synthesized();
274 }