Add Always on VPN to Privacy Settings page

This CL adds information about always-on VPNs to the Enterprise
Privacy Settings page.

Test: make RunSettingsRoboTests
Bug: 32692748

Change-Id: I2b59e2ec4c55308b323aaa478cd9c847fe0b4b55
This commit is contained in:
Bartosz Fabianowski
2017-01-09 12:05:59 +01:00
parent 78427d7467
commit fc018e4672
16 changed files with 552 additions and 5 deletions

View File

@@ -0,0 +1,89 @@
/*
* Copyright (C) 2017 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.enterprise;
import android.content.Context;
import android.support.v7.preference.Preference;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
/**
* Tests for {@link AlwaysOnVpnManagedProfilePreferenceController}.
*/
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public final class AlwaysOnVpnManagedProfilePreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
private FakeFeatureFactory mFeatureFactory;
private AlwaysOnVpnManagedProfilePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
FakeFeatureFactory.setupForTest(mContext);
mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
mController = new AlwaysOnVpnManagedProfilePreferenceController(mContext);
}
@Test
public void testUpdateState() {
final Preference preference = new Preference(mContext, null, 0, 0);
preference.setVisible(true);
when(mFeatureFactory.enterprisePrivacyFeatureProvider.isAlwaysOnVpnSetInManagedProfile())
.thenReturn(false);
mController.updateState(preference);
assertThat(preference.isVisible()).isFalse();
when(mFeatureFactory.enterprisePrivacyFeatureProvider.isAlwaysOnVpnSetInManagedProfile())
.thenReturn(true);
mController.updateState(preference);
assertThat(preference.isVisible()).isTrue();
}
@Test
public void testIsAvailable() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void testHandlePreferenceTreeClick() {
assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
.isFalse();
}
@Test
public void testGetPreferenceKey() {
assertThat(mController.getPreferenceKey()).isEqualTo("always_on_vpn_managed_profile");
}
}

View File

@@ -0,0 +1,113 @@
/*
* Copyright (C) 2017 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.enterprise;
import android.content.Context;
import com.android.settings.R;
import android.support.v7.preference.Preference;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
/**
* Tests for {@link AlwaysOnVpnPrimaryUserPreferenceController}.
*/
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public final class AlwaysOnVpnPrimaryUserPreferenceControllerTest {
private final String VPN_SET_DEVICE = "VPN set";
private final String VPN_SET_PERSONAL = "VPN set in personal profile";
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
private FakeFeatureFactory mFeatureFactory;
private AlwaysOnVpnPrimaryUserPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
FakeFeatureFactory.setupForTest(mContext);
mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
mController = new AlwaysOnVpnPrimaryUserPreferenceController(mContext);
when(mContext.getString(R.string.enterprise_privacy_always_on_vpn_device))
.thenReturn(VPN_SET_DEVICE);
when(mContext.getString(R.string.enterprise_privacy_always_on_vpn_personal))
.thenReturn(VPN_SET_PERSONAL);
}
@Test
public void testUpdateState() {
final Preference preference = new Preference(mContext, null, 0, 0);
preference.setVisible(true);
when(mFeatureFactory.enterprisePrivacyFeatureProvider.isInCompMode()).thenReturn(false);
when(mFeatureFactory.enterprisePrivacyFeatureProvider.isAlwaysOnVpnSetInPrimaryUser())
.thenReturn(false);
mController.updateState(preference);
assertThat(preference.isVisible()).isFalse();
when(mFeatureFactory.enterprisePrivacyFeatureProvider.isAlwaysOnVpnSetInPrimaryUser())
.thenReturn(true);
mController.updateState(preference);
assertThat(preference.isVisible()).isTrue();
assertThat(preference.getTitle()).isEqualTo(VPN_SET_DEVICE);
when(mFeatureFactory.enterprisePrivacyFeatureProvider.isInCompMode()).thenReturn(true);
when(mFeatureFactory.enterprisePrivacyFeatureProvider.isAlwaysOnVpnSetInPrimaryUser())
.thenReturn(false);
mController.updateState(preference);
assertThat(preference.isVisible()).isFalse();
when(mFeatureFactory.enterprisePrivacyFeatureProvider.isAlwaysOnVpnSetInPrimaryUser())
.thenReturn(true);
mController.updateState(preference);
assertThat(preference.isVisible()).isTrue();
assertThat(preference.getTitle()).isEqualTo(VPN_SET_PERSONAL);
}
@Test
public void testIsAvailable() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void testHandlePreferenceTreeClick() {
assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
.isFalse();
}
@Test
public void testGetPreferenceKey() {
assertThat(mController.getPreferenceKey()).isEqualTo("always_on_vpn_primary_user");
}
}

View File

