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