aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/ErrorTest.cpp
blob: 4802d53e146e3e1d9ebac4422ea39787ea0cd355 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkError.h"
#include "SkPath.h"
#include "SkRect.h"
#include "Test.h"

typedef struct {
    skiatest::Reporter *fReporter;
    unsigned int *fIntPointer;
} ErrorContext;

#define CHECK(errcode)                                                        \
  REPORTER_ASSERT( reporter, (err = SkGetLastError()) == errcode);            \
  if (err != kNoError_SkError)                                                \
  {                                                                           \
     SkClearLastError();                                                      \
  }

static void cb(SkError err, void *context) {
    ErrorContext *context_ptr = static_cast<ErrorContext *>(context);
    REPORTER_ASSERT( context_ptr->fReporter, (*(context_ptr->fIntPointer) == 0xdeadbeef) );
}

DEF_TEST(Error, reporter) {
    // Some previous user of this thread may have left an error laying around.
    SkClearLastError();

    SkError err;

    unsigned int test_value = 0xdeadbeef;
    ErrorContext context;
    context.fReporter = reporter;
    context.fIntPointer = &test_value;

    SkSetErrorCallback(cb, &context);

    CHECK(kNoError_SkError);

    SkRect r = SkRect::MakeWH(50, 100);
    CHECK(kNoError_SkError);

    SkPath path;
    path.addRect(r);
    CHECK(kNoError_SkError);

    path.addRoundRect(r, 10, 10);
    CHECK(kNoError_SkError);

    // should trigger the default error callback, which just prints to the screen.
    path.addRoundRect(r, -10, -10);
    CHECK(kInvalidArgument_SkError);
    CHECK(kNoError_SkError);

    // should trigger *our* callback.
    path.addRoundRect(r, -10, -10);
    CHECK(kInvalidArgument_SkError);
    CHECK(kNoError_SkError);
}