msi: Only insert entries into listbox if property value matches.
[wine] / dlls / comctl32 / tests / treeview.c
1 /* Unit tests for treeview.
2  *
3  * Copyright 2005 Krzysztof Foltman
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include <assert.h>
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "winnls.h"
28 #include "winreg.h"
29 #include "commctrl.h" 
30
31 #include "wine/test.h"
32  
33 static HWND hMainWnd;
34
35 static HWND hTree;
36 static HTREEITEM hRoot, hChild;
37
38 static int pos = 0;
39 static char sequence[256];
40
41 static void Clear(void)
42 {
43     pos = 0;
44     sequence[0] = '\0';
45 }
46
47 static void AddItem(char ch)
48 {
49     sequence[pos++] = ch;
50     sequence[pos] = '\0';
51 }
52
53 static void IdentifyItem(HTREEITEM hItem)
54 {
55     if (hItem == hRoot) {
56         AddItem('R');
57         return;
58     }
59     if (hItem == hChild) {
60         AddItem('C');
61         return;
62     }
63     if (hItem == NULL) {
64         AddItem('n');
65         return;
66     }
67     AddItem('?');
68 }
69
70 static void FillRoot(void)
71 {
72     TVINSERTSTRUCTA ins;
73     TVITEM tvi;
74     static CHAR root[]  = "Root",
75                 child[] = "Child";
76
77     Clear();
78     AddItem('A');
79     ins.hParent = TVI_ROOT;
80     ins.hInsertAfter = TVI_ROOT;
81     U(ins).item.mask = TVIF_TEXT;
82     U(ins).item.pszText = root;
83     hRoot = TreeView_InsertItem(hTree, &ins);
84     assert(hRoot);
85
86     /* UMLPad 1.15 depends on this being not -1 (I_IMAGECALLBACK) */
87     tvi.hItem = hRoot;
88     tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
89     SendMessage( hTree, TVM_GETITEM, 0, (LPARAM)&tvi );
90     ok(tvi.iImage == 0, "tvi.iImage=%d\n", tvi.iImage);
91     ok(tvi.iSelectedImage == 0, "tvi.iSelectedImage=%d\n", tvi.iSelectedImage);
92
93     AddItem('B');
94     ins.hParent = hRoot;
95     ins.hInsertAfter = TVI_FIRST;
96     U(ins).item.mask = TVIF_TEXT;
97     U(ins).item.pszText = child;
98     hChild = TreeView_InsertItem(hTree, &ins);
99     assert(hChild);
100     AddItem('.');
101
102     ok(!strcmp(sequence, "AB."), "Item creation\n");
103 }
104
105 static void DoTest1(void)
106 {
107     BOOL r;
108     r = TreeView_SelectItem(hTree, NULL);
109     Clear();
110     AddItem('1');
111     r = TreeView_SelectItem(hTree, hRoot);
112     AddItem('2');
113     r = TreeView_SelectItem(hTree, hRoot);
114     AddItem('3');
115     r = TreeView_SelectItem(hTree, NULL);
116     AddItem('4');
117     r = TreeView_SelectItem(hTree, NULL);
118     AddItem('5');
119     r = TreeView_SelectItem(hTree, hRoot);
120     AddItem('.');
121     ok(!strcmp(sequence, "1(nR)nR23(Rn)Rn45(nR)nR."), "root-none select test\n");
122 }
123
124 static void DoTest2(void)
125 {
126     BOOL r;
127     r = TreeView_SelectItem(hTree, NULL);
128     Clear();
129     AddItem('1');
130     r = TreeView_SelectItem(hTree, hRoot);
131     AddItem('2');
132     r = TreeView_SelectItem(hTree, hRoot);
133     AddItem('3');
134     r = TreeView_SelectItem(hTree, hChild);
135     AddItem('4');
136     r = TreeView_SelectItem(hTree, hChild);
137     AddItem('5');
138     r = TreeView_SelectItem(hTree, hRoot);
139     AddItem('.');
140     ok(!strcmp(sequence, "1(nR)nR23(RC)RC45(CR)CR."), "root-child select test\n");
141 }
142
143 static LRESULT CALLBACK MyWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
144 {
145     switch(msg) {
146
147     case WM_CREATE:
148     {
149         hTree = CreateWindowExA(WS_EX_CLIENTEDGE, WC_TREEVIEWA, NULL, WS_CHILD|WS_VISIBLE|
150             TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS, 
151             0, 0, 300, 50, hWnd, (HMENU)100, GetModuleHandleA(0), 0);
152     
153         SetFocus(hTree);
154         return 0;
155     }
156     case WM_NOTIFY:
157     {
158         NMHDR *pHdr = (NMHDR *)lParam;
159     
160         if (pHdr->idFrom == 100) {
161             NMTREEVIEWA *pTreeView = (LPNMTREEVIEWA) lParam;
162             switch(pHdr->code) {
163             case TVN_SELCHANGINGA:
164                 AddItem('(');
165                 IdentifyItem(pTreeView->itemOld.hItem);
166                 IdentifyItem(pTreeView->itemNew.hItem);
167                 return 0;
168             case TVN_SELCHANGEDA:
169                 AddItem(')');
170                 IdentifyItem(pTreeView->itemOld.hItem);
171                 IdentifyItem(pTreeView->itemNew.hItem);
172                 return 0;
173             }
174         }
175         return 0;
176     }
177   
178     case WM_SIZE:
179         MoveWindow(hTree, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
180         break;
181       
182     case WM_DESTROY:
183         PostQuitMessage(0);
184         break;
185   
186     default:
187         return DefWindowProcA(hWnd, msg, wParam, lParam);
188     }
189     return 0L;
190 }
191
192 START_TEST(treeview)
193 {
194     WNDCLASSA wc;
195     MSG msg;
196     INITCOMMONCONTROLSEX icex;
197     RECT rc;
198   
199     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
200     icex.dwICC   = ICC_TREEVIEW_CLASSES;
201     InitCommonControlsEx(&icex);
202   
203     wc.style = CS_HREDRAW | CS_VREDRAW;
204     wc.cbClsExtra = 0;
205     wc.cbWndExtra = 0;
206     wc.hInstance = GetModuleHandleA(NULL);
207     wc.hIcon = NULL;
208     wc.hCursor = LoadCursorA(NULL, MAKEINTRESOURCEA(IDC_IBEAM));
209     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
210     wc.lpszMenuName = NULL;
211     wc.lpszClassName = "MyTestWnd";
212     wc.lpfnWndProc = MyWndProc;
213     RegisterClassA(&wc);
214     
215     
216     hMainWnd = CreateWindowExA(0, "MyTestWnd", "Blah", WS_OVERLAPPEDWINDOW, 
217       CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, GetModuleHandleA(NULL), 0);
218     GetClientRect(hMainWnd, &rc);
219
220     FillRoot();
221     DoTest1();
222     DoTest2();
223
224     PostMessageA(hMainWnd, WM_CLOSE, 0, 0);
225     while(GetMessageA(&msg,0,0,0)) {
226         TranslateMessage(&msg);
227         DispatchMessageA(&msg);
228     }
229 }