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 
     17 package android.graphics.drawable.cts;
     18 
     19 import android.animation.ValueAnimator;
     20 import android.content.Context;
     21 import android.graphics.cts.R;
     22 import android.graphics.drawable.Animatable;
     23 import android.graphics.drawable.Drawable;
     24 import android.graphics.drawable.DrawableContainer;
     25 import android.support.test.InstrumentationRegistry;
     26 import android.support.test.filters.MediumTest;
     27 import android.support.test.filters.SmallTest;
     28 import android.support.test.runner.AndroidJUnit4;
     29 
     30 import org.junit.After;
     31 import org.junit.Before;
     32 import org.junit.Test;
     33 import org.junit.runner.RunWith;
     34 
     35 import static org.junit.Assert.assertTrue;
     36 import static org.junit.Assert.assertFalse;
     37 
     38 /**
     39  * This test is used to verify that the CustomAnimationScaleListDrawable's current drawable depends
     40  * on animation duration scale. When the scale is 0, it is a static drawable, otherwise, it is an
     41  * animatable drawable.
     42  */
     43 @SmallTest
     44 @RunWith(AndroidJUnit4.class)
     45 public class CustomAnimationScaleListDrawableTest {
     46     private static final int DRAWABLE_ID = R.drawable.custom_animation_scale_list_drawable;
     47     private Context mContext;
     48     private float mOriginalScale;
     49 
     50     @Before
     51     public void setup() {
     52         mContext = InstrumentationRegistry.getTargetContext();
     53         mOriginalScale = ValueAnimator.getDurationScale();
     54     }
     55 
     56     @After
     57     public void teardown() {
     58         // Restore the original duration scale.
     59         ValueAnimator.setDurationScale(mOriginalScale);
     60     }
     61 
     62     @Test
     63     public void testNonZeroDurationScale() {
     64         // Set the duration scale to a non-zero value will cause the AnimationScaleListDrawable's
     65         // current drawable choose the animatable one.
     66         ValueAnimator.setDurationScale(2.0f);
     67         Drawable dr = mContext.getDrawable(DRAWABLE_ID);
     68         assertTrue(dr instanceof DrawableContainer);
     69         assertTrue(dr.getCurrent() instanceof Animatable);
     70     }
     71 
     72     @Test
     73     public void testZeroDurationScale() {
     74         // Set the duration scale to zero will cause the AnimationScaleListDrawable's current
     75         // drawable choose the static one.
     76         ValueAnimator.setDurationScale(0f);
     77         Drawable dr = mContext.getDrawable(DRAWABLE_ID);
     78         assertTrue(dr instanceof DrawableContainer);
     79         assertFalse(dr.getCurrent() instanceof Animatable);
     80     }
     81 }
     82