1 package android.graphics; 2 3 import static com.google.common.truth.Truth.assertThat; 4 import static org.junit.Assert.assertThrows; 5 6 import android.graphics.Matrix.ScaleToFit; 7 import androidx.test.ext.junit.runners.AndroidJUnit4; 8 import org.junit.Test; 9 import org.junit.runner.RunWith; 10 11 /** Compatibility test for {@link Matrix} */ 12 @RunWith(AndroidJUnit4.class) 13 public final class MatrixTest { 14 15 @Test mapRadius()16 public void mapRadius() throws Exception { 17 Matrix matrix = new Matrix(); 18 19 assertThat(matrix.mapRadius(100f)).isEqualTo(100f); 20 assertThat(matrix.mapRadius(Float.MAX_VALUE)).isEqualTo(Float.POSITIVE_INFINITY); 21 assertThat(matrix.mapRadius(Float.MIN_VALUE)).isEqualTo(0f); 22 23 matrix.postScale(2.0f, 2.0f); 24 assertThat(matrix.mapRadius(1.0f)).isWithin(0.01f).of(2.0f); 25 } 26 27 @Test mapPoints()28 public void mapPoints() { 29 float[] value = new float[9]; 30 value[0] = 100f; 31 new Matrix().mapPoints(value); 32 assertThat(value[0]).isEqualTo(100f); 33 } 34 35 @Test mapPointsNull()36 public void mapPointsNull() { 37 assertThrows(Exception.class, () -> new Matrix().mapPoints(null)); 38 } 39 40 @Test mapPoints2()41 public void mapPoints2() { 42 float[] dst = new float[9]; 43 dst[0] = 100f; 44 float[] src = new float[9]; 45 src[0] = 200f; 46 new Matrix().mapPoints(dst, src); 47 assertThat(dst[0]).isEqualTo(200f); 48 } 49 50 @Test mapPointsArraysMismatch()51 public void mapPointsArraysMismatch() { 52 assertThrows(Exception.class, () -> new Matrix().mapPoints(new float[8], new float[9])); 53 } 54 55 @Test mapPointsWithIndices()56 public void mapPointsWithIndices() { 57 float[] dst = new float[9]; 58 dst[0] = 100f; 59 float[] src = new float[9]; 60 src[0] = 200f; 61 new Matrix().mapPoints(dst, 0, src, 0, 9 >> 1); 62 assertThat(dst[0]).isEqualTo(200f); 63 } 64 65 @Test mapPointsWithIndicesNull()66 public void mapPointsWithIndicesNull() { 67 assertThrows(Exception.class, () -> new Matrix().mapPoints(null, 0, new float[9], 0, 1)); 68 } 69 70 @Test setRectToRect()71 public void setRectToRect() { 72 RectF r1 = new RectF(); 73 r1.set(1f, 2f, 3f, 3f); 74 RectF r2 = new RectF(); 75 r1.set(10f, 20f, 30f, 30f); 76 Matrix matrix = new Matrix(); 77 float[] result = new float[9]; 78 79 assertThat(matrix.setRectToRect(r1, r2, ScaleToFit.CENTER)).isTrue(); 80 matrix.getValues(result); 81 assertThat(result) 82 .isEqualTo(new float[] {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}); 83 84 matrix.setRectToRect(r1, r2, ScaleToFit.END); 85 matrix.getValues(result); 86 assertThat(result) 87 .isEqualTo(new float[] {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}); 88 89 matrix.setRectToRect(r1, r2, ScaleToFit.FILL); 90 matrix.getValues(result); 91 assertThat(result) 92 .isEqualTo(new float[] {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}); 93 94 matrix.setRectToRect(r1, r2, ScaleToFit.START); 95 matrix.getValues(result); 96 assertThat(result) 97 .isEqualTo(new float[] {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}); 98 99 assertThat(matrix.setRectToRect(r2, r1, ScaleToFit.CENTER)).isFalse(); 100 matrix.getValues(result); 101 assertThat(result) 102 .isEqualTo(new float[] {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f}); 103 104 assertThat(matrix.setRectToRect(r2, r1, ScaleToFit.FILL)).isFalse(); 105 matrix.getValues(result); 106 assertThat(result) 107 .isEqualTo(new float[] {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f}); 108 109 assertThat(matrix.setRectToRect(r2, r1, ScaleToFit.START)).isFalse(); 110 matrix.getValues(result); 111 assertThat(result) 112 .isEqualTo(new float[] {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f}); 113 114 assertThat(matrix.setRectToRect(r2, r1, ScaleToFit.END)).isFalse(); 115 matrix.getValues(result); 116 assertThat(result) 117 .isEqualTo(new float[] {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f}); 118 } 119 120 @Test testSetRectToRectNull()121 public void testSetRectToRectNull() { 122 assertThrows(Exception.class, () -> new Matrix().setRectToRect(null, null, ScaleToFit.CENTER)); 123 } 124 } 125