2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import com.android.frameworks.coretests.R;
20 import android.view.ViewGroupChildren;
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;
32 * Exercises {@link android.view.ViewGroup}'s ability to add/remove children.
34 public class ViewGroupChildrenTest extends ActivityInstrumentationTestCase<ViewGroupChildren> {
35 private ViewGroup mGroup;
37 public ViewGroupChildrenTest() {
38 super("com.android.frameworks.coretests", ViewGroupChildren.class);
42 public void setUp() throws Exception {
45 final ViewGroupChildren a = getActivity();
46 mGroup = (ViewGroup) a.findViewById(R.id.group);
50 public void testSetUpConditions() throws Exception {
51 assertNotNull(mGroup);
55 public void testStartsEmpty() throws Exception {
56 assertEquals("A ViewGroup should have no child by default", 0, mGroup.getChildCount());
61 public void testAddChild() throws Exception {
62 View view = createView("1");
65 assertEquals(1, mGroup.getChildCount());
66 ViewAsserts.assertGroupIntegrity(mGroup);
67 ViewAsserts.assertGroupContains(mGroup, view);
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));
79 View view = createView("X");
80 mGroup.addView(view, 0);
82 assertEquals(25, mGroup.getChildCount());
83 ViewAsserts.assertGroupIntegrity(mGroup);
84 ViewAsserts.assertGroupContains(mGroup, view);
85 assertSame(view, mGroup.getChildAt(0));
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));
97 View view = createView("X");
98 mGroup.addView(view, 12);
100 assertEquals(25, mGroup.getChildCount());
101 ViewAsserts.assertGroupIntegrity(mGroup);
102 ViewAsserts.assertGroupContains(mGroup, view);
103 assertSame(view, mGroup.getChildAt(12));
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);
114 ViewAsserts.assertGroupContains(mGroup, view);
116 assertEquals(24, mGroup.getChildCount());
121 public void testRemoveChild() throws Exception {
122 View view = createView("1");
123 mGroup.addView(view);
125 ViewAsserts.assertGroupIntegrity(mGroup);
127 mGroup.removeView(view);
129 ViewAsserts.assertGroupIntegrity(mGroup);
130 ViewAsserts.assertGroupNotContains(mGroup, view);
132 assertEquals(0, mGroup.getChildCount());
133 assertNull(view.getParent());
138 public void testRemoveChildren() throws Exception {
139 // 24 should be greater than ViewGroup.ARRAY_CAPACITY_INCREMENT
140 final View[] views = new View[24];
142 for (int i = 0; i < views.length; i++) {
143 views[i] = createView(String.valueOf(i + 1));
144 mGroup.addView(views[i]);
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());
154 assertEquals(0, mGroup.getChildCount());
159 public void testRemoveChildrenBulk() throws Exception {
160 // 24 should be greater than ViewGroup.ARRAY_CAPACITY_INCREMENT
161 final View[] views = new View[24];
163 for (int i = 0; i < views.length; i++) {
164 views[i] = createView(String.valueOf(i + 1));
165 mGroup.addView(views[i]);
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());
175 assertEquals(17, mGroup.getChildCount());
180 public void testRemoveChildrenBulkAtFront() throws Exception {
181 // 24 should be greater than ViewGroup.ARRAY_CAPACITY_INCREMENT
182 final View[] views = new View[24];
184 for (int i = 0; i < views.length; i++) {
185 views[i] = createView(String.valueOf(i + 1));
186 mGroup.addView(views[i]);
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());
196 assertEquals("8", ((TextView) mGroup.getChildAt(0)).getText());
197 assertEquals(17, mGroup.getChildCount());
202 public void testRemoveChildrenBulkAtEnd() throws Exception {
203 // 24 should be greater than ViewGroup.ARRAY_CAPACITY_INCREMENT
204 final View[] views = new View[24];
206 for (int i = 0; i < views.length; i++) {
207 views[i] = createView(String.valueOf(i + 1));
208 mGroup.addView(views[i]);
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());
217 assertEquals("17", ((TextView) mGroup.getChildAt(mGroup.getChildCount() - 1)).getText());
218 assertEquals(17, mGroup.getChildCount());
223 public void testRemoveChildAtFront() throws Exception {
224 // 24 should be greater than ViewGroup.ARRAY_CAPACITY_INCREMENT
225 final View[] views = new View[24];
227 for (int i = 0; i < views.length; i++) {
228 views[i] = createView(String.valueOf(i + 1));
229 mGroup.addView(views[i]);
232 mGroup.removeViewAt(0);
233 ViewAsserts.assertGroupIntegrity(mGroup);
234 ViewAsserts.assertGroupNotContains(mGroup, views[0]);
235 assertNull(views[0].getParent());
237 assertEquals(views.length - 1, mGroup.getChildCount());
242 public void testRemoveChildInMiddle() throws Exception {
243 // 24 should be greater than ViewGroup.ARRAY_CAPACITY_INCREMENT
244 final View[] views = new View[24];
246 for (int i = 0; i < views.length; i++) {
247 views[i] = createView(String.valueOf(i + 1));
248 mGroup.addView(views[i]);
251 mGroup.removeViewAt(12);
252 ViewAsserts.assertGroupIntegrity(mGroup);
253 ViewAsserts.assertGroupNotContains(mGroup, views[12]);
254 assertNull(views[12].getParent());
256 assertEquals(views.length - 1, mGroup.getChildCount());
259 private TextView createView(String text) {
260 TextView view = new TextView(getActivity());
262 view.setLayoutParams(new LinearLayout.LayoutParams(
263 LinearLayout.LayoutParams.MATCH_PARENT,
264 LinearLayout.LayoutParams.WRAP_CONTENT