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.view.cts; 18 19 import com.android.cts.stub.R; 20 21 import dalvik.annotation.TestLevel; 22 import dalvik.annotation.TestTargetClass; 23 import dalvik.annotation.TestTargetNew; 24 import dalvik.annotation.TestTargets; 25 26 import android.app.Activity; 27 import android.graphics.Canvas; 28 import android.graphics.Rect; 29 import android.test.ActivityInstrumentationTestCase2; 30 import android.test.UiThreadTest; 31 import android.view.View; 32 33 /** 34 * For the view test is too big, we divide the test cases into several parts. 35 * This part contains size, padding, margin, layout and drawing 36 */ 37 @TestTargetClass(View.class) 38 public class View_LayoutPositionTest 39 extends ActivityInstrumentationTestCase2<ViewLayoutPositionTestStubActivity> { 40 41 private Activity mActivity; 42 43 public View_LayoutPositionTest() { 44 super("com.android.cts.stub", ViewLayoutPositionTestStubActivity.class); 45 } 46 47 @Override 48 protected void setUp() throws Exception { 49 super.setUp(); 50 mActivity = getActivity(); 51 } 52 53 @TestTargets({ 54 @TestTargetNew( 55 level = TestLevel.COMPLETE, 56 method = "getTop", 57 args = {} 58 ), 59 @TestTargetNew( 60 level = TestLevel.COMPLETE, 61 method = "getBottom", 62 args = {} 63 ), 64 @TestTargetNew( 65 level = TestLevel.COMPLETE, 66 method = "getRight", 67 args = {} 68 ), 69 @TestTargetNew( 70 level = TestLevel.COMPLETE, 71 method = "getLeft", 72 args = {} 73 ), 74 @TestTargetNew( 75 level = TestLevel.COMPLETE, 76 method = "offsetTopAndBottom", 77 args = {int.class} 78 ), 79 @TestTargetNew( 80 level = TestLevel.COMPLETE, 81 method = "offsetLeftAndRight", 82 args = {int.class} 83 ), 84 @TestTargetNew( 85 level = TestLevel.COMPLETE, 86 method = "getWidth", 87 args = {} 88 ), 89 @TestTargetNew( 90 level = TestLevel.COMPLETE, 91 method = "getHeight", 92 args = {} 93 ), 94 @TestTargetNew( 95 level = TestLevel.COMPLETE, 96 method = "getBaseLine", 97 args = {} 98 ), 99 @TestTargetNew( 100 level = TestLevel.COMPLETE, 101 method = "draw", 102 args = {Canvas.class} 103 ), 104 @TestTargetNew( 105 level = TestLevel.COMPLETE, 106 method = "layout", 107 args = {int.class, int.class, int.class, int.class} 108 ), 109 @TestTargetNew( 110 level = TestLevel.COMPLETE, 111 method = "getMeasuredWidth", 112 args = {} 113 ), 114 @TestTargetNew( 115 level = TestLevel.COMPLETE, 116 method = "getMeasuredHeight", 117 args = {} 118 ), 119 @TestTargetNew( 120 level = TestLevel.COMPLETE, 121 method = "getLocationOnScreen", 122 args = {int[].class} 123 ) 124 }) 125 @UiThreadTest 126 public void testPositionInParent() { 127 View parent = mActivity.findViewById(R.id.testparent); 128 View view = mActivity.findViewById(R.id.testview); 129 int [] pLocation = new int[2]; 130 int [] vLocation = new int[2]; 131 Rect pRect = new Rect(); 132 Rect vRect = new Rect(); 133 134 // baseline not support in view 135 assertEquals(-1, view.getBaseline()); 136 137 parent.getLocationOnScreen(pLocation); 138 view.getLocationOnScreen(vLocation); 139 140 parent.getDrawingRect(pRect); 141 view.getDrawingRect(vRect); 142 143 int left = vLocation[0] - pLocation[0]; 144 int top = vLocation[1] - pLocation[1]; 145 int right = left + vRect.width(); 146 int bottom = top + vRect.height(); 147 148 assertEquals(left, view.getLeft()); 149 assertEquals(top, view.getTop()); 150 assertEquals(right, view.getRight()); 151 assertEquals(bottom, view.getBottom()); 152 153 assertEquals(vRect.width(), view.getWidth()); 154 assertEquals(vRect.height(), view.getHeight()); 155 156 assertEquals(vRect.width(), view.getMeasuredWidth()); 157 assertEquals(vRect.height(), view.getMeasuredHeight()); 158 159 int v_offset = 10; 160 int h_offset = 20; 161 view.offsetTopAndBottom(v_offset); 162 view.offsetLeftAndRight(h_offset); 163 164 // update the position 165 parent.getLocationOnScreen(pLocation); 166 view.getLocationOnScreen(vLocation); 167 parent.getDrawingRect(pRect); 168 view.getDrawingRect(vRect); 169 170 int nleft = vLocation[0] - pLocation[0]; 171 int ntop = vLocation[1] - pLocation[1]; 172 int nright = nleft + vRect.width(); 173 int nbottom = ntop + vRect.height(); 174 175 assertEquals(left + h_offset , nleft); 176 assertEquals(top + v_offset, ntop); 177 assertEquals(right + h_offset, nright); 178 assertEquals(bottom + v_offset, nbottom); 179 } 180 181 @TestTargets({ 182 @TestTargetNew( 183 level = TestLevel.COMPLETE, 184 method = "getPaddingLeft", 185 args = {} 186 ), 187 @TestTargetNew( 188 level = TestLevel.COMPLETE, 189 method = "getPaddingTop", 190 args = {} 191 ), 192 @TestTargetNew( 193 level = TestLevel.COMPLETE, 194 method = "getPaddingRight", 195 args = {} 196 ), 197 @TestTargetNew( 198 level = TestLevel.COMPLETE, 199 method = "getPaddingBottom", 200 args = {} 201 ), 202 @TestTargetNew( 203 level = TestLevel.COMPLETE, 204 method = "setPadding", 205 args = {int.class, int.class, int.class, int.class} 206 ) 207 }) 208 public void testPadding() { 209 View view = new View(mActivity); 210 211 // default is 0 212 assertEquals(0, view.getPaddingLeft()); 213 assertEquals(0, view.getPaddingTop()); 214 assertEquals(0, view.getPaddingRight()); 215 assertEquals(0, view.getPaddingBottom()); 216 217 view.setPadding(-10, 0, 5, 1000); 218 assertEquals(-10, view.getPaddingLeft()); 219 assertEquals(0, view.getPaddingTop()); 220 assertEquals(5, view.getPaddingRight()); 221 assertEquals(1000, view.getPaddingBottom()); 222 } 223 } 224