comctl32/treeview: Fix TVN_DELETEITEM to use item that is about to be deleted.
[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 "v6util.h"
34 #include "msg.h"
35
36 static const char *TEST_CALLBACK_TEXT = "callback_text";
37
38 #define NUM_MSG_SEQUENCES   2
39 #define TREEVIEW_SEQ_INDEX  0
40 #define PARENT_SEQ_INDEX    1
41
42 #define expect(expected, got) ok(got == expected, "Expected %d, got %d\n", expected, got)
43
44 static struct msg_sequence *sequences[NUM_MSG_SEQUENCES];
45 static struct msg_sequence *item_sequence[1];
46
47 static const struct message FillRootSeq[] = {
48     { TVM_INSERTITEM, sent },
49     { TVM_INSERTITEM, sent },
50     { 0 }
51 };
52
53 static const struct message rootnone_select_seq[] = {
54     { TVM_SELECTITEM, sent|wparam, 9 },
55     { TVM_SELECTITEM, sent|wparam, 9 },
56     { TVM_SELECTITEM, sent|wparam, 9 },
57     { TVM_SELECTITEM, sent|wparam, 9 },
58     { TVM_SELECTITEM, sent|wparam, 9 },
59     { TVM_SELECTITEM, sent|wparam, 9 },
60     { 0 }
61 };
62
63 static const struct message rootchild_select_seq[] = {
64     { TVM_SELECTITEM, sent|wparam, 9 },
65     { TVM_SELECTITEM, sent|wparam, 9 },
66     { TVM_SELECTITEM, sent|wparam, 9 },
67     { TVM_SELECTITEM, sent|wparam, 9 },
68     { TVM_SELECTITEM, sent|wparam, 9 },
69     { TVM_SELECTITEM, sent|wparam, 9 },
70     { 0 }
71 };
72
73 static const struct message getitemtext_seq[] = {
74     { TVM_INSERTITEMA, sent },
75     { TVM_GETITEMA, sent },
76     { TVM_DELETEITEM, sent },
77     { 0 }
78 };
79
80 static const struct message focus_seq[] = {
81     { TVM_INSERTITEM, sent },
82     { TVM_INSERTITEM, sent },
83     { TVM_SELECTITEM, sent|wparam, 9 },
84     /* The following end up out of order in wine */
85     { WM_WINDOWPOSCHANGING, sent|defwinproc },
86     { WM_NCCALCSIZE, sent|wparam|defwinproc, TRUE },
87     { WM_WINDOWPOSCHANGED, sent|defwinproc },
88     { WM_SIZE, sent|defwinproc },
89     { WM_PAINT, sent|defwinproc },
90     { WM_NCPAINT, sent|wparam|defwinproc, 1 },
91     { WM_ERASEBKGND, sent|defwinproc },
92     { TVM_EDITLABEL, sent },
93     { WM_COMMAND, sent|wparam|defwinproc, MAKEWPARAM(0, EN_UPDATE) },
94     { WM_COMMAND, sent|wparam|defwinproc, MAKEWPARAM(0, EN_CHANGE) },
95     { WM_PARENTNOTIFY, sent|wparam|defwinproc, MAKEWPARAM(WM_CREATE, 0) },
96     { WM_KILLFOCUS, sent|defwinproc },
97     { WM_PAINT, sent|defwinproc },
98     { WM_IME_SETCONTEXT, sent|defwinproc|optional },
99     { WM_COMMAND, sent|wparam|defwinproc, MAKEWPARAM(0, EN_SETFOCUS) },
100     { WM_ERASEBKGND, sent|defwinproc|optional },
101     { WM_CTLCOLOREDIT, sent|defwinproc|optional },
102     { WM_CTLCOLOREDIT, sent|defwinproc|optional },
103     { 0 }
104 };
105
106 static const struct message test_get_set_bkcolor_seq[] = {
107     { TVM_GETBKCOLOR, sent|wparam|lparam, 0, 0 },
108     { TVM_SETBKCOLOR, sent|wparam|lparam, 0, 0 },
109     { TVM_GETBKCOLOR, sent|wparam|lparam, 0, 0 },
110     { TVM_SETBKCOLOR, sent|wparam|lparam, 0, 0x00ffffff },
111     { TVM_GETBKCOLOR, sent|wparam|lparam, 0, 0 },
112     { TVM_SETBKCOLOR, sent|wparam|lparam, 0, -1 },
113     { 0 }
114 };
115
116 static const struct message test_get_set_imagelist_seq[] = {
117     { TVM_SETIMAGELIST, sent|wparam|lparam, 0, 0 },
118     { TVM_GETIMAGELIST, sent|wparam|lparam, 0, 0 },
119     { 0 }
120 };
121
122 static const struct message test_get_set_indent_seq[] = {
123     { TVM_SETINDENT, sent|wparam|lparam, 0, 0 },
124     { TVM_GETINDENT, sent|wparam|lparam, 0, 0 },
125     /* The actual amount to indent is dependent on the system for this message */
126     { TVM_SETINDENT, sent },
127     { TVM_GETINDENT, sent|wparam|lparam, 0, 0 },
128     { 0 }
129 };
130
131 static const struct message test_get_set_insertmarkcolor_seq[] = {
132     { TVM_SETINSERTMARKCOLOR, sent|wparam|lparam, 0, 0 },
133     { TVM_GETINSERTMARKCOLOR, sent|wparam|lparam, 0, 0 },
134     { 0 }
135 };
136
137 static const struct message test_get_set_item_seq[] = {
138     { TVM_GETITEMA, sent },
139     { TVM_SETITEMA, sent },
140     { TVM_GETITEMA, sent },
141     { TVM_SETITEMA, sent },
142     { 0 }
143 };
144
145 static const struct message test_get_set_itemheight_seq[] = {
146     { TVM_GETITEMHEIGHT, sent|wparam|lparam, 0, 0 },
147     { TVM_SETITEMHEIGHT, sent|wparam|lparam, -1, 0 },
148     { TVM_GETITEMHEIGHT, sent|wparam|lparam, 0, 0 },
149     { TVM_SETITEMHEIGHT, sent|lparam, 0xcccccccc, 0 },
150     { TVM_GETITEMHEIGHT, sent|wparam|lparam|optional, 0, 0 },
151     { TVM_SETITEMHEIGHT, sent|wparam|lparam|optional, 9, 0 },
152     { TVM_GETITEMHEIGHT, sent|wparam|lparam, 0, 0 },
153     { 0 }
154 };
155
156 static const struct message test_get_set_scrolltime_seq[] = {
157     { TVM_SETSCROLLTIME, sent|wparam|lparam, 20, 0 },
158     { TVM_GETSCROLLTIME, sent|wparam|lparam, 0, 0 },
159     { 0 }
160 };
161
162 static const struct message test_get_set_textcolor_seq[] = {
163     { TVM_GETTEXTCOLOR, sent|wparam|lparam, 0, 0 },
164     { TVM_SETTEXTCOLOR, sent|wparam|lparam, 0, 0 },
165     { TVM_GETTEXTCOLOR, sent|wparam|lparam, 0, 0 },
166     { TVM_SETTEXTCOLOR, sent|wparam|lparam, 0, RGB(255, 255, 255) },
167     { TVM_GETTEXTCOLOR, sent|wparam|lparam, 0, 0 },
168     { TVM_SETTEXTCOLOR, sent|wparam|lparam, 0, CLR_NONE },
169     { 0 }
170 };
171
172 static const struct message test_get_set_tooltips_seq[] = {
173     { WM_KILLFOCUS,    sent },
174     { WM_IME_SETCONTEXT, sent|optional },
175     { WM_IME_NOTIFY, sent|optional },
176     { TVM_SETTOOLTIPS, sent|wparam|lparam, 0, 0 },
177     { TVM_GETTOOLTIPS, sent|wparam|lparam, 0, 0 },
178     { 0 }
179 };
180
181 static const struct message test_get_set_unicodeformat_seq[] = {
182     { TVM_SETUNICODEFORMAT, sent|wparam|lparam, TRUE, 0 },
183     { TVM_GETUNICODEFORMAT, sent|wparam|lparam, 0, 0 },
184     { TVM_SETUNICODEFORMAT, sent|wparam|lparam, 0, 0 },
185     { TVM_GETUNICODEFORMAT, sent|wparam|lparam, 0, 0 },
186     { TVM_SETUNICODEFORMAT, sent|wparam|lparam, 0, 0 },
187     { 0 }
188 };
189
190 static const struct message parent_expand_seq[] = {
191     { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDINGA },
192     { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDEDA },
193     { 0 }
194 };
195
196 static const struct message parent_singleexpand_seq[] = {
197     { WM_NOTIFY, sent|id, 0, 0, TVN_SELCHANGINGA },
198     { WM_NOTIFY, sent|id, 0, 0, TVN_SELCHANGEDA },
199     { WM_NOTIFY, sent|id, 0, 0, TVN_SINGLEEXPAND },
200     { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDINGA },
201     { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDEDA },
202     { 0 }
203 };
204
205 static const struct message empty_seq[] = {
206     { 0 }
207 };
208
209 static HWND hMainWnd;
210
211 static HTREEITEM hRoot, hChild;
212
213 static int pos = 0;
214 static char sequence[256];
215
216 static void Clear(void)
217 {
218     pos = 0;
219     sequence[0] = '\0';
220 }
221
222 static void AddItem(char ch)
223 {
224     sequence[pos++] = ch;
225     sequence[pos] = '\0';
226 }
227
228 static void IdentifyItem(HTREEITEM hItem)
229 {
230     if (hItem == hRoot) {
231         AddItem('R');
232         return;
233     }
234     if (hItem == hChild) {
235         AddItem('C');
236         return;
237     }
238     if (hItem == NULL) {
239         AddItem('n');
240         return;
241     }
242     AddItem('?');
243 }
244
245 /* This function hooks in and records all messages to the treeview control */
246 static LRESULT WINAPI TreeviewWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
247 {
248     static LONG defwndproc_counter = 0;
249     LRESULT ret;
250     struct message msg;
251     WNDPROC lpOldProc = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
252
253     msg.message = message;
254     msg.flags = sent|wparam|lparam;
255     if (defwndproc_counter) msg.flags |= defwinproc;
256     msg.wParam = wParam;
257     msg.lParam = lParam;
258     add_message(sequences, TREEVIEW_SEQ_INDEX, &msg);
259
260     defwndproc_counter++;
261     ret = CallWindowProcA(lpOldProc, hwnd, message, wParam, lParam);
262     defwndproc_counter--;
263
264     return ret;
265 }
266
267 static HWND create_treeview_control(void)
268 {
269     WNDPROC pOldWndProc;
270     HWND hTree;
271
272     hTree = CreateWindowExA(WS_EX_CLIENTEDGE, WC_TREEVIEWA, NULL, WS_CHILD|WS_VISIBLE|
273             TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS|TVS_EDITLABELS,
274             0, 0, 120, 100, hMainWnd, (HMENU)100, GetModuleHandleA(0), 0);
275
276     SetFocus(hTree);
277
278     /* Record the old WNDPROC so we can call it after recording the messages */
279     pOldWndProc = (WNDPROC)SetWindowLongPtrA(hTree, GWLP_WNDPROC, (LONG_PTR)TreeviewWndProc);
280     SetWindowLongPtrA(hTree, GWLP_USERDATA, (LONG_PTR)pOldWndProc);
281
282     return hTree;
283 }
284
285 static void fill_tree(HWND hTree)
286 {
287     TVINSERTSTRUCTA ins;
288     static CHAR root[]  = "Root",
289                 child[] = "Child";
290
291     ins.hParent = TVI_ROOT;
292     ins.hInsertAfter = TVI_ROOT;
293     U(ins).item.mask = TVIF_TEXT;
294     U(ins).item.pszText = root;
295     hRoot = TreeView_InsertItem(hTree, &ins);
296
297     ins.hParent = hRoot;
298     ins.hInsertAfter = TVI_FIRST;
299     U(ins).item.mask = TVIF_TEXT;
300     U(ins).item.pszText = child;
301     hChild = TreeView_InsertItem(hTree, &ins);
302 }
303
304 static void test_fillroot(void)
305 {
306     TVITEMA tvi;
307     HWND hTree;
308
309     hTree = create_treeview_control();
310
311     flush_sequences(sequences, NUM_MSG_SEQUENCES);
312
313     fill_tree(hTree);
314
315     Clear();
316     AddItem('A');
317     assert(hRoot);
318     AddItem('B');
319     assert(hChild);
320     AddItem('.');
321     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, FillRootSeq, "FillRoot", FALSE);
322     ok(!strcmp(sequence, "AB."), "Item creation\n");
323
324     /* UMLPad 1.15 depends on this being not -1 (I_IMAGECALLBACK) */
325     tvi.hItem = hRoot;
326     tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
327     SendMessage( hTree, TVM_GETITEMA, 0, (LPARAM)&tvi );
328     ok(tvi.iImage == 0, "tvi.iImage=%d\n", tvi.iImage);
329     ok(tvi.iSelectedImage == 0, "tvi.iSelectedImage=%d\n", tvi.iSelectedImage);
330
331     DestroyWindow(hTree);
332 }
333
334 static void test_callback(void)
335 {
336     HTREEITEM hRoot;
337     HTREEITEM hItem1, hItem2;
338     TVINSERTSTRUCTA ins;
339     TVITEMA tvi;
340     CHAR test_string[] = "Test_string";
341     CHAR buf[128];
342     LRESULT ret;
343     HWND hTree;
344
345     hTree = create_treeview_control();
346     fill_tree(hTree);
347
348     ret = TreeView_DeleteAllItems(hTree);
349     ok(ret == TRUE, "ret\n");
350     ins.hParent = TVI_ROOT;
351     ins.hInsertAfter = TVI_ROOT;
352     U(ins).item.mask = TVIF_TEXT;
353     U(ins).item.pszText = LPSTR_TEXTCALLBACK;
354     hRoot = TreeView_InsertItem(hTree, &ins);
355     assert(hRoot);
356
357     tvi.hItem = hRoot;
358     tvi.mask = TVIF_TEXT;
359     tvi.pszText = buf;
360     tvi.cchTextMax = sizeof(buf)/sizeof(buf[0]);
361     ret = TreeView_GetItem(hTree, &tvi);
362     ok(ret == 1, "ret\n");
363     ok(strcmp(tvi.pszText, TEST_CALLBACK_TEXT) == 0, "Callback item text mismatch %s vs %s\n",
364         tvi.pszText, TEST_CALLBACK_TEXT);
365
366     ins.hParent = hRoot;
367     ins.hInsertAfter = TVI_FIRST;
368     U(ins).item.mask = TVIF_TEXT;
369     U(ins).item.pszText = test_string;
370     hItem1 = TreeView_InsertItem(hTree, &ins);
371     assert(hItem1);
372
373     tvi.hItem = hItem1;
374     ret = TreeView_GetItem(hTree, &tvi);
375     ok(ret == TRUE, "ret\n");
376     ok(strcmp(tvi.pszText, test_string) == 0, "Item text mismatch %s vs %s\n",
377         tvi.pszText, test_string);
378
379     /* undocumented: pszText of NULL also means LPSTR_CALLBACK: */
380     tvi.pszText = NULL;
381     ret = TreeView_SetItem(hTree, &tvi);
382     ok(ret == 1, "Expected SetItem return 1, got %ld\n", ret);
383     tvi.pszText = buf;
384     ret = TreeView_GetItem(hTree, &tvi);
385     ok(ret == TRUE, "Expected GetItem return TRUE, got %ld\n", ret);
386     ok(strcmp(tvi.pszText, TEST_CALLBACK_TEXT) == 0, "Item text mismatch %s vs %s\n",
387         tvi.pszText, TEST_CALLBACK_TEXT);
388
389     U(ins).item.pszText = NULL;
390     hItem2 = TreeView_InsertItem(hTree, &ins);
391     assert(hItem2);
392     tvi.hItem = hItem2;
393     memset(buf, 0, sizeof(buf));
394     ret = TreeView_GetItem(hTree, &tvi);
395     ok(ret == TRUE, "Expected GetItem return TRUE, got %ld\n", ret);
396     ok(strcmp(tvi.pszText, TEST_CALLBACK_TEXT) == 0, "Item text mismatch %s vs %s\n",
397         tvi.pszText, TEST_CALLBACK_TEXT);
398
399     DestroyWindow(hTree);
400 }
401
402 static void test_select(void)
403 {
404     BOOL r;
405     HWND hTree;
406
407     hTree = create_treeview_control();
408     fill_tree(hTree);
409
410     /* root-none select tests */
411     flush_sequences(sequences, NUM_MSG_SEQUENCES);
412     r = TreeView_SelectItem(hTree, NULL);
413     expect(TRUE, r);
414     Clear();
415     AddItem('1');
416     r = TreeView_SelectItem(hTree, hRoot);
417     expect(TRUE, r);
418     AddItem('2');
419     r = TreeView_SelectItem(hTree, hRoot);
420     expect(TRUE, r);
421     AddItem('3');
422     r = TreeView_SelectItem(hTree, NULL);
423     expect(TRUE, r);
424     AddItem('4');
425     r = TreeView_SelectItem(hTree, NULL);
426     expect(TRUE, r);
427     AddItem('5');
428     r = TreeView_SelectItem(hTree, hRoot);
429     expect(TRUE, r);
430     AddItem('.');
431     ok(!strcmp(sequence, "1(nR)nR23(Rn)Rn45(nR)nR."), "root-none select test\n");
432     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, rootnone_select_seq,
433                 "root-none select seq", FALSE);
434
435     /* root-child select tests */
436     flush_sequences(sequences, NUM_MSG_SEQUENCES);
437     r = TreeView_SelectItem(hTree, NULL);
438     expect(TRUE, r);
439
440     Clear();
441     AddItem('1');
442     r = TreeView_SelectItem(hTree, hRoot);
443     expect(TRUE, r);
444     AddItem('2');
445     r = TreeView_SelectItem(hTree, hRoot);
446     expect(TRUE, r);
447     AddItem('3');
448     r = TreeView_SelectItem(hTree, hChild);
449     expect(TRUE, r);
450     AddItem('4');
451     r = TreeView_SelectItem(hTree, hChild);
452     expect(TRUE, r);
453     AddItem('5');
454     r = TreeView_SelectItem(hTree, hRoot);
455     expect(TRUE, r);
456     AddItem('.');
457     ok(!strcmp(sequence, "1(nR)nR23(RC)RC45(CR)CR."), "root-child select test\n");
458     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, rootchild_select_seq,
459                 "root-child select seq", FALSE);
460
461     DestroyWindow(hTree);
462 }
463
464 static void test_getitemtext(void)
465 {
466     TVINSERTSTRUCTA ins;
467     HTREEITEM hChild;
468     TVITEMA tvi;
469     HWND hTree;
470
471     CHAR szBuffer[80] = "Blah";
472     int nBufferSize = sizeof(szBuffer)/sizeof(CHAR);
473
474     hTree = create_treeview_control();
475     fill_tree(hTree);
476
477     flush_sequences(sequences, NUM_MSG_SEQUENCES);
478
479     /* add an item without TVIF_TEXT mask and pszText == NULL */
480     ins.hParent = hRoot;
481     ins.hInsertAfter = TVI_ROOT;
482     U(ins).item.mask = 0;
483     U(ins).item.pszText = NULL;
484     U(ins).item.cchTextMax = 0;
485     hChild = TreeView_InsertItem(hTree, &ins);
486     assert(hChild);
487
488     /* retrieve it with TVIF_TEXT mask */
489     tvi.hItem = hChild;
490     tvi.mask = TVIF_TEXT;
491     tvi.cchTextMax = nBufferSize;
492     tvi.pszText = szBuffer;
493
494     SendMessageA( hTree, TVM_GETITEMA, 0, (LPARAM)&tvi );
495     ok(!strcmp(szBuffer, ""), "szBuffer=\"%s\", expected \"\"\n", szBuffer);
496     ok(SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)hChild), "DeleteItem failed\n");
497     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, getitemtext_seq, "get item text seq", FALSE);
498
499     DestroyWindow(hTree);
500 }
501
502 static void test_focus(void)
503 {
504     TVINSERTSTRUCTA ins;
505     static CHAR child1[]  = "Edit",
506                 child2[]  = "A really long string";
507     HTREEITEM hChild1, hChild2;
508     HWND hTree;
509     HWND hEdit;
510
511     hTree = create_treeview_control();
512     fill_tree(hTree);
513
514     flush_sequences(sequences, NUM_MSG_SEQUENCES);
515
516     /* This test verifies that when a label is being edited, scrolling
517      * the treeview does not cause the label to lose focus. To test
518      * this, first some additional entries are added to generate
519      * scrollbars.
520      */
521     ins.hParent = hRoot;
522     ins.hInsertAfter = hChild;
523     U(ins).item.mask = TVIF_TEXT;
524     U(ins).item.pszText = child1;
525     hChild1 = TreeView_InsertItem(hTree, &ins);
526     assert(hChild1);
527     ins.hInsertAfter = hChild1;
528     U(ins).item.mask = TVIF_TEXT;
529     U(ins).item.pszText = child2;
530     hChild2 = TreeView_InsertItem(hTree, &ins);
531     assert(hChild2);
532
533     ShowWindow(hMainWnd,SW_SHOW);
534     SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hChild);
535     hEdit = TreeView_EditLabel(hTree, hChild);
536     ScrollWindowEx(hTree, -10, 0, NULL, NULL, NULL, NULL, SW_SCROLLCHILDREN);
537     ok(GetFocus() == hEdit, "Edit control should have focus\n");
538     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, focus_seq, "focus test", TRUE);
539
540     DestroyWindow(hTree);
541 }
542
543 static void test_get_set_bkcolor(void)
544 {
545     COLORREF crColor = RGB(0,0,0);
546     HWND hTree;
547
548     hTree = create_treeview_control();
549     fill_tree(hTree);
550
551     flush_sequences(sequences, NUM_MSG_SEQUENCES);
552
553     /* If the value is -1, the control is using the system color for the background color. */
554     crColor = (COLORREF)SendMessage( hTree, TVM_GETBKCOLOR, 0, 0 );
555     ok(crColor == -1, "Default background color reported as 0x%.8x\n", crColor);
556
557     /* Test for black background */
558     SendMessage( hTree, TVM_SETBKCOLOR, 0, RGB(0,0,0) );
559     crColor = (COLORREF)SendMessage( hTree, TVM_GETBKCOLOR, 0, 0 );
560     ok(crColor == RGB(0,0,0), "Black background color reported as 0x%.8x\n", crColor);
561
562     /* Test for white background */
563     SendMessage( hTree, TVM_SETBKCOLOR, 0, RGB(255,255,255) );
564     crColor = (COLORREF)SendMessage( hTree, TVM_GETBKCOLOR, 0, 0 );
565     ok(crColor == RGB(255,255,255), "White background color reported as 0x%.8x\n", crColor);
566
567     /* Reset the default background */
568     SendMessage( hTree, TVM_SETBKCOLOR, 0, -1 );
569
570     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_bkcolor_seq,
571         "test get set bkcolor", FALSE);
572
573     DestroyWindow(hTree);
574 }
575
576 static void test_get_set_imagelist(void)
577 {
578     HIMAGELIST hImageList = NULL;
579     HWND hTree;
580
581     hTree = create_treeview_control();
582     fill_tree(hTree);
583
584     flush_sequences(sequences, NUM_MSG_SEQUENCES);
585
586     /* Test a NULL HIMAGELIST */
587     SendMessage( hTree, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)hImageList );
588     hImageList = (HIMAGELIST)SendMessage( hTree, TVM_GETIMAGELIST, TVSIL_NORMAL, 0 );
589     ok(hImageList == NULL, "NULL image list, reported as 0x%p, expected 0.\n", hImageList);
590
591     /* TODO: Test an actual image list */
592
593     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_imagelist_seq,
594         "test get imagelist", FALSE);
595
596     DestroyWindow(hTree);
597 }
598
599 static void test_get_set_indent(void)
600 {
601     int ulIndent = -1;
602     int ulMinIndent = -1;
603     int ulMoreThanTwiceMin = -1;
604     HWND hTree;
605
606     hTree = create_treeview_control();
607     fill_tree(hTree);
608
609     flush_sequences(sequences, NUM_MSG_SEQUENCES);
610
611     /* Finding the minimum indent */
612     SendMessage( hTree, TVM_SETINDENT, 0, 0 );
613     ulMinIndent = (int)SendMessage( hTree, TVM_GETINDENT, 0, 0 );
614
615     /* Checking an indent that is more than twice the default indent */
616     ulMoreThanTwiceMin = 2*ulMinIndent+1;
617     SendMessage( hTree, TVM_SETINDENT, ulMoreThanTwiceMin, 0 );
618     ulIndent = (DWORD)SendMessage( hTree, TVM_GETINDENT, 0, 0 );
619     ok(ulIndent == ulMoreThanTwiceMin, "Indent reported as %d, expected %d\n", ulIndent, ulMoreThanTwiceMin);
620
621     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_indent_seq,
622         "test get set indent", FALSE);
623
624     DestroyWindow(hTree);
625 }
626
627 static void test_get_set_insertmark(void)
628 {
629     COLORREF crColor = RGB(0,0,0);
630     HWND hTree;
631
632     hTree = create_treeview_control();
633     fill_tree(hTree);
634
635     flush_sequences(sequences, NUM_MSG_SEQUENCES);
636
637     SendMessage( hTree, TVM_SETINSERTMARKCOLOR, 0, crColor );
638     crColor = (COLORREF)SendMessage( hTree, TVM_GETINSERTMARKCOLOR, 0, 0 );
639     ok(crColor == RGB(0,0,0), "Insert mark color reported as 0x%.8x, expected 0x00000000\n", crColor);
640
641     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_insertmarkcolor_seq,
642         "test get set insertmark color", FALSE);
643
644     DestroyWindow(hTree);
645 }
646
647 static void test_get_set_item(void)
648 {
649     TVITEMA tviRoot = {0};
650     int nBufferSize = 80;
651     char szBuffer[80] = {0};
652     HWND hTree;
653
654     hTree = create_treeview_control();
655     fill_tree(hTree);
656
657     flush_sequences(sequences, NUM_MSG_SEQUENCES);
658
659     /* Test the root item */
660     tviRoot.hItem = hRoot;
661     tviRoot.mask = TVIF_TEXT;
662     tviRoot.cchTextMax = nBufferSize;
663     tviRoot.pszText = szBuffer;
664     SendMessage( hTree, TVM_GETITEMA, 0, (LPARAM)&tviRoot );
665     ok(!strcmp("Root", szBuffer), "GetItem: szBuffer=\"%s\", expected \"Root\"\n", szBuffer);
666
667     /* Change the root text */
668     strncpy(szBuffer, "Testing123", nBufferSize);
669     SendMessage( hTree, TVM_SETITEMA, 0, (LPARAM)&tviRoot );
670     memset(szBuffer, 0, nBufferSize);
671     SendMessage( hTree, TVM_GETITEMA, 0, (LPARAM)&tviRoot );
672     ok(!strcmp("Testing123", szBuffer), "GetItem: szBuffer=\"%s\", expected \"Testing123\"\n", szBuffer);
673
674     /* Reset the root text */
675     memset(szBuffer, 0, nBufferSize);
676     strncpy(szBuffer, "Root", nBufferSize);
677     SendMessage( hTree, TVM_SETITEMA, 0, (LPARAM)&tviRoot );
678
679     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_item_seq,
680         "test get set item", FALSE);
681
682     DestroyWindow(hTree);
683 }
684
685 static void test_get_set_itemheight(void)
686 {
687     int ulOldHeight = 0;
688     int ulNewHeight = 0;
689     HWND hTree;
690
691     hTree = create_treeview_control();
692     fill_tree(hTree);
693
694     flush_sequences(sequences, NUM_MSG_SEQUENCES);
695
696     /* Assuming default height to begin with */
697     ulOldHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
698
699     /* Explicitly setting and getting the default height */
700     SendMessage( hTree, TVM_SETITEMHEIGHT, -1, 0 );
701     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
702     ok(ulNewHeight == ulOldHeight, "Default height not set properly, reported %d, expected %d\n", ulNewHeight, ulOldHeight);
703
704     /* Explicitly setting and getting the height of twice the normal */
705     SendMessage( hTree, TVM_SETITEMHEIGHT, 2*ulOldHeight, 0 );
706     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
707     ok(ulNewHeight == 2*ulOldHeight, "New height not set properly, reported %d, expected %d\n", ulNewHeight, 2*ulOldHeight);
708
709     /* Assuming tree doesn't have TVS_NONEVENHEIGHT set, so a set of 9 will round down to 8 */
710     SendMessage( hTree, TVM_SETITEMHEIGHT, 9, 0 );
711     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
712     ok(ulNewHeight == 8, "Uneven height not set properly, reported %d, expected %d\n", ulNewHeight, 8);
713
714     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_itemheight_seq,
715         "test get set item height", FALSE);
716
717     /* without TVS_NONEVENHEIGHT */
718     SetWindowLong(hTree, GWL_STYLE, GetWindowLong(hTree, GWL_STYLE) & ~TVS_NONEVENHEIGHT);
719     /* odd value */
720     ulOldHeight = SendMessage( hTree, TVM_SETITEMHEIGHT, 3, 0);
721     ok(ulOldHeight == 8, "got %d, expected %d\n", ulOldHeight, 8);
722     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
723     ok(ulNewHeight == 2, "got %d, expected %d\n", ulNewHeight, 2);
724
725     ulOldHeight = SendMessage( hTree, TVM_SETITEMHEIGHT, 4, 0);
726     ok(ulOldHeight == 2, "got %d, expected %d\n", ulOldHeight, 2);
727     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
728     ok(ulNewHeight == 4, "got %d, expected %d\n", ulNewHeight, 4);
729
730     /* with TVS_NONEVENHEIGHT */
731     SetWindowLong(hTree, GWL_STYLE, GetWindowLong(hTree, GWL_STYLE) | TVS_NONEVENHEIGHT);
732     /* odd value */
733     ulOldHeight = SendMessage( hTree, TVM_SETITEMHEIGHT, 3, 0);
734     ok(ulOldHeight == 4, "got %d, expected %d\n", ulOldHeight, 4);
735     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
736     ok(ulNewHeight == 3, "got %d, expected %d\n", ulNewHeight, 3);
737     /* even value */
738     ulOldHeight = SendMessage( hTree, TVM_SETITEMHEIGHT, 10, 0);
739     ok(ulOldHeight == 3, "got %d, expected %d\n", ulOldHeight, 3);
740     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
741     ok(ulNewHeight == 10, "got %d, expected %d\n", ulNewHeight, 10);
742
743     DestroyWindow(hTree);
744 }
745
746 static void test_get_set_scrolltime(void)
747 {
748     int ulExpectedTime = 20;
749     int ulTime = 0;
750     HWND hTree;
751
752     hTree = create_treeview_control();
753     fill_tree(hTree);
754
755     flush_sequences(sequences, NUM_MSG_SEQUENCES);
756
757     SendMessage( hTree, TVM_SETSCROLLTIME, ulExpectedTime, 0 );
758     ulTime = (int)SendMessage( hTree, TVM_GETSCROLLTIME, 0, 0 );
759     ok(ulTime == ulExpectedTime, "Scroll time reported as %d, expected %d\n", ulTime, ulExpectedTime);
760
761     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_scrolltime_seq,
762         "test get set scroll time", FALSE);
763
764     DestroyWindow(hTree);
765 }
766
767 static void test_get_set_textcolor(void)
768 {
769     /* If the value is -1, the control is using the system color for the text color. */
770     COLORREF crColor = RGB(0,0,0);
771     HWND hTree;
772
773     hTree = create_treeview_control();
774     fill_tree(hTree);
775
776     flush_sequences(sequences, NUM_MSG_SEQUENCES);
777
778     crColor = (COLORREF)SendMessage( hTree, TVM_GETTEXTCOLOR, 0, 0 );
779     ok(crColor == -1, "Default text color reported as 0x%.8x\n", crColor);
780
781     /* Test for black text */
782     SendMessage( hTree, TVM_SETTEXTCOLOR, 0, RGB(0,0,0) );
783     crColor = (COLORREF)SendMessage( hTree, TVM_GETTEXTCOLOR, 0, 0 );
784     ok(crColor == RGB(0,0,0), "Black text color reported as 0x%.8x\n", crColor);
785
786     /* Test for white text */
787     SendMessage( hTree, TVM_SETTEXTCOLOR, 0, RGB(255,255,255) );
788     crColor = (COLORREF)SendMessage( hTree, TVM_GETTEXTCOLOR, 0, 0 );
789     ok(crColor == RGB(255,255,255), "White text color reported as 0x%.8x\n", crColor);
790
791     /* Reset the default text color */
792     SendMessage( hTree, TVM_SETTEXTCOLOR, 0, CLR_NONE );
793
794     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_textcolor_seq,
795         "test get set text color", FALSE);
796
797     DestroyWindow(hTree);
798 }
799
800 static void test_get_set_tooltips(void)
801 {
802     HWND hwndLastToolTip = NULL;
803     HWND hPopupTreeView;
804     HWND hTree;
805
806     hTree = create_treeview_control();
807     fill_tree(hTree);
808
809     flush_sequences(sequences, NUM_MSG_SEQUENCES);
810
811     /* show even WS_POPUP treeview don't send NM_TOOLTIPSCREATED */
812     hPopupTreeView = CreateWindow(WC_TREEVIEW, NULL, WS_POPUP|WS_VISIBLE, 0, 0, 100, 100, hMainWnd, NULL, NULL, NULL);
813     DestroyWindow(hPopupTreeView);
814
815     /* Testing setting a NULL ToolTip */
816     SendMessage( hTree, TVM_SETTOOLTIPS, 0, 0 );
817     hwndLastToolTip = (HWND)SendMessage( hTree, TVM_GETTOOLTIPS, 0, 0 );
818     ok(hwndLastToolTip == NULL, "NULL tool tip, reported as 0x%p, expected 0.\n", hwndLastToolTip);
819
820     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_tooltips_seq,
821         "test get set tooltips", TRUE);
822
823     /* TODO: Add a test of an actual tooltip */
824     DestroyWindow(hTree);
825 }
826
827 static void test_get_set_unicodeformat(void)
828 {
829     BOOL bPreviousSetting = 0;
830     BOOL bNewSetting = 0;
831     HWND hTree;
832
833     hTree = create_treeview_control();
834     fill_tree(hTree);
835
836     flush_sequences(sequences, NUM_MSG_SEQUENCES);
837
838     /* Set to Unicode */
839     bPreviousSetting = (BOOL)SendMessage( hTree, TVM_SETUNICODEFORMAT, 1, 0 );
840     bNewSetting = (BOOL)SendMessage( hTree, TVM_GETUNICODEFORMAT, 0, 0 );
841     ok(bNewSetting == 1, "Unicode setting did not work.\n");
842
843     /* Set to ANSI */
844     SendMessage( hTree, TVM_SETUNICODEFORMAT, 0, 0 );
845     bNewSetting = (BOOL)SendMessage( hTree, TVM_GETUNICODEFORMAT, 0, 0 );
846     ok(bNewSetting == 0, "ANSI setting did not work.\n");
847
848     /* Revert to original setting */
849     SendMessage( hTree, TVM_SETUNICODEFORMAT, bPreviousSetting, 0 );
850
851     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_unicodeformat_seq,
852         "test get set unicode format", FALSE);
853
854     DestroyWindow(hTree);
855 }
856
857 static TVITEMA g_item_expanding, g_item_expanded;
858 static BOOL g_get_from_expand;
859 static BOOL g_get_rect_in_expand;
860
861 static LRESULT CALLBACK parent_wnd_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
862 {
863     static LONG defwndproc_counter = 0;
864     struct message msg;
865     LRESULT ret;
866     RECT rect;
867     HTREEITEM visibleItem;
868
869     msg.message = message;
870     msg.flags = sent|wparam|lparam;
871     if (defwndproc_counter) msg.flags |= defwinproc;
872     msg.wParam = wParam;
873     msg.lParam = lParam;
874     if (message == WM_NOTIFY && lParam) msg.id = ((NMHDR*)lParam)->code;
875
876     /* log system messages, except for painting */
877     if (message < WM_USER &&
878         message != WM_PAINT &&
879         message != WM_ERASEBKGND &&
880         message != WM_NCPAINT &&
881         message != WM_NCHITTEST &&
882         message != WM_GETTEXT &&
883         message != WM_GETICON &&
884         message != WM_DEVICECHANGE)
885     {
886         trace("parent: %p, %04x, %08lx, %08lx\n", hWnd, message, wParam, lParam);
887         add_message(sequences, PARENT_SEQ_INDEX, &msg);
888     }
889
890     switch(message) {
891     case WM_NOTIFY:
892     {
893         NMHDR *pHdr = (NMHDR *)lParam;
894     
895         ok(pHdr->code != NM_TOOLTIPSCREATED, "Treeview should not send NM_TOOLTIPSCREATED\n");
896         if (pHdr->idFrom == 100)
897         {
898             NMTREEVIEWA *pTreeView = (LPNMTREEVIEWA) lParam;
899             switch(pHdr->code)
900             {
901             case TVN_SELCHANGINGA:
902                 AddItem('(');
903                 IdentifyItem(pTreeView->itemOld.hItem);
904                 IdentifyItem(pTreeView->itemNew.hItem);
905                 break;
906             case TVN_SELCHANGEDA:
907                 AddItem(')');
908                 IdentifyItem(pTreeView->itemOld.hItem);
909                 IdentifyItem(pTreeView->itemNew.hItem);
910                 break;
911             case TVN_GETDISPINFOA: {
912                 NMTVDISPINFOA *disp = (NMTVDISPINFOA *)lParam;
913                 if (disp->item.mask & TVIF_TEXT) {
914                     lstrcpyn(disp->item.pszText, TEST_CALLBACK_TEXT, disp->item.cchTextMax);
915                 }
916                 break;
917               }
918             case TVN_ENDLABELEDIT: return TRUE;
919             case TVN_ITEMEXPANDINGA:
920                 ok(pTreeView->itemNew.mask ==
921                    (TVIF_HANDLE | TVIF_SELECTEDIMAGE | TVIF_IMAGE | TVIF_PARAM | TVIF_STATE),
922                    "got wrong mask %x\n", pTreeView->itemNew.mask);
923                 ok((pTreeView->itemNew.state & TVIS_EXPANDED) == 0,
924                    "got wrong state %x\n", pTreeView->itemNew.state);
925                 ok(pTreeView->itemOld.mask == 0,
926                    "got wrong mask %x\n", pTreeView->itemOld.mask);
927
928                 if (g_get_from_expand)
929                 {
930                   g_item_expanding.mask = TVIF_STATE;
931                   g_item_expanding.hItem = hRoot;
932                   ret = SendMessageA(pHdr->hwndFrom, TVM_GETITEMA, 0, (LPARAM)&g_item_expanding);
933                   ok(ret == TRUE, "got %lu\n", ret);
934                 }
935                 break;
936             case TVN_ITEMEXPANDEDA:
937                 ok(pTreeView->itemNew.mask & TVIF_STATE, "got wrong mask %x\n", pTreeView->itemNew.mask);
938                 ok(pTreeView->itemNew.state & (TVIS_EXPANDED|TVIS_EXPANDEDONCE),
939                    "got wrong mask %x\n", pTreeView->itemNew.mask);
940                 ok(pTreeView->itemOld.mask == 0,
941                    "got wrong mask %x\n", pTreeView->itemOld.mask);
942
943                 if (g_get_from_expand)
944                 {
945                   g_item_expanded.mask = TVIF_STATE;
946                   g_item_expanded.hItem = hRoot;
947                   ret = SendMessageA(pHdr->hwndFrom, TVM_GETITEMA, 0, (LPARAM)&g_item_expanded);
948                   ok(ret == TRUE, "got %lu\n", ret);
949                 }
950                 if (g_get_rect_in_expand)
951                 {
952                   visibleItem = TreeView_GetNextItem(pHdr->hwndFrom, NULL, TVGN_FIRSTVISIBLE);
953                   ok(pTreeView->itemNew.hItem == visibleItem, "expanded item == first visible item\n");
954                   *(HTREEITEM*)&rect = visibleItem;
955                   ok(SendMessage(pHdr->hwndFrom, TVM_GETITEMRECT, TRUE, (LPARAM)&rect), "Failed to get rect for first visible item.\n");
956                   visibleItem = TreeView_GetNextItem(pHdr->hwndFrom, visibleItem, TVGN_NEXTVISIBLE);
957                   *(HTREEITEM*)&rect = visibleItem;
958                   ok(visibleItem != NULL, "There must be a visible item after the first visisble item.\n");
959                   ok(SendMessage(pHdr->hwndFrom, TVM_GETITEMRECT, TRUE, (LPARAM)&rect), "Failed to get rect for second visible item.\n");
960                 }
961                 break;
962             case TVN_DELETEITEMA:
963             {
964                 struct message item;
965
966                 ok(pTreeView->itemNew.mask == 0, "got wrong mask 0x%x\n", pTreeView->itemNew.mask);
967
968                 ok(pTreeView->itemOld.mask == (TVIF_HANDLE | TVIF_PARAM), "got wrong mask 0x%x\n", pTreeView->itemOld.mask);
969                 ok(pTreeView->itemOld.hItem != NULL, "got %p\n", pTreeView->itemOld.hItem);
970
971                 memset(&item, 0, sizeof(item));
972                 item.lParam = (LPARAM)pTreeView->itemOld.hItem;
973                 add_message(item_sequence, 0, &item);
974
975                 break;
976             }
977             }
978         }
979     }
980
981     case WM_DESTROY:
982         PostQuitMessage(0);
983         break;
984     }
985
986     defwndproc_counter++;
987     ret = DefWindowProcA(hWnd, message, wParam, lParam);
988     defwndproc_counter--;
989
990     return ret;
991 }
992
993 static void test_expandinvisible(void)
994 {
995     static CHAR nodeText[][5] = {"0", "1", "2", "3", "4"};
996     TVINSERTSTRUCTA ins;
997     HTREEITEM node[5];
998     RECT dummyRect;
999     BOOL nodeVisible;
1000     LRESULT ret;
1001     HWND hTree;
1002
1003     hTree = create_treeview_control();
1004
1005     /* The test builds the following tree and expands then node 1, while node 0 is collapsed.
1006      *
1007      * 0
1008      * |- 1
1009      * |  |- 2
1010      * |  |- 3
1011      * |- 4
1012      *
1013      */
1014
1015     ret = TreeView_DeleteAllItems(hTree);
1016     ok(ret == TRUE, "ret\n");
1017     ins.hParent = TVI_ROOT;
1018     ins.hInsertAfter = TVI_ROOT;
1019     U(ins).item.mask = TVIF_TEXT;
1020     U(ins).item.pszText = nodeText[0];
1021     node[0] = TreeView_InsertItem(hTree, &ins);
1022     assert(node[0]);
1023
1024     ins.hInsertAfter = TVI_LAST;
1025     U(ins).item.mask = TVIF_TEXT;
1026     ins.hParent = node[0];
1027
1028     U(ins).item.pszText = nodeText[1];
1029     node[1] = TreeView_InsertItem(hTree, &ins);
1030     assert(node[1]);
1031     U(ins).item.pszText = nodeText[4];
1032     node[4] = TreeView_InsertItem(hTree, &ins);
1033     assert(node[4]);
1034
1035     ins.hParent = node[1];
1036
1037     U(ins).item.pszText = nodeText[2];
1038     node[2] = TreeView_InsertItem(hTree, &ins);
1039     assert(node[2]);
1040     U(ins).item.pszText = nodeText[3];
1041     node[3] = TreeView_InsertItem(hTree, &ins);
1042     assert(node[3]);
1043
1044
1045     nodeVisible = TreeView_GetItemRect(hTree, node[1], &dummyRect, FALSE);
1046     ok(!nodeVisible, "Node 1 should not be visible.\n");
1047     nodeVisible = TreeView_GetItemRect(hTree, node[2], &dummyRect, FALSE);
1048     ok(!nodeVisible, "Node 2 should not be visible.\n");
1049     nodeVisible = TreeView_GetItemRect(hTree, node[3], &dummyRect, FALSE);
1050     ok(!nodeVisible, "Node 3 should not be visible.\n");
1051     nodeVisible = TreeView_GetItemRect(hTree, node[4], &dummyRect, FALSE);
1052     ok(!nodeVisible, "Node 4 should not be visible.\n");
1053
1054     ok(TreeView_Expand(hTree, node[1], TVE_EXPAND), "Expand of node 1 failed.\n");
1055
1056     nodeVisible = TreeView_GetItemRect(hTree, node[1], &dummyRect, FALSE);
1057     ok(!nodeVisible, "Node 1 should not be visible.\n");
1058     nodeVisible = TreeView_GetItemRect(hTree, node[2], &dummyRect, FALSE);
1059     ok(!nodeVisible, "Node 2 should not be visible.\n");
1060     nodeVisible = TreeView_GetItemRect(hTree, node[3], &dummyRect, FALSE);
1061     ok(!nodeVisible, "Node 3 should not be visible.\n");
1062     nodeVisible = TreeView_GetItemRect(hTree, node[4], &dummyRect, FALSE);
1063     ok(!nodeVisible, "Node 4 should not be visible.\n");
1064
1065     DestroyWindow(hTree);
1066 }
1067
1068 static void test_itemedit(void)
1069 {
1070     DWORD r;
1071     HWND edit;
1072     TVITEMA item;
1073     CHAR buff[2];
1074     HWND hTree;
1075
1076     hTree = create_treeview_control();
1077     fill_tree(hTree);
1078
1079     /* try with null item */
1080     edit = (HWND)SendMessage(hTree, TVM_EDITLABEL, 0, 0);
1081     ok(!IsWindow(edit), "Expected valid handle\n");
1082
1083     /* trigger edit */
1084     edit = (HWND)SendMessage(hTree, TVM_EDITLABEL, 0, (LPARAM)hRoot);
1085     ok(IsWindow(edit), "Expected valid handle\n");
1086     /* item shouldn't be selected automatically after TVM_EDITLABEL */
1087     r = SendMessage(hTree, TVM_GETITEMSTATE, (WPARAM)hRoot, TVIS_SELECTED);
1088     expect(0, r);
1089     /* try to cancel with wrong edit handle */
1090     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), 0);
1091     expect(0, r);
1092     ok(IsWindow(edit), "Expected edit control to be valid\n");
1093     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1094     expect(0, r);
1095     ok(!IsWindow(edit), "Expected edit control to be destroyed\n");
1096     /* try to cancel without creating edit */
1097     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), 0);
1098     expect(0, r);
1099
1100     /* try to cancel with wrong (not null) handle */
1101     edit = (HWND)SendMessage(hTree, TVM_EDITLABEL, 0, (LPARAM)hRoot);
1102     ok(IsWindow(edit), "Expected valid handle\n");
1103     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)hTree);
1104     expect(0, r);
1105     ok(IsWindow(edit), "Expected edit control to be valid\n");
1106     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1107     expect(0, r);
1108
1109     /* remove selection after starting edit */
1110     r = TreeView_SelectItem(hTree, hRoot);
1111     expect(TRUE, r);
1112     edit = (HWND)SendMessage(hTree, TVM_EDITLABEL, 0, (LPARAM)hRoot);
1113     ok(IsWindow(edit), "Expected valid handle\n");
1114     r = TreeView_SelectItem(hTree, NULL);
1115     expect(TRUE, r);
1116     /* alter text */
1117     strncpy(buff, "x", sizeof(buff)/sizeof(CHAR));
1118     r = SendMessage(edit, WM_SETTEXT, 0, (LPARAM)buff);
1119     expect(TRUE, r);
1120     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1121     expect(0, r);
1122     ok(!IsWindow(edit), "Expected edit control to be destroyed\n");
1123     /* check that text is saved */
1124     item.mask = TVIF_TEXT;
1125     item.hItem = hRoot;
1126     item.pszText = buff;
1127     item.cchTextMax = sizeof(buff)/sizeof(CHAR);
1128     r = SendMessage(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1129     expect(TRUE, r);
1130     ok(!strcmp("x", buff), "Expected item text to change\n");
1131
1132     DestroyWindow(hTree);
1133 }
1134
1135 static void test_treeview_classinfo(void)
1136 {
1137     WNDCLASSA cls;
1138
1139     memset(&cls, 0, sizeof(cls));
1140     GetClassInfo(GetModuleHandleA("comctl32.dll"), WC_TREEVIEWA, &cls);
1141     ok(cls.hbrBackground == NULL, "Expected NULL background brush, got %p\n", cls.hbrBackground);
1142     ok(cls.style == (CS_GLOBALCLASS | CS_DBLCLKS), "Expected got %x\n", cls.style);
1143     expect(0, cls.cbClsExtra);
1144 }
1145
1146 static void test_get_linecolor(void)
1147 {
1148     COLORREF clr;
1149     HWND hTree;
1150
1151     hTree = create_treeview_control();
1152
1153     /* newly created control has default color */
1154     clr = (COLORREF)SendMessage(hTree, TVM_GETLINECOLOR, 0, 0);
1155     if (clr == 0)
1156         win_skip("TVM_GETLINECOLOR is not supported on comctl32 < 5.80\n");
1157     else
1158         expect(CLR_DEFAULT, clr);
1159
1160     DestroyWindow(hTree);
1161 }
1162
1163 static void test_get_insertmarkcolor(void)
1164 {
1165     COLORREF clr;
1166     HWND hTree;
1167
1168     hTree = create_treeview_control();
1169
1170     /* newly created control has default color */
1171     clr = (COLORREF)SendMessage(hTree, TVM_GETINSERTMARKCOLOR, 0, 0);
1172     if (clr == 0)
1173         win_skip("TVM_GETINSERTMARKCOLOR is not supported on comctl32 < 5.80\n");
1174     else
1175         expect(CLR_DEFAULT, clr);
1176
1177     DestroyWindow(hTree);
1178 }
1179
1180 static void test_expandnotify(void)
1181 {
1182     HWND hTree;
1183     BOOL ret;
1184     TVITEMA item;
1185
1186     hTree = create_treeview_control();
1187     fill_tree(hTree);
1188
1189     item.hItem = hRoot;
1190     item.mask = TVIF_STATE;
1191
1192     item.state = TVIS_EXPANDED;
1193     ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1194     ok(ret == TRUE, "got %d\n", ret);
1195     ok((item.state & TVIS_EXPANDED) == 0, "expected collapsed\n");
1196
1197     /* preselect root node here */
1198     ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
1199     ok(ret == TRUE, "got %d\n", ret);
1200
1201     g_get_from_expand = TRUE;
1202     /* expand */
1203     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1204     g_item_expanding.state = 0xdeadbeef;
1205     g_item_expanded.state = 0xdeadbeef;
1206     ret = SendMessageA(hTree, TVM_EXPAND, TVE_EXPAND, (LPARAM)hRoot);
1207     ok(ret == TRUE, "got %d\n", ret);
1208     ok(g_item_expanding.state == TVIS_SELECTED, "got state on TVN_ITEMEXPANDING 0x%08x\n",
1209        g_item_expanding.state);
1210     ok(g_item_expanded.state == (TVIS_SELECTED|TVIS_EXPANDED), "got state on TVN_ITEMEXPANDED 0x%08x\n",
1211        g_item_expanded.state);
1212     ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_seq, "expand notifications", FALSE);
1213     g_get_from_expand = FALSE;
1214
1215     /* check that it's expanded */
1216     item.state = TVIS_EXPANDED;
1217     ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1218     ok(ret == TRUE, "got %d\n", ret);
1219     ok((item.state & TVIS_EXPANDED) == TVIS_EXPANDED, "expected expanded\n");
1220
1221     /* collapse */
1222     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1223     ret = SendMessageA(hTree, TVM_EXPAND, TVE_COLLAPSE, (LPARAM)hRoot);
1224     ok(ret == TRUE, "got %d\n", ret);
1225     item.state = TVIS_EXPANDED;
1226     ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1227     ok(ret == TRUE, "got %d\n", ret);
1228     ok((item.state & TVIS_EXPANDED) == 0, "expected collapsed\n");
1229     /* all next collapse/expand attempts won't produce any notifications,
1230        the only way is to reset with all children removed */
1231     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "collapse after expand notifications", FALSE);
1232
1233     DestroyWindow(hTree);
1234
1235     /* test TVM_GETITEMRECT inside TVN_ITEMEXPANDED notification */
1236     hTree = create_treeview_control();
1237     fill_tree(hTree);
1238     g_get_rect_in_expand = TRUE;
1239     ret = TreeView_Select(hTree, hChild, TVGN_CARET);
1240     g_get_rect_in_expand = FALSE;
1241     ok(ret, "got %d\n", ret);
1242     DestroyWindow(hTree);
1243 }
1244
1245 static void test_expandedimage(void)
1246 {
1247     TVITEMEXA item;
1248     HWND hTree;
1249     BOOL ret;
1250
1251     hTree = create_treeview_control();
1252     fill_tree(hTree);
1253
1254     item.mask = TVIF_EXPANDEDIMAGE;
1255     item.iExpandedImage = 1;
1256     item.hItem = hRoot;
1257     ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
1258     ok(ret, "got %d\n", ret);
1259
1260     item.mask = TVIF_EXPANDEDIMAGE;
1261     item.iExpandedImage = -1;
1262     item.hItem = hRoot;
1263     ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1264     ok(ret, "got %d\n", ret);
1265
1266     if (item.iExpandedImage != 1)
1267     {
1268         win_skip("TVIF_EXPANDEDIMAGE not supported\n");
1269         DestroyWindow(hTree);
1270         return;
1271     }
1272
1273     /* test for default iExpandedImage value */
1274     item.mask = TVIF_EXPANDEDIMAGE;
1275     item.iExpandedImage = -1;
1276     item.hItem = hChild;
1277     ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1278     ok(ret, "got %d\n", ret);
1279     ok(item.iExpandedImage == (WORD)I_IMAGENONE, "got %d\n", item.iExpandedImage);
1280
1281     DestroyWindow(hTree);
1282 }
1283
1284 static void test_TVS_SINGLEEXPAND(void)
1285 {
1286     HWND hTree;
1287     BOOL ret;
1288
1289     hTree = create_treeview_control();
1290     SetWindowLongA(hTree, GWL_STYLE, GetWindowLong(hTree, GWL_STYLE) | TVS_SINGLEEXPAND);
1291     /* to avoid paiting related notifications */
1292     ShowWindow(hTree, SW_HIDE);
1293     fill_tree(hTree);
1294
1295     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1296     ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
1297     ok(ret, "got %d\n", ret);
1298     ok_sequence(sequences, PARENT_SEQ_INDEX, parent_singleexpand_seq, "singleexpand notifications", FALSE);
1299
1300     /* a workaround for NT4 that sends expanding notification when nothing is about to expand */
1301     ret = SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)hRoot);
1302     ok(ret, "got %d\n", ret);
1303     fill_tree(hTree);
1304     ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, 0);
1305     ok(ret, "got %d\n", ret);
1306
1307     DestroyWindow(hTree);
1308 }
1309
1310 static void test_WM_PAINT(void)
1311 {
1312     HWND hTree;
1313     COLORREF clr;
1314     LONG ret;
1315     RECT rc;
1316     HDC hdc;
1317
1318     hTree = create_treeview_control();
1319
1320     clr = SendMessageA(hTree, TVM_SETBKCOLOR, 0, RGB(255, 0, 0));
1321     ok(clr == -1, "got %d, expected -1\n", clr);
1322
1323     hdc = GetDC(hMainWnd);
1324
1325     GetClientRect(hMainWnd, &rc);
1326     FillRect(hdc, &rc, GetStockObject(BLACK_BRUSH));
1327
1328     clr = GetPixel(hdc, 1, 1);
1329     ok(clr == RGB(0, 0, 0), "got 0x%x\n", clr);
1330
1331     ret = SendMessageA(hTree, WM_PAINT, (WPARAM)hdc, 0);
1332     ok(ret == 0, "got %d\n", ret);
1333
1334     clr = GetPixel(hdc, 1, 1);
1335     ok(clr == RGB(255, 0, 0) || broken(clr == RGB(0, 0, 0)) /* win98 */,
1336         "got 0x%x\n", clr);
1337
1338     ReleaseDC(hMainWnd, hdc);
1339
1340     DestroyWindow(hTree);
1341 }
1342
1343 static void test_delete_items(void)
1344 {
1345     const struct message *msg;
1346     HWND hTree;
1347     INT ret;
1348
1349     hTree = create_treeview_control();
1350     fill_tree(hTree);
1351
1352     /* check delete order */
1353     flush_sequences(item_sequence, 1);
1354     ret = SendMessage(hTree, TVM_DELETEITEM, 0, 0);
1355     ok(ret == TRUE, "got %d\n", ret);
1356
1357     msg = item_sequence[0]->sequence;
1358     ok(item_sequence[0]->count == 2, "expected 2 items, got %d\n", item_sequence[0]->count);
1359
1360     if (item_sequence[0]->count == 2)
1361     {
1362       ok(msg[0].lParam == (LPARAM)hChild, "expected %p, got 0x%lx\n", hChild, msg[0].lParam);
1363       ok(msg[1].lParam == (LPARAM)hRoot, "expected %p, got 0x%lx\n", hRoot, msg[1].lParam);
1364     }
1365
1366     ret = SendMessageA(hTree, TVM_GETCOUNT, 0, 0);
1367     ok(ret == 0, "got %d\n", ret);
1368
1369     DestroyWindow(hTree);
1370 }
1371
1372 START_TEST(treeview)
1373 {
1374     HMODULE hComctl32;
1375     BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
1376     WNDCLASSA wc;
1377     MSG msg;
1378
1379     ULONG_PTR ctx_cookie;
1380     HANDLE hCtx;
1381     HWND hwnd;
1382   
1383     hComctl32 = GetModuleHandleA("comctl32.dll");
1384     pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
1385     if (pInitCommonControlsEx)
1386     {
1387         INITCOMMONCONTROLSEX iccex;
1388         iccex.dwSize = sizeof(iccex);
1389         iccex.dwICC  = ICC_TREEVIEW_CLASSES;
1390         pInitCommonControlsEx(&iccex);
1391     }
1392     else
1393         InitCommonControls();
1394
1395     init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
1396     init_msg_sequences(item_sequence, 1);
1397   
1398     wc.style = CS_HREDRAW | CS_VREDRAW;
1399     wc.cbClsExtra = 0;
1400     wc.cbWndExtra = 0;
1401     wc.hInstance = GetModuleHandleA(NULL);
1402     wc.hIcon = NULL;
1403     wc.hCursor = LoadCursorA(NULL, IDC_IBEAM);
1404     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1405     wc.lpszMenuName = NULL;
1406     wc.lpszClassName = "MyTestWnd";
1407     wc.lpfnWndProc = parent_wnd_proc;
1408     RegisterClassA(&wc);
1409
1410     hMainWnd = CreateWindowExA(0, "MyTestWnd", "Blah", WS_OVERLAPPEDWINDOW,
1411       CW_USEDEFAULT, CW_USEDEFAULT, 130, 105, NULL, NULL, GetModuleHandleA(NULL), 0);
1412
1413     ok(hMainWnd != NULL, "Failed to create parent window. Tests aborted.\n");
1414     if (!hMainWnd) return;
1415
1416     test_fillroot();
1417     test_select();
1418     test_getitemtext();
1419     test_focus();
1420     test_get_set_bkcolor();
1421     test_get_set_imagelist();
1422     test_get_set_indent();
1423     test_get_set_insertmark();
1424     test_get_set_item();
1425     test_get_set_itemheight();
1426     test_get_set_scrolltime();
1427     test_get_set_textcolor();
1428     test_get_linecolor();
1429     test_get_insertmarkcolor();
1430     test_get_set_tooltips();
1431     test_get_set_unicodeformat();
1432     test_callback();
1433     test_expandinvisible();
1434     test_itemedit();
1435     test_treeview_classinfo();
1436     test_expandnotify();
1437     test_TVS_SINGLEEXPAND();
1438     test_WM_PAINT();
1439     test_delete_items();
1440
1441     if (!load_v6_module(&ctx_cookie, &hCtx))
1442     {
1443         DestroyWindow(hMainWnd);
1444         return;
1445     }
1446
1447     /* this is a XP SP3 failure workaround */
1448     hwnd = CreateWindowExA(0, WC_TREEVIEW, "foo",
1449                            WS_CHILD | WS_BORDER | WS_VISIBLE,
1450                            0, 0, 100, 100,
1451                            hMainWnd, NULL, GetModuleHandleA(NULL), NULL);
1452     if (!IsWindow(hwnd))
1453     {
1454         win_skip("FIXME: failed to create TreeView window.\n");
1455         unload_v6_module(ctx_cookie, hCtx);
1456         DestroyWindow(hMainWnd);
1457         return;
1458     }
1459     else
1460         DestroyWindow(hwnd);
1461
1462     /* comctl32 version 6 tests start here */
1463     test_expandedimage();
1464
1465     unload_v6_module(ctx_cookie, hCtx);
1466
1467     PostMessageA(hMainWnd, WM_CLOSE, 0, 0);
1468     while(GetMessageA(&msg, 0, 0, 0))
1469     {
1470         TranslateMessage(&msg);
1471         DispatchMessageA(&msg);
1472     }
1473 }