RESTRICT AUTOMERGE Implement advanced vpn ui in vpn settings
Bug: 220684478 Test: atest -c VpnSettingsTest Change-Id: If89de16d02cb8b1f387b8f388f8fcf82ad39066b
This commit is contained in:
@@ -617,4 +617,7 @@
|
||||
|
||||
<!-- Whether to put the apps with system UID into system component bucket or not -->
|
||||
<bool name="config_battery_combine_system_components">false</bool>
|
||||
|
||||
<!-- Whether to enable the advanced vpn feature. The default is not to. -->
|
||||
<bool name="config_advanced_vpn_enabled">false</bool>
|
||||
</resources>
|
||||
|
@@ -16,4 +16,9 @@
|
||||
|
||||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:title="@string/vpn_title">
|
||||
<PreferenceCategory
|
||||
android:key="advanced_vpn_group"/>
|
||||
|
||||
<PreferenceCategory
|
||||
android:key="vpn_group"/>
|
||||
</PreferenceScreen>
|
||||
|
@@ -45,6 +45,7 @@ import com.android.settings.security.SecurityFeatureProvider;
|
||||
import com.android.settings.security.SecuritySettingsFeatureProvider;
|
||||
import com.android.settings.slices.SlicesFeatureProvider;
|
||||
import com.android.settings.users.UserFeatureProvider;
|
||||
import com.android.settings.vpn2.AdvancedVpnFeatureProvider;
|
||||
import com.android.settings.wifi.WifiTrackerLibProvider;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
|
||||
@@ -177,6 +178,11 @@ public abstract class FeatureFactory {
|
||||
*/
|
||||
public abstract AccessibilityMetricsFeatureProvider getAccessibilityMetricsFeatureProvider();
|
||||
|
||||
/**
|
||||
* Retrieves implementation for advanced vpn feature.
|
||||
*/
|
||||
public abstract AdvancedVpnFeatureProvider getAdvancedVpnFeatureProvider();
|
||||
|
||||
public static final class FactoryNotFoundException extends RuntimeException {
|
||||
public FactoryNotFoundException(Throwable throwable) {
|
||||
super("Unable to create factory. Did you misconfigure Proguard?", throwable);
|
||||
|
@@ -71,6 +71,8 @@ import com.android.settings.slices.SlicesFeatureProvider;
|
||||
import com.android.settings.slices.SlicesFeatureProviderImpl;
|
||||
import com.android.settings.users.UserFeatureProvider;
|
||||
import com.android.settings.users.UserFeatureProviderImpl;
|
||||
import com.android.settings.vpn2.AdvancedVpnFeatureProvider;
|
||||
import com.android.settings.vpn2.AdvancedVpnFeatureProviderImpl;
|
||||
import com.android.settings.wifi.WifiTrackerLibProvider;
|
||||
import com.android.settings.wifi.WifiTrackerLibProviderImpl;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
@@ -106,6 +108,7 @@ public class FeatureFactoryImpl extends FeatureFactory {
|
||||
private SecuritySettingsFeatureProvider mSecuritySettingsFeatureProvider;
|
||||
private AccessibilitySearchFeatureProvider mAccessibilitySearchFeatureProvider;
|
||||
private AccessibilityMetricsFeatureProvider mAccessibilityMetricsFeatureProvider;
|
||||
private AdvancedVpnFeatureProvider mAdvancedVpnFeatureProvider;
|
||||
|
||||
@Override
|
||||
public SupportFeatureProvider getSupportFeatureProvider(Context context) {
|
||||
@@ -334,4 +337,12 @@ public class FeatureFactoryImpl extends FeatureFactory {
|
||||
}
|
||||
return mAccessibilityMetricsFeatureProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AdvancedVpnFeatureProvider getAdvancedVpnFeatureProvider() {
|
||||
if (mAdvancedVpnFeatureProvider == null) {
|
||||
mAdvancedVpnFeatureProvider = new AdvancedVpnFeatureProviderImpl();
|
||||
}
|
||||
return mAdvancedVpnFeatureProvider;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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 android.content.Context;
|
||||
|
||||
/**
|
||||
* Feature Provider used in vpn usage
|
||||
*/
|
||||
public interface AdvancedVpnFeatureProvider {
|
||||
|
||||
/**
|
||||
* Returns package name of advanced vpn.
|
||||
*/
|
||||
String getAdvancedVpnPackageName();
|
||||
|
||||
/**
|
||||
* Returns {@code true} advanced vpn is supported.
|
||||
*/
|
||||
boolean isAdvancedVpnSupported(Context context);
|
||||
|
||||
/**
|
||||
* Returns the title of advanced vpn preference group.
|
||||
*/
|
||||
String getAdvancedVpnPreferenceGroupTitle(Context context);
|
||||
|
||||
/**
|
||||
* Returns the title of vpn preference group.
|
||||
*/
|
||||
String getVpnPreferenceGroupTitle(Context context);
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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 android.content.Context;
|
||||
|
||||
/**
|
||||
* Feature provider implementation for advanced vpn.
|
||||
*/
|
||||
public class AdvancedVpnFeatureProviderImpl implements AdvancedVpnFeatureProvider {
|
||||
@Override
|
||||
public String getAdvancedVpnPackageName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAdvancedVpnSupported(Context context) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAdvancedVpnPreferenceGroupTitle(Context context) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getVpnPreferenceGroupTitle(Context context) {
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -42,6 +42,7 @@ import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.security.Credentials;
|
||||
import android.security.LegacyVpnProfileStore;
|
||||
import android.text.TextUtils;
|
||||
import android.util.ArrayMap;
|
||||
import android.util.ArraySet;
|
||||
import android.util.Log;
|
||||
@@ -52,6 +53,7 @@ import android.view.MenuItem;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceGroup;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.internal.annotations.GuardedBy;
|
||||
import com.android.internal.net.LegacyVpnInfo;
|
||||
@@ -59,6 +61,7 @@ import com.android.internal.net.VpnConfig;
|
||||
import com.android.internal.net.VpnProfile;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.RestrictedSettingsFragment;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settings.widget.GearPreference;
|
||||
import com.android.settings.widget.GearPreference.OnGearClickListener;
|
||||
import com.android.settingslib.RestrictedLockUtilsInternal;
|
||||
@@ -79,9 +82,12 @@ import java.util.Set;
|
||||
public class VpnSettings extends RestrictedSettingsFragment implements
|
||||
Handler.Callback, Preference.OnPreferenceClickListener {
|
||||
private static final String LOG_TAG = "VpnSettings";
|
||||
private static final boolean DEBUG = Log.isLoggable(LOG_TAG, Log.DEBUG);
|
||||
|
||||
private static final int RESCAN_MESSAGE = 0;
|
||||
private static final int RESCAN_INTERVAL_MS = 1000;
|
||||
private static final String ADVANCED_VPN_GROUP_KEY = "advanced_vpn_group";
|
||||
private static final String VPN_GROUP_KEY = "vpn_group";
|
||||
|
||||
private static final NetworkRequest VPN_REQUEST = new NetworkRequest.Builder()
|
||||
.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)
|
||||
@@ -102,6 +108,9 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
||||
private LegacyVpnInfo mConnectedLegacyVpn;
|
||||
|
||||
private boolean mUnavailable;
|
||||
private AdvancedVpnFeatureProvider mFeatureProvider;
|
||||
private PreferenceScreen mPreferenceScreen;
|
||||
private boolean mIsAdvancedVpnSupported;
|
||||
|
||||
public VpnSettings() {
|
||||
super(UserManager.DISALLOW_CONFIG_VPN);
|
||||
@@ -119,11 +128,14 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
||||
mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
|
||||
mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
mVpnManager = (VpnManager) getSystemService(Context.VPN_MANAGEMENT_SERVICE);
|
||||
mFeatureProvider = FeatureFactory.getFactory(getContext()).getAdvancedVpnFeatureProvider();
|
||||
mIsAdvancedVpnSupported = mFeatureProvider.isAdvancedVpnSupported(getContext());
|
||||
|
||||
mUnavailable = isUiRestricted();
|
||||
setHasOptionsMenu(!mUnavailable);
|
||||
|
||||
addPreferencesFromResource(R.xml.vpn_settings2);
|
||||
mPreferenceScreen = getPreferenceScreen();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -265,7 +277,7 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
||||
|
||||
private final VpnSettings mSettings;
|
||||
|
||||
public UpdatePreferences(VpnSettings settings) {
|
||||
UpdatePreferences(VpnSettings settings) {
|
||||
mSettings = settings;
|
||||
}
|
||||
|
||||
@@ -332,7 +344,14 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
||||
}
|
||||
|
||||
// Trim out deleted VPN preferences
|
||||
mSettings.setShownPreferences(updates);
|
||||
if (DEBUG) {
|
||||
Log.d(LOG_TAG, "isAdvancedVpnSupported() : " + mSettings.mIsAdvancedVpnSupported);
|
||||
}
|
||||
if (mSettings.mIsAdvancedVpnSupported) {
|
||||
mSettings.setShownAdvancedPreferences(updates);
|
||||
} else {
|
||||
mSettings.setShownPreferences(updates);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -343,12 +362,61 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
||||
|
||||
@VisibleForTesting @UiThread
|
||||
public void setShownPreferences(final Collection<Preference> updates) {
|
||||
retainAllPreference(updates);
|
||||
|
||||
final PreferenceGroup vpnGroup = getPreferenceScreen();
|
||||
updatePreferenceGroup(vpnGroup, updates);
|
||||
|
||||
// Show all new preferences on the screen
|
||||
for (Preference pref : updates) {
|
||||
vpnGroup.addPreference(pref);
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting @UiThread
|
||||
void setShownAdvancedPreferences(final Collection<Preference> updates) {
|
||||
retainAllPreference(updates);
|
||||
|
||||
PreferenceGroup advancedVpnGroup = mPreferenceScreen.findPreference(ADVANCED_VPN_GROUP_KEY);
|
||||
PreferenceGroup vpnGroup = mPreferenceScreen.findPreference(VPN_GROUP_KEY);
|
||||
advancedVpnGroup.setTitle(
|
||||
mFeatureProvider.getAdvancedVpnPreferenceGroupTitle(getContext()));
|
||||
vpnGroup.setTitle(mFeatureProvider.getVpnPreferenceGroupTitle(getContext()));
|
||||
updatePreferenceGroup(advancedVpnGroup, updates);
|
||||
updatePreferenceGroup(vpnGroup, updates);
|
||||
|
||||
// Show all new preferences on the screen
|
||||
for (Preference pref : updates) {
|
||||
String packageName = "";
|
||||
if (pref instanceof LegacyVpnPreference) {
|
||||
LegacyVpnPreference legacyPref = (LegacyVpnPreference) pref;
|
||||
packageName = legacyPref.getPackageName();
|
||||
} else if (pref instanceof AppPreference) {
|
||||
AppPreference appPref = (AppPreference) pref;
|
||||
packageName = appPref.getPackageName();
|
||||
}
|
||||
if (DEBUG) {
|
||||
Log.d(LOG_TAG, "setShownAdvancedPreferences() package name : " + packageName);
|
||||
}
|
||||
if (TextUtils.equals(packageName, mFeatureProvider.getAdvancedVpnPackageName())) {
|
||||
advancedVpnGroup.addPreference(pref);
|
||||
} else {
|
||||
vpnGroup.addPreference(pref);
|
||||
}
|
||||
}
|
||||
|
||||
advancedVpnGroup.setVisible(advancedVpnGroup.getPreferenceCount() > 0);
|
||||
vpnGroup.setVisible(vpnGroup.getPreferenceCount() > 0);
|
||||
}
|
||||
|
||||
private void retainAllPreference(Collection<Preference> updates) {
|
||||
mLegacyVpnPreferences.values().retainAll(updates);
|
||||
mAppPreferences.values().retainAll(updates);
|
||||
}
|
||||
|
||||
private void updatePreferenceGroup(PreferenceGroup vpnGroup, Collection<Preference> updates) {
|
||||
// Change {@param updates} in-place to only contain new preferences that were not already
|
||||
// added to the preference screen.
|
||||
final PreferenceGroup vpnGroup = getPreferenceScreen();
|
||||
for (int i = vpnGroup.getPreferenceCount() - 1; i >= 0; i--) {
|
||||
Preference p = vpnGroup.getPreference(i);
|
||||
if (updates.contains(p)) {
|
||||
@@ -357,11 +425,6 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
||||
vpnGroup.removePreference(p);
|
||||
}
|
||||
}
|
||||
|
||||
// Show any new preferences on the screen
|
||||
for (Preference pref : updates) {
|
||||
vpnGroup.addPreference(pref);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -562,4 +625,10 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void init(PreferenceScreen preferenceScreen, AdvancedVpnFeatureProvider featureProvider) {
|
||||
mPreferenceScreen = preferenceScreen;
|
||||
mFeatureProvider = featureProvider;
|
||||
}
|
||||
}
|
||||
|
@@ -47,6 +47,7 @@ import com.android.settings.security.SecurityFeatureProvider;
|
||||
import com.android.settings.security.SecuritySettingsFeatureProvider;
|
||||
import com.android.settings.slices.SlicesFeatureProvider;
|
||||
import com.android.settings.users.UserFeatureProvider;
|
||||
import com.android.settings.vpn2.AdvancedVpnFeatureProvider;
|
||||
import com.android.settings.wifi.WifiTrackerLibProvider;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
|
||||
@@ -87,6 +88,7 @@ public class FakeFeatureFactory extends FeatureFactory {
|
||||
public SecuritySettingsFeatureProvider securitySettingsFeatureProvider;
|
||||
public AccessibilitySearchFeatureProvider mAccessibilitySearchFeatureProvider;
|
||||
public AccessibilityMetricsFeatureProvider mAccessibilityMetricsFeatureProvider;
|
||||
public AdvancedVpnFeatureProvider mAdvancedVpnFeatureProvider;
|
||||
|
||||
/**
|
||||
* Call this in {@code @Before} method of the test class to use fake factory.
|
||||
@@ -136,6 +138,7 @@ public class FakeFeatureFactory extends FeatureFactory {
|
||||
securitySettingsFeatureProvider = mock(SecuritySettingsFeatureProvider.class);
|
||||
mAccessibilitySearchFeatureProvider = mock(AccessibilitySearchFeatureProvider.class);
|
||||
mAccessibilityMetricsFeatureProvider = mock(AccessibilityMetricsFeatureProvider.class);
|
||||
mAdvancedVpnFeatureProvider = mock(AdvancedVpnFeatureProvider.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -272,4 +275,9 @@ public class FakeFeatureFactory extends FeatureFactory {
|
||||
public AccessibilityMetricsFeatureProvider getAccessibilityMetricsFeatureProvider() {
|
||||
return mAccessibilityMetricsFeatureProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AdvancedVpnFeatureProvider getAdvancedVpnFeatureProvider() {
|
||||
return mAdvancedVpnFeatureProvider;
|
||||
}
|
||||
}
|
||||
|
@@ -45,6 +45,7 @@ import com.android.settings.security.SecurityFeatureProvider;
|
||||
import com.android.settings.security.SecuritySettingsFeatureProvider;
|
||||
import com.android.settings.slices.SlicesFeatureProvider;
|
||||
import com.android.settings.users.UserFeatureProvider;
|
||||
import com.android.settings.vpn2.AdvancedVpnFeatureProvider;
|
||||
import com.android.settings.wifi.WifiTrackerLibProvider;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
|
||||
@@ -82,6 +83,7 @@ public class FakeFeatureFactory extends FeatureFactory {
|
||||
public SecuritySettingsFeatureProvider securitySettingsFeatureProvider;
|
||||
public AccessibilitySearchFeatureProvider mAccessibilitySearchFeatureProvider;
|
||||
public AccessibilityMetricsFeatureProvider mAccessibilityMetricsFeatureProvider;
|
||||
public AdvancedVpnFeatureProvider mAdvancedVpnFeatureProvider;
|
||||
|
||||
/**
|
||||
* Call this in {@code @Before} method of the test class to use fake factory.
|
||||
@@ -122,6 +124,7 @@ public class FakeFeatureFactory extends FeatureFactory {
|
||||
securitySettingsFeatureProvider = mock(SecuritySettingsFeatureProvider.class);
|
||||
mAccessibilitySearchFeatureProvider = mock(AccessibilitySearchFeatureProvider.class);
|
||||
mAccessibilityMetricsFeatureProvider = mock(AccessibilityMetricsFeatureProvider.class);
|
||||
mAdvancedVpnFeatureProvider = mock(AdvancedVpnFeatureProvider.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -258,4 +261,9 @@ public class FakeFeatureFactory extends FeatureFactory {
|
||||
public AccessibilityMetricsFeatureProvider getAccessibilityMetricsFeatureProvider() {
|
||||
return mAccessibilityMetricsFeatureProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AdvancedVpnFeatureProvider getAdvancedVpnFeatureProvider() {
|
||||
return mAdvancedVpnFeatureProvider;
|
||||
}
|
||||
}
|
||||
|
146
tests/unit/src/com/android/settings/vpn2/VpnSettingsTest.java
Normal file
146
tests/unit/src/com/android/settings/vpn2/VpnSettingsTest.java
Normal file
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* 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.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Looper;
|
||||
import android.os.UserHandle;
|
||||
import android.util.ArraySet;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceGroup;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
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 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;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class VpnSettingsTest {
|
||||
private static final String ADVANCED_VPN_GROUP_KEY = "advanced_vpn_group";
|
||||
private static final String VPN_GROUP_KEY = "vpn_group";
|
||||
private static final String ADVANCED_VPN_GROUP_TITLE = "advanced_vpn_group_title";
|
||||
private static final String VPN_GROUP_TITLE = "vpn_group_title";
|
||||
private static final String FAKE_PACKAGE_NAME = "com.fake.package.name";
|
||||
private static final String ADVANCED_VPN_GROUP_PACKAGE_NAME = "com.advanced.package.name";
|
||||
private static final int USER_ID_1 = UserHandle.USER_NULL;
|
||||
|
||||
@Rule
|
||||
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
|
||||
|
||||
private VpnSettings mVpnSettings;
|
||||
private Context mContext;
|
||||
private PreferenceManager mPreferenceManager;
|
||||
private PreferenceScreen mPreferenceScreen;
|
||||
private PreferenceGroup mAdvancedVpnGroup;
|
||||
private PreferenceGroup mVpnGroup;
|
||||
private FakeFeatureFactory mFakeFeatureFactory;
|
||||
|
||||
@Before
|
||||
@UiThreadTest
|
||||
public void setUp() throws PackageManager.NameNotFoundException {
|
||||
if (Looper.myLooper() == null) {
|
||||
Looper.prepare();
|
||||
}
|
||||
|
||||
mVpnSettings = spy(new VpnSettings());
|
||||
mContext = spy(ApplicationProvider.getApplicationContext());
|
||||
mAdvancedVpnGroup = spy(new PreferenceCategory(mContext));
|
||||
mVpnGroup = spy(new PreferenceCategory(mContext));
|
||||
mAdvancedVpnGroup.setKey(ADVANCED_VPN_GROUP_KEY);
|
||||
mVpnGroup.setKey(VPN_GROUP_KEY);
|
||||
mPreferenceManager = new PreferenceManager(mContext);
|
||||
mPreferenceScreen = mPreferenceManager.createPreferenceScreen(mContext);
|
||||
mPreferenceScreen.addPreference(mAdvancedVpnGroup);
|
||||
mPreferenceScreen.addPreference(mVpnGroup);
|
||||
mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||
mVpnSettings.init(mPreferenceScreen, mFakeFeatureFactory.getAdvancedVpnFeatureProvider());
|
||||
|
||||
when(mVpnSettings.getContext()).thenReturn(mContext);
|
||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider
|
||||
.getAdvancedVpnPreferenceGroupTitle(mContext)).thenReturn(ADVANCED_VPN_GROUP_TITLE);
|
||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.getVpnPreferenceGroupTitle(mContext))
|
||||
.thenReturn(VPN_GROUP_TITLE);
|
||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.getAdvancedVpnPackageName())
|
||||
.thenReturn(ADVANCED_VPN_GROUP_PACKAGE_NAME);
|
||||
doReturn(mContext).when(mContext).createContextAsUser(any(), anyInt());
|
||||
doReturn(mContext).when(mContext).createPackageContextAsUser(any(), anyInt(), any());
|
||||
doReturn(mPreferenceManager).when(mVpnGroup).getPreferenceManager();
|
||||
doReturn(mPreferenceManager).when(mAdvancedVpnGroup).getPreferenceManager();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setShownAdvancedPreferences_hasGeneralVpn_returnsVpnCountAs1() {
|
||||
Set<Preference> updates = new ArraySet<>();
|
||||
AppPreference pref =
|
||||
spy(new AppPreference(mContext, USER_ID_1, FAKE_PACKAGE_NAME));
|
||||
updates.add(pref);
|
||||
|
||||
mVpnSettings.setShownAdvancedPreferences(updates);
|
||||
|
||||
assertThat(mVpnGroup.getPreferenceCount()).isEqualTo(1);
|
||||
assertThat(mVpnGroup.isVisible()).isTrue();
|
||||
assertThat(mAdvancedVpnGroup.isVisible()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setShownAdvancedPreferences_hasAdvancedVpn_returnsAdvancedVpnCountAs1() {
|
||||
Set<Preference> updates = new ArraySet<>();
|
||||
AppPreference pref =
|
||||
spy(new AppPreference(mContext, USER_ID_1, ADVANCED_VPN_GROUP_PACKAGE_NAME));
|
||||
updates.add(pref);
|
||||
|
||||
mVpnSettings.setShownAdvancedPreferences(updates);
|
||||
|
||||
assertThat(mAdvancedVpnGroup.getPreferenceCount()).isEqualTo(1);
|
||||
assertThat(mAdvancedVpnGroup.isVisible()).isTrue();
|
||||
assertThat(mVpnGroup.isVisible()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setShownAdvancedPreferences_noVpn_returnsEmpty() {
|
||||
Set<Preference> updates = new ArraySet<>();
|
||||
|
||||
mVpnSettings.setShownAdvancedPreferences(updates);
|
||||
|
||||
assertThat(mAdvancedVpnGroup.getPreferenceCount()).isEqualTo(0);
|
||||
assertThat(mVpnGroup.getPreferenceCount()).isEqualTo(0);
|
||||
assertThat(mAdvancedVpnGroup.isVisible()).isFalse();
|
||||
assertThat(mVpnGroup.isVisible()).isFalse();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user