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.accessibility; 18 19 import static com.android.settings.accessibility.AccessibilityUtil.State.OFF; 20 import static com.android.settings.accessibility.MagnificationCapabilities.MagnificationMode; 21 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 22 import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE; 23 24 import static com.google.common.truth.Truth.assertThat; 25 26 import static org.mockito.Mockito.reset; 27 import static org.mockito.Mockito.spy; 28 import static org.mockito.Mockito.verify; 29 30 import android.content.Context; 31 import android.provider.Settings; 32 33 import androidx.preference.PreferenceManager; 34 import androidx.preference.PreferenceScreen; 35 import androidx.preference.SwitchPreference; 36 import androidx.test.core.app.ApplicationProvider; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.robolectric.RobolectricTestRunner; 42 import org.robolectric.shadow.api.Shadow; 43 import org.robolectric.shadows.ShadowContentResolver; 44 45 @RunWith(RobolectricTestRunner.class) 46 public class MagnificationAlwaysOnPreferenceControllerTest { 47 48 private static final String KEY_ALWAYS_ON = 49 Settings.Secure.ACCESSIBILITY_MAGNIFICATION_ALWAYS_ON_ENABLED; 50 51 private Context mContext; 52 private ShadowContentResolver mShadowContentResolver; 53 private SwitchPreference mSwitchPreference; 54 private MagnificationAlwaysOnPreferenceController mController; 55 56 @Before setUp()57 public void setUp() { 58 mContext = ApplicationProvider.getApplicationContext(); 59 mShadowContentResolver = Shadow.extract(mContext.getContentResolver()); 60 61 final PreferenceManager preferenceManager = new PreferenceManager(mContext); 62 final PreferenceScreen screen = preferenceManager.createPreferenceScreen(mContext); 63 mSwitchPreference = spy(new SwitchPreference(mContext)); 64 mSwitchPreference.setKey(MagnificationAlwaysOnPreferenceController.PREF_KEY); 65 screen.addPreference(mSwitchPreference); 66 67 mController = new MagnificationAlwaysOnPreferenceController(mContext, 68 MagnificationAlwaysOnPreferenceController.PREF_KEY); 69 mController.displayPreference(screen); 70 mController.updateState(mSwitchPreference); 71 72 reset(mSwitchPreference); 73 } 74 75 @Test performClick_switchDefaultStateForAlwaysOn_shouldReturnFalse()76 public void performClick_switchDefaultStateForAlwaysOn_shouldReturnFalse() { 77 mSwitchPreference.performClick(); 78 79 verify(mSwitchPreference).setChecked(false); 80 assertThat(mController.isChecked()).isFalse(); 81 assertThat(mSwitchPreference.isChecked()).isFalse(); 82 } 83 84 @Test updateState_disableAlwaysOn_shouldReturnFalse()85 public void updateState_disableAlwaysOn_shouldReturnFalse() { 86 Settings.Secure.putInt(mContext.getContentResolver(), KEY_ALWAYS_ON, OFF); 87 88 mController.updateState(mSwitchPreference); 89 90 verify(mSwitchPreference).setChecked(false); 91 assertThat(mController.isChecked()).isFalse(); 92 assertThat(mSwitchPreference.isChecked()).isFalse(); 93 } 94 95 @Test onResume_verifyRegisterCapabilityObserver()96 public void onResume_verifyRegisterCapabilityObserver() { 97 mController.onResume(); 98 assertThat(mShadowContentResolver.getContentObservers( 99 Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_MAGNIFICATION_CAPABILITY))) 100 .hasSize(1); 101 } 102 103 @Test onPause_verifyUnregisterCapabilityObserver()104 public void onPause_verifyUnregisterCapabilityObserver() { 105 mController.onResume(); 106 mController.onPause(); 107 assertThat(mShadowContentResolver.getContentObservers( 108 Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_MAGNIFICATION_CAPABILITY))) 109 .isEmpty(); 110 } 111 112 @Test updateState_windowModeOnly_preferenceBecomesUnavailable()113 public void updateState_windowModeOnly_preferenceBecomesUnavailable() { 114 MagnificationCapabilities.setCapabilities(mContext, MagnificationMode.WINDOW); 115 116 mController.updateState(mSwitchPreference); 117 assertThat(mSwitchPreference.isEnabled()).isFalse(); 118 } 119 120 @Test updateState_fullscreenModeOnly_preferenceIsAvailable()121 public void updateState_fullscreenModeOnly_preferenceIsAvailable() { 122 MagnificationCapabilities.setCapabilities(mContext, MagnificationMode.FULLSCREEN); 123 124 mController.updateState(mSwitchPreference); 125 assertThat(mSwitchPreference.isEnabled()).isTrue(); 126 } 127 128 @Test updateState_switchMode_preferenceIsAvailable()129 public void updateState_switchMode_preferenceIsAvailable() { 130 MagnificationCapabilities.setCapabilities(mContext, MagnificationMode.ALL); 131 132 mController.updateState(mSwitchPreference); 133 assertThat(mSwitchPreference.isEnabled()).isTrue(); 134 } 135 136 @Test getAvailableStatus_notInSetupWizard_returnAvailable()137 public void getAvailableStatus_notInSetupWizard_returnAvailable() { 138 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 139 } 140 141 @Test getAvailableStatus_inSetupWizard_returnConditionallyUnavailable()142 public void getAvailableStatus_inSetupWizard_returnConditionallyUnavailable() { 143 mController.setInSetupWizard(true); 144 assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); 145 } 146 } 147