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