1 /* 2 * Copyright (C) 2017 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.enterprise; 18 19 import android.content.Context; 20 import com.android.settings.R; 21 import android.support.v7.preference.Preference; 22 23 import com.android.settings.SettingsRobolectricTestRunner; 24 import com.android.settings.TestConfig; 25 import com.android.settings.core.PreferenceAvailabilityObserver; 26 import com.android.settings.testutils.FakeFeatureFactory; 27 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.mockito.Answers; 32 import org.mockito.Mock; 33 import org.mockito.MockitoAnnotations; 34 import org.robolectric.annotation.Config; 35 36 import static com.google.common.truth.Truth.assertThat; 37 import static org.mockito.Mockito.verify; 38 import static org.mockito.Mockito.when; 39 40 /** 41 * Tests for {@link AlwaysOnVpnCurrentUserPreferenceController}. 42 */ 43 @RunWith(SettingsRobolectricTestRunner.class) 44 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 45 public final class AlwaysOnVpnCurrentUserPreferenceControllerTest { 46 47 private static final String VPN_SET_DEVICE = "VPN set"; 48 private static final String VPN_SET_PERSONAL = "VPN set in personal profile"; 49 private static final String KEY_ALWAYS_ON_VPN_PRIMARY_USER = "always_on_vpn_primary_user"; 50 51 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 52 private Context mContext; 53 private FakeFeatureFactory mFeatureFactory; 54 @Mock private PreferenceAvailabilityObserver mObserver; 55 56 private AlwaysOnVpnCurrentUserPreferenceController mController; 57 58 @Before 59 public void setUp() { 60 MockitoAnnotations.initMocks(this); 61 FakeFeatureFactory.setupForTest(mContext); 62 mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); 63 mController = new AlwaysOnVpnCurrentUserPreferenceController(mContext, 64 null /* lifecycle */); 65 when(mContext.getString(R.string.enterprise_privacy_always_on_vpn_device)) 66 .thenReturn(VPN_SET_DEVICE); 67 when(mContext.getString(R.string.enterprise_privacy_always_on_vpn_personal)) 68 .thenReturn(VPN_SET_PERSONAL); 69 mController.setAvailabilityObserver(mObserver); 70 } 71 72 @Test 73 public void testGetAvailabilityObserver() { 74 assertThat(mController.getAvailabilityObserver()).isEqualTo(mObserver); 75 } 76 77 @Test 78 public void testUpdateState() { 79 final Preference preference = new Preference(mContext, null, 0, 0); 80 81 when(mFeatureFactory.enterprisePrivacyFeatureProvider.isAlwaysOnVpnSetInCurrentUser()) 82 .thenReturn(true); 83 84 when(mFeatureFactory.enterprisePrivacyFeatureProvider.isInCompMode()).thenReturn(false); 85 mController.updateState(preference); 86 assertThat(preference.getTitle()).isEqualTo(VPN_SET_DEVICE); 87 88 when(mFeatureFactory.enterprisePrivacyFeatureProvider.isInCompMode()).thenReturn(true); 89 mController.updateState(preference); 90 assertThat(preference.getTitle()).isEqualTo(VPN_SET_PERSONAL); 91 } 92 93 @Test 94 public void testIsAvailable() { 95 when(mFeatureFactory.enterprisePrivacyFeatureProvider.isAlwaysOnVpnSetInCurrentUser()) 96 .thenReturn(false); 97 assertThat(mController.isAvailable()).isFalse(); 98 verify(mObserver).onPreferenceAvailabilityUpdated(KEY_ALWAYS_ON_VPN_PRIMARY_USER, false); 99 100 when(mFeatureFactory.enterprisePrivacyFeatureProvider.isAlwaysOnVpnSetInCurrentUser()) 101 .thenReturn(true); 102 assertThat(mController.isAvailable()).isTrue(); 103 verify(mObserver).onPreferenceAvailabilityUpdated(KEY_ALWAYS_ON_VPN_PRIMARY_USER, true); 104 } 105 106 @Test 107 public void testHandlePreferenceTreeClick() { 108 assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0))) 109 .isFalse(); 110 } 111 112 @Test 113 public void testGetPreferenceKey() { 114 assertThat(mController.getPreferenceKey()).isEqualTo(KEY_ALWAYS_ON_VPN_PRIMARY_USER); 115 } 116 } 117