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