1 /* 2 * Copyright (C) 2022 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.vpn2; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.os.Looper; 27 28 import androidx.test.annotation.UiThreadTest; 29 import androidx.test.core.app.ApplicationProvider; 30 import androidx.test.ext.junit.runners.AndroidJUnit4; 31 32 import com.android.settings.testutils.FakeFeatureFactory; 33 import com.android.settingslib.RestrictedPreference; 34 35 import org.junit.Before; 36 import org.junit.Rule; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.junit.MockitoJUnit; 40 import org.mockito.junit.MockitoRule; 41 42 @RunWith(AndroidJUnit4.class) 43 public class AppManagementFragmentTest { 44 private static final String FAKE_PACKAGE_NAME = "com.fake.package.name"; 45 private static final String ADVANCED_VPN_GROUP_PACKAGE_NAME = "com.advanced.package.name"; 46 47 @Rule 48 public final MockitoRule mMockitoRule = MockitoJUnit.rule(); 49 50 private AppManagementFragment mFragment; 51 private Context mContext; 52 private FakeFeatureFactory mFakeFeatureFactory; 53 private RestrictedPreference mPreferenceForget; 54 55 @Before 56 @UiThreadTest setUp()57 public void setUp() { 58 if (Looper.myLooper() == null) { 59 Looper.prepare(); 60 } 61 62 mFragment = spy(new AppManagementFragment()); 63 mContext = spy(ApplicationProvider.getApplicationContext()); 64 mPreferenceForget = new RestrictedPreference(mContext); 65 66 mFakeFeatureFactory = FakeFeatureFactory.setupForTest(); 67 mFragment.init(ADVANCED_VPN_GROUP_PACKAGE_NAME, 68 mFakeFeatureFactory.getAdvancedVpnFeatureProvider(), mPreferenceForget); 69 when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.getAdvancedVpnPackageName()) 70 .thenReturn(ADVANCED_VPN_GROUP_PACKAGE_NAME); 71 when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.isAdvancedVpnSupported(any())) 72 .thenReturn(true); 73 } 74 75 @Test updateRestrictedViews_isAdvancedVpn_hidesForgetPreference()76 public void updateRestrictedViews_isAdvancedVpn_hidesForgetPreference() { 77 when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.isAdvancedVpnRemovable()) 78 .thenReturn(false); 79 mFragment.updateRestrictedViews(); 80 assertThat(mPreferenceForget.isVisible()).isFalse(); 81 } 82 83 @Test updateRestrictedViews_isNotAdvancedVpn_showsForgetPreference()84 public void updateRestrictedViews_isNotAdvancedVpn_showsForgetPreference() { 85 when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.isAdvancedVpnRemovable()) 86 .thenReturn(false); 87 mFragment.init(FAKE_PACKAGE_NAME, 88 mFakeFeatureFactory.getAdvancedVpnFeatureProvider(), mPreferenceForget); 89 mFragment.updateRestrictedViews(); 90 assertThat(mPreferenceForget.isVisible()).isTrue(); 91 } 92 93 @Test updateRestrictedViews_isAdvancedVpnRemovable_showsForgetPreference()94 public void updateRestrictedViews_isAdvancedVpnRemovable_showsForgetPreference() { 95 when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.isAdvancedVpnRemovable()) 96 .thenReturn(true); 97 mFragment.init(FAKE_PACKAGE_NAME, 98 mFakeFeatureFactory.getAdvancedVpnFeatureProvider(), mPreferenceForget); 99 mFragment.updateRestrictedViews(); 100 assertThat(mPreferenceForget.isVisible()).isTrue(); 101 } 102 } 103