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