Home | History | Annotate | Download | only in view
      1 /*
      2  * Copyright (C) 2007 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;
     18 
     19 import com.android.frameworks.coretests.R;
     20 import android.view.ViewGroupChildren;
     21 
     22 import android.test.ActivityInstrumentationTestCase;
     23 import android.test.suitebuilder.annotation.MediumTest;
     24 import android.test.ViewAsserts;
     25 import android.test.UiThreadTest;
     26 import android.view.View;
     27 import android.view.ViewGroup;
     28 import android.widget.LinearLayout;
     29 import android.widget.TextView;
     30 
     31 /**
     32  * Exercises {@link android.view.ViewGroup}'s ability to add/remove children.
     33  */
     34 public class ViewGroupChildrenTest extends ActivityInstrumentationTestCase<ViewGroupChildren> {
     35     private ViewGroup mGroup;
     36 
     37     public ViewGroupChildrenTest() {
     38         super("com.android.frameworks.coretests", ViewGroupChildren.class);
     39     }
     40 
     41     @Override
     42     public void setUp() throws Exception {
     43         super.setUp();
     44 
     45         final ViewGroupChildren a = getActivity();
     46         mGroup = (ViewGroup) a.findViewById(R.id.group);
     47     }
     48 
     49     @MediumTest
     50     public void testSetUpConditions() throws Exception {
     51         assertNotNull(mGroup);
     52     }
     53 
     54     @MediumTest
     55     public void testStartsEmpty() throws Exception {
     56         assertEquals("A ViewGroup should have no child by default", 0, mGroup.getChildCount());
     57     }
     58 
     59     @UiThreadTest
     60     @MediumTest
     61     public void testAddChild() throws Exception {
     62         View view = createView("1");
     63         mGroup.addView(view);
     64 
     65         assertEquals(1, mGroup.getChildCount());
     66         ViewAsserts.assertGroupIntegrity(mGroup);
     67         ViewAsserts.assertGroupContains(mGroup, view);
     68     }
     69 
     70     @UiThreadTest
     71     @MediumTest
     72     public void testAddChildAtFront() throws Exception {
     73         // 24 should be greater than ViewGroup.ARRAY_CAPACITY_INCREMENT
     74         for (int i = 0; i < 24; i++) {
     75             View view = createView(String.valueOf(i + 1));
     76             mGroup.addView(view);
     77         }
     78 
     79         View view = createView("X");
     80         mGroup.addView(view, 0);
     81 
     82         assertEquals(25, mGroup.getChildCount());
     83         ViewAsserts.assertGroupIntegrity(mGroup);
     84         ViewAsserts.assertGroupContains(mGroup, view);
     85         assertSame(view, mGroup.getChildAt(0));
     86     }
     87 
     88     @UiThreadTest
     89     @MediumTest
     90     public void testAddChildInMiddle() throws Exception {
     91         // 24 should be greater than ViewGroup.ARRAY_CAPACITY_INCREMENT
     92         for (int i = 0; i < 24; i++) {
     93             View view = createView(String.valueOf(i + 1));
     94             mGroup.addView(view);
     95         }
     96 
     97         View view = createView("X");
     98         mGroup.addView(view, 12);
     99 
    100         assertEquals(25, mGroup.getChildCount());
    101         ViewAsserts.assertGroupIntegrity(mGroup);
    102         ViewAsserts.assertGroupContains(mGroup, view);
    103         assertSame(view, mGroup.getChildAt(12));
    104     }
    105 
    106     @UiThreadTest
    107     @MediumTest
    108     public void testAddChildren() throws Exception {
    109         // 24 should be greater than ViewGroup.ARRAY_CAPACITY_INCREMENT
    110         for (int i = 0; i < 24; i++) {
    111             View view = createView(String.valueOf(i + 1));
    112             mGroup.addView(view);
    113 
    114             ViewAsserts.assertGroupContains(mGroup, view);
    115         }
    116         assertEquals(24, mGroup.getChildCount());
    117     }
    118 
    119     @UiThreadTest
    120     @MediumTest
    121     public void testRemoveChild() throws Exception {
    122         View view = createView("1");
    123         mGroup.addView(view);
    124 
    125         ViewAsserts.assertGroupIntegrity(mGroup);
    126 
    127         mGroup.removeView(view);
    128 
    129         ViewAsserts.assertGroupIntegrity(mGroup);
    130         ViewAsserts.assertGroupNotContains(mGroup, view);
    131 
    132         assertEquals(0, mGroup.getChildCount());
    133         assertNull(view.getParent());
    134     }
    135 
    136     @UiThreadTest
    137     @MediumTest
    138     public void testRemoveChildren() throws Exception {
    139         // 24 should be greater than ViewGroup.ARRAY_CAPACITY_INCREMENT
    140         final View[] views = new View[24];
    141 
    142         for (int i = 0; i < views.length; i++) {
    143             views[i] = createView(String.valueOf(i + 1));
    144             mGroup.addView(views[i]);
    145         }
    146 
    147         for (int i = views.length - 1; i >= 0; i--) {
    148             mGroup.removeViewAt(i);
    149             ViewAsserts.assertGroupIntegrity(mGroup);
    150             ViewAsserts.assertGroupNotContains(mGroup, views[i]);
    151             assertNull(views[i].getParent());
    152         }
    153 
    154         assertEquals(0, mGroup.getChildCount());
    155     }
    156 
    157     @UiThreadTest
    158     @MediumTest
    159     public void testRemoveChildrenBulk() throws Exception {
    160         // 24 should be greater than ViewGroup.ARRAY_CAPACITY_INCREMENT
    161         final View[] views = new View[24];
    162 
    163         for (int i = 0; i < views.length; i++) {
    164             views[i] = createView(String.valueOf(i + 1));
    165             mGroup.addView(views[i]);
    166         }
    167 
    168         mGroup.removeViews(6, 7);
    169         ViewAsserts.assertGroupIntegrity(mGroup);
    170         for (int i = 6; i < 13; i++) {
    171             ViewAsserts.assertGroupNotContains(mGroup, views[i]);
    172             assertNull(views[i].getParent());
    173         }
    174 
    175         assertEquals(17, mGroup.getChildCount());
    176     }
    177 
    178     @UiThreadTest
    179     @MediumTest
    180     public void testRemoveChildrenBulkAtFront() throws Exception {
    181         // 24 should be greater than ViewGroup.ARRAY_CAPACITY_INCREMENT
    182         final View[] views = new View[24];
    183 
    184         for (int i = 0; i < views.length; i++) {
    185             views[i] = createView(String.valueOf(i + 1));
    186             mGroup.addView(views[i]);
    187         }
    188 
    189         mGroup.removeViews(0, 7);
    190         ViewAsserts.assertGroupIntegrity(mGroup);
    191         for (int i = 0; i < 7; i++) {
    192             ViewAsserts.assertGroupNotContains(mGroup, views[i]);
    193             assertNull(views[i].getParent());
    194         }
    195 
    196         assertEquals("8", ((TextView) mGroup.getChildAt(0)).getText());
    197         assertEquals(17, mGroup.getChildCount());
    198     }
    199 
    200     @UiThreadTest
    201     @MediumTest
    202     public void testRemoveChildrenBulkAtEnd() throws Exception {
    203         // 24 should be greater than ViewGroup.ARRAY_CAPACITY_INCREMENT
    204         final View[] views = new View[24];
    205 
    206         for (int i = 0; i < views.length; i++) {
    207             views[i] = createView(String.valueOf(i + 1));
    208             mGroup.addView(views[i]);
    209         }
    210 
    211         mGroup.removeViews(17, 7);
    212         ViewAsserts.assertGroupIntegrity(mGroup);
    213         for (int i = 17; i < 24; i++) {
    214             ViewAsserts.assertGroupNotContains(mGroup, views[i]);
    215             assertNull(views[i].getParent());
    216         }
    217         assertEquals("17", ((TextView) mGroup.getChildAt(mGroup.getChildCount() - 1)).getText());
    218         assertEquals(17, mGroup.getChildCount());
    219     }
    220 
    221     @UiThreadTest
    222     @MediumTest
    223     public void testRemoveChildAtFront() throws Exception {
    224         // 24 should be greater than ViewGroup.ARRAY_CAPACITY_INCREMENT
    225         final View[] views = new View[24];
    226 
    227         for (int i = 0; i < views.length; i++) {
    228             views[i] = createView(String.valueOf(i + 1));
    229             mGroup.addView(views[i]);
    230         }
    231 
    232         mGroup.removeViewAt(0);
    233         ViewAsserts.assertGroupIntegrity(mGroup);
    234         ViewAsserts.assertGroupNotContains(mGroup, views[0]);
    235         assertNull(views[0].getParent());
    236 
    237         assertEquals(views.length - 1, mGroup.getChildCount());
    238     }
    239 
    240     @UiThreadTest
    241     @MediumTest
    242     public void testRemoveChildInMiddle() throws Exception {
    243         // 24 should be greater than ViewGroup.ARRAY_CAPACITY_INCREMENT
    244         final View[] views = new View[24];
    245 
    246         for (int i = 0; i < views.length; i++) {
    247             views[i] = createView(String.valueOf(i + 1));
    248             mGroup.addView(views[i]);
    249         }
    250 
    251         mGroup.removeViewAt(12);
    252         ViewAsserts.assertGroupIntegrity(mGroup);
    253         ViewAsserts.assertGroupNotContains(mGroup, views[12]);
    254         assertNull(views[12].getParent());
    255 
    256         assertEquals(views.length - 1, mGroup.getChildCount());
    257     }
    258 
    259     private TextView createView(String text) {
    260         TextView view = new TextView(getActivity());
    261         view.setText(text);
    262         view.setLayoutParams(new LinearLayout.LayoutParams(
    263                 LinearLayout.LayoutParams.MATCH_PARENT,
    264                 LinearLayout.LayoutParams.WRAP_CONTENT
    265         ));
    266         return view;
    267     }
    268 }
    269