1 /* 2 * Copyright (C) 2024 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.AccessibilityUtil.State.ON; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.verify; 26 27 import android.content.Context; 28 import android.provider.Settings; 29 30 import androidx.preference.PreferenceManager; 31 import androidx.preference.PreferenceScreen; 32 import androidx.preference.SwitchPreference; 33 import androidx.test.core.app.ApplicationProvider; 34 35 import com.android.settings.core.BasePreferenceController; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.robolectric.RobolectricTestRunner; 41 42 43 @RunWith(RobolectricTestRunner.class) 44 public class KeyboardStickyKeyPreferenceControllerTest { 45 46 private static final String KEY_ACCESSIBILITY_STICKY_KEYS = 47 Settings.Secure.ACCESSIBILITY_STICKY_KEYS; 48 private static final int UNKNOWN = -1; 49 50 private final Context mContext = ApplicationProvider.getApplicationContext(); 51 private final SwitchPreference mSwitchPreference = spy(new SwitchPreference(mContext)); 52 private final KeyboardStickyKeyPreferenceController mController = 53 new KeyboardStickyKeyPreferenceController(mContext, 54 KeyboardStickyKeyPreferenceController.PREF_KEY); 55 56 @Before setUp()57 public void setUp() { 58 final PreferenceManager preferenceManager = new PreferenceManager(mContext); 59 final PreferenceScreen screen = preferenceManager.createPreferenceScreen(mContext); 60 mSwitchPreference.setKey(KeyboardStickyKeyPreferenceController.PREF_KEY); 61 screen.addPreference(mSwitchPreference); 62 mController.displayPreference(screen); 63 } 64 65 @Test getAvailabilityStatus_byDefault_shouldReturnAvailable()66 public void getAvailabilityStatus_byDefault_shouldReturnAvailable() { 67 assertThat(mController.getAvailabilityStatus()).isEqualTo( 68 BasePreferenceController.AVAILABLE); 69 } 70 71 @Test isChecked_disableStickyKey_onResumeShouldReturnFalse()72 public void isChecked_disableStickyKey_onResumeShouldReturnFalse() { 73 Settings.Secure.putInt(mContext.getContentResolver(), KEY_ACCESSIBILITY_STICKY_KEYS, OFF); 74 75 mController.updateState(mSwitchPreference); 76 77 assertThat(mController.isChecked()).isFalse(); 78 assertThat(mSwitchPreference.isChecked()).isFalse(); 79 } 80 81 @Test isChecked_enableStickyKey_onResumeShouldReturnTrue()82 public void isChecked_enableStickyKey_onResumeShouldReturnTrue() { 83 Settings.Secure.putInt(mContext.getContentResolver(), KEY_ACCESSIBILITY_STICKY_KEYS, ON); 84 85 mController.updateState(mSwitchPreference); 86 87 assertThat(mController.isChecked()).isTrue(); 88 assertThat(mSwitchPreference.isChecked()).isTrue(); 89 } 90 91 @Test performClick_enableStickyKey_shouldReturnTrue()92 public void performClick_enableStickyKey_shouldReturnTrue() { 93 Settings.Secure.putInt(mContext.getContentResolver(), KEY_ACCESSIBILITY_STICKY_KEYS, OFF); 94 95 mController.updateState(mSwitchPreference); 96 97 mSwitchPreference.performClick(); 98 99 verify(mSwitchPreference).setChecked(true); 100 assertThat(mController.isChecked()).isTrue(); 101 assertThat(mSwitchPreference.isChecked()).isTrue(); 102 } 103 104 @Test performClick_disableStickyKey_shouldReturnFalse()105 public void performClick_disableStickyKey_shouldReturnFalse() { 106 Settings.Secure.putInt(mContext.getContentResolver(), KEY_ACCESSIBILITY_STICKY_KEYS, ON); 107 108 mController.updateState(mSwitchPreference); 109 110 mSwitchPreference.performClick(); 111 112 verify(mSwitchPreference).setChecked(false); 113 assertThat(mController.isChecked()).isFalse(); 114 assertThat(mSwitchPreference.isChecked()).isFalse(); 115 } 116 117 @Test setChecked_setFalse_shouldDisableStickyKey()118 public void setChecked_setFalse_shouldDisableStickyKey() { 119 mController.setChecked(false); 120 121 assertThat(Settings.Secure.getInt( 122 mContext.getContentResolver(), KEY_ACCESSIBILITY_STICKY_KEYS, UNKNOWN)).isEqualTo(OFF); 123 } 124 125 @Test setChecked_setTrue_shouldEnableStickyKey()126 public void setChecked_setTrue_shouldEnableStickyKey() { 127 mController.setChecked(true); 128 129 assertThat(Settings.Secure.getInt( 130 mContext.getContentResolver(), KEY_ACCESSIBILITY_STICKY_KEYS, UNKNOWN)).isEqualTo(ON); 131 } 132 } 133