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 android.support.v7.preference.Preference;
21 
22 import com.android.settings.SettingsRobolectricTestRunner;
23 import com.android.settings.TestConfig;
24 import com.android.settings.core.PreferenceAvailabilityObserver;
25 import com.android.settings.testutils.FakeFeatureFactory;
26 
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Answers;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.robolectric.annotation.Config;
34 
35 import static com.google.common.truth.Truth.assertThat;
36 import static org.mockito.Mockito.verify;
37 import static org.mockito.Mockito.when;
38 
39 /**
40  * Tests for {@link AlwaysOnVpnManagedProfilePreferenceController}.
41  */
42 @RunWith(SettingsRobolectricTestRunner.class)
43 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
44 public final class AlwaysOnVpnManagedProfilePreferenceControllerTest {
45 
46     private static final String KEY_ALWAYS_ON_VPN_MANAGED_PROFILE = "always_on_vpn_managed_profile";
47 
48     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
49     private Context mContext;
50     private FakeFeatureFactory mFeatureFactory;
51     @Mock private PreferenceAvailabilityObserver mObserver;
52 
53     private AlwaysOnVpnManagedProfilePreferenceController mController;
54 
55     @Before
setUp()56     public void setUp() {
57         MockitoAnnotations.initMocks(this);
58         FakeFeatureFactory.setupForTest(mContext);
59         mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
60         mController = new AlwaysOnVpnManagedProfilePreferenceController(mContext,
61                 null /* lifecycle */);
62         mController.setAvailabilityObserver(mObserver);
63     }
64 
65     @Test
testGetAvailabilityObserver()66     public void testGetAvailabilityObserver() {
67         assertThat(mController.getAvailabilityObserver()).isEqualTo(mObserver);
68     }
69 
70     @Test
testIsAvailable()71     public void testIsAvailable() {
72         when(mFeatureFactory.enterprisePrivacyFeatureProvider.isAlwaysOnVpnSetInManagedProfile())
73                 .thenReturn(false);
74         assertThat(mController.isAvailable()).isFalse();
75         verify(mObserver).onPreferenceAvailabilityUpdated(KEY_ALWAYS_ON_VPN_MANAGED_PROFILE, false);
76 
77         when(mFeatureFactory.enterprisePrivacyFeatureProvider.isAlwaysOnVpnSetInManagedProfile())
78                 .thenReturn(true);
79         assertThat(mController.isAvailable()).isTrue();
80         verify(mObserver).onPreferenceAvailabilityUpdated(KEY_ALWAYS_ON_VPN_MANAGED_PROFILE, true);
81     }
82 
83     @Test
testHandlePreferenceTreeClick()84     public void testHandlePreferenceTreeClick() {
85         assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
86                 .isFalse();
87     }
88 
89     @Test
testGetPreferenceKey()90     public void testGetPreferenceKey() {
91         assertThat(mController.getPreferenceKey()).isEqualTo(KEY_ALWAYS_ON_VPN_MANAGED_PROFILE);
92     }
93 }
94