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.testutils.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 GlobalHttpProxyPreferenceController}. 41 */ 42 @RunWith(SettingsRobolectricTestRunner.class) 43 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 44 public final class GlobalHttpProxyPreferenceControllerTest { 45 46 private static final String KEY_GLOBAL_HTTP_PROXY = "global_http_proxy"; 47 48 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 49 private Context mContext; 50 private FakeFeatureFactory mFeatureFactory; 51 @Mock private PreferenceAvailabilityObserver mObserver; 52 53 private GlobalHttpProxyPreferenceController mController; 54 55 @Before 56 public void setUp() { 57 MockitoAnnotations.initMocks(this); 58 FakeFeatureFactory.setupForTest(mContext); 59 mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); 60 mController = new GlobalHttpProxyPreferenceController(mContext, null /* lifecycle */); 61 mController.setAvailabilityObserver(mObserver); 62 } 63 64 @Test 65 public void testGetAvailabilityObserver() { 66 assertThat(mController.getAvailabilityObserver()).isEqualTo(mObserver); 67 } 68 69 @Test 70 public void testIsAvailable() { 71 when(mFeatureFactory.enterprisePrivacyFeatureProvider.isGlobalHttpProxySet()) 72 .thenReturn(false); 73 assertThat(mController.isAvailable()).isFalse(); 74 verify(mObserver).onPreferenceAvailabilityUpdated(KEY_GLOBAL_HTTP_PROXY, false); 75 76 when(mFeatureFactory.enterprisePrivacyFeatureProvider.isGlobalHttpProxySet()) 77 .thenReturn(true); 78 assertThat(mController.isAvailable()).isTrue(); 79 verify(mObserver).onPreferenceAvailabilityUpdated(KEY_GLOBAL_HTTP_PROXY, true); 80 } 81 82 @Test 83 public void testHandlePreferenceTreeClick() { 84 assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0))) 85 .isFalse(); 86 } 87 88 @Test 89 public void testGetPreferenceKey() { 90 assertThat(mController.getPreferenceKey()).isEqualTo(KEY_GLOBAL_HTTP_PROXY); 91 } 92 } 93