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