1 /*
2  * Copyright (C) 2015 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 android.animation.Animator;
19 import android.animation.ObjectAnimator;
20 import android.test.ActivityInstrumentationTestCase2;
21 import android.transition.Scene;
22 import android.transition.Transition;
23 import android.transition.TransitionManager;
24 import android.transition.TransitionValues;
25 import android.transition.Visibility;
26 import android.view.Choreographer;
27 import android.view.Choreographer.FrameCallback;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.FrameLayout;
31 
32 import java.util.ArrayList;
33 import java.util.concurrent.CountDownLatch;
34 import java.util.concurrent.TimeUnit;
35 
36 public class BaseTransitionTest extends ActivityInstrumentationTestCase2<TransitionActivity> {
37     protected TransitionActivity mActivity;
38     protected FrameLayout mSceneRoot;
39     public float mAnimatedValue;
40     protected ArrayList<View> mTargets = new ArrayList<View>();
41     protected Transition mTransition;
42     protected SimpleTransitionListener mListener;
43 
BaseTransitionTest()44     public BaseTransitionTest() {
45         super(TransitionActivity.class);
46     }
47 
48     @Override
setUp()49     protected void setUp() throws Exception {
50         super.setUp();
51         setActivityInitialTouchMode(false);
52         mActivity = getActivity();
53         mSceneRoot = (FrameLayout) mActivity.findViewById(R.id.container);
54         mTargets.clear();
55         mTransition = new TestTransition();
56         mListener = new SimpleTransitionListener();
57         mTransition.addListener(mListener);
58     }
59 
waitForStart()60     protected void waitForStart() throws InterruptedException {
61         waitForStart(mListener);
62     }
63 
waitForStart(SimpleTransitionListener listener)64     protected void waitForStart(SimpleTransitionListener listener) throws InterruptedException {
65         assertTrue(listener.startLatch.await(4000, TimeUnit.MILLISECONDS));
66     }
67 
waitForEnd(long waitMillis)68     protected void waitForEnd(long waitMillis) throws InterruptedException {
69         waitForEnd(mListener, waitMillis);
70         getInstrumentation().waitForIdleSync();
71     }
72 
waitForEnd(SimpleTransitionListener listener, long waitMillis)73     protected static void waitForEnd(SimpleTransitionListener listener, long waitMillis)
74             throws InterruptedException {
75         listener.endLatch.await(waitMillis, TimeUnit.MILLISECONDS);
76     }
77 
loadLayout(final int layout)78     protected View loadLayout(final int layout) throws Throwable {
79         View[] root = new View[1];
80 
81         runTestOnUiThread(new Runnable() {
82             @Override
83             public void run() {
84                 root[0] = mActivity.getLayoutInflater().inflate(layout, mSceneRoot, false);
85             }
86         });
87 
88         return root[0];
89     }
90 
loadScene(final View layout)91     protected Scene loadScene(final View layout) throws Throwable {
92         Scene[] scene = new Scene[1];
93         runTestOnUiThread(new Runnable() {
94             @Override
95             public void run() {
96                 scene[0] = new Scene(mSceneRoot, layout);
97             }
98         });
99 
100         return scene[0];
101     }
102 
loadScene(final int layoutId)103     protected Scene loadScene(final int layoutId) throws Throwable {
104         Scene scene[] = new Scene[1];
105         runTestOnUiThread(new Runnable() {
106             @Override
107             public void run() {
108                 scene[0] = Scene.getSceneForLayout(mSceneRoot, layoutId, mActivity);
109             }
110         });
111         return scene[0];
112     }
113 
startTransition(final int layoutId)114     protected void startTransition(final int layoutId) throws Throwable {
115         startTransition(loadScene(layoutId));
116     }
117 
startTransition(final Scene scene)118     protected void startTransition(final Scene scene) throws Throwable {
119         runTestOnUiThread(new Runnable() {
120             @Override
121             public void run() {
122                 TransitionManager.go(scene, mTransition);
123             }
124         });
125         waitForStart();
126     }
127 
endTransition()128     protected void endTransition() throws Throwable {
129         runTestOnUiThread(new Runnable() {
130             @Override
131             public void run() {
132                 TransitionManager.endTransitions(mSceneRoot);
133             }
134         });
135     }
136 
enterScene(final int layoutId)137     protected void enterScene(final int layoutId) throws Throwable {
138         enterScene(loadScene(layoutId));
139     }
140 
enterScene(final Scene scene)141     protected void enterScene(final Scene scene) throws Throwable {
142         runTestOnUiThread(new Runnable() {
143             @Override
144             public void run() {
145                 scene.enter();
146             }
147         });
148         getInstrumentation().waitForIdleSync();
149     }
150 
exitScene(final Scene scene)151     protected void exitScene(final Scene scene) throws Throwable {
152         runTestOnUiThread(new Runnable() {
153             @Override
154             public void run() {
155                 scene.exit();
156             }
157         });
158         getInstrumentation().waitForIdleSync();
159     }
160 
resetListener()161     protected void resetListener() {
162         mTransition.removeListener(mListener);
163         mListener = new SimpleTransitionListener();
164         mTransition.addListener(mListener);
165     }
166 
167     public class TestTransition extends Visibility {
168 
TestTransition()169         public TestTransition() {
170         }
171 
172         @Override
onAppear(ViewGroup sceneRoot, View view, TransitionValues startValues, TransitionValues endValues)173         public Animator onAppear(ViewGroup sceneRoot, View view, TransitionValues startValues,
174                 TransitionValues endValues) {
175             mTargets.add(endValues.view);
176             return ObjectAnimator.ofFloat(BaseTransitionTest.this, "mAnimatedValue", 0, 1);
177         }
178 
179         @Override
onDisappear(ViewGroup sceneRoot, View view, TransitionValues startValues, TransitionValues endValues)180         public Animator onDisappear(ViewGroup sceneRoot, View view, TransitionValues startValues,
181                 TransitionValues endValues) {
182             mTargets.add(startValues.view);
183             return ObjectAnimator.ofFloat(BaseTransitionTest.this, "mAnimatedValue", 1, 0);
184         }
185     }
186 }
187