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 static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.Mockito.when; 21 22 import android.content.Context; 23 import android.support.v7.preference.Preference; 24 25 import com.android.settings.R; 26 import com.android.settings.testutils.FakeFeatureFactory; 27 import com.android.settings.testutils.SettingsRobolectricTestRunner; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.mockito.Answers; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 36 @RunWith(SettingsRobolectricTestRunner.class) 37 public final class AlwaysOnVpnCurrentUserPreferenceControllerTest { 38 39 private static final String VPN_SET_DEVICE = "VPN set"; 40 private static final String VPN_SET_PERSONAL = "VPN set in personal profile"; 41 private static final String KEY_ALWAYS_ON_VPN_PRIMARY_USER = "always_on_vpn_primary_user"; 42 43 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 44 private Context mContext; 45 private FakeFeatureFactory mFeatureFactory; 46 47 private AlwaysOnVpnCurrentUserPreferenceController mController; 48 49 @Before setUp()50 public void setUp() { 51 MockitoAnnotations.initMocks(this); 52 mFeatureFactory = FakeFeatureFactory.setupForTest(); 53 mController = new AlwaysOnVpnCurrentUserPreferenceController(mContext); 54 when(mContext.getString(R.string.enterprise_privacy_always_on_vpn_device)) 55 .thenReturn(VPN_SET_DEVICE); 56 when(mContext.getString(R.string.enterprise_privacy_always_on_vpn_personal)) 57 .thenReturn(VPN_SET_PERSONAL); 58 } 59 60 @Test testUpdateState()61 public void testUpdateState() { 62 final Preference preference = new Preference(mContext, null, 0, 0); 63 64 when(mFeatureFactory.enterprisePrivacyFeatureProvider.isAlwaysOnVpnSetInCurrentUser()) 65 .thenReturn(true); 66 67 when(mFeatureFactory.enterprisePrivacyFeatureProvider.isInCompMode()).thenReturn(false); 68 mController.updateState(preference); 69 assertThat(preference.getTitle()).isEqualTo(VPN_SET_DEVICE); 70 71 when(mFeatureFactory.enterprisePrivacyFeatureProvider.isInCompMode()).thenReturn(true); 72 mController.updateState(preference); 73 assertThat(preference.getTitle()).isEqualTo(VPN_SET_PERSONAL); 74 } 75 76 @Test testIsAvailable()77 public void testIsAvailable() { 78 when(mFeatureFactory.enterprisePrivacyFeatureProvider.isAlwaysOnVpnSetInCurrentUser()) 79 .thenReturn(false); 80 assertThat(mController.isAvailable()).isFalse(); 81 82 when(mFeatureFactory.enterprisePrivacyFeatureProvider.isAlwaysOnVpnSetInCurrentUser()) 83 .thenReturn(true); 84 assertThat(mController.isAvailable()).isTrue(); 85 } 86 87 @Test testHandlePreferenceTreeClick()88 public void testHandlePreferenceTreeClick() { 89 assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0))) 90 .isFalse(); 91 } 92 93 @Test testGetPreferenceKey()94 public void testGetPreferenceKey() { 95 assertThat(mController.getPreferenceKey()).isEqualTo(KEY_ALWAYS_ON_VPN_PRIMARY_USER); 96 } 97 } 98