@@ -17,12 +17,15 @@
package com.android.settings.enterprise;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.os.UserHandle;
import android.os.UserManager;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.applications.PackageManagerWrapper;
import com.android.settings.vpn2.ConnectivityManagerWrapper;
import org.junit.Before;
import org.junit.Test;
@@ -31,7 +34,9 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
@@ -45,9 +50,16 @@ public final class EnterprisePrivacyFeatureProviderImplTest {
private final ComponentName DEVICE_OWNER = new ComponentName("dummy", "component");
private final Date TIMESTAMP = new Date(2011, 11, 11);
private final int MY_USER_ID = UserHandle.myUserId();
private final int MANAGED_PROFILE_USER_ID = MY_USER_ID + 1;
private final String VPN_PACKAGE_ID = "com.example.vpn";
private List<UserInfo> mProfiles = new ArrayList();
private @Mock DevicePolicyManagerWrapper mDevicePolicyManager;
private @Mock PackageManagerWrapper mPackageManager;
private @Mock UserManager mUserManager;
private @Mock ConnectivityManagerWrapper mConnectivityManger;
private EnterprisePrivacyFeatureProvider mProvider;
@@ -57,8 +69,11 @@ public final class EnterprisePrivacyFeatureProviderImplTest {
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN))
.thenReturn(true);
when(mUserManager.getProfiles(MY_USER_ID)).thenReturn(mProfiles);
mProfiles.add(new UserInfo(MY_USER_ID, "", "", 0 /* flags */));
mProvider = new EnterprisePrivacyFeatureProviderImpl(mDevicePolicyManager, mPackageManager);
mProvider = new EnterprisePrivacyFeatureProviderImpl(mDevicePolicyManager, mPackageManager,
mUserManager, mConnectivityManger);
}
@Test
@@ -70,6 +85,15 @@ public final class EnterprisePrivacyFeatureProviderImplTest {
assertThat(mProvider.hasDeviceOwner()).isTrue();
}
@Test
public void testIsInCompMode() {
when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(DEVICE_OWNER);
assertThat(mProvider.isInCompMode()).isFalse();
mProfiles.add(new UserInfo(MANAGED_PROFILE_USER_ID, "", "", UserInfo.FLAG_MANAGED_PROFILE));
assertThat(mProvider.isInCompMode()).isTrue();
}
@Test
public void testGetLastSecurityLogRetrievalTime() {
when(mDevicePolicyManager.getLastSecurityLogRetrievalTime()).thenReturn(-1L);
@@ -97,4 +121,29 @@ public final class EnterprisePrivacyFeatureProviderImplTest {
when(mDevicePolicyManager.getLastNetworkLogRetrievalTime()).thenReturn(TIMESTAMP.getTime());
assertThat(mProvider.getLastNetworkLogRetrievalTime()).isEqualTo(TIMESTAMP);
}
@Test
public void testIsAlwaysOnVpnSetInPrimaryUser() {
when(mConnectivityManger.getAlwaysOnVpnPackageForUser(MY_USER_ID)).thenReturn(null);
assertThat(mProvider.isAlwaysOnVpnSetInPrimaryUser()).isFalse();
when(mConnectivityManger.getAlwaysOnVpnPackageForUser(MY_USER_ID))
.thenReturn(VPN_PACKAGE_ID);
assertThat(mProvider.isAlwaysOnVpnSetInPrimaryUser()).isTrue();
}
@Test
public void testIsAlwaysOnVpnSetInManagedProfileProfile() {
assertThat(mProvider.isAlwaysOnVpnSetInManagedProfile()).isFalse();
mProfiles.add(new UserInfo(MANAGED_PROFILE_USER_ID, "", "", UserInfo.FLAG_MANAGED_PROFILE));
when(mConnectivityManger.getAlwaysOnVpnPackageForUser(MANAGED_PROFILE_USER_ID))
.thenReturn(null);
assertThat(mProvider.isAlwaysOnVpnSetInManagedProfile()).isFalse();
when(mConnectivityManger.getAlwaysOnVpnPackageForUser(MANAGED_PROFILE_USER_ID))
.thenReturn(VPN_PACKAGE_ID);
assertThat(mProvider.isAlwaysOnVpnSetInManagedProfile()).isTrue();
}
}

View File

@@ -73,10 +73,14 @@ public final class EnterprisePrivacySettingsTest {
final List<PreferenceController> controllers = mSettings.getPreferenceControllers(
ShadowApplication.getInstance().getApplicationContext());
assertThat(controllers).isNotNull();
assertThat(controllers.size()).isEqualTo(4);
assertThat(controllers.size()).isEqualTo(6);
assertThat(controllers.get(0)).isInstanceOf(InstalledPackagesPreferenceController.class);
assertThat(controllers.get(1)).isInstanceOf(NetworkLogsPreferenceController.class);
assertThat(controllers.get(2)).isInstanceOf(BugReportsPreferenceController.class);
assertThat(controllers.get(3)).isInstanceOf(SecurityLogsPreferenceController.class);
assertThat(controllers.get(4)).isInstanceOf(
AlwaysOnVpnPrimaryUserPreferenceController.class);
assertThat(controllers.get(5)).isInstanceOf(
AlwaysOnVpnManagedProfilePreferenceController.class);
}
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright (C) 2017 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 com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Tests for {@link VpnUtils}.
*/
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public final class VpnUtilsTest {
@Test
public void testIsAlwaysOnVpnSet() {
final ConnectivityManagerWrapper cm = mock(ConnectivityManagerWrapper.class);
when(cm.getAlwaysOnVpnPackageForUser(0)).thenReturn("com.example.vpn");
assertThat(VpnUtils.isAlwaysOnVpnSet(cm, 0)).isTrue();
when(cm.getAlwaysOnVpnPackageForUser(0)).thenReturn(null);
assertThat(VpnUtils.isAlwaysOnVpnSet(cm, 0)).isFalse();
}
}