comctl32/syslink: Check item type before increasing link ID in SYSLINK_LinkAtPt().
[wine.git] / dlls / comctl32 / tests / syslink.c
blobf6c31d92981749865dbd3557d2064aaeac36da56
1 /* Unit tests for the syslink control.
3 * Copyright 2011 Francois Gouget for CodeWeavers
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <windows.h>
21 #include <commctrl.h>
23 #include "wine/test.h"
24 #include "v6util.h"
25 #include "msg.h"
27 #define expect(expected, got) ok(got == expected, "Expected %d, got %d\n", expected, got)
28 #define NUM_MSG_SEQUENCE 2
29 #define PARENT_SEQ_INDEX 0
30 #define SYSLINK_SEQ_INDEX 1
32 static HWND hWndParent;
33 static int g_link_id;
35 static struct msg_sequence *sequences[NUM_MSG_SEQUENCE];
37 static const struct message empty_wnd_seq[] = {
38 {0}
41 static const struct message parent_create_syslink_wnd_seq[] = {
42 { WM_GETFONT, sent|optional}, /* Only on XP */
43 { WM_QUERYUISTATE, sent|optional},
44 { WM_CTLCOLORSTATIC, sent},
45 { WM_NOTIFY, sent|wparam, 0},
46 { WM_PARENTNOTIFY, sent|wparam, WM_CREATE},
47 {0}
50 static const struct message visible_syslink_wnd_seq[] = {
51 { WM_STYLECHANGING, sent|wparam, GWL_STYLE},
52 { WM_STYLECHANGED, sent|wparam, GWL_STYLE},
53 { WM_PAINT, sent},
54 {0}
57 static const struct message parent_visible_syslink_wnd_seq[] = {
58 { WM_CTLCOLORSTATIC, sent},
59 { WM_NOTIFY, sent|wparam, 0},
60 {0}
63 /* Try to make sure pending X events have been processed before continuing */
64 static void flush_events(void)
66 MSG msg;
67 int diff = 200;
68 int min_timeout = 100;
69 DWORD time = GetTickCount() + diff;
71 while (diff > 0)
73 if (MsgWaitForMultipleObjects( 0, NULL, FALSE, min_timeout, QS_ALLINPUT ) == WAIT_TIMEOUT) break;
74 while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageA( &msg );
75 diff = time - GetTickCount();
79 static LRESULT WINAPI parent_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
81 static LONG defwndproc_counter = 0;
82 LRESULT ret;
83 struct message msg;
85 /* log system messages, except for painting */
86 if (message < WM_USER &&
87 message != WM_PAINT &&
88 message != WM_ERASEBKGND &&
89 message != WM_NCPAINT &&
90 message != WM_NCHITTEST &&
91 message != WM_GETTEXT &&
92 message != WM_GETICON &&
93 message != WM_DEVICECHANGE)
95 msg.message = message;
96 msg.flags = sent|wparam|lparam;
97 if (defwndproc_counter) msg.flags |= defwinproc;
98 msg.wParam = wParam;
99 msg.lParam = lParam;
100 add_message(sequences, PARENT_SEQ_INDEX, &msg);
103 switch(message)
105 case WM_NOTIFY:
107 NMLINK *nml = ((NMLINK *)lParam);
108 if (nml && NM_CLICK == nml->hdr.code)
110 g_link_id = nml->item.iLink;
112 break;
114 default:
115 break;
117 defwndproc_counter++;
118 ret = DefWindowProcW(hwnd, message, wParam, lParam);
119 defwndproc_counter--;
121 return ret;
124 static BOOL register_parent_wnd_class(void)
126 WNDCLASSW cls;
128 cls.style = 0;
129 cls.lpfnWndProc = parent_wnd_proc;
130 cls.cbClsExtra = 0;
131 cls.cbWndExtra = 0;
132 cls.hInstance = GetModuleHandleW(NULL);
133 cls.hIcon = 0;
134 cls.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
135 cls.hbrBackground = GetStockObject(WHITE_BRUSH);
136 cls.lpszMenuName = NULL;
137 cls.lpszClassName = L"Syslink test parent class";
138 return RegisterClassW(&cls);
141 static HWND create_parent_window(void)
143 if (!register_parent_wnd_class())
144 return NULL;
146 return CreateWindowExW(0, L"Syslink test parent class", L"Syslink test parent window",
147 WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
148 WS_MAXIMIZEBOX | WS_VISIBLE,
149 0, 0, 200, 100, GetDesktopWindow(),
150 NULL, GetModuleHandleW(NULL), NULL);
153 static WNDPROC syslink_oldproc;
155 static LRESULT WINAPI syslink_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
157 static LONG defwndproc_counter = 0;
158 struct message msg = { 0 };
159 LRESULT ret;
161 msg.message = message;
162 msg.flags = sent|wparam|lparam;
163 if (defwndproc_counter) msg.flags |= defwinproc;
164 msg.wParam = wParam;
165 msg.lParam = lParam;
166 add_message(sequences, SYSLINK_SEQ_INDEX, &msg);
168 defwndproc_counter++;
169 ret = CallWindowProcW(syslink_oldproc, hwnd, message, wParam, lParam);
170 defwndproc_counter--;
172 return ret;
175 static HWND create_syslink(DWORD style, HWND parent)
177 HWND hWndSysLink;
179 /* Only Unicode will do here */
180 hWndSysLink = CreateWindowExW(0, WC_LINK, L"Head <a href=\"link1\">Name1</a> Middle <a href=\"link2\">Name2</a> Tail",
181 style, 0, 0, 150, 50,
182 parent, NULL, GetModuleHandleW(NULL), NULL);
183 if (!hWndSysLink) return NULL;
185 if (GetWindowLongPtrW(hWndSysLink, GWLP_USERDATA))
186 /* On Windows XP SysLink takes GWLP_USERDATA for itself! */
187 trace("SysLink makes use of GWLP_USERDATA\n");
189 syslink_oldproc = (WNDPROC)SetWindowLongPtrW(hWndSysLink, GWLP_WNDPROC, (LONG_PTR)syslink_subclass_proc);
191 return hWndSysLink;
194 static void test_create_syslink(void)
196 HWND hWndSysLink;
197 LONG oldstyle;
199 /* Create an invisible SysLink control */
200 flush_sequences(sequences, NUM_MSG_SEQUENCE);
201 hWndSysLink = create_syslink(WS_CHILD | WS_TABSTOP, hWndParent);
202 ok(hWndSysLink != NULL, "Expected non NULL value (le %lu)\n", GetLastError());
203 flush_events();
204 ok_sequence(sequences, SYSLINK_SEQ_INDEX, empty_wnd_seq, "create SysLink", FALSE);
205 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_create_syslink_wnd_seq, "create SysLink (parent)", TRUE);
207 /* Make the SysLink control visible */
208 flush_sequences(sequences, NUM_MSG_SEQUENCE);
209 oldstyle = GetWindowLongA(hWndSysLink, GWL_STYLE);
210 SetWindowLongA(hWndSysLink, GWL_STYLE, oldstyle | WS_VISIBLE);
211 RedrawWindow(hWndSysLink, NULL, NULL, RDW_INVALIDATE);
212 flush_events();
213 ok_sequence(sequences, SYSLINK_SEQ_INDEX, visible_syslink_wnd_seq, "visible SysLink", TRUE);
214 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_visible_syslink_wnd_seq, "visible SysLink (parent)", TRUE);
216 DestroyWindow(hWndSysLink);
219 static void test_LM_GETIDEALHEIGHT(void)
221 HWND hwnd;
222 LONG ret;
224 hwnd = create_syslink(WS_CHILD | WS_TABSTOP | WS_VISIBLE, hWndParent);
225 ok(hwnd != NULL, "Failed to create SysLink window.\n");
227 ret = SendMessageA(hwnd, LM_GETIDEALHEIGHT, 0, 0);
228 ok(ret > 0, "Unexpected ideal height, %ld.\n", ret);
230 DestroyWindow(hwnd);
233 static void test_LM_GETIDEALSIZE(void)
235 HWND hwnd;
236 LONG ret;
237 SIZE sz;
239 hwnd = create_syslink(WS_CHILD | WS_TABSTOP | WS_VISIBLE, hWndParent);
240 ok(hwnd != NULL, "Failed to create SysLink window.\n");
242 memset(&sz, 0, sizeof(sz));
243 ret = SendMessageA(hwnd, LM_GETIDEALSIZE, 0, (LPARAM)&sz);
244 ok(ret > 0, "Unexpected return value, %ld.\n", ret);
245 if (sz.cy == 0)
246 win_skip("LM_GETIDEALSIZE is not supported.\n");
247 else
249 ok(sz.cx > 5, "Unexpected ideal width, %ld.\n", sz.cx);
250 ok(sz.cy == ret, "Unexpected ideal height, %ld.\n", sz.cy);
253 DestroyWindow(hwnd);
256 static void test_link_id(void)
258 HWND hwnd;
260 hwnd = create_syslink(WS_CHILD | WS_TABSTOP | WS_VISIBLE, hWndParent);
261 ok(hwnd != NULL, "Failed to create SysLink window.\n");
263 /* test link1 at (50, 10) */
264 g_link_id = 0;
265 SendMessageA(hwnd, WM_LBUTTONDOWN, 1, MAKELPARAM(50, 10));
266 SendMessageA(hwnd, WM_LBUTTONUP, 0, MAKELPARAM(50, 10));
267 ok(g_link_id == 0, "Got unexpected link id %d.\n", g_link_id);
269 /* test link2 at (25, 25) */
270 g_link_id = 0;
271 SendMessageA(hwnd, WM_LBUTTONDOWN, 1, MAKELPARAM(25, 25));
272 SendMessageA(hwnd, WM_LBUTTONUP, 0, MAKELPARAM(25, 25));
273 ok(g_link_id == 1, "Got unexpected link id %d.\n", g_link_id);
275 DestroyWindow(hwnd);
278 START_TEST(syslink)
280 ULONG_PTR ctx_cookie;
281 HMODULE hComctl32;
282 POINT orig_pos;
283 HANDLE hCtx;
285 if (!load_v6_module(&ctx_cookie, &hCtx))
286 return;
288 /* LoadLibrary is needed. This file has no reference to functions in comctl32 */
289 hComctl32 = LoadLibraryA("comctl32.dll");
290 ok(hComctl32 != NULL, "Failed to load comctl32.dll.\n");
292 /* Move the cursor off the parent window */
293 GetCursorPos(&orig_pos);
294 SetCursorPos(400, 400);
296 init_msg_sequences(sequences, NUM_MSG_SEQUENCE);
298 /* Create parent window */
299 hWndParent = create_parent_window();
300 ok(hWndParent != NULL, "Failed to create parent Window!\n");
301 flush_events();
303 test_create_syslink();
304 test_LM_GETIDEALHEIGHT();
305 test_LM_GETIDEALSIZE();
306 test_link_id();
308 DestroyWindow(hWndParent);
309 unload_v6_module(ctx_cookie, hCtx);
310 SetCursorPos(orig_pos.x, orig_pos.y);