Home | History | Annotate | Download | only in cts
      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 static android.widget.GridLayout.spec;
     20 
     21 import static org.junit.Assert.assertEquals;
     22 import static org.junit.Assert.assertNotNull;
     23 import static org.junit.Assert.assertTrue;
     24 
     25 import android.app.Activity;
     26 import android.content.Context;
     27 import android.support.test.annotation.UiThreadTest;
     28 import android.support.test.filters.MediumTest;
     29 import android.support.test.rule.ActivityTestRule;
     30 import android.support.test.runner.AndroidJUnit4;
     31 import android.util.AttributeSet;
     32 import android.util.Xml;
     33 import android.view.Gravity;
     34 import android.view.View;
     35 import android.view.ViewGroup;
     36 import android.widget.AbsoluteLayout;
     37 import android.widget.Button;
     38 import android.widget.GridLayout;
     39 import android.widget.TextView;
     40 
     41 import org.junit.Before;
     42 import org.junit.Rule;
     43 import org.junit.Test;
     44 import org.junit.runner.RunWith;
     45 import org.xmlpull.v1.XmlPullParser;
     46 
     47 /**
     48  * Test {@link GridLayout}.
     49  */
     50 @MediumTest
     51 @RunWith(AndroidJUnit4.class)
     52 public class GridLayoutTest {
     53 
     54     // The size of the off-screen test container in which we we will testing layout.
     55     public static final int MAX_X = 2000;
     56     public static final int MAX_Y = 2000;
     57 
     58     private static abstract class Alignment {
     59         String name;
     60         int gravity;
     61 
     62         abstract int getValue(View v);
     63 
     64         protected Alignment(String name, int gravity) {
     65             this.name = name;
     66             this.gravity = gravity;
     67         }
     68     }
     69 
     70     private static final Alignment[] HORIZONTAL_ALIGNMENTS = {
     71             new Alignment("LEFT", Gravity.LEFT) {
     72                 @Override
     73                 int getValue(View v) {
     74                     return v.getLeft();
     75                 }
     76             },
     77             new Alignment("CENTER", Gravity.CENTER_HORIZONTAL) {
     78                 @Override
     79                 int getValue(View v) {
     80                     return (v.getLeft() + v.getRight()) / 2;
     81                 }
     82             },
     83             new Alignment("RIGHT", Gravity.RIGHT) {
     84                 @Override
     85                 int getValue(View v) {
     86                     return v.getRight();
     87                 }
     88             },
     89             new Alignment("FILL", Gravity.FILL_HORIZONTAL) {
     90                 @Override
     91                 int getValue(View v) {
     92                     return v.getWidth();
     93                 }
     94             }
     95     };
     96 
     97     private static final Alignment[] VERTICAL_ALIGNMENTS = {
     98             new Alignment("TOP", Gravity.TOP) {
     99                 @Override
    100                 int getValue(View v) {
    101                     return v.getTop();
    102                 }
    103             },
    104             new Alignment("CENTER", Gravity.CENTER_VERTICAL) {
    105                 @Override
    106                 int getValue(View v) {
    107                     return (v.getTop() + v.getBottom()) / 2;
    108                 }
    109             },
    110             new Alignment("BASELINE", Gravity.NO_GRAVITY) {
    111                 @Override
    112                 int getValue(View v) {
    113                     return v.getTop() + v.getBaseline();
    114                 }
    115             },
    116             new Alignment("BOTTOM", Gravity.BOTTOM) {
    117                 @Override
    118                 int getValue(View v) {
    119                     return v.getBottom();
    120                 }
    121             },
    122             new Alignment("FILL", Gravity.FILL_VERTICAL) {
    123                 @Override
    124                 int getValue(View v) {
    125                     return v.getHeight();
    126                 }
    127             }
    128     };
    129 
    130     private Activity mActivity;
    131     private GridLayout mGridLayout;
    132 
    133     @Rule
    134     public ActivityTestRule<GridLayoutCtsActivity> mActivityRule =
    135             new ActivityTestRule<>(GridLayoutCtsActivity.class);
    136 
    137     @Before
    138     public void setup() {
    139         mActivity = mActivityRule.getActivity();
    140         mGridLayout = (GridLayout) mActivity.findViewById(R.id.gridlayout);
    141     }
    142 
    143     @Test
    144     public void testConstructor() {
    145         new GridLayout(mActivity);
    146 
    147         new GridLayout(mActivity, null);
    148 
    149         XmlPullParser parser = mActivity.getResources().getXml(R.layout.gridlayout_layout);
    150         AttributeSet attrs = Xml.asAttributeSet(parser);
    151         new GridLayout(mActivity, attrs);
    152     }
    153 
    154     @Test(expected=NullPointerException.class)
    155     public void testConstructorNullContext() {
    156         new GridLayout(null, null);
    157     }
    158 
    159     @UiThreadTest
    160     @Test
    161     public void testCheckLayoutParams() {
    162         mGridLayout.addView(new TextView(mActivity),
    163                 new AbsoluteLayout.LayoutParams(0, 0, 0, 0));
    164 
    165         mGridLayout.addView(new TextView(mActivity),
    166                 new GridLayout.LayoutParams(GridLayout.spec(0), GridLayout.spec(0)));
    167     }
    168 
    169     @UiThreadTest
    170     @Test
    171     public void testGenerateDefaultLayoutParams() {
    172         ViewGroup.LayoutParams lp = mGridLayout.generateLayoutParams(null);
    173         assertNotNull(lp);
    174         assertTrue(lp instanceof GridLayout.LayoutParams);
    175         assertEquals(ViewGroup.LayoutParams.WRAP_CONTENT, lp.width);
    176         assertEquals(ViewGroup.LayoutParams.WRAP_CONTENT, lp.height);
    177     }
    178 
    179     @Test
    180     public void testGenerateLayoutParamsFromMarginParams() {
    181         MyGridLayout gridLayout = new MyGridLayout(mActivity);
    182         ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(3, 5);
    183         lp.leftMargin = 1;
    184         lp.topMargin = 2;
    185         lp.rightMargin = 3;
    186         lp.bottomMargin = 4;
    187         GridLayout.LayoutParams generated = gridLayout.generateLayoutParams(lp);
    188         assertNotNull(generated);
    189         assertEquals(3, generated.width);
    190         assertEquals(5, generated.height);
    191 
    192         assertEquals(1, generated.leftMargin);
    193         assertEquals(2, generated.topMargin);
    194         assertEquals(3, generated.rightMargin);
    195         assertEquals(4, generated.bottomMargin);
    196     }
    197 
    198     private View[][] populate(GridLayout container) {
    199         Context context = container.getContext();
    200         int N = VERTICAL_ALIGNMENTS.length;
    201         int M = HORIZONTAL_ALIGNMENTS.length;
    202 
    203         View[][] table = new View[N + 1][M + 1];
    204 
    205         {
    206             TextView v = new TextView(context);
    207             GridLayout.LayoutParams lp = new GridLayout.LayoutParams(spec(0), spec(0));
    208             lp.setGravity(Gravity.CENTER);
    209             v.setText("*");
    210             container.addView(v, lp);
    211         }
    212         for (int i = 0; i < N; i++) {
    213             Alignment va = VERTICAL_ALIGNMENTS[i];
    214             int row = i + 1;
    215             GridLayout.LayoutParams lp = new GridLayout.LayoutParams(spec(row), spec(0));
    216             lp.setGravity(va.gravity | Gravity.CENTER_HORIZONTAL);
    217             TextView v = new TextView(context);
    218             v.setGravity(Gravity.CENTER);
    219             v.setText(va.name);
    220             container.addView(v, lp);
    221             table[row][0] = v;
    222         }
    223         for (int j = 0; j < M; j++) {
    224             Alignment ha = HORIZONTAL_ALIGNMENTS[j];
    225             int col = j + 1;
    226             GridLayout.LayoutParams lp = new GridLayout.LayoutParams(spec(0), spec(col));
    227             lp.setGravity(Gravity.CENTER_VERTICAL | ha.gravity);
    228             TextView v = new TextView(context);
    229             v.setGravity(Gravity.CENTER);
    230             v.setText(ha.name);
    231             container.addView(v, lp);
    232             table[0][col] = v;
    233         }
    234         for (int i = 0; i < N; i++) {
    235             for (int j = 0; j < M; j++) {
    236                 Alignment ha = HORIZONTAL_ALIGNMENTS[j];
    237                 Alignment va = VERTICAL_ALIGNMENTS[i];
    238                 int row = i + 1;
    239                 int col = j + 1;
    240                 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(spec(row), spec(col));
    241                 lp.setGravity(va.gravity | ha.gravity);
    242                 TextView v = new Button(context);
    243                 v.setText("X");
    244                 v.setTextSize(10 + 5 * row * col);
    245                 container.addView(v, lp);
    246                 table[row][col] = v;
    247             }
    248         }
    249         return table;
    250     }
    251 
    252     private void verifyCellAlignment(int row, int col, Alignment a, View v0, View v1,
    253             String group) {
    254         int a0 = a.getValue(v0);
    255         int a1 = a.getValue(v1);
    256         assertEquals("View at row " + row + ", column " + col + " was not " + a.name +
    257                 " aligned with its title " + group + ";",
    258                 a0, a1);
    259     }
    260 
    261     private void verifyGridAlignment(GridLayout p, View[][] table) {
    262         p.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    263         p.layout(0, 0, MAX_X, MAX_Y);
    264 
    265         int N = VERTICAL_ALIGNMENTS.length;
    266         int M = HORIZONTAL_ALIGNMENTS.length;
    267 
    268         // test all horizontal alignments in each column
    269         for (int j = 0; j < M; j++) {
    270             int col = j + 1;
    271             View v0 = table[0][col];
    272             Alignment alignment = HORIZONTAL_ALIGNMENTS[j];
    273             for (int i = 0; i < N; i++) {
    274                 int row = i + 1;
    275                 verifyCellAlignment(row, col, alignment, v0, table[row][col], "column");
    276             }
    277         }
    278 
    279         // test all vertical alignments in each row
    280         for (int i = 0; i < N; i++) {
    281             int row = i + 1;
    282             View v0 = table[row][0];
    283             Alignment alignment = VERTICAL_ALIGNMENTS[i];
    284             for (int j = 0; j < M; j++) {
    285                 int col = j + 1;
    286                 verifyCellAlignment(row, col, alignment, v0, table[row][col], "row");
    287             }
    288         }
    289     }
    290 
    291     @UiThreadTest
    292     @Test
    293     public void testAlignment() {
    294         View[][] table = populate(mGridLayout);
    295         verifyGridAlignment(mGridLayout, table);
    296     }
    297 
    298     private static class MyGridLayout extends GridLayout {
    299 
    300         public MyGridLayout(Context context) {
    301             super(context);
    302         }
    303 
    304         public MyGridLayout(Context context, AttributeSet attrs) {
    305             super(context, attrs);
    306         }
    307 
    308         public MyGridLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    309             super(context, attrs, defStyleAttr);
    310         }
    311 
    312         public MyGridLayout(Context context, AttributeSet attrs, int defStyleAttr,
    313                 int defStyleRes) {
    314             super(context, attrs, defStyleAttr, defStyleRes);
    315         }
    316 
    317         @Override
    318         protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
    319             return super.generateLayoutParams(p);
    320         }
    321     }
    322 }
    323