• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.server.wm.ActivityManagerTestBase.createFullscreenActivityScenarioRule;
20 
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 
24 import android.app.Instrumentation;
25 import android.graphics.Insets;
26 import android.util.DisplayMetrics;
27 import android.view.MotionEvent;
28 import android.view.WindowInsets;
29 import android.view.WindowMetrics;
30 
31 import androidx.test.InstrumentationRegistry;
32 import androidx.test.ext.junit.rules.ActivityScenarioRule;
33 import androidx.test.runner.AndroidJUnit4;
34 
35 import org.junit.Before;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 
40 /**
41  * Tests {@link android.view.Window#setCloseOnTouchOutside(boolean)} through exposed Activity API.
42  */
43 @RunWith(AndroidJUnit4.class)
44 public class CloseOnOutsideTests {
45 
46     @Rule
47     public final ActivityScenarioRule<CloseOnOutsideTestActivity> mScenarioRule =
48             createFullscreenActivityScenarioRule(CloseOnOutsideTestActivity.class);
49 
50     private CloseOnOutsideTestActivity mTestActivity;
51 
52     @Before
setup()53     public void setup() {
54         mScenarioRule.getScenario().onActivity(activity -> mTestActivity = activity);
55     }
56 
57     @Test
withDefaults()58     public void withDefaults() {
59         touchAndAssert(false /* shouldBeFinishing */);
60     }
61 
62     @Test
finishTrue()63     public void finishTrue() {
64         mTestActivity.setFinishOnTouchOutside(true);
65         touchAndAssert(true /* shouldBeFinishing */);
66     }
67 
68     @Test
finishFalse()69     public void finishFalse() {
70         mTestActivity.setFinishOnTouchOutside(false);
71         touchAndAssert(false /* shouldBeFinishing */);
72     }
73 
74     // Tap the bottom right and check the Activity is finishing
touchAndAssert(boolean shouldBeFinishing)75     private void touchAndAssert(boolean shouldBeFinishing) {
76         DisplayMetrics displayMetrics =
77                 mTestActivity.getResources().getDisplayMetrics();
78         WindowMetrics windowMetrics =
79                 mTestActivity.getWindowManager().getCurrentWindowMetrics();
80         Insets systemBarInsets =
81                 windowMetrics.getWindowInsets().getInsets(WindowInsets.Type.systemBars());
82         Insets statusBarInsets =
83                 windowMetrics.getWindowInsets().getInsets(WindowInsets.Type.statusBars());
84 
85         // DisplayMetrics.widthPixels and DisplayMetrics.heightPixels
86         // do not include the navigation bar size, so subtract the status bar size.
87         int appAreaWidth =
88                 displayMetrics.widthPixels - statusBarInsets.left - statusBarInsets.right;
89         int appAreaHeight =
90                 displayMetrics.heightPixels - statusBarInsets.top - statusBarInsets.bottom;
91 
92         int width = (int) (systemBarInsets.left + (appAreaWidth * 0.875f));
93         int height = (int) (systemBarInsets.top + (appAreaHeight * 0.875f));
94 
95         Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
96 
97         // First setup the new window size to be smaller than the entire activity, "revealing" a
98         // clickable area outside the window.
99         instrumentation.runOnMainSync(() -> mTestActivity.setupWindowSize());
100 
101         // After that call is complete, run the test on the activity by simulating a touch outside
102         // the Window.
103         instrumentation.runOnMainSync(() -> {
104             // To be safe, make sure nothing else is finishing the Activity
105             assertFalse(mTestActivity.isFinishing());
106             mTestActivity.dispatchTouchEvent(
107                     MotionEvent.obtain(1, 1, MotionEvent.ACTION_DOWN, width, height, 0));
108             mTestActivity.dispatchTouchEvent(
109                     MotionEvent.obtain(1, 1, MotionEvent.ACTION_UP, width, height, 0));
110             assertEquals(shouldBeFinishing, mTestActivity.isFinishing());
111         });
112     }
113 }
114