• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 android.content.Context;
20 import android.test.ActivityInstrumentationTestCase;
21 import android.util.AttributeSet;
22 import android.util.Xml;
23 import android.view.Gravity;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.AbsoluteLayout;
27 import android.widget.Button;
28 import android.widget.GridLayout;
29 import android.widget.TextView;
30 import com.android.cts.widget.R;
31 import org.xmlpull.v1.XmlPullParser;
32 
33 import static android.view.ViewGroup.LAYOUT_MODE_OPTICAL_BOUNDS;
34 import static android.widget.GridLayout.spec;
35 
36 /**
37  * Test {@link android.widget.GridLayout}.
38  */
39 public class GridLayoutTest extends ActivityInstrumentationTestCase<GridLayoutCtsActivity> {
40 
41     // The size of the off-screen test container in which we we will testing layout.
42     public static final int MAX_X = 2000;
43     public static final int MAX_Y = 2000;
44 
45     private static abstract class Alignment {
46         String name;
47         int gravity;
48 
getValue(View v)49         abstract int getValue(View v);
50 
Alignment(String name, int gravity)51         protected Alignment(String name, int gravity) {
52             this.name = name;
53             this.gravity = gravity;
54         }
55     }
56 
57     private static final Alignment[] HORIZONTAL_ALIGNMENTS = {
58             new Alignment("LEFT", Gravity.LEFT) {
59                 @Override
60                 int getValue(View v) {
61                     return v.getLeft();
62                 }
63             },
64             new Alignment("CENTER", Gravity.CENTER_HORIZONTAL) {
65                 @Override
66                 int getValue(View v) {
67                     return (v.getLeft() + v.getRight()) / 2;
68                 }
69             },
70             new Alignment("RIGHT", Gravity.RIGHT) {
71                 @Override
72                 int getValue(View v) {
73                     return v.getRight();
74                 }
75             },
76             new Alignment("FILL", Gravity.FILL_HORIZONTAL) {
77                 @Override
78                 int getValue(View v) {
79                     return v.getWidth();
80                 }
81             }
82     };
83 
84     private static final Alignment[] VERTICAL_ALIGNMENTS = {
85             new Alignment("TOP", Gravity.TOP) {
86                 @Override
87                 int getValue(View v) {
88                     return v.getTop();
89                 }
90             },
91             new Alignment("CENTER", Gravity.CENTER_VERTICAL) {
92                 @Override
93                 int getValue(View v) {
94                     return (v.getTop() + v.getBottom()) / 2;
95                 }
96             },
97             new Alignment("BASELINE", Gravity.NO_GRAVITY) {
98                 @Override
99                 int getValue(View v) {
100                     return v.getTop() + v.getBaseline();
101                 }
102             },
103             new Alignment("BOTTOM", Gravity.BOTTOM) {
104                 @Override
105                 int getValue(View v) {
106                     return v.getBottom();
107                 }
108             },
109             new Alignment("FILL", Gravity.FILL_VERTICAL) {
110                 @Override
111                 int getValue(View v) {
112                     return v.getHeight();
113                 }
114             }
115     };
116 
117     private Context mContext;
118 
GridLayoutTest()119     public GridLayoutTest() {
120         super("com.android.cts.widget", GridLayoutCtsActivity.class);
121     }
122 
123     @Override
setUp()124     protected void setUp() throws Exception {
125         super.setUp();
126         mContext = getInstrumentation().getTargetContext();
127     }
128 
testConstructor()129     public void testConstructor() {
130         new GridLayout(mContext);
131 
132         new GridLayout(mContext, null);
133 
134         XmlPullParser parser = mContext.getResources().getXml(R.layout.gridlayout_layout);
135         AttributeSet attrs = Xml.asAttributeSet(parser);
136         new GridLayout(mContext, attrs);
137 
138         try {
139             new GridLayout(null, null);
140             fail("should throw NullPointerException.");
141         } catch (NullPointerException e) {
142         }
143     }
144 
testCheckLayoutParams()145     public void testCheckLayoutParams() {
146         GridLayout gridLayout = new GridLayout(mContext);
147 
148         gridLayout.addView(new TextView(mContext), new AbsoluteLayout.LayoutParams(0, 0, 0, 0));
149 
150         gridLayout.addView(new TextView(mContext), new GridLayout.LayoutParams(
151                 GridLayout.spec(0),
152                 GridLayout.spec(0)));
153 
154     }
155 
testGenerateDefaultLayoutParams()156     public void testGenerateDefaultLayoutParams() {
157         GridLayout gridLayout = new GridLayout(mContext);
158         ViewGroup.LayoutParams lp = gridLayout.generateLayoutParams(null);
159         assertNotNull(lp);
160         assertTrue(lp instanceof GridLayout.LayoutParams);
161         assertEquals(ViewGroup.LayoutParams.WRAP_CONTENT, lp.width);
162         assertEquals(ViewGroup.LayoutParams.WRAP_CONTENT, lp.height);
163     }
164 
populate(GridLayout container)165     private View[][] populate(GridLayout container) {
166         Context context = container.getContext();
167         int N = VERTICAL_ALIGNMENTS.length;
168         int M = HORIZONTAL_ALIGNMENTS.length;
169 
170         View[][] table = new View[N + 1][M + 1];
171 
172         {
173             TextView v = new TextView(context);
174             GridLayout.LayoutParams lp = new GridLayout.LayoutParams(spec(0), spec(0));
175             lp.setGravity(Gravity.CENTER);
176             v.setText("*");
177             container.addView(v, lp);
178         }
179         for (int i = 0; i < N; i++) {
180             Alignment va = VERTICAL_ALIGNMENTS[i];
181             int row = i + 1;
182             GridLayout.LayoutParams lp = new GridLayout.LayoutParams(spec(row), spec(0));
183             lp.setGravity(va.gravity | Gravity.CENTER_HORIZONTAL);
184             TextView v = new TextView(context);
185             v.setGravity(Gravity.CENTER);
186             v.setText(va.name);
187             container.addView(v, lp);
188             table[row][0] = v;
189         }
190         for (int j = 0; j < M; j++) {
191             Alignment ha = HORIZONTAL_ALIGNMENTS[j];
192             int col = j + 1;
193             GridLayout.LayoutParams lp = new GridLayout.LayoutParams(spec(0), spec(col));
194             lp.setGravity(Gravity.CENTER_VERTICAL | ha.gravity);
195             TextView v = new TextView(context);
196             v.setGravity(Gravity.CENTER);
197             v.setText(ha.name);
198             container.addView(v, lp);
199             table[0][col] = v;
200         }
201         for (int i = 0; i < N; i++) {
202             for (int j = 0; j < M; j++) {
203                 Alignment ha = HORIZONTAL_ALIGNMENTS[j];
204                 Alignment va = VERTICAL_ALIGNMENTS[i];
205                 int row = i + 1;
206                 int col = j + 1;
207                 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(spec(row), spec(col));
208                 lp.setGravity(va.gravity | ha.gravity);
209                 TextView v = new Button(context);
210                 v.setText("X");
211                 v.setTextSize(10 + 5 * row * col);
212                 container.addView(v, lp);
213                 table[row][col] = v;
214             }
215         }
216         return table;
217     }
218 
testAlignment(int row, int col, Alignment a, View v0, View v1, String group)219     private void testAlignment(int row, int col, Alignment a, View v0, View v1, String group) {
220         int a0 = a.getValue(v0);
221         int a1 = a.getValue(v1);
222         assertEquals("View at row " + row + ", column " + col + " was not " + a.name +
223                 " aligned with its title " + group + ";",
224                 a0, a1);
225     }
226 
test(GridLayout p, View[][] table)227     private void test(GridLayout p, View[][] table) {
228         p.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
229         p.layout(0, 0, MAX_X, MAX_Y);
230 
231         int N = VERTICAL_ALIGNMENTS.length;
232         int M = HORIZONTAL_ALIGNMENTS.length;
233 
234         // test all horizontal alignments in each column
235         for (int j = 0; j < M; j++) {
236             int col = j + 1;
237             View v0 = table[0][col];
238             Alignment alignment = HORIZONTAL_ALIGNMENTS[j];
239             for (int i = 0; i < N; i++) {
240                 int row = i + 1;
241                 testAlignment(row, col, alignment, v0, table[row][col], "column");
242             }
243         }
244 
245         // test all vertical alignments in each row
246         for (int i = 0; i < N; i++) {
247             int row = i + 1;
248             View v0 = table[row][0];
249             Alignment alignment = VERTICAL_ALIGNMENTS[i];
250             for (int j = 0; j < M; j++) {
251                 int col = j + 1;
252                 testAlignment(row, col, alignment, v0, table[row][col], "row");
253             }
254         }
255     }
testAlignment()256     public void testAlignment() {
257         GridLayout p = new GridLayout(mContext);
258         View[][] table = populate(p);
259         test(p, table);
260         //p.setLayoutMode(ViewGroup.LAYOUT_MODE_OPTICAL_BOUNDS);
261         //test(p, table);
262     }
263 }
264