1 /*
2  * Copyright (C) 2008 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 package android.graphics.cts;
17 
18 import static org.junit.Assert.assertArrayEquals;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 
25 import android.graphics.Camera;
26 import android.graphics.Matrix;
27 import android.graphics.Matrix.ScaleToFit;
28 import android.graphics.RectF;
29 import android.platform.test.annotations.DisabledOnRavenwood;
30 import android.platform.test.ravenwood.RavenwoodRule;
31 
32 import androidx.test.filters.SmallTest;
33 import androidx.test.runner.AndroidJUnit4;
34 
35 import org.junit.Before;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 
40 @SmallTest
41 @RunWith(AndroidJUnit4.class)
42 public class MatrixTest {
43     @Rule
44     public final RavenwoodRule mRavenwood = new RavenwoodRule();
45 
46     private Matrix mMatrix;
47     private float[] mValues;
48 
49     @Before
setup()50     public void setup() {
51         mMatrix = new Matrix();
52         mValues = new float[9];
53     }
54 
55     @Test
testConstructor()56     public void testConstructor() {
57         assertTrue(new Matrix().isIdentity());
58         assertTrue(new Matrix(mMatrix).isIdentity());
59     }
60 
61     @Test
testIsIdentity()62     public void testIsIdentity() {
63         assertTrue(mMatrix.isIdentity());
64         mMatrix.setScale(0f, 0f);
65         assertFalse(mMatrix.isIdentity());
66     }
67 
68     @Test
testIdentityMatrix()69     public void testIdentityMatrix() {
70         assertNotNull(Matrix.IDENTITY_MATRIX);
71         assertTrue(Matrix.IDENTITY_MATRIX.isIdentity());
72         assertTrue(Matrix.IDENTITY_MATRIX.isAffine());
73         assertTrue(Matrix.IDENTITY_MATRIX.rectStaysRect());
74     }
75 
76     @Test(expected = IllegalStateException.class)
testIdentityMatrixSet()77     public void testIdentityMatrixSet() {
78         Matrix m = new Matrix();
79         m.setRotate(90);
80         Matrix.IDENTITY_MATRIX.set(m);
81     }
82 
83     @Test(expected = IllegalStateException.class)
testIdentityMatrixReset()84     public void testIdentityMatrixReset() {
85         Matrix.IDENTITY_MATRIX.reset();
86     }
87 
88     @Test(expected = IllegalStateException.class)
testIdentityMatrixSetTranslate()89     public void testIdentityMatrixSetTranslate() {
90         Matrix.IDENTITY_MATRIX.setTranslate(1f, 1f);
91     }
92 
93     @Test(expected = IllegalStateException.class)
testIdentityMatrixSetScale()94     public void testIdentityMatrixSetScale() {
95         Matrix.IDENTITY_MATRIX.setScale(.5f, .5f);
96     }
97 
98     @Test(expected = IllegalStateException.class)
testIdentityMatrixSetScalePivot()99     public void testIdentityMatrixSetScalePivot() {
100         Matrix.IDENTITY_MATRIX.setScale(.5f, .5f, 10f, 10f);
101     }
102 
103     @Test(expected = IllegalStateException.class)
testIdentityMatrixSetRotate()104     public void testIdentityMatrixSetRotate() {
105         Matrix.IDENTITY_MATRIX.setRotate(60);
106     }
107 
108     @Test(expected = IllegalStateException.class)
testIdentityMatrixSetRotateAbout()109     public void testIdentityMatrixSetRotateAbout() {
110         Matrix.IDENTITY_MATRIX.setRotate(60, 100f, 100f);
111     }
112 
113     @Test(expected = IllegalStateException.class)
testIdentityMatrixSetSinCos()114     public void testIdentityMatrixSetSinCos() {
115         Matrix.IDENTITY_MATRIX.setSinCos(1f, 2f);
116     }
117 
118     @Test(expected = IllegalStateException.class)
testIdentityMatrixSetSinCosPivot()119     public void testIdentityMatrixSetSinCosPivot() {
120         Matrix.IDENTITY_MATRIX.setSinCos(1f, 2f, 3f, 4f);
121     }
122 
123     @Test(expected = IllegalStateException.class)
testIdentityMatrixSetSkew()124     public void testIdentityMatrixSetSkew() {
125         Matrix.IDENTITY_MATRIX.setSkew(1f, 2f);
126     }
127 
128     @Test(expected = IllegalStateException.class)
testIdentityMatrixSetSkewPivot()129     public void testIdentityMatrixSetSkewPivot() {
130         Matrix.IDENTITY_MATRIX.setSkew(1f, 2f, 3f, 4f);
131     }
132 
133     @Test(expected = IllegalStateException.class)
testIdentityMatrixSetConcat()134     public void testIdentityMatrixSetConcat() {
135         Matrix a = new Matrix();
136         Matrix b = new Matrix();
137         Matrix.IDENTITY_MATRIX.setConcat(a, b);
138     }
139 
140     @Test(expected = IllegalStateException.class)
testIdentityMatrixPreTranslate()141     public void testIdentityMatrixPreTranslate() {
142         Matrix.IDENTITY_MATRIX.preTranslate(10f, 10f);
143     }
144 
145     @Test(expected = IllegalStateException.class)
testIdentityMatrixPreScale()146     public void testIdentityMatrixPreScale() {
147         Matrix.IDENTITY_MATRIX.preScale(10f, 10f);
148     }
149 
150     @Test(expected = IllegalStateException.class)
testIdentityMatrixPreScalePivot()151     public void testIdentityMatrixPreScalePivot() {
152         Matrix.IDENTITY_MATRIX.preScale(10f, 10f, 100f, 100f);
153     }
154 
155     @Test(expected = IllegalStateException.class)
testIdentityMatrixPreRotate()156     public void testIdentityMatrixPreRotate() {
157         Matrix.IDENTITY_MATRIX.preRotate(10f);
158     }
159 
160     @Test(expected = IllegalStateException.class)
testIdentityMatrixPreRotatePivot()161     public void testIdentityMatrixPreRotatePivot() {
162         Matrix.IDENTITY_MATRIX.preRotate(10f, 10f, 10f);
163     }
164 
165     @Test(expected = IllegalStateException.class)
testIdentityMatrixPreSkew()166     public void testIdentityMatrixPreSkew() {
167         Matrix.IDENTITY_MATRIX.preSkew(1f, 3f);
168     }
169 
170     @Test(expected = IllegalStateException.class)
testIdentityMatrixPreSkewPivot()171     public void testIdentityMatrixPreSkewPivot() {
172         Matrix.IDENTITY_MATRIX.preSkew(1f, 3f, 4f, 7f);
173     }
174 
175     @Test(expected = IllegalStateException.class)
testIdentityMatrixPreConcat()176     public void testIdentityMatrixPreConcat() {
177         Matrix a = new Matrix();
178         Matrix.IDENTITY_MATRIX.preConcat(a);
179     }
180 
181     @Test(expected = IllegalStateException.class)
testIdentityMatrixPostTranslate()182     public void testIdentityMatrixPostTranslate() {
183         Matrix.IDENTITY_MATRIX.postTranslate(10f, 10f);
184     }
185 
186     @Test(expected = IllegalStateException.class)
testIdentityMatrixPostScale()187     public void testIdentityMatrixPostScale() {
188         Matrix.IDENTITY_MATRIX.postScale(10f, 10f);
189     }
190 
191     @Test(expected = IllegalStateException.class)
testIdentityMatrixPostScalePivot()192     public void testIdentityMatrixPostScalePivot() {
193         Matrix.IDENTITY_MATRIX.postScale(10f, 10f, 100f, 100f);
194     }
195 
196     @Test(expected = IllegalStateException.class)
testIdentityMatrixPostRotate()197     public void testIdentityMatrixPostRotate() {
198         Matrix.IDENTITY_MATRIX.postRotate(10f);
199     }
200 
201     @Test(expected = IllegalStateException.class)
testIdentityMatrixPostRotatePivot()202     public void testIdentityMatrixPostRotatePivot() {
203         Matrix.IDENTITY_MATRIX.postRotate(10f, 10f, 10f);
204     }
205 
206     @Test(expected = IllegalStateException.class)
testIdentityMatrixPostSkew()207     public void testIdentityMatrixPostSkew() {
208         Matrix.IDENTITY_MATRIX.postSkew(1f, 3f);
209     }
210 
211     @Test(expected = IllegalStateException.class)
testIdentityMatrixPostSkewPivot()212     public void testIdentityMatrixPostSkewPivot() {
213         Matrix.IDENTITY_MATRIX.postSkew(1f, 3f, 4f, 7f);
214     }
215 
216     @Test(expected = IllegalStateException.class)
testIdentityMatrixPostConcat()217     public void testIdentityMatrixPostConcat() {
218         Matrix a = new Matrix();
219         Matrix.IDENTITY_MATRIX.postConcat(a);
220     }
221 
222     @Test(expected = IllegalStateException.class)
testIdentityMatrixSetRectToRect()223     public void testIdentityMatrixSetRectToRect() {
224         Matrix.IDENTITY_MATRIX.setRectToRect(new RectF(), new RectF(), ScaleToFit.CENTER);
225     }
226 
227     @Test(expected = IllegalStateException.class)
testIdentityMatrixSetPolyToPoly()228     public void testIdentityMatrixSetPolyToPoly() {
229         float[] src = new float[9];
230         src[0] = 100f;
231         float[] dst = new float[9];
232         dst[0] = 200f;
233         dst[1] = 300f;
234         Matrix.IDENTITY_MATRIX.setPolyToPoly(src, 0, dst, 0, 1);
235     }
236 
237     @Test(expected = IllegalStateException.class)
testIdentityMatrixSetValues()238     public void testIdentityMatrixSetValues() {
239         Matrix.IDENTITY_MATRIX.setValues(new float[9]);
240     }
241 
242     @Test
testRectStaysRect()243     public void testRectStaysRect() {
244         assertTrue(mMatrix.rectStaysRect());
245         mMatrix.postRotate(80);
246         assertFalse(mMatrix.rectStaysRect());
247     }
248 
249     @Test
testIsAffine()250     public void testIsAffine() {
251         assertTrue(mMatrix.isAffine());
252 
253         // translate/scale/rotateZ don't affect whether matrix is affine
254         mMatrix.postTranslate(50, 50);
255         mMatrix.postScale(20, 4);
256         mMatrix.postRotate(80);
257         assertTrue(mMatrix.isAffine());
258 
259         mMatrix.setValues(new float[] {0, 0, 0, 0, 0, 0, 0, 0, 0.5f});
260         assertFalse(mMatrix.isAffine());
261     }
262 
263     @Test
264     @DisabledOnRavenwood(blockedBy = {Camera.class})
testIsAffine_withCamera()265     public void testIsAffine_withCamera() {
266         Camera camera = new Camera();
267         camera.setLocation(0, 0, 100);
268         camera.rotateX(20);
269         camera.getMatrix(mMatrix);
270         assertFalse(mMatrix.isAffine());
271     }
272 
273     @Test
testSet()274     public void testSet() {
275         mValues[0] = 1000;
276         mMatrix.getValues(mValues);
277         Matrix matrix = new Matrix();
278         matrix.set(mMatrix);
279         mValues = new float[9];
280         mValues[0] = 2000;
281         matrix.getValues(mValues);
282         assertEquals(1f, mValues[0], 0f);
283     }
284 
285     @Test
testEquals()286     public void testEquals() {
287         mMatrix.setScale(1f, 2f);
288         Matrix matrix = new Matrix();
289         matrix.set(mMatrix);
290         assertFalse(mMatrix.equals(null));
291         assertFalse(mMatrix.equals(new String()));
292         assertTrue(mMatrix.equals(matrix));
293     }
294 
295     @Test
testReset()296     public void testReset() {
297         mMatrix.setScale(1f, 2f, 3f, 4f);
298         verifyMatrix(new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 2.0f, -4.0f, 0.0f, 0.0f, 1.0f });
299         mMatrix.reset();
300         verifyMatrix(new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f });
301     }
302 
303     @Test
testSetScale()304     public void testSetScale() {
305         verifyMatrix(new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f });
306         mMatrix.setScale(1f, 2f);
307         verifyMatrix(new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 2.0f, 0.0f, 0.0f, 0.0f, 1.0f });
308     }
309 
310     @Test
testSetScale2()311     public void testSetScale2() {
312         verifyMatrix(new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f });
313 
314         mMatrix.setScale(1f, 2f, 3f, 4f);
315         verifyMatrix(new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 2.0f, -4.0f, 0.0f, 0.0f, 1.0f });
316     }
317 
318     @Test
testSetRotate()319     public void testSetRotate() {
320         mMatrix.setRotate(1f);
321         verifyMatrix(new float[] {
322             0.9998477f, -0.017452406f, 0.0f, 0.017452406f, 0.9998477f, 0.0f, 0.0f, 0.0f, 1.0f
323         });
324     }
325 
326     @Test
testSetRotate2()327     public void testSetRotate2() {
328         mMatrix.setRotate(1f, 2f, 3f);
329         verifyMatrix(new float[] {
330             0.9998477f, -0.017452406f, 0.0526618f, 0.017452406f, 0.9998477f, -0.034447942f, 0.0f,
331                 0.0f, 1.0f
332         });
333     }
334 
335     @Test
testSetSinCos()336     public void testSetSinCos() {
337         mMatrix.setSinCos(1f, 2f);
338         verifyMatrix(new float[] { 2.0f, -1.0f, 0.0f, 1.0f, 2.0f, 0.0f, 0.0f, 0.0f, 1.0f });
339     }
340 
341     @Test
testSetSinCos2()342     public void testSetSinCos2() {
343         mMatrix.setSinCos(1f, 2f, 3f, 4f);
344         verifyMatrix(new float[] { 2.0f, -1.0f, 1.0f, 1.0f, 2.0f, -7.0f, 0.0f, 0.0f, 1.0f });
345     }
346 
347     @Test
testSetSkew()348     public void testSetSkew() {
349         mMatrix.setSkew(1f, 2f);
350         verifyMatrix(new float[] { 1.0f, 1.0f, 0.0f, 2.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f });
351     }
352 
353     @Test
testSetSkew2()354     public void testSetSkew2() {
355         mMatrix.setSkew(1f, 2f, 3f, 4f);
356         verifyMatrix(new float[] { 1.0f, 1.0f, -4.0f, 2.0f, 1.0f, -6.0f, 0.0f, 0.0f, 1.0f });
357     }
358 
359     @Test
testSetConcat()360     public void testSetConcat() {
361         Matrix a = new Matrix();
362         Matrix b = new Matrix();
363         mMatrix.setConcat(a, b);
364         verifyMatrix(new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f });
365         mMatrix = new Matrix();
366         mMatrix.setConcat(mMatrix, b);
367         mMatrix.setConcat(a, b);
368         verifyMatrix(new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f });
369         mMatrix = new Matrix();
370         mValues = new float[9];
371         mMatrix.setConcat(a, mMatrix);
372         mMatrix.getValues(mValues);
373         verifyMatrix(new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f });
374     }
375 
376     @Test
testPreTranslate()377     public void testPreTranslate() {
378         assertTrue(mMatrix.preTranslate(1f, 2f));
379         verifyMatrix(new float[] { 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 2.0f, 0.0f, 0.0f, 1.0f });
380     }
381 
382     @Test
testPreScale()383     public void testPreScale() {
384         assertTrue(mMatrix.preScale(1f, 2f));
385         verifyMatrix(new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 2.0f, 0.0f, 0.0f, 0.0f, 1.0f });
386     }
387 
388     @Test
testPreScale2()389     public void testPreScale2() {
390         assertTrue(mMatrix.preScale(1f, 2f, 3f, 4f));
391         verifyMatrix(new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 2.0f, -4.0f, 0.0f, 0.0f, 1.0f });
392     }
393 
394     @Test
testPreRotate()395     public void testPreRotate() {
396         assertTrue(mMatrix.preRotate(1f));
397         verifyMatrix(new float[] {
398             0.9998477f, -0.017452406f, 0.0f, 0.017452406f, 0.9998477f, 0.0f, 0.0f, 0.0f, 1.0f
399         });
400     }
401 
402     @Test
testPreRotate2()403     public void testPreRotate2() {
404         assertTrue(mMatrix.preRotate(1f, 2f, 3f));
405         float[] values = new float[9];
406         mMatrix.getValues(values);
407         verifyMatrix(new float[] {
408             0.9998477f, -0.017452406f, 0.0526618f, 0.017452406f, 0.9998477f, -0.034447942f, 0.0f,
409                 0.0f, 1.0f
410         });
411     }
412 
413     @Test
testPreSkew()414     public void testPreSkew() {
415         assertTrue(mMatrix.preSkew(1f, 2f));
416         verifyMatrix(new float[] { 1.0f, 1.0f, 0.0f, 2.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f });
417     }
418 
419     @Test
testPreSkew2()420     public void testPreSkew2() {
421         assertTrue(mMatrix.preSkew(1f, 2f, 3f, 4f));
422         verifyMatrix(new float[] { 1.0f, 1.0f, -4.0f, 2.0f, 1.0f, -6.0f, 0.0f, 0.0f, 1.0f });
423     }
424 
425     @Test
testPreConcat()426     public void testPreConcat() {
427         float[] values = new float[9];
428         values[0] = 1000;
429         Matrix matrix = new Matrix();
430         matrix.setValues(values);
431         assertTrue(mMatrix.preConcat(matrix));
432         verifyMatrix(new float[] { 1000.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f });
433     }
434 
435     @Test
testPostTranslate()436     public void testPostTranslate() {
437         assertTrue(mMatrix.postTranslate(1f, 2f));
438         verifyMatrix(new float[] { 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 2.0f, 0.0f, 0.0f, 1.0f });
439     }
440 
441     @Test
testPostScale()442     public void testPostScale() {
443         assertTrue(mMatrix.postScale(1f, 2f));
444         verifyMatrix(new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 2.0f, 0.0f, 0.0f, 0.0f, 1.0f });
445     }
446 
447     @Test
testPostScale2()448     public void testPostScale2() {
449         assertTrue(mMatrix.postScale(1f, 2f, 3f, 4f));
450         verifyMatrix(new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 2.0f, -4.0f, 0.0f, 0.0f, 1.0f });
451     }
452 
453     @Test
testPostRotate()454     public void testPostRotate() {
455         assertTrue(mMatrix.postRotate(1f));
456         verifyMatrix(new float[] {
457             0.9998477f, -0.017452406f, 0.0f, 0.017452406f, 0.9998477f, 0.0f, 0.0f, 0.0f, 1.0f
458         });
459     }
460 
461     @Test
testPostRotate2()462     public void testPostRotate2() {
463         assertTrue(mMatrix.postRotate(1f, 2f, 3f));
464         verifyMatrix(new float[] {
465             0.9998477f, -0.017452406f, 0.0526618f, 0.017452406f, 0.9998477f, -0.034447942f, 0.0f,
466                 0.0f, 1.0f
467         });
468     }
469 
470     @Test
testPostSkew()471     public void testPostSkew() {
472         assertTrue(mMatrix.postSkew(1f, 2f));
473         verifyMatrix(new float[] { 1.0f, 1.0f, 0.0f, 2.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f });
474     }
475 
476     @Test
testPostSkew2()477     public void testPostSkew2() {
478         assertTrue(mMatrix.postSkew(1f, 2f, 3f, 4f));
479         verifyMatrix(new float[] { 1.0f, 1.0f, -4.0f, 2.0f, 1.0f, -6.0f, 0.0f, 0.0f, 1.0f });
480     }
481 
482     @Test
testPostConcat()483     public void testPostConcat() {
484         Matrix matrix = new Matrix();
485         float[] values = new float[9];
486         values[0] = 1000;
487         matrix.setValues(values);
488         assertTrue(mMatrix.postConcat(matrix));
489 
490         verifyMatrix(new float[] { 1000.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f });
491     }
492 
493     @Test
testSetRectToRect()494     public void testSetRectToRect() {
495         RectF r1 = new RectF();
496         r1.set(1f, 2f, 3f, 3f);
497         RectF r2 = new RectF();
498         r1.set(10f, 20f, 30f, 30f);
499         assertTrue(mMatrix.setRectToRect(r1, r2, ScaleToFit.CENTER));
500         verifyMatrix(new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f });
501         mMatrix.setRectToRect(r1, r2, ScaleToFit.END);
502 
503         verifyMatrix(new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f });
504         mMatrix.setRectToRect(r1, r2, ScaleToFit.FILL);
505         verifyMatrix(new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f });
506         mMatrix.setRectToRect(r1, r2, ScaleToFit.START);
507         verifyMatrix(new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f });
508 
509         assertFalse(mMatrix.setRectToRect(r2, r1, ScaleToFit.CENTER));
510 
511         verifyMatrix(new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f });
512         assertFalse(mMatrix.setRectToRect(r2, r1, ScaleToFit.FILL));
513         verifyMatrix(new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f });
514         assertFalse(mMatrix.setRectToRect(r2, r1, ScaleToFit.START));
515         verifyMatrix(new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f });
516         assertFalse(mMatrix.setRectToRect(r2, r1, ScaleToFit.END));
517         verifyMatrix(new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f });
518     }
519 
520     @Test(expected=Exception.class)
testSetRectToRectNull()521     public void testSetRectToRectNull() {
522         mMatrix.setRectToRect(null, null, ScaleToFit.CENTER);
523     }
524 
525     @Test
testInvert()526     public void testInvert() {
527         Matrix matrix = new Matrix();
528         float[] values = new float[9];
529         values[0] = 1000f;
530         matrix.setValues(values);
531         assertTrue(mMatrix.invert(matrix));
532         verifyMatrix(new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f });
533     }
534 
535     @Test(expected=Exception.class)
testInvertNull()536     public void testInvertNull() {
537         mMatrix.invert(null);
538     }
539 
540     @Test
testSetPolyToPoly()541     public void testSetPolyToPoly() {
542         float[] src = new float[9];
543         src[0] = 100f;
544         float[] dst = new float[9];
545         dst[0] = 200f;
546         dst[1] = 300f;
547         assertTrue(mMatrix.setPolyToPoly(src, 0, dst, 0, 1));
548         verifyMatrix(new float[] { 1.0f, 0.0f, 100.0f, 0.0f, 1.0f, 300.0f, 0.0f, 0.0f, 1.0f });
549         try {
550             mMatrix.setPolyToPoly(src, 0, dst, 0, 5);
551             fail("should throw exception");
552         } catch (Exception ignored) {
553         }
554     }
555 
556     @Test
testMapPoints()557     public void testMapPoints() {
558         float[] value = new float[9];
559         value[0] = 100f;
560         mMatrix.mapPoints(value);
561         assertEquals(value[0], 100f, 0f);
562     }
563 
564     @Test(expected=Exception.class)
testMapPointsNull()565     public void testMapPointsNull() {
566         mMatrix.mapPoints(null);
567     }
568 
569     @Test
testMapPoints2()570     public void testMapPoints2() {
571         float[] dst = new float[9];
572         dst[0] = 100f;
573         float[] src = new float[9];
574         src[0] = 200f;
575         mMatrix.mapPoints(dst, src);
576         assertEquals(dst[0], 200f, 0f);
577     }
578 
579     @Test(expected=Exception.class)
testMapPointsArraysMismatch()580     public void testMapPointsArraysMismatch() {
581         mMatrix.mapPoints(new float[8], new float[9]);
582     }
583 
584     @Test
testMapPointsWithIndices()585     public void testMapPointsWithIndices() {
586         float[] dst = new float[9];
587         dst[0] = 100f;
588         float[] src = new float[9];
589         src[0] = 200f;
590         mMatrix.mapPoints(dst, 0, src, 0, 9 >> 1);
591         assertEquals(dst[0], 200f, 0f);
592     }
593 
594     @Test(expected=Exception.class)
testMapPointsWithIndicesNull()595     public void testMapPointsWithIndicesNull() {
596         mMatrix.mapPoints(null, 0, new float[9], 0, 1);
597     }
598 
599     @Test
testMapVectors()600     public void testMapVectors() {
601         float[] values = new float[9];
602         values[0] = 100f;
603         mMatrix.mapVectors(values);
604         assertEquals(values[0], 100f, 0f);
605     }
606 
607     @Test(expected=Exception.class)
testMapVectorsNull()608     public void testMapVectorsNull() {
609         mMatrix.mapVectors(null);
610     }
611 
612     @Test
testMapVectorsDstSrc()613     public void testMapVectorsDstSrc() {
614         float[] src = new float[9];
615         src[0] = 100f;
616         float[] dst = new float[9];
617         dst[0] = 200f;
618         mMatrix.mapVectors(dst, src);
619         assertEquals(dst[0], 100f, 0f);
620     }
621 
622     @Test(expected=Exception.class)
testMapVectorsDstSrcMismatch()623     public void testMapVectorsDstSrcMismatch() {
624         mMatrix.mapVectors(new float[9], new float[8]);
625     }
626 
627     @Test
testMapVectorsDstSrcWithIndices()628     public void testMapVectorsDstSrcWithIndices() {
629         float[] src = new float[9];
630         src[0] = 100f;
631         float[] dst = new float[9];
632         dst[0] = 200f;
633         mMatrix.mapVectors(dst, 0, src, 0, 1);
634         assertEquals(dst[0], 100f, 0f);
635         try {
636             mMatrix.mapVectors(dst, 0, src, 0, 10);
637             fail("should throw exception");
638         } catch (Exception ignored) {
639         }
640     }
641 
642     @Test
testMapRadius()643     public void testMapRadius() {
644         assertEquals(mMatrix.mapRadius(100f), 100f, 0f);
645         assertEquals(mMatrix.mapRadius(Float.MAX_VALUE),
646                 Float.POSITIVE_INFINITY, 0f);
647         assertEquals(mMatrix.mapRadius(Float.MIN_VALUE), 0f, 0f);
648     }
649 
650     @Test
testMapRect()651     public void testMapRect() {
652         RectF r = new RectF();
653         r.set(1f, 2f, 3f, 4f);
654         assertTrue(mMatrix.mapRect(r));
655         assertEquals(1f, r.left, 0f);
656         assertEquals(2f, r.top, 0f);
657         assertEquals(3f, r.right, 0f);
658         assertEquals(4f, r.bottom, 0f);
659     }
660 
661     @Test(expected=Exception.class)
testMapRectNull()662     public void testMapRectNull() {
663         mMatrix.mapRect(null);
664     }
665 
666     @Test
testMapRectDstSrc()667     public void testMapRectDstSrc() {
668         RectF dst = new RectF();
669         dst.set(100f, 100f, 200f, 200f);
670         RectF src = new RectF();
671         dst.set(10f, 10f, 20f, 20f);
672         assertTrue(mMatrix.mapRect(dst, src));
673         assertEquals(0f, dst.left, 0f);
674         assertEquals(0f, dst.top, 0f);
675         assertEquals(0f, dst.right, 0f);
676         assertEquals(0f, dst.bottom, 0f);
677 
678         assertEquals(0f, src.left, 0f);
679         assertEquals(0f, src.top, 0f);
680         assertEquals(0f, src.right, 0f);
681         assertEquals(0f, src.bottom, 0f);
682     }
683 
684     @Test(expected=Exception.class)
testMapRectDstSrcNull()685     public void testMapRectDstSrcNull() {
686         mMatrix.mapRect(null, null);
687     }
688 
689     @Test
testAccessValues()690     public void testAccessValues() {
691         Matrix matrix = new Matrix();
692         mMatrix.invert(matrix);
693         float[] values = new float[9];
694         values[0] = 9f;
695         values[1] = 100f;
696         mMatrix.setValues(values);
697         values = new float[9];
698         mMatrix.getValues(values);
699         assertArrayEquals(new float[] {
700                 9.0f, 100.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f
701         }, values, 0.0f);
702     }
703 
704     @Test
testToString()705     public void testToString() {
706         assertNotNull(mMatrix.toString());
707     }
708 
709     @Test
testToShortString()710     public void testToShortString() {
711         String expect = "[1.0, 0.0, 0.0][0.0, 1.0, 0.0][0.0, 0.0, 1.0]";
712         assertEquals(expect, mMatrix.toShortString());
713     }
714 
715     @Test
testSetTranslate()716     public void testSetTranslate() {
717         mMatrix.setTranslate(2f, 3f);
718         verifyMatrix(new float[] { 1.0f, 0.0f, 2.0f, 0.0f, 1.0f, 3.0f, 0.0f, 0.0f, 1.0f });
719     }
720 
verifyMatrix(float[] expected)721     private void verifyMatrix(float[] expected) {
722         if ((expected == null) || (expected.length != 9)) {
723             fail("Expected does not have 9 elements");
724         }
725         final float[] actualValues = new float[9];
726         mMatrix.getValues(actualValues);
727         assertArrayEquals(expected, actualValues, 0.0f);
728     }
729 }
730