1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.uirendering.cts.testclasses;
18
19import android.graphics.Canvas;
20import android.graphics.Color;
21import android.graphics.Paint;
22import android.graphics.Picture;
23import android.graphics.Rect;
24import android.support.test.filters.MediumTest;
25import android.support.test.runner.AndroidJUnit4;
26import android.uirendering.cts.bitmapverifiers.ColorVerifier;
27import android.uirendering.cts.bitmapverifiers.RectVerifier;
28import android.uirendering.cts.testinfrastructure.ActivityTestBase;
29
30import org.junit.Test;
31import org.junit.runner.RunWith;
32
33@MediumTest
34@RunWith(AndroidJUnit4.class)
35public class PictureTest extends ActivityTestBase {
36
37    private static final Rect sRect = new Rect(0, 0, 40, 40);
38    private static final Rect sOffsetRect = new Rect(40, 0, 80, 40);
39
40    private static Picture greenSquare() {
41        Paint pt = new Paint();
42        pt.setColor(Color.GREEN);
43        Picture pic = new Picture();
44        Canvas subcanvas = pic.beginRecording(
45                ActivityTestBase.TEST_WIDTH, ActivityTestBase.TEST_HEIGHT);
46        subcanvas.drawRect(sRect, pt);
47        pic.endRecording();
48
49        return pic;
50    }
51
52    @Test
53    public void testPictureRespectsClip() throws Exception {
54        createTest()
55            .addCanvasClient(
56                    (canvas, width, height) -> {
57                        Picture pic = greenSquare();
58                        canvas.clipRect(sOffsetRect);
59                        pic.draw(canvas);  // should be clipped out
60                    }
61            ).runWithVerifier(new ColorVerifier(Color.WHITE));
62    }
63
64    @Test
65    public void testPictureRespectsTranslate() throws Exception {
66        createTest()
67            .addCanvasClient(
68                    (canvas, width, height) -> {
69                        Picture pic = greenSquare();
70                        canvas.translate(40, 0);
71                        pic.draw(canvas);  // should be offset
72                    }
73            ).runWithVerifier(
74                new RectVerifier(Color.WHITE, Color.GREEN, sOffsetRect));
75    }
76}
77
78