customlinecap.c 13.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Unit test suite for customlinecap
 *
 * Copyright (C) 2008 Nikolay Sivov
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */
20
#include <limits.h>
21

22
#include "objbase.h"
23 24 25 26
#include "gdiplus.h"
#include "wine/test.h"

#define expect(expected, got) ok(got == expected, "Expected %.8x, got %.8x\n", expected, got)
27
#define expectf(expected, got) ok(got == expected, "Expected %.2f, got %.2f\n", expected, got)
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
static BOOL compare_float(float f, float g, unsigned int ulps)
{
    int x = *(int *)&f;
    int y = *(int *)&g;

    if (x < 0)
        x = INT_MIN - x;
    if (y < 0)
        y = INT_MIN - y;

    if (abs(x - y) > ulps)
        return FALSE;

    return TRUE;
}

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
static void test_constructor_destructor(void)
{
    GpCustomLineCap *custom;
    GpPath *path, *path2;
    GpStatus stat;

    stat = GdipCreatePath(FillModeAlternate, &path);
    expect(Ok, stat);
    stat = GdipAddPathRectangle(path, 5.0, 5.0, 10.0, 10.0);
    expect(Ok, stat);

    stat = GdipCreatePath(FillModeAlternate, &path2);
    expect(Ok, stat);
    stat = GdipAddPathRectangle(path2, 5.0, 5.0, 10.0, 10.0);
    expect(Ok, stat);

    /* NULL args */
    stat = GdipCreateCustomLineCap(NULL, NULL, LineCapFlat, 0.0, NULL);
    expect(InvalidParameter, stat);
    stat = GdipCreateCustomLineCap(path, NULL, LineCapFlat, 0.0, NULL);
    expect(InvalidParameter, stat);
    stat = GdipCreateCustomLineCap(NULL, path, LineCapFlat, 0.0, NULL);
    expect(InvalidParameter, stat);
    stat = GdipCreateCustomLineCap(NULL, NULL, LineCapFlat, 0.0, &custom);
    expect(InvalidParameter, stat);
    stat = GdipDeleteCustomLineCap(NULL);
    expect(InvalidParameter, stat);

    /* valid args */
    stat = GdipCreateCustomLineCap(NULL, path2, LineCapFlat, 0.0, &custom);
    expect(Ok, stat);
    stat = GdipDeleteCustomLineCap(custom);
    expect(Ok, stat);
    /* it's strange but native returns NotImplemented on stroke == NULL */
79
    custom = NULL;
80 81
    stat = GdipCreateCustomLineCap(path, NULL, LineCapFlat, 10.0, &custom);
    todo_wine expect(NotImplemented, stat);
82
    todo_wine ok(custom == NULL, "Expected a failure on creation\n");
83
    if(stat == Ok) GdipDeleteCustomLineCap(custom);
84 85 86 87 88

    GdipDeletePath(path2);
    GdipDeletePath(path);
}

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
static void test_linejoin(void)
{
    GpCustomLineCap *custom;
    GpPath *path;
    GpLineJoin join;
    GpStatus stat;

    stat = GdipCreatePath(FillModeAlternate, &path);
    expect(Ok, stat);
    stat = GdipAddPathRectangle(path, 5.0, 5.0, 10.0, 10.0);
    expect(Ok, stat);

    stat = GdipCreateCustomLineCap(NULL, path, LineCapFlat, 0.0, &custom);
    expect(Ok, stat);

    /* NULL args */
    stat = GdipGetCustomLineCapStrokeJoin(NULL, NULL);
    expect(InvalidParameter, stat);
    stat = GdipGetCustomLineCapStrokeJoin(custom, NULL);
    expect(InvalidParameter, stat);
    stat = GdipGetCustomLineCapStrokeJoin(NULL, &join);
    expect(InvalidParameter, stat);
111 112
    stat = GdipSetCustomLineCapStrokeJoin(NULL, LineJoinBevel);
    expect(InvalidParameter, stat);
113 114 115 116 117 118

    /* LineJoinMiter is default */
    stat = GdipGetCustomLineCapStrokeJoin(custom, &join);
    expect(Ok, stat);
    expect(LineJoinMiter, join);

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    /* set/get */
    stat = GdipSetCustomLineCapStrokeJoin(custom, LineJoinBevel);
    expect(Ok, stat);
    stat = GdipGetCustomLineCapStrokeJoin(custom, &join);
    expect(Ok, stat);
    expect(LineJoinBevel, join);
    stat = GdipSetCustomLineCapStrokeJoin(custom, LineJoinRound);
    expect(Ok, stat);
    stat = GdipGetCustomLineCapStrokeJoin(custom, &join);
    expect(Ok, stat);
    expect(LineJoinRound, join);
    stat = GdipSetCustomLineCapStrokeJoin(custom, LineJoinMiterClipped);
    expect(Ok, stat);
    stat = GdipGetCustomLineCapStrokeJoin(custom, &join);
    expect(Ok, stat);
    expect(LineJoinMiterClipped, join);

136 137 138 139
    GdipDeleteCustomLineCap(custom);
    GdipDeletePath(path);
}

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
static void test_inset(void)
{
    GpCustomLineCap *custom;
    GpPath *path;
    REAL inset;
    GpStatus stat;

    stat = GdipCreatePath(FillModeAlternate, &path);
    expect(Ok, stat);
    stat = GdipAddPathRectangle(path, 5.0, 5.0, 10.0, 10.0);
    expect(Ok, stat);

    stat = GdipCreateCustomLineCap(NULL, path, LineCapFlat, 0.0, &custom);
    expect(Ok, stat);

    /* NULL args */
    stat = GdipGetCustomLineCapBaseInset(NULL, NULL);
    expect(InvalidParameter, stat);
    stat = GdipGetCustomLineCapBaseInset(NULL, &inset);
    expect(InvalidParameter, stat);
    stat = GdipGetCustomLineCapBaseInset(custom, NULL);
    expect(InvalidParameter, stat);
    /* valid args */
    inset = (REAL)0xdeadbeef;
    stat = GdipGetCustomLineCapBaseInset(custom, &inset);
    expect(Ok, stat);
    expectf(0.0, inset);

    GdipDeleteCustomLineCap(custom);
    GdipDeletePath(path);
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
static void test_scale(void)
{
    GpCustomLineCap *custom;
    GpPath *path;
    REAL scale;
    GpStatus stat;

    stat = GdipCreatePath(FillModeAlternate, &path);
    expect(Ok, stat);
    stat = GdipAddPathRectangle(path, 5.0, 5.0, 10.0, 10.0);
    expect(Ok, stat);

    stat = GdipCreateCustomLineCap(NULL, path, LineCapFlat, 0.0, &custom);
    expect(Ok, stat);

    /* NULL args */
    stat = GdipGetCustomLineCapWidthScale(NULL, NULL);
    expect(InvalidParameter, stat);
    stat = GdipGetCustomLineCapWidthScale(NULL, &scale);
    expect(InvalidParameter, stat);
    stat = GdipGetCustomLineCapWidthScale(custom, NULL);
    expect(InvalidParameter, stat);
194 195 196 197 198

    stat = GdipSetCustomLineCapWidthScale(NULL, 2.0);
    expect(InvalidParameter, stat);

    /* valid args: read default */
199 200 201 202 203
    scale = (REAL)0xdeadbeef;
    stat = GdipGetCustomLineCapWidthScale(custom, &scale);
    expect(Ok, stat);
    expectf(1.0, scale);

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    /* set and read back some scale values: there is no limit for the scale */
    stat = GdipSetCustomLineCapWidthScale(custom, 2.5);
    expect(Ok, stat);
    scale = (REAL)0xdeadbeef;
    stat = GdipGetCustomLineCapWidthScale(custom, &scale);
    expect(Ok, stat);
    expectf(2.5, scale);

    stat = GdipSetCustomLineCapWidthScale(custom, 42.0);
    expect(Ok, stat);
    scale = (REAL)0xdeadbeef;
    stat = GdipGetCustomLineCapWidthScale(custom, &scale);
    expect(Ok, stat);
    expectf(42.0, scale);

    stat = GdipSetCustomLineCapWidthScale(custom, 3000.0);
    expect(Ok, stat);
    scale = (REAL)0xdeadbeef;
    stat = GdipGetCustomLineCapWidthScale(custom, &scale);
    expect(Ok, stat);
    expectf(3000.0, scale);

    stat = GdipSetCustomLineCapWidthScale(custom, 0.0);
    expect(Ok, stat);
    scale = (REAL)0xdeadbeef;
    stat = GdipGetCustomLineCapWidthScale(custom, &scale);
    expect(Ok, stat);
    expectf(0.0, scale);

233 234 235 236
    GdipDeleteCustomLineCap(custom);
    GdipDeletePath(path);
}

237 238
static void test_create_adjustable_cap(void)
{
239
    REAL inset, scale, height, width;
240 241 242 243
    GpAdjustableArrowCap *cap;
    GpLineJoin join;
    GpStatus stat;
    GpLineCap base;
244
    BOOL ret;
245 246 247 248 249 250

    stat = GdipCreateAdjustableArrowCap(10.0, 10.0, TRUE, NULL);
    ok(stat == InvalidParameter, "Unexpected return code, %d\n", stat);

    stat = GdipCreateAdjustableArrowCap(17.0, 15.0, TRUE, &cap);
    ok(stat == Ok, "Failed to create adjustable cap, %d\n", stat);
251 252 253 254 255 256 257 258

    stat = GdipGetAdjustableArrowCapFillState(cap, NULL);
    ok(stat == InvalidParameter, "Unexpected return code, %d\n", stat);

    ret = FALSE;
    stat = GdipGetAdjustableArrowCapFillState(cap, &ret);
    ok(stat == Ok, "Unexpected return code, %d\n", stat);
    ok(ret, "Unexpected fill state %d\n", ret);
259

260 261 262 263 264 265 266 267 268 269 270 271 272
    stat = GdipGetAdjustableArrowCapHeight(cap, NULL);
    ok(stat == InvalidParameter, "Unexpected return code, %d\n", stat);

    stat = GdipGetAdjustableArrowCapHeight(cap, &height);
    ok(stat == Ok, "Unexpected return code, %d\n", stat);
    ok(height == 17.0, "Unexpected cap height %f\n", height);

    stat = GdipGetAdjustableArrowCapWidth(cap, NULL);
    ok(stat == InvalidParameter, "Unexpected return code, %d\n", stat);

    stat = GdipGetAdjustableArrowCapWidth(cap, &width);
    ok(stat == Ok, "Unexpected return code, %d\n", stat);
    ok(width == 15.0, "Unexpected cap width %f\n", width);
273 274 275 276 277 278 279 280 281 282 283 284 285

    stat = GdipGetAdjustableArrowCapMiddleInset(cap, NULL);
    ok(stat == InvalidParameter, "Unexpected return code, %d\n", stat);

    stat = GdipGetAdjustableArrowCapMiddleInset(cap, &inset);
    ok(stat == Ok, "Unexpected return code, %d\n", stat);
    ok(inset == 0.0f, "Unexpected middle inset %f\n", inset);

    stat = GdipGetCustomLineCapBaseCap((GpCustomLineCap*)cap, &base);
    ok(stat == Ok, "Unexpected return code, %d\n", stat);
    ok(base == LineCapTriangle, "Unexpected base cap %d\n", base);

    stat = GdipSetCustomLineCapBaseCap((GpCustomLineCap*)cap, LineCapSquare);
286
todo_wine
287 288 289 290
    ok(stat == Ok, "Unexpected return code, %d\n", stat);

    stat = GdipGetCustomLineCapBaseCap((GpCustomLineCap*)cap, &base);
    ok(stat == Ok, "Unexpected return code, %d\n", stat);
291
todo_wine
292 293
    ok(base == LineCapSquare, "Unexpected base cap %d\n", base);

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    /* Base inset */
    stat = GdipGetAdjustableArrowCapWidth(cap, &width);
    ok(stat == Ok, "Unexpected return code, %d\n", stat);

    stat = GdipGetAdjustableArrowCapHeight(cap, &height);
    ok(stat == Ok, "Unexpected return code, %d\n", stat);

    inset = 0.0;
    stat = GdipGetCustomLineCapBaseInset((GpCustomLineCap*)cap, &inset);
    ok(stat == Ok, "Unexpected return code, %d\n", stat);
    ok(compare_float(inset, height / width, 1), "Unexpected inset %f\n", inset);

    stat = GdipSetAdjustableArrowCapMiddleInset(cap, 1.0);
    ok(stat == Ok, "Unexpected return code, %d\n", stat);

    inset = 0.0;
    stat = GdipGetCustomLineCapBaseInset((GpCustomLineCap*)cap, &inset);
    ok(stat == Ok, "Unexpected return code, %d\n", stat);
    ok(compare_float(inset, height / width, 1), "Unexpected inset %f\n", inset);

    stat = GdipSetAdjustableArrowCapHeight(cap, 2.0 * height);
    ok(stat == Ok, "Unexpected return code, %d\n", stat);

    inset = 0.0;
318 319
    stat = GdipGetCustomLineCapBaseInset((GpCustomLineCap*)cap, &inset);
    ok(stat == Ok, "Unexpected return code, %d\n", stat);
320
    ok(compare_float(inset, 2.0 * height / width, 1), "Unexpected inset %f\n", inset);
321 322 323 324 325 326 327 328 329 330 331 332

    stat = GdipGetCustomLineCapWidthScale((GpCustomLineCap*)cap, &scale);
    ok(stat == Ok, "Unexpected return code, %d\n", stat);
    ok(scale == 1.0f, "Unexpected width scale %f\n", scale);

    stat = GdipGetCustomLineCapStrokeJoin((GpCustomLineCap*)cap, &join);
    ok(stat == Ok, "Unexpected return code, %d\n", stat);
    ok(join == LineJoinMiter, "Unexpected stroke join %d\n", join);

    GdipDeleteCustomLineCap((GpCustomLineCap*)cap);
}

333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
static void test_captype(void)
{
    GpAdjustableArrowCap *arrowcap;
    GpCustomLineCap *custom;
    CustomLineCapType type;
    GpStatus stat;
    GpPath *path;

    stat = GdipGetCustomLineCapType(NULL, NULL);
    ok(stat == InvalidParameter, "Unexpected return code, %d\n", stat);

    type = 10;
    stat = GdipGetCustomLineCapType(NULL, &type);
    ok(stat == InvalidParameter, "Unexpected return code, %d\n", stat);
    ok(type == 10, "Unexpected cap type, %d\n", type);

    /* default cap */
    stat = GdipCreatePath(FillModeAlternate, &path);
    ok(stat == Ok, "Failed to create path, %d\n", stat);
    stat = GdipAddPathRectangle(path, 5.0, 5.0, 10.0, 10.0);
    ok(stat == Ok, "AddPathRectangle failed, %d\n", stat);

    stat = GdipCreateCustomLineCap(NULL, path, LineCapFlat, 0.0, &custom);
    ok(stat == Ok, "Failed to create cap, %d\n", stat);
    stat = GdipGetCustomLineCapType(custom, &type);
    ok(stat == Ok, "Failed to get cap type, %d\n", stat);
    ok(type == CustomLineCapTypeDefault, "Unexpected cap type %d\n", stat);
    GdipDeleteCustomLineCap(custom);
    GdipDeletePath(path);

    /* arrow cap */
    stat = GdipCreateAdjustableArrowCap(17.0, 15.0, TRUE, &arrowcap);
    ok(stat == Ok, "Failed to create adjustable cap, %d\n", stat);

    stat = GdipGetCustomLineCapType((GpCustomLineCap*)arrowcap, &type);
    ok(stat == Ok, "Failed to get cap type, %d\n", stat);
    ok(type == CustomLineCapTypeAdjustableArrow, "Unexpected cap type %d\n", stat);

    GdipDeleteCustomLineCap((GpCustomLineCap*)arrowcap);
}

374 375 376 377
START_TEST(customlinecap)
{
    struct GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
378 379 380 381 382 383 384
    HMODULE hmsvcrt;
    int (CDECL * _controlfp_s)(unsigned int *cur, unsigned int newval, unsigned int mask);

    /* Enable all FP exceptions except _EM_INEXACT, which gdi32 can trigger */
    hmsvcrt = LoadLibraryA("msvcrt");
    _controlfp_s = (void*)GetProcAddress(hmsvcrt, "_controlfp_s");
    if (_controlfp_s) _controlfp_s(0, 0, 0x0008001e);
385 386 387 388 389 390 391 392 393

    gdiplusStartupInput.GdiplusVersion              = 1;
    gdiplusStartupInput.DebugEventCallback          = NULL;
    gdiplusStartupInput.SuppressBackgroundThread    = 0;
    gdiplusStartupInput.SuppressExternalCodecs      = 0;

    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    test_constructor_destructor();
394
    test_linejoin();
395
    test_inset();
396
    test_scale();
397
    test_create_adjustable_cap();
398
    test_captype();
399 400 401

    GdiplusShutdown(gdiplusToken);
}