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 22 import android.app.Activity; 23 import android.graphics.Canvas; 24 import android.graphics.Rect; 25 import android.test.ActivityInstrumentationTestCase2; 26 import android.test.UiThreadTest; 27 import android.view.View; 28 29 /** 30 * For the view test is too big, we divide the test cases into several parts. 31 * This part contains size, padding, margin, layout and drawing 32 */ 33 public class View_LayoutPositionTest 34 extends ActivityInstrumentationTestCase2<ViewLayoutPositionTestStubActivity> { 35 36 private Activity mActivity; 37 38 public View_LayoutPositionTest() { 39 super("com.android.cts.stub", ViewLayoutPositionTestStubActivity.class); 40 } 41 42 @Override 43 protected void setUp() throws Exception { 44 super.setUp(); 45 mActivity = getActivity(); 46 } 47 48 @UiThreadTest 49 public void testPositionInParent() { 50 View parent = mActivity.findViewById(R.id.testparent); 51 View view = mActivity.findViewById(R.id.testview); 52 int [] pLocation = new int[2]; 53 int [] vLocation = new int[2]; 54 Rect pRect = new Rect(); 55 Rect vRect = new Rect(); 56 57 // baseline not support in view 58 assertEquals(-1, view.getBaseline()); 59 60 parent.getLocationOnScreen(pLocation); 61 view.getLocationOnScreen(vLocation); 62 63 parent.getDrawingRect(pRect); 64 view.getDrawingRect(vRect); 65 66 int left = vLocation[0] - pLocation[0]; 67 int top = vLocation[1] - pLocation[1]; 68 int right = left + vRect.width(); 69 int bottom = top + vRect.height(); 70 71 assertEquals(left, view.getLeft()); 72 assertEquals(top, view.getTop()); 73 assertEquals(right, view.getRight()); 74 assertEquals(bottom, view.getBottom()); 75 76 assertEquals(vRect.width(), view.getWidth()); 77 assertEquals(vRect.height(), view.getHeight()); 78 79 assertEquals(vRect.width(), view.getMeasuredWidth()); 80 assertEquals(vRect.height(), view.getMeasuredHeight()); 81 82 int v_offset = 10; 83 int h_offset = 20; 84 view.offsetTopAndBottom(v_offset); 85 view.offsetLeftAndRight(h_offset); 86 87 // update the position 88 parent.getLocationOnScreen(pLocation); 89 view.getLocationOnScreen(vLocation); 90 parent.getDrawingRect(pRect); 91 view.getDrawingRect(vRect); 92 93 int nleft = vLocation[0] - pLocation[0]; 94 int ntop = vLocation[1] - pLocation[1]; 95 int nright = nleft + vRect.width(); 96 int nbottom = ntop + vRect.height(); 97 98 assertEquals(left + h_offset , nleft); 99 assertEquals(top + v_offset, ntop); 100 assertEquals(right + h_offset, nright); 101 assertEquals(bottom + v_offset, nbottom); 102 } 103 104 public void testPadding() { 105 View view = new View(mActivity); 106 107 // default is 0 108 assertEquals(0, view.getPaddingLeft()); 109 assertEquals(0, view.getPaddingTop()); 110 assertEquals(0, view.getPaddingRight()); 111 assertEquals(0, view.getPaddingBottom()); 112 113 view.setPadding(-10, 0, 5, 1000); 114 assertEquals(-10, view.getPaddingLeft()); 115 assertEquals(0, view.getPaddingTop()); 116 assertEquals(5, view.getPaddingRight()); 117 assertEquals(1000, view.getPaddingBottom()); 118 } 119 } 120