Added header control tests.
[wine] / dlls / comctl32 / tests / header.c
1 /* Unit test suite for header control.
2  *
3  * Copyright 2005 Vijay Kiran Kamuju 
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20
21 #include <windows.h>
22 #include <commctrl.h>
23 #include <assert.h>
24
25 #include "wine/test.h"
26
27 static HWND hHeaderParentWnd;
28 #define MAX_CHARS 100
29
30 static LONG addItem(HWND hdex, int idx, LPCSTR text)
31 {
32     HDITEMA hdItem;
33     hdItem.mask       = HDI_TEXT | HDI_WIDTH;
34     hdItem.cxy        = 100;
35     hdItem.pszText    = (LPSTR)text;
36     hdItem.cchTextMax = 0;
37     return (LONG)SendMessage(hdex, HDM_INSERTITEMA, (WPARAM)idx, (LPARAM)&hdItem);
38 }
39
40 static LONG setItem(HWND hdex, int idx, LPCSTR text)
41 {
42     HDITEMA hdexItem;
43     hdexItem.mask       = HDI_TEXT;
44     hdexItem.pszText    = (LPSTR)text;
45     hdexItem.cchTextMax = 0;
46     return (LONG)SendMessage(hdex, HDM_SETITEMA, (WPARAM)idx, (LPARAM)&hdexItem);
47 }
48
49 static LONG delItem(HWND hdex, int idx)
50 {
51     return (LONG)SendMessage(hdex, HDM_DELETEITEM, (WPARAM)idx, 0);
52 }
53
54 static LONG getItemCount(HWND hdex)
55 {
56     return (LONG)SendMessage(hdex, HDM_GETITEMCOUNT, 0, 0);
57 }
58
59 static LONG getItem(HWND hdex, int idx, LPSTR textBuffer)
60 {
61     HDITEMA hdItem;
62     hdItem.mask         = HDI_TEXT;
63     hdItem.pszText      = textBuffer;
64     hdItem.cchTextMax   = MAX_CHARS;
65     return (LONG)SendMessage(hdex, HDM_GETITEMA, (WPARAM)idx, (LPARAM)&hdItem);
66 }
67
68 static HWND create_header_control (void)
69 {
70     HWND handle;
71     HDLAYOUT hlayout;
72     RECT rectwin;
73     WINDOWPOS winpos;
74
75     handle = CreateWindowEx(0, WC_HEADER, NULL,
76                             WS_CHILD|WS_BORDER|WS_VISIBLE|HDS_BUTTONS|HDS_HORZ,
77                             0, 0, 0, 0,
78                             hHeaderParentWnd, NULL, NULL, NULL);
79     assert(handle);
80
81     if (winetest_interactive)
82         ShowWindow (hHeaderParentWnd, SW_SHOW);
83
84     GetClientRect(hHeaderParentWnd,&rectwin);
85     hlayout.prc = &rectwin;
86     hlayout.pwpos = &winpos;
87     SendMessageA(handle,HDM_LAYOUT,0,(LPARAM) &hlayout);
88     SetWindowPos(handle, winpos.hwndInsertAfter, winpos.x, winpos.y, 
89                  winpos.cx, winpos.cy, 0);
90
91     return handle;
92 }
93
94 static const char *str_items[] =
95     {"First Item", "Second Item", "Third Item", "Fourth Item", "Replace Item", "Out Of Range Item"};
96
97 #define TEST_GET_ITEM(i,c)\
98 {   res = getItem(hWndHeader, i, buffer);\
99     ok(res != 0, "Getting item[%d] using valid index failed unexpectedly (%ld)\n", i, res);\
100     ok(strcmp(str_items[c], buffer) == 0, "Getting item[%d] returned \"%s\" expecting \"%s\"\n", i, buffer, str_items[c]);\
101 }
102
103 #define TEST_GET_ITEMCOUNT(i)\
104 {   res = getItemCount(hWndHeader);\
105     ok(res == i, "Got Item Count as %ld\n", res);\
106 }
107
108 static void test_header_control (void)
109 {
110     HWND hWndHeader;
111     LONG res;
112     static char buffer[MAX_CHARS];
113     int i;
114
115     hWndHeader = create_header_control ();
116
117     for (i = 3; i >= 0; i--)
118     {
119         TEST_GET_ITEMCOUNT(3-i);
120         res = addItem(hWndHeader, 0, str_items[i]);
121         ok(res == 0, "Adding simple item failed (%ld)\n", res);
122     }
123
124     TEST_GET_ITEMCOUNT(4);
125     res = addItem(hWndHeader, 99, str_items[i+1]);
126     ok(res != -1, "Adding Out of Range item should fail with -1 got (%ld)\n", res);
127     TEST_GET_ITEMCOUNT(5);
128     res = addItem(hWndHeader, 5, str_items[i+1]);
129     ok(res != -1, "Adding Out of Range item should fail with -1 got (%ld)\n", res);
130     TEST_GET_ITEMCOUNT(6);
131
132     for (i = 0; i < 4; i++) { TEST_GET_ITEM(i,i); TEST_GET_ITEMCOUNT(6); }
133
134     res=getItem(hWndHeader, 99, buffer);
135     ok(res == 0, "Getting Out of Range item should fail with 0 (%ld), got %s\n", res,buffer);
136     res=getItem(hWndHeader, 5, buffer);
137     ok(res == 1, "Getting Out of Range item should fail with 1 (%ld), got %s\n", res,buffer);
138     res=getItem(hWndHeader, -2, buffer);
139     ok(res == 0, "Getting Out of Range item should fail with 0 (%ld), got %s\n", res,buffer);
140
141     if (winetest_interactive)
142     {
143         UpdateWindow(hHeaderParentWnd);
144         UpdateWindow(hWndHeader);
145     }
146
147     TEST_GET_ITEMCOUNT(6);
148     res=setItem(hWndHeader, 99, str_items[5]);
149     ok(res == 0, "Setting Out of Range item should fail with 0 (%ld)\n", res);
150     res=setItem(hWndHeader, 5, str_items[5]);
151     ok(res == 1, "Setting Out of Range item should fail with 1 (%ld)\n", res);
152     res=setItem(hWndHeader, -2, str_items[5]);
153     ok(res == 0, "Setting Out of Range item should fail with 0 (%ld)\n", res);
154     TEST_GET_ITEMCOUNT(6);
155
156     for (i = 0; i < 4; i++)
157     {
158         res = setItem(hWndHeader, i, str_items[4]);
159         ok(res != 0, "Setting %d item failed (%ld)\n", i+1, res);
160         TEST_GET_ITEM(i, 4);
161         TEST_GET_ITEMCOUNT(6);
162     }
163
164     res = delItem(hWndHeader, 5);
165     ok(res == 1, "Deleting Out of Range item should fail with 1 (%ld)\n", res);
166     res = delItem(hWndHeader, -2);
167     ok(res == 0, "Deleting Out of Range item should fail with 0 (%ld)\n", res);
168     TEST_GET_ITEMCOUNT(5);
169
170     res = delItem(hWndHeader, 3);
171     ok(res != 0, "Deleting using out of range index failed (%ld)\n", res);
172     TEST_GET_ITEMCOUNT(4);
173     res = delItem(hWndHeader, 0);
174     ok(res != 0, "Deleting using out of range index failed (%ld)\n", res);
175     TEST_GET_ITEMCOUNT(3);
176     res = delItem(hWndHeader, 0);
177     ok(res != 0, "Deleting using out of range index failed (%ld)\n", res);
178     TEST_GET_ITEMCOUNT(2);
179     res = delItem(hWndHeader, 0);
180     ok(res != 0, "Deleting using out of range index failed (%ld)\n", res);
181     TEST_GET_ITEMCOUNT(1);
182
183     DestroyWindow(hWndHeader);
184 }
185
186 START_TEST(header)
187 {
188     INITCOMMONCONTROLSEX icex;
189
190     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
191     icex.dwICC  = ICC_USEREX_CLASSES;
192     InitCommonControlsEx(&icex);
193     hHeaderParentWnd = CreateWindowExA(0, "static", "Header test", WS_OVERLAPPEDWINDOW,
194                         CW_USEDEFAULT, CW_USEDEFAULT, 480, 100, NULL, NULL, NULL, 0);
195     assert(hHeaderParentWnd != NULL);
196
197     test_header_control();
198
199     DestroyWindow(hHeaderParentWnd);
200 }