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 import java.io.File;
19 
20 @MediumTest
21 @RunWith(AndroidJUnit4.class)
22 public class PathTests extends ActivityTestBase {
23 
24     @Test
testTextPathWithOffset()25     public void testTextPathWithOffset() {
26         createTest()
27                 .addCanvasClient((canvas, width, height) -> {
28                     Paint paint = new Paint();
29                     paint.setColor(Color.BLACK);
30                     paint.setAntiAlias(true);
31                     paint.setTypeface(Typeface.create("sans-serif", Typeface.NORMAL));
32                     paint.setTextSize(26);
33 
34                     String text = "Abc";
35                     File fontFile = TypefaceTestUtil.getFirstFont(text, paint);
36 
37                     if (!fontFile.getName().startsWith("Roboto")) {
38                         Typeface typeface = paint.getTypeface();
39                         paint.setTypeface(
40                                 TypefaceTestUtil.getRobotoTypeface(
41                                         typeface.getWeight(),
42                                         typeface.isItalic()));
43                     }
44 
45                     Path path = new Path();
46                     paint.getTextPath(text, 0, text.length(), 0, 0, path);
47                     path.offset(0, 50);
48                     canvas.drawPath(path, paint);
49                 })
50                 .runWithVerifier(new GoldenImageVerifier(getActivity(),
51                         R.drawable.text_path_with_offset, new MSSIMComparer(0.89)));
52     }
53 
54     @Test
testPathApproximate_circle()55     public void testPathApproximate_circle() {
56         final Path path = new Path();
57         path.addCircle(45, 45, 40, Path.Direction.CW);
58         verifyPathApproximation(path, R.drawable.pathtest_path_approximate_circle);
59     }
60 
61     @Test
testPathApproximate_rect()62     public void testPathApproximate_rect() {
63         final Path path = new Path();
64         path.addRect(5, 5, 85, 85, Path.Direction.CW);
65         verifyPathApproximation(path, R.drawable.pathtest_path_approximate_rect);
66     }
67 
68     @Test
testPathApproximate_quads()69     public void testPathApproximate_quads() {
70         final Path path = new Path();
71         path.moveTo(5, 5);
72         path.quadTo(45, 45, 85, 5);
73         path.quadTo(45, 45, 85, 85);
74         path.quadTo(45, 45, 5, 85);
75         path.quadTo(45, 45, 5, 5);
76         verifyPathApproximation(path, R.drawable.pathtest_path_approximate_quads);
77     }
78 
79     @Test
testPathApproximate_cubics()80     public void testPathApproximate_cubics() {
81         final Path path = new Path();
82         path.moveTo(5, 5);
83         path.cubicTo(45, 45, 45, 45, 85, 5);
84         path.cubicTo(45, 45, 45, 45, 85, 85);
85         path.cubicTo(45, 45, 45, 45, 5, 85);
86         path.cubicTo(45, 45, 45, 45, 5, 5);
87         verifyPathApproximation(path, R.drawable.pathtest_path_approximate_cubics);
88     }
89 
verifyPathApproximation(Path path, int goldenResourceId)90     private void verifyPathApproximation(Path path, int goldenResourceId) {
91         float[] approx = path.approximate(0.5f);
92         final Paint paint = new Paint();
93         paint.setAntiAlias(true);
94         paint.setStrokeWidth(2);
95         createTest()
96                 .addCanvasClient((canvas, width, height) -> {
97                     float lastX = approx[1];
98                     float lastY = approx[2];
99                     for (int i = 3; i < approx.length; i += 3) {
100                         float x = approx[i + 1];
101                         float y = approx[i + 2];
102                         canvas.drawLine(lastX, lastY, x, y, paint);
103                         lastX = x;
104                         lastY = y;
105                     }
106                 })
107                 .runWithVerifier(new GoldenImageVerifier(getActivity(),
108                         goldenResourceId, new MSSIMComparer(0.98)));
109     }
110 }
111