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.hamcrest.CoreMatchers.is;
20 import static org.hamcrest.Matchers.greaterThan;
21 import static org.hamcrest.Matchers.lessThan;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertThat;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Mockito.never;
26 import static org.mockito.Mockito.verify;
27 
28 import android.os.SystemClock;
29 import android.support.test.InstrumentationRegistry;
30 import android.support.test.filters.MediumTest;
31 import android.view.View;
32 
33 import androidx.transition.test.R;
34 
35 import org.junit.Test;
36 
37 @MediumTest
38 public class ExplodeTest extends BaseTransitionTest {
39 
40     @Override
createTransition()41     Transition createTransition() {
42         return new Explode();
43     }
44 
45     @Test
testExplode()46     public void testExplode() throws Throwable {
47         enterScene(R.layout.scene10);
48         final View redSquare = rule.getActivity().findViewById(R.id.redSquare);
49         final View greenSquare = rule.getActivity().findViewById(R.id.greenSquare);
50         final View blueSquare = rule.getActivity().findViewById(R.id.blueSquare);
51         final View yellowSquare = rule.getActivity().findViewById(R.id.yellowSquare);
52 
53         rule.runOnUiThread(new Runnable() {
54             @Override
55             public void run() {
56                 TransitionManager.beginDelayedTransition(mRoot, mTransition);
57                 redSquare.setVisibility(View.INVISIBLE);
58                 greenSquare.setVisibility(View.INVISIBLE);
59                 blueSquare.setVisibility(View.INVISIBLE);
60                 yellowSquare.setVisibility(View.INVISIBLE);
61             }
62         });
63         waitForStart();
64         verify(mListener, never()).onTransitionEnd(any(Transition.class));
65         assertEquals(View.VISIBLE, redSquare.getVisibility());
66         assertEquals(View.VISIBLE, greenSquare.getVisibility());
67         assertEquals(View.VISIBLE, blueSquare.getVisibility());
68         assertEquals(View.VISIBLE, yellowSquare.getVisibility());
69         float redStartX = redSquare.getTranslationX();
70         float redStartY = redSquare.getTranslationY();
71 
72         SystemClock.sleep(100);
73         verifyTranslation(redSquare, true, true);
74         verifyTranslation(greenSquare, false, true);
75         verifyTranslation(blueSquare, false, false);
76         verifyTranslation(yellowSquare, true, false);
77         assertThat(redStartX, is(greaterThan(redSquare.getTranslationX()))); // moving left
78         assertThat(redStartY, is(greaterThan(redSquare.getTranslationY()))); // moving up
79         waitForEnd();
80 
81         verifyNoTranslation(redSquare);
82         verifyNoTranslation(greenSquare);
83         verifyNoTranslation(blueSquare);
84         verifyNoTranslation(yellowSquare);
85         assertEquals(View.INVISIBLE, redSquare.getVisibility());
86         assertEquals(View.INVISIBLE, greenSquare.getVisibility());
87         assertEquals(View.INVISIBLE, blueSquare.getVisibility());
88         assertEquals(View.INVISIBLE, yellowSquare.getVisibility());
89     }
90 
91     @Test
testImplode()92     public void testImplode() throws Throwable {
93         enterScene(R.layout.scene10);
94         final View redSquare = rule.getActivity().findViewById(R.id.redSquare);
95         final View greenSquare = rule.getActivity().findViewById(R.id.greenSquare);
96         final View blueSquare = rule.getActivity().findViewById(R.id.blueSquare);
97         final View yellowSquare = rule.getActivity().findViewById(R.id.yellowSquare);
98 
99         rule.runOnUiThread(new Runnable() {
100             @Override
101             public void run() {
102                 redSquare.setVisibility(View.INVISIBLE);
103                 greenSquare.setVisibility(View.INVISIBLE);
104                 blueSquare.setVisibility(View.INVISIBLE);
105                 yellowSquare.setVisibility(View.INVISIBLE);
106             }
107         });
108         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
109 
110         rule.runOnUiThread(new Runnable() {
111             @Override
112             public void run() {
113                 TransitionManager.beginDelayedTransition(mRoot, mTransition);
114                 redSquare.setVisibility(View.VISIBLE);
115                 greenSquare.setVisibility(View.VISIBLE);
116                 blueSquare.setVisibility(View.VISIBLE);
117                 yellowSquare.setVisibility(View.VISIBLE);
118             }
119         });
120         waitForStart();
121 
122         assertEquals(View.VISIBLE, redSquare.getVisibility());
123         assertEquals(View.VISIBLE, greenSquare.getVisibility());
124         assertEquals(View.VISIBLE, blueSquare.getVisibility());
125         assertEquals(View.VISIBLE, yellowSquare.getVisibility());
126         float redStartX = redSquare.getTranslationX();
127         float redStartY = redSquare.getTranslationY();
128 
129         SystemClock.sleep(100);
130         verifyTranslation(redSquare, true, true);
131         verifyTranslation(greenSquare, false, true);
132         verifyTranslation(blueSquare, false, false);
133         verifyTranslation(yellowSquare, true, false);
134         assertThat(redStartX, is(lessThan(redSquare.getTranslationX()))); // moving right
135         assertThat(redStartY, is(lessThan(redSquare.getTranslationY()))); // moving down
136         waitForEnd();
137 
138         verifyNoTranslation(redSquare);
139         verifyNoTranslation(greenSquare);
140         verifyNoTranslation(blueSquare);
141         verifyNoTranslation(yellowSquare);
142         assertEquals(View.VISIBLE, redSquare.getVisibility());
143         assertEquals(View.VISIBLE, greenSquare.getVisibility());
144         assertEquals(View.VISIBLE, blueSquare.getVisibility());
145         assertEquals(View.VISIBLE, yellowSquare.getVisibility());
146     }
147 
verifyTranslation(View view, boolean goLeft, boolean goUp)148     private void verifyTranslation(View view, boolean goLeft, boolean goUp) {
149         float translationX = view.getTranslationX();
150         float translationY = view.getTranslationY();
151 
152         if (goLeft) {
153             assertThat(translationX, is(lessThan(0.f)));
154         } else {
155             assertThat(translationX, is(greaterThan(0.f)));
156         }
157 
158         if (goUp) {
159             assertThat(translationY, is(lessThan(0.f)));
160         } else {
161             assertThat(translationY, is(greaterThan(0.f)));
162         }
163     }
164 
verifyNoTranslation(View view)165     private void verifyNoTranslation(View view) {
166         assertEquals(0f, view.getTranslationX(), 0.0f);
167         assertEquals(0f, view.getTranslationY(), 0.0f);
168     }
169 
170 }
171