Home | History | Annotate | Download | only in transition
      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 
     17 package android.support.transition;
     18 
     19 import static org.hamcrest.core.Is.is;
     20 import static org.junit.Assert.assertThat;
     21 import static org.junit.Assert.fail;
     22 
     23 import android.graphics.Color;
     24 import android.support.test.annotation.UiThreadTest;
     25 import android.support.test.filters.MediumTest;
     26 import android.view.View;
     27 import android.widget.LinearLayout;
     28 
     29 import org.junit.Before;
     30 import org.junit.Test;
     31 
     32 @MediumTest
     33 public class AutoTransitionTest extends BaseTest {
     34 
     35     private LinearLayout mRoot;
     36     private View mView0;
     37     private View mView1;
     38 
     39     @UiThreadTest
     40     @Before
     41     public void setUp() {
     42         mRoot = (LinearLayout) rule.getActivity().getRoot();
     43         mView0 = new View(rule.getActivity());
     44         mView0.setBackgroundColor(Color.RED);
     45         mRoot.addView(mView0, new LinearLayout.LayoutParams(100, 100));
     46         mView1 = new View(rule.getActivity());
     47         mView1.setBackgroundColor(Color.BLUE);
     48         mRoot.addView(mView1, new LinearLayout.LayoutParams(100, 100));
     49     }
     50 
     51     @Test
     52     public void testLayoutBetweenFadeAndChangeBounds() throws Throwable {
     53         final LayoutCounter counter = new LayoutCounter();
     54         rule.runOnUiThread(new Runnable() {
     55             @Override
     56             public void run() {
     57                 assertThat(mView1.getY(), is(100.f));
     58                 assertThat(mView0.getVisibility(), is(View.VISIBLE));
     59                 mView1.addOnLayoutChangeListener(counter);
     60             }
     61         });
     62         final SyncTransitionListener listener = new SyncTransitionListener(
     63                 SyncTransitionListener.EVENT_END);
     64         final Transition transition = new AutoTransition();
     65         transition.addListener(listener);
     66         rule.runOnUiThread(new Runnable() {
     67             @Override
     68             public void run() {
     69                 TransitionManager.beginDelayedTransition(mRoot, transition);
     70                 // This makes view0 fade out and causes view1 to move upwards.
     71                 mView0.setVisibility(View.GONE);
     72             }
     73         });
     74         assertThat("Timed out waiting for the TransitionListener",
     75                 listener.await(), is(true));
     76         assertThat(mView1.getY(), is(0.f));
     77         assertThat(mView0.getVisibility(), is(View.GONE));
     78         counter.reset();
     79         listener.reset();
     80         rule.runOnUiThread(new Runnable() {
     81             @Override
     82             public void run() {
     83                 TransitionManager.beginDelayedTransition(mRoot, transition);
     84                 // Revert
     85                 mView0.setVisibility(View.VISIBLE);
     86             }
     87         });
     88         assertThat("Timed out waiting for the TransitionListener",
     89                 listener.await(), is(true));
     90         assertThat(mView1.getY(), is(100.f));
     91         assertThat(mView0.getVisibility(), is(View.VISIBLE));
     92     }
     93 
     94     private static class LayoutCounter implements View.OnLayoutChangeListener {
     95 
     96         private int mCalledCount;
     97 
     98         @Override
     99         public void onLayoutChange(View v, int left, int top, int right, int bottom,
    100                 int oldLeft, int oldTop, int oldRight, int oldBottom) {
    101             mCalledCount++;
    102             // There should not be more than one layout request to view1.
    103             if (mCalledCount > 1) {
    104                 fail("View layout happened too many times");
    105             }
    106         }
    107 
    108         void reset() {
    109             mCalledCount = 0;
    110         }
    111 
    112     }
    113 
    114 }
    115