1 /*
2  * Copyright (C) 2021 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.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assert.assertFalse;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.ArgumentMatchers.anyInt;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.when;
27 
28 import android.app.admin.DevicePolicyManager;
29 import android.content.Context;
30 import android.content.pm.PackageManager.NameNotFoundException;
31 import android.os.UserHandle;
32 
33 import androidx.test.core.app.ApplicationProvider;
34 import androidx.test.ext.junit.runners.AndroidJUnit4;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 
42 /** Unittest for AppPreference */
43 @RunWith(AndroidJUnit4.class)
44 public class AppPreferenceTest {
45     // Additional mocking of the underying classes is necsesary if another user id is used.
46     private static final int USER_ID = UserHandle.USER_NULL;
47     private static final String PACKAGE_NAME = "test_package";
48     private static final String DIFFERENT_PACKAGE_NAME = "not_test_package";
49 
50     @Mock
51     private DevicePolicyManager mDevicePolicyManager;
52 
53     private Context mContext;
54     private AppPreference mAppPreference;
55 
56     @Before
setUp()57     public void setUp() throws NameNotFoundException {
58         MockitoAnnotations.initMocks(this);
59 
60         mContext = spy(ApplicationProvider.getApplicationContext());
61         doReturn(mContext).when(mContext).createContextAsUser(any(), anyInt());
62         doReturn(mContext).when(mContext).createPackageContextAsUser(any(), anyInt(), any());
63         when(mContext.getSystemService(DevicePolicyManager.class)).thenReturn(mDevicePolicyManager);
64     }
65 
66     @Test
getPackageName_returnsAccuratePackageName()67     public void getPackageName_returnsAccuratePackageName() {
68         doReturn(DIFFERENT_PACKAGE_NAME).when(mDevicePolicyManager).getAlwaysOnVpnPackage();
69 
70         mAppPreference = spy(new AppPreference(mContext, USER_ID, PACKAGE_NAME));
71         assertThat(mAppPreference.getPackageName()).isEqualTo(PACKAGE_NAME);
72     }
73 
74     @Test
disableIfConfiguredByAdmin_packageNameNotEqualsAlwaysOn_shouldEnable()75     public void disableIfConfiguredByAdmin_packageNameNotEqualsAlwaysOn_shouldEnable() {
76         doReturn(DIFFERENT_PACKAGE_NAME).when(mDevicePolicyManager).getAlwaysOnVpnPackage();
77 
78         mAppPreference = spy(new AppPreference(mContext, USER_ID, PACKAGE_NAME));
79         assertFalse(mAppPreference.isDisabledByAdmin());
80     }
81 }
82