mfreadwrite/reader: Add missing allocation check (Coverity).
[wine/zf.git] / dlls / comctl32 / tests / static.c
blob4efb7685790af5312a8fb98f6c7e813c9ad0cbdf
1 /* Unit test suite for static controls.
3 * Copyright 2007 Google (Mikolaj Zalewski)
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.
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.
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
20 #include <stdarg.h>
21 #include <stdio.h>
23 #define STRICT
24 #define WIN32_LEAN_AND_MEAN
25 #include <windows.h>
26 #include "commctrl.h"
27 #include "resources.h"
29 #include "wine/test.h"
31 #include "v6util.h"
33 #define TODO_COUNT 1
35 #define CTRL_ID 1995
37 static HWND hMainWnd;
38 static int g_nReceivedColorStatic;
40 /* try to make sure pending X events have been processed before continuing */
41 static void flush_events(void)
43 MSG msg;
44 int diff = 200;
45 int min_timeout = 100;
46 DWORD time = GetTickCount() + diff;
48 while (diff > 0)
50 if (MsgWaitForMultipleObjects( 0, NULL, FALSE, min_timeout, QS_ALLINPUT ) == WAIT_TIMEOUT) break;
51 while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageA( &msg );
52 diff = time - GetTickCount();
56 static HWND create_static(DWORD style)
58 return CreateWindowA(WC_STATICA, "Test", WS_VISIBLE|WS_CHILD|style, 5, 5, 100, 100, hMainWnd, (HMENU)CTRL_ID, NULL, 0);
61 static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
63 switch (msg)
65 case WM_CTLCOLORSTATIC:
67 HDC hdc = (HDC)wparam;
68 HRGN hrgn = CreateRectRgn(0, 0, 1, 1);
69 ok(GetClipRgn(hdc, hrgn) == 1, "Static controls during a WM_CTLCOLORSTATIC must have a clipping region\n");
70 DeleteObject(hrgn);
71 g_nReceivedColorStatic++;
72 return (LRESULT) GetStockObject(BLACK_BRUSH);
74 break;
77 return DefWindowProcA(hwnd, msg, wparam, lparam);
80 static void test_updates(int style, int flags)
82 HWND hStatic = create_static(style);
83 RECT r1 = {20, 20, 30, 30};
84 int exp;
86 flush_events();
87 g_nReceivedColorStatic = 0;
88 /* during each update parent WndProc will test the WM_CTLCOLORSTATIC message */
89 InvalidateRect(hMainWnd, NULL, FALSE);
90 UpdateWindow(hMainWnd);
91 InvalidateRect(hMainWnd, &r1, FALSE);
92 UpdateWindow(hMainWnd);
93 InvalidateRect(hStatic, &r1, FALSE);
94 UpdateWindow(hStatic);
95 InvalidateRect(hStatic, NULL, FALSE);
96 UpdateWindow(hStatic);
98 if ((style & SS_TYPEMASK) == SS_BITMAP)
100 HDC hdc = GetDC(hStatic);
101 COLORREF colour = GetPixel(hdc, 10, 10);
102 todo_wine
103 ok(colour == 0, "Unexpected pixel color.\n");
104 ReleaseDC(hStatic, hdc);
107 if (style != SS_ETCHEDHORZ && style != SS_ETCHEDVERT)
108 exp = 4;
109 else
110 exp = 1; /* SS_ETCHED* seems to send WM_CTLCOLORSTATIC only sometimes */
112 if (flags & TODO_COUNT)
113 todo_wine
114 ok(g_nReceivedColorStatic == exp, "Unexpected WM_CTLCOLORSTATIC value %d\n", g_nReceivedColorStatic);
115 else if ((style & SS_TYPEMASK) == SS_ICON || (style & SS_TYPEMASK) == SS_BITMAP)
116 ok(g_nReceivedColorStatic == exp, "Unexpected %u got %u\n", exp, g_nReceivedColorStatic);
117 else
118 ok(g_nReceivedColorStatic == exp, "Unexpected WM_CTLCOLORSTATIC value %d\n", g_nReceivedColorStatic);
119 DestroyWindow(hStatic);
122 static void test_set_text(void)
124 HWND hStatic = create_static(SS_SIMPLE);
125 char buffA[10];
127 GetWindowTextA(hStatic, buffA, sizeof(buffA));
128 ok(!strcmp(buffA, "Test"), "got wrong text %s\n", buffA);
130 SetWindowTextA(hStatic, NULL);
131 GetWindowTextA(hStatic, buffA, sizeof(buffA));
132 ok(buffA[0] == 0, "got wrong text %s\n", buffA);
134 DestroyWindow(hStatic);
137 static void remove_alpha(HBITMAP hbitmap)
139 BITMAP bm;
140 BYTE *ptr;
141 int i;
143 GetObjectW(hbitmap, sizeof(bm), &bm);
144 ok(bm.bmBits != NULL, "bmBits is NULL\n");
146 for (i = 0, ptr = bm.bmBits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
147 ptr[3] = 0;
150 static void test_image(HBITMAP image, BOOL is_dib, BOOL is_premult, BOOL is_alpha)
152 BITMAP bm;
153 HDC hdc;
154 BITMAPINFO info;
155 BYTE bits[4];
157 GetObjectW(image, sizeof(bm), &bm);
158 ok(bm.bmWidth == 1, "got %d\n", bm.bmWidth);
159 ok(bm.bmHeight == 1, "got %d\n", bm.bmHeight);
160 ok(bm.bmBitsPixel == 32, "got %d\n", bm.bmBitsPixel);
161 if (is_dib)
163 ok(bm.bmBits != NULL, "bmBits is NULL\n");
164 memcpy(bits, bm.bmBits, 4);
165 if (is_premult)
166 todo_wine
167 ok(bits[0] == 0x05 && bits[1] == 0x09 && bits[2] == 0x0e && bits[3] == 0x44,
168 "bits: %02x %02x %02x %02x\n", bits[0], bits[1], bits[2], bits[3]);
169 else if (is_alpha)
170 ok(bits[0] == 0x11 && bits[1] == 0x22 && bits[2] == 0x33 && bits[3] == 0x44,
171 "bits: %02x %02x %02x %02x\n", bits[0], bits[1], bits[2], bits[3]);
172 else
173 ok(bits[0] == 0x11 && bits[1] == 0x22 && bits[2] == 0x33 && bits[3] == 0,
174 "bits: %02x %02x %02x %02x\n", bits[0], bits[1], bits[2], bits[3]);
176 else
177 ok(bm.bmBits == NULL, "bmBits is not NULL\n");
179 info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
180 info.bmiHeader.biWidth = bm.bmWidth;
181 info.bmiHeader.biHeight = bm.bmHeight;
182 info.bmiHeader.biPlanes = 1;
183 info.bmiHeader.biBitCount = 32;
184 info.bmiHeader.biCompression = BI_RGB;
185 info.bmiHeader.biSizeImage = 4;
186 info.bmiHeader.biXPelsPerMeter = 0;
187 info.bmiHeader.biYPelsPerMeter = 0;
188 info.bmiHeader.biClrUsed = 0;
189 info.bmiHeader.biClrImportant = 0;
191 hdc = CreateCompatibleDC(0);
192 GetDIBits(hdc, image, 0, bm.bmHeight, bits, &info, DIB_RGB_COLORS);
193 DeleteDC(hdc);
195 if (is_premult)
196 todo_wine
197 ok(bits[0] == 0x05 && bits[1] == 0x09 && bits[2] == 0x0e && bits[3] == 0x44,
198 "bits: %02x %02x %02x %02x\n", bits[0], bits[1], bits[2], bits[3]);
199 else if (is_alpha)
200 ok(bits[0] == 0x11 && bits[1] == 0x22 && bits[2] == 0x33 && bits[3] == 0x44,
201 "bits: %02x %02x %02x %02x\n", bits[0], bits[1], bits[2], bits[3]);
202 else
203 ok(bits[0] == 0x11 && bits[1] == 0x22 && bits[2] == 0x33 && bits[3] == 0,
204 "bits: %02x %02x %02x %02x\n", bits[0], bits[1], bits[2], bits[3]);
207 static void test_set_image(void)
209 HWND hwnd = create_static(SS_BITMAP);
210 HBITMAP bmp1, bmp2, image;
212 image = LoadImageW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(IDB_BITMAP_1x1_32BPP), IMAGE_BITMAP, 0, 0, 0);
213 ok(image != NULL, "LoadImage failed\n");
215 test_image(image, FALSE, FALSE, TRUE);
217 bmp1 = (HBITMAP)SendMessageW(hwnd, STM_GETIMAGE, IMAGE_BITMAP, 0);
218 ok(bmp1 == NULL, "got not NULL\n");
220 bmp1 = (HBITMAP)SendMessageW(hwnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)image);
221 ok(bmp1 == NULL, "got not NULL\n");
223 bmp1 = (HBITMAP)SendMessageW(hwnd, STM_GETIMAGE, IMAGE_BITMAP, 0);
224 ok(bmp1 != NULL, "got NULL\n");
225 ok(bmp1 != image, "bmp == image\n");
226 test_image(bmp1, TRUE, TRUE, TRUE);
228 bmp2 = (HBITMAP)SendMessageW(hwnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)image);
229 ok(bmp2 != NULL, "got NULL\n");
230 ok(bmp2 != image, "bmp == image\n");
231 ok(bmp1 == bmp2, "bmp1 != bmp2\n");
232 test_image(bmp2, TRUE, TRUE, TRUE);
234 DeleteObject(image);
236 image = LoadImageW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(IDB_BITMAP_1x1_32BPP), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
237 ok(image != NULL, "LoadImage failed\n");
239 test_image(image, TRUE, FALSE, TRUE);
240 remove_alpha(image);
241 test_image(image, TRUE, FALSE, FALSE);
243 bmp1 = (HBITMAP)SendMessageW(hwnd, STM_GETIMAGE, IMAGE_BITMAP, 0);
244 ok(bmp1 != NULL, "got NULL\n");
245 ok(bmp1 != image, "bmp == image\n");
246 test_image(bmp1, TRUE, TRUE, TRUE);
248 bmp2 = (HBITMAP)SendMessageW(hwnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)image);
249 ok(bmp2 != NULL, "got NULL\n");
250 ok(bmp2 != image, "bmp == image\n");
251 ok(bmp1 == bmp2, "bmp1 != bmp2\n");
252 test_image(bmp1, TRUE, TRUE, FALSE);
254 bmp2 = (HBITMAP)SendMessageW(hwnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)image);
255 ok(bmp2 != NULL, "got NULL\n");
256 ok(bmp2 == image, "bmp1 != image\n");
257 test_image(bmp2, TRUE, FALSE, FALSE);
259 DestroyWindow(hwnd);
261 test_image(image, TRUE, FALSE, FALSE);
263 DeleteObject(image);
266 START_TEST(static)
268 static const char classname[] = "testclass";
269 WNDCLASSEXA wndclass;
270 ULONG_PTR ctx_cookie;
271 HANDLE hCtx;
273 if (!load_v6_module(&ctx_cookie, &hCtx))
274 return;
276 wndclass.cbSize = sizeof(wndclass);
277 wndclass.style = CS_HREDRAW | CS_VREDRAW;
278 wndclass.lpfnWndProc = WndProc;
279 wndclass.cbClsExtra = 0;
280 wndclass.cbWndExtra = 0;
281 wndclass.hInstance = GetModuleHandleA(NULL);
282 wndclass.hIcon = LoadIconA(NULL, (LPCSTR)IDI_APPLICATION);
283 wndclass.hIconSm = LoadIconA(NULL, (LPCSTR)IDI_APPLICATION);
284 wndclass.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
285 wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
286 wndclass.lpszClassName = classname;
287 wndclass.lpszMenuName = NULL;
288 RegisterClassExA(&wndclass);
290 hMainWnd = CreateWindowA(classname, "Test", WS_OVERLAPPEDWINDOW, 0, 0, 500, 500, NULL, NULL,
291 GetModuleHandleA(NULL), NULL);
292 ShowWindow(hMainWnd, SW_SHOW);
294 test_updates(0, 0);
295 test_updates(SS_SIMPLE, 0);
296 test_updates(SS_ICON, 0);
297 test_updates(SS_BITMAP, 0);
298 test_updates(SS_BITMAP | SS_CENTERIMAGE, 0);
299 test_updates(SS_BLACKRECT, TODO_COUNT);
300 test_updates(SS_WHITERECT, TODO_COUNT);
301 test_updates(SS_ETCHEDHORZ, TODO_COUNT);
302 test_updates(SS_ETCHEDVERT, TODO_COUNT);
303 test_set_text();
304 test_set_image();
306 DestroyWindow(hMainWnd);
308 unload_v6_module(ctx_cookie, hCtx);