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 com.android.compatibility.common.util.CtsMockitoUtils.within;
19 
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.assertSame;
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
27 
28 import android.animation.TimeInterpolator;
29 import android.graphics.Rect;
30 import android.transition.ArcMotion;
31 import android.transition.ChangeBounds;
32 import android.transition.Fade;
33 import android.transition.PathMotion;
34 import android.transition.Transition;
35 import android.transition.TransitionPropagation;
36 import android.transition.TransitionSet;
37 import android.transition.TransitionValues;
38 import android.view.ViewGroup;
39 import android.view.animation.AccelerateDecelerateInterpolator;
40 import android.view.animation.AccelerateInterpolator;
41 import android.view.animation.DecelerateInterpolator;
42 
43 import androidx.test.filters.MediumTest;
44 import androidx.test.runner.AndroidJUnit4;
45 
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 
49 @MediumTest
50 @RunWith(AndroidJUnit4.class)
51 public class TransitionSetTest extends BaseTransitionTest {
52     @Test
testTransitionTogether()53     public void testTransitionTogether() throws Throwable {
54         TransitionSet transitionSet = new TransitionSet();
55         Fade fade = new Fade();
56         final Transition.TransitionListener fadeListener =
57                 mock(Transition.TransitionListener.class);
58         fade.addListener(fadeListener);
59         ChangeBounds changeBounds = new ChangeBounds();
60         final Transition.TransitionListener changeBoundsListener =
61                 mock(Transition.TransitionListener.class);
62         changeBounds.addListener(changeBoundsListener);
63         transitionSet.addTransition(fade);
64         transitionSet.addTransition(changeBounds);
65         mTransition = transitionSet;
66         resetListener();
67 
68         assertEquals(TransitionSet.ORDERING_TOGETHER, transitionSet.getOrdering());
69         enterScene(R.layout.scene1);
70         startTransition(R.layout.scene3);
71         mActivityRule.runOnUiThread(() -> {
72             verify(fadeListener, times(1)).onTransitionStart(any());
73             verify(changeBoundsListener, times(1)).onTransitionStart(any());
74         });
75     }
76 
77     @Test
testTransitionSequentially()78     public void testTransitionSequentially() throws Throwable {
79         TransitionSet transitionSet = new TransitionSet();
80         Fade fade = new Fade();
81         fade.setDuration(500);
82         final Transition.TransitionListener fadeListener =
83                 mock(Transition.TransitionListener.class);
84         fade.addListener(fadeListener);
85         ChangeBounds changeBounds = new ChangeBounds();
86         final Transition.TransitionListener changeBoundsListener =
87                 mock(Transition.TransitionListener.class);
88         changeBounds.addListener(changeBoundsListener);
89         transitionSet.addTransition(fade);
90         transitionSet.addTransition(changeBounds);
91         mTransition = transitionSet;
92         resetListener();
93 
94         assertEquals(TransitionSet.ORDERING_TOGETHER, transitionSet.getOrdering());
95         transitionSet.setOrdering(TransitionSet.ORDERING_SEQUENTIAL);
96         assertEquals(TransitionSet.ORDERING_SEQUENTIAL, transitionSet.getOrdering());
97 
98         enterScene(R.layout.scene1);
99         startTransition(R.layout.scene3);
100         verify(fadeListener, within(500)).onTransitionStart(any());
101         verify(fadeListener, times(1)).onTransitionStart(any());
102 
103         // change bounds shouldn't start until after fade finishes
104         verify(fadeListener, times(0)).onTransitionEnd(any());
105         verify(changeBoundsListener, times(0)).onTransitionStart(any());
106 
107         // now wait for the fade transition to end
108         verify(fadeListener, within(1000)).onTransitionEnd(any());
109 
110         // The change bounds should start soon after
111         verify(changeBoundsListener, within(500)).onTransitionStart(any());
112         verify(changeBoundsListener, times(1)).onTransitionStart(any());
113     }
114 
115     @Test
testTransitionCount()116     public void testTransitionCount() throws Throwable {
117         TransitionSet transitionSet = new TransitionSet();
118         assertEquals(0, transitionSet.getTransitionCount());
119 
120         Fade fade = new Fade();
121         ChangeBounds changeBounds = new ChangeBounds();
122         transitionSet.addTransition(fade);
123         transitionSet.addTransition(changeBounds);
124 
125         assertEquals(2, transitionSet.getTransitionCount());
126         assertSame(fade, transitionSet.getTransitionAt(0));
127         assertSame(changeBounds, transitionSet.getTransitionAt(1));
128 
129         transitionSet.removeTransition(fade);
130 
131         assertEquals(1, transitionSet.getTransitionCount());
132         assertSame(changeBounds, transitionSet.getTransitionAt(0));
133 
134         transitionSet.removeTransition(fade); // remove one that isn't there
135         assertEquals(1, transitionSet.getTransitionCount());
136         assertSame(changeBounds, transitionSet.getTransitionAt(0));
137     }
138 
139     @Test
testSetTransferValuesDuringAdd()140     public void testSetTransferValuesDuringAdd() throws Throwable {
141         Fade fade = new Fade();
142         fade.setDuration(500);
143         fade.setPropagation(new TestPropagation());
144         fade.setEpicenterCallback(new Transition.EpicenterCallback() {
145             @Override
146             public Rect onGetEpicenter(Transition transition) {
147                 return null;
148             }
149         });
150         fade.setInterpolator(new AccelerateDecelerateInterpolator());
151         fade.setPathMotion(new ArcMotion());
152 
153         TransitionSet transitionSet = new TransitionSet();
154         int duration = 100;
155         TestPropagation propagation = new TestPropagation();
156         TimeInterpolator interpolator = new DecelerateInterpolator();
157         PathMotion pathMotion = new ArcMotion();
158         Transition.EpicenterCallback epicenterCallback = new Transition.EpicenterCallback() {
159             @Override
160             public Rect onGetEpicenter(Transition transition) {
161                 return null;
162             }
163         };
164         transitionSet.setDuration(duration);
165         transitionSet.setPropagation(propagation);
166         transitionSet.setInterpolator(interpolator);
167         transitionSet.setPathMotion(pathMotion);
168         transitionSet.setEpicenterCallback(epicenterCallback);
169 
170         transitionSet.addTransition(fade);
171         assertEquals(duration, fade.getDuration());
172         assertSame(propagation, fade.getPropagation());
173         assertSame(interpolator, fade.getInterpolator());
174         assertSame(pathMotion, fade.getPathMotion());
175         assertSame(epicenterCallback, fade.getEpicenterCallback());
176     }
177 
178     @Test
testSetTransferNullValuesDuringAdd()179     public void testSetTransferNullValuesDuringAdd() throws Throwable {
180         Fade fade = new Fade();
181         fade.setDuration(500);
182         fade.setPropagation(new TestPropagation());
183         fade.setEpicenterCallback(new Transition.EpicenterCallback() {
184             @Override
185             public Rect onGetEpicenter(Transition transition) {
186                 return null;
187             }
188         });
189         fade.setInterpolator(new AccelerateDecelerateInterpolator());
190         fade.setPathMotion(new ArcMotion());
191 
192         TransitionSet transitionSet = new TransitionSet();
193         transitionSet.setDuration(0);
194         transitionSet.setPropagation(null);
195         transitionSet.setInterpolator(null);
196         transitionSet.setPathMotion(null);
197         transitionSet.setEpicenterCallback(null);
198 
199         transitionSet.addTransition(fade);
200         assertEquals(0, fade.getDuration());
201         assertNull(fade.getPropagation());
202         assertNull(fade.getInterpolator());
203         assertSame(transitionSet.getPathMotion(), fade.getPathMotion());
204         assertNull(fade.getEpicenterCallback());
205     }
206 
207     @Test
testSetNoTransferValuesDuringAdd()208     public void testSetNoTransferValuesDuringAdd() throws Throwable {
209         Fade fade = new Fade();
210         int duration = 100;
211         TestPropagation propagation = new TestPropagation();
212         TimeInterpolator interpolator = new DecelerateInterpolator();
213         PathMotion pathMotion = new ArcMotion();
214         Transition.EpicenterCallback epicenterCallback = new Transition.EpicenterCallback() {
215             @Override
216             public Rect onGetEpicenter(Transition transition) {
217                 return null;
218             }
219         };
220         fade.setDuration(duration);
221         fade.setPropagation(propagation);
222         fade.setInterpolator(interpolator);
223         fade.setPathMotion(pathMotion);
224         fade.setEpicenterCallback(epicenterCallback);
225 
226         TransitionSet transitionSet = new TransitionSet();
227 
228         transitionSet.addTransition(fade);
229         assertEquals(duration, fade.getDuration());
230         assertSame(propagation, fade.getPropagation());
231         assertSame(interpolator, fade.getInterpolator());
232         assertSame(pathMotion, fade.getPathMotion());
233         assertSame(epicenterCallback, fade.getEpicenterCallback());
234     }
235 
236     @Test
testSetAllowsChildrenToOverrideConfigs()237     public void testSetAllowsChildrenToOverrideConfigs() {
238         TransitionSet transitionSet = new TransitionSet();
239         transitionSet.setDuration(100);
240         transitionSet.setInterpolator(new DecelerateInterpolator());
241 
242         Fade fade = new Fade();
243         transitionSet.addTransition(fade); // here set's duration and interpolator are applied
244 
245         int overriddenDuration = 200;
246         TimeInterpolator overriddenInterpolator = new AccelerateInterpolator();
247         fade.setDuration(overriddenDuration);
248         fade.setInterpolator(overriddenInterpolator);
249 
250         // emulate beginDelayedTransition where we clone the provided transition
251         transitionSet = (TransitionSet) transitionSet.clone();
252         fade = (Fade) transitionSet.getTransitionAt(0);
253 
254         assertEquals(overriddenDuration, fade.getDuration());
255         assertEquals(overriddenInterpolator, fade.getInterpolator());
256     }
257 
258     private static class TestPropagation extends TransitionPropagation {
259         @Override
getStartDelay(ViewGroup sceneRoot, Transition transition, TransitionValues startValues, TransitionValues endValues)260         public long getStartDelay(ViewGroup sceneRoot, Transition transition,
261                 TransitionValues startValues, TransitionValues endValues) {
262             return 0;
263         }
264 
265         @Override
captureValues(TransitionValues transitionValues)266         public void captureValues(TransitionValues transitionValues) {
267         }
268 
269         @Override
getPropagationProperties()270         public String[] getPropagationProperties() {
271             return new String[] { };
272         }
273     }
274 }
275 
276