• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 org.xmlpull.v1.XmlPullParser;
20 
21 import android.app.Activity;
22 import android.content.Context;
23 import android.test.ActivityInstrumentationTestCase;
24 import android.test.ViewAsserts;
25 import android.util.AttributeSet;
26 import android.util.Xml;
27 import android.view.ViewGroup;
28 import android.widget.LinearLayout.LayoutParams;
29 import android.widget.AbsoluteLayout;
30 import android.widget.LinearLayout;
31 import android.widget.ListView;
32 import android.widget.TextView;
33 
34 import com.android.cts.stub.R;
35 import dalvik.annotation.TestTargets;
36 import dalvik.annotation.TestLevel;
37 import dalvik.annotation.TestTargetNew;
38 import dalvik.annotation.TestTargetClass;
39 import dalvik.annotation.ToBeFixed;
40 
41 /**
42  * Test {@link LinearLayout}.
43  */
44 @TestTargetClass(LinearLayout.class)
45 public class LinearLayoutTest extends ActivityInstrumentationTestCase<LinearLayoutStubActivity> {
46     private Context mContext;
47     private Activity mActivity;
48 
LinearLayoutTest()49     public LinearLayoutTest() {
50         super("com.android.cts.stub", LinearLayoutStubActivity.class);
51     }
52 
53     @Override
setUp()54     protected void setUp() throws Exception {
55         super.setUp();
56         mActivity = getActivity();
57         mContext = getInstrumentation().getTargetContext();
58     }
59 
60     @TestTargets({
61         @TestTargetNew(
62             level = TestLevel.COMPLETE,
63             notes = "Test constructor(s) of {@link LinearLayout}",
64             method = "LinearLayout",
65             args = {android.content.Context.class}
66         ),
67         @TestTargetNew(
68             level = TestLevel.COMPLETE,
69             notes = "Test constructor(s) of {@link LinearLayout}",
70             method = "LinearLayout",
71             args = {android.content.Context.class, android.util.AttributeSet.class}
72         )
73     })
74     @ToBeFixed(bug="1417734", explanation="LinearLayout#LinearLayout(Context, AttributeSet)" +
75             " should check whether the input Context is null")
testConstructor()76     public void testConstructor() {
77         new LinearLayout(mContext);
78 
79         new LinearLayout(mContext, null);
80 
81         XmlPullParser parser = mContext.getResources().getXml(R.layout.linearlayout_layout);
82         AttributeSet attrs = Xml.asAttributeSet(parser);
83         new LinearLayout(mContext, attrs);
84 
85         try {
86             new LinearLayout(null, null);
87             fail("should throw NullPointerException.");
88         } catch (NullPointerException e) {
89         }
90     }
91 
92     @TestTargets({
93         @TestTargetNew(
94             level = TestLevel.COMPLETE,
95             method = "isBaselineAligned",
96             args = {}
97         ),
98         @TestTargetNew(
99             level = TestLevel.COMPLETE,
100             method = "setBaselineAligned",
101             args = {boolean.class}
102         )
103     })
testAccessBaselineAligned()104     public void testAccessBaselineAligned() {
105         LinearLayout linearLayout = new LinearLayout(mContext);
106         linearLayout.setBaselineAligned(true);
107         assertTrue(linearLayout.isBaselineAligned());
108 
109         linearLayout.setBaselineAligned(false);
110         assertFalse(linearLayout.isBaselineAligned());
111 
112         // android:baselineAligned="false" in LinearLayout weightsum
113         linearLayout = (LinearLayout) mActivity.findViewById(R.id.weightsum);
114         assertFalse(linearLayout.isBaselineAligned());
115 
116         // default mBaselineAligned is true.
117         linearLayout = (LinearLayout) mActivity.findViewById(R.id.horizontal);
118         assertTrue(linearLayout.isBaselineAligned());
119 
120         // default mBaselineAligned is true.
121         // Only applicable if {@link #mOrientation} is horizontal
122         linearLayout = (LinearLayout) mActivity.findViewById(R.id.vertical);
123         assertTrue(linearLayout.isBaselineAligned());
124     }
125 
126     @TestTargetNew(
127         level = TestLevel.COMPLETE,
128         notes = "Test {@link LinearLayout#getBaseline()}",
129         method = "getBaseline",
130         args = {}
131     )
testGetBaseline()132     public void testGetBaseline() {
133         LinearLayout linearLayout = new LinearLayout(mContext);
134         try {
135             linearLayout.getBaseline();
136             fail("LinearLayout.getBaseline() should throw exception here.");
137         } catch (RuntimeException e) {
138         }
139         ListView lv1 = new ListView(mContext);
140         linearLayout.addView(lv1);
141         assertEquals(-1, linearLayout.getBaseline());
142 
143         ListView lv2 = new ListView(mContext);
144         linearLayout.addView(lv2);
145         linearLayout.setBaselineAlignedChildIndex(1);
146         try {
147             linearLayout.getBaseline();
148             fail("LinearLayout.getBaseline() should throw exception here.");
149         } catch (RuntimeException e) {
150         }
151 
152         MockListView lv3 = new MockListView(mContext);
153         linearLayout.addView(lv3);
154         linearLayout.setBaselineAlignedChildIndex(2);
155         assertEquals(lv3.getBaseline(), linearLayout.getBaseline());
156     }
157 
158     @TestTargets({
159         @TestTargetNew(
160             level = TestLevel.COMPLETE,
161             method = "getBaselineAlignedChildIndex",
162             args = {}
163         ),
164         @TestTargetNew(
165             level = TestLevel.COMPLETE,
166             method = "setBaselineAlignedChildIndex",
167             args = {int.class}
168         )
169     })
testAccessBaselineAlignedChildIndex()170     public void testAccessBaselineAlignedChildIndex() {
171         LinearLayout linearLayout = new LinearLayout(mContext);
172         // set BaselineAlignedChildIndex
173         ListView lv1 = new ListView(mContext);
174         ListView lv2 = new ListView(mContext);
175         ListView lv3 = new ListView(mContext);
176         linearLayout.addView(lv1);
177         linearLayout.addView(lv2);
178         linearLayout.addView(lv3);
179         linearLayout.setBaselineAlignedChildIndex(1);
180         assertEquals(1, linearLayout.getBaselineAlignedChildIndex());
181 
182         linearLayout.setBaselineAlignedChildIndex(2);
183         assertEquals(2, linearLayout.getBaselineAlignedChildIndex());
184 
185         try {
186             linearLayout.setBaselineAlignedChildIndex(-1);
187             fail("LinearLayout should throw IllegalArgumentException here.");
188         } catch (IllegalArgumentException e) {
189         }
190         try {
191             linearLayout.setBaselineAlignedChildIndex(3);
192             fail("LinearLayout should throw IllegalArgumentException here.");
193         } catch (IllegalArgumentException e) {
194         }
195 
196         linearLayout = (LinearLayout) mActivity.findViewById(R.id.baseline_aligned_child_index);
197         assertEquals(1, linearLayout.getBaselineAlignedChildIndex());
198     }
199 
200     @TestTargets({
201         @TestTargetNew(
202             level = TestLevel.COMPLETE,
203             method = "setWeightSum",
204             args = {float.class}
205         ),
206         @TestTargetNew(
207             level = TestLevel.COMPLETE,
208             method = "getWeightSum",
209             args = {}
210         )
211     })
212     /**
213      * weightsum is a horizontal LinearLayout. There are three children in it.
214      */
testAccessWeightSum()215     public void testAccessWeightSum() {
216         LinearLayout parent = (LinearLayout) mActivity.findViewById(R.id.weightsum);
217         TextView weight02 = (TextView) mActivity.findViewById(R.id.weight_0_2);
218         TextView weight05 = (TextView) mActivity.findViewById(R.id.weight_0_5);
219         TextView weight03 = (TextView) mActivity.findViewById(R.id.weight_0_3);
220 
221         assertNotNull(parent);
222         assertNotNull(weight02);
223         assertNotNull(weight05);
224         assertNotNull(weight03);
225 
226         assertEquals(mContext.getResources().getString(R.string.horizontal_text_1),
227                 weight02.getText().toString());
228         assertEquals(mContext.getResources().getString(R.string.horizontal_text_2),
229                 weight05.getText().toString());
230         assertEquals(mContext.getResources().getString(R.string.horizontal_text_3),
231                 weight03.getText().toString());
232 
233         assertEquals(LinearLayout.HORIZONTAL, parent.getOrientation());
234         assertEquals(1.0f, parent.getWeightSum());
235 
236         int parentWidth = parent.getWidth();
237         assertEquals(parentWidth * 0.2, (float) weight02.getWidth(), 1.0);
238         assertEquals(parentWidth * 0.5, (float) weight05.getWidth(), 1.0);
239         assertEquals(parentWidth * 0.3, (float) weight03.getWidth(), 1.0);
240     }
241 
242     @TestTargets({
243         @TestTargetNew(
244             level = TestLevel.COMPLETE,
245             method = "generateLayoutParams",
246             args = {android.util.AttributeSet.class}
247         ),
248         @TestTargetNew(
249             level = TestLevel.COMPLETE,
250             method = "generateLayoutParams",
251             args = {android.view.ViewGroup.LayoutParams.class}
252         )
253     })
254     @ToBeFixed(bug="1417734", explanation="generateLayoutParams(AttributeSet)" +
255             " will throw a RuntimeException:" +
256             " Binary XML file line #-1: You must supply a layout_width attribute." +
257             " But 'layout_width' attribute have been assigned to be 'fill_parent'.")
testGenerateLayoutParams()258     public void testGenerateLayoutParams() {
259         ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(320, 240);
260         MockLinearLayout mockLinearLayout = new MockLinearLayout(mContext);
261         LayoutParams layoutParams1 = mockLinearLayout.generateLayoutParams(lp);
262         assertEquals(320, layoutParams1.width);
263         assertEquals(240, layoutParams1.height);
264 
265         // generateLayoutParams() always throw  a RuntimeException.
266 //        XmlPullParser parser = mContext.getResources().getXml(R.layout.linearlayout_layout);
267 //        AttributeSet attrs = Xml.asAttributeSet(parser);
268 //        LinearLayout linearLayout = new LinearLayout(mContext, attrs);
269 //        LayoutParams layoutParams2 = linearLayout.generateLayoutParams(attrs);
270 //        assertEquals(LayoutParams.FILL_PARENT, layoutParams2.width);
271 //        assertEquals(LayoutParams.WRAP_CONTENT, layoutParams2.height);
272     }
273 
274     @TestTargetNew(
275         level = TestLevel.COMPLETE,
276         notes = "Test {@link LinearLayout#checkLayoutParams(ViewGroup.LayoutParams)}",
277         method = "checkLayoutParams",
278         args = {android.view.ViewGroup.LayoutParams.class}
279     )
testCheckLayoutParams()280     public void testCheckLayoutParams() {
281         MockLinearLayout mockLinearLayout = new MockLinearLayout(mContext);
282 
283         ViewGroup.LayoutParams params = new AbsoluteLayout.LayoutParams(240, 320, 0, 0);
284         assertFalse(mockLinearLayout.checkLayoutParams(params));
285 
286         params = new LinearLayout.LayoutParams(240, 320);
287         assertTrue(mockLinearLayout.checkLayoutParams(params));
288     }
289 
290     @TestTargetNew(
291         level = TestLevel.COMPLETE,
292         notes = "Test {@link LinearLayout#generateDefaultLayoutParams()}",
293         method = "generateDefaultLayoutParams",
294         args = {}
295     )
testGenerateDefaultLayoutParams()296     public void testGenerateDefaultLayoutParams() {
297         MockLinearLayout mockLinearLayout = new MockLinearLayout(mContext);
298 
299         mockLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
300         ViewGroup.LayoutParams param = mockLinearLayout.generateDefaultLayoutParams();
301         assertNotNull(param);
302         assertTrue(param instanceof LinearLayout.LayoutParams);
303         assertEquals(ViewGroup.LayoutParams.WRAP_CONTENT, param.width);
304         assertEquals(ViewGroup.LayoutParams.WRAP_CONTENT, param.height);
305 
306         mockLinearLayout.setOrientation(LinearLayout.VERTICAL);
307         param = mockLinearLayout.generateDefaultLayoutParams();
308         assertNotNull(param);
309         assertTrue(param instanceof LinearLayout.LayoutParams);
310         assertEquals(ViewGroup.LayoutParams.FILL_PARENT, param.width);
311         assertEquals(ViewGroup.LayoutParams.WRAP_CONTENT, param.height);
312 
313         mockLinearLayout.setOrientation(-1);
314         assertNull(mockLinearLayout.generateDefaultLayoutParams());
315     }
316 
317     @TestTargets({
318         @TestTargetNew(
319             level = TestLevel.COMPLETE,
320             notes = "test layout three horizontal children",
321             method = "setGravity",
322             args = {int.class}
323         ),
324         @TestTargetNew(
325             level = TestLevel.COMPLETE,
326             notes = "test layout three horizontal children",
327             method = "setVerticalGravity",
328             args = {int.class}
329         ),
330         @TestTargetNew(
331             level = TestLevel.COMPLETE,
332             notes = "test layout three horizontal children",
333             method = "setOrientation",
334             args = {int.class}
335         ),
336         @TestTargetNew(
337             level = TestLevel.COMPLETE,
338             notes = "test layout three horizontal children",
339             method = "getOrientation",
340             args = {}
341         ),
342         @TestTargetNew(
343             level = TestLevel.COMPLETE,
344             notes = "test layout three horizontal children",
345             method = "onMeasure",
346             args = {int.class, int.class}
347         ),
348         @TestTargetNew(
349             level = TestLevel.COMPLETE,
350             notes = "test layout three horizontal children",
351             method = "onLayout",
352             args = {boolean.class, int.class, int.class, int.class, int.class}
353         )
354     })
355     /**
356      * layout of horizontal LinearLayout.
357      * ----------------------------------------------------
358      * | ------------ |                 |                 |
359      * | | top view | | --------------- |                 |
360      * | |          | | | center view | | --------------- |
361      * | ------------ | |             | | | bottom view | |
362      * |              | --------------- | |             | |
363      * |     parent   |                 | --------------- |
364      * ----------------------------------------------------
365      */
testLayoutHorizontal()366     public void testLayoutHorizontal() {
367         LinearLayout parent = (LinearLayout) mActivity.findViewById(R.id.horizontal);
368         TextView topView = (TextView) mActivity.findViewById(R.id.gravity_top);
369         TextView centerView = (TextView) mActivity.findViewById(R.id.gravity_center_vertical);
370         TextView bottomView = (TextView) mActivity.findViewById(R.id.gravity_bottom);
371 
372         assertNotNull(parent);
373         assertNotNull(topView);
374         assertNotNull(centerView);
375         assertNotNull(bottomView);
376 
377         assertEquals(mContext.getResources().getString(R.string.horizontal_text_1),
378                 topView.getText().toString());
379         assertEquals(mContext.getResources().getString(R.string.horizontal_text_2),
380                 centerView.getText().toString());
381         assertEquals(mContext.getResources().getString(R.string.horizontal_text_3),
382                 bottomView.getText().toString());
383 
384         assertEquals(LinearLayout.HORIZONTAL, parent.getOrientation());
385 
386         ViewAsserts.assertTopAligned(parent, topView);
387         ViewAsserts.assertVerticalCenterAligned(parent, centerView);
388         ViewAsserts.assertBottomAligned(parent, bottomView);
389 
390         assertEquals(0, topView.getTop());
391         assertEquals(topView.getHeight(), topView.getBottom());
392         assertEquals(0, topView.getLeft());
393         assertEquals(centerView.getLeft(), topView.getRight());
394 
395         int offset = (parent.getHeight() - centerView.getHeight()) / 2;
396         assertEquals(offset, centerView.getTop());
397         assertEquals(offset + centerView.getHeight(), centerView.getBottom());
398         assertEquals(topView.getRight(), centerView.getLeft());
399         assertEquals(bottomView.getLeft(), centerView.getRight());
400 
401         assertEquals(parent.getHeight() - bottomView.getHeight(), bottomView.getTop());
402         assertEquals(parent.getHeight(), bottomView.getBottom());
403         assertEquals(centerView.getRight(), bottomView.getLeft());
404         assertEquals(parent.getWidth(), bottomView.getRight());
405     }
406 
407     @TestTargets({
408         @TestTargetNew(
409             level = TestLevel.COMPLETE,
410             notes = "test layout three vertical children",
411             method = "setGravity",
412             args = {int.class}
413         ),
414         @TestTargetNew(
415             level = TestLevel.COMPLETE,
416             notes = "test layout three vertical children",
417             method = "setHorizontalGravity",
418             args = {int.class}
419         ),
420         @TestTargetNew(
421             level = TestLevel.COMPLETE,
422             notes = "test layout three vertical children",
423             method = "setOrientation",
424             args = {int.class}
425         ),
426         @TestTargetNew(
427             level = TestLevel.COMPLETE,
428             notes = "test layout three vertical children",
429             method = "getOrientation",
430             args = {}
431         ),
432         @TestTargetNew(
433             level = TestLevel.COMPLETE,
434             notes = "test layout three vertical children",
435             method = "onMeasure",
436             args = {int.class, int.class}
437         ),
438         @TestTargetNew(
439             level = TestLevel.COMPLETE,
440             notes = "test layout three vertical children",
441             method = "onLayout",
442             args = {boolean.class, int.class, int.class, int.class, int.class}
443         )
444     })
445     /**
446      * layout of vertical LinearLayout.
447      * -----------------------------------
448      * | -------------                   |
449      * | | left view |                   |
450      * | -------------                   |
451      * | - - - - - - - - - - - - - - - - |
452      * |        ---------------          |
453      * |        | center view |          |
454      * |        ---------------          |
455      * | - - - - - - - - - - - - - - - - |
456      * |                  -------------- |
457      * | parent           | right view | |
458      * |                  -------------- |
459      * -----------------------------------
460      */
testLayoutVertical()461     public void testLayoutVertical() {
462         LinearLayout parent = (LinearLayout) mActivity.findViewById(R.id.vertical);
463         TextView leftView = (TextView) mActivity.findViewById(R.id.gravity_left);
464         TextView centerView = (TextView) mActivity.findViewById(R.id.gravity_center_horizontal);
465         TextView rightView = (TextView) mActivity.findViewById(R.id.gravity_right);
466 
467         assertNotNull(parent);
468         assertNotNull(leftView);
469         assertNotNull(centerView);
470         assertNotNull(rightView);
471 
472         assertEquals(mContext.getResources().getString(R.string.vertical_text_1),
473                 leftView.getText().toString());
474         assertEquals(mContext.getResources().getString(R.string.vertical_text_2),
475                 centerView.getText().toString());
476         assertEquals(mContext.getResources().getString(R.string.vertical_text_3),
477                 rightView.getText().toString());
478 
479         assertEquals(LinearLayout.VERTICAL, parent.getOrientation());
480 
481         ViewAsserts.assertLeftAligned(parent, leftView);
482         ViewAsserts.assertHorizontalCenterAligned(parent, centerView);
483         ViewAsserts.assertRightAligned(parent, rightView);
484 
485         assertEquals(0, leftView.getTop());
486         assertEquals(centerView.getTop(), leftView.getBottom());
487         assertEquals(0, leftView.getLeft());
488         assertEquals(leftView.getWidth(), leftView.getRight());
489 
490         int offset = (parent.getWidth() - centerView.getWidth()) / 2;
491         assertEquals(leftView.getBottom(), centerView.getTop());
492         assertEquals(rightView.getTop(), centerView.getBottom());
493         assertEquals(offset, centerView.getLeft());
494         assertEquals(offset + centerView.getWidth(), centerView.getRight());
495 
496         assertEquals(centerView.getBottom(), rightView.getTop());
497         assertEquals(parent.getHeight(), rightView.getBottom());
498         assertEquals(parent.getWidth() - rightView.getWidth(), rightView.getLeft());
499         assertEquals(parent.getWidth(), rightView.getRight());
500     }
501 
502     private class MockListView extends ListView {
503         private final static int DEFAULT_CHILD_BASE_LINE = 1;
504 
MockListView(Context context)505         public MockListView(Context context) {
506             super(context);
507         }
508 
getBaseline()509         public int getBaseline() {
510             return DEFAULT_CHILD_BASE_LINE;
511         }
512     }
513 
514     /**
515      * Add MockLinearLayout to help for testing protected methods in LinearLayout.
516      * Because we can not access protected methods in LinearLayout directly, we have to
517      * extends from it and override protected methods so that we can access them in
518      * our test codes.
519      */
520     private class MockLinearLayout extends LinearLayout {
MockLinearLayout(Context c)521         public MockLinearLayout(Context c) {
522             super(c);
523         }
524 
525         @Override
checkLayoutParams(ViewGroup.LayoutParams p)526         protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
527             return super.checkLayoutParams(p);
528         }
529 
530         @Override
generateDefaultLayoutParams()531         protected LinearLayout.LayoutParams generateDefaultLayoutParams() {
532             return super.generateDefaultLayoutParams();
533         }
534 
535         @Override
generateLayoutParams(ViewGroup.LayoutParams p)536         protected LinearLayout.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
537             return super.generateLayoutParams(p);
538         }
539     }
540 }
541