1 /* 2 * Copyright (C) 2016 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.transition.cts; 17 18 import static org.junit.Assert.assertEquals; 19 import static org.junit.Assert.assertTrue; 20 import static org.mockito.Matchers.any; 21 import static org.mockito.Mockito.never; 22 import static org.mockito.Mockito.verify; 23 24 import android.support.test.filters.MediumTest; 25 import android.support.test.runner.AndroidJUnit4; 26 import android.transition.ChangeTransform; 27 import android.transition.TransitionManager; 28 import android.view.View; 29 import android.view.ViewGroup; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 35 @MediumTest 36 @RunWith(AndroidJUnit4.class) 37 public class ChangeTransformTest extends BaseTransitionTest { 38 ChangeTransform mChangeTransform; 39 40 @Override 41 @Before 42 public void setup() { 43 super.setup(); 44 resetChangeBoundsTransition(); 45 } 46 47 private void resetChangeBoundsTransition() { 48 mChangeTransform = new ChangeTransform(); 49 mTransition = mChangeTransform; 50 resetListener(); 51 } 52 53 @Test 54 public void testTranslation() throws Throwable { 55 enterScene(R.layout.scene1); 56 57 final View redSquare = mActivity.findViewById(R.id.redSquare); 58 59 mActivityRule.runOnUiThread(() -> { 60 TransitionManager.beginDelayedTransition(mSceneRoot, mChangeTransform); 61 redSquare.setTranslationX(500); 62 redSquare.setTranslationY(600); 63 }); 64 waitForStart(); 65 66 verify(mListener, never()).onTransitionEnd(any()); // still running 67 // There is no way to validate the intermediate matrix because it uses 68 // hidden properties of the View to execute. 69 waitForEnd(800); 70 assertEquals(500f, redSquare.getTranslationX(), 0.0f); 71 assertEquals(600f, redSquare.getTranslationY(), 0.0f); 72 } 73 74 @Test 75 public void testRotation() throws Throwable { 76 enterScene(R.layout.scene1); 77 78 final View redSquare = mActivity.findViewById(R.id.redSquare); 79 80 mActivityRule.runOnUiThread(() -> { 81 TransitionManager.beginDelayedTransition(mSceneRoot, mChangeTransform); 82 redSquare.setRotation(45); 83 }); 84 waitForStart(); 85 86 verify(mListener, never()).onTransitionEnd(any()); // still running 87 // There is no way to validate the intermediate matrix because it uses 88 // hidden properties of the View to execute. 89 waitForEnd(800); 90 assertEquals(45f, redSquare.getRotation(), 0.0f); 91 } 92 93 @Test 94 public void testScale() throws Throwable { 95 enterScene(R.layout.scene1); 96 97 final View redSquare = mActivity.findViewById(R.id.redSquare); 98 99 mActivityRule.runOnUiThread(() -> { 100 TransitionManager.beginDelayedTransition(mSceneRoot, mChangeTransform); 101 redSquare.setScaleX(2f); 102 redSquare.setScaleY(3f); 103 }); 104 waitForStart(); 105 106 verify(mListener, never()).onTransitionEnd(any()); // still running 107 // There is no way to validate the intermediate matrix because it uses 108 // hidden properties of the View to execute. 109 waitForEnd(800); 110 assertEquals(2f, redSquare.getScaleX(), 0.0f); 111 assertEquals(3f, redSquare.getScaleY(), 0.0f); 112 } 113 114 @Test 115 public void testReparent() throws Throwable { 116 assertEquals(true, mChangeTransform.getReparent()); 117 enterScene(R.layout.scene5); 118 startTransition(R.layout.scene9); 119 verify(mListener, never()).onTransitionEnd(any()); // still running 120 waitForEnd(800); 121 122 resetListener(); 123 mChangeTransform.setReparent(false); 124 assertEquals(false, mChangeTransform.getReparent()); 125 startTransition(R.layout.scene5); 126 waitForEnd(0); // no transition to run because reparent == false 127 } 128 129 @Test 130 public void testReparentWithOverlay() throws Throwable { 131 assertEquals(true, mChangeTransform.getReparentWithOverlay()); 132 enterScene(R.layout.scene5); 133 startTransition(R.layout.scene9); 134 verify(mListener, never()).onTransitionEnd(any()); // still running 135 mActivityRule.runOnUiThread(() -> { 136 View view = new View(mActivity); 137 view.setRight(100); 138 view.setBottom(100); 139 mSceneRoot.getOverlay().add(view); 140 ViewGroup container = (ViewGroup) view.getParent(); 141 assertEquals(2, container.getChildCount()); 142 mSceneRoot.getOverlay().remove(view); 143 assertTrue(mActivity.findViewById(R.id.text).getVisibility() != View.VISIBLE); 144 }); 145 waitForEnd(800); 146 147 mChangeTransform.setReparentWithOverlay(false); 148 assertEquals(false, mChangeTransform.getReparentWithOverlay()); 149 resetListener(); 150 startTransition(R.layout.scene5); 151 verify(mListener, never()).onTransitionEnd(any()); // still running 152 mActivityRule.runOnUiThread(() -> { 153 View view = new View(mActivity); 154 view.setRight(100); 155 view.setBottom(100); 156 mSceneRoot.getOverlay().add(view); 157 ViewGroup container = (ViewGroup) view.getParent(); 158 assertEquals(1, container.getChildCount()); 159 mSceneRoot.getOverlay().remove(view); 160 assertEquals(View.VISIBLE, mActivity.findViewById(R.id.text).getVisibility()); 161 }); 162 waitForEnd(800); 163 } 164 } 165 166