comctl32: Fix expanding of invisible sub trees.
[wine/hacks.git] / dlls / comctl32 / tests / treeview.c
blob05a52373ac3c021cbfc8e211715ffb0df7bfcd1a
1 /* Unit tests for treeview.
3 * Copyright 2005 Krzysztof Foltman
4 * Copyright 2007 Christopher James Peterson
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.
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.
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
21 #include <assert.h>
22 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "winnls.h"
29 #include "winreg.h"
30 #include "commctrl.h"
32 #include "wine/test.h"
33 #include "msg.h"
35 const char *TEST_CALLBACK_TEXT = "callback_text";
37 #define NUM_MSG_SEQUENCES 1
38 #define LISTVIEW_SEQ_INDEX 0
40 static struct msg_sequence *MsgSequences[NUM_MSG_SEQUENCES];
42 static const struct message FillRootSeq[] = {
43 { TVM_INSERTITEM, sent },
44 { TVM_GETITEM, sent },
45 { TVM_INSERTITEM, sent },
46 { 0 }
49 static const struct message DoTest1Seq[] = {
50 { TVM_SELECTITEM, sent|wparam, 0x00000009 },
51 { TVM_SELECTITEM, sent|wparam, 0x00000009 },
52 { TVM_SELECTITEM, sent|wparam, 0x00000009 },
53 { TVM_SELECTITEM, sent|wparam, 0x00000009 },
54 { TVM_SELECTITEM, sent|wparam, 0x00000009 },
55 { TVM_SELECTITEM, sent|wparam, 0x00000009 },
56 { 0 }
59 static const struct message DoTest2Seq[] = {
60 { TVM_SELECTITEM, sent|wparam, 0x00000009 },
61 { TVM_SELECTITEM, sent|wparam, 0x00000009 },
62 { TVM_SELECTITEM, sent|wparam, 0x00000009 },
63 { TVM_SELECTITEM, sent|wparam, 0x00000009 },
64 { TVM_SELECTITEM, sent|wparam, 0x00000009 },
65 { TVM_SELECTITEM, sent|wparam, 0x00000009 },
66 { 0 }
69 static const struct message DoTest3Seq[] = {
70 { TVM_INSERTITEM, sent },
71 { TVM_GETITEM, sent },
72 { TVM_DELETEITEM, sent },
73 { 0 }
76 static const struct message DoFocusTestSeq[] = {
77 { TVM_INSERTITEM, sent },
78 { TVM_INSERTITEM, sent },
79 { WM_WINDOWPOSCHANGING, sent|defwinproc },
80 { WM_NCCALCSIZE, sent|wparam|defwinproc, 0x00000001 },
81 { WM_WINDOWPOSCHANGED, sent|defwinproc },
82 { WM_SIZE, sent|defwinproc },
83 { WM_WINDOWPOSCHANGING, sent },
84 { WM_NCCALCSIZE, sent|wparam, 0x00000001 },
85 { WM_WINDOWPOSCHANGED, sent },
86 { WM_SIZE, sent|defwinproc },
87 { WM_WINDOWPOSCHANGING, sent|defwinproc|optional },
88 { WM_NCCALCSIZE, sent|wparam|defwinproc|optional, 0x00000001 },
89 { WM_WINDOWPOSCHANGED, sent|defwinproc|optional },
90 { WM_SIZE, sent|defwinproc|optional },
91 { TVM_SELECTITEM, sent|wparam, 0x00000009 },
92 /* The following end up out of order in wine */
93 { WM_PAINT, sent|defwinproc },
94 { WM_NCPAINT, sent|wparam|defwinproc, 0x00000001 },
95 { WM_ERASEBKGND, sent|defwinproc },
96 { TVM_EDITLABEL, sent },
97 { WM_COMMAND, sent|wparam|defwinproc, 0x04000000 },
98 { WM_COMMAND, sent|wparam|defwinproc, 0x03000000 },
99 { WM_PARENTNOTIFY, sent|wparam|defwinproc, 0x00000001 },
100 { WM_KILLFOCUS, sent|defwinproc },
101 { WM_PAINT, sent|defwinproc },
102 { WM_IME_SETCONTEXT, sent|defwinproc|optional },
103 { WM_COMMAND, sent|wparam|defwinproc, 0x01000000},
104 { WM_ERASEBKGND, sent|defwinproc },
105 { WM_CTLCOLOREDIT, sent|defwinproc|optional },
106 { WM_CTLCOLOREDIT, sent|defwinproc|optional },
107 { 0 }
110 static const struct message TestGetSetBkColorSeq[] = {
111 { TVM_GETBKCOLOR, sent|wparam|lparam, 0x00000000, 0x00000000 },
112 { TVM_SETBKCOLOR, sent|wparam|lparam, 0x00000000, 0x00000000 },
113 { TVM_GETBKCOLOR, sent|wparam|lparam, 0x00000000, 0x00000000 },
114 { TVM_SETBKCOLOR, sent|wparam|lparam, 0x00000000, 0x00ffffff },
115 { TVM_GETBKCOLOR, sent|wparam|lparam, 0x00000000, 0x00000000 },
116 { TVM_SETBKCOLOR, sent|wparam|lparam, 0x00000000, -1 },
117 { 0 }
120 static const struct message TestGetSetImageListSeq[] = {
121 { TVM_SETIMAGELIST, sent|wparam|lparam, 0x00000000, 0x00000000 },
122 { TVM_GETIMAGELIST, sent|wparam|lparam, 0x00000000, 0x00000000 },
123 { 0 }
126 static const struct message TestGetSetIndentSeq[] = {
127 { TVM_SETINDENT, sent|wparam|lparam, 0x00000000, 0x00000000 },
128 { TVM_GETINDENT, sent|wparam|lparam, 0x00000000, 0x00000000 },
129 /* The actual amount to indent is dependent on the system for this message */
130 { TVM_SETINDENT, sent },
131 { TVM_GETINDENT, sent|wparam|lparam, 0x00000000, 0x00000000 },
132 { 0 }
135 static const struct message TestGetSetInsertMarkColorSeq[] = {
136 { TVM_SETINSERTMARKCOLOR, sent|wparam|lparam, 0x00000000, 0x00000000 },
137 { TVM_GETINSERTMARKCOLOR, sent|wparam|lparam, 0x00000000, 0x00000000 },
138 { 0 }
141 static const struct message TestGetSetItemSeq[] = {
142 { TVM_GETITEM, sent },
143 { TVM_SETITEM, sent },
144 { TVM_GETITEM, sent },
145 { TVM_SETITEM, sent },
146 { 0 }
149 static const struct message TestGetSetItemHeightSeq[] = {
150 { TVM_GETITEMHEIGHT, sent|wparam|lparam, 0x00000000, 0x00000000 },
151 { TVM_SETITEMHEIGHT, sent|wparam|lparam, -1, 0x00000000 },
152 { TVM_GETITEMHEIGHT, sent|wparam|lparam, 0x00000000, 0x00000000 },
153 { TVM_SETITEMHEIGHT, sent|lparam, 0xcccccccc, 0x00000000 },
154 { TVM_GETITEMHEIGHT, sent|wparam|lparam|optional, 0x00000000, 0x00000000 },
155 { TVM_SETITEMHEIGHT, sent|wparam|lparam|optional, 0x00000009, 0x00000000 },
156 { WM_WINDOWPOSCHANGING, sent|defwinproc },
157 { WM_NCCALCSIZE, sent|wparam|defwinproc, 0x00000001 },
158 { WM_WINDOWPOSCHANGED, sent|defwinproc },
159 { WM_SIZE, sent|defwinproc },
160 { TVM_GETITEMHEIGHT, sent|wparam|lparam, 0x00000000, 0x00000000 },
161 { 0 }
164 static const struct message TestGetSetScrollTimeSeq[] = {
165 { TVM_SETSCROLLTIME, sent|wparam|lparam, 0x00000014, 0x00000000 },
166 { TVM_GETSCROLLTIME, sent|wparam|lparam, 0x00000000, 0x00000000 },
167 { 0 }
170 static const struct message TestGetSetTextColorSeq[] = {
171 { TVM_GETTEXTCOLOR, sent|wparam|lparam, 0x00000000, 0x00000000 },
172 { TVM_SETTEXTCOLOR, sent|wparam|lparam, 0x00000000, 0x00000000 },
173 { TVM_GETTEXTCOLOR, sent|wparam|lparam, 0x00000000, 0x00000000 },
174 { TVM_SETTEXTCOLOR, sent|wparam|lparam, 0x00000000, 0x00ffffff },
175 { TVM_GETTEXTCOLOR, sent|wparam|lparam, 0x00000000, 0x00000000 },
176 { TVM_SETTEXTCOLOR, sent|wparam|lparam, 0x00000000, -1 },
177 { 0 }
180 static const struct message TestGetSetToolTipsSeq[] = {
181 { WM_COMMAND, sent|wparam, 0x02000000 },
182 { WM_PARENTNOTIFY, sent|wparam|defwinproc, 0x00020002 },
183 { TVM_SETTOOLTIPS, sent|wparam|lparam, 0x00000000, 0x00000000 },
184 { TVM_GETTOOLTIPS, sent|wparam|lparam, 0x00000000, 0x00000000 },
185 { 0 }
188 static const struct message TestGetSetUnicodeFormatSeq[] = {
189 { TVM_SETUNICODEFORMAT, sent|wparam|lparam, 0x00000001, 0x00000000 },
190 { TVM_GETUNICODEFORMAT, sent|wparam|lparam, 0x00000000, 0x00000000 },
191 { TVM_SETUNICODEFORMAT, sent|wparam|lparam, 0x00000000, 0x00000000 },
192 { TVM_GETUNICODEFORMAT, sent|wparam|lparam, 0x00000000, 0x00000000 },
193 { TVM_SETUNICODEFORMAT, sent|wparam|lparam, 0x00000000, 0x00000000 },
194 { 0 }
197 static HWND hMainWnd;
199 static HWND hTree, hEdit;
200 static HTREEITEM hRoot, hChild;
202 static int pos = 0;
203 static char sequence[256];
205 static void Clear(void)
207 pos = 0;
208 sequence[0] = '\0';
211 static void AddItem(char ch)
213 sequence[pos++] = ch;
214 sequence[pos] = '\0';
217 static void IdentifyItem(HTREEITEM hItem)
219 if (hItem == hRoot) {
220 AddItem('R');
221 return;
223 if (hItem == hChild) {
224 AddItem('C');
225 return;
227 if (hItem == NULL) {
228 AddItem('n');
229 return;
231 AddItem('?');
234 static void FillRoot(void)
236 TVINSERTSTRUCTA ins;
237 TVITEM tvi;
238 static CHAR root[] = "Root",
239 child[] = "Child";
241 Clear();
242 AddItem('A');
243 ins.hParent = TVI_ROOT;
244 ins.hInsertAfter = TVI_ROOT;
245 U(ins).item.mask = TVIF_TEXT;
246 U(ins).item.pszText = root;
247 hRoot = TreeView_InsertItem(hTree, &ins);
248 assert(hRoot);
250 /* UMLPad 1.15 depends on this being not -1 (I_IMAGECALLBACK) */
251 tvi.hItem = hRoot;
252 tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
253 SendMessage( hTree, TVM_GETITEM, 0, (LPARAM)&tvi );
254 ok(tvi.iImage == 0, "tvi.iImage=%d\n", tvi.iImage);
255 ok(tvi.iSelectedImage == 0, "tvi.iSelectedImage=%d\n", tvi.iSelectedImage);
257 AddItem('B');
258 ins.hParent = hRoot;
259 ins.hInsertAfter = TVI_FIRST;
260 U(ins).item.mask = TVIF_TEXT;
261 U(ins).item.pszText = child;
262 hChild = TreeView_InsertItem(hTree, &ins);
263 assert(hChild);
264 AddItem('.');
266 ok(!strcmp(sequence, "AB."), "Item creation\n");
269 static void TestCallback(void)
271 HTREEITEM hRoot;
272 HTREEITEM hItem1, hItem2;
273 TVINSERTSTRUCTA ins;
274 TVITEM tvi;
275 CHAR test_string[] = "Test_string";
276 CHAR buf[128];
277 LRESULT ret;
279 TreeView_DeleteAllItems(hTree);
280 ins.hParent = TVI_ROOT;
281 ins.hInsertAfter = TVI_ROOT;
282 U(ins).item.mask = TVIF_TEXT;
283 U(ins).item.pszText = LPSTR_TEXTCALLBACK;
284 hRoot = TreeView_InsertItem(hTree, &ins);
285 assert(hRoot);
287 tvi.hItem = hRoot;
288 tvi.mask = TVIF_TEXT;
289 tvi.pszText = buf;
290 tvi.cchTextMax = sizeof(buf)/sizeof(buf[0]);
291 ret = TreeView_GetItem(hTree, &tvi);
292 ok(ret == 1, "ret\n");
293 ok(strcmp(tvi.pszText, TEST_CALLBACK_TEXT) == 0, "Callback item text mismatch %s vs %s\n",
294 tvi.pszText, TEST_CALLBACK_TEXT);
296 ins.hParent = hRoot;
297 ins.hInsertAfter = TVI_FIRST;
298 U(ins).item.mask = TVIF_TEXT;
299 U(ins).item.pszText = test_string;
300 hItem1 = TreeView_InsertItem(hTree, &ins);
301 assert(hItem1);
303 tvi.hItem = hItem1;
304 TreeView_GetItem(hTree, &tvi);
305 ok(strcmp(tvi.pszText, test_string) == 0, "Item text mismatch %s vs %s\n",
306 tvi.pszText, test_string);
308 /* undocumented: pszText of NULL also means LPSTR_CALLBACK: */
309 tvi.pszText = NULL;
310 ret = TreeView_SetItem(hTree, &tvi);
311 ok(ret == 1, "Expected SetItem return 1, got %ld\n", ret);
312 tvi.pszText = buf;
313 TreeView_GetItem(hTree, &tvi);
314 ok(strcmp(tvi.pszText, TEST_CALLBACK_TEXT) == 0, "Item text mismatch %s vs %s\n",
315 tvi.pszText, TEST_CALLBACK_TEXT);
317 U(ins).item.pszText = NULL;
318 hItem2 = TreeView_InsertItem(hTree, &ins);
319 assert(hItem2);
320 tvi.hItem = hItem2;
321 memset(buf, 0, sizeof(buf));
322 TreeView_GetItem(hTree, &tvi);
323 ok(strcmp(tvi.pszText, TEST_CALLBACK_TEXT) == 0, "Item text mismatch %s vs %s\n",
324 tvi.pszText, TEST_CALLBACK_TEXT);
327 static void DoTest1(void)
329 BOOL r;
330 r = TreeView_SelectItem(hTree, NULL);
331 Clear();
332 AddItem('1');
333 r = TreeView_SelectItem(hTree, hRoot);
334 AddItem('2');
335 r = TreeView_SelectItem(hTree, hRoot);
336 AddItem('3');
337 r = TreeView_SelectItem(hTree, NULL);
338 AddItem('4');
339 r = TreeView_SelectItem(hTree, NULL);
340 AddItem('5');
341 r = TreeView_SelectItem(hTree, hRoot);
342 AddItem('.');
343 ok(!strcmp(sequence, "1(nR)nR23(Rn)Rn45(nR)nR."), "root-none select test\n");
346 static void DoTest2(void)
348 BOOL r;
349 r = TreeView_SelectItem(hTree, NULL);
350 Clear();
351 AddItem('1');
352 r = TreeView_SelectItem(hTree, hRoot);
353 AddItem('2');
354 r = TreeView_SelectItem(hTree, hRoot);
355 AddItem('3');
356 r = TreeView_SelectItem(hTree, hChild);
357 AddItem('4');
358 r = TreeView_SelectItem(hTree, hChild);
359 AddItem('5');
360 r = TreeView_SelectItem(hTree, hRoot);
361 AddItem('.');
362 ok(!strcmp(sequence, "1(nR)nR23(RC)RC45(CR)CR."), "root-child select test\n");
365 static void DoTest3(void)
367 TVINSERTSTRUCTA ins;
368 HTREEITEM hChild;
369 TVITEM tvi;
371 int nBufferSize = 80;
372 CHAR szBuffer[80] = "Blah";
374 /* add an item without TVIF_TEXT mask and pszText == NULL */
375 ins.hParent = hRoot;
376 ins.hInsertAfter = TVI_ROOT;
377 U(ins).item.mask = 0;
378 U(ins).item.pszText = NULL;
379 U(ins).item.cchTextMax = 0;
380 hChild = TreeView_InsertItem(hTree, &ins);
381 assert(hChild);
383 /* retrieve it with TVIF_TEXT mask */
384 tvi.hItem = hChild;
385 tvi.mask = TVIF_TEXT;
386 tvi.cchTextMax = nBufferSize;
387 tvi.pszText = szBuffer;
389 SendMessageA( hTree, TVM_GETITEM, 0, (LPARAM)&tvi );
390 ok(!strcmp(szBuffer, ""), "szBuffer=\"%s\", expected \"\"\n", szBuffer);
391 ok(SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)hChild), "DeleteItem failed\n");
394 static void DoFocusTest(void)
396 TVINSERTSTRUCTA ins;
397 static CHAR child1[] = "Edit",
398 child2[] = "A really long string";
399 HTREEITEM hChild1, hChild2;
401 /* This test verifies that when a label is being edited, scrolling
402 * the treeview does not cause the label to lose focus. To test
403 * this, first some additional entries are added to generate
404 * scrollbars.
406 ins.hParent = hRoot;
407 ins.hInsertAfter = hChild;
408 U(ins).item.mask = TVIF_TEXT;
409 U(ins).item.pszText = child1;
410 hChild1 = TreeView_InsertItem(hTree, &ins);
411 assert(hChild1);
412 ins.hInsertAfter = hChild1;
413 U(ins).item.mask = TVIF_TEXT;
414 U(ins).item.pszText = child2;
415 hChild2 = TreeView_InsertItem(hTree, &ins);
416 assert(hChild2);
418 ShowWindow(hMainWnd,SW_SHOW);
419 /* Using SendMessageA since Win98 doesn't have default unicode support */
420 SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hChild);
421 hEdit = TreeView_EditLabel(hTree, hChild);
422 ScrollWindowEx(hTree, -10, 0, NULL, NULL, NULL, NULL, SW_SCROLLCHILDREN);
423 ok(GetFocus() == hEdit, "Edit control should have focus\n");
426 static void TestGetSetBkColor(void)
428 COLORREF crColor = RGB(0,0,0);
430 /* If the value is -1, the control is using the system color for the background color. */
431 crColor = (COLORREF)SendMessage( hTree, TVM_GETBKCOLOR, 0, 0 );
432 ok(crColor == -1, "Default background color reported as 0x%.8x\n", crColor);
434 /* Test for black background */
435 SendMessage( hTree, TVM_SETBKCOLOR, 0, (LPARAM)RGB(0,0,0) );
436 crColor = (COLORREF)SendMessage( hTree, TVM_GETBKCOLOR, 0, 0 );
437 ok(crColor == RGB(0,0,0), "Black background color reported as 0x%.8x\n", crColor);
439 /* Test for white background */
440 SendMessage( hTree, TVM_SETBKCOLOR, 0, (LPARAM)RGB(255,255,255) );
441 crColor = (COLORREF)SendMessage( hTree, TVM_GETBKCOLOR, 0, 0 );
442 ok(crColor == RGB(255,255,255), "White background color reported as 0x%.8x\n", crColor);
444 /* Reset the default background */
445 SendMessage( hTree, TVM_SETBKCOLOR, 0, -1 );
448 static void TestGetSetImageList(void)
450 HIMAGELIST hImageList = NULL;
452 /* Test a NULL HIMAGELIST */
453 SendMessage( hTree, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)hImageList );
454 hImageList = (HIMAGELIST)SendMessage( hTree, TVM_GETIMAGELIST, TVSIL_NORMAL, 0 );
455 ok(hImageList == NULL, "NULL image list, reported as 0x%p, expected 0.\n", hImageList);
457 /* TODO: Test an actual image list */
460 static void TestGetSetIndent(void)
462 int ulIndent = -1;
463 int ulMinIndent = -1;
464 int ulMoreThanTwiceMin = -1;
466 /* Finding the minimum indent */
467 SendMessage( hTree, TVM_SETINDENT, 0, 0 );
468 ulMinIndent = (int)SendMessage( hTree, TVM_GETINDENT, 0, 0 );
470 /* Checking an indent that is more than twice the default indent */
471 ulMoreThanTwiceMin = 2*ulMinIndent+1;
472 SendMessage( hTree, TVM_SETINDENT, ulMoreThanTwiceMin, 0 );
473 ulIndent = (DWORD)SendMessage( hTree, TVM_GETINDENT, 0, 0 );
474 ok(ulIndent == ulMoreThanTwiceMin, "Indent reported as %d, expected %d\n", ulIndent, ulMoreThanTwiceMin);
477 static void TestGetSetInsertMarkColor(void)
479 COLORREF crColor = RGB(0,0,0);
480 SendMessage( hTree, TVM_SETINSERTMARKCOLOR, 0, crColor );
481 crColor = (COLORREF)SendMessage( hTree, TVM_GETINSERTMARKCOLOR, 0, 0 );
482 ok(crColor == RGB(0,0,0), "Insert mark color reported as 0x%.8x, expected 0x00000000\n", crColor);
485 static void TestGetSetItem(void)
487 TVITEM tviRoot = {0};
488 int nBufferSize = 80;
489 char szBuffer[80] = {0};
491 /* Test the root item */
492 tviRoot.hItem = hRoot;
493 tviRoot.mask = TVIF_TEXT;
494 tviRoot.cchTextMax = nBufferSize;
495 tviRoot.pszText = szBuffer;
496 SendMessage( hTree, TVM_GETITEM, 0, (LPARAM)&tviRoot );
497 ok(!strcmp("Root", szBuffer), "GetItem: szBuffer=\"%s\", expected \"Root\"\n", szBuffer);
499 /* Change the root text */
500 strncpy(szBuffer, "Testing123", nBufferSize);
501 SendMessage( hTree, TVM_SETITEM, 0, (LPARAM)&tviRoot );
502 memset(szBuffer, 0, nBufferSize);
503 SendMessage( hTree, TVM_GETITEM, 0, (LPARAM)&tviRoot );
504 ok(!strcmp("Testing123", szBuffer), "GetItem: szBuffer=\"%s\", expected \"Testing123\"\n", szBuffer);
506 /* Reset the root text */
507 memset(szBuffer, 0, nBufferSize);
508 strncpy(szBuffer, "Root", nBufferSize);
509 SendMessage( hTree, TVM_SETITEM, 0, (LPARAM)&tviRoot );
512 static void TestGetSetItemHeight(void)
514 int ulOldHeight = 0;
515 int ulNewHeight = 0;
517 /* Assuming default height to begin with */
518 ulOldHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
520 /* Explicitly setting and getting the default height */
521 SendMessage( hTree, TVM_SETITEMHEIGHT, -1, 0 );
522 ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
523 ok(ulNewHeight == ulOldHeight, "Default height not set properly, reported %d, expected %d\n", ulNewHeight, ulOldHeight);
525 /* Explicitly setting and getting the height of twice the normal */
526 SendMessage( hTree, TVM_SETITEMHEIGHT, 2*ulOldHeight, 0 );
527 ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
528 ok(ulNewHeight == 2*ulOldHeight, "New height not set properly, reported %d, expected %d\n", ulNewHeight, 2*ulOldHeight);
530 /* Assuming tree doesn't have TVS_NONEVENHEIGHT set, so a set of 9 will round down to 8 */
531 SendMessage( hTree, TVM_SETITEMHEIGHT, 9, 0 );
532 ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
533 ok(ulNewHeight == 8, "Uneven height not set properly, reported %d, expected %d\n", ulNewHeight, 8);
536 static void TestGetSetScrollTime(void)
538 int ulExpectedTime = 20;
539 int ulTime = 0;
540 SendMessage( hTree, TVM_SETSCROLLTIME, ulExpectedTime, 0 );
541 ulTime = (int)SendMessage( hTree, TVM_GETSCROLLTIME, 0, 0 );
542 ok(ulTime == ulExpectedTime, "Scroll time reported as %d, expected %d\n", ulTime, ulExpectedTime);
545 static void TestGetSetTextColor(void)
547 /* If the value is -1, the control is using the system color for the text color. */
548 COLORREF crColor = RGB(0,0,0);
549 crColor = (COLORREF)SendMessage( hTree, TVM_GETTEXTCOLOR, 0, 0 );
550 ok(crColor == -1, "Default text color reported as 0x%.8x\n", crColor);
552 /* Test for black text */
553 SendMessage( hTree, TVM_SETTEXTCOLOR, 0, (LPARAM)RGB(0,0,0) );
554 crColor = (COLORREF)SendMessage( hTree, TVM_GETTEXTCOLOR, 0, 0 );
555 ok(crColor == RGB(0,0,0), "Black text color reported as 0x%.8x\n", crColor);
557 /* Test for white text */
558 SendMessage( hTree, TVM_SETTEXTCOLOR, 0, (LPARAM)RGB(255,255,255) );
559 crColor = (COLORREF)SendMessage( hTree, TVM_GETTEXTCOLOR, 0, 0 );
560 ok(crColor == RGB(255,255,255), "White text color reported as 0x%.8x\n", crColor);
562 /* Reset the default text color */
563 SendMessage( hTree, TVM_SETTEXTCOLOR, 0, -1 );
566 static void TestGetSetToolTips(void)
568 HWND hwndLastToolTip = NULL;
569 HWND hPopupTreeView;
571 /* show even WS_POPUP treeview don't send NM_TOOLTIPSCREATED */
572 hPopupTreeView = CreateWindow(WC_TREEVIEW, NULL, WS_POPUP|WS_VISIBLE, 0, 0, 100, 100, hMainWnd, NULL, NULL, NULL);
573 DestroyWindow(hPopupTreeView);
575 /* Testing setting a NULL ToolTip */
576 SendMessage( hTree, TVM_SETTOOLTIPS, 0, 0 );
577 hwndLastToolTip = (HWND)SendMessage( hTree, TVM_GETTOOLTIPS, 0, 0 );
578 ok(hwndLastToolTip == NULL, "NULL tool tip, reported as 0x%p, expected 0.\n", hwndLastToolTip);
580 /* TODO: Add a test of an actual tooltip */
583 static void TestGetSetUnicodeFormat(void)
585 BOOL bPreviousSetting = 0;
586 BOOL bNewSetting = 0;
588 /* Set to Unicode */
589 bPreviousSetting = (BOOL)SendMessage( hTree, TVM_SETUNICODEFORMAT, 1, 0 );
590 bNewSetting = (BOOL)SendMessage( hTree, TVM_GETUNICODEFORMAT, 0, 0 );
591 ok(bNewSetting == 1, "Unicode setting did not work.\n");
593 /* Set to ANSI */
594 SendMessage( hTree, TVM_SETUNICODEFORMAT, 0, 0 );
595 bNewSetting = (BOOL)SendMessage( hTree, TVM_GETUNICODEFORMAT, 0, 0 );
596 ok(bNewSetting == 0, "ANSI setting did not work.\n");
598 /* Revert to original setting */
599 SendMessage( hTree, TVM_SETUNICODEFORMAT, (LPARAM)bPreviousSetting, 0 );
602 static void TestGetSet(void)
604 /* TVM_GETBKCOLOR and TVM_SETBKCOLOR */
605 flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
606 TestGetSetBkColor();
607 ok_sequence(MsgSequences, LISTVIEW_SEQ_INDEX, TestGetSetBkColorSeq,
608 "TestGetSetBkColor", FALSE);
610 /* TVM_GETIMAGELIST and TVM_SETIMAGELIST */
611 flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
612 TestGetSetImageList();
613 ok_sequence(MsgSequences, LISTVIEW_SEQ_INDEX, TestGetSetImageListSeq,
614 "TestGetImageList", FALSE);
616 /* TVM_SETINDENT and TVM_GETINDENT */
617 flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
618 TestGetSetIndent();
619 ok_sequence(MsgSequences, LISTVIEW_SEQ_INDEX, TestGetSetIndentSeq,
620 "TestGetSetIndent", FALSE);
622 /* TVM_GETINSERTMARKCOLOR and TVM_GETINSERTMARKCOLOR */
623 flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
624 TestGetSetInsertMarkColor();
625 ok_sequence(MsgSequences, LISTVIEW_SEQ_INDEX, TestGetSetInsertMarkColorSeq,
626 "TestGetSetInsertMarkColor", FALSE);
628 /* TVM_GETITEM and TVM_SETITEM */
629 flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
630 TestGetSetItem();
631 ok_sequence(MsgSequences, LISTVIEW_SEQ_INDEX, TestGetSetItemSeq,
632 "TestGetSetItem", FALSE);
634 /* TVM_GETITEMHEIGHT and TVM_SETITEMHEIGHT */
635 flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
636 TestGetSetItemHeight();
637 ok_sequence(MsgSequences, LISTVIEW_SEQ_INDEX, TestGetSetItemHeightSeq,
638 "TestGetSetItemHeight", FALSE);
640 /* TVM_GETSCROLLTIME and TVM_SETSCROLLTIME */
641 flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
642 TestGetSetScrollTime();
643 ok_sequence(MsgSequences, LISTVIEW_SEQ_INDEX, TestGetSetScrollTimeSeq,
644 "TestGetSetScrollTime", FALSE);
646 /* TVM_GETTEXTCOLOR and TVM_SETTEXTCOLOR */
647 flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
648 TestGetSetTextColor();
649 ok_sequence(MsgSequences, LISTVIEW_SEQ_INDEX, TestGetSetTextColorSeq,
650 "TestGetSetTextColor", FALSE);
652 /* TVM_GETTOOLTIPS and TVM_SETTOOLTIPS */
653 flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
654 TestGetSetToolTips();
655 ok_sequence(MsgSequences, LISTVIEW_SEQ_INDEX, TestGetSetToolTipsSeq,
656 "TestGetSetToolTips", TRUE);
658 /* TVM_GETUNICODEFORMAT and TVM_SETUNICODEFORMAT */
659 flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
660 TestGetSetUnicodeFormat();
661 ok_sequence(MsgSequences, LISTVIEW_SEQ_INDEX, TestGetSetUnicodeFormatSeq,
662 "TestGetSetUnicodeFormat", FALSE);
665 /* This function hooks in and records all messages to the treeview control */
666 static LRESULT WINAPI TreeviewWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
668 static LONG defwndproc_counter = 0;
669 LRESULT ret;
670 struct message msg;
671 WNDPROC lpOldProc = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
673 msg.message = message;
674 msg.flags = sent|wparam|lparam;
675 if (defwndproc_counter) msg.flags |= defwinproc;
676 msg.wParam = wParam;
677 msg.lParam = lParam;
678 add_message(MsgSequences, LISTVIEW_SEQ_INDEX, &msg);
680 defwndproc_counter++;
681 ret = CallWindowProcA(lpOldProc, hwnd, message, wParam, lParam);
682 defwndproc_counter--;
684 return ret;
687 static LRESULT CALLBACK MyWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
689 WNDPROC pOldWndProc;
691 switch(msg) {
693 case WM_CREATE:
695 hTree = CreateWindowExA(WS_EX_CLIENTEDGE, WC_TREEVIEWA, NULL, WS_CHILD|WS_VISIBLE|
696 TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS|TVS_EDITLABELS,
697 0, 0, 120, 100, hWnd, (HMENU)100, GetModuleHandleA(0), 0);
699 SetFocus(hTree);
701 /* Record the old WNDPROC so we can call it after recording the messages */
702 pOldWndProc = (WNDPROC)SetWindowLongPtrA(hTree, GWLP_WNDPROC, (LONG_PTR)TreeviewWndProc);
703 SetWindowLongPtrA(hTree, GWLP_USERDATA, (LONG_PTR)pOldWndProc);
705 return 0;
707 case WM_NOTIFY:
709 NMHDR *pHdr = (NMHDR *)lParam;
711 ok(pHdr->code != NM_FIRST - 19, "Treeview should not send NM_TOOLTIPSCREATED\n");
712 if (pHdr->idFrom == 100) {
713 NMTREEVIEWA *pTreeView = (LPNMTREEVIEWA) lParam;
714 switch(pHdr->code) {
715 case TVN_SELCHANGINGA:
716 AddItem('(');
717 IdentifyItem(pTreeView->itemOld.hItem);
718 IdentifyItem(pTreeView->itemNew.hItem);
719 return 0;
720 case TVN_SELCHANGEDA:
721 AddItem(')');
722 IdentifyItem(pTreeView->itemOld.hItem);
723 IdentifyItem(pTreeView->itemNew.hItem);
724 return 0;
725 case TVN_GETDISPINFOA: {
726 NMTVDISPINFOA *disp = (NMTVDISPINFOA *)lParam;
727 if (disp->item.mask & TVIF_TEXT) {
728 lstrcpyn(disp->item.pszText, TEST_CALLBACK_TEXT, disp->item.cchTextMax);
730 return 0;
734 return 0;
737 case WM_SIZE:
738 MoveWindow(hTree, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
739 break;
741 case WM_DESTROY:
742 PostQuitMessage(0);
743 break;
745 default:
746 return DefWindowProcA(hWnd, msg, wParam, lParam);
748 return 0L;
751 static void TestExpandInvisible(void)
753 static CHAR nodeText[][5] = {"0", "1", "2", "3", "4"};
754 TVINSERTSTRUCTA ins;
755 HTREEITEM node[5];
756 RECT dummyRect;
757 BOOL nodeVisible;
759 /* The test builds the following tree and expands then node 1, while node 0 is collapsed.
762 * |- 1
763 * | |- 2
764 * | |- 3
765 * |- 4
769 TreeView_DeleteAllItems(hTree);
771 ins.hParent = TVI_ROOT;
772 ins.hInsertAfter = TVI_ROOT;
773 U(ins).item.mask = TVIF_TEXT;
774 U(ins).item.pszText = nodeText[0];
775 node[0] = TreeView_InsertItem(hTree, &ins);
776 assert(node[0]);
778 ins.hInsertAfter = TVI_LAST;
779 U(ins).item.mask = TVIF_TEXT;
780 ins.hParent = node[0];
782 U(ins).item.pszText = nodeText[1];
783 node[1] = TreeView_InsertItem(hTree, &ins);
784 assert(node[1]);
785 U(ins).item.pszText = nodeText[4];
786 node[4] = TreeView_InsertItem(hTree, &ins);
787 assert(node[4]);
789 ins.hParent = node[1];
791 U(ins).item.pszText = nodeText[2];
792 node[2] = TreeView_InsertItem(hTree, &ins);
793 assert(node[2]);
794 U(ins).item.pszText = nodeText[3];
795 node[3] = TreeView_InsertItem(hTree, &ins);
796 assert(node[3]);
799 nodeVisible = TreeView_GetItemRect(hTree, node[1], &dummyRect, FALSE);
800 ok(!nodeVisible, "Node 1 should not be visible.\n");
801 nodeVisible = TreeView_GetItemRect(hTree, node[2], &dummyRect, FALSE);
802 ok(!nodeVisible, "Node 2 should not be visible.\n");
803 nodeVisible = TreeView_GetItemRect(hTree, node[3], &dummyRect, FALSE);
804 ok(!nodeVisible, "Node 3 should not be visible.\n");
805 nodeVisible = TreeView_GetItemRect(hTree, node[4], &dummyRect, FALSE);
806 ok(!nodeVisible, "Node 4 should not be visible.\n");
808 ok(TreeView_Expand(hTree, node[1], TVE_EXPAND), "Expand of node 1 failed.\n");
810 nodeVisible = TreeView_GetItemRect(hTree, node[1], &dummyRect, FALSE);
811 ok(!nodeVisible, "Node 1 should not be visible.\n");
812 nodeVisible = TreeView_GetItemRect(hTree, node[2], &dummyRect, FALSE);
813 ok(!nodeVisible, "Node 2 should not be visible.\n");
814 nodeVisible = TreeView_GetItemRect(hTree, node[3], &dummyRect, FALSE);
815 ok(!nodeVisible, "Node 3 should not be visible.\n");
816 nodeVisible = TreeView_GetItemRect(hTree, node[4], &dummyRect, FALSE);
817 ok(!nodeVisible, "Node 4 should not be visible.\n");
821 START_TEST(treeview)
823 HMODULE hComctl32;
824 BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
825 WNDCLASSA wc;
826 MSG msg;
828 hComctl32 = GetModuleHandleA("comctl32.dll");
829 pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
830 if (pInitCommonControlsEx)
832 INITCOMMONCONTROLSEX iccex;
833 iccex.dwSize = sizeof(iccex);
834 iccex.dwICC = ICC_TREEVIEW_CLASSES;
835 pInitCommonControlsEx(&iccex);
837 else
838 InitCommonControls();
840 init_msg_sequences(MsgSequences, NUM_MSG_SEQUENCES);
842 wc.style = CS_HREDRAW | CS_VREDRAW;
843 wc.cbClsExtra = 0;
844 wc.cbWndExtra = 0;
845 wc.hInstance = GetModuleHandleA(NULL);
846 wc.hIcon = NULL;
847 wc.hCursor = LoadCursorA(NULL, IDC_IBEAM);
848 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
849 wc.lpszMenuName = NULL;
850 wc.lpszClassName = "MyTestWnd";
851 wc.lpfnWndProc = MyWndProc;
852 RegisterClassA(&wc);
855 hMainWnd = CreateWindowExA(0, "MyTestWnd", "Blah", WS_OVERLAPPEDWINDOW,
856 CW_USEDEFAULT, CW_USEDEFAULT, 130, 105, NULL, NULL, GetModuleHandleA(NULL), 0);
858 if ( !ok(hMainWnd != NULL, "Failed to create parent window. Tests aborted.\n") )
859 return;
861 flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
862 FillRoot();
863 ok_sequence(MsgSequences, LISTVIEW_SEQ_INDEX, FillRootSeq, "FillRoot", FALSE);
865 flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
866 DoTest1();
867 ok_sequence(MsgSequences, LISTVIEW_SEQ_INDEX, DoTest1Seq, "DoTest1", FALSE);
869 flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
870 DoTest2();
871 ok_sequence(MsgSequences, LISTVIEW_SEQ_INDEX, DoTest2Seq, "DoTest2", FALSE);
873 flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
874 DoTest3();
875 ok_sequence(MsgSequences, LISTVIEW_SEQ_INDEX, DoTest3Seq, "DoTest3", FALSE);
877 flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
878 DoFocusTest();
879 ok_sequence(MsgSequences, LISTVIEW_SEQ_INDEX, DoFocusTestSeq, "DoFocusTest", TRUE);
881 /* Sequences tested inside due to number */
882 TestGetSet();
884 /* Clears all the previous items */
885 TestCallback();
887 /* Clears all the previous items */
888 TestExpandInvisible();
890 PostMessageA(hMainWnd, WM_CLOSE, 0, 0);
891 while(GetMessageA(&msg,0,0,0)) {
892 TranslateMessage(&msg);
893 DispatchMessageA(&msg);