[USER32_WINETEST]
[reactos.git] / rostests / winetests / user32 / static.c
1 /* Unit test suite for static controls.
2 *
3 * Copyright 2007 Google (Mikolaj Zalewski)
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 <stdarg.h>
21 #include <stdio.h>
22
23 #define STRICT
24 #define WIN32_LEAN_AND_MEAN
25 #include <windows.h>
26
27 #include "wine/test.h"
28
29 #define TODO_COUNT 1
30
31 #define CTRL_ID 1995
32
33 static HWND hMainWnd;
34
35 #define expect_eq(expr, value, type, fmt) { type val = expr; ok(val == (value), #expr " expected " fmt " got " fmt "\n", (value), val); }
36 #define expect_rect(r, _left, _top, _right, _bottom) ok(r.left == _left && r.top == _top && \
37 r.bottom == _bottom && r.right == _right, "Invalid rect (%d,%d) (%d,%d) vs (%d,%d) (%d,%d)\n", \
38 r.left, r.top, r.right, r.bottom, _left, _top, _right, _bottom);
39
40 static int g_nReceivedColorStatic = 0;
41
42 /* try to make sure pending X events have been processed before continuing */
43 static void flush_events(void)
44 {
45 MSG msg;
46 int diff = 200;
47 int min_timeout = 100;
48 DWORD time = GetTickCount() + diff;
49
50 while (diff > 0)
51 {
52 if (MsgWaitForMultipleObjects( 0, NULL, FALSE, min_timeout, QS_ALLINPUT ) == WAIT_TIMEOUT) break;
53 while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageA( &msg );
54 diff = time - GetTickCount();
55 }
56 }
57
58 static HWND build_static(DWORD style)
59 {
60 return CreateWindowA("static", "Test", WS_VISIBLE|WS_CHILD|style, 5, 5, 100, 100, hMainWnd, (HMENU)CTRL_ID, NULL, 0);
61 }
62
63 static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
64 {
65 switch (msg)
66 {
67 case WM_CTLCOLORSTATIC:
68 {
69 HDC hdc = (HDC)wparam;
70 HRGN hrgn = CreateRectRgn(0, 0, 1, 1);
71 ok(GetClipRgn(hdc, hrgn) == 1, "Static controls during a WM_CTLCOLORSTATIC must have a clipping region\n");
72 DeleteObject(hrgn);
73 g_nReceivedColorStatic++;
74 return (LRESULT) GetStockObject(BLACK_BRUSH);
75 }
76 break;
77 }
78
79 return DefWindowProcA(hwnd, msg, wparam, lparam);
80 }
81
82 static void test_updates(int style, int flags)
83 {
84 RECT r1 = {20, 20, 30, 30};
85 HWND hStatic = build_static(style);
86 int exp;
87
88 flush_events();
89 trace("Testing style 0x%x\n", style);
90 g_nReceivedColorStatic = 0;
91 /* during each update parent WndProc will test the WM_CTLCOLORSTATIC message */
92 InvalidateRect(hMainWnd, NULL, FALSE);
93 UpdateWindow(hMainWnd);
94 InvalidateRect(hMainWnd, &r1, FALSE);
95 UpdateWindow(hMainWnd);
96 InvalidateRect(hStatic, &r1, FALSE);
97 UpdateWindow(hStatic);
98 InvalidateRect(hStatic, NULL, FALSE);
99 UpdateWindow(hStatic);
100
101 if( (style & SS_TYPEMASK) == SS_BITMAP) {
102 HDC hdc = GetDC( hStatic);
103 COLORREF colour = GetPixel( hdc, 10, 10);
104 ok ( colour != 0, "pixel should NOT be painted black!\n");
105 }
106 if (style != SS_ETCHEDHORZ && style != SS_ETCHEDVERT)
107 exp = 4;
108 else
109 exp = 1; /* SS_ETCHED* seems to send WM_CTLCOLORSTATIC only sometimes */
110
111 if (flags & TODO_COUNT)
112 todo_wine { expect_eq(g_nReceivedColorStatic, exp, int, "%d"); }
113 else if ((style & SS_TYPEMASK) == SS_ICON || (style & SS_TYPEMASK) == SS_BITMAP)
114 ok( g_nReceivedColorStatic == exp, "expected %u got %u\n", exp, g_nReceivedColorStatic );
115 else
116 expect_eq(g_nReceivedColorStatic, exp, int, "%d");
117 DestroyWindow(hStatic);
118 }
119
120 START_TEST(static)
121 {
122 static const char szClassName[] = "testclass";
123 WNDCLASSEXA wndclass;
124
125 wndclass.cbSize = sizeof(wndclass);
126 wndclass.style = CS_HREDRAW | CS_VREDRAW;
127 wndclass.lpfnWndProc = WndProc;
128 wndclass.cbClsExtra = 0;
129 wndclass.cbWndExtra = 0;
130 wndclass.hInstance = GetModuleHandleA(NULL);
131 wndclass.hIcon = LoadIconA(NULL, (LPCSTR)IDI_APPLICATION);
132 wndclass.hIconSm = LoadIconA(NULL, (LPCSTR)IDI_APPLICATION);
133 wndclass.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
134 wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
135 wndclass.lpszClassName = szClassName;
136 wndclass.lpszMenuName = NULL;
137 RegisterClassExA(&wndclass);
138
139 hMainWnd = CreateWindowA(szClassName, "Test", WS_OVERLAPPEDWINDOW, 0, 0, 500, 500, NULL, NULL, GetModuleHandleA(NULL), NULL);
140 ShowWindow(hMainWnd, SW_SHOW);
141
142 test_updates(0, 0);
143 test_updates(SS_SIMPLE, 0);
144 test_updates(SS_ICON, 0);
145 test_updates(SS_BITMAP, 0);
146 test_updates(SS_BITMAP | SS_CENTERIMAGE, 0);
147 test_updates(SS_BLACKRECT, TODO_COUNT);
148 test_updates(SS_WHITERECT, TODO_COUNT);
149 test_updates(SS_ETCHEDHORZ, TODO_COUNT);
150 test_updates(SS_ETCHEDVERT, TODO_COUNT);
151
152 DestroyWindow(hMainWnd);
153 }