Merge "Make VPN by Google One always appear in VPN settings"
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright (C) 2022 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.vpn2;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Looper;
|
||||
|
||||
import androidx.test.annotation.UiThreadTest;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settingslib.RestrictedPreference;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.junit.MockitoJUnit;
|
||||
import org.mockito.junit.MockitoRule;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class AppManagementFragmentTest {
|
||||
private static final String FAKE_PACKAGE_NAME = "com.fake.package.name";
|
||||
private static final String ADVANCED_VPN_GROUP_PACKAGE_NAME = "com.advanced.package.name";
|
||||
|
||||
@Rule
|
||||
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
|
||||
|
||||
private AppManagementFragment mFragment;
|
||||
private Context mContext;
|
||||
private FakeFeatureFactory mFakeFeatureFactory;
|
||||
private RestrictedPreference mPreferenceForget;
|
||||
|
||||
@Before
|
||||
@UiThreadTest
|
||||
public void setUp() {
|
||||
if (Looper.myLooper() == null) {
|
||||
Looper.prepare();
|
||||
}
|
||||
|
||||
mFragment = spy(new AppManagementFragment());
|
||||
mContext = spy(ApplicationProvider.getApplicationContext());
|
||||
mPreferenceForget = new RestrictedPreference(mContext);
|
||||
|
||||
mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||
mFragment.init(ADVANCED_VPN_GROUP_PACKAGE_NAME,
|
||||
mFakeFeatureFactory.getAdvancedVpnFeatureProvider(), mPreferenceForget);
|
||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.getAdvancedVpnPackageName())
|
||||
.thenReturn(ADVANCED_VPN_GROUP_PACKAGE_NAME);
|
||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.isAdvancedVpnSupported(any()))
|
||||
.thenReturn(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateRestrictedViews_isAdvancedVpn_hidesForgetPreference() {
|
||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.isAdvancedVpnRemovable())
|
||||
.thenReturn(false);
|
||||
mFragment.updateRestrictedViews();
|
||||
assertThat(mPreferenceForget.isVisible()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateRestrictedViews_isNotAdvancedVpn_showsForgetPreference() {
|
||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.isAdvancedVpnRemovable())
|
||||
.thenReturn(false);
|
||||
mFragment.init(FAKE_PACKAGE_NAME,
|
||||
mFakeFeatureFactory.getAdvancedVpnFeatureProvider(), mPreferenceForget);
|
||||
mFragment.updateRestrictedViews();
|
||||
assertThat(mPreferenceForget.isVisible()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateRestrictedViews_isAdvancedVpnRemovable_showsForgetPreference() {
|
||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.isAdvancedVpnRemovable())
|
||||
.thenReturn(true);
|
||||
mFragment.init(FAKE_PACKAGE_NAME,
|
||||
mFakeFeatureFactory.getAdvancedVpnFeatureProvider(), mPreferenceForget);
|
||||
mFragment.updateRestrictedViews();
|
||||
assertThat(mPreferenceForget.isVisible()).isTrue();
|
||||
}
|
||||
}
|
@@ -24,6 +24,7 @@ import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.AppOpsManager;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Looper;
|
||||
@@ -45,9 +46,12 @@ import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnit;
|
||||
import org.mockito.junit.MockitoRule;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@@ -63,6 +67,9 @@ public class VpnSettingsTest {
|
||||
@Rule
|
||||
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
|
||||
|
||||
@Mock
|
||||
private AppOpsManager mAppOpsManager;
|
||||
|
||||
private VpnSettings mVpnSettings;
|
||||
private Context mContext;
|
||||
private PreferenceManager mPreferenceManager;
|
||||
@@ -98,6 +105,8 @@ public class VpnSettingsTest {
|
||||
.thenReturn(VPN_GROUP_TITLE);
|
||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.getAdvancedVpnPackageName())
|
||||
.thenReturn(ADVANCED_VPN_GROUP_PACKAGE_NAME);
|
||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.isAdvancedVpnSupported(any()))
|
||||
.thenReturn(true);
|
||||
doReturn(mContext).when(mContext).createContextAsUser(any(), anyInt());
|
||||
doReturn(mContext).when(mContext).createPackageContextAsUser(any(), anyInt(), any());
|
||||
doReturn(mPreferenceManager).when(mVpnGroup).getPreferenceManager();
|
||||
@@ -143,4 +152,34 @@ public class VpnSettingsTest {
|
||||
assertThat(mAdvancedVpnGroup.isVisible()).isFalse();
|
||||
assertThat(mVpnGroup.isVisible()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getVpnApps_isAdvancedVpn_returnsOne() {
|
||||
int uid = 1111;
|
||||
List<AppOpsManager.OpEntry> opEntries = new ArrayList<>();
|
||||
List<AppOpsManager.PackageOps> apps = new ArrayList<>();
|
||||
AppOpsManager.PackageOps packageOps =
|
||||
new AppOpsManager.PackageOps(ADVANCED_VPN_GROUP_PACKAGE_NAME, uid, opEntries);
|
||||
apps.add(packageOps);
|
||||
when(mAppOpsManager.getPackagesForOps((int[]) any())).thenReturn(apps);
|
||||
|
||||
assertThat(VpnSettings.getVpnApps(mContext, /* includeProfiles= */ false,
|
||||
mFakeFeatureFactory.getAdvancedVpnFeatureProvider(),
|
||||
mAppOpsManager).size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getVpnApps_isNotAdvancedVpn_returnsEmpty() {
|
||||
int uid = 1111;
|
||||
List<AppOpsManager.OpEntry> opEntries = new ArrayList<>();
|
||||
List<AppOpsManager.PackageOps> apps = new ArrayList<>();
|
||||
AppOpsManager.PackageOps packageOps =
|
||||
new AppOpsManager.PackageOps(FAKE_PACKAGE_NAME, uid, opEntries);
|
||||
apps.add(packageOps);
|
||||
when(mAppOpsManager.getPackagesForOps((int[]) any())).thenReturn(apps);
|
||||
|
||||
assertThat(VpnSettings.getVpnApps(mContext, /* includeProfiles= */ false,
|
||||
mFakeFeatureFactory.getAdvancedVpnFeatureProvider(),
|
||||
mAppOpsManager)).isEmpty();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user