Home | History | Annotate | Download | only in cts
      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.assertSame;
     22 import static org.mockito.Matchers.any;
     23 import static org.mockito.Mockito.mock;
     24 import static org.mockito.Mockito.times;
     25 import static org.mockito.Mockito.verify;
     26 
     27 import android.support.test.filters.MediumTest;
     28 import android.support.test.runner.AndroidJUnit4;
     29 import android.transition.ChangeBounds;
     30 import android.transition.Fade;
     31 import android.transition.Transition;
     32 import android.transition.TransitionSet;
     33 
     34 import org.junit.Test;
     35 import org.junit.runner.RunWith;
     36 
     37 @MediumTest
     38 @RunWith(AndroidJUnit4.class)
     39 public class TransitionSetTest extends BaseTransitionTest {
     40     @Test
     41     public void testTransitionTogether() throws Throwable {
     42         TransitionSet transitionSet = new TransitionSet();
     43         Fade fade = new Fade();
     44         final Transition.TransitionListener fadeListener =
     45                 mock(Transition.TransitionListener.class);
     46         fade.addListener(fadeListener);
     47         ChangeBounds changeBounds = new ChangeBounds();
     48         final Transition.TransitionListener changeBoundsListener =
     49                 mock(Transition.TransitionListener.class);
     50         changeBounds.addListener(changeBoundsListener);
     51         transitionSet.addTransition(fade);
     52         transitionSet.addTransition(changeBounds);
     53         mTransition = transitionSet;
     54         resetListener();
     55 
     56         assertEquals(TransitionSet.ORDERING_TOGETHER, transitionSet.getOrdering());
     57         enterScene(R.layout.scene1);
     58         startTransition(R.layout.scene3);
     59         mActivityRule.runOnUiThread(() -> {
     60             verify(fadeListener, times(1)).onTransitionStart(any());
     61             verify(changeBoundsListener, times(1)).onTransitionStart(any());
     62         });
     63     }
     64 
     65     @Test
     66     public void testTransitionSequentially() throws Throwable {
     67         TransitionSet transitionSet = new TransitionSet();
     68         Fade fade = new Fade();
     69         fade.setDuration(500);
     70         final Transition.TransitionListener fadeListener =
     71                 mock(Transition.TransitionListener.class);
     72         fade.addListener(fadeListener);
     73         ChangeBounds changeBounds = new ChangeBounds();
     74         final Transition.TransitionListener changeBoundsListener =
     75                 mock(Transition.TransitionListener.class);
     76         changeBounds.addListener(changeBoundsListener);
     77         transitionSet.addTransition(fade);
     78         transitionSet.addTransition(changeBounds);
     79         mTransition = transitionSet;
     80         resetListener();
     81 
     82         assertEquals(TransitionSet.ORDERING_TOGETHER, transitionSet.getOrdering());
     83         transitionSet.setOrdering(TransitionSet.ORDERING_SEQUENTIAL);
     84         assertEquals(TransitionSet.ORDERING_SEQUENTIAL, transitionSet.getOrdering());
     85 
     86         enterScene(R.layout.scene1);
     87         startTransition(R.layout.scene3);
     88         verify(fadeListener, within(500)).onTransitionStart(any());
     89         verify(fadeListener, times(1)).onTransitionStart(any());
     90 
     91         // change bounds shouldn't start until after fade finishes
     92         verify(fadeListener, times(0)).onTransitionEnd(any());
     93         verify(changeBoundsListener, times(0)).onTransitionStart(any());
     94 
     95         // now wait for the fade transition to end
     96         verify(fadeListener, within(1000)).onTransitionEnd(any());
     97 
     98         // The change bounds should start soon after
     99         verify(changeBoundsListener, within(500)).onTransitionStart(any());
    100         verify(changeBoundsListener, times(1)).onTransitionStart(any());
    101     }
    102 
    103     @Test
    104     public void testTransitionCount() throws Throwable {
    105         TransitionSet transitionSet = new TransitionSet();
    106         assertEquals(0, transitionSet.getTransitionCount());
    107 
    108         Fade fade = new Fade();
    109         ChangeBounds changeBounds = new ChangeBounds();
    110         transitionSet.addTransition(fade);
    111         transitionSet.addTransition(changeBounds);
    112 
    113         assertEquals(2, transitionSet.getTransitionCount());
    114         assertSame(fade, transitionSet.getTransitionAt(0));
    115         assertSame(changeBounds, transitionSet.getTransitionAt(1));
    116 
    117         transitionSet.removeTransition(fade);
    118 
    119         assertEquals(1, transitionSet.getTransitionCount());
    120         assertSame(changeBounds, transitionSet.getTransitionAt(0));
    121 
    122         transitionSet.removeTransition(fade); // remove one that isn't there
    123         assertEquals(1, transitionSet.getTransitionCount());
    124         assertSame(changeBounds, transitionSet.getTransitionAt(0));
    125     }
    126 }
    127 
    128