ExplodeTest.java revision b83b084834029a274192cfb24022f0edd73e03ce
14c904b38c763179727b9b5ea7a80454630545663Yuichi Araki/*
24c904b38c763179727b9b5ea7a80454630545663Yuichi Araki * Copyright (C) 2017 The Android Open Source Project
34c904b38c763179727b9b5ea7a80454630545663Yuichi Araki *
44c904b38c763179727b9b5ea7a80454630545663Yuichi Araki * Licensed under the Apache License, Version 2.0 (the "License");
54c904b38c763179727b9b5ea7a80454630545663Yuichi Araki * you may not use this file except in compliance with the License.
64c904b38c763179727b9b5ea7a80454630545663Yuichi Araki * You may obtain a copy of the License at
74c904b38c763179727b9b5ea7a80454630545663Yuichi Araki *
84c904b38c763179727b9b5ea7a80454630545663Yuichi Araki *      http://www.apache.org/licenses/LICENSE-2.0
94c904b38c763179727b9b5ea7a80454630545663Yuichi Araki *
104c904b38c763179727b9b5ea7a80454630545663Yuichi Araki * Unless required by applicable law or agreed to in writing, software
114c904b38c763179727b9b5ea7a80454630545663Yuichi Araki * distributed under the License is distributed on an "AS IS" BASIS,
124c904b38c763179727b9b5ea7a80454630545663Yuichi Araki * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134c904b38c763179727b9b5ea7a80454630545663Yuichi Araki * See the License for the specific language governing permissions and
144c904b38c763179727b9b5ea7a80454630545663Yuichi Araki * limitations under the License.
154c904b38c763179727b9b5ea7a80454630545663Yuichi Araki */
164c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
174c904b38c763179727b9b5ea7a80454630545663Yuichi Arakipackage android.support.transition;
184c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
19b83b084834029a274192cfb24022f0edd73e03ceYuichi Arakiimport static org.hamcrest.CoreMatchers.is;
20b83b084834029a274192cfb24022f0edd73e03ceYuichi Arakiimport static org.hamcrest.Matchers.greaterThan;
21b83b084834029a274192cfb24022f0edd73e03ceYuichi Arakiimport static org.hamcrest.Matchers.lessThan;
224c904b38c763179727b9b5ea7a80454630545663Yuichi Arakiimport static org.junit.Assert.assertEquals;
23b83b084834029a274192cfb24022f0edd73e03ceYuichi Arakiimport static org.junit.Assert.assertThat;
244c904b38c763179727b9b5ea7a80454630545663Yuichi Arakiimport static org.mockito.Matchers.any;
254c904b38c763179727b9b5ea7a80454630545663Yuichi Arakiimport static org.mockito.Mockito.never;
264c904b38c763179727b9b5ea7a80454630545663Yuichi Arakiimport static org.mockito.Mockito.verify;
274c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
284c904b38c763179727b9b5ea7a80454630545663Yuichi Arakiimport android.os.SystemClock;
294c904b38c763179727b9b5ea7a80454630545663Yuichi Arakiimport android.support.test.InstrumentationRegistry;
304c904b38c763179727b9b5ea7a80454630545663Yuichi Arakiimport android.support.test.filters.MediumTest;
314c904b38c763179727b9b5ea7a80454630545663Yuichi Arakiimport android.support.transition.test.R;
324c904b38c763179727b9b5ea7a80454630545663Yuichi Arakiimport android.view.View;
334c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
344c904b38c763179727b9b5ea7a80454630545663Yuichi Arakiimport org.junit.Test;
354c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
364c904b38c763179727b9b5ea7a80454630545663Yuichi Araki@MediumTest
374c904b38c763179727b9b5ea7a80454630545663Yuichi Arakipublic class ExplodeTest extends BaseTransitionTest {
384c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
394c904b38c763179727b9b5ea7a80454630545663Yuichi Araki    @Override
404c904b38c763179727b9b5ea7a80454630545663Yuichi Araki    Transition createTransition() {
414c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        return new Explode();
424c904b38c763179727b9b5ea7a80454630545663Yuichi Araki    }
434c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
444c904b38c763179727b9b5ea7a80454630545663Yuichi Araki    @Test
454c904b38c763179727b9b5ea7a80454630545663Yuichi Araki    public void testExplode() throws Throwable {
464c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        enterScene(R.layout.scene10);
474c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        final View redSquare = rule.getActivity().findViewById(R.id.redSquare);
484c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        final View greenSquare = rule.getActivity().findViewById(R.id.greenSquare);
494c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        final View blueSquare = rule.getActivity().findViewById(R.id.blueSquare);
504c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        final View yellowSquare = rule.getActivity().findViewById(R.id.yellowSquare);
514c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
524c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        rule.runOnUiThread(new Runnable() {
534c904b38c763179727b9b5ea7a80454630545663Yuichi Araki            @Override
544c904b38c763179727b9b5ea7a80454630545663Yuichi Araki            public void run() {
554c904b38c763179727b9b5ea7a80454630545663Yuichi Araki                TransitionManager.beginDelayedTransition(mRoot, mTransition);
564c904b38c763179727b9b5ea7a80454630545663Yuichi Araki                redSquare.setVisibility(View.INVISIBLE);
574c904b38c763179727b9b5ea7a80454630545663Yuichi Araki                greenSquare.setVisibility(View.INVISIBLE);
584c904b38c763179727b9b5ea7a80454630545663Yuichi Araki                blueSquare.setVisibility(View.INVISIBLE);
594c904b38c763179727b9b5ea7a80454630545663Yuichi Araki                yellowSquare.setVisibility(View.INVISIBLE);
604c904b38c763179727b9b5ea7a80454630545663Yuichi Araki            }
614c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        });
624c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        waitForStart();
634c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        verify(mListener, never()).onTransitionEnd(any(Transition.class));
644c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        assertEquals(View.VISIBLE, redSquare.getVisibility());
654c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        assertEquals(View.VISIBLE, greenSquare.getVisibility());
664c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        assertEquals(View.VISIBLE, blueSquare.getVisibility());
674c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        assertEquals(View.VISIBLE, yellowSquare.getVisibility());
684c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        float redStartX = redSquare.getTranslationX();
694c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        float redStartY = redSquare.getTranslationY();
704c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
714c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        SystemClock.sleep(100);
724c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        verifyTranslation(redSquare, true, true);
734c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        verifyTranslation(greenSquare, false, true);
744c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        verifyTranslation(blueSquare, false, false);
754c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        verifyTranslation(yellowSquare, true, false);
76b83b084834029a274192cfb24022f0edd73e03ceYuichi Araki        assertThat(redStartX, is(greaterThan(redSquare.getTranslationX()))); // moving left
77b83b084834029a274192cfb24022f0edd73e03ceYuichi Araki        assertThat(redStartY, is(greaterThan(redSquare.getTranslationY()))); // moving up
784c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        waitForEnd();
794c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
804c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        verifyNoTranslation(redSquare);
814c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        verifyNoTranslation(greenSquare);
824c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        verifyNoTranslation(blueSquare);
834c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        verifyNoTranslation(yellowSquare);
844c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        assertEquals(View.INVISIBLE, redSquare.getVisibility());
854c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        assertEquals(View.INVISIBLE, greenSquare.getVisibility());
864c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        assertEquals(View.INVISIBLE, blueSquare.getVisibility());
874c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        assertEquals(View.INVISIBLE, yellowSquare.getVisibility());
884c904b38c763179727b9b5ea7a80454630545663Yuichi Araki    }
894c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
904c904b38c763179727b9b5ea7a80454630545663Yuichi Araki    @Test
914c904b38c763179727b9b5ea7a80454630545663Yuichi Araki    public void testImplode() throws Throwable {
924c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        enterScene(R.layout.scene10);
934c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        final View redSquare = rule.getActivity().findViewById(R.id.redSquare);
944c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        final View greenSquare = rule.getActivity().findViewById(R.id.greenSquare);
954c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        final View blueSquare = rule.getActivity().findViewById(R.id.blueSquare);
964c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        final View yellowSquare = rule.getActivity().findViewById(R.id.yellowSquare);
974c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
984c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        rule.runOnUiThread(new Runnable() {
994c904b38c763179727b9b5ea7a80454630545663Yuichi Araki            @Override
1004c904b38c763179727b9b5ea7a80454630545663Yuichi Araki            public void run() {
1014c904b38c763179727b9b5ea7a80454630545663Yuichi Araki                redSquare.setVisibility(View.INVISIBLE);
1024c904b38c763179727b9b5ea7a80454630545663Yuichi Araki                greenSquare.setVisibility(View.INVISIBLE);
1034c904b38c763179727b9b5ea7a80454630545663Yuichi Araki                blueSquare.setVisibility(View.INVISIBLE);
1044c904b38c763179727b9b5ea7a80454630545663Yuichi Araki                yellowSquare.setVisibility(View.INVISIBLE);
1054c904b38c763179727b9b5ea7a80454630545663Yuichi Araki            }
1064c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        });
1074c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
1084c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
1094c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        rule.runOnUiThread(new Runnable() {
1104c904b38c763179727b9b5ea7a80454630545663Yuichi Araki            @Override
1114c904b38c763179727b9b5ea7a80454630545663Yuichi Araki            public void run() {
1124c904b38c763179727b9b5ea7a80454630545663Yuichi Araki                TransitionManager.beginDelayedTransition(mRoot, mTransition);
1134c904b38c763179727b9b5ea7a80454630545663Yuichi Araki                redSquare.setVisibility(View.VISIBLE);
1144c904b38c763179727b9b5ea7a80454630545663Yuichi Araki                greenSquare.setVisibility(View.VISIBLE);
1154c904b38c763179727b9b5ea7a80454630545663Yuichi Araki                blueSquare.setVisibility(View.VISIBLE);
1164c904b38c763179727b9b5ea7a80454630545663Yuichi Araki                yellowSquare.setVisibility(View.VISIBLE);
1174c904b38c763179727b9b5ea7a80454630545663Yuichi Araki            }
1184c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        });
1194c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        waitForStart();
1204c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
1214c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        assertEquals(View.VISIBLE, redSquare.getVisibility());
1224c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        assertEquals(View.VISIBLE, greenSquare.getVisibility());
1234c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        assertEquals(View.VISIBLE, blueSquare.getVisibility());
1244c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        assertEquals(View.VISIBLE, yellowSquare.getVisibility());
1254c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        float redStartX = redSquare.getTranslationX();
1264c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        float redStartY = redSquare.getTranslationY();
1274c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
1284c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        SystemClock.sleep(100);
1294c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        verifyTranslation(redSquare, true, true);
1304c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        verifyTranslation(greenSquare, false, true);
1314c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        verifyTranslation(blueSquare, false, false);
1324c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        verifyTranslation(yellowSquare, true, false);
133b83b084834029a274192cfb24022f0edd73e03ceYuichi Araki        assertThat(redStartX, is(lessThan(redSquare.getTranslationX()))); // moving right
134b83b084834029a274192cfb24022f0edd73e03ceYuichi Araki        assertThat(redStartY, is(lessThan(redSquare.getTranslationY()))); // moving down
1354c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        waitForEnd();
1364c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
1374c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        verifyNoTranslation(redSquare);
1384c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        verifyNoTranslation(greenSquare);
1394c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        verifyNoTranslation(blueSquare);
1404c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        verifyNoTranslation(yellowSquare);
1414c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        assertEquals(View.VISIBLE, redSquare.getVisibility());
1424c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        assertEquals(View.VISIBLE, greenSquare.getVisibility());
1434c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        assertEquals(View.VISIBLE, blueSquare.getVisibility());
1444c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        assertEquals(View.VISIBLE, yellowSquare.getVisibility());
1454c904b38c763179727b9b5ea7a80454630545663Yuichi Araki    }
1464c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
1474c904b38c763179727b9b5ea7a80454630545663Yuichi Araki    private void verifyTranslation(View view, boolean goLeft, boolean goUp) {
1484c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        float translationX = view.getTranslationX();
1494c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        float translationY = view.getTranslationY();
1504c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
1514c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        if (goLeft) {
152b83b084834029a274192cfb24022f0edd73e03ceYuichi Araki            assertThat(translationX, is(lessThan(0.f)));
1534c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        } else {
154b83b084834029a274192cfb24022f0edd73e03ceYuichi Araki            assertThat(translationX, is(greaterThan(0.f)));
1554c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        }
1564c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
1574c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        if (goUp) {
158b83b084834029a274192cfb24022f0edd73e03ceYuichi Araki            assertThat(translationY, is(lessThan(0.f)));
1594c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        } else {
160b83b084834029a274192cfb24022f0edd73e03ceYuichi Araki            assertThat(translationY, is(greaterThan(0.f)));
1614c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        }
1624c904b38c763179727b9b5ea7a80454630545663Yuichi Araki    }
1634c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
1644c904b38c763179727b9b5ea7a80454630545663Yuichi Araki    private void verifyNoTranslation(View view) {
1654c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        assertEquals(0f, view.getTranslationX(), 0.0f);
1664c904b38c763179727b9b5ea7a80454630545663Yuichi Araki        assertEquals(0f, view.getTranslationY(), 0.0f);
1674c904b38c763179727b9b5ea7a80454630545663Yuichi Araki    }
1684c904b38c763179727b9b5ea7a80454630545663Yuichi Araki
1694c904b38c763179727b9b5ea7a80454630545663Yuichi Araki}
170