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 com.android.cts.transition.R;
19 
20 import android.transition.Scene;
21 import android.transition.TransitionManager;
22 import android.view.View;
23 
24 public class TransitionManagerTest extends BaseTransitionTest {
25 
TransitionManagerTest()26     public TransitionManagerTest() {
27     }
28 
testBeginDelayedTransition()29     public void testBeginDelayedTransition() throws Throwable {
30         runTestOnUiThread(new Runnable() {
31             @Override
32             public void run() {
33                 TransitionManager.beginDelayedTransition(mSceneRoot, mTransition);
34                 View view = mActivity.getLayoutInflater().inflate(R.layout.scene1, mSceneRoot,
35                         false);
36                 mSceneRoot.addView(view);
37             }
38         });
39 
40         waitForStart();
41         waitForEnd(150);
42         assertFalse(mTransition.listener.resumed);
43         assertFalse(mTransition.listener.paused);
44         assertFalse(mTransition.listener.canceled);
45         assertNotNull(mTransition.listener.transition);
46         assertEquals(TestTransition.class, mTransition.listener.transition.getClass());
47         assertTrue(mTransition != mTransition.listener.transition);
48         runTestOnUiThread(new Runnable() {
49             @Override
50             public void run() {
51                 assertNotNull(mActivity.findViewById(R.id.redSquare));
52                 assertNotNull(mActivity.findViewById(R.id.greenSquare));
53             }
54         });
55     }
56 
testGo()57     public void testGo() throws Throwable {
58         startTransition(R.layout.scene1);
59         waitForStart();
60         waitForEnd(150);
61 
62         assertFalse(mTransition.listener.resumed);
63         assertFalse(mTransition.listener.paused);
64         assertFalse(mTransition.listener.canceled);
65         assertNotNull(mTransition.listener.transition);
66         assertEquals(TestTransition.class, mTransition.listener.transition.getClass());
67         assertTrue(mTransition != mTransition.listener.transition);
68         runTestOnUiThread(new Runnable() {
69             @Override
70             public void run() {
71                 assertNotNull(mActivity.findViewById(R.id.redSquare));
72                 assertNotNull(mActivity.findViewById(R.id.greenSquare));
73             }
74         });
75     }
76 
testSetTransition1()77     public void testSetTransition1() throws Throwable {
78         final TransitionManager transitionManager = new TransitionManager();
79 
80         runTestOnUiThread(new Runnable() {
81             @Override
82             public void run() {
83                 Scene scene = Scene.getSceneForLayout(mSceneRoot, R.layout.scene1, mActivity);
84                 transitionManager.setTransition(scene, mTransition);
85                 transitionManager.transitionTo(scene);
86             }
87         });
88 
89         waitForStart();
90         waitForEnd(150);
91         assertFalse(mTransition.listener.resumed);
92         assertFalse(mTransition.listener.paused);
93         assertFalse(mTransition.listener.canceled);
94         assertNotNull(mTransition.listener.transition);
95         assertEquals(TestTransition.class, mTransition.listener.transition.getClass());
96         assertTrue(mTransition != mTransition.listener.transition);
97         runTestOnUiThread(new Runnable() {
98             @Override
99             public void run() {
100                 assertNotNull(mActivity.findViewById(R.id.redSquare));
101                 assertNotNull(mActivity.findViewById(R.id.greenSquare));
102                 mTransition.listener.started = false;
103                 mTransition.listener.ended = false;
104                 Scene scene = Scene.getSceneForLayout(mSceneRoot, R.layout.scene2, mActivity);
105                 transitionManager.transitionTo(scene);
106             }
107         });
108         Thread.sleep(50);
109         assertFalse(mTransition.listener.started);
110         endTransition();
111     }
112 
testSetTransition2()113     public void testSetTransition2() throws Throwable {
114         final TransitionManager transitionManager = new TransitionManager();
115         final Scene[] scenes = new Scene[3];
116 
117         runTestOnUiThread(new Runnable() {
118             @Override
119             public void run() {
120                 scenes[0] = Scene.getSceneForLayout(mSceneRoot, R.layout.scene1, mActivity);
121                 scenes[1] = Scene.getSceneForLayout(mSceneRoot, R.layout.scene2, mActivity);
122                 scenes[2] = Scene.getSceneForLayout(mSceneRoot, R.layout.scene3, mActivity);
123                 transitionManager.setTransition(scenes[0], scenes[1], mTransition);
124                 transitionManager.transitionTo(scenes[0]);
125             }
126         });
127         Thread.sleep(50);
128         assertFalse(mTransition.listener.started);
129 
130         runTestOnUiThread(new Runnable() {
131             @Override
132             public void run() {
133                 transitionManager.transitionTo(scenes[1]);
134             }
135         });
136 
137         waitForStart();
138         waitForEnd(150);
139         assertFalse(mTransition.listener.resumed);
140         assertFalse(mTransition.listener.paused);
141         assertFalse(mTransition.listener.canceled);
142         assertNotNull(mTransition.listener.transition);
143         assertEquals(TestTransition.class, mTransition.listener.transition.getClass());
144         assertTrue(mTransition != mTransition.listener.transition);
145         runTestOnUiThread(new Runnable() {
146             @Override
147             public void run() {
148                 mTransition.listener.started = false;
149                 mTransition.listener.ended = false;
150                 transitionManager.transitionTo(scenes[2]);
151             }
152         });
153         Thread.sleep(50);
154         assertFalse(mTransition.listener.started);
155         endTransition();
156     }
157 
testEndTransitions()158     public void testEndTransitions() throws Throwable {
159         mTransition.setDuration(400);
160 
161         startTransition(R.layout.scene1);
162         waitForStart();
163         endTransition();
164         waitForEnd(50);
165     }
166 
testEndTransitionsBeforeStarted()167     public void testEndTransitionsBeforeStarted() throws Throwable {
168         mTransition.setDuration(400);
169 
170         runTestOnUiThread(new Runnable() {
171             @Override
172             public void run() {
173                 Scene scene = Scene.getSceneForLayout(mSceneRoot, R.layout.scene1, mActivity);
174                 TransitionManager.go(scene, mTransition);
175                 TransitionManager.endTransitions(mSceneRoot);
176             }
177         });
178         Thread.sleep(50);
179         assertFalse(mTransition.listener.started);
180         assertFalse(mTransition.listener.ended);
181     }
182 }
183 
184