1 /* 2 * Copyright (C) 2021 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.widget; 18 19 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import android.content.Context; 24 import android.view.View; 25 import android.widget.ImageView; 26 27 import androidx.preference.PreferenceViewHolder; 28 29 import com.android.settings.R; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.RobolectricTestRunner; 37 import org.robolectric.RuntimeEnvironment; 38 import org.robolectric.util.ReflectionHelpers; 39 40 @RunWith(RobolectricTestRunner.class) 41 public class SettingsMainSwitchPreferenceTest { 42 43 @Mock 44 private EnforcedAdmin mEnforcedAdmin; 45 private SettingsMainSwitchPreference mPreference; 46 private PreferenceViewHolder mHolder; 47 48 @Before setUp()49 public void setUp() { 50 MockitoAnnotations.initMocks(this); 51 final Context context = RuntimeEnvironment.application; 52 final SettingsMainSwitchBar switchBar = new SettingsMainSwitchBar(context); 53 mPreference = new SettingsMainSwitchPreference(context); 54 ReflectionHelpers.setField(mPreference, "mEnforcedAdmin", mEnforcedAdmin); 55 ReflectionHelpers.setField(mPreference, "mMainSwitchBar", switchBar); 56 final View rootView = View.inflate(context, R.layout.preference_widget_main_switch, 57 null /* parent */); 58 mHolder = PreferenceViewHolder.createInstanceForTests(rootView); 59 } 60 61 @Test onBindViewHolder_isRestricted_restrictIconShouldDisplay()62 public void onBindViewHolder_isRestricted_restrictIconShouldDisplay() { 63 mPreference.onBindViewHolder(mHolder); 64 65 final SettingsMainSwitchBar switchBar = mPreference.getSwitchBar(); 66 final ImageView restrictedIcon = switchBar.findViewById( 67 com.android.settingslib.widget.R.id.restricted_icon); 68 69 assertThat(restrictedIcon.getVisibility() == View.VISIBLE).isTrue(); 70 } 71 72 @Test show_preferenceShouldDisplay()73 public void show_preferenceShouldDisplay() { 74 mPreference.show(); 75 76 mPreference.onBindViewHolder(mHolder); 77 78 assertThat(mPreference.isShowing()).isTrue(); 79 assertThat(mPreference.isVisible()).isTrue(); 80 } 81 82 @Test hide_preferenceShouldNotDisplay()83 public void hide_preferenceShouldNotDisplay() { 84 mPreference.hide(); 85 86 mPreference.onBindViewHolder(mHolder); 87 88 assertThat(mPreference.isShowing()).isFalse(); 89 assertThat(mPreference.isVisible()).isFalse(); 90 } 91 } 92