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.assertNotEquals; 20 import static org.junit.Assert.assertTrue; 21 import static org.mockito.Matchers.any; 22 import static org.mockito.Mockito.never; 23 import static org.mockito.Mockito.verify; 24 25 import android.graphics.PointF; 26 import android.transition.Explode; 27 import android.transition.TransitionManager; 28 import android.view.View; 29 30 import androidx.test.filters.MediumTest; 31 import androidx.test.runner.AndroidJUnit4; 32 33 import com.android.compatibility.common.util.PollingCheck; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 39 import java.util.List; 40 41 @MediumTest 42 @RunWith(AndroidJUnit4.class) 43 public class ExplodeTest extends BaseTransitionTest { 44 Explode mExplode; 45 46 @Override 47 @Before 48 public void setup() { 49 super.setup(); 50 resetTransition(); 51 } 52 53 private void resetTransition() { 54 mExplode = new Explode(); 55 mExplode.setDuration(1000); 56 mTransition = mExplode; 57 resetListener(); 58 } 59 60 @Test 61 public void testExplode() throws Throwable { 62 enterScene(R.layout.scene10); 63 final View redSquare = mActivity.findViewById(R.id.redSquare); 64 final View greenSquare = mActivity.findViewById(R.id.greenSquare); 65 final View blueSquare = mActivity.findViewById(R.id.blueSquare); 66 final View yellowSquare = mActivity.findViewById(R.id.yellowSquare); 67 68 final List<PointF> redPoints = captureTranslations(redSquare); 69 final List<PointF> greenPoints = captureTranslations(greenSquare); 70 final List<PointF> bluePoints = captureTranslations(blueSquare); 71 final List<PointF> yellowPoints = captureTranslations(yellowSquare); 72 73 mActivityRule.runOnUiThread(() -> { 74 TransitionManager.beginDelayedTransition(mSceneRoot, mTransition); 75 redSquare.setVisibility(View.INVISIBLE); 76 greenSquare.setVisibility(View.INVISIBLE); 77 blueSquare.setVisibility(View.INVISIBLE); 78 yellowSquare.setVisibility(View.INVISIBLE); 79 }); 80 waitForStart(); 81 verify(mListener, never()).onTransitionEnd(any()); 82 assertEquals(View.VISIBLE, redSquare.getVisibility()); 83 assertEquals(View.VISIBLE, greenSquare.getVisibility()); 84 assertEquals(View.VISIBLE, blueSquare.getVisibility()); 85 assertEquals(View.VISIBLE, yellowSquare.getVisibility()); 86 87 waitForEnd(5000); 88 verifyMovement(redPoints, false, false, true); 89 verifyMovement(greenPoints, true, false, true); 90 verifyMovement(bluePoints, true, true, true); 91 verifyMovement(yellowPoints, false, true, true); 92 93 verifyNoTranslation(redSquare); 94 verifyNoTranslation(greenSquare); 95 verifyNoTranslation(blueSquare); 96 verifyNoTranslation(yellowSquare); 97 assertEquals(View.INVISIBLE, redSquare.getVisibility()); 98 assertEquals(View.INVISIBLE, greenSquare.getVisibility()); 99 assertEquals(View.INVISIBLE, blueSquare.getVisibility()); 100 assertEquals(View.INVISIBLE, yellowSquare.getVisibility()); 101 } 102 103 @Test 104 public void testImplode() throws Throwable { 105 enterScene(R.layout.scene10); 106 final View redSquare = mActivity.findViewById(R.id.redSquare); 107 final View greenSquare = mActivity.findViewById(R.id.greenSquare); 108 final View blueSquare = mActivity.findViewById(R.id.blueSquare); 109 final View yellowSquare = mActivity.findViewById(R.id.yellowSquare); 110 111 final List<PointF> redPoints = captureTranslations(redSquare); 112 final List<PointF> greenPoints = captureTranslations(greenSquare); 113 final List<PointF> bluePoints = captureTranslations(blueSquare); 114 final List<PointF> yellowPoints = captureTranslations(yellowSquare); 115 116 mActivityRule.runOnUiThread(() -> { 117 redSquare.setVisibility(View.INVISIBLE); 118 greenSquare.setVisibility(View.INVISIBLE); 119 blueSquare.setVisibility(View.INVISIBLE); 120 yellowSquare.setVisibility(View.INVISIBLE); 121 }); 122 mInstrumentation.waitForIdleSync(); 123 124 mActivityRule.runOnUiThread(() -> { 125 TransitionManager.beginDelayedTransition(mSceneRoot, mTransition); 126 redSquare.setVisibility(View.VISIBLE); 127 greenSquare.setVisibility(View.VISIBLE); 128 blueSquare.setVisibility(View.VISIBLE); 129 yellowSquare.setVisibility(View.VISIBLE); 130 }); 131 waitForStart(); 132 133 assertEquals(View.VISIBLE, redSquare.getVisibility()); 134 assertEquals(View.VISIBLE, greenSquare.getVisibility()); 135 assertEquals(View.VISIBLE, blueSquare.getVisibility()); 136 assertEquals(View.VISIBLE, yellowSquare.getVisibility()); 137 138 waitForEnd(5000); 139 verifyMovement(redPoints, true, true, false); 140 verifyMovement(greenPoints, false, true, false); 141 verifyMovement(bluePoints, false, false, false); 142 verifyMovement(yellowPoints, true, false, false); 143 144 verifyNoTranslation(redSquare); 145 verifyNoTranslation(greenSquare); 146 verifyNoTranslation(blueSquare); 147 verifyNoTranslation(yellowSquare); 148 assertEquals(View.VISIBLE, redSquare.getVisibility()); 149 assertEquals(View.VISIBLE, greenSquare.getVisibility()); 150 assertEquals(View.VISIBLE, blueSquare.getVisibility()); 151 assertEquals(View.VISIBLE, yellowSquare.getVisibility()); 152 } 153 154 private void verifyMovement(List<PointF> points, boolean moveRight, boolean moveDown, 155 boolean explode) { 156 int numPoints = points.size(); 157 assertTrue(numPoints > 3); 158 159 // skip the first point -- it is the value before the change 160 PointF firstPoint = points.get(1); 161 162 // Skip the last point -- it may be the settled value after the change 163 PointF lastPoint = points.get(numPoints - 2); 164 165 assertNotEquals(lastPoint.x, firstPoint.x); 166 assertNotEquals(lastPoint.y, firstPoint.y); 167 assertEquals(moveRight, firstPoint.x < lastPoint.x); 168 assertEquals(moveDown, firstPoint.y < lastPoint.y); 169 170 assertEquals(explode, Math.abs(firstPoint.x) < Math.abs(lastPoint.x)); 171 assertEquals(explode, Math.abs(firstPoint.y) < Math.abs(lastPoint.y)); 172 } 173 174 private void waitForMovement(View view, float startX, float startY) { 175 PollingCheck.waitFor(5000, () -> hasMoved(view, startX, startY)); 176 } 177 178 private boolean hasMoved(View view, float x, float y) { 179 return Math.abs(view.getTranslationX() - x) > 2f 180 || Math.abs(view.getTranslationY() - y) > 2f; 181 } 182 183 private void verifyNoTranslation(View view) { 184 assertEquals(0f, view.getTranslationX(), 0.0f); 185 assertEquals(0f, view.getTranslationY(), 0.0f); 186 } 187 } 188 189