Add AccessibilityShortcutPreferenceFragment to share non-accesibility tools to add shortcut
Bug: 190022774 Test: make RunSettingsRoboTests -j52 ROBOTEST_FILTER=AccessibilityShortcutPreferenceFragmentTest Change-Id: I1cae8fbed059ba7c309126e2dff46adfb48cffd8
This commit is contained in:
@@ -0,0 +1,366 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2021 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.AccessibilityDialogUtils.DialogEnums;
|
||||||
|
|
||||||
|
import android.app.Dialog;
|
||||||
|
import android.app.settings.SettingsEnums;
|
||||||
|
import android.content.ComponentName;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.DialogInterface;
|
||||||
|
import android.icu.text.CaseMap;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.provider.Settings;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.view.accessibility.AccessibilityManager;
|
||||||
|
import android.widget.CheckBox;
|
||||||
|
|
||||||
|
import androidx.annotation.VisibleForTesting;
|
||||||
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.SettingsPreferenceFragment;
|
||||||
|
import com.android.settings.utils.LocaleUtils;
|
||||||
|
|
||||||
|
import com.google.android.setupcompat.util.WizardManagerHelper;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base class for accessibility fragments shortcut functions and dialog management.
|
||||||
|
*/
|
||||||
|
public abstract class AccessibilityShortcutPreferenceFragment extends SettingsPreferenceFragment
|
||||||
|
implements ShortcutPreference.OnClickCallback {
|
||||||
|
private static final String KEY_SHORTCUT_PREFERENCE = "shortcut_preference";
|
||||||
|
protected static final String KEY_SAVED_USER_SHORTCUT_TYPE = "shortcut_type";
|
||||||
|
protected static final int NOT_SET = -1;
|
||||||
|
// Save user's shortcutType value when savedInstance has value (e.g. device rotated).
|
||||||
|
protected int mSavedCheckBoxValue = NOT_SET;
|
||||||
|
|
||||||
|
protected ShortcutPreference mShortcutPreference;
|
||||||
|
private AccessibilityManager.TouchExplorationStateChangeListener
|
||||||
|
mTouchExplorationStateChangeListener;
|
||||||
|
private SettingsContentObserver mSettingsContentObserver;
|
||||||
|
private CheckBox mSoftwareTypeCheckBox;
|
||||||
|
private CheckBox mHardwareTypeCheckBox;
|
||||||
|
|
||||||
|
/** Returns the accessibility component name. */
|
||||||
|
protected abstract ComponentName getComponentName();
|
||||||
|
|
||||||
|
/** Returns the accessibility feature name. */
|
||||||
|
protected abstract CharSequence getLabelName();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
|
// Restore the user shortcut type.
|
||||||
|
if (savedInstanceState != null && savedInstanceState.containsKey(
|
||||||
|
KEY_SAVED_USER_SHORTCUT_TYPE)) {
|
||||||
|
mSavedCheckBoxValue = savedInstanceState.getInt(KEY_SAVED_USER_SHORTCUT_TYPE, NOT_SET);
|
||||||
|
}
|
||||||
|
|
||||||
|
final int resId = getPreferenceScreenResId();
|
||||||
|
if (resId <= 0) {
|
||||||
|
final PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(
|
||||||
|
getPrefContext());
|
||||||
|
setPreferenceScreen(preferenceScreen);
|
||||||
|
}
|
||||||
|
|
||||||
|
final List<String> shortcutFeatureKeys = new ArrayList<>();
|
||||||
|
shortcutFeatureKeys.add(Settings.Secure.ACCESSIBILITY_BUTTON_TARGETS);
|
||||||
|
shortcutFeatureKeys.add(Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGET_SERVICE);
|
||||||
|
mSettingsContentObserver = new SettingsContentObserver(new Handler(), shortcutFeatureKeys) {
|
||||||
|
@Override
|
||||||
|
public void onChange(boolean selfChange, Uri uri) {
|
||||||
|
updateShortcutPreferenceData();
|
||||||
|
updateShortcutPreference();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||||
|
Bundle savedInstanceState) {
|
||||||
|
mShortcutPreference = new ShortcutPreference(getPrefContext(), /* attrs= */ null);
|
||||||
|
mShortcutPreference.setPersistent(false);
|
||||||
|
mShortcutPreference.setKey(getShortcutPreferenceKey());
|
||||||
|
mShortcutPreference.setOnClickCallback(this);
|
||||||
|
|
||||||
|
final CharSequence title = getString(R.string.accessibility_shortcut_title, getLabelName());
|
||||||
|
mShortcutPreference.setTitle(title);
|
||||||
|
getPreferenceScreen().addPreference(mShortcutPreference);
|
||||||
|
|
||||||
|
mTouchExplorationStateChangeListener = isTouchExplorationEnabled -> {
|
||||||
|
removeDialog(DialogEnums.EDIT_SHORTCUT);
|
||||||
|
mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
|
||||||
|
};
|
||||||
|
|
||||||
|
return super.onCreateView(inflater, container, savedInstanceState);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
final AccessibilityManager am = getPrefContext().getSystemService(
|
||||||
|
AccessibilityManager.class);
|
||||||
|
am.addTouchExplorationStateChangeListener(mTouchExplorationStateChangeListener);
|
||||||
|
mSettingsContentObserver.register(getContentResolver());
|
||||||
|
updateShortcutPreferenceData();
|
||||||
|
updateShortcutPreference();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPause() {
|
||||||
|
final AccessibilityManager am = getPrefContext().getSystemService(
|
||||||
|
AccessibilityManager.class);
|
||||||
|
am.removeTouchExplorationStateChangeListener(mTouchExplorationStateChangeListener);
|
||||||
|
mSettingsContentObserver.unregister(getContentResolver());
|
||||||
|
super.onPause();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSaveInstanceState(Bundle outState) {
|
||||||
|
final int value = getShortcutTypeCheckBoxValue();
|
||||||
|
if (value != NOT_SET) {
|
||||||
|
outState.putInt(KEY_SAVED_USER_SHORTCUT_TYPE, value);
|
||||||
|
}
|
||||||
|
super.onSaveInstanceState(outState);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Dialog onCreateDialog(int dialogId) {
|
||||||
|
switch (dialogId) {
|
||||||
|
case DialogEnums.EDIT_SHORTCUT:
|
||||||
|
final CharSequence dialogTitle = getPrefContext().getString(
|
||||||
|
R.string.accessibility_shortcut_title, getLabelName());
|
||||||
|
final int dialogType = WizardManagerHelper.isAnySetupWizard(getIntent())
|
||||||
|
? AccessibilityDialogUtils.DialogType.EDIT_SHORTCUT_GENERIC_SUW :
|
||||||
|
AccessibilityDialogUtils.DialogType.EDIT_SHORTCUT_GENERIC;
|
||||||
|
final Dialog dialog = AccessibilityDialogUtils.showEditShortcutDialog(
|
||||||
|
getPrefContext(), dialogType, dialogTitle,
|
||||||
|
this::callOnAlertDialogCheckboxClicked);
|
||||||
|
setupEditShortcutDialog(dialog);
|
||||||
|
return dialog;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Unsupported dialogId " + dialogId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getDialogMetricsCategory(int dialogId) {
|
||||||
|
switch (dialogId) {
|
||||||
|
case DialogEnums.EDIT_SHORTCUT:
|
||||||
|
return SettingsEnums.DIALOG_ACCESSIBILITY_SERVICE_EDIT_SHORTCUT;
|
||||||
|
default:
|
||||||
|
return SettingsEnums.ACTION_UNKNOWN;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSettingsClicked(ShortcutPreference preference) {
|
||||||
|
showDialog(DialogEnums.EDIT_SHORTCUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onToggleClicked(ShortcutPreference preference) {
|
||||||
|
if (getComponentName() == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final int shortcutTypes = PreferredShortcuts.retrieveUserShortcutType(getPrefContext(),
|
||||||
|
getComponentName().flattenToString(), AccessibilityUtil.UserShortcutType.SOFTWARE);
|
||||||
|
if (preference.isChecked()) {
|
||||||
|
AccessibilityUtil.optInAllValuesToSettings(getPrefContext(), shortcutTypes,
|
||||||
|
getComponentName());
|
||||||
|
showDialog(DialogEnums.LAUNCH_ACCESSIBILITY_TUTORIAL);
|
||||||
|
} else {
|
||||||
|
AccessibilityUtil.optOutAllValuesFromSettings(getPrefContext(), shortcutTypes,
|
||||||
|
getComponentName());
|
||||||
|
}
|
||||||
|
mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String getShortcutPreferenceKey() {
|
||||||
|
return KEY_SHORTCUT_PREFERENCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
void setupEditShortcutDialog(Dialog dialog) {
|
||||||
|
final View dialogSoftwareView = dialog.findViewById(R.id.software_shortcut);
|
||||||
|
mSoftwareTypeCheckBox = dialogSoftwareView.findViewById(R.id.checkbox);
|
||||||
|
setDialogTextAreaClickListener(dialogSoftwareView, mSoftwareTypeCheckBox);
|
||||||
|
|
||||||
|
final View dialogHardwareView = dialog.findViewById(R.id.hardware_shortcut);
|
||||||
|
mHardwareTypeCheckBox = dialogHardwareView.findViewById(R.id.checkbox);
|
||||||
|
setDialogTextAreaClickListener(dialogHardwareView, mHardwareTypeCheckBox);
|
||||||
|
|
||||||
|
updateEditShortcutDialogCheckBox();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns accumulated {@link AccessibilityUtil.UserShortcutType} checkbox value or
|
||||||
|
* {@code NOT_SET} if checkboxes did not exist.
|
||||||
|
*/
|
||||||
|
protected int getShortcutTypeCheckBoxValue() {
|
||||||
|
if (mSoftwareTypeCheckBox == null || mHardwareTypeCheckBox == null) {
|
||||||
|
return NOT_SET;
|
||||||
|
}
|
||||||
|
|
||||||
|
int value = AccessibilityUtil.UserShortcutType.EMPTY;
|
||||||
|
if (mSoftwareTypeCheckBox.isChecked()) {
|
||||||
|
value |= AccessibilityUtil.UserShortcutType.SOFTWARE;
|
||||||
|
}
|
||||||
|
if (mHardwareTypeCheckBox.isChecked()) {
|
||||||
|
value |= AccessibilityUtil.UserShortcutType.HARDWARE;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will be invoked when a button in the edit shortcut dialog is clicked.
|
||||||
|
*
|
||||||
|
* @param dialog The dialog that received the click
|
||||||
|
* @param which The button that was clicked
|
||||||
|
*/
|
||||||
|
protected void callOnAlertDialogCheckboxClicked(DialogInterface dialog, int which) {
|
||||||
|
if (getComponentName() == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final int value = getShortcutTypeCheckBoxValue();
|
||||||
|
|
||||||
|
saveNonEmptyUserShortcutType(value);
|
||||||
|
AccessibilityUtil.optInAllValuesToSettings(getPrefContext(), value, getComponentName());
|
||||||
|
AccessibilityUtil.optOutAllValuesFromSettings(getPrefContext(), ~value, getComponentName());
|
||||||
|
mShortcutPreference.setChecked(value != AccessibilityUtil.UserShortcutType.EMPTY);
|
||||||
|
mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
void saveNonEmptyUserShortcutType(int type) {
|
||||||
|
if (type == AccessibilityUtil.UserShortcutType.EMPTY) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final PreferredShortcut shortcut = new PreferredShortcut(
|
||||||
|
getComponentName().flattenToString(), type);
|
||||||
|
PreferredShortcuts.saveUserShortcutType(getPrefContext(), shortcut);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setDialogTextAreaClickListener(View dialogView, CheckBox checkBox) {
|
||||||
|
final View dialogTextArea = dialogView.findViewById(R.id.container);
|
||||||
|
dialogTextArea.setOnClickListener(v -> checkBox.toggle());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected CharSequence getShortcutTypeSummary(Context context) {
|
||||||
|
if (!mShortcutPreference.isSettingsEditable()) {
|
||||||
|
return context.getText(R.string.accessibility_shortcut_edit_dialog_title_hardware);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!mShortcutPreference.isChecked()) {
|
||||||
|
return context.getText(R.string.switch_off_text);
|
||||||
|
}
|
||||||
|
|
||||||
|
final int shortcutTypes = PreferredShortcuts.retrieveUserShortcutType(context,
|
||||||
|
getComponentName().flattenToString(), AccessibilityUtil.UserShortcutType.SOFTWARE);
|
||||||
|
|
||||||
|
final List<CharSequence> list = new ArrayList<>();
|
||||||
|
final CharSequence softwareTitle = context.getText(
|
||||||
|
R.string.accessibility_shortcut_edit_summary_software);
|
||||||
|
|
||||||
|
if (hasShortcutType(shortcutTypes, AccessibilityUtil.UserShortcutType.SOFTWARE)) {
|
||||||
|
list.add(softwareTitle);
|
||||||
|
}
|
||||||
|
if (hasShortcutType(shortcutTypes, AccessibilityUtil.UserShortcutType.HARDWARE)) {
|
||||||
|
final CharSequence hardwareTitle = context.getText(
|
||||||
|
R.string.accessibility_shortcut_hardware_keyword);
|
||||||
|
list.add(hardwareTitle);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show software shortcut if first time to use.
|
||||||
|
if (list.isEmpty()) {
|
||||||
|
list.add(softwareTitle);
|
||||||
|
}
|
||||||
|
|
||||||
|
return CaseMap.toTitle().wholeString().noLowercase().apply(Locale.getDefault(), /* iter= */
|
||||||
|
null, LocaleUtils.getConcatenatedString(list));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateEditShortcutDialogCheckBox() {
|
||||||
|
// If it is during onConfigChanged process then restore the value, or get the saved value
|
||||||
|
// when shortcutPreference is checked.
|
||||||
|
int value = restoreOnConfigChangedValue();
|
||||||
|
if (value == NOT_SET) {
|
||||||
|
final int lastNonEmptyUserShortcutType = PreferredShortcuts.retrieveUserShortcutType(
|
||||||
|
getPrefContext(), getComponentName().flattenToString(),
|
||||||
|
AccessibilityUtil.UserShortcutType.SOFTWARE);
|
||||||
|
value = mShortcutPreference.isChecked() ? lastNonEmptyUserShortcutType
|
||||||
|
: AccessibilityUtil.UserShortcutType.EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
mSoftwareTypeCheckBox.setChecked(
|
||||||
|
hasShortcutType(value, AccessibilityUtil.UserShortcutType.SOFTWARE));
|
||||||
|
mHardwareTypeCheckBox.setChecked(
|
||||||
|
hasShortcutType(value, AccessibilityUtil.UserShortcutType.HARDWARE));
|
||||||
|
}
|
||||||
|
|
||||||
|
private int restoreOnConfigChangedValue() {
|
||||||
|
final int savedValue = mSavedCheckBoxValue;
|
||||||
|
mSavedCheckBoxValue = NOT_SET;
|
||||||
|
return savedValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean hasShortcutType(int value, @AccessibilityUtil.UserShortcutType int type) {
|
||||||
|
return (value & type) == type;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void updateShortcutPreferenceData() {
|
||||||
|
if (getComponentName() == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final int shortcutTypes = AccessibilityUtil.getUserShortcutTypesFromSettings(
|
||||||
|
getPrefContext(), getComponentName());
|
||||||
|
if (shortcutTypes != AccessibilityUtil.UserShortcutType.EMPTY) {
|
||||||
|
final PreferredShortcut shortcut = new PreferredShortcut(
|
||||||
|
getComponentName().flattenToString(), shortcutTypes);
|
||||||
|
PreferredShortcuts.saveUserShortcutType(getPrefContext(), shortcut);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void updateShortcutPreference() {
|
||||||
|
if (getComponentName() == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final int shortcutTypes = PreferredShortcuts.retrieveUserShortcutType(getPrefContext(),
|
||||||
|
getComponentName().flattenToString(), AccessibilityUtil.UserShortcutType.SOFTWARE);
|
||||||
|
mShortcutPreference.setChecked(
|
||||||
|
AccessibilityUtil.hasValuesInSettings(getPrefContext(), shortcutTypes,
|
||||||
|
getComponentName()));
|
||||||
|
mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
|
||||||
|
}
|
||||||
|
}
|
@@ -136,7 +136,7 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
|
|||||||
setupDefaultShortcutIfNecessary(getPrefContext());
|
setupDefaultShortcutIfNecessary(getPrefContext());
|
||||||
final int resId = getPreferenceScreenResId();
|
final int resId = getPreferenceScreenResId();
|
||||||
if (resId <= 0) {
|
if (resId <= 0) {
|
||||||
PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(
|
final PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(
|
||||||
getPrefContext());
|
getPrefContext());
|
||||||
setPreferenceScreen(preferenceScreen);
|
setPreferenceScreen(preferenceScreen);
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,222 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2021 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.AccessibilityShortcutPreferenceFragment.KEY_SAVED_USER_SHORTCUT_TYPE;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.content.ComponentName;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.DialogInterface;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.provider.Settings;
|
||||||
|
|
||||||
|
import androidx.appcompat.app.AlertDialog;
|
||||||
|
import androidx.preference.PreferenceManager;
|
||||||
|
import androidx.preference.PreferenceScreen;
|
||||||
|
import androidx.test.core.app.ApplicationProvider;
|
||||||
|
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.testutils.shadow.ShadowFragment;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Answers;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.RobolectricTestRunner;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
|
/** Tests for {@link AccessibilityShortcutPreferenceFragment} */
|
||||||
|
@RunWith(RobolectricTestRunner.class)
|
||||||
|
public class AccessibilityShortcutPreferenceFragmentTest {
|
||||||
|
|
||||||
|
private static final String PLACEHOLDER_PACKAGE_NAME = "com.placeholder.example";
|
||||||
|
private static final String PLACEHOLDER_CLASS_NAME = PLACEHOLDER_PACKAGE_NAME + ".placeholder";
|
||||||
|
private static final ComponentName PLACEHOLDER_COMPONENT_NAME = new ComponentName(
|
||||||
|
PLACEHOLDER_PACKAGE_NAME, PLACEHOLDER_CLASS_NAME);
|
||||||
|
private static final String PLACEHOLDER_DIALOG_TITLE = "title";
|
||||||
|
|
||||||
|
private static final String SOFTWARE_SHORTCUT_KEY =
|
||||||
|
Settings.Secure.ACCESSIBILITY_BUTTON_TARGETS;
|
||||||
|
private static final String HARDWARE_SHORTCUT_KEY =
|
||||||
|
Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGET_SERVICE;
|
||||||
|
|
||||||
|
private TestAccessibilityShortcutPreferenceFragment mFragment;
|
||||||
|
private PreferenceScreen mScreen;
|
||||||
|
private Context mContext = ApplicationProvider.getApplicationContext();
|
||||||
|
|
||||||
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
|
private PreferenceManager mPreferenceManager;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUpTestFragment() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
|
mFragment = spy(new TestAccessibilityShortcutPreferenceFragment());
|
||||||
|
when(mFragment.getPreferenceManager()).thenReturn(mPreferenceManager);
|
||||||
|
when(mFragment.getPreferenceManager().getContext()).thenReturn(mContext);
|
||||||
|
when(mFragment.getContext()).thenReturn(mContext);
|
||||||
|
mScreen = spy(new PreferenceScreen(mContext, null));
|
||||||
|
when(mScreen.getPreferenceManager()).thenReturn(mPreferenceManager);
|
||||||
|
doReturn(mScreen).when(mFragment).getPreferenceScreen();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateShortcutPreferenceData_assignDefaultValueToVariable() {
|
||||||
|
mFragment.updateShortcutPreferenceData();
|
||||||
|
|
||||||
|
final int expectedType = PreferredShortcuts.retrieveUserShortcutType(mContext,
|
||||||
|
mFragment.getComponentName().flattenToString(),
|
||||||
|
AccessibilityUtil.UserShortcutType.SOFTWARE);
|
||||||
|
// Compare to default UserShortcutType
|
||||||
|
assertThat(expectedType).isEqualTo(AccessibilityUtil.UserShortcutType.SOFTWARE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateShortcutPreferenceData_hasValueInSettings_assignToVariable() {
|
||||||
|
putStringIntoSettings(SOFTWARE_SHORTCUT_KEY, PLACEHOLDER_COMPONENT_NAME.flattenToString());
|
||||||
|
putStringIntoSettings(HARDWARE_SHORTCUT_KEY, PLACEHOLDER_COMPONENT_NAME.flattenToString());
|
||||||
|
|
||||||
|
mFragment.updateShortcutPreferenceData();
|
||||||
|
|
||||||
|
final int expectedType = PreferredShortcuts.retrieveUserShortcutType(mContext,
|
||||||
|
mFragment.getComponentName().flattenToString(),
|
||||||
|
AccessibilityUtil.UserShortcutType.SOFTWARE);
|
||||||
|
assertThat(expectedType).isEqualTo(AccessibilityUtil.UserShortcutType.SOFTWARE
|
||||||
|
| AccessibilityUtil.UserShortcutType.HARDWARE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateShortcutPreferenceData_hasValueInSharedPreference_assignToVariable() {
|
||||||
|
final PreferredShortcut hardwareShortcut = new PreferredShortcut(
|
||||||
|
PLACEHOLDER_COMPONENT_NAME.flattenToString(),
|
||||||
|
AccessibilityUtil.UserShortcutType.HARDWARE);
|
||||||
|
|
||||||
|
putUserShortcutTypeIntoSharedPreference(mContext, hardwareShortcut);
|
||||||
|
mFragment.updateShortcutPreferenceData();
|
||||||
|
|
||||||
|
final int expectedType = PreferredShortcuts.retrieveUserShortcutType(mContext,
|
||||||
|
mFragment.getComponentName().flattenToString(),
|
||||||
|
AccessibilityUtil.UserShortcutType.SOFTWARE);
|
||||||
|
assertThat(expectedType).isEqualTo(AccessibilityUtil.UserShortcutType.HARDWARE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setupEditShortcutDialog_shortcutPreferenceOff_checkboxIsEmptyValue() {
|
||||||
|
mContext.setTheme(R.style.Theme_AppCompat);
|
||||||
|
final AlertDialog dialog = AccessibilityDialogUtils.showEditShortcutDialog(
|
||||||
|
mContext, AccessibilityDialogUtils.DialogType.EDIT_SHORTCUT_GENERIC,
|
||||||
|
PLACEHOLDER_DIALOG_TITLE,
|
||||||
|
this::callEmptyOnClicked);
|
||||||
|
final ShortcutPreference shortcutPreference = new ShortcutPreference(mContext, /* attrs= */
|
||||||
|
null);
|
||||||
|
mFragment.mShortcutPreference = shortcutPreference;
|
||||||
|
|
||||||
|
mFragment.mShortcutPreference.setChecked(false);
|
||||||
|
mFragment.setupEditShortcutDialog(dialog);
|
||||||
|
|
||||||
|
final int checkboxValue = mFragment.getShortcutTypeCheckBoxValue();
|
||||||
|
assertThat(checkboxValue).isEqualTo(AccessibilityUtil.UserShortcutType.EMPTY);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setupEditShortcutDialog_shortcutPreferenceOn_checkboxIsSavedValue() {
|
||||||
|
mContext.setTheme(R.style.Theme_AppCompat);
|
||||||
|
final AlertDialog dialog = AccessibilityDialogUtils.showEditShortcutDialog(
|
||||||
|
mContext, AccessibilityDialogUtils.DialogType.EDIT_SHORTCUT_GENERIC,
|
||||||
|
PLACEHOLDER_DIALOG_TITLE,
|
||||||
|
this::callEmptyOnClicked);
|
||||||
|
final ShortcutPreference shortcutPreference = new ShortcutPreference(mContext, /* attrs= */
|
||||||
|
null);
|
||||||
|
final PreferredShortcut hardwareShortcut = new PreferredShortcut(
|
||||||
|
PLACEHOLDER_COMPONENT_NAME.flattenToString(),
|
||||||
|
AccessibilityUtil.UserShortcutType.HARDWARE);
|
||||||
|
mFragment.mShortcutPreference = shortcutPreference;
|
||||||
|
|
||||||
|
PreferredShortcuts.saveUserShortcutType(mContext, hardwareShortcut);
|
||||||
|
mFragment.mShortcutPreference.setChecked(true);
|
||||||
|
mFragment.setupEditShortcutDialog(dialog);
|
||||||
|
|
||||||
|
final int checkboxValue = mFragment.getShortcutTypeCheckBoxValue();
|
||||||
|
assertThat(checkboxValue).isEqualTo(AccessibilityUtil.UserShortcutType.HARDWARE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Config(shadows = ShadowFragment.class)
|
||||||
|
public void restoreValueFromSavedInstanceState_assignToVariable() {
|
||||||
|
mContext.setTheme(R.style.Theme_AppCompat);
|
||||||
|
final AlertDialog dialog = AccessibilityDialogUtils.showEditShortcutDialog(
|
||||||
|
mContext, AccessibilityDialogUtils.DialogType.EDIT_SHORTCUT_GENERIC,
|
||||||
|
PLACEHOLDER_DIALOG_TITLE,
|
||||||
|
this::callEmptyOnClicked);
|
||||||
|
final Bundle savedInstanceState = new Bundle();
|
||||||
|
final ShortcutPreference shortcutPreference = new ShortcutPreference(mContext, /* attrs= */
|
||||||
|
null);
|
||||||
|
mFragment.mShortcutPreference = shortcutPreference;
|
||||||
|
|
||||||
|
savedInstanceState.putInt(KEY_SAVED_USER_SHORTCUT_TYPE,
|
||||||
|
AccessibilityUtil.UserShortcutType.SOFTWARE
|
||||||
|
| AccessibilityUtil.UserShortcutType.HARDWARE);
|
||||||
|
mFragment.onCreate(savedInstanceState);
|
||||||
|
mFragment.setupEditShortcutDialog(dialog);
|
||||||
|
final int value = mFragment.getShortcutTypeCheckBoxValue();
|
||||||
|
mFragment.saveNonEmptyUserShortcutType(value);
|
||||||
|
|
||||||
|
final int expectedType = PreferredShortcuts.retrieveUserShortcutType(mContext,
|
||||||
|
mFragment.getComponentName().flattenToString(),
|
||||||
|
AccessibilityUtil.UserShortcutType.SOFTWARE);
|
||||||
|
assertThat(expectedType).isEqualTo(
|
||||||
|
AccessibilityUtil.UserShortcutType.SOFTWARE
|
||||||
|
| AccessibilityUtil.UserShortcutType.HARDWARE);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void callEmptyOnClicked(DialogInterface dialog, int which) {}
|
||||||
|
|
||||||
|
private void putStringIntoSettings(String key, String componentName) {
|
||||||
|
Settings.Secure.putString(mContext.getContentResolver(), key, componentName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void putUserShortcutTypeIntoSharedPreference(Context context,
|
||||||
|
PreferredShortcut shortcut) {
|
||||||
|
PreferredShortcuts.saveUserShortcutType(context, shortcut);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class TestAccessibilityShortcutPreferenceFragment
|
||||||
|
extends AccessibilityShortcutPreferenceFragment {
|
||||||
|
@Override
|
||||||
|
protected ComponentName getComponentName() {
|
||||||
|
return PLACEHOLDER_COMPONENT_NAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected CharSequence getLabelName() {
|
||||||
|
return PLACEHOLDER_PACKAGE_NAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMetricsCategory() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
@@ -118,9 +118,9 @@ public class ToggleFeaturePreferenceFragmentTest {
|
|||||||
@Test
|
@Test
|
||||||
public void updateShortcutPreferenceData_hasValueInSettings_assignToVariable() {
|
public void updateShortcutPreferenceData_hasValueInSettings_assignToVariable() {
|
||||||
mFragment.mComponentName = PLACEHOLDER_COMPONENT_NAME;
|
mFragment.mComponentName = PLACEHOLDER_COMPONENT_NAME;
|
||||||
|
|
||||||
putStringIntoSettings(SOFTWARE_SHORTCUT_KEY, PLACEHOLDER_COMPONENT_NAME.flattenToString());
|
putStringIntoSettings(SOFTWARE_SHORTCUT_KEY, PLACEHOLDER_COMPONENT_NAME.flattenToString());
|
||||||
putStringIntoSettings(HARDWARE_SHORTCUT_KEY, PLACEHOLDER_COMPONENT_NAME.flattenToString());
|
putStringIntoSettings(HARDWARE_SHORTCUT_KEY, PLACEHOLDER_COMPONENT_NAME.flattenToString());
|
||||||
|
|
||||||
mFragment.updateShortcutPreferenceData();
|
mFragment.updateShortcutPreferenceData();
|
||||||
|
|
||||||
final int expectedType = PreferredShortcuts.retrieveUserShortcutType(mContext,
|
final int expectedType = PreferredShortcuts.retrieveUserShortcutType(mContext,
|
||||||
|
@@ -186,6 +186,7 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
|
|||||||
public void updateShortcutPreferenceData_hasValueInSettings_assignToVariable() {
|
public void updateShortcutPreferenceData_hasValueInSettings_assignToVariable() {
|
||||||
putStringIntoSettings(SOFTWARE_SHORTCUT_KEY, MAGNIFICATION_CONTROLLER_NAME);
|
putStringIntoSettings(SOFTWARE_SHORTCUT_KEY, MAGNIFICATION_CONTROLLER_NAME);
|
||||||
setMagnificationTripleTapEnabled(/* enabled= */ true);
|
setMagnificationTripleTapEnabled(/* enabled= */ true);
|
||||||
|
|
||||||
mFragment.updateShortcutPreferenceData();
|
mFragment.updateShortcutPreferenceData();
|
||||||
|
|
||||||
final int expectedType = PreferredShortcuts.retrieveUserShortcutType(mContext,
|
final int expectedType = PreferredShortcuts.retrieveUserShortcutType(mContext,
|
||||||
|
Reference in New Issue
Block a user