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.server.wm; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 22 import android.app.ActivityOptions; 23 import android.app.Instrumentation; 24 import android.app.WindowConfiguration; 25 import android.util.DisplayMetrics; 26 27 import androidx.test.InstrumentationRegistry; 28 import androidx.test.runner.AndroidJUnit4; 29 import androidx.test.ext.junit.rules.ActivityScenarioRule; 30 31 import com.android.compatibility.common.util.ShellUtils; 32 33 import org.junit.Before; 34 import org.junit.Rule; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 38 /** 39 * Tests {@link android.view.Window#setCloseOnTouchOutside(boolean)} through exposed Activity API. 40 */ 41 @RunWith(AndroidJUnit4.class) 42 public class CloseOnOutsideTests { 43 44 @Rule 45 public final ActivityScenarioRule<CloseOnOutsideTestActivity> mScenarioRule = 46 new ActivityScenarioRule<>(CloseOnOutsideTestActivity.class); 47 48 private CloseOnOutsideTestActivity mTestActivity; 49 50 @Before setup()51 public void setup() { 52 ActivityOptions options = ActivityOptions.makeBasic(); 53 options.setLaunchWindowingMode(WindowConfiguration.WINDOWING_MODE_FULLSCREEN); 54 mScenarioRule.getScenario().launch(CloseOnOutsideTestActivity.class, options.toBundle()) 55 .onActivity(activity -> { 56 mTestActivity = activity; 57 }); 58 } 59 60 @Test withDefaults()61 public void withDefaults() { 62 touchAndAssert(false /* shouldBeFinishing */); 63 } 64 65 @Test finishTrue()66 public void finishTrue() { 67 mTestActivity.setFinishOnTouchOutside(true); 68 touchAndAssert(true /* shouldBeFinishing */); 69 } 70 71 @Test finishFalse()72 public void finishFalse() { 73 mTestActivity.setFinishOnTouchOutside(false); 74 touchAndAssert(false /* shouldBeFinishing */); 75 } 76 77 // Tap the bottom right and check the Activity is finishing touchAndAssert(boolean shouldBeFinishing)78 private void touchAndAssert(boolean shouldBeFinishing) { 79 DisplayMetrics displayMetrics = mTestActivity.getResources().getDisplayMetrics(); 80 int width = (int) (displayMetrics.widthPixels * 0.875f); 81 int height = (int) (displayMetrics.heightPixels * 0.875f); 82 83 Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); 84 85 // To be safe, make sure nothing else is finishing the Activity 86 instrumentation.runOnMainSync(() -> assertFalse(mTestActivity.isFinishing())); 87 88 ShellUtils.runShellCommand("input tap %d %d", width, height); 89 90 instrumentation.runOnMainSync( 91 () -> assertEquals(shouldBeFinishing, mTestActivity.isFinishing())); 92 } 93 } 94