1 /*
2  * Copyright (C) 2016 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 package android.transition.cts;
17 
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertFalse;
20 
21 import android.graphics.Path;
22 import android.graphics.PathMeasure;
23 
24 public abstract class PathMotionTest {
assertPathMatches(Path expectedPath, Path path)25     public static void assertPathMatches(Path expectedPath, Path path) {
26         PathMeasure expectedMeasure = new PathMeasure(expectedPath, false);
27         PathMeasure pathMeasure = new PathMeasure(path, false);
28 
29         boolean expectedNextContour;
30         boolean pathNextContour;
31         int contourIndex = 0;
32         do {
33             float expectedLength = expectedMeasure.getLength();
34             assertEquals("Lengths differ", expectedLength, pathMeasure.getLength(), 0.01f);
35 
36             float minLength = Math.min(expectedLength, pathMeasure.getLength());
37 
38             float[] pos = new float[2];
39 
40             float increment = minLength / 5f;
41             for (float along = 0; along <= minLength; along += increment) {
42                 expectedMeasure.getPosTan(along, pos, null);
43                 float expectedX = pos[0];
44                 float expectedY = pos[1];
45 
46                 pathMeasure.getPosTan(along, pos, null);
47                 assertEquals("Failed at " + increment + " in contour " + contourIndex,
48                         expectedX, pos[0], 0.01f);
49                 assertEquals("Failed at " + increment + " in contour " + contourIndex,
50                         expectedY, pos[1], 0.01f);
51             }
52             expectedNextContour = expectedMeasure.nextContour();
53             pathNextContour = pathMeasure.nextContour();
54             contourIndex++;
55         } while (expectedNextContour && pathNextContour);
56         assertFalse(expectedNextContour);
57         assertFalse(pathNextContour);
58     }
59 }
60