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