push cc8bc80451cc24f4d7cf75168b569f0ebfe19547
[wine/hacks.git] / dlls / gdi32 / tests / mapping.c
blob03428174617cf07276c1cf0d4b92e7a78333a167
1 /*
2 * Unit tests for mapping functions
4 * Copyright (c) 2005 Huw Davies
5 * Copyright (c) 2008 Dmitry Timoshkov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <assert.h>
23 #include <stdio.h>
24 #include <math.h>
26 #include "wine/test.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "winerror.h"
32 #define rough_match(got, expected) (abs((got) - (expected)) <= 5)
34 #define expect_LPtoDP(_hdc, _x, _y) \
35 { \
36 POINT _pt = { 1000, 1000 }; \
37 LPtoDP(_hdc, &_pt, 1); \
38 ok(rough_match(_pt.x, _x), "expected x %d, got %d\n", (_x), _pt.x); \
39 ok(rough_match(_pt.y, _y), "expected y %d, got %d\n", (_y), _pt.y); \
42 #define expect_world_trasform(_hdc, _em11, _em22) \
43 { \
44 BOOL _ret; \
45 XFORM _xform; \
46 SetLastError(0xdeadbeef); \
47 _ret = GetWorldTransform(_hdc, &_xform); \
48 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) \
49 { \
50 ok(_ret, "GetWorldTransform error %u\n", GetLastError()); \
51 ok(_xform.eM11 == (_em11), "expected %f, got %f\n", (_em11), _xform.eM11); \
52 ok(_xform.eM12 == 0.0, "expected 0.0, got %f\n", _xform.eM12); \
53 ok(_xform.eM21 == 0.0, "expected 0.0, got %f\n", _xform.eM21); \
54 ok(_xform.eM22 == (_em22), "expected %f, got %f\n", (_em22), _xform.eM22); \
55 ok(_xform.eDx == 0.0, "expected 0.0, got %f\n", _xform.eDx); \
56 ok(_xform.eDy == 0.0, "expected 0.0, got %f\n", _xform.eDy); \
57 } \
60 #define expect_dc_ext(_func, _hdc, _cx, _cy) \
61 { \
62 BOOL _ret; \
63 SIZE _size; \
64 SetLastError(0xdeadbeef); \
65 _ret = _func(_hdc, &_size); \
66 ok(_ret, #_func " error %u\n", GetLastError()); \
67 ok(_size.cx == (_cx), "expected cx %d, got %d\n", (_cx), _size.cx); \
68 ok(_size.cy == (_cy), "expected cy %d, got %d\n", (_cy), _size.cy); \
71 #define expect_viewport_ext(_hdc, _cx, _cy) expect_dc_ext(GetViewportExtEx, _hdc, _cx, _cy)
72 #define expect_window_ext(_hdc, _cx, _cy) expect_dc_ext(GetWindowExtEx, _hdc, _cx, _cy)
74 static void test_world_transform(void)
76 BOOL is_win9x;
77 HDC hdc;
78 INT ret, size_cx, size_cy, res_x, res_y, dpi_x, dpi_y;
79 XFORM xform;
80 SIZE size;
82 SetLastError(0xdeadbeef);
83 GetWorldTransform(0, NULL);
84 is_win9x = GetLastError() == ERROR_CALL_NOT_IMPLEMENTED;
86 hdc = CreateCompatibleDC(0);
88 xform.eM11 = 1.0f;
89 xform.eM12 = 0.0f;
90 xform.eM21 = 0.0f;
91 xform.eM22 = 1.0f;
92 xform.eDx = 0.0f;
93 xform.eDy = 0.0f;
94 ret = SetWorldTransform(hdc, &xform);
95 ok(!ret, "SetWorldTransform should fail in GM_COMPATIBLE mode\n");
97 size_cx = GetDeviceCaps(hdc, HORZSIZE);
98 size_cy = GetDeviceCaps(hdc, VERTSIZE);
99 res_x = GetDeviceCaps(hdc, HORZRES);
100 res_y = GetDeviceCaps(hdc, VERTRES);
101 dpi_x = GetDeviceCaps(hdc, LOGPIXELSX);
102 dpi_y = GetDeviceCaps(hdc, LOGPIXELSY);
103 trace("dc size %d x %d, resolution %d x %d dpi %d x %d\n",
104 size_cx, size_cy, res_x, res_y, dpi_x, dpi_y );
106 expect_viewport_ext(hdc, 1, 1);
107 expect_window_ext(hdc, 1, 1);
108 expect_world_trasform(hdc, 1.0, 1.0);
109 expect_LPtoDP(hdc, 1000, 1000);
111 SetLastError(0xdeadbeef);
112 ret = SetMapMode(hdc, MM_LOMETRIC);
113 ok(ret == MM_TEXT, "expected MM_TEXT, got %d\n", ret);
115 if (is_win9x)
117 expect_viewport_ext(hdc, dpi_x, dpi_y);
118 expect_window_ext(hdc, 254, -254);
120 else
122 expect_viewport_ext(hdc, res_x, -res_y);
123 ok( GetWindowExtEx( hdc, &size ), "GetWindowExtEx failed\n" );
124 ok( size.cx == size_cx * 10 ||
125 size.cx == MulDiv( res_x, 254, dpi_x ), /* Vista uses a more precise method */
126 "expected cx %d or %d, got %d\n", size_cx * 10, MulDiv( res_x, 254, dpi_x ), size.cx );
127 ok( size.cy == size_cy * 10 ||
128 size.cy == MulDiv( res_y, 254, dpi_y ), /* Vista uses a more precise method */
129 "expected cy %d or %d, got %d\n", size_cy * 10, MulDiv( res_y, 254, dpi_y ), size.cy );
131 expect_world_trasform(hdc, 1.0, 1.0);
132 expect_LPtoDP(hdc, MulDiv(1000 / 10, res_x, size_cx), -MulDiv(1000 / 10, res_y, size_cy));
134 SetLastError(0xdeadbeef);
135 ret = SetMapMode(hdc, MM_TEXT);
136 ok(ret == MM_LOMETRIC, "expected MM_LOMETRIC, got %d\n", ret);
138 expect_viewport_ext(hdc, 1, 1);
139 expect_window_ext(hdc, 1, 1);
140 expect_world_trasform(hdc, 1.0, 1.0);
141 expect_LPtoDP(hdc, 1000, 1000);
143 ret = SetGraphicsMode(hdc, GM_ADVANCED);
144 if (!ret)
146 DeleteDC(hdc);
147 skip("GM_ADVANCED is not supported on this platform\n");
148 return;
151 expect_viewport_ext(hdc, 1, 1);
152 expect_window_ext(hdc, 1, 1);
153 expect_world_trasform(hdc, 1.0, 1.0);
154 expect_LPtoDP(hdc, 1000, 1000);
156 /* The transform must conform to (eM11 * eM22 != eM12 * eM21) requirement */
157 xform.eM11 = 1.0f;
158 xform.eM12 = 2.0f;
159 xform.eM21 = 1.0f;
160 xform.eM22 = 2.0f;
161 xform.eDx = 0.0f;
162 xform.eDy = 0.0f;
163 ret = SetWorldTransform(hdc, &xform);
164 ok(!ret ||
165 broken(ret), /* NT4 */
166 "SetWorldTransform should fail with an invalid xform\n");
168 xform.eM11 = 20.0f;
169 xform.eM12 = 0.0f;
170 xform.eM21 = 0.0f;
171 xform.eM22 = 20.0f;
172 xform.eDx = 0.0f;
173 xform.eDy = 0.0f;
174 SetLastError(0xdeadbeef);
175 ret = SetWorldTransform(hdc, &xform);
176 ok(ret, "SetWorldTransform error %u\n", GetLastError());
178 expect_viewport_ext(hdc, 1, 1);
179 expect_window_ext(hdc, 1, 1);
180 expect_world_trasform(hdc, 20.0, 20.0);
181 expect_LPtoDP(hdc, 20000, 20000);
183 SetLastError(0xdeadbeef);
184 ret = SetMapMode(hdc, MM_LOMETRIC);
185 ok(ret == MM_TEXT, "expected MM_TEXT, got %d\n", ret);
187 expect_viewport_ext(hdc, res_x, -res_y);
188 ok( GetWindowExtEx( hdc, &size ), "GetWindowExtEx failed\n" );
189 ok( size.cx == size_cx * 10 ||
190 size.cx == MulDiv( res_x, 254, dpi_x ), /* Vista uses a more precise method */
191 "expected cx %d or %d, got %d\n", size_cx * 10, MulDiv( res_x, 254, dpi_x ), size.cx );
192 ok( size.cy == size_cy * 10 ||
193 size.cy == MulDiv( res_y, 254, dpi_y ), /* Vista uses a more precise method */
194 "expected cy %d or %d, got %d\n", size_cy * 10, MulDiv( res_y, 254, dpi_y ), size.cy );
195 expect_world_trasform(hdc, 20.0, 20.0);
196 expect_LPtoDP(hdc, MulDiv(20000, res_x, size.cx), -MulDiv(20000, res_y, size.cy));
198 SetLastError(0xdeadbeef);
199 ret = SetMapMode(hdc, MM_TEXT);
200 ok(ret == MM_LOMETRIC, "expected MM_LOMETRIC, got %d\n", ret);
202 expect_viewport_ext(hdc, 1, 1);
203 expect_window_ext(hdc, 1, 1);
204 expect_world_trasform(hdc, 20.0, 20.0);
205 expect_LPtoDP(hdc, 20000, 20000);
207 ret = SetGraphicsMode(hdc, GM_COMPATIBLE);
208 ok(ret, "SetGraphicsMode(GM_COMPATIBLE) should not fail if DC has't an identity transform\n");
209 ret = GetGraphicsMode(hdc);
210 ok(ret == GM_COMPATIBLE, "expected GM_COMPATIBLE, got %d\n", ret);
212 expect_viewport_ext(hdc, 1, 1);
213 expect_window_ext(hdc, 1, 1);
214 expect_world_trasform(hdc, 20.0, 20.0);
215 expect_LPtoDP(hdc, 20000, 20000);
217 DeleteDC(hdc);
220 static void test_modify_world_transform(void)
222 HDC hdc = GetDC(0);
223 int ret;
225 ret = SetGraphicsMode(hdc, GM_ADVANCED);
226 if(!ret) /* running in win9x so quit */
228 ReleaseDC(0, hdc);
229 skip("GM_ADVANCED is not supported on this platform\n");
230 return;
233 ret = ModifyWorldTransform(hdc, NULL, MWT_IDENTITY);
234 ok(ret, "ret = %d\n", ret);
236 ret = ModifyWorldTransform(hdc, NULL, MWT_LEFTMULTIPLY);
237 ok(!ret, "ret = %d\n", ret);
239 ret = ModifyWorldTransform(hdc, NULL, MWT_RIGHTMULTIPLY);
240 ok(!ret, "ret = %d\n", ret);
242 ReleaseDC(0, hdc);
245 static void test_SetWindowExt(HDC hdc, LONG cx, LONG cy, LONG expected_vp_cx, LONG expected_vp_cy)
247 SIZE windowExt, viewportExt;
248 POINT windowOrg, windowOrgAfter, viewportOrg, viewportOrgAfter;
250 GetWindowOrgEx(hdc, &windowOrg);
251 GetViewportOrgEx(hdc, &viewportOrg);
253 SetWindowExtEx(hdc, cx, cy, NULL);
254 GetWindowExtEx(hdc, &windowExt);
255 ok(windowExt.cx == cx && windowExt.cy == cy,
256 "Window extension: Expected %dx%d, got %dx%d\n",
257 cx, cy, windowExt.cx, windowExt.cy);
259 GetViewportExtEx(hdc, &viewportExt);
260 ok(rough_match(viewportExt.cx, expected_vp_cx) && rough_match(viewportExt.cy, expected_vp_cy),
261 "Viewport extents have not been properly adjusted: Expected %dx%d, got %dx%d\n",
262 expected_vp_cx, expected_vp_cy, viewportExt.cx, viewportExt.cy);
264 GetWindowOrgEx(hdc, &windowOrgAfter);
265 ok(windowOrg.x == windowOrgAfter.x && windowOrg.y == windowOrgAfter.y,
266 "Window origin changed from (%d,%d) to (%d,%d)\n",
267 windowOrg.x, windowOrg.y, windowOrgAfter.x, windowOrgAfter.y);
269 GetViewportOrgEx(hdc, &viewportOrgAfter);
270 ok(viewportOrg.x == viewportOrgAfter.x && viewportOrg.y == viewportOrgAfter.y,
271 "Viewport origin changed from (%d,%d) to (%d,%d)\n",
272 viewportOrg.x, viewportOrg.y, viewportOrgAfter.x, viewportOrgAfter.y);
275 static void test_SetViewportExt(HDC hdc, LONG cx, LONG cy, LONG expected_vp_cx, LONG expected_vp_cy)
277 SIZE windowExt, windowExtAfter, viewportExt;
278 POINT windowOrg, windowOrgAfter, viewportOrg, viewportOrgAfter;
280 GetWindowOrgEx(hdc, &windowOrg);
281 GetViewportOrgEx(hdc, &viewportOrg);
282 GetWindowExtEx(hdc, &windowExt);
284 SetViewportExtEx(hdc, cx, cy, NULL);
285 GetViewportExtEx(hdc, &viewportExt);
286 ok(rough_match(viewportExt.cx, expected_vp_cx) && rough_match(viewportExt.cy, expected_vp_cy),
287 "Viewport extents have not been properly adjusted: Expected %dx%d, got %dx%d\n",
288 expected_vp_cx, expected_vp_cy, viewportExt.cx, viewportExt.cy);
290 GetWindowExtEx(hdc, &windowExtAfter);
291 ok(windowExt.cx == windowExtAfter.cx && windowExt.cy == windowExtAfter.cy,
292 "Window extension changed from %dx%d to %dx%d\n",
293 windowExt.cx, windowExt.cy, windowExtAfter.cx, windowExtAfter.cy);
295 GetWindowOrgEx(hdc, &windowOrgAfter);
296 ok(windowOrg.x == windowOrgAfter.x && windowOrg.y == windowOrgAfter.y,
297 "Window origin changed from (%d,%d) to (%d,%d)\n",
298 windowOrg.x, windowOrg.y, windowOrgAfter.x, windowOrgAfter.y);
300 GetViewportOrgEx(hdc, &viewportOrgAfter);
301 ok(viewportOrg.x == viewportOrgAfter.x && viewportOrg.y == viewportOrgAfter.y,
302 "Viewport origin changed from (%d,%d) to (%d,%d)\n",
303 viewportOrg.x, viewportOrg.y, viewportOrgAfter.x, viewportOrgAfter.y);
306 static void test_isotropic_mapping(void)
308 SIZE win, vp;
309 HDC hdc = GetDC(0);
311 SetMapMode(hdc, MM_ISOTROPIC);
313 /* MM_ISOTROPIC is set up like MM_LOMETRIC.
314 Initial values after SetMapMode():
315 (1 inch = 25.4 mm)
317 Windows 9x: Windows NT:
318 Window Ext: 254 x -254 HORZSIZE*10 x VERTSIZE*10
319 Viewport Ext: LOGPIXELSX x LOGPIXELSY HORZRES x -VERTRES
321 To test without rounding errors, we have to use multiples of
322 these values!
325 GetWindowExtEx(hdc, &win);
326 GetViewportExtEx(hdc, &vp);
328 test_SetViewportExt(hdc, 10 * vp.cx, 10 * vp.cy, 10 * vp.cx, 10 * vp.cy);
329 test_SetWindowExt(hdc, win.cx, win.cy, 10 * vp.cx, 10 * vp.cy);
330 test_SetWindowExt(hdc, 2 * win.cx, win.cy, 10 * vp.cx, 5 * vp.cy);
331 test_SetWindowExt(hdc, win.cx, win.cy, 5 * vp.cx, 5 * vp.cy);
332 test_SetViewportExt(hdc, 4 * vp.cx, 2 * vp.cy, 2 * vp.cx, 2 * vp.cy);
333 test_SetViewportExt(hdc, vp.cx, 2 * vp.cy, vp.cx, vp.cy);
334 test_SetViewportExt(hdc, 2 * vp.cx, 2 * vp.cy, 2 * vp.cx, 2 * vp.cy);
335 test_SetViewportExt(hdc, 4 * vp.cx, 2 * vp.cy, 2 * vp.cx, 2 * vp.cy);
336 test_SetWindowExt(hdc, 4 * win.cx, 2 * win.cy, 2 * vp.cx, vp.cy);
337 test_SetViewportExt(hdc, -2 * vp.cx, -4 * vp.cy, -2 * vp.cx, -vp.cy);
338 test_SetViewportExt(hdc, -2 * vp.cx, -1 * vp.cy, -2 * vp.cx, -vp.cy);
339 test_SetWindowExt(hdc, -4 * win.cx, -2 * win.cy, -2 * vp.cx, -vp.cy);
340 test_SetWindowExt(hdc, 4 * win.cx, -4 * win.cy, -vp.cx, -vp.cy);
342 ReleaseDC(0, hdc);
345 START_TEST(mapping)
347 test_modify_world_transform();
348 test_world_transform();
349 test_isotropic_mapping();