Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 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.widget.cts;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertNull;
     21 import static org.junit.Assert.assertSame;
     22 import static org.junit.Assert.assertTrue;
     23 
     24 import android.app.Activity;
     25 import android.util.AttributeSet;
     26 import android.util.Xml;
     27 import android.view.View;
     28 import android.view.ViewGroup.LayoutParams;
     29 import android.view.animation.AccelerateInterpolator;
     30 import android.view.animation.Animation;
     31 import android.view.animation.AnimationSet;
     32 import android.widget.RelativeLayout;
     33 import android.widget.ViewAnimator;
     34 
     35 import androidx.test.annotation.UiThreadTest;
     36 import androidx.test.filters.MediumTest;
     37 import androidx.test.rule.ActivityTestRule;
     38 import androidx.test.runner.AndroidJUnit4;
     39 
     40 import org.junit.Before;
     41 import org.junit.Rule;
     42 import org.junit.Test;
     43 import org.junit.runner.RunWith;
     44 import org.xmlpull.v1.XmlPullParser;
     45 
     46 @MediumTest
     47 @RunWith(AndroidJUnit4.class)
     48 public class ViewAnimatorTest {
     49     private Activity mActivity;
     50     private ViewAnimator mViewAnimator;
     51     private AttributeSet mAttributeSet;
     52 
     53     @Rule
     54     public ActivityTestRule<ViewAnimatorCtsActivity> mActivityRule =
     55             new ActivityTestRule<>(ViewAnimatorCtsActivity.class);
     56 
     57     @Before
     58     public void setup() {
     59         mActivity = mActivityRule.getActivity();
     60 
     61         XmlPullParser parser = mActivity.getResources().getXml(R.layout.viewanimator_layout);
     62         mAttributeSet = Xml.asAttributeSet(parser);
     63         mViewAnimator = new ViewAnimator(mActivity, mAttributeSet);
     64     }
     65 
     66     @Test
     67     public void testConstructor() {
     68         new ViewAnimator(mActivity);
     69         new ViewAnimator(mActivity, mAttributeSet);
     70     }
     71 
     72     @Test
     73     public void testAccessInAnimation() {
     74         AnimationSet expected = new AnimationSet(mActivity, mAttributeSet);
     75         assertNull(mViewAnimator.getInAnimation());
     76 
     77         mViewAnimator.setInAnimation(expected);
     78         assertSame(expected, mViewAnimator.getInAnimation());
     79 
     80         // input null as param
     81         mViewAnimator.setInAnimation(null);
     82         assertNull(mViewAnimator.getInAnimation());
     83 
     84         mViewAnimator.setInAnimation(mActivity, R.anim.anim_alpha);
     85         Animation animation = mViewAnimator.getInAnimation();
     86         assertTrue(animation.getInterpolator() instanceof AccelerateInterpolator);
     87         assertEquals(500, animation.getDuration());
     88     }
     89 
     90     @UiThreadTest
     91     @Test
     92     public void testShowNext() {
     93         final View v1 = mActivity.findViewById(R.id.ok);
     94         final View v2 = mActivity.findViewById(R.id.cancel);
     95         final View v3 = mActivity.findViewById(R.id.label);
     96         final View v4 = mActivity.findViewById(R.id.entry);
     97         final RelativeLayout parent = (RelativeLayout) v1.getParent();
     98 
     99         parent.removeView(v1);
    100         parent.removeView(v2);
    101         parent.removeView(v3);
    102         parent.removeView(v4);
    103         assertEquals(0, mViewAnimator.getChildCount());
    104 
    105         mViewAnimator.addView(v1);
    106         mViewAnimator.addView(v2);
    107         mViewAnimator.addView(v3);
    108         mViewAnimator.addView(v4);
    109         assertEquals(4, mViewAnimator.getChildCount());
    110 
    111         int current = 0;
    112 
    113         mViewAnimator.setDisplayedChild(current);
    114         assertEquals(current, mViewAnimator.getDisplayedChild());
    115 
    116         mViewAnimator.showNext();
    117         assertEquals(1, mViewAnimator.getDisplayedChild());
    118 
    119         mViewAnimator.showNext();
    120         assertEquals(2, mViewAnimator.getDisplayedChild());
    121 
    122         mViewAnimator.showNext();
    123         assertEquals(3, mViewAnimator.getDisplayedChild());
    124 
    125         mViewAnimator.removeAllViews();
    126         assertEquals(0, mViewAnimator.getChildCount());
    127     }
    128 
    129     @Test
    130     public void testSetAnimateFirstView() {
    131         mViewAnimator.setAnimateFirstView(true);
    132         mViewAnimator.setAnimateFirstView(false);
    133 
    134         // TODO: how to check
    135     }
    136 
    137     @UiThreadTest
    138     @Test
    139     public void testAccessDisplayedChild() {
    140         final View v1 = mActivity.findViewById(R.id.ok);
    141         final View v2 = mActivity.findViewById(R.id.cancel);
    142         final RelativeLayout parent = (RelativeLayout) v1.getParent();
    143 
    144         parent.removeView(v1);
    145         parent.removeView(v2);
    146         assertEquals(0, mViewAnimator.getChildCount());
    147 
    148         mViewAnimator.addView(v1);
    149         assertEquals(1, mViewAnimator.getChildCount());
    150 
    151         mViewAnimator.addView(v2);
    152         assertEquals(2, mViewAnimator.getChildCount());
    153 
    154         mViewAnimator.setDisplayedChild(0);
    155         assertEquals(0, mViewAnimator.getDisplayedChild());
    156 
    157         // set a negative value, then switch to getChildCount()-1.
    158         mViewAnimator.setDisplayedChild(-1);
    159         assertEquals(1, mViewAnimator.getDisplayedChild());
    160 
    161         // set larger than ChildCount, then switch to 0.
    162         mViewAnimator.setDisplayedChild(2);
    163         assertEquals(0, mViewAnimator.getDisplayedChild());
    164 
    165         mViewAnimator.setDisplayedChild(1);
    166         assertEquals(1, mViewAnimator.getDisplayedChild());
    167 
    168         mViewAnimator.removeAllViews();
    169         assertEquals(0, mViewAnimator.getChildCount());
    170     }
    171 
    172     @UiThreadTest
    173     @Test
    174     public void testAccessDisplayedChildBoundary() {
    175         final View v1 = mActivity.findViewById(R.id.ok);
    176         final View v2 = mActivity.findViewById(R.id.cancel);
    177         final RelativeLayout parent = (RelativeLayout) v1.getParent();
    178 
    179         parent.removeView(v1);
    180         parent.removeView(v2);
    181         assertEquals(0, mViewAnimator.getChildCount());
    182 
    183         mViewAnimator.addView(v1);
    184         assertEquals(1, mViewAnimator.getChildCount());
    185 
    186         mViewAnimator.addView(v2);
    187         assertEquals(2, mViewAnimator.getChildCount());
    188 
    189         int index = -1;
    190         mViewAnimator.setDisplayedChild(index);
    191         assertEquals(1, mViewAnimator.getDisplayedChild());
    192 
    193         index = 2;
    194         mViewAnimator.setDisplayedChild(index);
    195         assertEquals(0, mViewAnimator.getDisplayedChild());
    196 
    197         mViewAnimator.removeAllViews();
    198         assertEquals(0, mViewAnimator.getChildCount());
    199     }
    200 
    201     @UiThreadTest
    202     @Test
    203     public void testGetBaseline() {
    204         final View v1 = mActivity.findViewById(R.id.ok);
    205         final View v2 = mActivity.findViewById(R.id.cancel);
    206         final RelativeLayout parent = (RelativeLayout) v1.getParent();
    207 
    208         parent.removeView(v1);
    209         parent.removeView(v2);
    210         assertEquals(0, mViewAnimator.getChildCount());
    211 
    212         mViewAnimator.addView(v1);
    213         mViewAnimator.addView(v2);
    214         assertEquals(2, mViewAnimator.getChildCount());
    215 
    216         int expected = v1.getBaseline();
    217         mViewAnimator.setDisplayedChild(0);
    218         assertEquals(expected, mViewAnimator.getBaseline());
    219 
    220         expected = v2.getBaseline();
    221         mViewAnimator.setDisplayedChild(1);
    222         assertEquals(expected, mViewAnimator.getBaseline());
    223 
    224         mViewAnimator.removeAllViews();
    225         assertEquals(0, mViewAnimator.getChildCount());
    226     }
    227 
    228     @UiThreadTest
    229     @Test
    230     public void testShowPrevious() {
    231         final View v1 = mActivity.findViewById(R.id.ok);
    232         final View v2 = mActivity.findViewById(R.id.cancel);
    233         final View v3 = mActivity.findViewById(R.id.label);
    234         final View v4 = mActivity.findViewById(R.id.entry);
    235         final RelativeLayout parent = (RelativeLayout) v1.getParent();
    236 
    237         parent.removeView(v1);
    238         parent.removeView(v2);
    239         parent.removeView(v3);
    240         parent.removeView(v4);
    241         assertEquals(0, mViewAnimator.getChildCount());
    242 
    243         mViewAnimator.addView(v1);
    244         mViewAnimator.addView(v2);
    245         mViewAnimator.addView(v3);
    246         mViewAnimator.addView(v4);
    247         assertEquals(4, mViewAnimator.getChildCount());
    248 
    249         int current = 3;
    250 
    251         // set DisplayedChild by {@link mViewAnimator#setDisplayedChild(int)}
    252         mViewAnimator.setDisplayedChild(current);
    253         assertEquals(current, mViewAnimator.getDisplayedChild());
    254 
    255         mViewAnimator.showPrevious();
    256         assertEquals(2, mViewAnimator.getDisplayedChild());
    257 
    258         mViewAnimator.showPrevious();
    259         assertEquals(1, mViewAnimator.getDisplayedChild());
    260 
    261         mViewAnimator.showPrevious();
    262         assertEquals(0, mViewAnimator.getDisplayedChild());
    263 
    264         mViewAnimator.removeAllViews();
    265         assertEquals(0, mViewAnimator.getChildCount());
    266     }
    267 
    268     @UiThreadTest
    269     @Test
    270     public void testGetCurrentView() {
    271         final View v = mActivity.findViewById(R.id.label);
    272         final RelativeLayout parent = (RelativeLayout) v.getParent();
    273 
    274         parent.removeView(v);
    275         assertEquals(0, mViewAnimator.getChildCount());
    276 
    277         mViewAnimator.addView(v);
    278         assertEquals(1, mViewAnimator.getChildCount());
    279 
    280         int current = 0;
    281         mViewAnimator.setDisplayedChild(current);
    282         assertSame(v, mViewAnimator.getCurrentView());
    283 
    284         mViewAnimator.removeAllViews();
    285         assertEquals(0, mViewAnimator.getChildCount());
    286     }
    287 
    288     @UiThreadTest
    289     @Test
    290     public void testAddView() {
    291         final View v1 = mActivity.findViewById(R.id.ok);
    292         final View v2 = mActivity.findViewById(R.id.cancel);
    293         final RelativeLayout parent = (RelativeLayout) v1.getParent();
    294 
    295         parent.removeView(v1);
    296         parent.removeView(v2);
    297         assertEquals(0, mViewAnimator.getChildCount());
    298 
    299         LayoutParams p =
    300             new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    301         mViewAnimator.addView(v1, 0, p);
    302         assertEquals(1, mViewAnimator.getChildCount());
    303         assertEquals(0, mViewAnimator.indexOfChild(v1));
    304 
    305         mViewAnimator.addView(v2, 1, p);
    306         assertEquals(2, mViewAnimator.getChildCount());
    307         assertEquals(1, mViewAnimator.indexOfChild(v2));
    308 
    309         mViewAnimator.removeAllViews();
    310         assertEquals(0, mViewAnimator.getChildCount());
    311     }
    312 
    313     @Test
    314     public void testAccessOutAnimation() {
    315         AnimationSet expected = new AnimationSet(mActivity, mAttributeSet);
    316         assertNull(mViewAnimator.getOutAnimation());
    317 
    318         mViewAnimator.setOutAnimation(expected);
    319         assertSame(expected, mViewAnimator.getOutAnimation());
    320 
    321         mViewAnimator.setOutAnimation(null);
    322         assertNull(mViewAnimator.getOutAnimation());
    323 
    324         mViewAnimator.setOutAnimation(mActivity, R.anim.anim_alpha);
    325         Animation animation = mViewAnimator.getOutAnimation();
    326         assertTrue(animation.getInterpolator() instanceof AccelerateInterpolator);
    327         assertEquals(500, animation.getDuration());
    328     }
    329 
    330     @UiThreadTest
    331     @Test
    332     public void testRemoveViews() {
    333         final View v1 = mActivity.findViewById(R.id.ok);
    334         final View v2 = mActivity.findViewById(R.id.cancel);
    335         final View v3 = mActivity.findViewById(R.id.label);
    336         final View v4 = mActivity.findViewById(R.id.entry);
    337         final RelativeLayout parent = (RelativeLayout) v1.getParent();
    338 
    339         parent.removeView(v1);
    340         parent.removeView(v2);
    341         parent.removeView(v3);
    342         parent.removeView(v4);
    343         assertEquals(0, mViewAnimator.getChildCount());
    344 
    345         mViewAnimator.addView(v1);
    346         mViewAnimator.addView(v2);
    347         mViewAnimator.addView(v3);
    348         mViewAnimator.addView(v4);
    349         assertEquals(4, mViewAnimator.getChildCount());
    350 
    351         mViewAnimator.removeViewAt(3);
    352         assertEquals(3, mViewAnimator.getChildCount());
    353         assertSame(v1, mViewAnimator.getChildAt(0));
    354         assertSame(v2, mViewAnimator.getChildAt(1));
    355         assertSame(v3, mViewAnimator.getChildAt(2));
    356 
    357         mViewAnimator.removeView(v3);
    358         assertEquals(2, mViewAnimator.getChildCount());
    359         assertSame(v1, mViewAnimator.getChildAt(0));
    360         assertSame(v2, mViewAnimator.getChildAt(1));
    361 
    362         mViewAnimator.removeViewInLayout(v2);
    363         assertEquals(1, mViewAnimator.getChildCount());
    364         assertSame(v1, mViewAnimator.getChildAt(0));
    365 
    366         mViewAnimator.addView(v2);
    367         mViewAnimator.addView(v3);
    368         mViewAnimator.addView(v4);
    369         assertEquals(4, mViewAnimator.getChildCount());
    370 
    371         mViewAnimator.removeViews(0, 2);
    372         assertEquals(2, mViewAnimator.getChildCount());
    373 
    374         // the remain views should be v3, v4
    375         assertSame(v3, mViewAnimator.getChildAt(0));
    376         assertSame(v4, mViewAnimator.getChildAt(1));
    377 
    378         mViewAnimator.removeViewsInLayout(1, 1);
    379         assertEquals(1, mViewAnimator.getChildCount());
    380 
    381         // the remain view should be v3
    382         assertSame(v3, mViewAnimator.getChildAt(0));
    383 
    384         mViewAnimator.removeAllViews();
    385         assertEquals(0, mViewAnimator.getChildCount());
    386     }
    387 }
    388