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