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.Matchers.anyInt; 20 import static org.mockito.Matchers.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.os.RemoteException; 27 import android.os.UserManager; 28 import com.android.settings.testutils.SettingsRobolectricTestRunner; 29 import com.android.settings.TestConfig; 30 import com.android.settingslib.applications.ApplicationsState.AppEntry; 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.annotation.Config; 37 38 @RunWith(SettingsRobolectricTestRunner.class) 39 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 40 public final class AppStateAppOpsBridgeTest { 41 42 @Mock private Context mContext; 43 @Mock private UserManager mUserManager; 44 @Mock private IPackageManagerWrapper mPackageManagerService; 45 @Mock private AppOpsManager mAppOpsManager; 46 47 @Before setUp()48 public void setUp() { 49 MockitoAnnotations.initMocks(this); 50 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 51 when(mContext.getSystemService(Context.APP_OPS_SERVICE)).thenReturn(mAppOpsManager); 52 } 53 54 @Test getPermissionInfo_nullPackageInfo_shouldNotCrash()55 public void getPermissionInfo_nullPackageInfo_shouldNotCrash() throws RemoteException { 56 when(mPackageManagerService.getPackageInfo(anyString(), anyInt(), anyInt())) 57 .thenReturn(null); 58 TestAppStateAppOpsBridge appStateAppOpsBridge = new TestAppStateAppOpsBridge(); 59 60 appStateAppOpsBridge.getPermissionInfo("pkg1", 1); 61 // should not crash 62 } 63 64 private class TestAppStateAppOpsBridge extends AppStateAppOpsBridge { TestAppStateAppOpsBridge()65 public TestAppStateAppOpsBridge() { 66 super(mContext, null, null, AppOpsManager.OP_SYSTEM_ALERT_WINDOW, 67 new String[] {Manifest.permission.SYSTEM_ALERT_WINDOW}, 68 mPackageManagerService); 69 } 70 71 @Override updateExtraInfo(AppEntry app, String pkg, int uid)72 protected void updateExtraInfo(AppEntry app, String pkg, int uid) { 73 } 74 } 75 } 76