comctl32/treeview: Use separate pointer for currently edited item.
[wine] / dlls / comctl32 / tests / treeview.c
1 /* Unit tests for treeview.
2  *
3  * Copyright 2005 Krzysztof Foltman
4  * Copyright 2007 Christopher James Peterson
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <assert.h>
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "winnls.h"
29 #include "winreg.h"
30 #include "commctrl.h" 
31
32 #include "wine/test.h"
33 #include "msg.h"
34
35 const char *TEST_CALLBACK_TEXT = "callback_text";
36
37 #define NUM_MSG_SEQUENCES   1
38 #define TREEVIEW_SEQ_INDEX  0
39
40 #define expect(expected, got) ok(got == expected, "Expected %d, got %d\n", expected, got)
41
42 static struct msg_sequence *MsgSequences[NUM_MSG_SEQUENCES];
43
44 static const struct message FillRootSeq[] = {
45     { TVM_INSERTITEM, sent },
46     { TVM_INSERTITEM, sent },
47     { 0 }
48 };
49
50 static const struct message rootnone_select_seq[] = {
51     { TVM_SELECTITEM, sent|wparam, 9 },
52     { TVM_SELECTITEM, sent|wparam, 9 },
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     { 0 }
58 };
59
60 static const struct message rootchild_select_seq[] = {
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     { TVM_SELECTITEM, sent|wparam, 9 },
66     { TVM_SELECTITEM, sent|wparam, 9 },
67     { 0 }
68 };
69
70 static const struct message getitemtext_seq[] = {
71     { TVM_INSERTITEM, sent },
72     { TVM_GETITEM, sent },
73     { TVM_DELETEITEM, sent },
74     { 0 }
75 };
76
77 static const struct message focus_seq[] = {
78     { TVM_INSERTITEM, sent },
79     { TVM_INSERTITEM, sent },
80     { TVM_SELECTITEM, sent|wparam, 9 },
81     /* The following end up out of order in wine */
82     { WM_WINDOWPOSCHANGING, sent|defwinproc },
83     { WM_NCCALCSIZE, sent|wparam|defwinproc, TRUE },
84     { WM_WINDOWPOSCHANGED, sent|defwinproc },
85     { WM_SIZE, sent|defwinproc },
86     { WM_PAINT, sent|defwinproc },
87     { WM_NCPAINT, sent|wparam|defwinproc, 1 },
88     { WM_ERASEBKGND, sent|defwinproc },
89     { TVM_EDITLABEL, sent },
90     { WM_COMMAND, sent|wparam|defwinproc, MAKEWPARAM(0, EN_UPDATE) },
91     { WM_COMMAND, sent|wparam|defwinproc, MAKEWPARAM(0, EN_CHANGE) },
92     { WM_PARENTNOTIFY, sent|wparam|defwinproc, MAKEWPARAM(WM_CREATE, 0) },
93     { WM_KILLFOCUS, sent|defwinproc },
94     { WM_PAINT, sent|defwinproc },
95     { WM_IME_SETCONTEXT, sent|defwinproc|optional },
96     { WM_COMMAND, sent|wparam|defwinproc, MAKEWPARAM(0, EN_SETFOCUS) },
97     { WM_CTLCOLOREDIT, sent|defwinproc|optional },
98     { WM_CTLCOLOREDIT, sent|defwinproc|optional },
99     { 0 }
100 };
101
102 static const struct message TestGetSetBkColorSeq[] = {
103     { TVM_GETBKCOLOR, sent|wparam|lparam, 0, 0 },
104     { TVM_SETBKCOLOR, sent|wparam|lparam, 0, 0 },
105     { TVM_GETBKCOLOR, sent|wparam|lparam, 0, 0 },
106     { TVM_SETBKCOLOR, sent|wparam|lparam, 0, 0x00ffffff },
107     { TVM_GETBKCOLOR, sent|wparam|lparam, 0, 0 },
108     { TVM_SETBKCOLOR, sent|wparam|lparam, 0, -1 },
109     { 0 }
110 };
111
112 static const struct message TestGetSetImageListSeq[] = {
113     { TVM_SETIMAGELIST, sent|wparam|lparam, 0, 0 },
114     { TVM_GETIMAGELIST, sent|wparam|lparam, 0, 0 },
115     { 0 }
116 };
117
118 static const struct message TestGetSetIndentSeq[] = {
119     { TVM_SETINDENT, sent|wparam|lparam, 0, 0 },
120     { TVM_GETINDENT, sent|wparam|lparam, 0, 0 },
121     /* The actual amount to indent is dependent on the system for this message */
122     { TVM_SETINDENT, sent },
123     { TVM_GETINDENT, sent|wparam|lparam, 0, 0 },
124     { 0 }
125 };
126
127 static const struct message TestGetSetInsertMarkColorSeq[] = {
128     { TVM_SETINSERTMARKCOLOR, sent|wparam|lparam, 0, 0 },
129     { TVM_GETINSERTMARKCOLOR, sent|wparam|lparam, 0, 0 },
130     { 0 }
131 };
132
133 static const struct message TestGetSetItemSeq[] = {
134     { TVM_GETITEM, sent },
135     { TVM_SETITEM, sent },
136     { TVM_GETITEM, sent },
137     { TVM_SETITEM, sent },
138     { 0 }
139 };
140
141 static const struct message TestGetSetItemHeightSeq[] = {
142     { TVM_GETITEMHEIGHT, sent|wparam|lparam, 0, 0 },
143     { TVM_SETITEMHEIGHT, sent|wparam|lparam, -1, 0 },
144     { TVM_GETITEMHEIGHT, sent|wparam|lparam, 0, 0 },
145     { TVM_SETITEMHEIGHT, sent|lparam, 0xcccccccc, 0 },
146     { TVM_GETITEMHEIGHT, sent|wparam|lparam|optional, 0, 0 },
147     { TVM_SETITEMHEIGHT, sent|wparam|lparam|optional, 9, 0 },
148     { TVM_GETITEMHEIGHT, sent|wparam|lparam, 0, 0 },
149     { 0 }
150 };
151
152 static const struct message TestGetSetScrollTimeSeq[] = {
153     { TVM_SETSCROLLTIME, sent|wparam|lparam, 20, 0 },
154     { TVM_GETSCROLLTIME, sent|wparam|lparam, 0, 0 },
155     { 0 }
156 };
157
158 static const struct message TestGetSetTextColorSeq[] = {
159     { TVM_GETTEXTCOLOR, sent|wparam|lparam, 0, 0 },
160     { TVM_SETTEXTCOLOR, sent|wparam|lparam, 0, 0 },
161     { TVM_GETTEXTCOLOR, sent|wparam|lparam, 0, 0 },
162     { TVM_SETTEXTCOLOR, sent|wparam|lparam, 0, 0x00ffffff },
163     { TVM_GETTEXTCOLOR, sent|wparam|lparam, 0, 0 },
164     { TVM_SETTEXTCOLOR, sent|wparam|lparam, 0, -1 },
165     { 0 }
166 };
167
168 static const struct message TestGetSetToolTipsSeq[] = {
169     { WM_KILLFOCUS,    sent },
170     { TVM_SETTOOLTIPS, sent|wparam|lparam, 0, 0 },
171     { TVM_GETTOOLTIPS, sent|wparam|lparam, 0, 0 },
172     { 0 }
173 };
174
175 static const struct message TestGetSetUnicodeFormatSeq[] = {
176     { TVM_SETUNICODEFORMAT, sent|wparam|lparam, TRUE, 0 },
177     { TVM_GETUNICODEFORMAT, sent|wparam|lparam, 0, 0 },
178     { TVM_SETUNICODEFORMAT, sent|wparam|lparam, 0, 0 },
179     { TVM_GETUNICODEFORMAT, sent|wparam|lparam, 0, 0 },
180     { TVM_SETUNICODEFORMAT, sent|wparam|lparam, 0, 0 },
181     { 0 }
182 };
183
184 static HWND hMainWnd;
185
186 static HWND hTree, hEdit;
187 static HTREEITEM hRoot, hChild;
188
189 static int pos = 0;
190 static char sequence[256];
191
192 static void Clear(void)
193 {
194     pos = 0;
195     sequence[0] = '\0';
196 }
197
198 static void AddItem(char ch)
199 {
200     sequence[pos++] = ch;
201     sequence[pos] = '\0';
202 }
203
204 static void IdentifyItem(HTREEITEM hItem)
205 {
206     if (hItem == hRoot) {
207         AddItem('R');
208         return;
209     }
210     if (hItem == hChild) {
211         AddItem('C');
212         return;
213     }
214     if (hItem == NULL) {
215         AddItem('n');
216         return;
217     }
218     AddItem('?');
219 }
220
221 /* This function hooks in and records all messages to the treeview control */
222 static LRESULT WINAPI TreeviewWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
223 {
224     static LONG defwndproc_counter = 0;
225     LRESULT ret;
226     struct message msg;
227     WNDPROC lpOldProc = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
228
229     msg.message = message;
230     msg.flags = sent|wparam|lparam;
231     if (defwndproc_counter) msg.flags |= defwinproc;
232     msg.wParam = wParam;
233     msg.lParam = lParam;
234     add_message(MsgSequences, TREEVIEW_SEQ_INDEX, &msg);
235
236     defwndproc_counter++;
237     ret = CallWindowProcA(lpOldProc, hwnd, message, wParam, lParam);
238     defwndproc_counter--;
239
240     return ret;
241 }
242
243 static HWND create_treeview_control(void)
244 {
245     WNDPROC pOldWndProc;
246     HWND hTree;
247
248     hTree = CreateWindowExA(WS_EX_CLIENTEDGE, WC_TREEVIEWA, NULL, WS_CHILD|WS_VISIBLE|
249             TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS|TVS_EDITLABELS,
250             0, 0, 120, 100, hMainWnd, (HMENU)100, GetModuleHandleA(0), 0);
251
252     SetFocus(hTree);
253
254     /* Record the old WNDPROC so we can call it after recording the messages */
255     pOldWndProc = (WNDPROC)SetWindowLongPtrA(hTree, GWLP_WNDPROC, (LONG_PTR)TreeviewWndProc);
256     SetWindowLongPtrA(hTree, GWLP_USERDATA, (LONG_PTR)pOldWndProc);
257
258     return hTree;
259 }
260
261 static void fill_tree(HWND hTree)
262 {
263     TVINSERTSTRUCTA ins;
264     static CHAR root[]  = "Root",
265                 child[] = "Child";
266
267     ins.hParent = TVI_ROOT;
268     ins.hInsertAfter = TVI_ROOT;
269     U(ins).item.mask = TVIF_TEXT;
270     U(ins).item.pszText = root;
271     hRoot = TreeView_InsertItem(hTree, &ins);
272
273     ins.hParent = hRoot;
274     ins.hInsertAfter = TVI_FIRST;
275     U(ins).item.mask = TVIF_TEXT;
276     U(ins).item.pszText = child;
277     hChild = TreeView_InsertItem(hTree, &ins);
278 }
279
280 static void test_fillroot(void)
281 {
282     TVITEM tvi;
283
284     hTree = create_treeview_control();
285
286     flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
287
288     fill_tree(hTree);
289
290     Clear();
291     AddItem('A');
292     assert(hRoot);
293     AddItem('B');
294     assert(hChild);
295     AddItem('.');
296     ok_sequence(MsgSequences, TREEVIEW_SEQ_INDEX, FillRootSeq, "FillRoot", FALSE);
297     ok(!strcmp(sequence, "AB."), "Item creation\n");
298
299     /* UMLPad 1.15 depends on this being not -1 (I_IMAGECALLBACK) */
300     tvi.hItem = hRoot;
301     tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
302     SendMessage( hTree, TVM_GETITEM, 0, (LPARAM)&tvi );
303     ok(tvi.iImage == 0, "tvi.iImage=%d\n", tvi.iImage);
304     ok(tvi.iSelectedImage == 0, "tvi.iSelectedImage=%d\n", tvi.iSelectedImage);
305
306     DestroyWindow(hTree);
307 }
308
309 static void test_callback(void)
310 {
311     HTREEITEM hRoot;
312     HTREEITEM hItem1, hItem2;
313     TVINSERTSTRUCTA ins;
314     TVITEM tvi;
315     CHAR test_string[] = "Test_string";
316     CHAR buf[128];
317     LRESULT ret;
318
319     hTree = create_treeview_control();
320     fill_tree(hTree);
321
322     ret = TreeView_DeleteAllItems(hTree);
323     ok(ret == TRUE, "ret\n");
324     ins.hParent = TVI_ROOT;
325     ins.hInsertAfter = TVI_ROOT;
326     U(ins).item.mask = TVIF_TEXT;
327     U(ins).item.pszText = LPSTR_TEXTCALLBACK;
328     hRoot = TreeView_InsertItem(hTree, &ins);
329     assert(hRoot);
330
331     tvi.hItem = hRoot;
332     tvi.mask = TVIF_TEXT;
333     tvi.pszText = buf;
334     tvi.cchTextMax = sizeof(buf)/sizeof(buf[0]);
335     ret = TreeView_GetItem(hTree, &tvi);
336     ok(ret == 1, "ret\n");
337     ok(strcmp(tvi.pszText, TEST_CALLBACK_TEXT) == 0, "Callback item text mismatch %s vs %s\n",
338         tvi.pszText, TEST_CALLBACK_TEXT);
339
340     ins.hParent = hRoot;
341     ins.hInsertAfter = TVI_FIRST;
342     U(ins).item.mask = TVIF_TEXT;
343     U(ins).item.pszText = test_string;
344     hItem1 = TreeView_InsertItem(hTree, &ins);
345     assert(hItem1);
346
347     tvi.hItem = hItem1;
348     ret = TreeView_GetItem(hTree, &tvi);
349     ok(ret == TRUE, "ret\n");
350     ok(strcmp(tvi.pszText, test_string) == 0, "Item text mismatch %s vs %s\n",
351         tvi.pszText, test_string);
352
353     /* undocumented: pszText of NULL also means LPSTR_CALLBACK: */
354     tvi.pszText = NULL;
355     ret = TreeView_SetItem(hTree, &tvi);
356     ok(ret == 1, "Expected SetItem return 1, got %ld\n", ret);
357     tvi.pszText = buf;
358     ret = TreeView_GetItem(hTree, &tvi);
359     ok(ret == TRUE, "Expected GetItem return TRUE, got %ld\n", ret);
360     ok(strcmp(tvi.pszText, TEST_CALLBACK_TEXT) == 0, "Item text mismatch %s vs %s\n",
361         tvi.pszText, TEST_CALLBACK_TEXT);
362
363     U(ins).item.pszText = NULL;
364     hItem2 = TreeView_InsertItem(hTree, &ins);
365     assert(hItem2);
366     tvi.hItem = hItem2;
367     memset(buf, 0, sizeof(buf));
368     ret = TreeView_GetItem(hTree, &tvi);
369     ok(ret == TRUE, "Expected GetItem return TRUE, got %ld\n", ret);
370     ok(strcmp(tvi.pszText, TEST_CALLBACK_TEXT) == 0, "Item text mismatch %s vs %s\n",
371         tvi.pszText, TEST_CALLBACK_TEXT);
372
373     DestroyWindow(hTree);
374 }
375
376 static void test_select(void)
377 {
378     BOOL r;
379
380     hTree = create_treeview_control();
381     fill_tree(hTree);
382
383     /* root-none select tests */
384     flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
385     r = TreeView_SelectItem(hTree, NULL);
386     Clear();
387     AddItem('1');
388     r = TreeView_SelectItem(hTree, hRoot);
389     AddItem('2');
390     r = TreeView_SelectItem(hTree, hRoot);
391     AddItem('3');
392     r = TreeView_SelectItem(hTree, NULL);
393     AddItem('4');
394     r = TreeView_SelectItem(hTree, NULL);
395     AddItem('5');
396     r = TreeView_SelectItem(hTree, hRoot);
397     AddItem('.');
398     ok(!strcmp(sequence, "1(nR)nR23(Rn)Rn45(nR)nR."), "root-none select test\n");
399     ok_sequence(MsgSequences, TREEVIEW_SEQ_INDEX, rootnone_select_seq,
400                 "root-none select seq", FALSE);
401
402     /* root-child select tests */
403     flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
404     r = TreeView_SelectItem(hTree, NULL);
405     Clear();
406     AddItem('1');
407     r = TreeView_SelectItem(hTree, hRoot);
408     AddItem('2');
409     r = TreeView_SelectItem(hTree, hRoot);
410     AddItem('3');
411     r = TreeView_SelectItem(hTree, hChild);
412     AddItem('4');
413     r = TreeView_SelectItem(hTree, hChild);
414     AddItem('5');
415     r = TreeView_SelectItem(hTree, hRoot);
416     AddItem('.');
417     ok(!strcmp(sequence, "1(nR)nR23(RC)RC45(CR)CR."), "root-child select test\n");
418     ok_sequence(MsgSequences, TREEVIEW_SEQ_INDEX, rootchild_select_seq,
419                 "root-child select seq", FALSE);
420
421     DestroyWindow(hTree);
422 }
423
424 static void test_getitemtext(void)
425 {
426     TVINSERTSTRUCTA ins;
427     HTREEITEM hChild;
428     TVITEM tvi;
429
430     CHAR szBuffer[80] = "Blah";
431     int nBufferSize = sizeof(szBuffer)/sizeof(CHAR);
432
433     hTree = create_treeview_control();
434     fill_tree(hTree);
435
436     flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
437
438     /* add an item without TVIF_TEXT mask and pszText == NULL */
439     ins.hParent = hRoot;
440     ins.hInsertAfter = TVI_ROOT;
441     U(ins).item.mask = 0;
442     U(ins).item.pszText = NULL;
443     U(ins).item.cchTextMax = 0;
444     hChild = TreeView_InsertItem(hTree, &ins);
445     assert(hChild);
446
447     /* retrieve it with TVIF_TEXT mask */
448     tvi.hItem = hChild;
449     tvi.mask = TVIF_TEXT;
450     tvi.cchTextMax = nBufferSize;
451     tvi.pszText = szBuffer;
452
453     SendMessageA( hTree, TVM_GETITEM, 0, (LPARAM)&tvi );
454     ok(!strcmp(szBuffer, ""), "szBuffer=\"%s\", expected \"\"\n", szBuffer);
455     ok(SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)hChild), "DeleteItem failed\n");
456     ok_sequence(MsgSequences, TREEVIEW_SEQ_INDEX, getitemtext_seq, "get item text seq", FALSE);
457
458     DestroyWindow(hTree);
459 }
460
461 static void test_focus(void)
462 {
463     TVINSERTSTRUCTA ins;
464     static CHAR child1[]  = "Edit",
465                 child2[]  = "A really long string";
466     HTREEITEM hChild1, hChild2;
467
468     hTree = create_treeview_control();
469     fill_tree(hTree);
470
471     flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
472
473     /* This test verifies that when a label is being edited, scrolling
474      * the treeview does not cause the label to lose focus. To test
475      * this, first some additional entries are added to generate
476      * scrollbars.
477      */
478     ins.hParent = hRoot;
479     ins.hInsertAfter = hChild;
480     U(ins).item.mask = TVIF_TEXT;
481     U(ins).item.pszText = child1;
482     hChild1 = TreeView_InsertItem(hTree, &ins);
483     assert(hChild1);
484     ins.hInsertAfter = hChild1;
485     U(ins).item.mask = TVIF_TEXT;
486     U(ins).item.pszText = child2;
487     hChild2 = TreeView_InsertItem(hTree, &ins);
488     assert(hChild2);
489
490     ShowWindow(hMainWnd,SW_SHOW);
491     SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hChild);
492     hEdit = TreeView_EditLabel(hTree, hChild);
493     ScrollWindowEx(hTree, -10, 0, NULL, NULL, NULL, NULL, SW_SCROLLCHILDREN);
494     ok(GetFocus() == hEdit, "Edit control should have focus\n");
495     ok_sequence(MsgSequences, TREEVIEW_SEQ_INDEX, focus_seq, "focus test", TRUE);
496
497     DestroyWindow(hTree);
498 }
499
500 static void TestGetSetBkColor(void)
501 {
502     COLORREF crColor = RGB(0,0,0);
503
504     /* If the value is -1, the control is using the system color for the background color. */
505     crColor = (COLORREF)SendMessage( hTree, TVM_GETBKCOLOR, 0, 0 );
506     ok(crColor == -1, "Default background color reported as 0x%.8x\n", crColor);
507
508     /* Test for black background */
509     SendMessage( hTree, TVM_SETBKCOLOR, 0, (LPARAM)RGB(0,0,0) );
510     crColor = (COLORREF)SendMessage( hTree, TVM_GETBKCOLOR, 0, 0 );
511     ok(crColor == RGB(0,0,0), "Black background color reported as 0x%.8x\n", crColor);
512
513     /* Test for white background */
514     SendMessage( hTree, TVM_SETBKCOLOR, 0, (LPARAM)RGB(255,255,255) );
515     crColor = (COLORREF)SendMessage( hTree, TVM_GETBKCOLOR, 0, 0 );
516     ok(crColor == RGB(255,255,255), "White background color reported as 0x%.8x\n", crColor);
517
518     /* Reset the default background */
519     SendMessage( hTree, TVM_SETBKCOLOR, 0, -1 );
520 }
521
522 static void TestGetSetImageList(void)
523 {
524     HIMAGELIST hImageList = NULL;
525
526     /* Test a NULL HIMAGELIST */
527     SendMessage( hTree, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)hImageList );
528     hImageList = (HIMAGELIST)SendMessage( hTree, TVM_GETIMAGELIST, TVSIL_NORMAL, 0 );
529     ok(hImageList == NULL, "NULL image list, reported as 0x%p, expected 0.\n", hImageList);
530
531     /* TODO: Test an actual image list */
532 }
533
534 static void TestGetSetIndent(void)
535 {
536     int ulIndent = -1;
537     int ulMinIndent = -1;
538     int ulMoreThanTwiceMin = -1;
539
540     /* Finding the minimum indent */
541     SendMessage( hTree, TVM_SETINDENT, 0, 0 );
542     ulMinIndent = (int)SendMessage( hTree, TVM_GETINDENT, 0, 0 );
543
544     /* Checking an indent that is more than twice the default indent */
545     ulMoreThanTwiceMin = 2*ulMinIndent+1;
546     SendMessage( hTree, TVM_SETINDENT, ulMoreThanTwiceMin, 0 );
547     ulIndent = (DWORD)SendMessage( hTree, TVM_GETINDENT, 0, 0 );
548     ok(ulIndent == ulMoreThanTwiceMin, "Indent reported as %d, expected %d\n", ulIndent, ulMoreThanTwiceMin);
549 }
550
551 static void TestGetSetInsertMarkColor(void)
552 {
553     COLORREF crColor = RGB(0,0,0);
554     SendMessage( hTree, TVM_SETINSERTMARKCOLOR, 0, crColor );
555     crColor = (COLORREF)SendMessage( hTree, TVM_GETINSERTMARKCOLOR, 0, 0 );
556     ok(crColor == RGB(0,0,0), "Insert mark color reported as 0x%.8x, expected 0x00000000\n", crColor);
557 }
558
559 static void TestGetSetItem(void)
560 {
561     TVITEM tviRoot = {0};
562     int nBufferSize = 80;
563     char szBuffer[80] = {0};
564
565     /* Test the root item */
566     tviRoot.hItem = hRoot;
567     tviRoot.mask = TVIF_TEXT;
568     tviRoot.cchTextMax = nBufferSize;
569     tviRoot.pszText = szBuffer;
570     SendMessage( hTree, TVM_GETITEM, 0, (LPARAM)&tviRoot );
571     ok(!strcmp("Root", szBuffer), "GetItem: szBuffer=\"%s\", expected \"Root\"\n", szBuffer);
572
573     /* Change the root text */
574     strncpy(szBuffer, "Testing123", nBufferSize);
575     SendMessage( hTree, TVM_SETITEM, 0, (LPARAM)&tviRoot );
576     memset(szBuffer, 0, nBufferSize);
577     SendMessage( hTree, TVM_GETITEM, 0, (LPARAM)&tviRoot );
578     ok(!strcmp("Testing123", szBuffer), "GetItem: szBuffer=\"%s\", expected \"Testing123\"\n", szBuffer);
579
580     /* Reset the root text */
581     memset(szBuffer, 0, nBufferSize);
582     strncpy(szBuffer, "Root", nBufferSize);
583     SendMessage( hTree, TVM_SETITEM, 0, (LPARAM)&tviRoot );
584 }
585
586 static void TestGetSetItemHeight(void)
587 {
588     int ulOldHeight = 0;
589     int ulNewHeight = 0;
590
591     /* Assuming default height to begin with */
592     ulOldHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
593
594     /* Explicitly setting and getting the default height */
595     SendMessage( hTree, TVM_SETITEMHEIGHT, -1, 0 );
596     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
597     ok(ulNewHeight == ulOldHeight, "Default height not set properly, reported %d, expected %d\n", ulNewHeight, ulOldHeight);
598
599     /* Explicitly setting and getting the height of twice the normal */
600     SendMessage( hTree, TVM_SETITEMHEIGHT, 2*ulOldHeight, 0 );
601     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
602     ok(ulNewHeight == 2*ulOldHeight, "New height not set properly, reported %d, expected %d\n", ulNewHeight, 2*ulOldHeight);
603
604     /* Assuming tree doesn't have TVS_NONEVENHEIGHT set, so a set of 9 will round down to 8 */
605     SendMessage( hTree, TVM_SETITEMHEIGHT, 9, 0 );
606     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
607     ok(ulNewHeight == 8, "Uneven height not set properly, reported %d, expected %d\n", ulNewHeight, 8);
608 }
609
610 static void TestGetSetScrollTime(void)
611 {
612     int ulExpectedTime = 20;
613     int ulTime = 0;
614     SendMessage( hTree, TVM_SETSCROLLTIME, ulExpectedTime, 0 );
615     ulTime = (int)SendMessage( hTree, TVM_GETSCROLLTIME, 0, 0 );
616     ok(ulTime == ulExpectedTime, "Scroll time reported as %d, expected %d\n", ulTime, ulExpectedTime);
617 }
618
619 static void TestGetSetTextColor(void)
620 {
621     /* If the value is -1, the control is using the system color for the text color. */
622     COLORREF crColor = RGB(0,0,0);
623     crColor = (COLORREF)SendMessage( hTree, TVM_GETTEXTCOLOR, 0, 0 );
624     ok(crColor == -1, "Default text color reported as 0x%.8x\n", crColor);
625
626     /* Test for black text */
627     SendMessage( hTree, TVM_SETTEXTCOLOR, 0, (LPARAM)RGB(0,0,0) );
628     crColor = (COLORREF)SendMessage( hTree, TVM_GETTEXTCOLOR, 0, 0 );
629     ok(crColor == RGB(0,0,0), "Black text color reported as 0x%.8x\n", crColor);
630
631     /* Test for white text */
632     SendMessage( hTree, TVM_SETTEXTCOLOR, 0, (LPARAM)RGB(255,255,255) );
633     crColor = (COLORREF)SendMessage( hTree, TVM_GETTEXTCOLOR, 0, 0 );
634     ok(crColor == RGB(255,255,255), "White text color reported as 0x%.8x\n", crColor);
635
636     /* Reset the default text color */
637     SendMessage( hTree, TVM_SETTEXTCOLOR, 0, -1 );
638 }
639
640 static void TestGetSetToolTips(void)
641 {
642     HWND hwndLastToolTip = NULL;
643     HWND hPopupTreeView;
644
645     /* show even WS_POPUP treeview don't send NM_TOOLTIPSCREATED */
646     hPopupTreeView = CreateWindow(WC_TREEVIEW, NULL, WS_POPUP|WS_VISIBLE, 0, 0, 100, 100, hMainWnd, NULL, NULL, NULL);
647     DestroyWindow(hPopupTreeView);
648
649     /* Testing setting a NULL ToolTip */
650     SendMessage( hTree, TVM_SETTOOLTIPS, 0, 0 );
651     hwndLastToolTip = (HWND)SendMessage( hTree, TVM_GETTOOLTIPS, 0, 0 );
652     ok(hwndLastToolTip == NULL, "NULL tool tip, reported as 0x%p, expected 0.\n", hwndLastToolTip);
653
654     /* TODO: Add a test of an actual tooltip */
655 }
656
657 static void TestGetSetUnicodeFormat(void)
658 {
659     BOOL bPreviousSetting = 0;
660     BOOL bNewSetting = 0;
661
662     /* Set to Unicode */
663     bPreviousSetting = (BOOL)SendMessage( hTree, TVM_SETUNICODEFORMAT, 1, 0 );
664     bNewSetting = (BOOL)SendMessage( hTree, TVM_GETUNICODEFORMAT, 0, 0 );
665     ok(bNewSetting == 1, "Unicode setting did not work.\n");
666
667     /* Set to ANSI */
668     SendMessage( hTree, TVM_SETUNICODEFORMAT, 0, 0 );
669     bNewSetting = (BOOL)SendMessage( hTree, TVM_GETUNICODEFORMAT, 0, 0 );
670     ok(bNewSetting == 0, "ANSI setting did not work.\n");
671
672     /* Revert to original setting */
673     SendMessage( hTree, TVM_SETUNICODEFORMAT, (LPARAM)bPreviousSetting, 0 );
674 }
675
676 static void test_getset(void)
677 {
678     hTree = create_treeview_control();
679     fill_tree(hTree);
680
681     /* TVM_GETBKCOLOR and TVM_SETBKCOLOR */
682     flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
683     TestGetSetBkColor();
684     ok_sequence(MsgSequences, TREEVIEW_SEQ_INDEX, TestGetSetBkColorSeq,
685         "TestGetSetBkColor", FALSE);
686
687     /* TVM_GETIMAGELIST and TVM_SETIMAGELIST */
688     flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
689     TestGetSetImageList();
690     ok_sequence(MsgSequences, TREEVIEW_SEQ_INDEX, TestGetSetImageListSeq,
691         "TestGetImageList", FALSE);
692
693     /* TVM_SETINDENT and TVM_GETINDENT */
694     flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
695     TestGetSetIndent();
696     ok_sequence(MsgSequences, TREEVIEW_SEQ_INDEX, TestGetSetIndentSeq,
697         "TestGetSetIndent", FALSE);
698
699     /* TVM_GETINSERTMARKCOLOR and TVM_GETINSERTMARKCOLOR */
700     flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
701     TestGetSetInsertMarkColor();
702     ok_sequence(MsgSequences, TREEVIEW_SEQ_INDEX, TestGetSetInsertMarkColorSeq,
703         "TestGetSetInsertMarkColor", FALSE);
704
705     /* TVM_GETITEM and TVM_SETITEM */
706     flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
707     TestGetSetItem();
708     ok_sequence(MsgSequences, TREEVIEW_SEQ_INDEX, TestGetSetItemSeq,
709         "TestGetSetItem", FALSE);
710
711     /* TVM_GETITEMHEIGHT and TVM_SETITEMHEIGHT */
712     flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
713     TestGetSetItemHeight();
714     ok_sequence(MsgSequences, TREEVIEW_SEQ_INDEX, TestGetSetItemHeightSeq,
715         "TestGetSetItemHeight", FALSE);
716
717     /* TVM_GETSCROLLTIME and TVM_SETSCROLLTIME */
718     flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
719     TestGetSetScrollTime();
720     ok_sequence(MsgSequences, TREEVIEW_SEQ_INDEX, TestGetSetScrollTimeSeq,
721         "TestGetSetScrollTime", FALSE);
722
723     /* TVM_GETTEXTCOLOR and TVM_SETTEXTCOLOR */
724     flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
725     TestGetSetTextColor();
726     ok_sequence(MsgSequences, TREEVIEW_SEQ_INDEX, TestGetSetTextColorSeq,
727         "TestGetSetTextColor", FALSE);
728
729     /* TVM_GETTOOLTIPS and TVM_SETTOOLTIPS */
730     flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
731     TestGetSetToolTips();
732     ok_sequence(MsgSequences, TREEVIEW_SEQ_INDEX, TestGetSetToolTipsSeq,
733         "TestGetSetToolTips", TRUE);
734
735     /* TVM_GETUNICODEFORMAT and TVM_SETUNICODEFORMAT */
736     flush_sequences(MsgSequences, NUM_MSG_SEQUENCES);
737     TestGetSetUnicodeFormat();
738     ok_sequence(MsgSequences, TREEVIEW_SEQ_INDEX, TestGetSetUnicodeFormatSeq,
739         "TestGetSetUnicodeFormat", FALSE);
740
741     DestroyWindow(hTree);
742 }
743
744 static LRESULT CALLBACK MyWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
745 {
746     switch(msg) {
747     case WM_NOTIFY:
748     {
749         NMHDR *pHdr = (NMHDR *)lParam;
750     
751         ok(pHdr->code != NM_FIRST - 19, "Treeview should not send NM_TOOLTIPSCREATED\n");
752         if (pHdr->idFrom == 100) {
753             NMTREEVIEWA *pTreeView = (LPNMTREEVIEWA) lParam;
754             switch(pHdr->code) {
755             case TVN_SELCHANGINGA:
756                 AddItem('(');
757                 IdentifyItem(pTreeView->itemOld.hItem);
758                 IdentifyItem(pTreeView->itemNew.hItem);
759                 return 0;
760             case TVN_SELCHANGEDA:
761                 AddItem(')');
762                 IdentifyItem(pTreeView->itemOld.hItem);
763                 IdentifyItem(pTreeView->itemNew.hItem);
764                 return 0;
765             case TVN_GETDISPINFOA: {
766                 NMTVDISPINFOA *disp = (NMTVDISPINFOA *)lParam;
767                 if (disp->item.mask & TVIF_TEXT) {
768                     lstrcpyn(disp->item.pszText, TEST_CALLBACK_TEXT, disp->item.cchTextMax);
769                 }
770                 return 0;
771               }
772             case TVN_ENDLABELEDIT: return TRUE;
773             }
774         }
775         return 0;
776     }
777   
778     case WM_DESTROY:
779         PostQuitMessage(0);
780         break;
781   
782     default:
783         return DefWindowProcA(hWnd, msg, wParam, lParam);
784     }
785     return 0L;
786 }
787
788 static void test_expandinvisible(void)
789 {
790     static CHAR nodeText[][5] = {"0", "1", "2", "3", "4"};
791     TVINSERTSTRUCTA ins;
792     HTREEITEM node[5];
793     RECT dummyRect;
794     BOOL nodeVisible;
795     LRESULT ret;
796
797     hTree = create_treeview_control();
798
799     /* The test builds the following tree and expands then node 1, while node 0 is collapsed.
800      *
801      * 0
802      * |- 1
803      * |  |- 2
804      * |  |- 3
805      * |- 4
806      *
807      */
808
809     ret = TreeView_DeleteAllItems(hTree);
810     ok(ret == TRUE, "ret\n");
811     ins.hParent = TVI_ROOT;
812     ins.hInsertAfter = TVI_ROOT;
813     U(ins).item.mask = TVIF_TEXT;
814     U(ins).item.pszText = nodeText[0];
815     node[0] = TreeView_InsertItem(hTree, &ins);
816     assert(node[0]);
817
818     ins.hInsertAfter = TVI_LAST;
819     U(ins).item.mask = TVIF_TEXT;
820     ins.hParent = node[0];
821
822     U(ins).item.pszText = nodeText[1];
823     node[1] = TreeView_InsertItem(hTree, &ins);
824     assert(node[1]);
825     U(ins).item.pszText = nodeText[4];
826     node[4] = TreeView_InsertItem(hTree, &ins);
827     assert(node[4]);
828
829     ins.hParent = node[1];
830
831     U(ins).item.pszText = nodeText[2];
832     node[2] = TreeView_InsertItem(hTree, &ins);
833     assert(node[2]);
834     U(ins).item.pszText = nodeText[3];
835     node[3] = TreeView_InsertItem(hTree, &ins);
836     assert(node[3]);
837
838
839     nodeVisible = TreeView_GetItemRect(hTree, node[1], &dummyRect, FALSE);
840     ok(!nodeVisible, "Node 1 should not be visible.\n");
841     nodeVisible = TreeView_GetItemRect(hTree, node[2], &dummyRect, FALSE);
842     ok(!nodeVisible, "Node 2 should not be visible.\n");
843     nodeVisible = TreeView_GetItemRect(hTree, node[3], &dummyRect, FALSE);
844     ok(!nodeVisible, "Node 3 should not be visible.\n");
845     nodeVisible = TreeView_GetItemRect(hTree, node[4], &dummyRect, FALSE);
846     ok(!nodeVisible, "Node 4 should not be visible.\n");
847
848     ok(TreeView_Expand(hTree, node[1], TVE_EXPAND), "Expand of node 1 failed.\n");
849
850     nodeVisible = TreeView_GetItemRect(hTree, node[1], &dummyRect, FALSE);
851     ok(!nodeVisible, "Node 1 should not be visible.\n");
852     nodeVisible = TreeView_GetItemRect(hTree, node[2], &dummyRect, FALSE);
853     ok(!nodeVisible, "Node 2 should not be visible.\n");
854     nodeVisible = TreeView_GetItemRect(hTree, node[3], &dummyRect, FALSE);
855     ok(!nodeVisible, "Node 3 should not be visible.\n");
856     nodeVisible = TreeView_GetItemRect(hTree, node[4], &dummyRect, FALSE);
857     ok(!nodeVisible, "Node 4 should not be visible.\n");
858
859     DestroyWindow(hTree);
860 }
861
862 static void test_itemedit(void)
863 {
864     DWORD r;
865     HWND edit;
866     TVITEMA item;
867     CHAR buff[2];
868
869     hTree = create_treeview_control();
870     fill_tree(hTree);
871
872     /* try with null item */
873     edit = (HWND)SendMessage(hTree, TVM_EDITLABEL, 0, (LPARAM)NULL);
874     ok(!IsWindow(edit), "Expected valid handle\n");
875
876     /* trigger edit */
877     edit = (HWND)SendMessage(hTree, TVM_EDITLABEL, 0, (LPARAM)hRoot);
878     ok(IsWindow(edit), "Expected valid handle\n");
879     /* item shouldn't be selected automatically after TVM_EDITLABEL */
880     r = SendMessage(hTree, TVM_GETITEMSTATE, (WPARAM)hRoot, TVIS_SELECTED);
881     expect(0, r);
882     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
883     expect(0, r);
884     ok(!IsWindow(edit), "Expected edit control to be destroyed\n");
885
886     /* remove selection after starting edit */
887     r = TreeView_SelectItem(hTree, hRoot);
888     expect(TRUE, r);
889     edit = (HWND)SendMessage(hTree, TVM_EDITLABEL, 0, (LPARAM)hRoot);
890     ok(IsWindow(edit), "Expected valid handle\n");
891     r = TreeView_SelectItem(hTree, NULL);
892     expect(TRUE, r);
893     /* alter text */
894     strncpy(buff, "x", sizeof(buff)/sizeof(CHAR));
895     r = SendMessage(edit, WM_SETTEXT, 0, (LPARAM)buff);
896     expect(TRUE, r);
897     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
898     expect(0, r);
899     ok(!IsWindow(edit), "Expected edit control to be destroyed\n");
900     /* check that text is saved */
901     item.mask = TVIF_TEXT;
902     item.hItem = hRoot;
903     item.pszText = buff;
904     item.cchTextMax = sizeof(buff)/sizeof(CHAR);
905     r = SendMessage(hTree, TVM_GETITEM, 0, (LPARAM)&item);
906     expect(TRUE, r);
907     ok(!strcmp("x", buff), "Expected item text to change\n");
908
909     DestroyWindow(hTree);
910 }
911
912 START_TEST(treeview)
913 {
914     HMODULE hComctl32;
915     BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
916     WNDCLASSA wc;
917     MSG msg;
918   
919     hComctl32 = GetModuleHandleA("comctl32.dll");
920     pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
921     if (pInitCommonControlsEx)
922     {
923         INITCOMMONCONTROLSEX iccex;
924         iccex.dwSize = sizeof(iccex);
925         iccex.dwICC  = ICC_TREEVIEW_CLASSES;
926         pInitCommonControlsEx(&iccex);
927     }
928     else
929         InitCommonControls();
930
931     init_msg_sequences(MsgSequences, NUM_MSG_SEQUENCES);
932   
933     wc.style = CS_HREDRAW | CS_VREDRAW;
934     wc.cbClsExtra = 0;
935     wc.cbWndExtra = 0;
936     wc.hInstance = GetModuleHandleA(NULL);
937     wc.hIcon = NULL;
938     wc.hCursor = LoadCursorA(NULL, IDC_IBEAM);
939     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
940     wc.lpszMenuName = NULL;
941     wc.lpszClassName = "MyTestWnd";
942     wc.lpfnWndProc = MyWndProc;
943     RegisterClassA(&wc);
944
945
946     hMainWnd = CreateWindowExA(0, "MyTestWnd", "Blah", WS_OVERLAPPEDWINDOW,
947       CW_USEDEFAULT, CW_USEDEFAULT, 130, 105, NULL, NULL, GetModuleHandleA(NULL), 0);
948
949     if ( !ok(hMainWnd != NULL, "Failed to create parent window. Tests aborted.\n") )
950         return;
951
952     test_fillroot();
953     test_select();
954     test_getitemtext();
955     test_focus();
956     test_getset();
957     test_callback();
958     test_expandinvisible();
959     test_itemedit();
960
961     PostMessageA(hMainWnd, WM_CLOSE, 0, 0);
962     while(GetMessageA(&msg,0,0,0)) {
963         TranslateMessage(&msg);
964         DispatchMessageA(&msg);
965     }
966 }