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