Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 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 | |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 19 | import static android.app.AppOpsManager.MODE_ALLOWED; |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 20 | import static android.app.AppOpsManager.OPSTR_SYSTEM_ALERT_WINDOW; |
Brett Chabot | 2ca72e9 | 2019-02-15 11:21:51 -0800 | [diff] [blame] | 21 | |
| 22 | import static androidx.test.InstrumentationRegistry.getInstrumentation; |
Tadashi G. Takaoka | f280a9b | 2019-02-01 15:28:04 +0900 | [diff] [blame] | 23 | |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 24 | import static org.junit.Assert.assertFalse; |
| 25 | import static org.junit.Assert.assertTrue; |
| 26 | import static org.mockito.ArgumentMatchers.anyBoolean; |
| 27 | import static org.mockito.ArgumentMatchers.anyInt; |
| 28 | import static org.mockito.ArgumentMatchers.anyString; |
| 29 | import static org.mockito.ArgumentMatchers.eq; |
| 30 | import static org.mockito.Mockito.mock; |
| 31 | import static org.mockito.Mockito.reset; |
| 32 | import static org.mockito.Mockito.timeout; |
| 33 | import static org.mockito.Mockito.verify; |
| 34 | |
Tadashi G. Takaoka | f280a9b | 2019-02-01 15:28:04 +0900 | [diff] [blame] | 35 | import android.app.AppOpsManager; |
| 36 | import android.os.Process; |
| 37 | import android.platform.test.annotations.Presubmit; |
Brett Chabot | 2ca72e9 | 2019-02-15 11:21:51 -0800 | [diff] [blame] | 38 | |
Brett Chabot | 2ca72e9 | 2019-02-15 11:21:51 -0800 | [diff] [blame] | 39 | import androidx.test.rule.ActivityTestRule; |
Tadashi G. Takaoka | f280a9b | 2019-02-01 15:28:04 +0900 | [diff] [blame] | 40 | |
| 41 | import com.android.compatibility.common.util.AppOpsUtils; |
| 42 | |
| 43 | import org.junit.AfterClass; |
| 44 | import org.junit.BeforeClass; |
| 45 | import org.junit.Rule; |
| 46 | import org.junit.Test; |
| 47 | |
| 48 | import java.io.IOException; |
| 49 | import java.util.concurrent.TimeUnit; |
| 50 | |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 51 | /** |
| 52 | * Test whether system alert window properly interacts with app ops. |
| 53 | * |
Tadashi G. Takaoka | f280a9b | 2019-02-01 15:28:04 +0900 | [diff] [blame] | 54 | * Build/Install/Run: |
| 55 | * atest CtsWindowManagerDeviceTestCases:AlertWindowsAppOpsTests |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 56 | */ |
Tadashi G. Takaoka | f280a9b | 2019-02-01 15:28:04 +0900 | [diff] [blame] | 57 | @Presubmit |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 58 | public class AlertWindowsAppOpsTests { |
| 59 | private static final long APP_OP_CHANGE_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(2); |
| 60 | |
Bernardo Rufino | 9ab0d98 | 2020-10-15 18:36:34 +0000 | [diff] [blame] | 61 | private static int sPreviousSawAppOp; |
| 62 | |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 63 | @Rule |
| 64 | public final ActivityTestRule<AlertWindowsAppOpsTestsActivity> mActivityRule = |
| 65 | new ActivityTestRule<>(AlertWindowsAppOpsTestsActivity.class); |
| 66 | |
| 67 | @BeforeClass |
| 68 | public static void grantSystemAlertWindowAccess() throws IOException { |
Bernardo Rufino | 9ab0d98 | 2020-10-15 18:36:34 +0000 | [diff] [blame] | 69 | String packageName = getInstrumentation().getContext().getPackageName(); |
| 70 | sPreviousSawAppOp = AppOpsUtils.getOpMode(packageName, OPSTR_SYSTEM_ALERT_WINDOW); |
| 71 | AppOpsUtils.setOpMode(packageName, OPSTR_SYSTEM_ALERT_WINDOW, MODE_ALLOWED); |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @AfterClass |
| 75 | public static void revokeSystemAlertWindowAccess() throws IOException { |
Tadashi G. Takaoka | f280a9b | 2019-02-01 15:28:04 +0900 | [diff] [blame] | 76 | AppOpsUtils.setOpMode(getInstrumentation().getContext().getPackageName(), |
Bernardo Rufino | 9ab0d98 | 2020-10-15 18:36:34 +0000 | [diff] [blame] | 77 | OPSTR_SYSTEM_ALERT_WINDOW, sPreviousSawAppOp); |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void testSystemAlertWindowAppOpsInitiallyAllowed() { |
Tadashi G. Takaoka | f280a9b | 2019-02-01 15:28:04 +0900 | [diff] [blame] | 82 | final String packageName = getInstrumentation().getContext().getPackageName(); |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 83 | final int uid = Process.myUid(); |
| 84 | |
Tadashi G. Takaoka | f280a9b | 2019-02-01 15:28:04 +0900 | [diff] [blame] | 85 | final AppOpsManager appOpsManager = getInstrumentation().getContext() |
| 86 | .getSystemService(AppOpsManager.class); |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 87 | final AppOpsManager.OnOpActiveChangedListener listener = mock( |
| 88 | AppOpsManager.OnOpActiveChangedListener.class); |
| 89 | |
| 90 | // Launch our activity. |
| 91 | final AlertWindowsAppOpsTestsActivity activity = mActivityRule.getActivity(); |
| 92 | |
| 93 | // Start watching for app op |
Jeff Sharkey | 055639b | 2019-08-20 16:50:37 -0600 | [diff] [blame] | 94 | appOpsManager.startWatchingActive(new String[] { OPSTR_SYSTEM_ALERT_WINDOW }, |
| 95 | getInstrumentation().getContext().getMainExecutor(), listener); |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 96 | |
| 97 | // Assert the app op is not started |
Jeff Sharkey | 055639b | 2019-08-20 16:50:37 -0600 | [diff] [blame] | 98 | assertFalse(appOpsManager.isOpActive(OPSTR_SYSTEM_ALERT_WINDOW, uid, packageName)); |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 99 | |
| 100 | |
| 101 | // Show a system alert window. |
Tadashi G. Takaoka | f280a9b | 2019-02-01 15:28:04 +0900 | [diff] [blame] | 102 | getInstrumentation().runOnMainSync(activity::showSystemAlertWindow); |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 103 | |
| 104 | // The app op should start |
| 105 | verify(listener, timeout(APP_OP_CHANGE_TIMEOUT_MILLIS) |
Jeff Sharkey | 055639b | 2019-08-20 16:50:37 -0600 | [diff] [blame] | 106 | .only()).onOpActiveChanged(eq(OPSTR_SYSTEM_ALERT_WINDOW), |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 107 | eq(uid), eq(packageName), eq(true)); |
| 108 | |
| 109 | // The app op should be reported as started |
Jeff Sharkey | 055639b | 2019-08-20 16:50:37 -0600 | [diff] [blame] | 110 | assertTrue(appOpsManager.isOpActive(OPSTR_SYSTEM_ALERT_WINDOW, |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 111 | uid, packageName)); |
| 112 | |
| 113 | |
| 114 | // Start with a clean slate |
| 115 | reset(listener); |
| 116 | |
| 117 | // Hide a system alert window. |
Tadashi G. Takaoka | f280a9b | 2019-02-01 15:28:04 +0900 | [diff] [blame] | 118 | getInstrumentation().runOnMainSync(activity::hideSystemAlertWindow); |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 119 | |
| 120 | // The app op should finish |
| 121 | verify(listener, timeout(APP_OP_CHANGE_TIMEOUT_MILLIS) |
Jeff Sharkey | 055639b | 2019-08-20 16:50:37 -0600 | [diff] [blame] | 122 | .only()).onOpActiveChanged(eq(OPSTR_SYSTEM_ALERT_WINDOW), |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 123 | eq(uid), eq(packageName), eq(false)); |
| 124 | |
| 125 | // The app op should be reported as finished |
Jeff Sharkey | 055639b | 2019-08-20 16:50:37 -0600 | [diff] [blame] | 126 | assertFalse(appOpsManager.isOpActive(OPSTR_SYSTEM_ALERT_WINDOW, uid, packageName)); |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 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 |
Tadashi G. Takaoka | f280a9b | 2019-02-01 15:28:04 +0900 | [diff] [blame] | 136 | getInstrumentation().runOnMainSync(activity::showSystemAlertWindow); |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 137 | |
| 138 | // No other callbacks expected |
| 139 | verify(listener, timeout(APP_OP_CHANGE_TIMEOUT_MILLIS).times(0)) |
Jeff Sharkey | 055639b | 2019-08-20 16:50:37 -0600 | [diff] [blame] | 140 | .onOpActiveChanged(eq(OPSTR_SYSTEM_ALERT_WINDOW), |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 141 | anyInt(), anyString(), anyBoolean()); |
| 142 | |
| 143 | // The app op should be reported as started |
Jeff Sharkey | 055639b | 2019-08-20 16:50:37 -0600 | [diff] [blame] | 144 | assertTrue(appOpsManager.isOpActive(OPSTR_SYSTEM_ALERT_WINDOW, uid, packageName)); |
Svet Ganov | 29881a5 | 2018-02-20 23:18:52 -0800 | [diff] [blame] | 145 | } |
Wale Ogunwale | 5871c5b | 2019-02-21 15:56:14 -0800 | [diff] [blame] | 146 | } |