Home | History | Annotate | Download | only in transition
      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.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertNotNull;
     21 import static org.junit.Assert.assertNull;
     22 import static org.junit.Assert.assertTrue;
     23 
     24 import android.graphics.Rect;
     25 import android.support.test.filters.MediumTest;
     26 import android.support.test.filters.SdkSuppress;
     27 import android.view.View;
     28 
     29 import androidx.core.view.ViewCompat;
     30 import androidx.transition.test.R;
     31 
     32 import org.junit.Test;
     33 
     34 @MediumTest
     35 public class ChangeClipBoundsTest extends BaseTransitionTest {
     36 
     37     @Override
     38     Transition createTransition() {
     39         return new ChangeClipBounds();
     40     }
     41 
     42     @SdkSuppress(minSdkVersion = 18)
     43     @Test
     44     public void testChangeClipBounds() throws Throwable {
     45         enterScene(R.layout.scene1);
     46 
     47         final View redSquare = rule.getActivity().findViewById(R.id.redSquare);
     48         final Rect newClip = new Rect(redSquare.getLeft() + 10, redSquare.getTop() + 10,
     49                 redSquare.getRight() - 10, redSquare.getBottom() - 10);
     50 
     51         rule.runOnUiThread(new Runnable() {
     52             @Override
     53             public void run() {
     54                 assertNull(ViewCompat.getClipBounds(redSquare));
     55                 TransitionManager.beginDelayedTransition(mRoot, mTransition);
     56                 ViewCompat.setClipBounds(redSquare, newClip);
     57             }
     58         });
     59         waitForStart();
     60         Thread.sleep(150);
     61         rule.runOnUiThread(new Runnable() {
     62             @Override
     63             public void run() {
     64                 Rect midClip = ViewCompat.getClipBounds(redSquare);
     65                 assertNotNull(midClip);
     66                 assertTrue(midClip.left > 0 && midClip.left < newClip.left);
     67                 assertTrue(midClip.top > 0 && midClip.top < newClip.top);
     68                 assertTrue(midClip.right < redSquare.getRight() && midClip.right > newClip.right);
     69                 assertTrue(midClip.bottom < redSquare.getBottom()
     70                         && midClip.bottom > newClip.bottom);
     71             }
     72         });
     73         waitForEnd();
     74 
     75         rule.runOnUiThread(new Runnable() {
     76             @Override
     77             public void run() {
     78                 final Rect endRect = ViewCompat.getClipBounds(redSquare);
     79                 assertNotNull(endRect);
     80                 assertEquals(newClip, endRect);
     81             }
     82         });
     83 
     84         resetListener();
     85         rule.runOnUiThread(new Runnable() {
     86             @Override
     87             public void run() {
     88                 TransitionManager.beginDelayedTransition(mRoot, mTransition);
     89                 ViewCompat.setClipBounds(redSquare, null);
     90             }
     91         });
     92         waitForStart();
     93         Thread.sleep(150);
     94         rule.runOnUiThread(new Runnable() {
     95             @Override
     96             public void run() {
     97                 Rect midClip = ViewCompat.getClipBounds(redSquare);
     98                 assertNotNull(midClip);
     99                 assertTrue(midClip.left > 0 && midClip.left < newClip.left);
    100                 assertTrue(midClip.top > 0 && midClip.top < newClip.top);
    101                 assertTrue(midClip.right < redSquare.getRight() && midClip.right > newClip.right);
    102                 assertTrue(midClip.bottom < redSquare.getBottom()
    103                         && midClip.bottom > newClip.bottom);
    104             }
    105         });
    106         waitForEnd();
    107 
    108         rule.runOnUiThread(new Runnable() {
    109             @Override
    110             public void run() {
    111                 assertNull(ViewCompat.getClipBounds(redSquare));
    112             }
    113         });
    114 
    115     }
    116 
    117     @Test
    118     public void dummy() {
    119         // Avoid "No tests found" on older devices
    120     }
    121 
    122 }
    123