Home | History | Annotate | Download | only in applications
      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.content.pm.IPackageManager;
     27 import android.os.RemoteException;
     28 import android.os.UserManager;
     29 
     30 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     31 import com.android.settingslib.applications.ApplicationsState.AppEntry;
     32 
     33 import org.junit.Before;
     34 import org.junit.Test;
     35 import org.junit.runner.RunWith;
     36 import org.mockito.Mock;
     37 import org.mockito.MockitoAnnotations;
     38 
     39 @RunWith(SettingsRobolectricTestRunner.class)
     40 public final class AppStateAppOpsBridgeTest {
     41 
     42     @Mock private Context mContext;
     43     @Mock private UserManager mUserManager;
     44     @Mock private IPackageManager mPackageManagerService;
     45     @Mock private AppOpsManager mAppOpsManager;
     46 
     47     @Before
     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
     55     public void getPermissionInfo_nullPackageInfo_shouldNotCrash() throws RemoteException {
     56         when(mPackageManagerService.getPackageInfo(anyString(), anyInt(), anyInt()))
     57             .thenReturn(null);
     58 
     59         new TestAppStateAppOpsBridge().getPermissionInfo("pkg1", 1);
     60         // should not crash
     61     }
     62 
     63     private class TestAppStateAppOpsBridge extends AppStateAppOpsBridge {
     64         public TestAppStateAppOpsBridge() {
     65             super(mContext, null, null, AppOpsManager.OP_SYSTEM_ALERT_WINDOW,
     66                 new String[] {Manifest.permission.SYSTEM_ALERT_WINDOW},
     67                 mPackageManagerService);
     68         }
     69 
     70         @Override
     71         protected void updateExtraInfo(AppEntry app, String pkg, int uid) {
     72         }
     73     }
     74 }
     75