1 package android.uirendering.cts.testclasses; 2 3 import android.graphics.Color; 4 import android.graphics.Outline; 5 import android.graphics.Rect; 6 import android.uirendering.cts.bitmapverifiers.BitmapVerifier; 7 import android.uirendering.cts.bitmapverifiers.RectVerifier; 8 import android.uirendering.cts.testclasses.view.UnclippedBlueView; 9 import android.uirendering.cts.testinfrastructure.ActivityTestBase; 10 import android.uirendering.cts.testinfrastructure.ViewInitializer; 11 import android.view.View; 12 import android.view.ViewGroup; 13 import android.view.ViewOutlineProvider; 14 import com.android.cts.uirendering.R; 15 16 /** 17 * This tests view clipping by modifying properties of blue_padded_layout, and validating 18 * the resulting rect of content. 19 * 20 * Since the layout is blue on a white background, this is always done with a RectVerifier. 21 */ 22 public class ViewClippingTests extends ActivityTestBase { 23 final Rect FULL_RECT = new Rect(0, 0, 90, 90); 24 final Rect BOUNDS_RECT = new Rect(0, 0, 80, 80); 25 final Rect PADDED_RECT = new Rect(15, 16, 63, 62); 26 final Rect OUTLINE_RECT = new Rect(1, 2, 78, 79); 27 final Rect CLIP_BOUNDS_RECT = new Rect(10, 20, 50, 60); 28 29 final ViewInitializer BOUNDS_CLIP_INIT = new ViewInitializer() { 30 @Override 31 public void initializeView(View rootView) { 32 ((ViewGroup)rootView).setClipChildren(true); 33 } 34 }; 35 final ViewInitializer PADDING_CLIP_INIT = new ViewInitializer() { 36 @Override 37 public void initializeView(View rootView) { 38 ViewGroup child = (ViewGroup) rootView.findViewById(R.id.child); 39 child.setClipToPadding(true); 40 child.setWillNotDraw(true); 41 child.addView(new UnclippedBlueView(rootView.getContext())); 42 } 43 }; 44 final ViewInitializer OUTLINE_CLIP_INIT = new ViewInitializer() { 45 @Override 46 public void initializeView(View rootView) { 47 View child = rootView.findViewById(R.id.child); 48 child.setOutlineProvider(new ViewOutlineProvider() { 49 @Override 50 public void getOutline(View view, Outline outline) { 51 outline.setRect(OUTLINE_RECT); 52 } 53 }); 54 child.setClipToOutline(true); 55 } 56 }; 57 final ViewInitializer CLIP_BOUNDS_CLIP_INIT = new ViewInitializer() { 58 @Override 59 public void initializeView(View view) { 60 view.setClipBounds(CLIP_BOUNDS_RECT); 61 } 62 }; 63 makeClipVerifier(Rect blueBoundsRect)64 static BitmapVerifier makeClipVerifier(Rect blueBoundsRect) { 65 // very high error tolerance, since all these tests care about is clip alignment 66 return new RectVerifier(Color.WHITE, Color.BLUE, blueBoundsRect, 75); 67 } 68 testSimpleUnclipped()69 public void testSimpleUnclipped() { 70 createTest() 71 .addLayout(R.layout.blue_padded_layout, null) 72 .runWithVerifier(makeClipVerifier(FULL_RECT)); 73 } 74 testSimpleBoundsClip()75 public void testSimpleBoundsClip() { 76 createTest() 77 .addLayout(R.layout.blue_padded_layout, BOUNDS_CLIP_INIT) 78 .runWithVerifier(makeClipVerifier(BOUNDS_RECT)); 79 } 80 testSimpleClipBoundsClip()81 public void testSimpleClipBoundsClip() { 82 createTest() 83 .addLayout(R.layout.blue_padded_layout, CLIP_BOUNDS_CLIP_INIT) 84 .runWithVerifier(makeClipVerifier(CLIP_BOUNDS_RECT)); 85 } 86 testSimplePaddingClip()87 public void testSimplePaddingClip() { 88 createTest() 89 .addLayout(R.layout.blue_padded_layout, PADDING_CLIP_INIT) 90 .runWithVerifier(makeClipVerifier(PADDED_RECT)); 91 } 92 testSimpleOutlineClip()93 public void testSimpleOutlineClip() { 94 // NOTE: Only HW is supported 95 createTest() 96 .addLayout(R.layout.blue_padded_layout, OUTLINE_CLIP_INIT, true) 97 .runWithVerifier(makeClipVerifier(OUTLINE_RECT)); 98 99 // SW ignores the outline clip 100 createTest() 101 .addLayout(R.layout.blue_padded_layout, OUTLINE_CLIP_INIT, false) 102 .runWithVerifier(makeClipVerifier(FULL_RECT)); 103 } 104 105 // TODO: add tests with clip + scroll, and with interesting combinations of the above 106 } 107