Home | History | Annotate | Download | only in wm
      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 static android.app.AppOpsManager.MODE_ALLOWED;
     20 import static android.app.AppOpsManager.MODE_ERRORED;
     21 import static android.app.AppOpsManager.OPSTR_SYSTEM_ALERT_WINDOW;
     22 import static android.app.AppOpsManager.OP_SYSTEM_ALERT_WINDOW;
     23 
     24 import static androidx.test.InstrumentationRegistry.getInstrumentation;
     25 
     26 import static org.junit.Assert.assertFalse;
     27 import static org.junit.Assert.assertTrue;
     28 import static org.mockito.ArgumentMatchers.anyBoolean;
     29 import static org.mockito.ArgumentMatchers.anyInt;
     30 import static org.mockito.ArgumentMatchers.anyString;
     31 import static org.mockito.ArgumentMatchers.eq;
     32 import static org.mockito.Mockito.mock;
     33 import static org.mockito.Mockito.reset;
     34 import static org.mockito.Mockito.timeout;
     35 import static org.mockito.Mockito.verify;
     36 
     37 import android.app.AppOpsManager;
     38 import android.os.Process;
     39 import android.platform.test.annotations.Presubmit;
     40 
     41 import androidx.test.filters.FlakyTest;
     42 import androidx.test.rule.ActivityTestRule;
     43 
     44 import com.android.compatibility.common.util.AppOpsUtils;
     45 
     46 import org.junit.AfterClass;
     47 import org.junit.BeforeClass;
     48 import org.junit.Rule;
     49 import org.junit.Test;
     50 
     51 import java.io.IOException;
     52 import java.util.concurrent.TimeUnit;
     53 
     54 /**
     55  * Test whether system alert window properly interacts with app ops.
     56  *
     57  * Build/Install/Run:
     58  *     atest CtsWindowManagerDeviceTestCases:AlertWindowsAppOpsTests
     59  */
     60 @Presubmit
     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
     69     public static void grantSystemAlertWindowAccess() throws IOException {
     70         AppOpsUtils.setOpMode(getInstrumentation().getContext().getPackageName(),
     71                 OPSTR_SYSTEM_ALERT_WINDOW, MODE_ALLOWED);
     72     }
     73 
     74     @AfterClass
     75     public static void revokeSystemAlertWindowAccess() throws IOException {
     76         AppOpsUtils.setOpMode(getInstrumentation().getContext().getPackageName(),
     77                 OPSTR_SYSTEM_ALERT_WINDOW, MODE_ERRORED);
     78     }
     79 
     80     @Test
     81     public void testSystemAlertWindowAppOpsInitiallyAllowed() {
     82         final String packageName = getInstrumentation().getContext().getPackageName();
     83         final int uid = Process.myUid();
     84 
     85         final AppOpsManager appOpsManager = getInstrumentation().getContext()
     86                 .getSystemService(AppOpsManager.class);
     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
     94         appOpsManager.startWatchingActive(new int[] {OP_SYSTEM_ALERT_WINDOW}, listener);
     95 
     96         // Assert the app op is not started
     97         assertFalse(appOpsManager.isOperationActive(OP_SYSTEM_ALERT_WINDOW, uid, packageName));
     98 
     99 
    100         // Show a system alert window.
    101         getInstrumentation().runOnMainSync(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         getInstrumentation().runOnMainSync(activity::hideSystemAlertWindow);
    118 
    119         // The app op should finish
    120         verify(listener, timeout(APP_OP_CHANGE_TIMEOUT_MILLIS)
    121                 .only()).onOpActiveChanged(eq(OP_SYSTEM_ALERT_WINDOW),
    122                 eq(uid), eq(packageName), eq(false));
    123 
    124         // The app op should be reported as finished
    125         assertFalse(appOpsManager.isOperationActive(OP_SYSTEM_ALERT_WINDOW, uid, packageName));
    126 
    127 
    128         // Start with a clean slate
    129         reset(listener);
    130 
    131         // Stop watching for app op
    132         appOpsManager.stopWatchingActive(listener);
    133 
    134         // Show a system alert window
    135         getInstrumentation().runOnMainSync(activity::showSystemAlertWindow);
    136 
    137         // No other callbacks expected
    138         verify(listener, timeout(APP_OP_CHANGE_TIMEOUT_MILLIS).times(0))
    139                 .onOpActiveChanged(eq(OP_SYSTEM_ALERT_WINDOW),
    140                         anyInt(), anyString(), anyBoolean());
    141 
    142         // The app op should be reported as started
    143         assertTrue(appOpsManager.isOperationActive(OP_SYSTEM_ALERT_WINDOW, uid, packageName));
    144     }
    145 }
    146