1 /* 2 * Copyright (C) 2019 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 import static org.junit.Assert.assertSame; 21 import static org.junit.Assert.assertTrue; 22 23 import android.Manifest; 24 import android.graphics.Canvas; 25 import android.graphics.Point; 26 import android.platform.test.annotations.AppModeSdkSandbox; 27 import android.view.View.DragShadowBuilder; 28 29 import androidx.test.ext.junit.runners.AndroidJUnit4; 30 import androidx.test.filters.MediumTest; 31 import androidx.test.platform.app.InstrumentationRegistry; 32 import androidx.test.rule.ActivityTestRule; 33 34 import com.android.compatibility.common.util.AdoptShellPermissionsRule; 35 36 import org.junit.Before; 37 import org.junit.Rule; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 41 /** 42 * Test {@link DragShadowBuilder}. 43 */ 44 @MediumTest 45 @RunWith(AndroidJUnit4.class) 46 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).") 47 public class View_DragShadowBuilderTest { 48 private DragShadowBuilder mBuilder; 49 private MockView mView; 50 51 @Rule(order = 0) 52 public AdoptShellPermissionsRule mAdoptShellPermissionsRule = new AdoptShellPermissionsRule( 53 InstrumentationRegistry.getInstrumentation().getUiAutomation(), 54 Manifest.permission.START_ACTIVITIES_FROM_SDK_SANDBOX); 55 56 @Rule(order = 1) 57 public ActivityTestRule<CtsActivity> mActivityRule = 58 new ActivityTestRule<>(CtsActivity.class); 59 60 @Before setup()61 public void setup() { 62 mView = new MockView(mActivityRule.getActivity()); 63 mBuilder = new DragShadowBuilder(mView); 64 } 65 66 @Test testConstructor()67 public void testConstructor() { 68 new DragShadowBuilder(mView); 69 70 new DragShadowBuilder(); 71 } 72 73 @Test testGetView()74 public void testGetView() { 75 assertSame(mView, mBuilder.getView()); 76 } 77 78 @Test testOnProvideShadowMetrics()79 public void testOnProvideShadowMetrics() { 80 Point outShadowSize = new Point(); 81 Point outShadowTouchPoint = new Point(); 82 83 mView.setLeftTopRightBottom(0, 0, 50, 50); 84 mBuilder.onProvideShadowMetrics(outShadowSize, outShadowTouchPoint); 85 86 assertEquals(mView.getWidth(), outShadowSize.x); 87 assertEquals(mView.getHeight(), outShadowSize.y); 88 89 assertEquals(outShadowSize.x / 2, outShadowTouchPoint.x); 90 assertEquals(outShadowSize.y / 2, outShadowTouchPoint.y); 91 } 92 93 @Test testOnDrawShadow()94 public void testOnDrawShadow() { 95 Canvas canvas = new Canvas(); 96 mBuilder.onDrawShadow(canvas); 97 98 assertTrue(mView.hasCalledOnDraw()); 99 } 100 } 101