1package android.uirendering.cts.testclasses;
2
3import android.graphics.Color;
4import android.graphics.Paint;
5import android.graphics.Path;
6import android.graphics.Typeface;
7import android.support.test.filters.MediumTest;
8import android.support.test.runner.AndroidJUnit4;
9import android.uirendering.cts.R;
10import android.uirendering.cts.bitmapcomparers.MSSIMComparer;
11import android.uirendering.cts.bitmapverifiers.GoldenImageVerifier;
12import android.uirendering.cts.testinfrastructure.ActivityTestBase;
13
14import org.junit.Test;
15import org.junit.runner.RunWith;
16
17@MediumTest
18@RunWith(AndroidJUnit4.class)
19public class PathTests extends ActivityTestBase {
20
21    @Test
22    public void testTextPathWithOffset() {
23        createTest()
24                .addCanvasClient((canvas, width, height) -> {
25                    Paint paint = new Paint();
26                    paint.setColor(Color.BLACK);
27                    paint.setAntiAlias(true);
28                    paint.setTypeface(Typeface.create("sans-serif", Typeface.NORMAL));
29                    paint.setTextSize(26);
30                    Path path = new Path();
31                    String text = "Abc";
32                    paint.getTextPath(text, 0, text.length(), 0, 0, path);
33                    path.offset(0, 50);
34                    canvas.drawPath(path, paint);
35                })
36                .runWithVerifier(new GoldenImageVerifier(getActivity(),
37                        R.drawable.text_path_with_offset, new MSSIMComparer(0.92)));
38    }
39
40    @Test
41    public void testPathApproximate_circle() {
42        final Path path = new Path();
43        path.addCircle(45, 45, 40, Path.Direction.CW);
44        verifyPathApproximation(path, R.drawable.pathtest_path_approximate_circle);
45    }
46
47    @Test
48    public void testPathApproximate_rect() {
49        final Path path = new Path();
50        path.addRect(5, 5, 85, 85, Path.Direction.CW);
51        verifyPathApproximation(path, R.drawable.pathtest_path_approximate_rect);
52    }
53
54    @Test
55    public void testPathApproximate_quads() {
56        final Path path = new Path();
57        path.moveTo(5, 5);
58        path.quadTo(45, 45, 85, 5);
59        path.quadTo(45, 45, 85, 85);
60        path.quadTo(45, 45, 5, 85);
61        path.quadTo(45, 45, 5, 5);
62        verifyPathApproximation(path, R.drawable.pathtest_path_approximate_quads);
63    }
64
65    @Test
66    public void testPathApproximate_cubics() {
67        final Path path = new Path();
68        path.moveTo(5, 5);
69        path.cubicTo(45, 45, 45, 45, 85, 5);
70        path.cubicTo(45, 45, 45, 45, 85, 85);
71        path.cubicTo(45, 45, 45, 45, 5, 85);
72        path.cubicTo(45, 45, 45, 45, 5, 5);
73        verifyPathApproximation(path, R.drawable.pathtest_path_approximate_cubics);
74    }
75
76    private void verifyPathApproximation(Path path, int goldenResourceId) {
77        float[] approx = path.approximate(0.5f);
78        final Paint paint = new Paint();
79        paint.setAntiAlias(true);
80        paint.setStrokeWidth(2);
81        createTest()
82                .addCanvasClient((canvas, width, height) -> {
83                    float lastX = approx[1];
84                    float lastY = approx[2];
85                    for (int i = 3; i < approx.length; i += 3) {
86                        float x = approx[i + 1];
87                        float y = approx[i + 2];
88                        canvas.drawLine(lastX, lastY, x, y, paint);
89                        lastX = x;
90                        lastY = y;
91                    }
92                })
93                .runWithVerifier(new GoldenImageVerifier(getActivity(),
94                        goldenResourceId, new MSSIMComparer(0.98)));
95    }
96}
97