1 /* Unit test suite for user interface functions
2  *
3  * Copyright 2009 Nikolay Sivov
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 "precomp.h"
21 
22 static void test_FillRect(void)
23 {
24     HDC hdc, hdcmem;
25     DWORD bits[64];
26     HBITMAP hbmp, oldhbmp;
27     COLORREF col;
28     HBRUSH old_brush;
29     RECT r;
30 
31     /* fill bitmap data with white */
32     memset(bits, 0xff, sizeof(bits));
33 
34     hdc = GetDC(0);
35     ok( hdc != NULL, "CreateDC rets %p\n", hdc);
36     /* create a memory dc */
37     hdcmem = CreateCompatibleDC(hdc);
38     ok(hdcmem != NULL, "CreateCompatibleDC rets %p\n", hdcmem);
39     /* test monochrome bitmap: should always work */
40     hbmp = CreateBitmap(32, 32, 1, 1, bits);
41     ok(hbmp != NULL, "CreateBitmap returns %p\n", hbmp);
42     oldhbmp = SelectObject(hdcmem, hbmp);
43     ok(oldhbmp != NULL, "SelectObject returned NULL\n"); /* a memdc always has a bitmap selected */
44     col = GetPixel(hdcmem, 0, 0);
45     ok( col == 0xffffff, "GetPixel returned %08x, expected 0xffffff\n", col);
46 
47     /* select black brush */
48     old_brush = SelectObject(hdcmem, GetStockObject(BLACK_BRUSH));
49     SetRect(&r, 0, 0, 5, 5);
50     FillRect(hdcmem, &r, 0);
51     SelectObject(hdcmem, old_brush);
52     /* bitmap filled with last selected brush */
53     col = GetPixel(hdcmem, 0, 0);
54     ok(col == 0, "GetPixel returned %08x, expected 0\n", col);
55 
56     SelectObject(hdcmem, oldhbmp);
57     DeleteObject(hbmp);
58     DeleteDC(hdcmem);
59     ReleaseDC(0, hdc);
60 }
61 
62 static void test_SubtractRect(void)
63 {
64     RECT rect1;
65     RECT rect2;
66     RECT rectr;
67     BOOL result;
68 
69     /* source rectangles don't intersect */
70     SetRect(&rect1, 50, 50, 150, 100);
71     SetRect(&rect2, 250, 200, 1500, 1000);
72     result = SubtractRect(&rectr, &rect1, &rect2);
73     ok(result, "SubtractRect returned FALSE but subtraction should not be empty\n");
74     ok(result && rectr.left == 50 && rectr.top == 50 && rectr.right ==150
75        && rectr.bottom == 100, "wrong rect subtraction of SubtractRect (dest rect=%s)\n",
76        wine_dbgstr_rect(&rectr));
77 
78     /* source rect 2 partially overlaps rect 1 */
79     SetRect(&rect1, 2431, 626, 3427, 1608);
80     SetRect(&rect2, 2499, 626, 3427, 1608);
81     result = SubtractRect(&rectr, &rect1, &rect2);
82     ok(result, "SubtractRect returned FALSE but subtraction should not be empty\n");
83     ok(result && rectr.left == 2431 && rectr.top == 626 && rectr.right == 2499
84        && rectr.bottom == 1608, "wrong rect subtraction of SubtractRect (dest rect=%s)\n",
85        wine_dbgstr_rect(&rectr));
86 
87     /* source rect 2 partially overlaps rect 1 - dest is src rect 2 */
88     SetRect(&rect1, 2431, 626, 3427, 1608);
89     SetRect(&rect2, 2499, 626, 3427, 1608);
90     result = SubtractRect(&rect2, &rect1, &rect2);
91     ok(result, "SubtractRect returned FALSE but subtraction should not be empty\n");
92     ok(result && rectr.left == 2431 && rectr.top == 626 && rectr.right == 2499
93        && rectr.bottom == 1608, "wrong rect subtraction of SubtractRect (dest rect=%s)\n",
94        wine_dbgstr_rect(&rectr));
95 
96     /* source rect 2 completely overlaps rect 1 */
97     SetRect(&rect1, 250, 250, 400, 500);
98     SetRect(&rect2, 50, 50, 1500, 1000);
99     result = SubtractRect(&rectr, &rect1, &rect2);
100     ok(!result, "SubtractRect returned TRUE but subtraction should be empty (dest rect=%s)\n",
101        wine_dbgstr_rect(&rectr));
102 
103     /* source rect 2 completely overlaps rect 1 - dest is src rect 2 */
104     SetRect(&rect1, 250, 250, 400, 500);
105     SetRect(&rect2, 50, 50, 1500, 1000);
106     result = SubtractRect(&rect2, &rect1, &rect2);
107     ok(!result, "SubtractRect returned TRUE but subtraction should be empty (dest rect=%s)\n",
108        wine_dbgstr_rect(&rect2));
109 }
110 
111 static void test_EqualRect(void)
112 {
113     RECT rect1, rect2;
114     BOOL ret;
115 
116     SetRect(&rect1, 0, 0, 0, 0);
117     SetRect(&rect2, 1, 1, 1, 1);
118 
119     ret = EqualRect(NULL, NULL);
120     ok(!ret, "got %d\n", ret);
121 
122     ret = EqualRect(&rect1, NULL);
123     ok(!ret, "got %d\n", ret);
124 
125     ret = EqualRect(NULL, &rect2);
126     ok(!ret, "got %d\n", ret);
127 
128     ret = EqualRect(&rect1, &rect2);
129     ok(!ret, "got %d\n", ret);
130 
131     SetRect(&rect1, 0, 0, 10, 10);
132     SetRect(&rect2, 10, 10, 0, 0);
133 
134     ret = EqualRect(&rect1, &rect2);
135     ok(!ret, "got %d\n", ret);
136 
137     ret = EqualRect(&rect1, &rect1);
138     ok(ret, "got %d\n", ret);
139 
140     rect2 = rect1;
141     ret = EqualRect(&rect1, &rect2);
142     ok(ret, "got %d\n", ret);
143 }
144 
145 static void test_IsRectEmpty(void)
146 {
147     BOOL ret;
148     unsigned int i;
149     static const struct {
150         RECT rect;
151         BOOL ret;
152     } rtest[] = {
153         {{0, 0, 0, 0}, TRUE},
154         {{127, 131, 127, 131}, TRUE},
155         {{MAXLONG, MAXLONG, MAXLONG, MAXLONG}, TRUE},
156         {{-1, -1, -1, -1}, TRUE},
157         {{-2011, -2017, -2011, -2017}, TRUE},
158         {{MINLONG, MINLONG, MINLONG, MINLONG}, TRUE},
159         /* Only width or height are 0 */
160         {{31, 37, 31, 41}, TRUE},
161         {{881, 883, 887, 883}, TRUE},
162         {{-1721, 1723, -1721, 7213}, TRUE},
163         /* Negative width and/or height */
164         {{11, 13, 5, 7}, TRUE},
165         {{-11, -13, -19, -23}, TRUE},
166         {{11, 13, -17, 19}, TRUE},
167         {{11, 13, 17, 11}, TRUE},
168         /* Non empty rects */
169         {{101, 103, 107, 109}, FALSE},
170         {{1, -9, 7, 3}, FALSE},
171         {{-109, -107, -103, -101}, FALSE},
172     };
173 
174     for (i = 0; i < sizeof(rtest)/sizeof(rtest[0]); i++) {
175         ret = IsRectEmpty(&rtest[i].rect);
176         ok(ret == rtest[i].ret, "Test %d: IsRectEmpty returned %s for %s\n", i,
177            ret ? "TRUE" : "FALSE", wine_dbgstr_rect(&rtest[i].rect));
178     }
179 }
180 
181 static void test_SetRect(void)
182 {
183     RECT rect;
184     BOOL ret;
185 
186     ret = SetRect(NULL, 0, 0, 0, 0);
187     ok(!ret, "got %d\n", ret);
188 
189     ret = SetRect(&rect, 1, 2, 3, 4);
190     ok(ret, "got %d\n", ret);
191     ok(rect.left == 1 && rect.top == 2 && rect.right == 3 && rect.bottom == 4,
192         "got wrong rectangle\n");
193 
194     ret = SetRect(&rect, 10, 10, 5, 5);
195     ok(ret, "got %d\n", ret);
196     ok(rect.left == 10 && rect.top == 10 && rect.right == 5 && rect.bottom == 5,
197         "got wrong rectangle\n");
198 }
199 
200 START_TEST(uitools)
201 {
202     test_FillRect();
203     test_SubtractRect();
204     test_EqualRect();
205     test_IsRectEmpty();
206     test_SetRect();
207 }
208