user32/tests: Skip tests instead of crashing if global hook cannot be set.
[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 "winuser.h"
25
26 static BOOL is_win9x = FALSE;
27
28 #define test_last_error(expected_error) \
29     do \
30     { \
31         if (!is_win9x) \
32             ok(GetLastError() == expected_error, \
33                "Last error should be set to %d, not %d\n", \
34                 expected_error, GetLastError()); \
35     } while (0)
36
37 static void test_ClipboardOwner(void)
38 {
39     HWND hWnd1, hWnd2;
40     BOOL ret;
41
42     SetLastError(0xdeadbeef);
43     ok(!GetClipboardOwner() && GetLastError() == 0xdeadbeef,
44        "could not perform clipboard test: clipboard already owned\n");
45
46     hWnd1 = CreateWindowExA(0, "static", NULL, WS_POPUP,
47                                  0, 0, 10, 10, 0, 0, 0, NULL);
48     ok(hWnd1 != 0, "CreateWindowExA error %d\n", GetLastError());
49     trace("hWnd1 = %p\n", hWnd1);
50
51     hWnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP,
52                                  0, 0, 10, 10, 0, 0, 0, NULL);
53     ok(hWnd2 != 0, "CreateWindowExA error %d\n", GetLastError());
54     trace("hWnd2 = %p\n", hWnd2);
55
56     SetLastError(0xdeadbeef);
57     ok(!CloseClipboard(), "CloseClipboard should fail if clipboard wasn't open\n");
58     test_last_error(ERROR_CLIPBOARD_NOT_OPEN);
59
60     ok(OpenClipboard(0), "OpenClipboard failed\n");
61     ok(!GetClipboardOwner(), "clipboard should still be not owned\n");
62     ok(!OpenClipboard(hWnd1), "OpenClipboard should fail since clipboard already opened\n");
63     ret = CloseClipboard();
64     ok( ret, "CloseClipboard error %d\n", GetLastError());
65
66     ok(OpenClipboard(hWnd1), "OpenClipboard failed\n");
67
68     SetLastError(0xdeadbeef);
69     ok(!OpenClipboard(hWnd2) &&
70        (GetLastError() == 0xdeadbeef || GetLastError() == ERROR_ACCESS_DENIED),
71        "OpenClipboard should fail without setting last error value, or with ERROR_ACCESS_DENIED, got error %d\n", GetLastError());
72
73     SetLastError(0xdeadbeef);
74     ok(!GetClipboardOwner() && GetLastError() == 0xdeadbeef, "clipboard should still be not owned\n");
75     ret = EmptyClipboard();
76     ok( ret, "EmptyClipboard error %d\n", GetLastError());
77     ok(GetClipboardOwner() == hWnd1, "clipboard should be owned by %p, not by %p\n", hWnd1, GetClipboardOwner());
78
79     SetLastError(0xdeadbeef);
80     ok(!OpenClipboard(hWnd2) &&
81        (GetLastError() == 0xdeadbeef || GetLastError() == ERROR_ACCESS_DENIED),
82        "OpenClipboard should fail without setting last error valuei, or with ERROR_ACCESS_DENIED, got error %d\n", GetLastError());
83
84     ret = CloseClipboard();
85     ok( ret, "CloseClipboard error %d\n", GetLastError());
86     ok(GetClipboardOwner() == hWnd1, "clipboard should still be owned\n");
87
88     ret = DestroyWindow(hWnd1);
89     ok( ret, "DestroyWindow error %d\n", GetLastError());
90     ret = DestroyWindow(hWnd2);
91     ok( ret, "DestroyWindow error %d\n", GetLastError());
92     SetLastError(0xdeadbeef);
93     ok(!GetClipboardOwner() && GetLastError() == 0xdeadbeef, "clipboard should not be owned\n");
94 }
95
96 static void test_RegisterClipboardFormatA(void)
97 {
98     ATOM atom_id;
99     UINT format_id, format_id2;
100     char buf[256];
101     int len;
102     BOOL ret;
103
104     format_id = RegisterClipboardFormatA("my_cool_clipboard_format");
105     ok(format_id > 0xc000 && format_id < 0xffff, "invalid clipboard format id %04x\n", format_id);
106
107     format_id2 = RegisterClipboardFormatA("MY_COOL_CLIPBOARD_FORMAT");
108     ok(format_id2 == format_id, "invalid clipboard format id %04x\n", format_id2);
109
110     len = GetClipboardFormatNameA(format_id, buf, 256);
111     ok(len == lstrlenA("my_cool_clipboard_format"), "wrong format name length %d\n", len);
112     ok(!lstrcmpA(buf, "my_cool_clipboard_format"), "wrong format name \"%s\"\n", buf);
113
114     lstrcpyA(buf, "foo");
115     SetLastError(0xdeadbeef);
116     len = GetAtomNameA((ATOM)format_id, buf, 256);
117     ok(len == 0, "GetAtomNameA should fail\n");
118     test_last_error(ERROR_INVALID_HANDLE);
119
120 todo_wine
121 {
122     lstrcpyA(buf, "foo");
123     SetLastError(0xdeadbeef);
124     len = GlobalGetAtomNameA((ATOM)format_id, buf, 256);
125     ok(len == 0, "GlobalGetAtomNameA should fail\n");
126     test_last_error(ERROR_INVALID_HANDLE);
127 }
128
129     SetLastError(0xdeadbeef);
130     atom_id = FindAtomA("my_cool_clipboard_format");
131     ok(atom_id == 0, "FindAtomA should fail\n");
132     test_last_error(ERROR_FILE_NOT_FOUND);
133
134     if (0)
135     {
136     /* this relies on the clipboard and global atom table being different */
137     SetLastError(0xdeadbeef);
138     atom_id = GlobalFindAtomA("my_cool_clipboard_format");
139     ok(atom_id == 0, "GlobalFindAtomA should fail\n");
140     test_last_error(ERROR_FILE_NOT_FOUND);
141
142     for (format_id = 0; format_id < 0xffff; format_id++)
143     {
144         SetLastError(0xdeadbeef);
145         len = GetClipboardFormatNameA(format_id, buf, 256);
146
147         if (format_id < 0xc000)
148         {
149             ok(!len, "GetClipboardFormatNameA should fail, but it returned %d (%s)\n", len, buf);
150             test_last_error(ERROR_INVALID_PARAMETER);
151         }
152         else
153         {
154             if (len)
155                 trace("%04x: %s\n", format_id, len ? buf : "");
156             else
157                 test_last_error(ERROR_INVALID_HANDLE);
158         }
159     }
160     }
161
162     ret = OpenClipboard(0);
163     ok( ret, "OpenClipboard error %d\n", GetLastError());
164
165     trace("# of formats available: %d\n", CountClipboardFormats());
166
167     format_id = 0;
168     while ((format_id = EnumClipboardFormats(format_id)))
169     {
170         ok(IsClipboardFormatAvailable(format_id), "format %04x was listed as available\n", format_id);
171         len = GetClipboardFormatNameA(format_id, buf, 256);
172         trace("%04x: %s\n", format_id, len ? buf : "");
173     }
174
175     ret = EmptyClipboard();
176     ok( ret, "EmptyClipboard error %d\n", GetLastError());
177     ret =CloseClipboard();
178     ok( ret, "CloseClipboard error %d\n", GetLastError());
179
180     if (CountClipboardFormats())
181     {
182         SetLastError(0xdeadbeef);
183         ok(!EnumClipboardFormats(0), "EnumClipboardFormats should fail if clipboard wasn't open\n");
184         ok(GetLastError() == ERROR_CLIPBOARD_NOT_OPEN,
185            "Last error should be set to ERROR_CLIPBOARD_NOT_OPEN, not %d\n", GetLastError());
186     }
187
188     SetLastError(0xdeadbeef);
189     ok(!EmptyClipboard(), "EmptyClipboard should fail if clipboard wasn't open\n");
190     test_last_error(ERROR_CLIPBOARD_NOT_OPEN);
191 }
192
193 START_TEST(clipboard)
194 {
195     SetLastError(0xdeadbeef);
196     FindAtomW(NULL);
197     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) is_win9x = TRUE;
198
199     test_RegisterClipboardFormatA();
200     test_ClipboardOwner();
201 }