Home | History | Annotate | Download | only in tests
      1 #include "Test.h"
      2 
      3 #include "SkRecord.h"
      4 #include "SkRecordPattern.h"
      5 #include "SkRecorder.h"
      6 #include "SkRecords.h"
      7 
      8 using namespace SkRecords;
      9 typedef Pattern3<Is<Save>,
     10                  Is<ClipRect>,
     11                  Is<Restore> >
     12     SaveClipRectRestore;
     13 
     14 DEF_TEST(RecordPattern_Simple, r) {
     15     SaveClipRectRestore pattern;
     16 
     17     SkRecord record;
     18     REPORTER_ASSERT(r, !pattern.match(&record, 0));
     19 
     20     SkRecorder recorder(&record, 1920, 1200);
     21 
     22     // Build up a save-clip-restore block.  The pattern will match only it's complete.
     23     recorder.save();
     24     REPORTER_ASSERT(r, !pattern.match(&record, 0));
     25 
     26     recorder.clipRect(SkRect::MakeWH(300, 200));
     27     REPORTER_ASSERT(r, !pattern.match(&record, 0));
     28 
     29     recorder.restore();
     30     REPORTER_ASSERT(r, pattern.match(&record, 0));
     31     REPORTER_ASSERT(r, pattern.first<Save>()      != NULL);
     32     REPORTER_ASSERT(r, pattern.second<ClipRect>() != NULL);
     33     REPORTER_ASSERT(r, pattern.third<Restore>()   != NULL);
     34 }
     35 
     36 DEF_TEST(RecordPattern_StartingIndex, r) {
     37     SaveClipRectRestore pattern;
     38 
     39     SkRecord record;
     40     SkRecorder recorder(&record, 1920, 1200);
     41 
     42     // There will be two save-clipRect-restore blocks [0,3) and [3,6).
     43     for (int i = 0; i < 2; i++) {
     44         recorder.save();
     45             recorder.clipRect(SkRect::MakeWH(300, 200));
     46         recorder.restore();
     47     }
     48 
     49     // We should match only at 0 and 3.  Going over the length should fail gracefully.
     50     for (unsigned i = 0; i < 8; i++) {
     51         if (i == 0 || i == 3) {
     52             REPORTER_ASSERT(r, pattern.match(&record, i) == i + 3);
     53         } else {
     54             REPORTER_ASSERT(r, !pattern.match(&record, i));
     55         }
     56     }
     57 }
     58 
     59 DEF_TEST(RecordPattern_DontMatchSubsequences, r) {
     60     SaveClipRectRestore pattern;
     61 
     62     SkRecord record;
     63     SkRecorder recorder(&record, 1920, 1200);
     64 
     65     recorder.save();
     66         recorder.clipRect(SkRect::MakeWH(300, 200));
     67         recorder.drawRect(SkRect::MakeWH(600, 300), SkPaint());
     68     recorder.restore();
     69 
     70     REPORTER_ASSERT(r, !pattern.match(&record, 0));
     71 }
     72 
     73 DEF_TEST(RecordPattern_Star, r) {
     74     Pattern3<Is<Save>, Star<Is<ClipRect> >, Is<Restore> > pattern;
     75 
     76     SkRecord record;
     77     SkRecorder recorder(&record, 1920, 1200);
     78 
     79     recorder.save();
     80     recorder.restore();
     81     REPORTER_ASSERT(r, pattern.match(&record, 0));
     82 
     83     recorder.save();
     84         recorder.clipRect(SkRect::MakeWH(300, 200));
     85     recorder.restore();
     86     REPORTER_ASSERT(r, pattern.match(&record, 2));
     87 
     88     recorder.save();
     89         recorder.clipRect(SkRect::MakeWH(300, 200));
     90         recorder.clipRect(SkRect::MakeWH(100, 100));
     91     recorder.restore();
     92     REPORTER_ASSERT(r, pattern.match(&record, 5));
     93 }
     94 
     95 DEF_TEST(RecordPattern_IsDraw, r) {
     96     Pattern3<Is<Save>, IsDraw, Is<Restore> > pattern;
     97 
     98     SkRecord record;
     99     SkRecorder recorder(&record, 1920, 1200);
    100 
    101     recorder.save();
    102         recorder.clipRect(SkRect::MakeWH(300, 200));
    103     recorder.restore();
    104 
    105     REPORTER_ASSERT(r, !pattern.match(&record, 0));
    106 
    107     SkPaint paint;
    108 
    109     recorder.save();
    110         paint.setColor(0xEEAA8822);
    111         recorder.drawRect(SkRect::MakeWH(300, 200), paint);
    112     recorder.restore();
    113 
    114     recorder.save();
    115         paint.setColor(0xFACEFACE);
    116         recorder.drawPaint(paint);
    117     recorder.restore();
    118 
    119     REPORTER_ASSERT(r, pattern.match(&record, 3));
    120     REPORTER_ASSERT(r, pattern.first<Save>()    != NULL);
    121     REPORTER_ASSERT(r, pattern.second<SkPaint>()->getColor() == 0xEEAA8822);
    122     REPORTER_ASSERT(r, pattern.third<Restore>() != NULL);
    123 
    124     REPORTER_ASSERT(r, pattern.match(&record, 6));
    125     REPORTER_ASSERT(r, pattern.first<Save>()    != NULL);
    126     REPORTER_ASSERT(r, pattern.second<SkPaint>()->getColor() == 0xFACEFACE);
    127     REPORTER_ASSERT(r, pattern.third<Restore>() != NULL);
    128 }
    129 
    130 DEF_TEST(RecordPattern_Complex, r) {
    131     Pattern3<Is<Save>,
    132              Star<Not<Or3<Is<Save>,
    133                           Is<Restore>,
    134                           IsDraw> > >,
    135              Is<Restore> > pattern;
    136 
    137     SkRecord record;
    138     SkRecorder recorder(&record, 1920, 1200);
    139 
    140     recorder.save();
    141     recorder.restore();
    142     REPORTER_ASSERT(r, pattern.match(&record, 0) == 2);
    143 
    144     recorder.save();
    145         recorder.save();
    146         recorder.restore();
    147     recorder.restore();
    148     REPORTER_ASSERT(r, !pattern.match(&record, 2));
    149     REPORTER_ASSERT(r, pattern.match(&record, 3) == 5);
    150 
    151     recorder.save();
    152         recorder.clipRect(SkRect::MakeWH(300, 200));
    153     recorder.restore();
    154     REPORTER_ASSERT(r, pattern.match(&record, 6) == 9);
    155 
    156     recorder.save();
    157         recorder.clipRect(SkRect::MakeWH(300, 200));
    158         recorder.drawRect(SkRect::MakeWH(100, 3000), SkPaint());
    159     recorder.restore();
    160     REPORTER_ASSERT(r, !pattern.match(&record, 9));
    161 
    162     recorder.save();
    163         recorder.pushCull(SkRect::MakeWH(300, 200));
    164         recorder.clipRect(SkRect::MakeWH(300, 200));
    165         recorder.clipRect(SkRect::MakeWH(100, 400));
    166         recorder.popCull();
    167     recorder.restore();
    168     REPORTER_ASSERT(r, pattern.match(&record, 13) == 19);
    169 
    170     // Same as above, but using pattern.search to step through matches.
    171     unsigned begin, end = 0;
    172     REPORTER_ASSERT(r, pattern.search(&record, &begin, &end));
    173     REPORTER_ASSERT(r, begin == 0);
    174     REPORTER_ASSERT(r, end == 2);
    175 
    176     REPORTER_ASSERT(r, pattern.search(&record, &begin, &end));
    177     REPORTER_ASSERT(r, begin == 3);
    178     REPORTER_ASSERT(r, end == 5);
    179 
    180     REPORTER_ASSERT(r, pattern.search(&record, &begin, &end));
    181     REPORTER_ASSERT(r, begin == 6);
    182     REPORTER_ASSERT(r, end == 9);
    183 
    184     REPORTER_ASSERT(r, pattern.search(&record, &begin, &end));
    185     REPORTER_ASSERT(r, begin == 13);
    186     REPORTER_ASSERT(r, end == 19);
    187 
    188     REPORTER_ASSERT(r, !pattern.search(&record, &begin, &end));
    189 }
    190 
    191 DEF_TEST(RecordPattern_SaveLayerIsNotADraw, r) {
    192     Pattern1<IsDraw> pattern;
    193 
    194     SkRecord record;
    195     SkRecorder recorder(&record, 1920, 1200);
    196     recorder.saveLayer(NULL, NULL);
    197 
    198     REPORTER_ASSERT(r, !pattern.match(&record, 0));
    199 }
    200