comctl32/tests: Write-strings warnings fix.
[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     static CHAR root[]  = "Root",
74                 child[] = "Child";
75
76     Clear();
77     AddItem('A');
78     ins.hParent = TVI_ROOT;
79     ins.hInsertAfter = TVI_ROOT;
80     U(ins).item.mask = TVIF_TEXT;
81     U(ins).item.pszText = root;
82     hRoot = TreeView_InsertItem(hTree, &ins);
83     assert(hRoot);
84
85     AddItem('B');
86     ins.hParent = hRoot;
87     ins.hInsertAfter = TVI_FIRST;
88     U(ins).item.mask = TVIF_TEXT;
89     U(ins).item.pszText = child;
90     hChild = TreeView_InsertItem(hTree, &ins);
91     assert(hChild);
92     AddItem('.');
93
94     ok(!strcmp(sequence, "AB."), "Item creation\n");
95 }
96
97 static void DoTest1(void)
98 {
99     BOOL r;
100     r = TreeView_SelectItem(hTree, NULL);
101     Clear();
102     AddItem('1');
103     r = TreeView_SelectItem(hTree, hRoot);
104     AddItem('2');
105     r = TreeView_SelectItem(hTree, hRoot);
106     AddItem('3');
107     r = TreeView_SelectItem(hTree, NULL);
108     AddItem('4');
109     r = TreeView_SelectItem(hTree, NULL);
110     AddItem('5');
111     r = TreeView_SelectItem(hTree, hRoot);
112     AddItem('.');
113     ok(!strcmp(sequence, "1(nR)nR23(Rn)Rn45(nR)nR."), "root-none select test\n");
114 }
115
116 static void DoTest2(void)
117 {
118     BOOL r;
119     r = TreeView_SelectItem(hTree, NULL);
120     Clear();
121     AddItem('1');
122     r = TreeView_SelectItem(hTree, hRoot);
123     AddItem('2');
124     r = TreeView_SelectItem(hTree, hRoot);
125     AddItem('3');
126     r = TreeView_SelectItem(hTree, hChild);
127     AddItem('4');
128     r = TreeView_SelectItem(hTree, hChild);
129     AddItem('5');
130     r = TreeView_SelectItem(hTree, hRoot);
131     AddItem('.');
132     ok(!strcmp(sequence, "1(nR)nR23(RC)RC45(CR)CR."), "root-child select test\n");
133 }
134
135 LRESULT CALLBACK MyWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
136 {
137     switch(msg) {
138                   
139     case WM_CREATE:
140     {
141         hTree = CreateWindowExA(WS_EX_CLIENTEDGE, WC_TREEVIEWA, NULL, WS_CHILD|WS_VISIBLE|
142             TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS, 
143             0, 0, 300, 50, hWnd, (HMENU)100, GetModuleHandleA(0), 0);
144     
145         SetFocus(hTree);
146         return 0;
147     }
148     case WM_NOTIFY:
149     {
150         NMHDR *pHdr = (NMHDR *)lParam;
151     
152         if (pHdr->idFrom == 100) {
153             NMTREEVIEWA *pTreeView = (LPNMTREEVIEWA) lParam;
154             switch(pHdr->code) {
155             case TVN_SELCHANGINGA:
156                 AddItem('(');
157                 IdentifyItem(pTreeView->itemOld.hItem);
158                 IdentifyItem(pTreeView->itemNew.hItem);
159                 return 0;
160             case TVN_SELCHANGEDA:
161                 AddItem(')');
162                 IdentifyItem(pTreeView->itemOld.hItem);
163                 IdentifyItem(pTreeView->itemNew.hItem);
164                 return 0;
165             }
166         }
167         return 0;
168     }
169   
170     case WM_SIZE:
171         MoveWindow(hTree, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
172         break;
173       
174     case WM_DESTROY:
175         PostQuitMessage(0);
176         break;
177   
178     default:
179         return DefWindowProcA(hWnd, msg, wParam, lParam);
180     }
181     return 0L;
182 }
183
184 START_TEST(treeview)
185 {
186     WNDCLASSA wc;
187     MSG msg;
188     INITCOMMONCONTROLSEX icex;
189     RECT rc;
190   
191     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
192     icex.dwICC   = ICC_TREEVIEW_CLASSES;
193     InitCommonControlsEx(&icex);
194   
195     wc.style = CS_HREDRAW | CS_VREDRAW;
196     wc.cbClsExtra = 0;
197     wc.cbWndExtra = 0;
198     wc.hInstance = GetModuleHandleA(NULL);
199     wc.hIcon = NULL;
200     wc.hCursor = LoadCursorA(NULL, MAKEINTRESOURCEA(IDC_IBEAM));
201     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
202     wc.lpszMenuName = NULL;
203     wc.lpszClassName = "MyTestWnd";
204     wc.lpfnWndProc = MyWndProc;
205     RegisterClassA(&wc);
206     
207     
208     hMainWnd = CreateWindowExA(0, "MyTestWnd", "Blah", WS_OVERLAPPEDWINDOW, 
209       CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, GetModuleHandleA(NULL), 0);
210     GetClientRect(hMainWnd, &rc);
211
212     FillRoot();
213     DoTest1();
214     DoTest2();
215
216     PostMessageA(hMainWnd, WM_CLOSE, 0, 0);
217     while(GetMessageA(&msg,0,0,0)) {
218         TranslateMessage(&msg);
219         DispatchMessageA(&msg);
220     }
221 }