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 26 import androidx.preference.PreferenceViewHolder; 27 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.mockito.Mock; 32 import org.mockito.MockitoAnnotations; 33 import org.robolectric.RobolectricTestRunner; 34 import org.robolectric.RuntimeEnvironment; 35 import org.robolectric.util.ReflectionHelpers; 36 37 @RunWith(RobolectricTestRunner.class) 38 public class SettingsMainSwitchPreferenceTest { 39 40 @Mock 41 private EnforcedAdmin mEnforcedAdmin; 42 private SettingsMainSwitchPreference mPreference; 43 private PreferenceViewHolder mHolder; 44 private View mRootView; 45 46 @Before setUp()47 public void setUp() { 48 MockitoAnnotations.initMocks(this); 49 final Context context = RuntimeEnvironment.application; 50 final SettingsMainSwitchBar switchBar = new SettingsMainSwitchBar(context); 51 mPreference = new SettingsMainSwitchPreference(context); 52 ReflectionHelpers.setField(mPreference, "mEnforcedAdmin", mEnforcedAdmin); 53 ReflectionHelpers.setField(mPreference, "mMainSwitchBar", switchBar); 54 mRootView = View.inflate(context, com.android.settings.R.layout.preference_widget_main_switch, 55 null /* parent */); 56 mHolder = PreferenceViewHolder.createInstanceForTests(mRootView); 57 } 58 59 @Test show_preferenceShouldDisplay()60 public void show_preferenceShouldDisplay() { 61 mPreference.show(); 62 63 mPreference.onBindViewHolder(mHolder); 64 65 assertThat(mPreference.isShowing()).isTrue(); 66 assertThat(mPreference.isVisible()).isTrue(); 67 } 68 69 @Test hide_preferenceShouldNotDisplay()70 public void hide_preferenceShouldNotDisplay() { 71 mPreference.hide(); 72 73 mPreference.onBindViewHolder(mHolder); 74 75 assertThat(mPreference.isShowing()).isFalse(); 76 assertThat(mPreference.isVisible()).isFalse(); 77 } 78 79 @Test focusability_mainSwitchBarIsNotFocusable()80 public void focusability_mainSwitchBarIsNotFocusable() { 81 mPreference.show(); 82 83 mPreference.onBindViewHolder(mHolder); 84 85 assertThat(mPreference.getSwitchBar().isFocusable()).isFalse(); 86 } 87 88 @Test focusability_mainSwitchBarFrameLayoutIsFocusable()89 public void focusability_mainSwitchBarFrameLayoutIsFocusable() { 90 mPreference.show(); 91 92 mPreference.onBindViewHolder(mHolder); 93 94 assertThat(mRootView.isFocusable()).isTrue(); 95 } 96 } 97