1 /* 2 * Copyright (C) 2017 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 com.android.settings.applications; 18 19 import static org.mockito.ArgumentMatchers.anyInt; 20 import static org.mockito.ArgumentMatchers.anyString; 21 import static org.mockito.Mockito.when; 22 23 import android.Manifest; 24 import android.app.AppOpsManager; 25 import android.content.Context; 26 import android.content.pm.IPackageManager; 27 import android.os.RemoteException; 28 import android.os.UserHandle; 29 import android.os.UserManager; 30 31 import com.android.settingslib.applications.ApplicationsState.AppEntry; 32 33 import com.google.common.truth.Truth; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.RobolectricTestRunner; 41 42 @RunWith(RobolectricTestRunner.class) 43 public final class AppStateAppOpsBridgeTest { 44 45 @Mock private Context mContext; 46 @Mock private UserManager mUserManager; 47 @Mock private IPackageManager mPackageManagerService; 48 @Mock private AppOpsManager mAppOpsManager; 49 50 @Before setUp()51 public void setUp() { 52 MockitoAnnotations.initMocks(this); 53 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 54 when(mContext.getSystemService(Context.APP_OPS_SERVICE)).thenReturn(mAppOpsManager); 55 } 56 57 @Test getPermissionInfo_nullPackageInfo_shouldNotCrash()58 public void getPermissionInfo_nullPackageInfo_shouldNotCrash() throws RemoteException { 59 when(mPackageManagerService.getPackageInfo(anyString(), anyInt(), anyInt())) 60 .thenReturn(null); 61 62 new TestAppStateAppOpsBridge().getPermissionInfo("pkg1", 1); 63 // should not crash 64 } 65 66 @Test permissionState_modeDefault_IsPermissible()67 public void permissionState_modeDefault_IsPermissible() { 68 AppStateAppOpsBridge.PermissionState permissionState = 69 new AppStateAppOpsBridge.PermissionState("pkg1", UserHandle.of(123)); 70 permissionState.appOpMode = AppOpsManager.MODE_DEFAULT; 71 72 permissionState.staticPermissionGranted = true; 73 Truth.assertThat(permissionState.isPermissible()).isTrue(); 74 75 permissionState.staticPermissionGranted = false; 76 Truth.assertThat(permissionState.isPermissible()).isFalse(); 77 } 78 79 @Test permissionState_modeErrored_IsPermissible()80 public void permissionState_modeErrored_IsPermissible() { 81 AppStateAppOpsBridge.PermissionState permissionState = 82 new AppStateAppOpsBridge.PermissionState("pkg1", UserHandle.of(123)); 83 permissionState.appOpMode = AppOpsManager.MODE_ERRORED; 84 85 permissionState.staticPermissionGranted = true; 86 Truth.assertThat(permissionState.isPermissible()).isFalse(); 87 88 permissionState.staticPermissionGranted = false; 89 Truth.assertThat(permissionState.isPermissible()).isFalse(); 90 } 91 92 @Test permissionState_modeAllowed_IsPermissible()93 public void permissionState_modeAllowed_IsPermissible() { 94 AppStateAppOpsBridge.PermissionState permissionState = 95 new AppStateAppOpsBridge.PermissionState("pkg1", UserHandle.of(123)); 96 permissionState.appOpMode = AppOpsManager.MODE_ALLOWED; 97 98 permissionState.staticPermissionGranted = true; 99 Truth.assertThat(permissionState.isPermissible()).isTrue(); 100 101 permissionState.staticPermissionGranted = false; 102 Truth.assertThat(permissionState.isPermissible()).isTrue(); 103 } 104 105 @Test permissionState_modeIgnored_IsPermissible()106 public void permissionState_modeIgnored_IsPermissible() { 107 AppStateAppOpsBridge.PermissionState permissionState = 108 new AppStateAppOpsBridge.PermissionState("pkg1", UserHandle.of(123)); 109 permissionState.appOpMode = AppOpsManager.MODE_IGNORED; 110 111 permissionState.staticPermissionGranted = true; 112 Truth.assertThat(permissionState.isPermissible()).isFalse(); 113 114 permissionState.staticPermissionGranted = false; 115 Truth.assertThat(permissionState.isPermissible()).isFalse(); 116 } 117 118 private class TestAppStateAppOpsBridge extends AppStateAppOpsBridge { TestAppStateAppOpsBridge()119 private TestAppStateAppOpsBridge() { 120 super(mContext, null, null, AppOpsManager.OP_SYSTEM_ALERT_WINDOW, 121 new String[] {Manifest.permission.SYSTEM_ALERT_WINDOW}, 122 mPackageManagerService); 123 } 124 125 @Override updateExtraInfo(AppEntry app, String pkg, int uid)126 protected void updateExtraInfo(AppEntry app, String pkg, int uid) { 127 } 128 } 129 } 130