wined3d: IWineD3DBuffer_Unmap() can't fail.
[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
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_INSERTITEMA, sent },
74     { TVM_GETITEMA, 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_GETITEMA, sent },
138     { TVM_SETITEMA, sent },
139     { TVM_GETITEMA, sent },
140     { TVM_SETITEMA, 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_ITEMEXPANDINGA },
191     { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDEDA },
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     TVITEMA 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_GETITEMA, 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     TVITEMA 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     TVITEMA 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_GETITEMA, 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     TVITEMA 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_GETITEMA, 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_SETITEMA, 0, (LPARAM)&tviRoot );
669     memset(szBuffer, 0, nBufferSize);
670     SendMessage( hTree, TVM_GETITEMA, 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_SETITEMA, 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     /* without TVS_NONEVENHEIGHT */
717     SetWindowLong(hTree, GWL_STYLE, GetWindowLong(hTree, GWL_STYLE) & ~TVS_NONEVENHEIGHT);
718     /* odd value */
719     ulOldHeight = SendMessage( hTree, TVM_SETITEMHEIGHT, 3, 0);
720     ok(ulOldHeight == 8, "got %d, expected %d\n", ulOldHeight, 8);
721     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
722     ok(ulNewHeight == 2, "got %d, expected %d\n", ulNewHeight, 2);
723
724     ulOldHeight = SendMessage( hTree, TVM_SETITEMHEIGHT, 4, 0);
725     ok(ulOldHeight == 2, "got %d, expected %d\n", ulOldHeight, 2);
726     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
727     ok(ulNewHeight == 4, "got %d, expected %d\n", ulNewHeight, 4);
728
729     /* with TVS_NONEVENHEIGHT */
730     SetWindowLong(hTree, GWL_STYLE, GetWindowLong(hTree, GWL_STYLE) | TVS_NONEVENHEIGHT);
731     /* odd value */
732     ulOldHeight = SendMessage( hTree, TVM_SETITEMHEIGHT, 3, 0);
733     ok(ulOldHeight == 4, "got %d, expected %d\n", ulOldHeight, 4);
734     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
735     ok(ulNewHeight == 3, "got %d, expected %d\n", ulNewHeight, 3);
736     /* even value */
737     ulOldHeight = SendMessage( hTree, TVM_SETITEMHEIGHT, 10, 0);
738     ok(ulOldHeight == 3, "got %d, expected %d\n", ulOldHeight, 3);
739     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
740     ok(ulNewHeight == 10, "got %d, expected %d\n", ulNewHeight, 10);
741
742     DestroyWindow(hTree);
743 }
744
745 static void test_get_set_scrolltime(void)
746 {
747     int ulExpectedTime = 20;
748     int ulTime = 0;
749     HWND hTree;
750
751     hTree = create_treeview_control();
752     fill_tree(hTree);
753
754     flush_sequences(sequences, NUM_MSG_SEQUENCES);
755
756     SendMessage( hTree, TVM_SETSCROLLTIME, ulExpectedTime, 0 );
757     ulTime = (int)SendMessage( hTree, TVM_GETSCROLLTIME, 0, 0 );
758     ok(ulTime == ulExpectedTime, "Scroll time reported as %d, expected %d\n", ulTime, ulExpectedTime);
759
760     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_scrolltime_seq,
761         "test get set scroll time", FALSE);
762
763     DestroyWindow(hTree);
764 }
765
766 static void test_get_set_textcolor(void)
767 {
768     /* If the value is -1, the control is using the system color for the text color. */
769     COLORREF crColor = RGB(0,0,0);
770     HWND hTree;
771
772     hTree = create_treeview_control();
773     fill_tree(hTree);
774
775     flush_sequences(sequences, NUM_MSG_SEQUENCES);
776
777     crColor = (COLORREF)SendMessage( hTree, TVM_GETTEXTCOLOR, 0, 0 );
778     ok(crColor == -1, "Default text color reported as 0x%.8x\n", crColor);
779
780     /* Test for black text */
781     SendMessage( hTree, TVM_SETTEXTCOLOR, 0, RGB(0,0,0) );
782     crColor = (COLORREF)SendMessage( hTree, TVM_GETTEXTCOLOR, 0, 0 );
783     ok(crColor == RGB(0,0,0), "Black text color reported as 0x%.8x\n", crColor);
784
785     /* Test for white text */
786     SendMessage( hTree, TVM_SETTEXTCOLOR, 0, RGB(255,255,255) );
787     crColor = (COLORREF)SendMessage( hTree, TVM_GETTEXTCOLOR, 0, 0 );
788     ok(crColor == RGB(255,255,255), "White text color reported as 0x%.8x\n", crColor);
789
790     /* Reset the default text color */
791     SendMessage( hTree, TVM_SETTEXTCOLOR, 0, CLR_NONE );
792
793     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_textcolor_seq,
794         "test get set text color", FALSE);
795
796     DestroyWindow(hTree);
797 }
798
799 static void test_get_set_tooltips(void)
800 {
801     HWND hwndLastToolTip = NULL;
802     HWND hPopupTreeView;
803     HWND hTree;
804
805     hTree = create_treeview_control();
806     fill_tree(hTree);
807
808     flush_sequences(sequences, NUM_MSG_SEQUENCES);
809
810     /* show even WS_POPUP treeview don't send NM_TOOLTIPSCREATED */
811     hPopupTreeView = CreateWindow(WC_TREEVIEW, NULL, WS_POPUP|WS_VISIBLE, 0, 0, 100, 100, hMainWnd, NULL, NULL, NULL);
812     DestroyWindow(hPopupTreeView);
813
814     /* Testing setting a NULL ToolTip */
815     SendMessage( hTree, TVM_SETTOOLTIPS, 0, 0 );
816     hwndLastToolTip = (HWND)SendMessage( hTree, TVM_GETTOOLTIPS, 0, 0 );
817     ok(hwndLastToolTip == NULL, "NULL tool tip, reported as 0x%p, expected 0.\n", hwndLastToolTip);
818
819     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_tooltips_seq,
820         "test get set tooltips", TRUE);
821
822     /* TODO: Add a test of an actual tooltip */
823     DestroyWindow(hTree);
824 }
825
826 static void test_get_set_unicodeformat(void)
827 {
828     BOOL bPreviousSetting = 0;
829     BOOL bNewSetting = 0;
830     HWND hTree;
831
832     hTree = create_treeview_control();
833     fill_tree(hTree);
834
835     flush_sequences(sequences, NUM_MSG_SEQUENCES);
836
837     /* Set to Unicode */
838     bPreviousSetting = (BOOL)SendMessage( hTree, TVM_SETUNICODEFORMAT, 1, 0 );
839     bNewSetting = (BOOL)SendMessage( hTree, TVM_GETUNICODEFORMAT, 0, 0 );
840     ok(bNewSetting == 1, "Unicode setting did not work.\n");
841
842     /* Set to ANSI */
843     SendMessage( hTree, TVM_SETUNICODEFORMAT, 0, 0 );
844     bNewSetting = (BOOL)SendMessage( hTree, TVM_GETUNICODEFORMAT, 0, 0 );
845     ok(bNewSetting == 0, "ANSI setting did not work.\n");
846
847     /* Revert to original setting */
848     SendMessage( hTree, TVM_SETUNICODEFORMAT, bPreviousSetting, 0 );
849
850     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_unicodeformat_seq,
851         "test get set unicode format", FALSE);
852
853     DestroyWindow(hTree);
854 }
855
856 static TVITEMA g_item_expanding, g_item_expanded;
857 static BOOL g_get_from_expand;
858 static BOOL g_get_rect_in_expand;
859
860 static LRESULT CALLBACK parent_wnd_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
861 {
862     static LONG defwndproc_counter = 0;
863     struct message msg;
864     LRESULT ret;
865     RECT rect;
866     HTREEITEM visibleItem;
867
868     msg.message = message;
869     msg.flags = sent|wparam|lparam;
870     if (defwndproc_counter) msg.flags |= defwinproc;
871     msg.wParam = wParam;
872     msg.lParam = lParam;
873     if (message == WM_NOTIFY && lParam) msg.id = ((NMHDR*)lParam)->code;
874
875     /* log system messages, except for painting */
876     if (message < WM_USER &&
877         message != WM_PAINT &&
878         message != WM_ERASEBKGND &&
879         message != WM_NCPAINT &&
880         message != WM_NCHITTEST &&
881         message != WM_GETTEXT &&
882         message != WM_GETICON &&
883         message != WM_DEVICECHANGE)
884     {
885         trace("parent: %p, %04x, %08lx, %08lx\n", hWnd, message, wParam, lParam);
886         add_message(sequences, PARENT_SEQ_INDEX, &msg);
887     }
888
889     switch(message) {
890     case WM_NOTIFY:
891     {
892         NMHDR *pHdr = (NMHDR *)lParam;
893     
894         ok(pHdr->code != NM_TOOLTIPSCREATED, "Treeview should not send NM_TOOLTIPSCREATED\n");
895         if (pHdr->idFrom == 100)
896         {
897             NMTREEVIEWA *pTreeView = (LPNMTREEVIEWA) lParam;
898             switch(pHdr->code)
899             {
900             case TVN_SELCHANGINGA:
901                 AddItem('(');
902                 IdentifyItem(pTreeView->itemOld.hItem);
903                 IdentifyItem(pTreeView->itemNew.hItem);
904                 break;
905             case TVN_SELCHANGEDA:
906                 AddItem(')');
907                 IdentifyItem(pTreeView->itemOld.hItem);
908                 IdentifyItem(pTreeView->itemNew.hItem);
909                 break;
910             case TVN_GETDISPINFOA: {
911                 NMTVDISPINFOA *disp = (NMTVDISPINFOA *)lParam;
912                 if (disp->item.mask & TVIF_TEXT) {
913                     lstrcpyn(disp->item.pszText, TEST_CALLBACK_TEXT, disp->item.cchTextMax);
914                 }
915                 break;
916               }
917             case TVN_ENDLABELEDIT: return TRUE;
918             case TVN_ITEMEXPANDINGA:
919                 ok(pTreeView->itemNew.mask ==
920                    (TVIF_HANDLE | TVIF_SELECTEDIMAGE | TVIF_IMAGE | TVIF_PARAM | TVIF_STATE),
921                    "got wrong mask %x\n", pTreeView->itemNew.mask);
922                 ok((pTreeView->itemNew.state & TVIS_EXPANDED) == 0,
923                    "got wrong state %x\n", pTreeView->itemNew.state);
924                 ok(pTreeView->itemOld.mask == 0,
925                    "got wrong mask %x\n", pTreeView->itemOld.mask);
926
927                 if (g_get_from_expand)
928                 {
929                   g_item_expanding.mask = TVIF_STATE;
930                   g_item_expanding.hItem = hRoot;
931                   ret = SendMessageA(pHdr->hwndFrom, TVM_GETITEMA, 0, (LPARAM)&g_item_expanding);
932                   ok(ret == TRUE, "got %lu\n", ret);
933                 }
934                 break;
935             case TVN_ITEMEXPANDEDA:
936                 ok(pTreeView->itemNew.mask & TVIF_STATE, "got wrong mask %x\n", pTreeView->itemNew.mask);
937                 ok(pTreeView->itemNew.state & (TVIS_EXPANDED|TVIS_EXPANDEDONCE),
938                    "got wrong mask %x\n", pTreeView->itemNew.mask);
939                 ok(pTreeView->itemOld.mask == 0,
940                    "got wrong mask %x\n", pTreeView->itemOld.mask);
941
942                 if (g_get_from_expand)
943                 {
944                   g_item_expanded.mask = TVIF_STATE;
945                   g_item_expanded.hItem = hRoot;
946                   ret = SendMessageA(pHdr->hwndFrom, TVM_GETITEMA, 0, (LPARAM)&g_item_expanded);
947                   ok(ret == TRUE, "got %lu\n", ret);
948                 }
949                 if (g_get_rect_in_expand)
950                 {
951                   visibleItem = TreeView_GetNextItem(pHdr->hwndFrom, NULL, TVGN_FIRSTVISIBLE);
952                   ok(pTreeView->itemNew.hItem == visibleItem, "expanded item == first visible item\n");
953                   *(HTREEITEM*)&rect = visibleItem;
954                   ok(SendMessage(pHdr->hwndFrom, TVM_GETITEMRECT, TRUE, (LPARAM)&rect), "Failed to get rect for first visible item.\n");
955                   visibleItem = TreeView_GetNextItem(pHdr->hwndFrom, visibleItem, TVGN_NEXTVISIBLE);
956                   *(HTREEITEM*)&rect = visibleItem;
957                   ok(visibleItem != NULL, "There must be a visible item after the first visisble item.\n");
958                   ok(SendMessage(pHdr->hwndFrom, TVM_GETITEMRECT, TRUE, (LPARAM)&rect), "Failed to get rect for second visible item.\n");
959                 }
960                 break;
961             }
962         }
963     }
964
965     case WM_DESTROY:
966         PostQuitMessage(0);
967         break;
968     }
969
970     defwndproc_counter++;
971     ret = DefWindowProcA(hWnd, message, wParam, lParam);
972     defwndproc_counter--;
973
974     return ret;
975 }
976
977 static void test_expandinvisible(void)
978 {
979     static CHAR nodeText[][5] = {"0", "1", "2", "3", "4"};
980     TVINSERTSTRUCTA ins;
981     HTREEITEM node[5];
982     RECT dummyRect;
983     BOOL nodeVisible;
984     LRESULT ret;
985     HWND hTree;
986
987     hTree = create_treeview_control();
988
989     /* The test builds the following tree and expands then node 1, while node 0 is collapsed.
990      *
991      * 0
992      * |- 1
993      * |  |- 2
994      * |  |- 3
995      * |- 4
996      *
997      */
998
999     ret = TreeView_DeleteAllItems(hTree);
1000     ok(ret == TRUE, "ret\n");
1001     ins.hParent = TVI_ROOT;
1002     ins.hInsertAfter = TVI_ROOT;
1003     U(ins).item.mask = TVIF_TEXT;
1004     U(ins).item.pszText = nodeText[0];
1005     node[0] = TreeView_InsertItem(hTree, &ins);
1006     assert(node[0]);
1007
1008     ins.hInsertAfter = TVI_LAST;
1009     U(ins).item.mask = TVIF_TEXT;
1010     ins.hParent = node[0];
1011
1012     U(ins).item.pszText = nodeText[1];
1013     node[1] = TreeView_InsertItem(hTree, &ins);
1014     assert(node[1]);
1015     U(ins).item.pszText = nodeText[4];
1016     node[4] = TreeView_InsertItem(hTree, &ins);
1017     assert(node[4]);
1018
1019     ins.hParent = node[1];
1020
1021     U(ins).item.pszText = nodeText[2];
1022     node[2] = TreeView_InsertItem(hTree, &ins);
1023     assert(node[2]);
1024     U(ins).item.pszText = nodeText[3];
1025     node[3] = TreeView_InsertItem(hTree, &ins);
1026     assert(node[3]);
1027
1028
1029     nodeVisible = TreeView_GetItemRect(hTree, node[1], &dummyRect, FALSE);
1030     ok(!nodeVisible, "Node 1 should not be visible.\n");
1031     nodeVisible = TreeView_GetItemRect(hTree, node[2], &dummyRect, FALSE);
1032     ok(!nodeVisible, "Node 2 should not be visible.\n");
1033     nodeVisible = TreeView_GetItemRect(hTree, node[3], &dummyRect, FALSE);
1034     ok(!nodeVisible, "Node 3 should not be visible.\n");
1035     nodeVisible = TreeView_GetItemRect(hTree, node[4], &dummyRect, FALSE);
1036     ok(!nodeVisible, "Node 4 should not be visible.\n");
1037
1038     ok(TreeView_Expand(hTree, node[1], TVE_EXPAND), "Expand of node 1 failed.\n");
1039
1040     nodeVisible = TreeView_GetItemRect(hTree, node[1], &dummyRect, FALSE);
1041     ok(!nodeVisible, "Node 1 should not be visible.\n");
1042     nodeVisible = TreeView_GetItemRect(hTree, node[2], &dummyRect, FALSE);
1043     ok(!nodeVisible, "Node 2 should not be visible.\n");
1044     nodeVisible = TreeView_GetItemRect(hTree, node[3], &dummyRect, FALSE);
1045     ok(!nodeVisible, "Node 3 should not be visible.\n");
1046     nodeVisible = TreeView_GetItemRect(hTree, node[4], &dummyRect, FALSE);
1047     ok(!nodeVisible, "Node 4 should not be visible.\n");
1048
1049     DestroyWindow(hTree);
1050 }
1051
1052 static void test_itemedit(void)
1053 {
1054     DWORD r;
1055     HWND edit;
1056     TVITEMA item;
1057     CHAR buff[2];
1058     HWND hTree;
1059
1060     hTree = create_treeview_control();
1061     fill_tree(hTree);
1062
1063     /* try with null item */
1064     edit = (HWND)SendMessage(hTree, TVM_EDITLABEL, 0, 0);
1065     ok(!IsWindow(edit), "Expected valid handle\n");
1066
1067     /* trigger edit */
1068     edit = (HWND)SendMessage(hTree, TVM_EDITLABEL, 0, (LPARAM)hRoot);
1069     ok(IsWindow(edit), "Expected valid handle\n");
1070     /* item shouldn't be selected automatically after TVM_EDITLABEL */
1071     r = SendMessage(hTree, TVM_GETITEMSTATE, (WPARAM)hRoot, TVIS_SELECTED);
1072     expect(0, r);
1073     /* try to cancel with wrong edit handle */
1074     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), 0);
1075     expect(0, r);
1076     ok(IsWindow(edit), "Expected edit control to be valid\n");
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     /* try to cancel without creating edit */
1081     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), 0);
1082     expect(0, r);
1083
1084     /* try to cancel with wrong (not null) handle */
1085     edit = (HWND)SendMessage(hTree, TVM_EDITLABEL, 0, (LPARAM)hRoot);
1086     ok(IsWindow(edit), "Expected valid handle\n");
1087     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)hTree);
1088     expect(0, r);
1089     ok(IsWindow(edit), "Expected edit control to be valid\n");
1090     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1091     expect(0, r);
1092
1093     /* remove selection after starting edit */
1094     r = TreeView_SelectItem(hTree, hRoot);
1095     expect(TRUE, r);
1096     edit = (HWND)SendMessage(hTree, TVM_EDITLABEL, 0, (LPARAM)hRoot);
1097     ok(IsWindow(edit), "Expected valid handle\n");
1098     r = TreeView_SelectItem(hTree, NULL);
1099     expect(TRUE, r);
1100     /* alter text */
1101     strncpy(buff, "x", sizeof(buff)/sizeof(CHAR));
1102     r = SendMessage(edit, WM_SETTEXT, 0, (LPARAM)buff);
1103     expect(TRUE, r);
1104     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1105     expect(0, r);
1106     ok(!IsWindow(edit), "Expected edit control to be destroyed\n");
1107     /* check that text is saved */
1108     item.mask = TVIF_TEXT;
1109     item.hItem = hRoot;
1110     item.pszText = buff;
1111     item.cchTextMax = sizeof(buff)/sizeof(CHAR);
1112     r = SendMessage(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1113     expect(TRUE, r);
1114     ok(!strcmp("x", buff), "Expected item text to change\n");
1115
1116     DestroyWindow(hTree);
1117 }
1118
1119 static void test_treeview_classinfo(void)
1120 {
1121     WNDCLASSA cls;
1122
1123     memset(&cls, 0, sizeof(cls));
1124     GetClassInfo(GetModuleHandleA("comctl32.dll"), WC_TREEVIEWA, &cls);
1125     ok(cls.hbrBackground == NULL, "Expected NULL background brush, got %p\n", cls.hbrBackground);
1126     ok(cls.style == (CS_GLOBALCLASS | CS_DBLCLKS), "Expected got %x\n", cls.style);
1127     expect(0, cls.cbClsExtra);
1128 }
1129
1130 static void test_get_linecolor(void)
1131 {
1132     COLORREF clr;
1133     HWND hTree;
1134
1135     hTree = create_treeview_control();
1136
1137     /* newly created control has default color */
1138     clr = (COLORREF)SendMessage(hTree, TVM_GETLINECOLOR, 0, 0);
1139     if (clr == 0)
1140         win_skip("TVM_GETLINECOLOR is not supported on comctl32 < 5.80\n");
1141     else
1142         expect(CLR_DEFAULT, clr);
1143
1144     DestroyWindow(hTree);
1145 }
1146
1147 static void test_get_insertmarkcolor(void)
1148 {
1149     COLORREF clr;
1150     HWND hTree;
1151
1152     hTree = create_treeview_control();
1153
1154     /* newly created control has default color */
1155     clr = (COLORREF)SendMessage(hTree, TVM_GETINSERTMARKCOLOR, 0, 0);
1156     if (clr == 0)
1157         win_skip("TVM_GETINSERTMARKCOLOR is not supported on comctl32 < 5.80\n");
1158     else
1159         expect(CLR_DEFAULT, clr);
1160
1161     DestroyWindow(hTree);
1162 }
1163
1164 static void test_expandnotify(void)
1165 {
1166     HWND hTree;
1167     BOOL ret;
1168     TVITEMA item;
1169
1170     hTree = create_treeview_control();
1171     fill_tree(hTree);
1172
1173     item.hItem = hRoot;
1174     item.mask = TVIF_STATE;
1175
1176     item.state = TVIS_EXPANDED;
1177     ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1178     ok(ret == TRUE, "got %d\n", ret);
1179     ok((item.state & TVIS_EXPANDED) == 0, "expected collapsed\n");
1180
1181     /* preselect root node here */
1182     ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
1183     ok(ret == TRUE, "got %d\n", ret);
1184
1185     g_get_from_expand = TRUE;
1186     /* expand */
1187     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1188     g_item_expanding.state = 0xdeadbeef;
1189     g_item_expanded.state = 0xdeadbeef;
1190     ret = SendMessageA(hTree, TVM_EXPAND, TVE_EXPAND, (LPARAM)hRoot);
1191     ok(ret == TRUE, "got %d\n", ret);
1192     ok(g_item_expanding.state == TVIS_SELECTED, "got state on TVN_ITEMEXPANDING 0x%08x\n",
1193        g_item_expanding.state);
1194     ok(g_item_expanded.state == (TVIS_SELECTED|TVIS_EXPANDED), "got state on TVN_ITEMEXPANDED 0x%08x\n",
1195        g_item_expanded.state);
1196     ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_seq, "expand notifications", FALSE);
1197     g_get_from_expand = FALSE;
1198
1199     /* check that it's expanded */
1200     item.state = TVIS_EXPANDED;
1201     ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1202     ok(ret == TRUE, "got %d\n", ret);
1203     ok((item.state & TVIS_EXPANDED) == TVIS_EXPANDED, "expected expanded\n");
1204
1205     /* collapse */
1206     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1207     ret = SendMessageA(hTree, TVM_EXPAND, TVE_COLLAPSE, (LPARAM)hRoot);
1208     ok(ret == TRUE, "got %d\n", ret);
1209     item.state = TVIS_EXPANDED;
1210     ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1211     ok(ret == TRUE, "got %d\n", ret);
1212     ok((item.state & TVIS_EXPANDED) == 0, "expected collapsed\n");
1213     /* all next collapse/expand attempts won't produce any notifications,
1214        the only way is to reset with all children removed */
1215     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "collapse after expand notifications", FALSE);
1216
1217     DestroyWindow(hTree);
1218
1219     /* test TVM_GETITEMRECT inside TVN_ITEMEXPANDED notification */
1220     hTree = create_treeview_control();
1221     fill_tree(hTree);
1222     g_get_rect_in_expand = TRUE;
1223     ret = TreeView_Select(hTree, hChild, TVGN_CARET);
1224     g_get_rect_in_expand = FALSE;
1225     ok(ret, "got %d\n", ret);
1226     DestroyWindow(hTree);
1227 }
1228
1229 static void test_expandedimage(void)
1230 {
1231     TVITEMEXA item;
1232     HWND hTree;
1233     BOOL ret;
1234
1235     hTree = create_treeview_control();
1236     fill_tree(hTree);
1237
1238     item.mask = TVIF_EXPANDEDIMAGE;
1239     item.iExpandedImage = 1;
1240     item.hItem = hRoot;
1241     ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
1242     ok(ret, "got %d\n", ret);
1243
1244     item.mask = TVIF_EXPANDEDIMAGE;
1245     item.iExpandedImage = -1;
1246     item.hItem = hRoot;
1247     ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1248     ok(ret, "got %d\n", ret);
1249
1250     if (item.iExpandedImage != 1)
1251     {
1252         win_skip("TVIF_EXPANDEDIMAGE not supported\n");
1253         DestroyWindow(hTree);
1254         return;
1255     }
1256
1257     /* test for default iExpandedImage value */
1258     item.mask = TVIF_EXPANDEDIMAGE;
1259     item.iExpandedImage = -1;
1260     item.hItem = hChild;
1261     ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1262     ok(ret, "got %d\n", ret);
1263     ok(item.iExpandedImage == (WORD)I_IMAGENONE, "got %d\n", item.iExpandedImage);
1264
1265     DestroyWindow(hTree);
1266 }
1267
1268 static void test_TVS_SINGLEEXPAND(void)
1269 {
1270     HWND hTree;
1271     BOOL ret;
1272
1273     hTree = create_treeview_control();
1274     SetWindowLongA(hTree, GWL_STYLE, GetWindowLong(hTree, GWL_STYLE) | TVS_SINGLEEXPAND);
1275     /* to avoid paiting related notifications */
1276     ShowWindow(hTree, SW_HIDE);
1277     fill_tree(hTree);
1278
1279     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1280     ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
1281     ok(ret, "got %d\n", ret);
1282     ok_sequence(sequences, PARENT_SEQ_INDEX, parent_singleexpand_seq, "singleexpand notifications", FALSE);
1283
1284     /* a workaround for NT4 that sends expanding notification when nothing is about to expand */
1285     ret = SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)hRoot);
1286     ok(ret, "got %d\n", ret);
1287     fill_tree(hTree);
1288     ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, 0);
1289     ok(ret, "got %d\n", ret);
1290
1291     DestroyWindow(hTree);
1292 }
1293
1294 static void test_WM_PAINT(void)
1295 {
1296     HWND hTree;
1297     COLORREF clr;
1298     LONG ret;
1299     RECT rc;
1300     HDC hdc;
1301
1302     hTree = create_treeview_control();
1303
1304     clr = SendMessageA(hTree, TVM_SETBKCOLOR, 0, RGB(255, 0, 0));
1305     ok(clr == -1, "got %d, expected -1\n", clr);
1306
1307     hdc = GetDC(hMainWnd);
1308
1309     GetClientRect(hMainWnd, &rc);
1310     FillRect(hdc, &rc, GetStockObject(BLACK_BRUSH));
1311
1312     clr = GetPixel(hdc, 1, 1);
1313     ok(clr == RGB(0, 0, 0), "got 0x%x\n", clr);
1314
1315     ret = SendMessageA(hTree, WM_PAINT, (WPARAM)hdc, 0);
1316     ok(ret == 0, "got %d\n", ret);
1317
1318     clr = GetPixel(hdc, 1, 1);
1319     ok(clr == RGB(255, 0, 0) || broken(clr == RGB(0, 0, 0)) /* win98 */,
1320         "got 0x%x\n", clr);
1321
1322     ReleaseDC(hMainWnd, hdc);
1323
1324     DestroyWindow(hTree);
1325 }
1326
1327 START_TEST(treeview)
1328 {
1329     HMODULE hComctl32;
1330     BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
1331     WNDCLASSA wc;
1332     MSG msg;
1333
1334     ULONG_PTR ctx_cookie;
1335     HANDLE hCtx;
1336     HWND hwnd;
1337   
1338     hComctl32 = GetModuleHandleA("comctl32.dll");
1339     pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
1340     if (pInitCommonControlsEx)
1341     {
1342         INITCOMMONCONTROLSEX iccex;
1343         iccex.dwSize = sizeof(iccex);
1344         iccex.dwICC  = ICC_TREEVIEW_CLASSES;
1345         pInitCommonControlsEx(&iccex);
1346     }
1347     else
1348         InitCommonControls();
1349
1350     init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
1351   
1352     wc.style = CS_HREDRAW | CS_VREDRAW;
1353     wc.cbClsExtra = 0;
1354     wc.cbWndExtra = 0;
1355     wc.hInstance = GetModuleHandleA(NULL);
1356     wc.hIcon = NULL;
1357     wc.hCursor = LoadCursorA(NULL, IDC_IBEAM);
1358     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1359     wc.lpszMenuName = NULL;
1360     wc.lpszClassName = "MyTestWnd";
1361     wc.lpfnWndProc = parent_wnd_proc;
1362     RegisterClassA(&wc);
1363
1364     hMainWnd = CreateWindowExA(0, "MyTestWnd", "Blah", WS_OVERLAPPEDWINDOW,
1365       CW_USEDEFAULT, CW_USEDEFAULT, 130, 105, NULL, NULL, GetModuleHandleA(NULL), 0);
1366
1367     ok(hMainWnd != NULL, "Failed to create parent window. Tests aborted.\n");
1368     if (!hMainWnd) return;
1369
1370     test_fillroot();
1371     test_select();
1372     test_getitemtext();
1373     test_focus();
1374     test_get_set_bkcolor();
1375     test_get_set_imagelist();
1376     test_get_set_indent();
1377     test_get_set_insertmark();
1378     test_get_set_item();
1379     test_get_set_itemheight();
1380     test_get_set_scrolltime();
1381     test_get_set_textcolor();
1382     test_get_linecolor();
1383     test_get_insertmarkcolor();
1384     test_get_set_tooltips();
1385     test_get_set_unicodeformat();
1386     test_callback();
1387     test_expandinvisible();
1388     test_itemedit();
1389     test_treeview_classinfo();
1390     test_expandnotify();
1391     test_TVS_SINGLEEXPAND();
1392     test_WM_PAINT();
1393
1394     if (!load_v6_module(&ctx_cookie, &hCtx))
1395     {
1396         DestroyWindow(hMainWnd);
1397         return;
1398     }
1399
1400     /* this is a XP SP3 failure workaround */
1401     hwnd = CreateWindowExA(0, WC_TREEVIEW, "foo",
1402                            WS_CHILD | WS_BORDER | WS_VISIBLE,
1403                            0, 0, 100, 100,
1404                            hMainWnd, NULL, GetModuleHandleA(NULL), NULL);
1405     if (!IsWindow(hwnd))
1406     {
1407         win_skip("FIXME: failed to create TreeView window.\n");
1408         unload_v6_module(ctx_cookie, hCtx);
1409         DestroyWindow(hMainWnd);
1410         return;
1411     }
1412     else
1413         DestroyWindow(hwnd);
1414
1415     /* comctl32 version 6 tests start here */
1416     test_expandedimage();
1417
1418     unload_v6_module(ctx_cookie, hCtx);
1419
1420     PostMessageA(hMainWnd, WM_CLOSE, 0, 0);
1421     while(GetMessageA(&msg, 0, 0, 0))
1422     {
1423         TranslateMessage(&msg);
1424         DispatchMessageA(&msg);
1425     }
1426 }