Home | History | Annotate | Download | only in cts
      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 com.android.cts.widget.R;
     20 
     21 
     22 import org.xmlpull.v1.XmlPullParser;
     23 import org.xmlpull.v1.XmlPullParserException;
     24 
     25 import android.app.Activity;
     26 import android.content.Context;
     27 import android.cts.util.WidgetTestUtils;
     28 import android.test.ActivityInstrumentationTestCase2;
     29 import android.util.AttributeSet;
     30 import android.util.Xml;
     31 import android.view.ViewGroup;
     32 import android.widget.AbsoluteLayout;
     33 import android.widget.AbsoluteLayout.LayoutParams;
     34 
     35 import java.io.IOException;
     36 
     37 @SuppressWarnings("deprecation")
     38 public class AbsoluteLayoutTest extends ActivityInstrumentationTestCase2<CtsActivity> {
     39     private static final int DEFAULT_X      = 5;
     40     private static final int DEFAULT_Y      = 10;
     41     private static final int DEFAULT_WIDTH  = 20;
     42     private static final int DEFAULT_HEIGHT = 30;
     43 
     44     private Activity mActivity;
     45     private MyAbsoluteLayout mMyAbsoluteLayout;
     46     private LayoutParams mAbsoluteLayoutParams;
     47 
     48     public AbsoluteLayoutTest() {
     49         super("com.android.cts.widget", CtsActivity.class);
     50     }
     51 
     52     @Override
     53     protected void setUp() throws Exception {
     54         super.setUp();
     55         mActivity = getActivity();
     56         mMyAbsoluteLayout = new MyAbsoluteLayout(mActivity);
     57         mAbsoluteLayoutParams = new LayoutParams(DEFAULT_WIDTH, DEFAULT_HEIGHT,
     58                 DEFAULT_X, DEFAULT_Y);
     59     }
     60 
     61     private AttributeSet getAttributeSet() throws XmlPullParserException, IOException {
     62         XmlPullParser parser = mActivity.getResources().getLayout(R.layout.absolute_layout);
     63         WidgetTestUtils.beginDocument(parser, "LinearLayout");
     64         return Xml.asAttributeSet(parser);
     65     }
     66 
     67     public void testConstructor() throws XmlPullParserException, IOException {
     68         AttributeSet attrs = getAttributeSet();
     69 
     70         new AbsoluteLayout(mActivity);
     71         new AbsoluteLayout(mActivity, attrs);
     72         new AbsoluteLayout(mActivity, attrs, 0);
     73         new AbsoluteLayout(mActivity, null, 1);
     74         new AbsoluteLayout(mActivity, attrs, -1);
     75     }
     76 
     77     public void testOnMeasure() {
     78         // onMeasure() is implementation details, do NOT test
     79     }
     80 
     81     public void testOnLayout() {
     82         // onMeasure() is implementation details, do NOT test
     83     }
     84 
     85     public void testCheckLayoutParams() {
     86         assertTrue(mMyAbsoluteLayout.checkLayoutParams(mAbsoluteLayoutParams));
     87 
     88         ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(1, 2);
     89         assertFalse(mMyAbsoluteLayout.checkLayoutParams(layoutParams));
     90         assertFalse(mMyAbsoluteLayout.checkLayoutParams(null));
     91     }
     92 
     93     public void testGenerateLayoutParams1() throws Throwable {
     94         runTestOnUiThread(new Runnable() {
     95             public void run() {
     96                 mActivity.setContentView(R.layout.absolute_layout);
     97             }
     98         });
     99         getInstrumentation().waitForIdleSync();
    100         AbsoluteLayout layout = (AbsoluteLayout) mActivity.findViewById(R.id.absolute_view);
    101         LayoutParams params = (LayoutParams) layout.generateLayoutParams(getAttributeSet());
    102 
    103         assertNotNull(params);
    104         assertEquals(LayoutParams.MATCH_PARENT, params.width);
    105         assertEquals(LayoutParams.MATCH_PARENT, params.height);
    106         assertEquals(0, params.x);
    107         assertEquals(0, params.y);
    108     }
    109 
    110     public void testGenerateLayoutParams2() {
    111         LayoutParams params =
    112             (LayoutParams) mMyAbsoluteLayout.generateLayoutParams(mAbsoluteLayoutParams);
    113 
    114         assertEquals(DEFAULT_WIDTH, params.width);
    115         assertEquals(DEFAULT_HEIGHT, params.height);
    116         assertEquals(0, params.x);
    117         assertEquals(0, params.y);
    118 
    119         try {
    120             mMyAbsoluteLayout.generateLayoutParams((LayoutParams) null);
    121             fail("did not throw NullPointerException when ViewGroup.LayoutParams is null.");
    122         } catch (NullPointerException e) {
    123             // expected, test success
    124         }
    125     }
    126 
    127     public void testGenerateDefaultLayoutParams() {
    128         LayoutParams params = (LayoutParams) mMyAbsoluteLayout.generateDefaultLayoutParams();
    129 
    130         assertEquals(LayoutParams.WRAP_CONTENT, params.width);
    131         assertEquals(LayoutParams.WRAP_CONTENT, params.height);
    132         assertEquals(0, params.x);
    133         assertEquals(0, params.y);
    134     }
    135 
    136     private static class MyAbsoluteLayout extends AbsoluteLayout {
    137         public MyAbsoluteLayout(Context context) {
    138             super(context);
    139         }
    140 
    141         @Override
    142         protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
    143             return super.checkLayoutParams(p);
    144         }
    145 
    146         @Override
    147         protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
    148             return super.generateDefaultLayoutParams();
    149         }
    150 
    151         @Override
    152         protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
    153             return super.generateLayoutParams(p);
    154         }
    155     }
    156 }
    157