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.Instrumentation;
23 import android.util.DisplayMetrics;
24 
25 import androidx.test.InstrumentationRegistry;
26 import androidx.test.rule.ActivityTestRule;
27 import androidx.test.runner.AndroidJUnit4;
28 
29 import com.android.compatibility.common.util.ShellUtils;
30 
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 
35 /**
36  * Tests {@link android.view.Window#setCloseOnTouchOutside(boolean)} through exposed Activity API.
37  */
38 @RunWith(AndroidJUnit4.class)
39 public class CloseOnOutsideTests {
40 
41     @Rule
42     public final ActivityTestRule<CloseOnOutsideTestActivity> mTestActivity =
43             new ActivityTestRule<>(CloseOnOutsideTestActivity.class, true, true);
44 
45     @Test
withDefaults()46     public void withDefaults() {
47         touchAndAssert(false /* shouldBeFinishing */);
48     }
49 
50     @Test
finishTrue()51     public void finishTrue() {
52         mTestActivity.getActivity().setFinishOnTouchOutside(true);
53         touchAndAssert(true /* shouldBeFinishing */);
54     }
55 
56     @Test
finishFalse()57     public void finishFalse() {
58         mTestActivity.getActivity().setFinishOnTouchOutside(false);
59         touchAndAssert(false /* shouldBeFinishing */);
60     }
61 
62     // Tap the bottom right and check the Activity is finishing
touchAndAssert(boolean shouldBeFinishing)63     private void touchAndAssert(boolean shouldBeFinishing) {
64         DisplayMetrics displayMetrics =
65                 mTestActivity.getActivity().getResources().getDisplayMetrics();
66         int width = (int) (displayMetrics.widthPixels * 0.875f);
67         int height = (int) (displayMetrics.heightPixels * 0.875f);
68 
69         Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
70 
71         // To be safe, make sure nothing else is finishing the Activity
72         instrumentation.runOnMainSync(() -> assertFalse(mTestActivity.getActivity().isFinishing()));
73 
74         ShellUtils.runShellCommand("input tap %d %d", width, height);
75 
76         instrumentation.runOnMainSync(
77                 () -> assertEquals(shouldBeFinishing, mTestActivity.getActivity().isFinishing()));
78     }
79 }
80