Home | History | Annotate | Download | only in tests
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 #include "Test.h"
      9 #include "SkClipStack.h"
     10 #include "SkPath.h"
     11 #include "SkRect.h"
     12 
     13 static void test_assign_and_comparison(skiatest::Reporter* reporter) {
     14     SkClipStack s;
     15     bool doAA = false;
     16 
     17     // Build up a clip stack with a path, an empty clip, and a rect.
     18     s.save();
     19     SkPath p;
     20     p.moveTo(5, 6);
     21     p.lineTo(7, 8);
     22     p.lineTo(5, 9);
     23     p.close();
     24     s.clipDevPath(p, SkRegion::kIntersect_Op, doAA);
     25 
     26     s.save();
     27     SkRect r = SkRect::MakeLTRB(1, 2, 3, 4);
     28     s.clipDevRect(r, SkRegion::kIntersect_Op, doAA);
     29     r = SkRect::MakeLTRB(10, 11, 12, 13);
     30     s.clipDevRect(r, SkRegion::kIntersect_Op, doAA);
     31 
     32     s.save();
     33     r = SkRect::MakeLTRB(14, 15, 16, 17);
     34     s.clipDevRect(r, SkRegion::kUnion_Op, doAA);
     35 
     36     // Test that assignment works.
     37     SkClipStack copy = s;
     38     REPORTER_ASSERT(reporter, s == copy);
     39 
     40     // Test that different save levels triggers not equal.
     41     s.restore();
     42     REPORTER_ASSERT(reporter, s != copy);
     43 
     44     // Test that an equal, but not copied version is equal.
     45     s.save();
     46     r = SkRect::MakeLTRB(14, 15, 16, 17);
     47     s.clipDevRect(r, SkRegion::kUnion_Op, doAA);
     48     REPORTER_ASSERT(reporter, s == copy);
     49 
     50     // Test that a different op on one level triggers not equal.
     51     s.restore();
     52     s.save();
     53     r = SkRect::MakeLTRB(14, 15, 16, 17);
     54     s.clipDevRect(r, SkRegion::kIntersect_Op, doAA);
     55     REPORTER_ASSERT(reporter, s != copy);
     56 
     57     // Test that different state (clip type) triggers not equal.
     58     s.restore();
     59     s.save();
     60     SkPath rp;
     61     rp.addRect(r);
     62     s.clipDevPath(rp, SkRegion::kUnion_Op, doAA);
     63     REPORTER_ASSERT(reporter, s != copy);
     64 
     65     // Test that different rects triggers not equal.
     66     s.restore();
     67     s.save();
     68     r = SkRect::MakeLTRB(24, 25, 26, 27);
     69     s.clipDevRect(r, SkRegion::kUnion_Op, doAA);
     70     REPORTER_ASSERT(reporter, s != copy);
     71 
     72     // Sanity check
     73     s.restore();
     74     copy.restore();
     75     REPORTER_ASSERT(reporter, s == copy);
     76     s.restore();
     77     copy.restore();
     78     REPORTER_ASSERT(reporter, s == copy);
     79 
     80     // Test that different paths triggers not equal.
     81     s.restore();
     82     s.save();
     83     p.addRect(r);
     84     s.clipDevPath(p, SkRegion::kIntersect_Op, doAA);
     85     REPORTER_ASSERT(reporter, s != copy);
     86 }
     87 
     88 static void assert_count(skiatest::Reporter* reporter, const SkClipStack& stack,
     89                          int count) {
     90     REPORTER_ASSERT(reporter, count == stack.getSaveCount());
     91 
     92     SkClipStack::B2FIter iter(stack);
     93     int counter = 0;
     94     while (iter.next()) {
     95         counter += 1;
     96     }
     97     REPORTER_ASSERT(reporter, count == counter);
     98 }
     99 
    100 static void TestClipStack(skiatest::Reporter* reporter) {
    101     SkClipStack stack;
    102 
    103     assert_count(reporter, stack, 0);
    104 
    105     static const SkIRect gRects[] = {
    106         { 0, 0, 100, 100 },
    107         { 25, 25, 125, 125 },
    108         { 0, 0, 1000, 1000 },
    109         { 0, 0, 75, 75 }
    110     };
    111     for (size_t i = 0; i < SK_ARRAY_COUNT(gRects); i++) {
    112         stack.clipDevRect(gRects[i], SkRegion::kIntersect_Op);
    113     }
    114 
    115     // all of the above rects should have been intersected, leaving only 1 rect
    116     SkClipStack::B2FIter iter(stack);
    117     const SkClipStack::B2FIter::Clip* clip = iter.next();
    118     SkRect answer;
    119     answer.iset(25, 25, 75, 75);
    120 
    121     REPORTER_ASSERT(reporter, clip);
    122     REPORTER_ASSERT(reporter, clip->fRect);
    123     REPORTER_ASSERT(reporter, !clip->fPath);
    124     REPORTER_ASSERT(reporter, SkRegion::kIntersect_Op == clip->fOp);
    125     REPORTER_ASSERT(reporter, *clip->fRect == answer);
    126     // now check that we only had one in our iterator
    127     REPORTER_ASSERT(reporter, !iter.next());
    128 
    129     stack.reset();
    130     assert_count(reporter, stack, 0);
    131 
    132     test_assign_and_comparison(reporter);
    133 }
    134 
    135 #include "TestClassDef.h"
    136 DEFINE_TESTCLASS("ClipStack", TestClipStackClass, TestClipStack)
    137