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