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.assertFalse;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23 
24 import android.content.Context;
25 import android.graphics.Path;
26 import android.graphics.PathMeasure;
27 import android.transition.ArcMotion;
28 import android.transition.AutoTransition;
29 import android.transition.ChangeBounds;
30 import android.transition.ChangeClipBounds;
31 import android.transition.ChangeImageTransform;
32 import android.transition.ChangeScroll;
33 import android.transition.ChangeTransform;
34 import android.transition.Explode;
35 import android.transition.Fade;
36 import android.transition.PathMotion;
37 import android.transition.PatternPathMotion;
38 import android.transition.Scene;
39 import android.transition.Slide;
40 import android.transition.Transition;
41 import android.transition.TransitionInflater;
42 import android.transition.TransitionManager;
43 import android.transition.TransitionSet;
44 import android.transition.TransitionValues;
45 import android.transition.Visibility;
46 import android.util.AttributeSet;
47 import android.view.Gravity;
48 import android.widget.ImageView;
49 import android.widget.TextView;
50 
51 import androidx.test.filters.MediumTest;
52 import androidx.test.runner.AndroidJUnit4;
53 
54 import org.junit.Test;
55 import org.junit.runner.RunWith;
56 
57 import java.util.List;
58 
59 @MediumTest
60 @RunWith(AndroidJUnit4.class)
61 public class TransitionInflaterTest extends BaseTransitionTest {
62     @Test
testInflationConstructors()63     public void testInflationConstructors() throws Throwable {
64         TransitionInflater inflater = TransitionInflater.from(mActivity);
65         Transition transition = inflater.inflateTransition(R.transition.transition_constructors);
66         assertTrue(transition instanceof TransitionSet);
67         TransitionSet set = (TransitionSet) transition;
68         assertEquals(10, set.getTransitionCount());
69     }
70 
71     @Test
testInflation()72     public void testInflation() {
73         TransitionInflater inflater = TransitionInflater.from(mActivity);
74         verifyFadeProperties(inflater.inflateTransition(R.transition.fade));
75         verifyChangeBoundsProperties(inflater.inflateTransition(R.transition.change_bounds));
76         verifySlideProperties(inflater.inflateTransition(R.transition.slide));
77         verifyExplodeProperties(inflater.inflateTransition(R.transition.explode));
78         verifyChangeImageTransformProperties(
79                 inflater.inflateTransition(R.transition.change_image_transform));
80         verifyChangeTransformProperties(inflater.inflateTransition(R.transition.change_transform));
81         verifyChangeClipBoundsProperties(
82                 inflater.inflateTransition(R.transition.change_clip_bounds));
83         verifyAutoTransitionProperties(inflater.inflateTransition(R.transition.auto_transition));
84         verifyChangeScrollProperties(inflater.inflateTransition(R.transition.change_scroll));
85         verifyTransitionSetProperties(inflater.inflateTransition(R.transition.transition_set));
86         verifyCustomTransitionProperties(
87                 inflater.inflateTransition(R.transition.custom_transition));
88         verifyTargetIds(inflater.inflateTransition(R.transition.target_ids));
89         verifyTargetNames(inflater.inflateTransition(R.transition.target_names));
90         verifyTargetClass(inflater.inflateTransition(R.transition.target_classes));
91         verifyArcMotion(inflater.inflateTransition(R.transition.arc_motion));
92         verifyCustomPathMotion(inflater.inflateTransition(R.transition.custom_path_motion));
93         verifyPatternPathMotion(inflater.inflateTransition(R.transition.pattern_path_motion));
94     }
95 
96     @Test
testInflateTransitionManager()97     public void testInflateTransitionManager() throws Throwable {
98         TransitionInflater inflater = TransitionInflater.from(mActivity);
99         TransitionManager transitionManager =
100                 inflater.inflateTransitionManager(R.transition.transition_manager, mSceneRoot);
101         assertNotNull(transitionManager);
102 
103         Scene scene1 = Scene.getSceneForLayout(mSceneRoot, R.layout.scene1, mActivity);
104         Transition transition = transitionManager.getTransition(scene1);
105         assertNotNull(transition);
106         assertTrue(transition instanceof Fade);
107         enterScene(scene1);
108 
109         Scene scene2 = Scene.getSceneForLayout(mSceneRoot, R.layout.scene2, mActivity);
110         transition = transitionManager.getTransition(scene2);
111         assertNotNull(transition);
112         assertTrue(transition instanceof ChangeBounds);
113     }
114 
verifyFadeProperties(Transition transition)115     private void verifyFadeProperties(Transition transition) {
116         assertTrue(transition instanceof Fade);
117         Fade fade = (Fade) transition;
118         assertEquals(Fade.OUT, fade.getMode());
119     }
120 
verifyChangeBoundsProperties(Transition transition)121     private void verifyChangeBoundsProperties(Transition transition) {
122         assertTrue(transition instanceof ChangeBounds);
123         ChangeBounds changeBounds = (ChangeBounds) transition;
124         assertTrue(changeBounds.getResizeClip());
125     }
126 
verifySlideProperties(Transition transition)127     private void verifySlideProperties(Transition transition) {
128         assertTrue(transition instanceof Slide);
129         Slide slide = (Slide) transition;
130         assertEquals(Gravity.TOP, slide.getSlideEdge());
131     }
132 
verifyExplodeProperties(Transition transition)133     private void verifyExplodeProperties(Transition transition) {
134         assertTrue(transition instanceof Explode);
135         Visibility visibility = (Visibility) transition;
136         assertEquals(Visibility.MODE_IN, visibility.getMode());
137     }
138 
verifyChangeImageTransformProperties(Transition transition)139     private void verifyChangeImageTransformProperties(Transition transition) {
140         assertTrue(transition instanceof ChangeImageTransform);
141     }
142 
verifyChangeTransformProperties(Transition transition)143     private void verifyChangeTransformProperties(Transition transition) {
144         assertTrue(transition instanceof ChangeTransform);
145         ChangeTransform changeTransform = (ChangeTransform) transition;
146         assertFalse(changeTransform.getReparent());
147         assertFalse(changeTransform.getReparentWithOverlay());
148     }
149 
verifyChangeClipBoundsProperties(Transition transition)150     private void verifyChangeClipBoundsProperties(Transition transition) {
151         assertTrue(transition instanceof ChangeClipBounds);
152     }
153 
verifyAutoTransitionProperties(Transition transition)154     private void verifyAutoTransitionProperties(Transition transition) {
155         assertTrue(transition instanceof AutoTransition);
156     }
157 
verifyChangeScrollProperties(Transition transition)158     private void verifyChangeScrollProperties(Transition transition) {
159         assertTrue(transition instanceof ChangeScroll);
160     }
161 
verifyTransitionSetProperties(Transition transition)162     private void verifyTransitionSetProperties(Transition transition) {
163         assertTrue(transition instanceof TransitionSet);
164         TransitionSet set = (TransitionSet) transition;
165         assertEquals(TransitionSet.ORDERING_SEQUENTIAL, set.getOrdering());
166         assertEquals(300, set.getDuration());
167         assertNotNull(set.getPathMotion());
168         assertEquals(2, set.getTransitionCount());
169         assertTrue(set.getTransitionAt(0) instanceof ChangeBounds);
170         assertTrue(set.getTransitionAt(1) instanceof Fade);
171     }
172 
verifyCustomTransitionProperties(Transition transition)173     private void verifyCustomTransitionProperties(Transition transition) {
174         assertTrue(transition instanceof CustomTransition);
175     }
176 
verifyTargetIds(Transition transition)177     private void verifyTargetIds(Transition transition) {
178         List<Integer> targets = transition.getTargetIds();
179         assertNotNull(targets);
180         assertEquals(2, targets.size());
181         assertEquals(R.id.hello, (int) targets.get(0));
182         assertEquals(R.id.world, (int) targets.get(1));
183     }
184 
verifyTargetNames(Transition transition)185     private void verifyTargetNames(Transition transition) {
186         List<String> targets = transition.getTargetNames();
187         assertNotNull(targets);
188         assertEquals(2, targets.size());
189         assertEquals("hello", targets.get(0));
190         assertEquals("world", targets.get(1));
191     }
192 
verifyTargetClass(Transition transition)193     private void verifyTargetClass(Transition transition) {
194         List<Class> targets = transition.getTargetTypes();
195         assertNotNull(targets);
196         assertEquals(2, targets.size());
197         assertEquals(TextView.class, targets.get(0));
198         assertEquals(ImageView.class, targets.get(1));
199     }
200 
verifyArcMotion(Transition transition)201     private void verifyArcMotion(Transition transition) {
202         assertNotNull(transition);
203         PathMotion motion = transition.getPathMotion();
204         assertNotNull(motion);
205         assertTrue(motion instanceof ArcMotion);
206         ArcMotion arcMotion = (ArcMotion) motion;
207         assertEquals(1f, arcMotion.getMinimumVerticalAngle(), 0.01f);
208         assertEquals(2f, arcMotion.getMinimumHorizontalAngle(), 0.01f);
209         assertEquals(53f, arcMotion.getMaximumAngle(), 0.01f);
210     }
211 
verifyCustomPathMotion(Transition transition)212     private void verifyCustomPathMotion(Transition transition) {
213         assertNotNull(transition);
214         PathMotion motion = transition.getPathMotion();
215         assertNotNull(motion);
216         assertTrue(motion instanceof CustomPathMotion);
217     }
218 
verifyPatternPathMotion(Transition transition)219     private void verifyPatternPathMotion(Transition transition) {
220         assertNotNull(transition);
221         PathMotion motion = transition.getPathMotion();
222         assertNotNull(motion);
223         assertTrue(motion instanceof PatternPathMotion);
224         PatternPathMotion pattern = (PatternPathMotion) motion;
225         Path path = pattern.getPatternPath();
226         PathMeasure measure = new PathMeasure(path, false);
227         assertEquals(200f, measure.getLength(), 0.1f);
228     }
229 
230     public static class CustomTransition extends Transition {
CustomTransition()231         public CustomTransition() {
232             fail("Default constructor was not expected");
233         }
234 
CustomTransition(Context context, AttributeSet attrs)235         public CustomTransition(Context context, AttributeSet attrs) {
236             super(context, attrs);
237         }
238 
239         @Override
captureStartValues(TransitionValues transitionValues)240         public void captureStartValues(TransitionValues transitionValues) {
241         }
242 
243         @Override
captureEndValues(TransitionValues transitionValues)244         public void captureEndValues(TransitionValues transitionValues) {
245         }
246     }
247 
248     public static class CustomPathMotion extends PathMotion {
CustomPathMotion()249         public CustomPathMotion() {
250             fail("default constructor shouldn't be called.");
251         }
252 
CustomPathMotion(Context context, AttributeSet attrs)253         public CustomPathMotion(Context context, AttributeSet attrs) {
254             super(context, attrs);
255         }
256 
257         @Override
getPath(float startX, float startY, float endX, float endY)258         public Path getPath(float startX, float startY, float endX, float endY) {
259             return null;
260         }
261     }
262 
263     public static class InflationFade extends Fade {
InflationFade(Context context, AttributeSet attrs)264         public InflationFade(Context context, AttributeSet attrs) {
265             super(context, attrs);
266         }
267     }
268 
269     public static class InflationChangeBounds extends ChangeBounds {
InflationChangeBounds(Context context, AttributeSet attrs)270         public InflationChangeBounds(Context context, AttributeSet attrs) {
271             super(context, attrs);
272         }
273     }
274 
275     public static class InflationSlide extends Slide {
InflationSlide(Context context, AttributeSet attrs)276         public InflationSlide(Context context, AttributeSet attrs) {
277             super(context, attrs);
278         }
279     }
280 
281     public static class InflationTransitionSet extends TransitionSet {
InflationTransitionSet(Context context, AttributeSet attrs)282         public InflationTransitionSet(Context context, AttributeSet attrs) {
283             super(context, attrs);
284         }
285     }
286 
287     public static class InflationChangeImageTransform extends ChangeImageTransform {
InflationChangeImageTransform(Context context, AttributeSet attrs)288         public InflationChangeImageTransform(Context context, AttributeSet attrs) {
289             super(context, attrs);
290         }
291     }
292 
293     public static class InflationChangeTransform extends ChangeTransform {
InflationChangeTransform(Context context, AttributeSet attrs)294         public InflationChangeTransform(Context context, AttributeSet attrs) {
295             super(context, attrs);
296         }
297     }
298 
299     public static class InflationAutoTransition extends AutoTransition {
InflationAutoTransition(Context context, AttributeSet attrs)300         public InflationAutoTransition(Context context, AttributeSet attrs) {
301             super(context, attrs);
302         }
303     }
304 
305     public static class InflationChangeClipBounds extends ChangeClipBounds {
InflationChangeClipBounds(Context context, AttributeSet attrs)306         public InflationChangeClipBounds(Context context, AttributeSet attrs) {
307             super(context, attrs);
308         }
309     }
310 
311     public static class InflationChangeScroll extends ChangeScroll {
InflationChangeScroll(Context context, AttributeSet attrs)312         public InflationChangeScroll(Context context, AttributeSet attrs) {
313             super(context, attrs);
314         }
315     }
316 
317     public static class InflationExplode extends Explode {
InflationExplode(Context context, AttributeSet attrs)318         public InflationExplode(Context context, AttributeSet attrs) {
319             super(context, attrs);
320         }
321     }
322 }
323