1/*
2 * Copyright (C) 2014 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 */
16package android.uirendering.cts.testclasses;
17
18import android.graphics.Color;
19import android.graphics.Point;
20import android.graphics.Rect;
21import android.support.test.filters.LargeTest;
22import android.support.test.filters.MediumTest;
23import android.support.test.runner.AndroidJUnit4;
24import android.uirendering.cts.R;
25import android.uirendering.cts.bitmapcomparers.BitmapComparer;
26import android.uirendering.cts.bitmapcomparers.MSSIMComparer;
27import android.uirendering.cts.bitmapverifiers.RectVerifier;
28import android.uirendering.cts.testinfrastructure.ActivityTestBase;
29import android.uirendering.cts.testinfrastructure.CanvasClient;
30import android.uirendering.cts.testinfrastructure.ViewInitializer;
31
32import org.junit.Test;
33import org.junit.runner.RunWith;
34
35@MediumTest
36@RunWith(AndroidJUnit4.class)
37public class InfrastructureTests extends ActivityTestBase {
38
39    @Test
40    public void testScreenshot() {
41        for (int i = 0 ; i < 500 ; i ++) {
42            takeScreenshot(new Point());
43            System.gc();
44        }
45    }
46
47    /**
48     * Ensure that both render paths are producing independent output. We do this
49     * by verifying that two paths that should render differently *do* render
50     * differently.
51     */
52    @LargeTest
53    @Test
54    public void testRenderSpecIsolation() {
55        CanvasClient canvasClient = (canvas, width, height) -> {
56            canvas.drawColor(canvas.isHardwareAccelerated() ? Color.BLACK : Color.WHITE);
57        };
58        BitmapComparer inverseComparer = new BitmapComparer() {
59            @Override
60            public boolean verifySame(int[] ideal, int[] given, int offset, int stride, int width,
61                    int height) {
62
63                // Return true if the images aren't even 10% similar. They should be completely
64                // different, since they should both be completely different colors.
65                final float threshold = 0.1f;
66                return !(new MSSIMComparer(threshold)).verifySame(ideal, given, offset, stride,
67                        width, height);
68            }
69        };
70        createTest()
71                // Because of the inverseComparer, we can't use Picture because
72                // software w/ picture = software w/o picture (same for hardware).
73                // (The inverseComparer assumes that there are only two render paths are they
74                // are different.)
75                .addCanvasClientWithoutUsingPicture(canvasClient)
76                .runWithComparer(inverseComparer);
77    }
78
79    @Test
80    public void testViewInitializer() {
81        final Rect clipRect = new Rect(0, 0, 50, 50);
82        ViewInitializer viewInitializer = view -> view.setClipBounds(clipRect);
83        createTest()
84                .addLayout(R.layout.simple_red_layout, viewInitializer)
85                .runWithVerifier(new RectVerifier(Color.WHITE, Color.RED, clipRect));
86    }
87}
88