From f0b6123acc14a629ccdd5df675e12060af54d41c Mon Sep 17 00:00:00 2001 From: shaoweishen Date: Fri, 27 Sep 2024 14:26:20 +0000 Subject: [PATCH] [Physical Keyboard][Setting] New pk a11y entry under a11y page Add entry for physical keyboard a11y page under a11y setting Bug: 345399212 Test: atest SettingsRoboTests Flag: com.android.settings.keyboard.keyboard_and_touchpad_a11y_new_page_enabled Change-Id: I0c59c3a9df1b5df5e22349ab3907f767682d6fb9 --- res/xml/accessibility_settings.xml | 35 +--- .../accessibility/AccessibilitySettings.java | 66 +------ ...KeyboardBounceKeyPreferenceController.java | 79 --------- .../KeyboardSlowKeyPreferenceController.java | 80 --------- ...KeyboardStickyKeyPreferenceController.java | 76 -------- ...sicalKeyboardA11yPreferenceController.java | 50 ++++++ ...oardBounceKeyPreferenceControllerTest.java | 163 ------------------ ...yboardSlowKeyPreferenceControllerTest.java | 163 ------------------ ...oardStickyKeyPreferenceControllerTest.java | 161 ----------------- ...lKeyboardA11yPreferenceControllerTest.java | 98 +++++++++++ 10 files changed, 161 insertions(+), 810 deletions(-) delete mode 100644 src/com/android/settings/accessibility/KeyboardBounceKeyPreferenceController.java delete mode 100644 src/com/android/settings/accessibility/KeyboardSlowKeyPreferenceController.java delete mode 100644 src/com/android/settings/accessibility/KeyboardStickyKeyPreferenceController.java create mode 100644 src/com/android/settings/inputmethod/PhysicalKeyboardA11yPreferenceController.java delete mode 100644 tests/robotests/src/com/android/settings/accessibility/KeyboardBounceKeyPreferenceControllerTest.java delete mode 100644 tests/robotests/src/com/android/settings/accessibility/KeyboardSlowKeyPreferenceControllerTest.java delete mode 100644 tests/robotests/src/com/android/settings/accessibility/KeyboardStickyKeyPreferenceControllerTest.java create mode 100644 tests/robotests/src/com/android/settings/inputmethod/PhysicalKeyboardA11yPreferenceControllerTest.java diff --git a/res/xml/accessibility_settings.xml b/res/xml/accessibility_settings.xml index 18e645591ff..fcc5ce30a88 100644 --- a/res/xml/accessibility_settings.xml +++ b/res/xml/accessibility_settings.xml @@ -120,37 +120,14 @@ settings:controller="com.android.settings.inputmethod.PointerTouchpadPreferenceController" settings:searchable="true"/> - - - - - - - - - + android:fragment="com.android.settings.inputmethod.PhysicalKeyboardA11yFragment" + settings:controller="com.android.settings.inputmethod.PhysicalKeyboardA11yPreferenceController" /> controllers = new ArrayList<>(); @@ -567,53 +560,6 @@ public class AccessibilitySettings extends DashboardFragment implements findPreference(controller.getPreferenceKey()))); } - private void updateKeyboardPreferencesVisibility() { - if (!mCategoryToPrefCategoryMap.containsKey(CATEGORY_KEYBOARD_OPTIONS)) { - return; - } - boolean isVisible = isAnyHardKeyboardsExist() - && isAnyKeyboardPreferenceAvailable(); - mCategoryToPrefCategoryMap.get(CATEGORY_KEYBOARD_OPTIONS).setVisible( - isVisible); - if (isVisible) { - //set summary here. - findPreference(KeyboardBounceKeyPreferenceController.PREF_KEY).setSummary( - getContext().getString(R.string.bounce_keys_summary, - PhysicalKeyboardFragment.BOUNCE_KEYS_THRESHOLD)); - findPreference(KeyboardSlowKeyPreferenceController.PREF_KEY).setSummary( - getContext().getString(R.string.slow_keys_summary, - PhysicalKeyboardFragment.SLOW_KEYS_THRESHOLD)); - } - } - - static boolean isAnyHardKeyboardsExist() { - for (int deviceId : InputDevice.getDeviceIds()) { - final InputDevice device = InputDevice.getDevice(deviceId); - if (device != null && !device.isVirtual() && device.isFullKeyboard()) { - return true; - } - } - return false; - } - - private boolean isAnyKeyboardPreferenceAvailable() { - for (List controllerList : getPreferenceControllers()) { - for (AbstractPreferenceController controller : controllerList) { - if (controller.getPreferenceKey().equals( - KeyboardBounceKeyPreferenceController.PREF_KEY) - || controller.getPreferenceKey().equals( - KeyboardSlowKeyPreferenceController.PREF_KEY) - || controller.getPreferenceKey().equals( - KeyboardStickyKeyPreferenceController.PREF_KEY)) { - if (controller.isAvailable()) { - return true; - } - } - } - } - return false; - } - public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = new BaseSearchIndexProvider(R.xml.accessibility_settings) { @Override @@ -671,10 +617,12 @@ public class AccessibilitySettings extends DashboardFragment implements }; @Override - public void onInputDeviceAdded(int deviceId) {} + public void onInputDeviceAdded(int deviceId) { + } @Override - public void onInputDeviceRemoved(int deviceId) {} + public void onInputDeviceRemoved(int deviceId) { + } @Override public void onInputDeviceChanged(int deviceId) { diff --git a/src/com/android/settings/accessibility/KeyboardBounceKeyPreferenceController.java b/src/com/android/settings/accessibility/KeyboardBounceKeyPreferenceController.java deleted file mode 100644 index 840caa6fa6c..00000000000 --- a/src/com/android/settings/accessibility/KeyboardBounceKeyPreferenceController.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (C) 2024 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.accessibility; - - -import android.content.Context; -import android.hardware.input.InputSettings; -import android.util.Log; - -import androidx.annotation.NonNull; - -import com.android.settings.R; -import com.android.settings.core.TogglePreferenceController; -import com.android.settings.inputmethod.PhysicalKeyboardFragment; - -import java.util.List; - -/** - * A toggle preference controller for keyboard bounce key. - */ -public class KeyboardBounceKeyPreferenceController extends TogglePreferenceController { - private static final String TAG = "BounceKeyPrefController"; - static final String PREF_KEY = "toggle_keyboard_bounce_keys"; - - public KeyboardBounceKeyPreferenceController(Context context, String preferenceKey) { - super(context, preferenceKey); - } - - @Override - public int getAvailabilityStatus() { - return InputSettings.isAccessibilityBounceKeysFeatureEnabled() - ? AVAILABLE : UNSUPPORTED_ON_DEVICE; - } - - @Override - public boolean isChecked() { - return InputSettings.isAccessibilityBounceKeysEnabled(mContext); - } - - @Override - public boolean setChecked(boolean isChecked) { - InputSettings.setAccessibilityBounceKeysThreshold(mContext, - isChecked ? PhysicalKeyboardFragment.BOUNCE_KEYS_THRESHOLD - : 0); - return true; - } - - @Override - public int getSliceHighlightMenuRes() { - return R.string.menu_key_accessibility; - } - - @Override - public void updateNonIndexableKeys(@NonNull List keys) { - super.updateNonIndexableKeys(keys); - - if (Flags.fixA11ySettingsSearch() && !AccessibilitySettings.isAnyHardKeyboardsExist()) { - if (keys.contains(getPreferenceKey())) { - Log.w(TAG, "Skipping updateNonIndexableKeys, key already in list."); - return; - } - keys.add(getPreferenceKey()); - } - } -} diff --git a/src/com/android/settings/accessibility/KeyboardSlowKeyPreferenceController.java b/src/com/android/settings/accessibility/KeyboardSlowKeyPreferenceController.java deleted file mode 100644 index bb9d95049ad..00000000000 --- a/src/com/android/settings/accessibility/KeyboardSlowKeyPreferenceController.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (C) 2024 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.accessibility; - - -import android.content.Context; -import android.hardware.input.InputSettings; -import android.util.Log; - -import androidx.annotation.NonNull; - -import com.android.settings.R; -import com.android.settings.core.TogglePreferenceController; -import com.android.settings.inputmethod.PhysicalKeyboardFragment; - -import java.util.List; - -/** - * A toggle preference controller for keyboard slow key. - */ -public class KeyboardSlowKeyPreferenceController extends TogglePreferenceController { - private static final String TAG = "SlowKeyPrefController"; - - static final String PREF_KEY = "toggle_keyboard_slow_keys"; - - public KeyboardSlowKeyPreferenceController(Context context, String preferenceKey) { - super(context, preferenceKey); - } - - @Override - public int getAvailabilityStatus() { - return InputSettings.isAccessibilitySlowKeysFeatureFlagEnabled() - ? AVAILABLE : UNSUPPORTED_ON_DEVICE; - } - - @Override - public boolean isChecked() { - return InputSettings.isAccessibilitySlowKeysEnabled(mContext); - } - - @Override - public boolean setChecked(boolean isChecked) { - InputSettings.setAccessibilitySlowKeysThreshold(mContext, - isChecked ? PhysicalKeyboardFragment.SLOW_KEYS_THRESHOLD - : 0); - return true; - } - - @Override - public int getSliceHighlightMenuRes() { - return R.string.menu_key_accessibility; - } - - @Override - public void updateNonIndexableKeys(@NonNull List keys) { - super.updateNonIndexableKeys(keys); - - if (Flags.fixA11ySettingsSearch() && !AccessibilitySettings.isAnyHardKeyboardsExist()) { - if (keys.contains(getPreferenceKey())) { - Log.w(TAG, "Skipping updateNonIndexableKeys, key already in list."); - return; - } - keys.add(getPreferenceKey()); - } - } -} diff --git a/src/com/android/settings/accessibility/KeyboardStickyKeyPreferenceController.java b/src/com/android/settings/accessibility/KeyboardStickyKeyPreferenceController.java deleted file mode 100644 index c896c9c0d27..00000000000 --- a/src/com/android/settings/accessibility/KeyboardStickyKeyPreferenceController.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (C) 2024 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.accessibility; - - -import android.content.Context; -import android.hardware.input.InputSettings; -import android.util.Log; - -import androidx.annotation.NonNull; - -import com.android.settings.R; -import com.android.settings.core.TogglePreferenceController; - -import java.util.List; - -/** - * A toggle preference controller for keyboard sticky key. - */ -public class KeyboardStickyKeyPreferenceController extends TogglePreferenceController { - private static final String TAG = "StickyKeyPrefController"; - static final String PREF_KEY = "toggle_keyboard_sticky_keys"; - - public KeyboardStickyKeyPreferenceController(Context context, String preferenceKey) { - super(context, preferenceKey); - } - - @Override - public int getAvailabilityStatus() { - return InputSettings.isAccessibilityStickyKeysFeatureEnabled() - ? AVAILABLE : UNSUPPORTED_ON_DEVICE; - } - - @Override - public boolean isChecked() { - return InputSettings.isAccessibilityStickyKeysEnabled(mContext); - } - - @Override - public boolean setChecked(boolean isChecked) { - InputSettings.setAccessibilityStickyKeysEnabled(mContext, isChecked); - return true; - } - - @Override - public int getSliceHighlightMenuRes() { - return R.string.menu_key_accessibility; - } - - @Override - public void updateNonIndexableKeys(@NonNull List keys) { - super.updateNonIndexableKeys(keys); - - if (Flags.fixA11ySettingsSearch() && !AccessibilitySettings.isAnyHardKeyboardsExist()) { - if (keys.contains(getPreferenceKey())) { - Log.w(TAG, "Skipping updateNonIndexableKeys, key already in list."); - return; - } - keys.add(getPreferenceKey()); - } - } -} diff --git a/src/com/android/settings/inputmethod/PhysicalKeyboardA11yPreferenceController.java b/src/com/android/settings/inputmethod/PhysicalKeyboardA11yPreferenceController.java new file mode 100644 index 00000000000..41be5b91798 --- /dev/null +++ b/src/com/android/settings/inputmethod/PhysicalKeyboardA11yPreferenceController.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.inputmethod; + +import static com.android.settings.keyboard.Flags.keyboardAndTouchpadA11yNewPageEnabled; + +import android.content.Context; +import android.view.InputDevice; + +import com.android.settings.core.BasePreferenceController; + +/** Controller that shows and updates the Physical Keyboard a11y preference. */ +public class PhysicalKeyboardA11yPreferenceController extends BasePreferenceController { + + + public PhysicalKeyboardA11yPreferenceController(Context context, + String preferenceKey) { + super(context, preferenceKey); + } + + @Override + public int getAvailabilityStatus() { + return keyboardAndTouchpadA11yNewPageEnabled() + && isAnyHardKeyboardsExist() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; + } + + private static boolean isAnyHardKeyboardsExist() { + for (int deviceId : InputDevice.getDeviceIds()) { + final InputDevice device = InputDevice.getDevice(deviceId); + if (device != null && !device.isVirtual() && device.isFullKeyboard()) { + return true; + } + } + return false; + } +} diff --git a/tests/robotests/src/com/android/settings/accessibility/KeyboardBounceKeyPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/KeyboardBounceKeyPreferenceControllerTest.java deleted file mode 100644 index bf6efd097d7..00000000000 --- a/tests/robotests/src/com/android/settings/accessibility/KeyboardBounceKeyPreferenceControllerTest.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Copyright (C) 2024 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.accessibility; - -import static com.android.settings.accessibility.AccessibilityUtil.State.OFF; -import static com.android.settings.accessibility.AccessibilityUtil.State.ON; - -import static com.google.common.truth.Truth.assertThat; - -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; - -import android.content.Context; -import android.platform.test.annotations.EnableFlags; -import android.platform.test.flag.junit.SetFlagsRule; -import android.provider.Settings; - -import androidx.preference.PreferenceManager; -import androidx.preference.PreferenceScreen; -import androidx.preference.SwitchPreference; -import androidx.test.core.app.ApplicationProvider; - -import com.android.settings.core.BasePreferenceController; - -import org.junit.Assume; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -import java.util.ArrayList; -import java.util.List; - -@RunWith(RobolectricTestRunner.class) -public class KeyboardBounceKeyPreferenceControllerTest { - private static final String KEY_ACCESSIBILITY_BOUNCE_KEYS = - Settings.Secure.ACCESSIBILITY_BOUNCE_KEYS; - private static final int UNKNOWN = -1; - - @Rule - public final SetFlagsRule mSetFlagRule = new SetFlagsRule(); - private final Context mContext = ApplicationProvider.getApplicationContext(); - private final SwitchPreference mSwitchPreference = spy(new SwitchPreference(mContext)); - private final KeyboardBounceKeyPreferenceController mController = - new KeyboardBounceKeyPreferenceController(mContext, - KeyboardBounceKeyPreferenceController.PREF_KEY); - - @Before - public void setUp() { - final PreferenceManager preferenceManager = new PreferenceManager(mContext); - final PreferenceScreen screen = preferenceManager.createPreferenceScreen(mContext); - mSwitchPreference.setKey(KeyboardBounceKeyPreferenceController.PREF_KEY); - screen.addPreference(mSwitchPreference); - mController.displayPreference(screen); - } - - @Test - public void getAvailabilityStatus_byDefault_shouldReturnAvailable() { - assertThat(mController.getAvailabilityStatus()).isEqualTo( - BasePreferenceController.AVAILABLE); - } - - @Test - public void isChecked_disableBounceKey_onResumeShouldReturnFalse() { - Settings.Secure.putInt(mContext.getContentResolver(), KEY_ACCESSIBILITY_BOUNCE_KEYS, OFF); - - mController.updateState(mSwitchPreference); - - assertThat(mController.isChecked()).isFalse(); - assertThat(mSwitchPreference.isChecked()).isFalse(); - } - - @Test - public void isChecked_enableBounceKey_onResumeShouldReturnTrue() { - Settings.Secure.putInt(mContext.getContentResolver(), KEY_ACCESSIBILITY_BOUNCE_KEYS, ON); - - mController.updateState(mSwitchPreference); - - assertThat(mController.isChecked()).isTrue(); - assertThat(mSwitchPreference.isChecked()).isTrue(); - } - - @Test - public void performClick_enableBounceKey_shouldReturnTrue() { - Settings.Secure.putInt(mContext.getContentResolver(), KEY_ACCESSIBILITY_BOUNCE_KEYS, OFF); - - mController.updateState(mSwitchPreference); - - mSwitchPreference.performClick(); - - verify(mSwitchPreference).setChecked(true); - assertThat(mController.isChecked()).isTrue(); - assertThat(mSwitchPreference.isChecked()).isTrue(); - } - - @Test - public void performClick_disableBounceKey_shouldReturnFalse() { - Settings.Secure.putInt(mContext.getContentResolver(), KEY_ACCESSIBILITY_BOUNCE_KEYS, ON); - - mController.updateState(mSwitchPreference); - - mSwitchPreference.performClick(); - - verify(mSwitchPreference).setChecked(false); - assertThat(mController.isChecked()).isFalse(); - assertThat(mSwitchPreference.isChecked()).isFalse(); - } - - @Test - public void setChecked_setFalse_shouldDisableBounceKey() { - mController.setChecked(false); - - assertThat(Settings.Secure.getInt( - mContext.getContentResolver(), KEY_ACCESSIBILITY_BOUNCE_KEYS, UNKNOWN)).isEqualTo( - OFF); - } - - @Test - public void setChecked_setTrue_shouldEnableBounceKey() { - mController.setChecked(true); - - assertThat(Settings.Secure.getInt( - mContext.getContentResolver(), KEY_ACCESSIBILITY_BOUNCE_KEYS, - UNKNOWN)).isNotEqualTo(OFF); - } - - @Test - @EnableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH) - public void updateNonIndexableKeys_physicalKeyboardExists_returnEmptyList() { - Assume.assumeTrue(AccessibilitySettings.isAnyHardKeyboardsExist()); - - List nonIndexableKeys = new ArrayList<>(); - mController.updateNonIndexableKeys(nonIndexableKeys); - - assertThat(nonIndexableKeys).isEmpty(); - } - - @Test - @EnableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH) - public void updateNonIndexableKeys_noPhysicalKeyboard_returnPreKey() { - Assume.assumeFalse(AccessibilitySettings.isAnyHardKeyboardsExist()); - - List nonIndexableKeys = new ArrayList<>(); - mController.updateNonIndexableKeys(nonIndexableKeys); - - assertThat(nonIndexableKeys).contains(mController.getPreferenceKey()); - } -} diff --git a/tests/robotests/src/com/android/settings/accessibility/KeyboardSlowKeyPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/KeyboardSlowKeyPreferenceControllerTest.java deleted file mode 100644 index 2721a64874c..00000000000 --- a/tests/robotests/src/com/android/settings/accessibility/KeyboardSlowKeyPreferenceControllerTest.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Copyright (C) 2024 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.accessibility; - -import static com.android.settings.accessibility.AccessibilityUtil.State.OFF; -import static com.android.settings.accessibility.AccessibilityUtil.State.ON; - -import static com.google.common.truth.Truth.assertThat; - -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; - -import android.content.Context; -import android.platform.test.annotations.EnableFlags; -import android.platform.test.flag.junit.SetFlagsRule; -import android.provider.Settings; - -import androidx.preference.PreferenceManager; -import androidx.preference.PreferenceScreen; -import androidx.preference.SwitchPreference; -import androidx.test.core.app.ApplicationProvider; - -import com.android.settings.core.BasePreferenceController; - -import org.junit.Assume; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -import java.util.ArrayList; -import java.util.List; - -@RunWith(RobolectricTestRunner.class) -public class KeyboardSlowKeyPreferenceControllerTest { - private static final String KEY_ACCESSIBILITY_SLOW_KEYS = - Settings.Secure.ACCESSIBILITY_SLOW_KEYS; - private static final int UNKNOWN = -1; - - @Rule - public final SetFlagsRule mSetFlagRule = new SetFlagsRule(); - private final Context mContext = ApplicationProvider.getApplicationContext(); - private final SwitchPreference mSwitchPreference = spy(new SwitchPreference(mContext)); - private final KeyboardSlowKeyPreferenceController mController = - new KeyboardSlowKeyPreferenceController(mContext, - KeyboardSlowKeyPreferenceController.PREF_KEY); - - @Before - public void setUp() { - final PreferenceManager preferenceManager = new PreferenceManager(mContext); - final PreferenceScreen screen = preferenceManager.createPreferenceScreen(mContext); - mSwitchPreference.setKey(KeyboardSlowKeyPreferenceController.PREF_KEY); - screen.addPreference(mSwitchPreference); - mController.displayPreference(screen); - } - - @Test - public void getAvailabilityStatus_byDefault_shouldReturnAvailable() { - assertThat(mController.getAvailabilityStatus()).isEqualTo( - BasePreferenceController.AVAILABLE); - } - - @Test - public void isChecked_disableSlowKey_onResumeShouldReturnFalse() { - Settings.Secure.putInt(mContext.getContentResolver(), KEY_ACCESSIBILITY_SLOW_KEYS, OFF); - - mController.updateState(mSwitchPreference); - - assertThat(mController.isChecked()).isFalse(); - assertThat(mSwitchPreference.isChecked()).isFalse(); - } - - @Test - public void isChecked_enableSlowKey_onResumeShouldReturnTrue() { - Settings.Secure.putInt(mContext.getContentResolver(), KEY_ACCESSIBILITY_SLOW_KEYS, ON); - - mController.updateState(mSwitchPreference); - - assertThat(mController.isChecked()).isTrue(); - assertThat(mSwitchPreference.isChecked()).isTrue(); - } - - @Test - public void performClick_enableSlowKey_shouldReturnTrue() { - Settings.Secure.putInt(mContext.getContentResolver(), KEY_ACCESSIBILITY_SLOW_KEYS, OFF); - - mController.updateState(mSwitchPreference); - - mSwitchPreference.performClick(); - - verify(mSwitchPreference).setChecked(true); - assertThat(mController.isChecked()).isTrue(); - assertThat(mSwitchPreference.isChecked()).isTrue(); - } - - @Test - public void performClick_disableSlowKey_shouldReturnFalse() { - Settings.Secure.putInt(mContext.getContentResolver(), KEY_ACCESSIBILITY_SLOW_KEYS, ON); - - mController.updateState(mSwitchPreference); - - mSwitchPreference.performClick(); - - verify(mSwitchPreference).setChecked(false); - assertThat(mController.isChecked()).isFalse(); - assertThat(mSwitchPreference.isChecked()).isFalse(); - } - - @Test - public void setChecked_setFalse_shouldDisableSlowKey() { - mController.setChecked(false); - - assertThat(Settings.Secure.getInt( - mContext.getContentResolver(), KEY_ACCESSIBILITY_SLOW_KEYS, UNKNOWN)).isEqualTo( - OFF); - } - - @Test - public void setChecked_setTrue_shouldEnableSlowKey() { - mController.setChecked(true); - - assertThat(Settings.Secure.getInt( - mContext.getContentResolver(), KEY_ACCESSIBILITY_SLOW_KEYS, UNKNOWN)).isNotEqualTo( - OFF); - } - - @Test - @EnableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH) - public void updateNonIndexableKeys_physicalKeyboardExists_returnEmptyList() { - Assume.assumeTrue(AccessibilitySettings.isAnyHardKeyboardsExist()); - - List nonIndexableKeys = new ArrayList<>(); - mController.updateNonIndexableKeys(nonIndexableKeys); - - assertThat(nonIndexableKeys).isEmpty(); - } - - @Test - @EnableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH) - public void updateNonIndexableKeys_noPhysicalKeyboard_returnPreKey() { - Assume.assumeFalse(AccessibilitySettings.isAnyHardKeyboardsExist()); - - List nonIndexableKeys = new ArrayList<>(); - mController.updateNonIndexableKeys(nonIndexableKeys); - - assertThat(nonIndexableKeys).contains(mController.getPreferenceKey()); - } -} diff --git a/tests/robotests/src/com/android/settings/accessibility/KeyboardStickyKeyPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/KeyboardStickyKeyPreferenceControllerTest.java deleted file mode 100644 index 0001e8552d1..00000000000 --- a/tests/robotests/src/com/android/settings/accessibility/KeyboardStickyKeyPreferenceControllerTest.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright (C) 2024 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.accessibility; - -import static com.android.settings.accessibility.AccessibilityUtil.State.OFF; -import static com.android.settings.accessibility.AccessibilityUtil.State.ON; - -import static com.google.common.truth.Truth.assertThat; - -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; - -import android.content.Context; -import android.platform.test.annotations.EnableFlags; -import android.platform.test.flag.junit.SetFlagsRule; -import android.provider.Settings; - -import androidx.preference.PreferenceManager; -import androidx.preference.PreferenceScreen; -import androidx.preference.SwitchPreference; -import androidx.test.core.app.ApplicationProvider; - -import com.android.settings.core.BasePreferenceController; - -import org.junit.Assume; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -import java.util.ArrayList; -import java.util.List; - -@RunWith(RobolectricTestRunner.class) -public class KeyboardStickyKeyPreferenceControllerTest { - private static final String KEY_ACCESSIBILITY_STICKY_KEYS = - Settings.Secure.ACCESSIBILITY_STICKY_KEYS; - private static final int UNKNOWN = -1; - - @Rule - public final SetFlagsRule mSetFlagRule = new SetFlagsRule(); - private final Context mContext = ApplicationProvider.getApplicationContext(); - private final SwitchPreference mSwitchPreference = spy(new SwitchPreference(mContext)); - private final KeyboardStickyKeyPreferenceController mController = - new KeyboardStickyKeyPreferenceController(mContext, - KeyboardStickyKeyPreferenceController.PREF_KEY); - - @Before - public void setUp() { - final PreferenceManager preferenceManager = new PreferenceManager(mContext); - final PreferenceScreen screen = preferenceManager.createPreferenceScreen(mContext); - mSwitchPreference.setKey(KeyboardStickyKeyPreferenceController.PREF_KEY); - screen.addPreference(mSwitchPreference); - mController.displayPreference(screen); - } - - @Test - public void getAvailabilityStatus_byDefault_shouldReturnAvailable() { - assertThat(mController.getAvailabilityStatus()).isEqualTo( - BasePreferenceController.AVAILABLE); - } - - @Test - public void isChecked_disableStickyKey_onResumeShouldReturnFalse() { - Settings.Secure.putInt(mContext.getContentResolver(), KEY_ACCESSIBILITY_STICKY_KEYS, OFF); - - mController.updateState(mSwitchPreference); - - assertThat(mController.isChecked()).isFalse(); - assertThat(mSwitchPreference.isChecked()).isFalse(); - } - - @Test - public void isChecked_enableStickyKey_onResumeShouldReturnTrue() { - Settings.Secure.putInt(mContext.getContentResolver(), KEY_ACCESSIBILITY_STICKY_KEYS, ON); - - mController.updateState(mSwitchPreference); - - assertThat(mController.isChecked()).isTrue(); - assertThat(mSwitchPreference.isChecked()).isTrue(); - } - - @Test - public void performClick_enableStickyKey_shouldReturnTrue() { - Settings.Secure.putInt(mContext.getContentResolver(), KEY_ACCESSIBILITY_STICKY_KEYS, OFF); - - mController.updateState(mSwitchPreference); - - mSwitchPreference.performClick(); - - verify(mSwitchPreference).setChecked(true); - assertThat(mController.isChecked()).isTrue(); - assertThat(mSwitchPreference.isChecked()).isTrue(); - } - - @Test - public void performClick_disableStickyKey_shouldReturnFalse() { - Settings.Secure.putInt(mContext.getContentResolver(), KEY_ACCESSIBILITY_STICKY_KEYS, ON); - - mController.updateState(mSwitchPreference); - - mSwitchPreference.performClick(); - - verify(mSwitchPreference).setChecked(false); - assertThat(mController.isChecked()).isFalse(); - assertThat(mSwitchPreference.isChecked()).isFalse(); - } - - @Test - public void setChecked_setFalse_shouldDisableStickyKey() { - mController.setChecked(false); - - assertThat(Settings.Secure.getInt( - mContext.getContentResolver(), KEY_ACCESSIBILITY_STICKY_KEYS, UNKNOWN)).isEqualTo(OFF); - } - - @Test - public void setChecked_setTrue_shouldEnableStickyKey() { - mController.setChecked(true); - - assertThat(Settings.Secure.getInt( - mContext.getContentResolver(), KEY_ACCESSIBILITY_STICKY_KEYS, UNKNOWN)).isEqualTo(ON); - } - - @Test - @EnableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH) - public void updateNonIndexableKeys_physicalKeyboardExists_returnEmptyList() { - Assume.assumeTrue(AccessibilitySettings.isAnyHardKeyboardsExist()); - - List nonIndexableKeys = new ArrayList<>(); - mController.updateNonIndexableKeys(nonIndexableKeys); - - assertThat(nonIndexableKeys).isEmpty(); - } - - @Test - @EnableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH) - public void updateNonIndexableKeys_noPhysicalKeyboard_returnPreKey() { - Assume.assumeFalse(AccessibilitySettings.isAnyHardKeyboardsExist()); - - List nonIndexableKeys = new ArrayList<>(); - mController.updateNonIndexableKeys(nonIndexableKeys); - - assertThat(nonIndexableKeys).contains(mController.getPreferenceKey()); - } -} diff --git a/tests/robotests/src/com/android/settings/inputmethod/PhysicalKeyboardA11yPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/inputmethod/PhysicalKeyboardA11yPreferenceControllerTest.java new file mode 100644 index 00000000000..5c6dd14d841 --- /dev/null +++ b/tests/robotests/src/com/android/settings/inputmethod/PhysicalKeyboardA11yPreferenceControllerTest.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.inputmethod; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.platform.test.annotations.EnableFlags; +import android.platform.test.flag.junit.SetFlagsRule; +import android.view.InputDevice; + +import androidx.test.core.app.ApplicationProvider; + +import com.android.settings.core.BasePreferenceController; +import com.android.settings.keyboard.Flags; +import com.android.settings.testutils.shadow.ShadowInputDevice; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +/** Tests for {@link PhysicalKeyboardA11yPreferenceController} */ +@RunWith(RobolectricTestRunner.class) +@Config(shadows = { + com.android.settings.testutils.shadow.ShadowInputDevice.class, +}) +public class PhysicalKeyboardA11yPreferenceControllerTest { + @Rule + public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); + @Rule + public MockitoRule rule = MockitoJUnit.rule(); + private static final String PREFERENCE_KEY = "physical_keyboard_a11y"; + private Context mContext; + private PhysicalKeyboardA11yPreferenceController mController; + @Mock + InputDevice mInputDevice; + + @Before + public void setUp() { + mContext = ApplicationProvider.getApplicationContext(); + mController = new PhysicalKeyboardA11yPreferenceController(mContext, PREFERENCE_KEY); + } + + @Test + @EnableFlags(Flags.FLAG_KEYBOARD_AND_TOUCHPAD_A11Y_NEW_PAGE_ENABLED) + public void getAvailabilityStatus_expected() { + int deviceId = 1; + ShadowInputDevice.sDeviceIds = new int[]{deviceId}; + when(mInputDevice.isVirtual()).thenReturn(false); + when(mInputDevice.isFullKeyboard()).thenReturn(true); + + ShadowInputDevice.addDevice(deviceId, mInputDevice); + + assertThat(InputDevice.getDeviceIds()).isNotEmpty(); + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.AVAILABLE); + + } + + @Test + @EnableFlags(Flags.FLAG_KEYBOARD_AND_TOUCHPAD_A11Y_NEW_PAGE_ENABLED) + public void getAvailabilityStatus_deviceIsNotAsExpected_unavailable() { + int deviceId = 1; + ShadowInputDevice.sDeviceIds = new int[]{deviceId}; + when(mInputDevice.isVirtual()).thenReturn(true); + when(mInputDevice.isFullKeyboard()).thenReturn(false); + + ShadowInputDevice.addDevice(deviceId, mInputDevice); + + assertThat(InputDevice.getDeviceIds()).isNotEmpty(); + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.CONDITIONALLY_UNAVAILABLE); + + } + +}