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