1 /* 2 * Copyright (C) 2017 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 17 package androidx.transition; 18 19 import static org.mockito.Matchers.any; 20 import static org.mockito.Mockito.mock; 21 import static org.mockito.Mockito.timeout; 22 import static org.mockito.Mockito.verify; 23 24 import android.animation.Animator; 25 import android.animation.ObjectAnimator; 26 import android.support.test.InstrumentationRegistry; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.widget.LinearLayout; 30 31 import androidx.transition.test.R; 32 33 import org.junit.Before; 34 35 import java.util.ArrayList; 36 37 public abstract class BaseTransitionTest extends BaseTest { 38 39 ArrayList<View> mTransitionTargets = new ArrayList<>(); 40 LinearLayout mRoot; 41 Transition mTransition; 42 Transition.TransitionListener mListener; 43 float mAnimatedValue; 44 45 @Before setUp()46 public void setUp() { 47 InstrumentationRegistry.getInstrumentation().setInTouchMode(false); 48 mRoot = (LinearLayout) rule.getActivity().findViewById(R.id.root); 49 mTransitionTargets.clear(); 50 mTransition = createTransition(); 51 mListener = mock(Transition.TransitionListener.class); 52 mTransition.addListener(mListener); 53 } 54 createTransition()55 Transition createTransition() { 56 return new TestTransition(); 57 } 58 waitForStart()59 void waitForStart() { 60 verify(mListener, timeout(3000)).onTransitionStart(any(Transition.class)); 61 } 62 waitForEnd()63 void waitForEnd() { 64 verify(mListener, timeout(3000)).onTransitionEnd(any(Transition.class)); 65 } 66 loadScene(final int layoutId)67 Scene loadScene(final int layoutId) throws Throwable { 68 final Scene[] scene = new Scene[1]; 69 rule.runOnUiThread(new Runnable() { 70 @Override 71 public void run() { 72 scene[0] = Scene.getSceneForLayout(mRoot, layoutId, rule.getActivity()); 73 } 74 }); 75 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); 76 return scene[0]; 77 } 78 startTransition(final int layoutId)79 void startTransition(final int layoutId) throws Throwable { 80 startTransition(loadScene(layoutId)); 81 } 82 startTransition(final Scene scene)83 private void startTransition(final Scene scene) throws Throwable { 84 rule.runOnUiThread(new Runnable() { 85 @Override 86 public void run() { 87 TransitionManager.go(scene, mTransition); 88 } 89 }); 90 waitForStart(); 91 } 92 enterScene(final int layoutId)93 void enterScene(final int layoutId) throws Throwable { 94 enterScene(loadScene(layoutId)); 95 } 96 enterScene(final Scene scene)97 void enterScene(final Scene scene) throws Throwable { 98 rule.runOnUiThread(new Runnable() { 99 @Override 100 public void run() { 101 scene.enter(); 102 } 103 }); 104 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); 105 } 106 resetListener()107 void resetListener() { 108 mTransition.removeListener(mListener); 109 mListener = mock(Transition.TransitionListener.class); 110 mTransition.addListener(mListener); 111 } 112 setAnimatedValue(float animatedValue)113 void setAnimatedValue(float animatedValue) { 114 mAnimatedValue = animatedValue; 115 } 116 117 public class TestTransition extends Visibility { 118 119 @Override onAppear(ViewGroup sceneRoot, View view, TransitionValues startValues, TransitionValues endValues)120 public Animator onAppear(ViewGroup sceneRoot, View view, TransitionValues startValues, 121 TransitionValues endValues) { 122 mTransitionTargets.add(endValues.view); 123 return ObjectAnimator.ofFloat(BaseTransitionTest.this, "animatedValue", 0, 1); 124 } 125 126 @Override onDisappear(ViewGroup sceneRoot, View view, TransitionValues startValues, TransitionValues endValues)127 public Animator onDisappear(ViewGroup sceneRoot, View view, TransitionValues startValues, 128 TransitionValues endValues) { 129 mTransitionTargets.add(startValues.view); 130 return ObjectAnimator.ofFloat(BaseTransitionTest.this, "animatedValue", 1, 0); 131 } 132 133 } 134 135 } 136