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.isAlwaysOnSupportedByApp;
20 import static com.android.settings.vpn2.AppManagementFragment.appHasVpnPermission;
21 import static org.mockito.Mockito.*;
22 
23 import android.app.AppOpsManager;
24 import android.content.pm.ApplicationInfo;
25 import android.os.Build;
26 import android.os.Process;
27 import android.test.AndroidTestCase;
28 import android.test.suitebuilder.annotation.SmallTest;
29 import android.content.Context;
30 
31 import java.util.Arrays;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
35 
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
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
testAlwaysOnVersionRestriction()52     public void testAlwaysOnVersionRestriction() {
53         ApplicationInfo mockApp = createMockApp();
54 
55         // API 23 (MNC) = not supported
56         mockApp.targetSdkVersion = Build.VERSION_CODES.M;
57         assertFalse(isAlwaysOnSupportedByApp(mockApp));
58 
59         // API 24 (NYC) = supported
60         mockApp.targetSdkVersion = Build.VERSION_CODES.N;
61         assertTrue(isAlwaysOnSupportedByApp(mockApp));
62 
63         // API 25 (NYC MR1) = supported
64         mockApp.targetSdkVersion = Build.VERSION_CODES.N_MR1;
65         assertTrue(isAlwaysOnSupportedByApp(mockApp));
66     }
67 
68     @SmallTest
testAppOpsRequiredToOpenFragment()69     public void testAppOpsRequiredToOpenFragment() {
70         ApplicationInfo mockApp = createMockApp();
71 
72         final AppOpsManager.PackageOps[] blankOps = {
73             new AppOpsManager.PackageOps(mockApp.packageName, mockApp.uid, new ArrayList<>()),
74             new AppOpsManager.PackageOps(mockApp.packageName, mockApp.uid, new ArrayList<>())
75         };
76 
77         // List with one package op
78         when(mAppOps.getOpsForPackage(eq(mockApp.uid), eq(mockApp.packageName), any()))
79                 .thenReturn(Arrays.asList(new AppOpsManager.PackageOps[] {blankOps[0]}));
80         assertTrue(appHasVpnPermission(mContext, mockApp));
81 
82         // List with more than one package op
83         when(mAppOps.getOpsForPackage(eq(mockApp.uid), eq(mockApp.packageName), any()))
84                 .thenReturn(Arrays.asList(blankOps));
85         assertTrue(appHasVpnPermission(mContext, mockApp));
86 
87         // Empty list
88         when(mAppOps.getOpsForPackage(eq(mockApp.uid), eq(mockApp.packageName), any()))
89                 .thenReturn(Collections.emptyList());
90         assertFalse(appHasVpnPermission(mContext, mockApp));
91 
92         // Null list (may be returned in place of an empty list)
93         when(mAppOps.getOpsForPackage(eq(mockApp.uid), eq(mockApp.packageName), any()))
94                 .thenReturn(null);
95         assertFalse(appHasVpnPermission(mContext, mockApp));
96     }
97 
createMockApp()98     private static ApplicationInfo createMockApp() {
99         final ApplicationInfo app = new ApplicationInfo();
100         app.packageName = "com.example.mockvpn";
101         app.uid = Process.FIRST_APPLICATION_UID;
102         return app;
103     }
104 }
105