comctl32/tests: Test expanding of a invisible sub tree.
[wine] / dlls / comctl32 / tests / treeview.c
1 /* Unit tests for treeview.
2  *
3  * Copyright 2005 Krzysztof Foltman
4  * Copyright 2007 Christopher James Peterson
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 <assert.h>
22 #include <stdarg.h>
23
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" 
31
32 #include "wine/test.h"
33 #include "msg.h"
34
35 const char *TEST_CALLBACK_TEXT = "callback_text";
36
37 #define NUM_MSG_SEQUENCES   1
38 #define LISTVIEW_SEQ_INDEX  0
39
40 static struct msg_sequence *MsgSequences[NUM_MSG_SEQUENCES];
41
42 static const struct message FillRootSeq[] = {
43     { TVM_INSERTITEM, sent },
44     { TVM_GETITEM, sent },
45     { TVM_INSERTITEM, sent },
46     { 0 }
47 };
48
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 }
57 };
58
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 }
67 };
68
69 static const struct message DoTest3Seq[] = {
70     { TVM_INSERTITEM, sent },
71     { TVM_GETITEM, sent },
72     { TVM_DELETEITEM, sent },
73     { 0 }
74 };
75
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 }
108 };
109
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 }
118 };
119
120 static const struct message TestGetSetImageListSeq[] = {
121     { TVM_SETIMAGELIST, sent|wparam|lparam, 0x00000000, 0x00000000 },
122     { TVM_GETIMAGELIST, sent|wparam|lparam, 0x00000000, 0x00000000 },
123     { 0 }
124 };
125
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 }
133 };
134
135 static const struct message TestGetSetInsertMarkColorSeq[] = {
136     { TVM_SETINSERTMARKCOLOR, sent|wparam|lparam, 0x00000000, 0x00000000 },
137     { TVM_GETINSERTMARKCOLOR, sent|wparam|lparam, 0x00000000, 0x00000000 },
138     { 0 }
139 };
140
141 static const struct message TestGetSetItemSeq[] = {
142     { TVM_GETITEM, sent },
143     { TVM_SETITEM, sent },
144     { TVM_GETITEM, sent },
145     { TVM_SETITEM, sent },
146     { 0 }
147 };
148
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 }
162 };
163
164 static const struct message TestGetSetScrollTimeSeq[] = {
165     { TVM_SETSCROLLTIME, sent|wparam|lparam, 0x00000014, 0x00000000 },
166     { TVM_GETSCROLLTIME, sent|wparam|lparam, 0x00000000, 0x00000000 },
167     { 0 }
168 };
169
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 }
178 };
179
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 }
186 };
187
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 }
195 };
196
197 static HWND hMainWnd;
198
199 static HWND hTree, hEdit;
200 static HTREEITEM hRoot, hChild;
201
202 static int pos = 0;
203 static char sequence[256];
204
205 static void Clear(void)
206 {
207     pos = 0;
208     sequence[0] = '\0';
209 }
210
211 static void AddItem(char ch)
212 {
213     sequence[pos++] = ch;
214     sequence[pos] = '\0';
215 }
216
217 static void IdentifyItem(HTREEITEM hItem)
218 {
219     if (hItem == hRoot) {
220         AddItem('R');
221         return;
222     }
223     if (hItem == hChild) {
224         AddItem('C');
225         return;
226     }
227     if (hItem == NULL) {
228         AddItem('n');
229         return;
230     }
231     AddItem('?');
232 }
233
234 static void FillRoot(void)
235 {
236     TVINSERTSTRUCTA ins;
237     TVITEM tvi;
238     static CHAR root[]  = "Root",
239                 child[] = "Child";
240
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);
249
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);
256
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('.');
265
266     ok(!strcmp(sequence, "AB."), "Item creation\n");
267 }
268
269 static void TestCallback(void)
270 {
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;
278
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);
286
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);
295
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);
302
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);
307
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);
316
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);
325 }
326
327 static void DoTest1(void)
328 {
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");
344 }
345
346 static void DoTest2(void)
347 {
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");
363 }
364
365 static void DoTest3(void)
366 {
367     TVINSERTSTRUCTA ins;
368     HTREEITEM hChild;
369     TVITEM tvi;
370
371     int nBufferSize = 80;
372     CHAR szBuffer[80] = "Blah";
373
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);
382
383     /* retrieve it with TVIF_TEXT mask */
384     tvi.hItem = hChild;
385     tvi.mask = TVIF_TEXT;
386     tvi.cchTextMax = nBufferSize;
387     tvi.pszText = szBuffer;
388
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");
392 }
393
394 static void DoFocusTest(void)
395 {
396     TVINSERTSTRUCTA ins;
397     static CHAR child1[]  = "Edit",
398                 child2[]  = "A really long string";
399     HTREEITEM hChild1, hChild2;
400
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.
405      */
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);
417
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");
424 }
425
426 static void TestGetSetBkColor(void)
427 {
428     COLORREF crColor = RGB(0,0,0);
429
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);
433
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);
438
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);
443
444     /* Reset the default background */
445     SendMessage( hTree, TVM_SETBKCOLOR, 0, -1 );
446 }
447
448 static void TestGetSetImageList(void)
449 {
450     HIMAGELIST hImageList = NULL;
451
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);
456
457     /* TODO: Test an actual image list */
458 }
459
460 static void TestGetSetIndent(void)
461 {
462     int ulIndent = -1;
463     int ulMinIndent = -1;
464     int ulMoreThanTwiceMin = -1;
465
466     /* Finding the minimum indent */
467     SendMessage( hTree, TVM_SETINDENT, 0, 0 );
468     ulMinIndent = (int)SendMessage( hTree, TVM_GETINDENT, 0, 0 );
469
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);
475 }
476
477 static void TestGetSetInsertMarkColor(void)
478 {
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);
483 }
484
485 static void TestGetSetItem(void)
486 {
487     TVITEM tviRoot = {0};
488     int nBufferSize = 80;
489     char szBuffer[80] = {0};
490
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);
498
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);
505
506     /* Reset the root text */
507     memset(szBuffer, 0, nBufferSize);
508     strncpy(szBuffer, "Root", nBufferSize);
509     SendMessage( hTree, TVM_SETITEM, 0, (LPARAM)&tviRoot );
510 }
511
512 static void TestGetSetItemHeight(void)
513 {
514     int ulOldHeight = 0;
515     int ulNewHeight = 0;
516
517     /* Assuming default height to begin with */
518     ulOldHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
519
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);
524
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);
529
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);
534 }
535
536 static void TestGetSetScrollTime(void)
537 {
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);
543 }
544
545 static void TestGetSetTextColor(void)
546 {
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);
551
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);
556
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);
561
562     /* Reset the default text color */
563     SendMessage( hTree, TVM_SETTEXTCOLOR, 0, -1 );
564 }
565
566 static void TestGetSetToolTips(void)
567 {
568     HWND hwndLastToolTip = NULL;
569     HWND hPopupTreeView;
570
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);
574
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);
579
580     /* TODO: Add a test of an actual tooltip */
581 }
582
583 static void TestGetSetUnicodeFormat(void)
584 {
585     BOOL bPreviousSetting = 0;
586     BOOL bNewSetting = 0;
587
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");
592
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");
597
598     /* Revert to original setting */
599     SendMessage( hTree, TVM_SETUNICODEFORMAT, (LPARAM)bPreviousSetting, 0 );
600 }
601
602 static void TestGetSet(void)
603 {
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);
609
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);
615
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);
621
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);
627
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);
633
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);
639
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);
645
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);
651
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);
657
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);
663 }
664
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)
667 {
668     static LONG defwndproc_counter = 0;
669     LRESULT ret;
670     struct message msg;
671     WNDPROC lpOldProc = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
672
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);
679
680     defwndproc_counter++;
681     ret = CallWindowProcA(lpOldProc, hwnd, message, wParam, lParam);
682     defwndproc_counter--;
683
684     return ret;
685 }
686
687 static LRESULT CALLBACK MyWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
688 {
689     WNDPROC pOldWndProc;
690
691     switch(msg) {
692
693     case WM_CREATE:
694     {
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);
698
699         SetFocus(hTree);
700
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);
704
705         return 0;
706     }
707     case WM_NOTIFY:
708     {
709         NMHDR *pHdr = (NMHDR *)lParam;
710     
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);
729                 }
730                 return 0;
731               }
732             }
733         }
734         return 0;
735     }
736   
737     case WM_SIZE:
738         MoveWindow(hTree, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
739         break;
740       
741     case WM_DESTROY:
742         PostQuitMessage(0);
743         break;
744   
745     default:
746         return DefWindowProcA(hWnd, msg, wParam, lParam);
747     }
748     return 0L;
749 }
750
751 static void TestExpandInvisible(void)
752 {
753     static CHAR nodeText[][5] = {"0", "1", "2", "3", "4"};
754     TVINSERTSTRUCTA ins;
755     HTREEITEM node[5];
756     RECT dummyRect;
757     BOOL nodeVisible;
758
759     /* The test builds the following tree and expands then node 1, while node 0 is collapsed.
760      *
761      * 0
762      * |- 1
763      * |  |- 2
764      * |  |- 3
765      * |- 4
766      *
767      */
768
769     TreeView_DeleteAllItems(hTree);
770
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]);
777
778     ins.hInsertAfter = TVI_LAST;
779     U(ins).item.mask = TVIF_TEXT;
780     ins.hParent = node[0];
781
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]);
788
789     ins.hParent = node[1];
790
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]);
797
798
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");
807
808     ok(TreeView_Expand(hTree, node[1], TVE_EXPAND), "Expand of node 1 failed.\n");
809
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 todo_wine
814     ok(!nodeVisible, "Node 2 should not be visible.\n");
815     nodeVisible = TreeView_GetItemRect(hTree, node[3], &dummyRect, FALSE);
816 todo_wine
817     ok(!nodeVisible, "Node 3 should not be visible.\n");
818     nodeVisible = TreeView_GetItemRect(hTree, node[4], &dummyRect, FALSE);
819 todo_wine
820     ok(!nodeVisible, "Node 4 should not be visible.\n");
821 }
822
823
824 START_TEST(treeview)
825 {
826     HMODULE hComctl32;
827     BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
828     WNDCLASSA wc;
829     MSG msg;
830   
831     hComctl32 = GetModuleHandleA("comctl32.dll");
832     pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
833     if (pInitCommonControlsEx)
834     {
835         INITCOMMONCONTROLSEX iccex;
836         iccex.dwSize = sizeof(iccex);
837         iccex.dwICC  = ICC_TREEVIEW_CLASSES;
838         pInitCommonControlsEx(&iccex);
839     }
840     else
841         InitCommonControls();
842
843     init_msg_sequences(MsgSequences, NUM_MSG_SEQUENCES);
844   
845     wc.style = CS_HREDRAW | CS_VREDRAW;
846     wc.cbClsExtra = 0;
847     wc.cbWndExtra = 0;
848     wc.hInstance = GetModuleHandleA(NULL);
849     wc.hIcon = NULL;
850     wc.hCursor = LoadCursorA(NULL, IDC_IBEAM);
851     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
852     wc.lpszMenuName = NULL;
853     wc.lpszClassName = "MyTestWnd";
854     wc.lpfnWndProc = MyWndProc;
855     RegisterClassA(&wc);
856
857
858     hMainWnd = CreateWindowExA(0, "MyTestWnd", "Blah", WS_OVERLAPPEDWINDOW,
859       CW_USEDEFAULT, CW_USEDEFAULT, 130, 105, NULL, NULL, GetModuleHandleA(NULL), 0);
860
861     if ( !ok(hMainWnd != NULL, "Failed to create parent window. Tests aborted.\n") )
862         return;
863
864     flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
865     FillRoot();
866     ok_sequence(MsgSequences, LISTVIEW_SEQ_INDEX, FillRootSeq, "FillRoot", FALSE);
867
868     flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
869     DoTest1();
870     ok_sequence(MsgSequences, LISTVIEW_SEQ_INDEX, DoTest1Seq, "DoTest1", FALSE);
871
872     flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
873     DoTest2();
874     ok_sequence(MsgSequences, LISTVIEW_SEQ_INDEX, DoTest2Seq, "DoTest2", FALSE);
875
876     flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
877     DoTest3();
878     ok_sequence(MsgSequences, LISTVIEW_SEQ_INDEX, DoTest3Seq, "DoTest3", FALSE);
879
880     flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
881     DoFocusTest();
882     ok_sequence(MsgSequences, LISTVIEW_SEQ_INDEX, DoFocusTestSeq, "DoFocusTest", TRUE);
883
884     /* Sequences tested inside due to number */
885     TestGetSet();
886
887     /* Clears all the previous items */
888     TestCallback();
889
890     /* Clears all the previous items */
891     TestExpandInvisible();
892
893     PostMessageA(hMainWnd, WM_CLOSE, 0, 0);
894     while(GetMessageA(&msg,0,0,0)) {
895         TranslateMessage(&msg);
896         DispatchMessageA(&msg);
897     }
898 }