aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/CanvasTest.cpp
blob: da1fafd3945efcb0bc7c9731143bcdefabb45361 (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
65
66
67

/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "Test.h"
#include "SkBitmap.h"
#include "SkCanvas.h"

static void test_isDrawingToLayer(skiatest::Reporter* reporter) {
    SkBitmap bm;
    bm.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
    bm.allocPixels();
    
    SkCanvas canvas(bm);

    REPORTER_ASSERT(reporter, !canvas.isDrawingToLayer());
    canvas.save();
    REPORTER_ASSERT(reporter, !canvas.isDrawingToLayer());
    
    const SkRect* bounds = NULL;    // null means include entire bounds
    const SkPaint* paint = NULL;

    canvas.saveLayer(bounds, paint);
    REPORTER_ASSERT(reporter, canvas.isDrawingToLayer());
    canvas.restore();
    REPORTER_ASSERT(reporter, !canvas.isDrawingToLayer());

    canvas.saveLayer(bounds, paint);
    canvas.saveLayer(bounds, paint);
    REPORTER_ASSERT(reporter, canvas.isDrawingToLayer());
    canvas.restore();
    REPORTER_ASSERT(reporter, canvas.isDrawingToLayer());
    canvas.restore();
    // now layer count should be 0
    REPORTER_ASSERT(reporter, !canvas.isDrawingToLayer());
}

static void TestCanvas(skiatest::Reporter* reporter) {
    SkBitmap bm;
    bm.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
    bm.allocPixels();

    SkCanvas canvas(bm);
    int n;

    REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
    n = canvas.save();
    REPORTER_ASSERT(reporter, 1 == n);
    REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
    canvas.save();
    canvas.save();
    REPORTER_ASSERT(reporter, 4 == canvas.getSaveCount());
    canvas.restoreToCount(2);
    REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());

    // should this pin to 1, or be a no-op, or crash?
    canvas.restoreToCount(0);
    REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());

    test_isDrawingToLayer(reporter);
}

#include "TestClassDef.h"
DEFINE_TESTCLASS("Canvas", TestCanvasClass, TestCanvas)