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 17package android.view.animation.cts; 18 19import android.graphics.Matrix; 20import android.test.AndroidTestCase; 21import android.view.animation.Transformation; 22 23public class TransformationTest extends AndroidTestCase { 24 25 public void testConstructor() { 26 new Transformation(); 27 } 28 29 public void testCompose() { 30 final Transformation t1 = new Transformation(); 31 final Transformation t2 = new Transformation(); 32 t1.setAlpha(0.5f); 33 t2.setAlpha(0.4f); 34 t1.getMatrix().setScale(3, 1); 35 t2.getMatrix().setScale(3, 1); 36 t1.setTransformationType(Transformation.TYPE_MATRIX); 37 t2.setTransformationType(Transformation.TYPE_ALPHA); 38 t2.compose(t1); 39 40 Matrix expectedMatrix = new Matrix(); 41 expectedMatrix.setScale(9, 1); 42 assertEquals(expectedMatrix, t2.getMatrix()); 43 assertEquals(0.4f * 0.5f, t2.getAlpha()); 44 assertEquals(Transformation.TYPE_ALPHA, t2.getTransformationType()); 45 46 t1.setTransformationType(Transformation.TYPE_IDENTITY); 47 t2.compose(t1); 48 expectedMatrix = new Matrix(); 49 expectedMatrix.setScale(27, 1); 50 assertEquals(expectedMatrix, t2.getMatrix()); 51 assertEquals(0.4f * 0.5f * 0.5f, t2.getAlpha()); 52 assertEquals(Transformation.TYPE_ALPHA, t2.getTransformationType()); 53 } 54 55 public void testClear() { 56 final Transformation t1 = new Transformation(); 57 final Transformation t2 = new Transformation(); 58 t2.set(t1); 59 assertTransformationEquals(t1, t2); 60 61 // Change the t2 62 t2.setAlpha(0.0f); 63 t2.getMatrix().setScale(2, 3); 64 t2.setTransformationType(Transformation.TYPE_ALPHA); 65 assertTransformationNotSame(t1, t2); 66 67 // Clear the change 68 t2.clear(); 69 assertTransformationEquals(t1, t2); 70 } 71 72 private void assertTransformationNotSame(Transformation expected, Transformation actual) { 73 assertNotSame(expected.getAlpha(), actual.getAlpha()); 74 assertFalse(expected.getMatrix().equals(actual.getMatrix())); 75 assertNotSame(expected.getTransformationType(), actual.getTransformationType()); 76 } 77 78 private void assertTransformationEquals(Transformation expected, Transformation actual) { 79 assertEquals(expected.getAlpha(), actual.getAlpha()); 80 assertEquals(expected.getMatrix(), actual.getMatrix()); 81 assertEquals(expected.getTransformationType(), actual.getTransformationType()); 82 } 83 84 public void testAccessTransformationType() { 85 final Transformation transformation = new Transformation(); 86 87 // From Javadoc of {@link Transformation#clear()}, we see the default type is TYPE_BOTH. 88 assertEquals(Transformation.TYPE_BOTH, transformation.getTransformationType()); 89 90 transformation.setTransformationType(Transformation.TYPE_IDENTITY); 91 assertEquals(Transformation.TYPE_IDENTITY, transformation.getTransformationType()); 92 93 transformation.setTransformationType(Transformation.TYPE_ALPHA); 94 assertEquals(Transformation.TYPE_ALPHA, transformation.getTransformationType()); 95 96 transformation.setTransformationType(Transformation.TYPE_MATRIX); 97 assertEquals(Transformation.TYPE_MATRIX, transformation.getTransformationType()); 98 99 transformation.setTransformationType(Transformation.TYPE_BOTH); 100 assertEquals(Transformation.TYPE_BOTH, transformation.getTransformationType()); 101 } 102 103 public void testSet() { 104 final Transformation t1 = new Transformation(); 105 t1.setAlpha(0.0f); 106 final Transformation t2 = new Transformation(); 107 t2.set(t1); 108 assertTransformationEquals(t1, t2); 109 } 110 111 public void testAccessAlpha() { 112 final Transformation transformation = new Transformation(); 113 114 transformation.setAlpha(0.0f); 115 assertEquals(0.0f, transformation.getAlpha()); 116 117 transformation.setAlpha(0.5f); 118 assertEquals(0.5f, transformation.getAlpha()); 119 120 transformation.setAlpha(1.0f); 121 assertEquals(1.0f, transformation.getAlpha()); 122 } 123 124 public void testToString() { 125 assertNotNull(new Transformation().toString()); 126 assertNotNull(new Transformation().toShortString()); 127 } 128 129 public void testGetMatrix() { 130 final Matrix expected = new Matrix(); 131 final Transformation transformation = new Transformation(); 132 assertEquals(expected, transformation.getMatrix()); 133 } 134} 135