1 /*
2  * Copyright (C) 2018 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 android.app.AppOpsManager;
20 import android.os.Process;
21 import android.support.test.InstrumentationRegistry;
22 import android.platform.test.annotations.Presubmit;
23 import android.support.test.filters.FlakyTest;
24 import android.support.test.rule.ActivityTestRule;
25 import android.support.test.runner.AndroidJUnit4;
26 import com.android.compatibility.common.util.AppOpsUtils;
27 import org.junit.AfterClass;
28 import org.junit.BeforeClass;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 import java.io.IOException;
34 import java.util.concurrent.TimeUnit;
35 
36 import static android.support.test.InstrumentationRegistry.getContext;
37 
38 import static android.app.AppOpsManager.MODE_ALLOWED;
39 import static android.app.AppOpsManager.MODE_ERRORED;
40 import static android.app.AppOpsManager.OPSTR_SYSTEM_ALERT_WINDOW;
41 import static android.app.AppOpsManager.OP_SYSTEM_ALERT_WINDOW;
42 import static org.junit.Assert.assertFalse;
43 import static org.junit.Assert.assertTrue;
44 import static org.mockito.ArgumentMatchers.anyBoolean;
45 import static org.mockito.ArgumentMatchers.anyInt;
46 import static org.mockito.ArgumentMatchers.anyString;
47 import static org.mockito.ArgumentMatchers.eq;
48 import static org.mockito.Mockito.mock;
49 import static org.mockito.Mockito.reset;
50 import static org.mockito.Mockito.timeout;
51 import static org.mockito.Mockito.verify;
52 
53 /**
54  * Test whether system alert window properly interacts with app ops.
55  *
56  * Build/Install/Run: atest CtsWindowManagerDeviceTestCases:AlertWindowsAppOpsTests
57  */
58 @Presubmit
59 @FlakyTest(detail = "Can be promoted to pre-submit once confirmed stable.")
60 @RunWith(AndroidJUnit4.class)
61 public class AlertWindowsAppOpsTests {
62     private static final long APP_OP_CHANGE_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(2);
63 
64     @Rule
65     public final ActivityTestRule<AlertWindowsAppOpsTestsActivity> mActivityRule =
66             new ActivityTestRule<>(AlertWindowsAppOpsTestsActivity.class);
67 
68     @BeforeClass
grantSystemAlertWindowAccess()69     public static void grantSystemAlertWindowAccess() throws IOException {
70         AppOpsUtils.setOpMode(getContext().getPackageName(),
71                 OPSTR_SYSTEM_ALERT_WINDOW, MODE_ALLOWED);
72     }
73 
74     @AfterClass
revokeSystemAlertWindowAccess()75     public static void revokeSystemAlertWindowAccess() throws IOException {
76         AppOpsUtils.setOpMode(getContext().getPackageName(),
77                 OPSTR_SYSTEM_ALERT_WINDOW, MODE_ERRORED);
78     }
79 
80     @Test
testSystemAlertWindowAppOpsInitiallyAllowed()81     public void testSystemAlertWindowAppOpsInitiallyAllowed() {
82         final String packageName = getContext().getPackageName();
83         final int uid = Process.myUid();
84 
85         final AppOpsManager appOpsManager = getContext().getSystemService(AppOpsManager.class);
86         final AppOpsManager.OnOpActiveChangedListener listener = mock(
87                 AppOpsManager.OnOpActiveChangedListener.class);
88 
89         // Launch our activity.
90         final AlertWindowsAppOpsTestsActivity activity = mActivityRule.getActivity();
91 
92         // Start watching for app op
93         appOpsManager.startWatchingActive(new int[] {OP_SYSTEM_ALERT_WINDOW}, listener);
94 
95         // Assert the app op is not started
96         assertFalse(appOpsManager.isOperationActive(OP_SYSTEM_ALERT_WINDOW, uid, packageName));
97 
98 
99         // Show a system alert window.
100         InstrumentationRegistry.getInstrumentation().runOnMainSync(
101                 activity::showSystemAlertWindow);
102 
103         // The app op should start
104         verify(listener, timeout(APP_OP_CHANGE_TIMEOUT_MILLIS)
105                 .only()).onOpActiveChanged(eq(OP_SYSTEM_ALERT_WINDOW),
106                 eq(uid), eq(packageName), eq(true));
107 
108         // The app op should be reported as started
109         assertTrue(appOpsManager.isOperationActive(OP_SYSTEM_ALERT_WINDOW,
110                 uid, packageName));
111 
112 
113         // Start with a clean slate
114         reset(listener);
115 
116         // Hide a system alert window.
117         InstrumentationRegistry.getInstrumentation().runOnMainSync(
118                 activity::hideSystemAlertWindow);
119 
120         // The app op should finish
121         verify(listener, timeout(APP_OP_CHANGE_TIMEOUT_MILLIS)
122                 .only()).onOpActiveChanged(eq(OP_SYSTEM_ALERT_WINDOW),
123                 eq(uid), eq(packageName), eq(false));
124 
125         // The app op should be reported as finished
126         assertFalse(appOpsManager.isOperationActive(OP_SYSTEM_ALERT_WINDOW, uid, packageName));
127 
128 
129         // Start with a clean slate
130         reset(listener);
131 
132         // Stop watching for app op
133         appOpsManager.stopWatchingActive(listener);
134 
135         // Show a system alert window
136         InstrumentationRegistry.getInstrumentation().runOnMainSync(
137                 activity::showSystemAlertWindow);
138 
139         // No other callbacks expected
140         verify(listener, timeout(APP_OP_CHANGE_TIMEOUT_MILLIS).times(0))
141                 .onOpActiveChanged(eq(OP_SYSTEM_ALERT_WINDOW),
142                         anyInt(), anyString(), anyBoolean());
143 
144         // The app op should be reported as started
145         assertTrue(appOpsManager.isOperationActive(OP_SYSTEM_ALERT_WINDOW, uid, packageName));
146     }
147 }