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.integrationtests.nativegraphics; 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, 71 () -> { 72 pathMeasure.getPosTan(distance, pos, tan); 73 }); 74 } 75 76 @Test testGetPosTan()77 public void testGetPosTan() { 78 float distance = 1f; 79 float[] pos2 = {1f, 2f}; 80 float[] tan2 = {1f, 3f}; 81 assertFalse(pathMeasure.getPosTan(distance, pos2, tan2)); 82 83 pathMeasure.setPath(path, true); 84 path.addRect(1f, 2f, 3f, 4f, Path.Direction.CW); 85 pathMeasure.setPath(path, true); 86 float[] pos3 = {1f, 2f, 3f, 4f}; 87 float[] tan3 = {1f, 2f, 3f, 4f}; 88 assertTrue(pathMeasure.getPosTan(0f, pos3, tan3)); 89 } 90 91 @Test testNextContour()92 public void testNextContour() { 93 assertFalse(pathMeasure.nextContour()); 94 path.addRect(1, 2, 3, 4, Path.Direction.CW); 95 path.addRect(1, 2, 3, 4, Path.Direction.CW); 96 pathMeasure.setPath(path, true); 97 assertTrue(pathMeasure.nextContour()); 98 assertFalse(pathMeasure.nextContour()); 99 } 100 101 @Test testGetLength()102 public void testGetLength() { 103 assertEquals(0f, pathMeasure.getLength(), 0.0f); 104 path.addRect(1, 2, 3, 4, Path.Direction.CW); 105 pathMeasure.setPath(path, true); 106 assertEquals(8.0f, pathMeasure.getLength(), 0.0f); 107 } 108 109 @Test testIsClosed()110 public void testIsClosed() { 111 Path circle = new Path(); 112 circle.addCircle(0, 0, 1, Direction.CW); 113 114 PathMeasure measure = new PathMeasure(circle, false); 115 assertTrue(measure.isClosed()); 116 measure.setPath(circle, true); 117 assertTrue(measure.isClosed()); 118 119 Path line = new Path(); 120 line.lineTo(5, 5); 121 122 measure.setPath(line, false); 123 assertFalse(measure.isClosed()); 124 measure.setPath(line, true); 125 assertTrue(measure.isClosed()); 126 } 127 128 @Test testSetPath()129 public void testSetPath() { 130 pathMeasure.setPath(path, true); 131 // There is no getter and we can't obtain any status about it. 132 } 133 134 @Test testGetSegment()135 public void testGetSegment() { 136 assertEquals(0f, pathMeasure.getLength(), 0.0f); 137 path.addRect(1, 2, 3, 4, Path.Direction.CW); 138 pathMeasure.setPath(path, true); 139 assertEquals(8f, pathMeasure.getLength(), 0.0f); 140 Path dst = new Path(); 141 assertTrue(pathMeasure.getSegment(0, pathMeasure.getLength(), dst, true)); 142 assertFalse(pathMeasure.getSegment(pathMeasure.getLength(), 0, dst, true)); 143 } 144 145 @Test testGetMatrix()146 public void testGetMatrix() { 147 Matrix matrix = new Matrix(); 148 assertFalse(pathMeasure.getMatrix(1f, matrix, PathMeasure.POSITION_MATRIX_FLAG)); 149 matrix.setScale(1f, 2f); 150 path.addRect(1f, 2f, 3f, 4f, Path.Direction.CW); 151 pathMeasure.setPath(path, true); 152 assertTrue(pathMeasure.getMatrix(0f, matrix, PathMeasure.TANGENT_MATRIX_FLAG)); 153 } 154 } 155