1 /* 2 * Copyright (C) 2016 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.vpn2; 18 19 import static com.android.settings.vpn2.AppManagementFragment.appHasVpnPermission; 20 21 import static org.mockito.Mockito.any; 22 import static org.mockito.Mockito.eq; 23 import static org.mockito.Mockito.when; 24 25 import android.app.AppOpsManager; 26 import android.content.Context; 27 import android.content.pm.ApplicationInfo; 28 import android.os.Process; 29 import android.test.AndroidTestCase; 30 import android.test.suitebuilder.annotation.SmallTest; 31 32 import org.mockito.Mock; 33 import org.mockito.MockitoAnnotations; 34 35 import java.util.ArrayList; 36 import java.util.Arrays; 37 import java.util.Collections; 38 39 public class AppSettingsTest extends AndroidTestCase { 40 private static final String TAG = AppSettingsTest.class.getSimpleName(); 41 42 @Mock private Context mContext; 43 @Mock private AppOpsManager mAppOps; 44 45 @Override setUp()46 public void setUp() throws Exception { 47 MockitoAnnotations.initMocks(this); 48 when(mContext.getSystemService(eq(Context.APP_OPS_SERVICE))).thenReturn(mAppOps); 49 } 50 51 @SmallTest testAppOpsRequiredToOpenFragment()52 public void testAppOpsRequiredToOpenFragment() { 53 ApplicationInfo mockApp = createMockApp(); 54 55 final AppOpsManager.PackageOps[] blankOps = { 56 new AppOpsManager.PackageOps(mockApp.packageName, mockApp.uid, new ArrayList<>()), 57 new AppOpsManager.PackageOps(mockApp.packageName, mockApp.uid, new ArrayList<>()) 58 }; 59 60 // List with one package op 61 when(mAppOps.getOpsForPackage(eq(mockApp.uid), eq(mockApp.packageName), any())) 62 .thenReturn(Arrays.asList(new AppOpsManager.PackageOps[] {blankOps[0]})); 63 assertTrue(appHasVpnPermission(mContext, mockApp)); 64 65 // List with more than one package op 66 when(mAppOps.getOpsForPackage(eq(mockApp.uid), eq(mockApp.packageName), any())) 67 .thenReturn(Arrays.asList(blankOps)); 68 assertTrue(appHasVpnPermission(mContext, mockApp)); 69 70 // Empty list 71 when(mAppOps.getOpsForPackage(eq(mockApp.uid), eq(mockApp.packageName), any())) 72 .thenReturn(Collections.emptyList()); 73 assertFalse(appHasVpnPermission(mContext, mockApp)); 74 75 // Null list (may be returned in place of an empty list) 76 when(mAppOps.getOpsForPackage(eq(mockApp.uid), eq(mockApp.packageName), any())) 77 .thenReturn(null); 78 assertFalse(appHasVpnPermission(mContext, mockApp)); 79 } 80 createMockApp()81 private static ApplicationInfo createMockApp() { 82 final ApplicationInfo app = new ApplicationInfo(); 83 app.packageName = "com.example.mockvpn"; 84 app.uid = Process.FIRST_APPLICATION_UID; 85 return app; 86 } 87 } 88