1 /* 2 * Copyright (C) 2016 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.content.Context; 22 import android.graphics.Color; 23 import android.graphics.Outline; 24 import android.graphics.Rect; 25 import android.graphics.drawable.ColorDrawable; 26 import android.view.View; 27 import android.view.ViewOutlineProvider; 28 29 import androidx.annotation.NonNull; 30 import androidx.test.InstrumentationRegistry; 31 import androidx.test.annotation.UiThreadTest; 32 import androidx.test.filters.SmallTest; 33 import androidx.test.runner.AndroidJUnit4; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 39 @SmallTest 40 @RunWith(AndroidJUnit4.class) 41 public class ViewOutlineProviderTest { 42 private Context mContext; 43 44 @Before 45 public void setup() { 46 mContext = InstrumentationRegistry.getTargetContext(); 47 } 48 49 private void setViewLeftTopRightBottom(View view, int left, int top, int right, int bottom) { 50 view.setLeft(left); 51 view.setTop(top); 52 view.setRight(right); 53 view.setBottom(bottom); 54 } 55 56 @UiThreadTest 57 @Test 58 public void testBackground() { 59 View view = new View(mContext); 60 setViewLeftTopRightBottom(view, 100, 200, 300, 400); 61 62 Outline outline = new Outline(); 63 outline.setAlpha(1.0f); 64 Rect queryRect = new Rect(); 65 66 // No background - outline is 0 alpha, width x height rect 67 ViewOutlineProvider.BACKGROUND.getOutline(view, outline); 68 outline.getRect(queryRect); 69 assertEquals(new Rect(0, 0, 200, 200), queryRect); 70 assertEquals(0f, outline.getAlpha(), 0f); 71 72 // With background - outline is passed directly from background 73 view.setBackground(new ColorDrawable(Color.BLACK) { 74 @Override 75 public void getOutline(@NonNull Outline outline) { 76 outline.setRect(1, 2, 3, 4); 77 outline.setAlpha(0.123f); 78 } 79 }); 80 ViewOutlineProvider.BACKGROUND.getOutline(view, outline); 81 outline.getRect(queryRect); 82 assertEquals(new Rect(1, 2, 3, 4), queryRect); 83 assertEquals(0.123f, outline.getAlpha(), 0f); 84 } 85 86 @UiThreadTest 87 @Test 88 public void testBounds() { 89 View view = new View(mContext); 90 91 Outline outline = new Outline(); 92 Rect queryRect = new Rect(); 93 outline.setAlpha(0.123f); 94 95 setViewLeftTopRightBottom(view, 1, 2, 3, 4); 96 ViewOutlineProvider.BOUNDS.getOutline(view, outline); 97 outline.getRect(queryRect); 98 assertEquals(new Rect(0, 0, 2, 2), queryRect); // local width/height 99 assertEquals(0.123f, outline.getAlpha(), 0f); // alpha not changed 100 101 setViewLeftTopRightBottom(view, 100, 200, 300, 400); 102 ViewOutlineProvider.BOUNDS.getOutline(view, outline); 103 outline.getRect(queryRect); 104 assertEquals(new Rect(0, 0, 200, 200), queryRect); // local width/height 105 assertEquals(0.123f, outline.getAlpha(), 0f); // alpha not changed 106 } 107 108 @UiThreadTest 109 @Test 110 public void testPaddedBounds() { 111 View view = new View(mContext); 112 113 Outline outline = new Outline(); 114 Rect queryRect = new Rect(); 115 outline.setAlpha(0.123f); 116 117 setViewLeftTopRightBottom(view, 10, 20, 30, 40); 118 view.setPadding(0, 0, 0, 0); 119 ViewOutlineProvider.PADDED_BOUNDS.getOutline(view, outline); 120 outline.getRect(queryRect); 121 assertEquals(new Rect(0, 0, 20, 20), queryRect); // local width/height 122 assertEquals(0.123f, outline.getAlpha(), 0f); // alpha not changed 123 124 view.setPadding(5, 5, 5, 5); 125 ViewOutlineProvider.PADDED_BOUNDS.getOutline(view, outline); 126 outline.getRect(queryRect); 127 assertEquals(new Rect(5, 5, 15, 15), queryRect); // local width/height, inset by 5 128 assertEquals(0.123f, outline.getAlpha(), 0f); // alpha not changed 129 } 130 } 131