Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright 2013 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "SkError.h"
      9 #include "SkPath.h"
     10 #include "SkRect.h"
     11 #include "Test.h"
     12 
     13 typedef struct {
     14     skiatest::Reporter *fReporter;
     15     unsigned int *fIntPointer;
     16 } ErrorContext;
     17 
     18 #define CHECK(errcode)                                                        \
     19   REPORTER_ASSERT( reporter, (err = SkGetLastError()) == errcode);            \
     20   if (err != kNoError_SkError)                                                \
     21   {                                                                           \
     22      SkClearLastError();                                                      \
     23   }
     24 
     25 static void cb(SkError err, void *context) {
     26     ErrorContext *context_ptr = static_cast<ErrorContext *>(context);
     27     REPORTER_ASSERT( context_ptr->fReporter, (*(context_ptr->fIntPointer) == 0xdeadbeef) );
     28 }
     29 
     30 DEF_TEST(Error, reporter) {
     31     // Some previous user of this thread may have left an error laying around.
     32     SkClearLastError();
     33 
     34     SkError err;
     35 
     36     unsigned int test_value = 0xdeadbeef;
     37     ErrorContext context;
     38     context.fReporter = reporter;
     39     context.fIntPointer = &test_value;
     40 
     41     SkSetErrorCallback(cb, &context);
     42 
     43     CHECK(kNoError_SkError);
     44 
     45     SkRect r = SkRect::MakeWH(50, 100);
     46     CHECK(kNoError_SkError);
     47 
     48     SkPath path;
     49     path.addRect(r);
     50     CHECK(kNoError_SkError);
     51 
     52     path.addRoundRect(r, 10, 10);
     53     CHECK(kNoError_SkError);
     54 
     55     // should trigger the default error callback, which just prints to the screen.
     56     path.addRoundRect(r, -10, -10);
     57     CHECK(kInvalidArgument_SkError);
     58     CHECK(kNoError_SkError);
     59 
     60     // should trigger *our* callback.
     61     path.addRoundRect(r, -10, -10);
     62     CHECK(kInvalidArgument_SkError);
     63     CHECK(kNoError_SkError);
     64 }
     65