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.view.cts;
     18 
     19 import com.android.cts.stub.R;
     20 
     21 import dalvik.annotation.KnownFailure;
     22 import dalvik.annotation.TestLevel;
     23 import dalvik.annotation.TestTargetClass;
     24 import dalvik.annotation.TestTargetNew;
     25 import dalvik.annotation.TestTargets;
     26 import dalvik.annotation.ToBeFixed;
     27 
     28 import org.xmlpull.v1.XmlPullParser;
     29 
     30 import android.app.Activity;
     31 import android.content.Context;
     32 import android.test.ActivityInstrumentationTestCase;
     33 import android.test.UiThreadTest;
     34 import android.util.AttributeSet;
     35 import android.util.Xml;
     36 import android.view.View;
     37 import android.view.ViewParent;
     38 import android.view.ViewStub;
     39 import android.view.ViewStub.OnInflateListener;
     40 import android.widget.LinearLayout;
     41 
     42 /**
     43  * Test {@link ViewStub}.
     44  */
     45 @TestTargetClass(ViewStub.class)
     46 public class ViewStubTest extends ActivityInstrumentationTestCase<ViewStubStubActivity> {
     47     private Context mContext;
     48     private Activity mActivity;
     49 
     50     public ViewStubTest() {
     51         super("com.android.cts.stub", ViewStubStubActivity.class);
     52     }
     53 
     54     @Override
     55     protected void setUp() throws Exception {
     56         super.setUp();
     57         mContext = getInstrumentation().getContext();
     58         mActivity = getActivity();
     59     }
     60 
     61     @TestTargets({
     62         @TestTargetNew(
     63             level = TestLevel.COMPLETE,
     64             notes = "Test constructor(s) of {@link ViewStub}",
     65             method = "ViewStub",
     66             args = {android.content.Context.class}
     67         ),
     68         @TestTargetNew(
     69             level = TestLevel.COMPLETE,
     70             notes = "Test constructor(s) of {@link ViewStub}",
     71             method = "ViewStub",
     72             args = {android.content.Context.class, android.util.AttributeSet.class}
     73         ),
     74         @TestTargetNew(
     75             level = TestLevel.COMPLETE,
     76             notes = "Test constructor(s) of {@link ViewStub}",
     77             method = "ViewStub",
     78             args = {android.content.Context.class, android.util.AttributeSet.class, int.class}
     79         ),
     80         @TestTargetNew(
     81             level = TestLevel.COMPLETE,
     82             notes = "Test constructor(s) of {@link ViewStub}",
     83             method = "ViewStub",
     84             args = {android.content.Context.class, int.class}
     85         )
     86     })
     87     public void testConstructor() {
     88         XmlPullParser parser = mActivity.getResources().getXml(R.layout.viewstub_layout);
     89         AttributeSet attrs = Xml.asAttributeSet(parser);
     90         assertNotNull(attrs);
     91 
     92         new ViewStub(mContext);
     93 
     94         new ViewStub(mContext, 10);
     95 
     96         new ViewStub(mContext, attrs);
     97 
     98         new ViewStub(mContext, attrs, 30);
     99     }
    100 
    101     @TestTargetNew(
    102         level = TestLevel.COMPLETE,
    103         notes = "Test {@link ViewStub#draw(Canvas)}",
    104         method = "draw",
    105         args = {android.graphics.Canvas.class}
    106     )
    107     public void testDraw() {
    108         ViewStub viewStub = new ViewStub(mContext);
    109         // if the function draw() does not throw any exception,
    110         // we think it is right, because it's an empty method.
    111         viewStub.draw(null);
    112     }
    113 
    114     @TestTargetNew(
    115         level = TestLevel.COMPLETE,
    116         notes = "Test {@link ViewStub#setVisibility(int)}",
    117         method = "setVisibility",
    118         args = {int.class}
    119     )
    120     @UiThreadTest
    121     public void testSetVisibility() {
    122         final ViewStub viewStub1 = (ViewStub) mActivity.findViewById(R.id.viewstub);
    123         MockOnInflateListener listener = new MockOnInflateListener();
    124         viewStub1.setOnInflateListener(listener);
    125         assertFalse(listener.hasCalledOnInflate());
    126         assertNotNull(viewStub1.getParent());
    127 
    128         // set GONE
    129         viewStub1.setVisibility(View.GONE);
    130         assertEquals(View.GONE, viewStub1.getVisibility());
    131         // does not call inflate
    132         assertFalse(listener.hasCalledOnInflate());
    133         assertNotNull(viewStub1.getParent());
    134 
    135         // set VISIBLE
    136         viewStub1.setVisibility(View.VISIBLE);
    137         assertEquals(View.VISIBLE, viewStub1.getVisibility());
    138         //assure the inflate is called
    139         assertTrue(listener.hasCalledOnInflate());
    140         assertNull(viewStub1.getParent());
    141 
    142         // set INVISIBLE when parent is null
    143         final ViewStub viewStub2 = new ViewStub(mContext);
    144         assertNull(viewStub2.getParent());
    145         try {
    146             viewStub2.setVisibility(View.INVISIBLE);
    147             fail("should throw IllegalStateException");
    148         } catch (IllegalStateException e) {
    149         }
    150         assertEquals(View.INVISIBLE, viewStub2.getVisibility());
    151     }
    152 
    153     @TestTargets({
    154         @TestTargetNew(
    155             level = TestLevel.COMPLETE,
    156             method = "getLayoutResource",
    157             args = {}
    158         ),
    159         @TestTargetNew(
    160             level = TestLevel.COMPLETE,
    161             method = "setLayoutResource",
    162             args = {int.class}
    163         )
    164     })
    165     public void testAccessLayoutResource() {
    166         ViewStub viewStub = new ViewStub(mContext);
    167 
    168         viewStub.setLayoutResource(R.layout.viewstub_layout);
    169         assertEquals(R.layout.viewstub_layout, viewStub.getLayoutResource());
    170 
    171         viewStub.setLayoutResource(0);
    172         assertEquals(0, viewStub.getLayoutResource());
    173 
    174         viewStub.setLayoutResource(-1);
    175         assertEquals(-1, viewStub.getLayoutResource());
    176     }
    177 
    178     @TestTargetNew(
    179         level = TestLevel.COMPLETE,
    180         notes = "Test {@link ViewStub#onMeasure(int, int)}",
    181         method = "onMeasure",
    182         args = {int.class, int.class}
    183     )
    184     public void testViewStubHasNoDimensions() {
    185         ViewStub viewStub = new ViewStub(mContext);
    186 
    187         viewStub.forceLayout();
    188         viewStub.measure(200, 300);
    189         assertEquals(0, viewStub.getMeasuredWidth());
    190         assertEquals(0, viewStub.getMeasuredHeight());
    191 
    192         viewStub.measure(100, 200);
    193         assertEquals(0, viewStub.getMeasuredWidth());
    194         assertEquals(0, viewStub.getMeasuredHeight());
    195     }
    196 
    197     @TestTargetNew(
    198         level = TestLevel.COMPLETE,
    199         notes = "Test {@link ViewStub#setOnInflateListener(OnInflateListener)}",
    200         method = "setOnInflateListener",
    201         args = {android.view.ViewStub.OnInflateListener.class}
    202     )
    203     @UiThreadTest
    204     public void testSetOnInflateListener() {
    205         final ViewStub viewStub = (ViewStub) mActivity.findViewById(R.id.viewstub);
    206         final MockOnInflateListener listener = new MockOnInflateListener();
    207 
    208         viewStub.setOnInflateListener(listener);
    209         assertFalse(listener.hasCalledOnInflate());
    210         viewStub.inflate();
    211         assertTrue(listener.hasCalledOnInflate());
    212     }
    213 
    214     @TestTargetNew(
    215         level = TestLevel.COMPLETE,
    216         method = "setOnInflateListener",
    217         args = {android.view.ViewStub.OnInflateListener.class}
    218     )
    219     @UiThreadTest
    220     public void testSetOnInflateListenerError() {
    221         final ViewStub viewStub = (ViewStub) mActivity.findViewById(R.id.viewstub);
    222 
    223         viewStub.setOnInflateListener(null);
    224         try {
    225             viewStub.inflate();
    226         } catch (NullPointerException e) {
    227             fail("should not throw NullPointerException");
    228         }
    229     }
    230 
    231     @TestTargets({
    232         @TestTargetNew(
    233             level = TestLevel.COMPLETE,
    234             notes = "Test {@link ViewStub#getInflatedId()} and {@link ViewStub#setInflatedId(int)}",
    235             method = "getInflatedId",
    236             args = {}
    237         ),
    238         @TestTargetNew(
    239             level = TestLevel.COMPLETE,
    240             notes = "Test {@link ViewStub#getInflatedId()} and {@link ViewStub#setInflatedId(int)}",
    241             method = "setInflatedId",
    242             args = {int.class}
    243         )
    244     })
    245     public void testAccessInflatedId() {
    246         ViewStub viewStub = new ViewStub(mContext);
    247         assertEquals(0, viewStub.getInflatedId());
    248 
    249         viewStub.setInflatedId(R.id.inflated_id);
    250         assertEquals(R.id.inflated_id, viewStub.getInflatedId());
    251 
    252         viewStub.setInflatedId(-1);
    253         assertEquals(-1, viewStub.getInflatedId());
    254     }
    255 
    256     @TestTargetNew(
    257         level = TestLevel.COMPLETE,
    258         notes = "Test {@link ViewStub#inflate()}",
    259         method = "inflate",
    260         args = {}
    261     )
    262     @UiThreadTest
    263     public void testInflate() {
    264         final ViewStub viewStub = (ViewStub) mActivity.findViewById(R.id.viewstub);
    265         final ViewParent vsParent = viewStub.getParent();
    266         final MockOnInflateListener listener = new MockOnInflateListener();
    267 
    268         viewStub.setOnInflateListener(listener);
    269         assertFalse(listener.hasCalledOnInflate());
    270         assertNotNull(vsParent);
    271 
    272         View view = viewStub.inflate();
    273         assertNotNull(view);
    274         assertTrue(view instanceof LinearLayout);
    275         assertEquals(viewStub.getLayoutParams().width, view.getLayoutParams().width);
    276         assertEquals(viewStub.getLayoutParams().height, view.getLayoutParams().height);
    277         assertNull(viewStub.getParent());
    278         assertSame(vsParent, view.getParent());
    279         assertEquals(R.id.inflated_id, view.getId());
    280         assertTrue(listener.hasCalledOnInflate());
    281     }
    282 
    283     @TestTargetNew(
    284         level = TestLevel.COMPLETE,
    285         notes = "Test abnormal condition of {@link ViewStub#inflate()}",
    286         method = "inflate",
    287         args = {}
    288     )
    289     @ToBeFixed(bug="", explanation="should add javadoc for ViewStub#inflate(): it will" +
    290             " throw IllegalArgumentException when resource is invalid and " +
    291             " throw IllegalStateException when parent is null")
    292     public void testInflateError() {
    293         final ViewStub viewStub = (ViewStub) mActivity.findViewById(R.id.viewstub);
    294 
    295         // mLayoutResource is 0
    296         viewStub.setLayoutResource(0);
    297         try {
    298             viewStub.inflate();
    299             fail("should throw IllegalArgumentException");
    300         } catch (IllegalArgumentException e) {
    301         }
    302 
    303         // parent is null
    304         ViewStub stub = new ViewStub(mContext);
    305         assertNull(stub.getParent());
    306         try {
    307             stub.inflate();
    308             fail("should throw IllegalStateException");
    309         } catch (IllegalStateException e) {
    310         }
    311     }
    312 
    313     private class MockOnInflateListener implements OnInflateListener {
    314         private boolean mCalledOnInflate = false;
    315 
    316         public void onInflate(ViewStub stub, View inflated) {
    317             mCalledOnInflate = true;
    318         }
    319 
    320         public boolean hasCalledOnInflate() {
    321             return mCalledOnInflate;
    322         }
    323 
    324         public void reset() {
    325             mCalledOnInflate = false;
    326         }
    327     }
    328 }
    329