1 /* 2 * Copyright (C) 2007 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 * This test is created from Android CTS tests: 17 * 18 * https://cs.android.com/android/platform/superproject/+/master:cts/tests/tests/graphics/src/android/graphics/cts/PathMeasureTest.java 19 */ 20 21 package org.robolectric.shadows; 22 23 import static android.os.Build.VERSION_CODES.O; 24 import static org.junit.Assert.assertEquals; 25 import static org.junit.Assert.assertFalse; 26 import static org.junit.Assert.assertThrows; 27 import static org.junit.Assert.assertTrue; 28 29 import android.graphics.Matrix; 30 import android.graphics.Path; 31 import android.graphics.Path.Direction; 32 import android.graphics.PathMeasure; 33 import androidx.test.ext.junit.runners.AndroidJUnit4; 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.robolectric.annotation.Config; 38 39 @RunWith(AndroidJUnit4.class) 40 @Config(minSdk = O) 41 public class ShadowNativePathMeasureTest { 42 private PathMeasure pathMeasure; 43 private Path path; 44 45 @Before setup()46 public void setup() { 47 path = new Path(); 48 pathMeasure = new PathMeasure(); 49 } 50 51 @Test testConstructor()52 public void testConstructor() { 53 pathMeasure = new PathMeasure(); 54 55 // new the PathMeasure instance 56 Path path = new Path(); 57 pathMeasure = new PathMeasure(path, true); 58 59 // new the PathMeasure instance 60 pathMeasure = new PathMeasure(path, false); 61 } 62 63 @Test testGetPosTanArraysTooSmall()64 public void testGetPosTanArraysTooSmall() { 65 float distance = 1f; 66 float[] pos = {1f}; 67 float[] tan = {1f}; 68 69 assertThrows( 70 ArrayIndexOutOfBoundsException.class, () -> pathMeasure.getPosTan(distance, pos, tan)); 71 } 72 73 @Test testGetPosTan()74 public void testGetPosTan() { 75 float distance = 1f; 76 float[] pos2 = {1f, 2f}; 77 float[] tan2 = {1f, 3f}; 78 assertFalse(pathMeasure.getPosTan(distance, pos2, tan2)); 79 80 pathMeasure.setPath(path, true); 81 path.addRect(1f, 2f, 3f, 4f, Path.Direction.CW); 82 pathMeasure.setPath(path, true); 83 float[] pos3 = {1f, 2f, 3f, 4f}; 84 float[] tan3 = {1f, 2f, 3f, 4f}; 85 assertTrue(pathMeasure.getPosTan(0f, pos3, tan3)); 86 } 87 88 @Test testNextContour()89 public void testNextContour() { 90 assertFalse(pathMeasure.nextContour()); 91 path.addRect(1, 2, 3, 4, Path.Direction.CW); 92 path.addRect(1, 2, 3, 4, Path.Direction.CW); 93 pathMeasure.setPath(path, true); 94 assertTrue(pathMeasure.nextContour()); 95 assertFalse(pathMeasure.nextContour()); 96 } 97 98 @Test testGetLength()99 public void testGetLength() { 100 assertEquals(0f, pathMeasure.getLength(), 0.0f); 101 path.addRect(1, 2, 3, 4, Path.Direction.CW); 102 pathMeasure.setPath(path, true); 103 assertEquals(8.0f, pathMeasure.getLength(), 0.0f); 104 } 105 106 @Test testIsClosed()107 public void testIsClosed() { 108 Path circle = new Path(); 109 circle.addCircle(0, 0, 1, Direction.CW); 110 111 PathMeasure measure = new PathMeasure(circle, false); 112 assertTrue(measure.isClosed()); 113 measure.setPath(circle, true); 114 assertTrue(measure.isClosed()); 115 116 Path line = new Path(); 117 line.lineTo(5, 5); 118 119 measure.setPath(line, false); 120 assertFalse(measure.isClosed()); 121 measure.setPath(line, true); 122 assertTrue(measure.isClosed()); 123 } 124 125 @Test testSetPath()126 public void testSetPath() { 127 pathMeasure.setPath(path, true); 128 // There is no getter and we can't obtain any status about it. 129 } 130 131 @Test testGetSegment()132 public void testGetSegment() { 133 assertEquals(0f, pathMeasure.getLength(), 0.0f); 134 path.addRect(1, 2, 3, 4, Path.Direction.CW); 135 pathMeasure.setPath(path, true); 136 assertEquals(8f, pathMeasure.getLength(), 0.0f); 137 Path dst = new Path(); 138 assertTrue(pathMeasure.getSegment(0, pathMeasure.getLength(), dst, true)); 139 assertFalse(pathMeasure.getSegment(pathMeasure.getLength(), 0, dst, true)); 140 } 141 142 @Test testGetMatrix()143 public void testGetMatrix() { 144 Matrix matrix = new Matrix(); 145 assertFalse(pathMeasure.getMatrix(1f, matrix, PathMeasure.POSITION_MATRIX_FLAG)); 146 matrix.setScale(1f, 2f); 147 path.addRect(1f, 2f, 3f, 4f, Path.Direction.CW); 148 pathMeasure.setPath(path, true); 149 assertTrue(pathMeasure.getMatrix(0f, matrix, PathMeasure.TANGENT_MATRIX_FLAG)); 150 } 151 } 152