Show QS shortcut option if the a11y feature provides a tile.

Bug: 314852953
Test: atest com.android.settings.accessibility
Test: atest com.android.settings.accessibility.shortcuts
Test: manual
Flag: ACONFIG android.view.accessibility.a11y_qs_shortcut STAGING
Change-Id: Id7a6b3adfd4c4f3cb2b9f7fe15269b57a3d00543
This commit is contained in:
Chun-Ku Lin
2024-02-05 23:11:56 +00:00
parent 46369353ed
commit 8b67c17a7b
35 changed files with 1501 additions and 163 deletions

View File

@@ -98,7 +98,20 @@ public final class AccessibilityGestureNavigationTutorialTest {
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
public void createTutorialPages_turnOnTwoFingerTripleTapShortcut_hasOnePage() {
mShortcutTypes |= UserShortcutType.TWOFINGERTRIPLETAP;
mShortcutTypes |= UserShortcutType.TWOFINGER_DOUBLETAP;
final AlertDialog alertDialog =
createAccessibilityTutorialDialog(mContext, mShortcutTypes);
assertThat(createShortcutTutorialPages(mContext,
mShortcutTypes)).hasSize(/* expectedSize= */ 1);
assertThat(alertDialog).isNotNull();
}
@Test
@RequiresFlagsEnabled(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
public void createTutorialPages_turnOnQuickSettingShortcut_hasOnePage() {
mShortcutTypes |= UserShortcutType.QUICK_SETTINGS;
final AlertDialog alertDialog =
createAccessibilityTutorialDialog(mContext, mShortcutTypes);

View File

@@ -28,15 +28,24 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;
import android.app.Activity;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.icu.text.CaseMap;
import android.os.Bundle;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.Flags;
import android.widget.PopupWindow;
import androidx.annotation.Nullable;
@@ -46,9 +55,14 @@ import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.SubSettings;
import com.android.settings.accessibility.shortcuts.EditShortcutsPreferenceFragment;
import com.android.settings.testutils.shadow.ShadowFragment;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
@@ -60,13 +74,14 @@ import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowApplication;
import java.util.Locale;
/** Tests for {@link AccessibilityShortcutPreferenceFragment} */
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {
com.android.settings.testutils.shadow.ShadowFragment.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 String PLACEHOLDER_TILE_CLASS_NAME =
@@ -83,7 +98,8 @@ public class AccessibilityShortcutPreferenceFragmentTest {
Settings.Secure.ACCESSIBILITY_BUTTON_TARGETS;
private static final String HARDWARE_SHORTCUT_KEY =
Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGET_SERVICE;
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private TestAccessibilityShortcutPreferenceFragment mFragment;
private PreferenceScreen mScreen;
private Context mContext = ApplicationProvider.getApplicationContext();
@@ -206,6 +222,7 @@ public class AccessibilityShortcutPreferenceFragmentTest {
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
@Config(shadows = ShadowFragment.class)
public void restoreValueFromSavedInstanceState_showTooltipView() {
mContext.setTheme(androidx.appcompat.R.style.Theme_AppCompat);
@@ -222,6 +239,15 @@ public class AccessibilityShortcutPreferenceFragmentTest {
assertThat(getLatestPopupWindow().isShowing()).isTrue();
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
@Config(shadows = ShadowFragment.class)
public void showQuickSettingsTooltipIfNeeded_qsFlagOn_dontShowTooltipView() {
mFragment.showQuickSettingsTooltipIfNeeded(QuickSettingsTooltipType.GUIDE_TO_EDIT);
assertThat(getLatestPopupWindow()).isNull();
}
@Test
@Config(shadows = ShadowFragment.class)
public void showGeneralCategory_shouldInitCategory() {
@@ -238,6 +264,50 @@ public class AccessibilityShortcutPreferenceFragmentTest {
assertThat(mFragment.getGeneralCategoryDescription(null)).isNotNull();
}
@Test
@EnableFlags(com.android.settings.accessibility.Flags.FLAG_EDIT_SHORTCUTS_IN_FULL_SCREEN)
public void onSettingsClicked_editShortcutsFullScreenFlagOn_showFullScreenEditShortcutScreen() {
Activity activity = Robolectric.setupActivity(FragmentActivity.class);
when(mFragment.getContext()).thenReturn(activity);
Context context = mFragment.getContext();
final ShortcutPreference shortcutPreference =
new ShortcutPreference(context, /* attrs= */ null);
mFragment.onSettingsClicked(shortcutPreference);
Intent intent = shadowOf(
(Application) context.getApplicationContext()).getNextStartedActivity();
assertThat(intent).isNotNull();
assertThat(intent.getAction()).isEqualTo(Intent.ACTION_MAIN);
assertThat(intent.getComponent()).isEqualTo(
new ComponentName(context, SubSettings.class));
assertThat(intent.getExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT))
.isEqualTo(EditShortcutsPreferenceFragment.class.getName());
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void getShortcutTypeSummary_shortcutSummaryIsCorrectlySet() {
final PreferredShortcut userPreferredShortcut = new PreferredShortcut(
PLACEHOLDER_COMPONENT_NAME.flattenToString(),
UserShortcutType.HARDWARE | UserShortcutType.QUICK_SETTINGS);
putUserShortcutTypeIntoSharedPreference(mContext, userPreferredShortcut);
final ShortcutPreference shortcutPreference =
new ShortcutPreference(mContext, /* attrs= */ null);
shortcutPreference.setChecked(true);
shortcutPreference.setSettingsEditable(true);
mFragment.mShortcutPreference = shortcutPreference;
String expected = CaseMap.toTitle().wholeString().noLowercase().apply(Locale.getDefault(),
/* iter= */ null,
mContext.getString(
R.string.accessibility_feature_shortcut_setting_summary_quick_settings)
+ ", "
+ mContext.getString(R.string.accessibility_shortcut_hardware_keyword));
String summary = mFragment.getShortcutTypeSummary(mContext).toString();
assertThat(summary).isEqualTo(expected);
}
private void callEmptyOnClicked(DialogInterface dialog, int which) {}
private void putStringIntoSettings(String key, String componentName) {

View File

@@ -18,6 +18,10 @@ package com.android.settings.accessibility;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.ComponentName;
import android.content.Context;
@@ -25,20 +29,30 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.os.Build;
import android.os.UserHandle;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.Flags;
import androidx.test.core.app.ApplicationProvider;
import com.android.internal.accessibility.util.ShortcutUtils;
import com.android.settings.R;
import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType;
import com.android.settings.testutils.AccessibilityTestUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.Set;
import java.util.StringJoiner;
@RunWith(RobolectricTestRunner.class)
@@ -55,14 +69,18 @@ public final class AccessibilityUtilTest {
Settings.Secure.ACCESSIBILITY_BUTTON_TARGETS;
private static final String HARDWARE_SHORTCUT_KEY =
Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGET_SERVICE;
private static final String QUICK_SETTINGS_SHORTCUT_KEY =
Settings.Secure.ACCESSIBILITY_QS_TARGETS;
private static final String PLACEHOLDER_SETTING_FEATURE = "placeholderSettingFeature";
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private Context mContext;
@Before
public void setUp() {
mContext = ApplicationProvider.getApplicationContext();
mContext = spy(ApplicationProvider.getApplicationContext());
}
@Test
@@ -151,11 +169,12 @@ public final class AccessibilityUtilTest {
public void getUserShortcutTypeFromSettings_putOneValue_hasValue() {
setShortcut(UserShortcutType.SOFTWARE, MOCK_COMPONENT_NAME.flattenToString());
final int shortcutType = AccessibilityUtil.getUserShortcutTypesFromSettings(mContext,
final int shortcutTypes = AccessibilityUtil.getUserShortcutTypesFromSettings(mContext,
MOCK_COMPONENT_NAME);
assertThat(
(shortcutType & UserShortcutType.SOFTWARE) == UserShortcutType.SOFTWARE).isTrue();
assertThat(shortcutTypes).isEqualTo(
UserShortcutType.SOFTWARE
);
}
@Test
@@ -163,16 +182,34 @@ public final class AccessibilityUtilTest {
setShortcut(UserShortcutType.SOFTWARE, MOCK_COMPONENT_NAME.flattenToString());
setShortcut(UserShortcutType.HARDWARE, MOCK_COMPONENT_NAME.flattenToString());
final int shortcutType = AccessibilityUtil.getUserShortcutTypesFromSettings(mContext,
final int shortcutTypes = AccessibilityUtil.getUserShortcutTypesFromSettings(mContext,
MOCK_COMPONENT_NAME);
assertThat(
(shortcutType & UserShortcutType.SOFTWARE) == UserShortcutType.SOFTWARE).isTrue();
assertThat(
(shortcutType & UserShortcutType.HARDWARE) == UserShortcutType.HARDWARE).isTrue();
assertThat(shortcutTypes).isEqualTo(
UserShortcutType.SOFTWARE
| UserShortcutType.HARDWARE
);
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void getUserShortcutTypeFromSettings_threeShortcutTypesChosen() {
setShortcut(UserShortcutType.SOFTWARE, MOCK_COMPONENT_NAME.flattenToString());
setShortcut(UserShortcutType.HARDWARE, MOCK_COMPONENT_NAME.flattenToString());
setShortcut(UserShortcutType.QUICK_SETTINGS, MOCK_COMPONENT_NAME.flattenToString());
final int shortcutTypes = AccessibilityUtil.getUserShortcutTypesFromSettings(mContext,
MOCK_COMPONENT_NAME);
assertThat(shortcutTypes).isEqualTo(
UserShortcutType.SOFTWARE
| UserShortcutType.HARDWARE
| UserShortcutType.QUICK_SETTINGS
);
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void optInAllValuesToSettings_optInValue_haveMatchString() {
clearShortcuts();
int shortcutTypes = UserShortcutType.SOFTWARE | UserShortcutType.HARDWARE;
@@ -187,6 +224,24 @@ public final class AccessibilityUtilTest {
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void optInAllValuesToSettings_optInValue_callsA11yManager() {
AccessibilityManager a11yManager =
AccessibilityTestUtils.setupMockAccessibilityManager(mContext);
Set<String> shortcutTargets = Set.of(MOCK_COMPONENT_NAME.flattenToString());
int shortcutTypes = UserShortcutType.SOFTWARE | UserShortcutType.HARDWARE
| UserShortcutType.QUICK_SETTINGS;
AccessibilityUtil.optInAllValuesToSettings(mContext, shortcutTypes, MOCK_COMPONENT_NAME);
verify(a11yManager).enableShortcutsForTargets(
/* enable= */ true, shortcutTypes,
shortcutTargets, UserHandle.myUserId());
verifyNoMoreInteractions(a11yManager);
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void optInValueToSettings_optInValue_haveMatchString() {
setShortcut(UserShortcutType.SOFTWARE, MOCK_COMPONENT_NAME.flattenToString());
@@ -199,6 +254,23 @@ public final class AccessibilityUtilTest {
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void optInValueToSettings_optInValue_callsA11yManager() {
AccessibilityManager a11yManager =
AccessibilityTestUtils.setupMockAccessibilityManager(mContext);
Set<String> shortcutTargets = Set.of(MOCK_COMPONENT_NAME2.flattenToString());
AccessibilityUtil.optInValueToSettings(
mContext, UserShortcutType.HARDWARE, MOCK_COMPONENT_NAME2);
verify(a11yManager).enableShortcutsForTargets(
/* enable= */ true, UserShortcutType.HARDWARE,
shortcutTargets, UserHandle.myUserId());
verifyNoMoreInteractions(a11yManager);
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void optInValueToSettings_optInTwoValues_haveMatchString() {
setShortcut(UserShortcutType.SOFTWARE, MOCK_COMPONENT_NAME.flattenToString());
@@ -213,6 +285,7 @@ public final class AccessibilityUtilTest {
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void optOutAllValuesToSettings_optOutValue_emptyString() {
setShortcut(UserShortcutType.SOFTWARE, MOCK_COMPONENT_NAME.flattenToString());
setShortcut(UserShortcutType.HARDWARE, MOCK_COMPONENT_NAME.flattenToString());
@@ -227,6 +300,27 @@ public final class AccessibilityUtilTest {
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void optOutAllValuesToSettings_optOutValue_callsA1yManager() {
AccessibilityManager a11yManager =
AccessibilityTestUtils.setupMockAccessibilityManager(mContext);
int shortcutTypes =
UserShortcutType.SOFTWARE | UserShortcutType.HARDWARE
| UserShortcutType.QUICK_SETTINGS;
Set<String> shortcutTargets = Set.of(MOCK_COMPONENT_NAME.flattenToString());
AccessibilityUtil.optOutAllValuesFromSettings(mContext, shortcutTypes,
MOCK_COMPONENT_NAME);
verify(a11yManager).enableShortcutsForTargets(
/* enable= */ false,
shortcutTypes,
shortcutTargets, UserHandle.myUserId());
verifyNoMoreInteractions(a11yManager);
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void optOutValueFromSettings_optOutValue_emptyString() {
setShortcut(UserShortcutType.SOFTWARE, MOCK_COMPONENT_NAME.flattenToString());
@@ -237,6 +331,7 @@ public final class AccessibilityUtilTest {
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void optOutValueFromSettings_optOutValue_haveMatchString() {
setShortcut(UserShortcutType.SOFTWARE, MOCK_COMPONENT_NAME.flattenToString(),
MOCK_COMPONENT_NAME2.flattenToString());
@@ -248,6 +343,55 @@ public final class AccessibilityUtilTest {
MOCK_COMPONENT_NAME.flattenToString());
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void optOutValueFromSettings_optOutValue_callsA11yManager() {
AccessibilityManager a11yManager =
AccessibilityTestUtils.setupMockAccessibilityManager(mContext);
Set<String> shortcutTargets = Set.of(MOCK_COMPONENT_NAME.flattenToString());
AccessibilityUtil.optOutValueFromSettings(
mContext, UserShortcutType.QUICK_SETTINGS, MOCK_COMPONENT_NAME);
verify(a11yManager).enableShortcutsForTargets(
/* enable= */ false, UserShortcutType.QUICK_SETTINGS,
shortcutTargets, UserHandle.myUserId());
verifyNoMoreInteractions(a11yManager);
}
@Test
public void convertKeyFromSettings_shortcutTypeSoftware() {
assertThat(AccessibilityUtil.convertKeyFromSettings(UserShortcutType.SOFTWARE))
.isEqualTo(Settings.Secure.ACCESSIBILITY_BUTTON_TARGETS);
}
@Test
public void convertKeyFromSettings_shortcutTypeHardware() {
assertThat(AccessibilityUtil.convertKeyFromSettings(UserShortcutType.HARDWARE))
.isEqualTo(Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGET_SERVICE);
}
@Test
public void convertKeyFromSettings_shortcutTypeTripleTap() {
assertThat(AccessibilityUtil.convertKeyFromSettings(UserShortcutType.TRIPLETAP))
.isEqualTo(Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED);
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void convertKeyFromSettings_shortcutTypeMultiFingersMultiTap() {
assertThat(AccessibilityUtil.convertKeyFromSettings(UserShortcutType.TWOFINGER_DOUBLETAP))
.isEqualTo(
Settings.Secure.ACCESSIBILITY_MAGNIFICATION_TWO_FINGER_TRIPLE_TAP_ENABLED);
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void convertKeyFromSettings_shortcutTypeQuickSettings() {
assertThat(AccessibilityUtil.convertKeyFromSettings(UserShortcutType.QUICK_SETTINGS))
.isEqualTo(Settings.Secure.ACCESSIBILITY_QS_TARGETS);
}
private AccessibilityServiceInfo getMockAccessibilityServiceInfo() {
final ApplicationInfo applicationInfo = new ApplicationInfo();
final ServiceInfo serviceInfo = new ServiceInfo();
@@ -287,12 +431,12 @@ public final class AccessibilityUtilTest {
shortcutComponents.add(componentName);
}
Settings.Secure.putString(mContext.getContentResolver(),
shortcutType == UserShortcutType.SOFTWARE ? SOFTWARE_SHORTCUT_KEY
: HARDWARE_SHORTCUT_KEY, shortcutComponents.toString());
ShortcutUtils.convertToKey(shortcutType), shortcutComponents.toString());
}
private void clearShortcuts() {
Settings.Secure.putString(mContext.getContentResolver(), SOFTWARE_SHORTCUT_KEY, "");
Settings.Secure.putString(mContext.getContentResolver(), HARDWARE_SHORTCUT_KEY, "");
Settings.Secure.putString(mContext.getContentResolver(), QUICK_SETTINGS_SHORTCUT_KEY, "");
}
}

View File

@@ -31,10 +31,13 @@ import android.app.settings.SettingsEnums;
import android.content.ComponentName;
import android.content.Context;
import android.os.Bundle;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.Flags;
import android.widget.PopupWindow;
import androidx.fragment.app.FragmentActivity;
@@ -49,6 +52,7 @@ import com.android.settings.testutils.shadow.ShadowFragment;
import com.android.settings.widget.SettingsMainSwitchPreference;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
@@ -65,6 +69,8 @@ import java.util.List;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowFragment.class)
public class ToggleColorInversionPreferenceFragmentTest {
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private final Context mContext = ApplicationProvider.getApplicationContext();
private TestToggleColorInversionPreferenceFragment mFragment;
private PreferenceScreen mScreen;
@@ -131,6 +137,7 @@ public class ToggleColorInversionPreferenceFragmentTest {
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void onPreferenceToggled_colorCorrectDisabled_shouldReturnTrueAndShowTooltipView() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, OFF);

View File

@@ -35,10 +35,13 @@ import android.app.settings.SettingsEnums;
import android.content.ComponentName;
import android.content.Context;
import android.os.Bundle;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.Flags;
import android.widget.PopupWindow;
import androidx.fragment.app.FragmentActivity;
@@ -54,6 +57,7 @@ import com.android.settings.widget.SettingsMainSwitchPreference;
import com.android.settingslib.widget.SelectorWithWidgetPreference;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
@@ -70,6 +74,8 @@ import java.util.List;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowFragment.class)
public class ToggleDaltonizerPreferenceFragmentTest {
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private final Context mContext = ApplicationProvider.getApplicationContext();
private TestToggleDaltonizerPreferenceFragment mFragment;
private PreferenceScreen mScreen;
@@ -154,6 +160,7 @@ public class ToggleDaltonizerPreferenceFragmentTest {
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void onPreferenceToggled_colorCorrectDisabled_shouldReturnTrueAndShowTooltipView() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, OFF);

View File

@@ -34,10 +34,11 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.icu.text.CaseMap;
import android.os.Bundle;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
@@ -76,6 +77,8 @@ import org.robolectric.annotation.LooperMode;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowApplication;
import java.util.Locale;
/** Tests for {@link ToggleFeaturePreferenceFragment} */
@RunWith(RobolectricTestRunner.class)
@LooperMode(LooperMode.Mode.LEGACY)
@@ -83,9 +86,8 @@ import org.robolectric.shadows.ShadowApplication;
ShadowFragment.class,
})
public class ToggleFeaturePreferenceFragmentTest {
@Rule
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private static final String PLACEHOLDER_PACKAGE_NAME = "com.placeholder.example";
private static final String PLACEHOLDER_CLASS_NAME = PLACEHOLDER_PACKAGE_NAME + ".placeholder";
@@ -295,6 +297,7 @@ public class ToggleFeaturePreferenceFragmentTest {
}
@Test
@DisableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
@Config(shadows = ShadowFragment.class)
public void onPreferenceToggledOnEnabledService_showTooltipView() {
mFragment.onPreferenceToggled(
@@ -304,7 +307,7 @@ public class ToggleFeaturePreferenceFragmentTest {
}
@Test
@RequiresFlagsEnabled(com.android.settings.accessibility.Flags.FLAG_REMOVE_QS_TOOLTIP_IN_SUW)
@EnableFlags(com.android.settings.accessibility.Flags.FLAG_REMOVE_QS_TOOLTIP_IN_SUW)
@Config(shadows = ShadowFragment.class)
public void onPreferenceToggledOnEnabledService_inSuw_toolTipViewShouldNotShow() {
Intent suwIntent = new Intent();
@@ -318,6 +321,7 @@ public class ToggleFeaturePreferenceFragmentTest {
}
@Test
@DisableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
@Config(shadows = ShadowFragment.class)
public void onPreferenceToggledOnEnabledService_tooltipViewShown_notShowTooltipView() {
mFragment.onPreferenceToggled(
@@ -348,7 +352,7 @@ public class ToggleFeaturePreferenceFragmentTest {
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ACCESSIBILITY_SHOW_APP_INFO_BUTTON)
@EnableFlags(Flags.FLAG_ACCESSIBILITY_SHOW_APP_INFO_BUTTON)
public void createAppInfoPreference_withValidComponentName() {
when(mPackageManager.isPackageAvailable(PLACEHOLDER_PACKAGE_NAME)).thenReturn(true);
mFragment.mComponentName = PLACEHOLDER_COMPONENT_NAME;
@@ -363,7 +367,7 @@ public class ToggleFeaturePreferenceFragmentTest {
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ACCESSIBILITY_SHOW_APP_INFO_BUTTON)
@EnableFlags(Flags.FLAG_ACCESSIBILITY_SHOW_APP_INFO_BUTTON)
public void createAppInfoPreference_noComponentName_shouldBeNull() {
mFragment.mComponentName = null;
@@ -373,7 +377,7 @@ public class ToggleFeaturePreferenceFragmentTest {
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ACCESSIBILITY_SHOW_APP_INFO_BUTTON)
@EnableFlags(Flags.FLAG_ACCESSIBILITY_SHOW_APP_INFO_BUTTON)
public void createAppInfoPreference_withUnavailablePackage_shouldBeNull() {
when(mPackageManager.isPackageAvailable(PLACEHOLDER_PACKAGE_NAME)).thenReturn(false);
mFragment.mComponentName = PLACEHOLDER_COMPONENT_NAME;
@@ -384,7 +388,7 @@ public class ToggleFeaturePreferenceFragmentTest {
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ACCESSIBILITY_SHOW_APP_INFO_BUTTON)
@EnableFlags(Flags.FLAG_ACCESSIBILITY_SHOW_APP_INFO_BUTTON)
public void createAppInfoPreference_inSetupWizard_shouldBeNull() {
when(mFragment.isAnySetupWizard()).thenReturn(true);
mFragment.mComponentName = PLACEHOLDER_COMPONENT_NAME;
@@ -421,6 +425,40 @@ public class ToggleFeaturePreferenceFragmentTest {
.isEqualTo(PLACEHOLDER_COMPONENT_NAME.flattenToString());
}
@Test
@EnableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
@Config(shadows = ShadowFragment.class)
public void showQuickSettingsTooltipIfNeeded_qsFlagOn_dontShowTooltipView() {
mFragment.showQuickSettingsTooltipIfNeeded(QuickSettingsTooltipType.GUIDE_TO_EDIT);
assertThat(getLatestPopupWindow()).isNull();
}
@Test
@EnableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
public void getShortcutTypeSummary_shortcutSummaryIsCorrectlySet() {
final PreferredShortcut userPreferredShortcut = new PreferredShortcut(
PLACEHOLDER_COMPONENT_NAME.flattenToString(),
UserShortcutType.HARDWARE | UserShortcutType.QUICK_SETTINGS);
putUserShortcutTypeIntoSharedPreference(mContext, userPreferredShortcut);
final ShortcutPreference shortcutPreference =
new ShortcutPreference(mContext, /* attrs= */ null);
shortcutPreference.setChecked(true);
shortcutPreference.setSettingsEditable(true);
mFragment.mComponentName = PLACEHOLDER_COMPONENT_NAME;
mFragment.mShortcutPreference = shortcutPreference;
String expected = CaseMap.toTitle().wholeString().noLowercase().apply(Locale.getDefault(),
/* iter= */ null,
mContext.getString(
R.string.accessibility_feature_shortcut_setting_summary_quick_settings)
+ ", "
+ mContext.getString(R.string.accessibility_shortcut_hardware_keyword));
String summary = mFragment.getShortcutTypeSummary(mContext).toString();
assertThat(summary).isEqualTo(expected);
}
private void putSecureStringIntoSettings(String key, String componentName) {
Settings.Secure.putString(mContext.getContentResolver(), key, componentName);
}

View File

@@ -30,6 +30,7 @@ import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import android.app.settings.SettingsEnums;
@@ -39,13 +40,16 @@ import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.icu.text.CaseMap;
import android.net.Uri;
import android.os.Bundle;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import android.os.UserHandle;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.DeviceConfig;
import android.provider.Settings;
import android.view.accessibility.AccessibilityManager;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference;
@@ -57,6 +61,7 @@ import com.android.settings.DialogCreatable;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.accessibility.AccessibilityDialogUtils.DialogType;
import com.android.settings.testutils.AccessibilityTestUtils;
import com.android.settings.testutils.shadow.ShadowDeviceConfig;
import com.android.settings.testutils.shadow.ShadowStorageManager;
import com.android.settings.testutils.shadow.ShadowUserManager;
@@ -80,6 +85,8 @@ import org.robolectric.util.ReflectionHelpers;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Set;
/** Tests for {@link ToggleScreenMagnificationPreferenceFragment}. */
@RunWith(RobolectricTestRunner.class)
@@ -92,7 +99,7 @@ import java.util.List;
public class ToggleScreenMagnificationPreferenceFragmentTest {
@Rule
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private static final String PLACEHOLDER_PACKAGE_NAME = "com.mock.example";
private static final String PLACEHOLDER_CLASS_NAME =
@@ -122,13 +129,15 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
private FragmentController<ToggleScreenMagnificationPreferenceFragment> mFragController;
private Context mContext;
private AccessibilityManager mAccessibilityManager;
private Resources mSpyResources;
private ShadowPackageManager mShadowPackageManager;
@Before
public void setUpTestFragment() {
mContext = ApplicationProvider.getApplicationContext();
mContext = spy(ApplicationProvider.getApplicationContext());
mAccessibilityManager = AccessibilityTestUtils.setupMockAccessibilityManager(mContext);
// Set up the fragment that support window magnification feature
mSpyResources = spy(mContext.getResources());
@@ -319,26 +328,27 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
@EnableFlags(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
public void hasMagnificationValuesInSettings_twoFingerTripleTapIsOn_isTrue() {
Settings.Secure.putInt(
mContext.getContentResolver(), TWO_FINGER_TRIPLE_TAP_SHORTCUT_KEY, ON);
assertThat(ToggleScreenMagnificationPreferenceFragment.hasMagnificationValuesInSettings(
mContext, UserShortcutType.TWOFINGERTRIPLETAP)).isTrue();
mContext, UserShortcutType.TWOFINGER_DOUBLETAP)).isTrue();
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
@EnableFlags(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
public void hasMagnificationValuesInSettings_twoFingerTripleTapIsOff_isFalse() {
Settings.Secure.putInt(
mContext.getContentResolver(), TWO_FINGER_TRIPLE_TAP_SHORTCUT_KEY, OFF);
assertThat(ToggleScreenMagnificationPreferenceFragment.hasMagnificationValuesInSettings(
mContext, UserShortcutType.TWOFINGERTRIPLETAP)).isFalse();
mContext, UserShortcutType.TWOFINGER_DOUBLETAP)).isFalse();
}
@Test
@DisableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
public void optInAllValuesToSettings_optInValue_haveMatchString() {
int shortcutTypes = UserShortcutType.SOFTWARE | UserShortcutType.TRIPLETAP;
@@ -351,9 +361,36 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
@EnableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
public void optInAllValuesToSettings_optInValue_callA11yManager() {
int shortcutTypes =
UserShortcutType.SOFTWARE | UserShortcutType.TRIPLETAP | UserShortcutType.HARDWARE
| UserShortcutType.QUICK_SETTINGS;
Set<String> shortcutTargets = Set.of(MAGNIFICATION_CONTROLLER_NAME);
ToggleScreenMagnificationPreferenceFragment.optInAllMagnificationValuesToSettings(mContext,
shortcutTypes);
verify(mAccessibilityManager).enableShortcutsForTargets(
/* enable= */ true, UserShortcutType.SOFTWARE,
shortcutTargets, UserHandle.myUserId());
verify(mAccessibilityManager).enableShortcutsForTargets(
/* enable= */ true, UserShortcutType.HARDWARE,
shortcutTargets, UserHandle.myUserId());
verify(mAccessibilityManager).enableShortcutsForTargets(
/* enable= */ true, UserShortcutType.QUICK_SETTINGS,
shortcutTargets, UserHandle.myUserId());
verify(mAccessibilityManager).enableShortcutsForTargets(
/* enable= */ true, UserShortcutType.TRIPLETAP,
shortcutTargets, UserHandle.myUserId());
verifyNoMoreInteractions(mAccessibilityManager);
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
@DisableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
public void optInAllValuesToSettings_twoFingerTripleTap_haveMatchString() {
int shortcutTypes = UserShortcutType.TWOFINGERTRIPLETAP;
int shortcutTypes = UserShortcutType.TWOFINGER_DOUBLETAP;
ToggleScreenMagnificationPreferenceFragment.optInAllMagnificationValuesToSettings(mContext,
shortcutTypes);
@@ -363,6 +400,7 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
}
@Test
@DisableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
public void optInAllValuesToSettings_existOtherValue_optInValue_haveMatchString() {
putStringIntoSettings(SOFTWARE_SHORTCUT_KEY, PLACEHOLDER_COMPONENT_NAME.flattenToString());
@@ -374,6 +412,7 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
}
@Test
@DisableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
public void optInAllValuesToSettings_software_sizeValueIsNull_putLargeSizeValue() {
ShadowSettings.ShadowSecure.reset();
@@ -441,6 +480,7 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
}
@Test
@DisableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
public void optOutAllValuesToSettings_optOutValue_emptyString() {
putStringIntoSettings(SOFTWARE_SHORTCUT_KEY, MAGNIFICATION_CONTROLLER_NAME);
putStringIntoSettings(HARDWARE_SHORTCUT_KEY, MAGNIFICATION_CONTROLLER_NAME);
@@ -457,19 +497,46 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
@EnableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
public void optOutAllValuesToSettings_optOutValue_callA11yManager() {
Set<String> shortcutTargets = Set.of(MAGNIFICATION_CONTROLLER_NAME);
putStringIntoSettings(SOFTWARE_SHORTCUT_KEY, MAGNIFICATION_CONTROLLER_NAME);
putStringIntoSettings(HARDWARE_SHORTCUT_KEY, MAGNIFICATION_CONTROLLER_NAME);
setMagnificationTripleTapEnabled(/* enabled= */ true);
int shortcutTypes =
UserShortcutType.SOFTWARE | UserShortcutType.HARDWARE | UserShortcutType.TRIPLETAP;
ToggleScreenMagnificationPreferenceFragment.optOutAllMagnificationValuesFromSettings(
mContext, shortcutTypes);
verify(mAccessibilityManager).enableShortcutsForTargets(
/* enable= */ false, UserShortcutType.SOFTWARE,
shortcutTargets, UserHandle.myUserId());
verify(mAccessibilityManager).enableShortcutsForTargets(
/* enable= */ false, UserShortcutType.HARDWARE,
shortcutTargets, UserHandle.myUserId());
verify(mAccessibilityManager).enableShortcutsForTargets(
/* enable= */ false, UserShortcutType.TRIPLETAP,
shortcutTargets, UserHandle.myUserId());
verifyNoMoreInteractions(mAccessibilityManager);
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
@DisableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
public void optOutAllValuesToSettings_twoFingerTripleTap_settingsValueIsOff() {
Settings.Secure.putInt(mContext.getContentResolver(),
TWO_FINGER_TRIPLE_TAP_SHORTCUT_KEY, ON);
ToggleScreenMagnificationPreferenceFragment.optOutAllMagnificationValuesFromSettings(
mContext, UserShortcutType.TWOFINGERTRIPLETAP);
mContext, UserShortcutType.TWOFINGER_DOUBLETAP);
assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
TWO_FINGER_TRIPLE_TAP_SHORTCUT_KEY, ON)).isEqualTo(OFF);
}
@Test
@DisableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
public void optOutValueFromSettings_existOtherValue_optOutValue_haveMatchString() {
putStringIntoSettings(SOFTWARE_SHORTCUT_KEY,
PLACEHOLDER_COMPONENT_NAME.flattenToString() + ":" + MAGNIFICATION_CONTROLLER_NAME);
@@ -526,7 +593,7 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
@EnableFlags(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
public void updateShortcutPreferenceData_hasTwoFingerTripleTapInSettings_assignToVariable() {
Settings.Secure.putInt(
mContext.getContentResolver(), TWO_FINGER_TRIPLE_TAP_SHORTCUT_KEY, ON);
@@ -536,14 +603,14 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
final int expectedType = PreferredShortcuts.retrieveUserShortcutType(mContext,
MAGNIFICATION_CONTROLLER_NAME);
assertThat(expectedType).isEqualTo(UserShortcutType.TWOFINGERTRIPLETAP);
assertThat(expectedType).isEqualTo(UserShortcutType.TWOFINGER_DOUBLETAP);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
@EnableFlags(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
public void updateShortcutPreferenceData_hasTwoFingerTripleTapInSharedPref_assignToVariable() {
final PreferredShortcut tripleTapShortcut = new PreferredShortcut(
MAGNIFICATION_CONTROLLER_NAME, UserShortcutType.TWOFINGERTRIPLETAP);
MAGNIFICATION_CONTROLLER_NAME, UserShortcutType.TWOFINGER_DOUBLETAP);
putUserShortcutTypeIntoSharedPreference(mContext, tripleTapShortcut);
mFragController.create(R.id.main_content, /* bundle= */ null).start().resume();
@@ -551,7 +618,7 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
final int expectedType = PreferredShortcuts.retrieveUserShortcutType(mContext,
MAGNIFICATION_CONTROLLER_NAME);
assertThat(expectedType).isEqualTo(UserShortcutType.TWOFINGERTRIPLETAP);
assertThat(expectedType).isEqualTo(UserShortcutType.TWOFINGER_DOUBLETAP);
}
@Test
@@ -590,7 +657,7 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
@EnableFlags(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
public void setupMagnificationEditShortcutDialog_twoFingerTripleTapOn_checkboxIsSavedValue() {
ToggleScreenMagnificationPreferenceFragment fragment =
mFragController.create(R.id.main_content, /* bundle= */
@@ -598,7 +665,7 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
final ShortcutPreference shortcutPreference = new ShortcutPreference(mContext, /* attrs= */
null);
final PreferredShortcut twoFingerTripleTapShortcut = new PreferredShortcut(
MAGNIFICATION_CONTROLLER_NAME, UserShortcutType.TWOFINGERTRIPLETAP);
MAGNIFICATION_CONTROLLER_NAME, UserShortcutType.TWOFINGER_DOUBLETAP);
fragment.mShortcutPreference = shortcutPreference;
PreferredShortcuts.saveUserShortcutType(mContext, twoFingerTripleTapShortcut);
@@ -607,7 +674,7 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
createEditShortcutDialog(fragment.getActivity()));
final int checkboxValue = fragment.getShortcutTypeCheckBoxValue();
assertThat(checkboxValue).isEqualTo(UserShortcutType.TWOFINGERTRIPLETAP);
assertThat(checkboxValue).isEqualTo(UserShortcutType.TWOFINGER_DOUBLETAP);
}
@Test
@@ -632,10 +699,10 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
@EnableFlags(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
public void restoreValueFromSavedInstanceState_twoFingerTripleTap_assignToVariable() {
final Bundle fragmentState =
createFragmentSavedInstanceState(UserShortcutType.TWOFINGERTRIPLETAP);
createFragmentSavedInstanceState(UserShortcutType.TWOFINGER_DOUBLETAP);
ToggleScreenMagnificationPreferenceFragment fragment = mFragController.get();
// Had to use reflection to pass the savedInstanceState when launching the fragment
ReflectionHelpers.setField(fragment, "mSavedFragmentState", fragmentState);
@@ -649,8 +716,8 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
final int expectedType = PreferredShortcuts.retrieveUserShortcutType(mContext,
MAGNIFICATION_CONTROLLER_NAME);
assertThat(value).isEqualTo(UserShortcutType.TWOFINGERTRIPLETAP);
assertThat(expectedType).isEqualTo(UserShortcutType.TWOFINGERTRIPLETAP);
assertThat(value).isEqualTo(UserShortcutType.TWOFINGER_DOUBLETAP);
assertThat(expectedType).isEqualTo(UserShortcutType.TWOFINGER_DOUBLETAP);
}
@Test
@@ -789,7 +856,7 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
@EnableFlags(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
public void getSummary_magnificationGestureEnabled_returnShortcutOnWithSummary() {
Settings.Secure.putInt(
mContext.getContentResolver(), TWO_FINGER_TRIPLE_TAP_SHORTCUT_KEY, ON);
@@ -803,7 +870,7 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
@EnableFlags(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
public void getSummary_magnificationGestureDisabled_returnShortcutOffWithSummary() {
Settings.Secure.putInt(
mContext.getContentResolver(), TWO_FINGER_TRIPLE_TAP_SHORTCUT_KEY, OFF);
@@ -817,6 +884,33 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
mContext.getText(R.string.magnification_feature_summary)));
}
@Test
@EnableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
public void getShortcutTypeSummary_shortcutSummaryIsCorrectlySet() {
final PreferredShortcut userPreferredShortcut = new PreferredShortcut(
MAGNIFICATION_CONTROLLER_NAME,
UserShortcutType.HARDWARE | UserShortcutType.QUICK_SETTINGS);
putUserShortcutTypeIntoSharedPreference(mContext, userPreferredShortcut);
final ShortcutPreference shortcutPreference =
new ShortcutPreference(mContext, /* attrs= */ null);
shortcutPreference.setChecked(true);
shortcutPreference.setSettingsEditable(true);
ToggleScreenMagnificationPreferenceFragment fragment =
mFragController.create(R.id.main_content, /* bundle= */
null).start().resume().get();
fragment.mShortcutPreference = shortcutPreference;
String expected = CaseMap.toTitle().wholeString().noLowercase().apply(Locale.getDefault(),
/* iter= */ null,
mContext.getString(
R.string.accessibility_feature_shortcut_setting_summary_quick_settings)
+ ", "
+ mContext.getString(R.string.accessibility_shortcut_hardware_keyword));
String summary = fragment.getShortcutTypeSummary(mContext).toString();
assertThat(summary).isEqualTo(expected);
}
private void putStringIntoSettings(String key, String componentName) {
Settings.Secure.putString(mContext.getContentResolver(), key, componentName);
}

View File

@@ -86,7 +86,10 @@ import java.util.Set;
/**
* Tests for {@link EditShortcutsPreferenceFragment}
*/
@Config(shadows = SettingsShadowResources.class)
@Config(shadows = {
SettingsShadowResources.class,
com.android.settings.testutils.shadow.ShadowAccessibilityManager.class
})
@RunWith(RobolectricTestRunner.class)
public class EditShortcutsPreferenceFragmentTest {
private static final int METRICS_CATEGORY = 123;
@@ -99,7 +102,8 @@ public class EditShortcutsPreferenceFragmentTest {
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private final Context mContext = ApplicationProvider.getApplicationContext();
private Context mContext = ApplicationProvider.getApplicationContext();
private FragmentActivity mActivity;
private FragmentScenario<EditShortcutsPreferenceFragment> mFragmentScenario;
@@ -291,7 +295,7 @@ public class EditShortcutsPreferenceFragmentTest {
mFragmentScenario.onFragment(fragment -> {
TwoStatePreference preference = fragment.findPreference(
mContext.getString(
R.string.accessibility_shortcut_two_fingers_double_tap_pref));
R.string.accessibility_shortcut_two_finger_double_tap_pref));
assertThat(preference.isChecked()).isTrue();
});
}
@@ -476,7 +480,26 @@ public class EditShortcutsPreferenceFragmentTest {
));
}
@Test
public void onQuickSettingsShortcutSettingChanged_preferredShortcutsUpdated() {
mFragmentScenario = createFragScenario(/* isInSuw= */ false);
mFragmentScenario.moveToState(Lifecycle.State.CREATED);
int currentPreferredShortcut =
PreferredShortcuts.retrieveUserShortcutType(mContext, TARGET);
assertThat(currentPreferredShortcut
& ShortcutConstants.UserShortcutType.QUICK_SETTINGS).isEqualTo(0);
ShortcutUtils.optInValueToSettings(
mContext, ShortcutConstants.UserShortcutType.QUICK_SETTINGS, TARGET);
// Calls onFragment so that the change to Setting is notified to its observer
mFragmentScenario.onFragment(fragment ->
assertThat(
PreferredShortcuts.retrieveUserShortcutType(
mContext, TARGET)
).isEqualTo(ShortcutConstants.UserShortcutType.QUICK_SETTINGS)
);
}
private void assertLaunchSubSettingWithCurrentTargetComponents(
String componentName, boolean isInSuw) {

View File

@@ -16,9 +16,10 @@
package com.android.settings.accessibility.shortcuts;
import static com.android.settings.testutils.AccessibilityTestUtils.setupMockAccessibilityManager;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
@@ -148,8 +149,7 @@ public class GestureShortcutOptionControllerTest {
}
private void enableTouchExploration(boolean enable) {
AccessibilityManager am = mock(AccessibilityManager.class);
when(mContext.getSystemService(AccessibilityManager.class)).thenReturn(am);
AccessibilityManager am = setupMockAccessibilityManager(mContext);
when(am.isTouchExplorationEnabled()).thenReturn(enable);
}
}

View File

@@ -0,0 +1,180 @@
/*
* 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.shortcuts;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.ComponentName;
import android.content.Context;
import android.os.UserHandle;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.Flags;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.internal.accessibility.common.ShortcutConstants;
import com.android.internal.accessibility.util.ShortcutUtils;
import com.android.settings.R;
import com.android.settings.testutils.AccessibilityTestUtils;
import com.android.settings.testutils.shadow.SettingsShadowResources;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Tests for {@link QuickSettingsShortcutOptionController}
*/
@RunWith(RobolectricTestRunner.class)
public class QuickSettingsShortcutOptionControllerTest {
private static final String PREF_KEY = "prefKey";
private static final ComponentName TARGET = new ComponentName("FakePackage", "FakeClass");
private static final String TARGET_FLATTEN = TARGET.flattenToString();
private static final ComponentName TARGET_TILE =
new ComponentName("FakePackage", "FakeTileClass");
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private final Context mContext = spy(ApplicationProvider.getApplicationContext());
private QuickSettingsShortcutOptionController mController;
private ShortcutOptionPreference mShortcutOptionPreference;
private AccessibilityManager mAccessibilityManager;
private PreferenceScreen mPreferenceScreen;
@Before
public void setUp() {
SettingsShadowResources.overrideResource(
com.android.internal.R.bool.config_quickSettingsSupported, true);
mAccessibilityManager = AccessibilityTestUtils.setupMockAccessibilityManager(mContext);
mController = new QuickSettingsShortcutOptionController(
mContext, PREF_KEY);
mController.setShortcutTargets(Set.of(TARGET_FLATTEN));
mShortcutOptionPreference = new ShortcutOptionPreference(mContext);
mShortcutOptionPreference.setKey(PREF_KEY);
mPreferenceScreen = new PreferenceManager(mContext).createPreferenceScreen(mContext);
mPreferenceScreen.addPreference(mShortcutOptionPreference);
}
@Test
public void displayPreference_verifyScreenTextSet() {
mController.displayPreference(mPreferenceScreen);
assertThat(mShortcutOptionPreference.getTitle().toString()).isEqualTo(
mContext.getString(
R.string.accessibility_shortcut_edit_dialog_title_quick_settings));
assertThat(mShortcutOptionPreference.getSummary().toString()).isEqualTo(
mContext.getString(
R.string.accessibility_shortcut_edit_dialog_summary_quick_settings));
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void isShortcutAvailable_a11yQsShortcutFlagDisabled_returnsFalse() {
assertThat(mController.isShortcutAvailable()).isFalse();
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void isShortcutAvailable_qsNotSupported_returnsFalse() {
SettingsShadowResources.overrideResource(
com.android.internal.R.bool.config_quickSettingsSupported, false);
assertThat(mController.isShortcutAvailable()).isFalse();
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void isShortcutAvailable_qsTileProvided_returnsTrue() {
when(mAccessibilityManager.getA11yFeatureToTileMap(UserHandle.myUserId()))
.thenReturn(Map.of(TARGET, TARGET_TILE));
assertThat(mController.isShortcutAvailable()).isTrue();
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void isShortcutAvailable_qsTileNotProvided_returnsFalse() {
when(mAccessibilityManager.getA11yFeatureToTileMap(UserHandle.myUserId()))
.thenReturn(Collections.emptyMap());
assertThat(mController.isShortcutAvailable()).isFalse();
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void isShortcutAvailable_qsTileProvided_invalidUseCase_returnFalse() {
AccessibilityServiceInfo mockStandardA11yService =
AccessibilityTestUtils.createAccessibilityServiceInfo(
mContext, TARGET, /* isAlwaysOnService= */ false);
when(mAccessibilityManager.getA11yFeatureToTileMap(UserHandle.myUserId()))
.thenReturn(Map.of(TARGET, TARGET_TILE));
// setup target as a standard a11y service
when(mAccessibilityManager.getInstalledAccessibilityServiceList())
.thenReturn(List.of(mockStandardA11yService));
assertThat(mController.isShortcutAvailable()).isFalse();
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void isShortcutAvailable_qsTileProvided_validUseCase_returnTrue() {
AccessibilityServiceInfo mockAlwaysOnA11yService =
AccessibilityTestUtils.createAccessibilityServiceInfo(
mContext, TARGET, /* isAlwaysOnService= */ true);
when(mAccessibilityManager.getA11yFeatureToTileMap(UserHandle.myUserId()))
.thenReturn(Map.of(TARGET, TARGET_TILE));
// setup target as a always-on a11y service
when(mAccessibilityManager.getInstalledAccessibilityServiceList())
.thenReturn(List.of(mockAlwaysOnA11yService));
assertThat(mController.isShortcutAvailable()).isTrue();
}
@Test
public void isChecked_targetUseQsShortcut_returnTrue() {
ShortcutUtils.optInValueToSettings(
mContext, ShortcutConstants.UserShortcutType.QUICK_SETTINGS, TARGET_FLATTEN);
assertThat(mController.isChecked()).isTrue();
}
@Test
public void isChecked_targetNotUseQsShortcut_returnFalse() {
ShortcutUtils.optOutValueFromSettings(
mContext, ShortcutConstants.UserShortcutType.QUICK_SETTINGS, TARGET_FLATTEN);
assertThat(mController.isChecked()).isFalse();
}
}

View File

@@ -22,6 +22,8 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import android.accessibilityservice.AccessibilityServiceInfo;
@@ -30,14 +32,15 @@ import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.os.Build;
import android.os.UserHandle;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.Flags;
import androidx.fragment.app.FragmentActivity;
@@ -48,18 +51,18 @@ import com.android.settings.SettingsActivity;
import com.android.settings.SubSettings;
import com.android.settings.accessibility.AccessibilityButtonFragment;
import com.android.settings.accessibility.FloatingMenuSizePreferenceController;
import com.android.settings.testutils.AccessibilityTestUtils;
import com.android.settings.utils.AnnotationSpan;
import com.android.settingslib.accessibility.AccessibilityUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.Shadows;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.List;
import java.util.Set;
@@ -77,18 +80,23 @@ public class SoftwareShortcutOptionPreferenceControllerTest {
new ComponentName("FakePackage", "StandardA11yService");
private static final String SOFTWARE_SHORTCUT_SETTING_NAME =
Settings.Secure.ACCESSIBILITY_BUTTON_TARGETS;
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private Context mContext;
private AccessibilityManager mAccessibilityManager;
private TestSoftwareShortcutOptionPreferenceController mController;
@Before
public void setUp() {
mContext = spy(Robolectric.buildActivity(FragmentActivity.class).get());
mAccessibilityManager = AccessibilityTestUtils.setupMockAccessibilityManager(mContext);
AccessibilityServiceInfo mAlwaysOnServiceInfo = createAccessibilityServiceInfo(
TARGET_ALWAYS_ON_A11Y_SERVICE, /* isAlwaysOnService= */ true);
AccessibilityServiceInfo mStandardServiceInfo = createAccessibilityServiceInfo(
TARGET_STANDARD_A11Y_SERVICE, /* isAlwaysOnService= */ false);
AccessibilityServiceInfo mAlwaysOnServiceInfo =
AccessibilityTestUtils.createAccessibilityServiceInfo(
mContext, TARGET_ALWAYS_ON_A11Y_SERVICE, /* isAlwaysOnService= */ true);
AccessibilityServiceInfo mStandardServiceInfo =
AccessibilityTestUtils.createAccessibilityServiceInfo(
mContext, TARGET_STANDARD_A11Y_SERVICE, /* isAlwaysOnService= */ false);
AccessibilityManager am = mock(AccessibilityManager.class);
when(mContext.getSystemService(Context.ACCESSIBILITY_SERVICE)).thenReturn(am);
when(am.getInstalledAccessibilityServiceList()).thenReturn(
@@ -170,6 +178,7 @@ public class SoftwareShortcutOptionPreferenceControllerTest {
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_enableShortcut_shortcutTurnedOn() {
String target = TARGET_ALWAYS_ON_A11Y_SERVICE.flattenToString();
mController.setShortcutTargets(Set.of(target));
@@ -185,6 +194,27 @@ public class SoftwareShortcutOptionPreferenceControllerTest {
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_enableShortcut_callA11yManager() {
String target = TARGET_ALWAYS_ON_A11Y_SERVICE.flattenToString();
mController.setShortcutTargets(Set.of(target));
assertThat(ShortcutUtils.isComponentIdExistingInSettings(
mContext, ShortcutConstants.UserShortcutType.SOFTWARE, target
)).isFalse();
mController.enableShortcutForTargets(true);
verify(mAccessibilityManager).enableShortcutsForTargets(
/* enable= */ true,
ShortcutConstants.UserShortcutType.SOFTWARE,
Set.of(target),
UserHandle.myUserId()
);
verifyNoMoreInteractions(mAccessibilityManager);
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_disableShortcut_shortcutTurnedOff() {
String target = TARGET_ALWAYS_ON_A11Y_SERVICE.flattenToString();
ShortcutUtils.optInValueToSettings(
@@ -202,6 +232,29 @@ public class SoftwareShortcutOptionPreferenceControllerTest {
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_disableShortcut_callA11yManager() {
String target = TARGET_ALWAYS_ON_A11Y_SERVICE.flattenToString();
ShortcutUtils.optInValueToSettings(
mContext, ShortcutConstants.UserShortcutType.SOFTWARE, target);
assertThat(ShortcutUtils.isComponentIdExistingInSettings(
mContext, ShortcutConstants.UserShortcutType.SOFTWARE, target
)).isTrue();
mController.setShortcutTargets(Set.of(target));
mController.enableShortcutForTargets(false);
verify(mAccessibilityManager).enableShortcutsForTargets(
/* enable= */ false,
ShortcutConstants.UserShortcutType.SOFTWARE,
Set.of(target),
UserHandle.myUserId()
);
verifyNoMoreInteractions(mAccessibilityManager);
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_enableShortcutWithMagnification_menuSizeIncreased() {
mController.setShortcutTargets(Set.of(TARGET_MAGNIFICATION));
@@ -216,6 +269,7 @@ public class SoftwareShortcutOptionPreferenceControllerTest {
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_enableShortcutWithMagnification_userConfigureSmallMenuSize_menuSizeNotChanged() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_FLOATING_MENU_SIZE,
@@ -233,6 +287,7 @@ public class SoftwareShortcutOptionPreferenceControllerTest {
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_enableAlwaysOnServiceShortcut_turnsOnAlwaysOnService() {
mController.setShortcutTargets(
Set.of(TARGET_ALWAYS_ON_A11Y_SERVICE.flattenToString()));
@@ -244,6 +299,7 @@ public class SoftwareShortcutOptionPreferenceControllerTest {
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_disableAlwaysOnServiceShortcut_turnsOffAlwaysOnService() {
mController.setShortcutTargets(
Set.of(TARGET_ALWAYS_ON_A11Y_SERVICE.flattenToString()));
@@ -255,6 +311,7 @@ public class SoftwareShortcutOptionPreferenceControllerTest {
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_enableStandardServiceShortcut_wontTurnOnService() {
mController.setShortcutTargets(
Set.of(TARGET_STANDARD_A11Y_SERVICE.flattenToString()));
@@ -266,6 +323,7 @@ public class SoftwareShortcutOptionPreferenceControllerTest {
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_disableStandardServiceShortcutWithServiceOn_wontTurnOffService() {
mController.setShortcutTargets(
Set.of(TARGET_STANDARD_A11Y_SERVICE.flattenToString()));
@@ -288,32 +346,6 @@ public class SoftwareShortcutOptionPreferenceControllerTest {
assertThat(intent.getExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)).isEqualTo(page);
}
private AccessibilityServiceInfo createAccessibilityServiceInfo(
ComponentName componentName, boolean isAlwaysOnService) {
final ApplicationInfo applicationInfo = new ApplicationInfo();
applicationInfo.targetSdkVersion = Build.VERSION_CODES.R;
final ServiceInfo serviceInfo = new ServiceInfo();
applicationInfo.packageName = componentName.getPackageName();
serviceInfo.packageName = componentName.getPackageName();
serviceInfo.name = componentName.getClassName();
serviceInfo.applicationInfo = applicationInfo;
final ResolveInfo resolveInfo = new ResolveInfo();
resolveInfo.serviceInfo = serviceInfo;
try {
final AccessibilityServiceInfo info = new AccessibilityServiceInfo(resolveInfo,
mContext);
info.setComponentName(componentName);
if (isAlwaysOnService) {
info.flags |= AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON;
}
return info;
} catch (XmlPullParserException | IOException e) {
// Do nothing
}
return null;
}
private static class TestSoftwareShortcutOptionPreferenceController
extends SoftwareShortcutOptionPreferenceController {

View File

@@ -21,19 +21,32 @@ import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_U
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import android.content.ComponentName;
import android.content.Context;
import android.icu.text.MessageFormat;
import android.os.UserHandle;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.Flags;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.internal.accessibility.common.ShortcutConstants;
import com.android.settings.R;
import com.android.settings.accessibility.AccessibilityUtil;
import com.android.settings.testutils.AccessibilityTestUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@@ -50,13 +63,17 @@ public class TripleTapShortcutOptionControllerTest {
"com.android.server.accessibility.MagnificationController";
private static final String TARGET_FAKE =
new ComponentName("FakePackage", "FakeClass").flattenToString();
private final Context mContext = ApplicationProvider.getApplicationContext();
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private final Context mContext = spy(ApplicationProvider.getApplicationContext());
private TripleTapShortcutOptionController mController;
private ShortcutOptionPreference mShortcutOptionPreference;
private AccessibilityManager mAccessibilityManager;
private PreferenceScreen mPreferenceScreen;
@Before
public void setUp() {
mAccessibilityManager = AccessibilityTestUtils.setupMockAccessibilityManager(mContext);
mController = new TripleTapShortcutOptionController(mContext, PREF_KEY);
mController.setShortcutTargets(Set.of(TARGET_MAGNIFICATION));
mShortcutOptionPreference = new ShortcutOptionPreference(mContext);
@@ -147,19 +164,26 @@ public class TripleTapShortcutOptionControllerTest {
@Test
public void isChecked_tripleTapConfigured_returnTrue() {
mController.enableShortcutForTargets(true);
Settings.Secure.putInt(
mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED,
AccessibilityUtil.State.ON);
assertThat(mController.isChecked()).isTrue();
}
@Test
public void isChecked_tripleTapNotConfigured_returnFalse() {
mController.enableShortcutForTargets(false);
Settings.Secure.putInt(
mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED,
AccessibilityUtil.State.OFF);
assertThat(mController.isChecked()).isFalse();
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_enableShortcut_settingUpdated() {
mController.enableShortcutForTargets(true);
@@ -172,6 +196,21 @@ public class TripleTapShortcutOptionControllerTest {
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_enableShortcut_callA11yManager() {
mController.enableShortcutForTargets(true);
verify(mAccessibilityManager).enableShortcutsForTargets(
/* enable= */ true,
ShortcutConstants.UserShortcutType.TRIPLETAP,
Set.of(TARGET_MAGNIFICATION),
UserHandle.myUserId()
);
verifyNoMoreInteractions(mAccessibilityManager);
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_disableShortcut_settingUpdated() {
mController.enableShortcutForTargets(false);
@@ -182,4 +221,18 @@ public class TripleTapShortcutOptionControllerTest {
AccessibilityUtil.State.OFF)
).isEqualTo(AccessibilityUtil.State.OFF);
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_disableShortcut_callA11yManager() {
mController.enableShortcutForTargets(false);
verify(mAccessibilityManager).enableShortcutsForTargets(
/* enable= */ false,
ShortcutConstants.UserShortcutType.TRIPLETAP,
Set.of(TARGET_MAGNIFICATION),
UserHandle.myUserId()
);
verifyNoMoreInteractions(mAccessibilityManager);
}
}

View File

@@ -18,22 +18,29 @@ package com.android.settings.accessibility.shortcuts;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import android.content.ComponentName;
import android.content.Context;
import android.icu.text.MessageFormat;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import android.os.UserHandle;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import android.view.accessibility.AccessibilityManager;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.internal.accessibility.common.ShortcutConstants;
import com.android.server.accessibility.Flags;
import com.android.settings.R;
import com.android.settings.accessibility.AccessibilityUtil;
import com.android.settings.testutils.AccessibilityTestUtils;
import org.junit.Before;
import org.junit.Rule;
@@ -44,26 +51,28 @@ import org.robolectric.RobolectricTestRunner;
import java.util.Set;
/**
* Tests for {@link TwoFingersDoubleTapShortcutOptionController}
* Tests for {@link TwoFingerDoubleTapShortcutOptionController}
*/
@RunWith(RobolectricTestRunner.class)
public class TwoFingersDoubleTapShortcutOptionControllerTest {
public class TwoFingerDoubleTapShortcutOptionControllerTest {
private static final String PREF_KEY = "prefKey";
private static final String TARGET_MAGNIFICATION =
"com.android.server.accessibility.MagnificationController";
private static final String TARGET_FAKE =
new ComponentName("FakePackage", "FakeClass").flattenToString();
@Rule
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
private final Context mContext = ApplicationProvider.getApplicationContext();
private TwoFingersDoubleTapShortcutOptionController mController;
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private final Context mContext = spy(ApplicationProvider.getApplicationContext());
private AccessibilityManager mAccessibilityManager;
private TwoFingerDoubleTapShortcutOptionController mController;
private ShortcutOptionPreference mShortcutOptionPreference;
private PreferenceScreen mPreferenceScreen;
@Before
public void setUp() {
mController = new TwoFingersDoubleTapShortcutOptionController(mContext, PREF_KEY);
mAccessibilityManager = AccessibilityTestUtils.setupMockAccessibilityManager(mContext);
mController = new TwoFingerDoubleTapShortcutOptionController(mContext, PREF_KEY);
mController.setShortcutTargets(Set.of(TARGET_MAGNIFICATION));
mShortcutOptionPreference = new ShortcutOptionPreference(mContext);
mShortcutOptionPreference.setKey(PREF_KEY);
@@ -84,30 +93,30 @@ public class TwoFingersDoubleTapShortcutOptionControllerTest {
2));
}
@RequiresFlagsDisabled(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
@Test
@DisableFlags(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
public void isShortcutAvailable_featureFlagTurnedOff_returnFalse() {
assertThat(mController.isShortcutAvailable()).isFalse();
}
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
@Test
@EnableFlags(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
public void isShortcutAvailable_multipleTargets_returnFalse() {
mController.setShortcutTargets(Set.of(TARGET_FAKE, TARGET_MAGNIFICATION));
assertThat(mController.isShortcutAvailable()).isFalse();
}
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
@Test
@EnableFlags(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
public void isShortcutAvailable_magnificationTargetOnly_returnTrue() {
mController.setShortcutTargets(Set.of(TARGET_MAGNIFICATION));
assertThat(mController.isShortcutAvailable()).isTrue();
}
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
@Test
@EnableFlags(Flags.FLAG_ENABLE_MAGNIFICATION_MULTIPLE_FINGER_MULTIPLE_TAP_GESTURE)
public void isShortcutAvailable_nonMagnificationTarget_returnFalse() {
mController.setShortcutTargets(Set.of(TARGET_FAKE));
@@ -116,19 +125,26 @@ public class TwoFingersDoubleTapShortcutOptionControllerTest {
@Test
public void isChecked_twoFingersDoubleTapConfigured_returnTrue() {
mController.enableShortcutForTargets(true);
Settings.Secure.putInt(
mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_MAGNIFICATION_TWO_FINGER_TRIPLE_TAP_ENABLED,
AccessibilityUtil.State.ON);
assertThat(mController.isChecked()).isTrue();
}
@Test
public void isChecked_twoFingersDoubleTapNotConfigured_returnFalse() {
mController.enableShortcutForTargets(false);
Settings.Secure.putInt(
mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_MAGNIFICATION_TWO_FINGER_TRIPLE_TAP_ENABLED,
AccessibilityUtil.State.OFF);
assertThat(mController.isChecked()).isFalse();
}
@Test
@DisableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_enableShortcut_settingUpdated() {
mController.enableShortcutForTargets(true);
@@ -141,6 +157,21 @@ public class TwoFingersDoubleTapShortcutOptionControllerTest {
}
@Test
@EnableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_enableShortcut_callA11yManager() {
mController.enableShortcutForTargets(true);
verify(mAccessibilityManager).enableShortcutsForTargets(
/* enable= */ true,
ShortcutConstants.UserShortcutType.TWOFINGER_DOUBLETAP,
Set.of(TARGET_MAGNIFICATION),
UserHandle.myUserId()
);
verifyNoMoreInteractions(mAccessibilityManager);
}
@Test
@DisableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_disableShortcut_settingUpdated() {
mController.enableShortcutForTargets(false);
@@ -151,4 +182,18 @@ public class TwoFingersDoubleTapShortcutOptionControllerTest {
AccessibilityUtil.State.OFF)
).isEqualTo(AccessibilityUtil.State.OFF);
}
@Test
@EnableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_disableShortcut_callA11yManager() {
mController.enableShortcutForTargets(false);
verify(mAccessibilityManager).enableShortcutsForTargets(
/* enable= */ false,
ShortcutConstants.UserShortcutType.TWOFINGER_DOUBLETAP,
Set.of(TARGET_MAGNIFICATION),
UserHandle.myUserId()
);
verifyNoMoreInteractions(mAccessibilityManager);
}
}

View File

@@ -18,8 +18,18 @@ package com.android.settings.accessibility.shortcuts;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import android.content.ComponentName;
import android.content.Context;
import android.os.UserHandle;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.Flags;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
@@ -28,8 +38,10 @@ import androidx.test.core.app.ApplicationProvider;
import com.android.internal.accessibility.common.ShortcutConstants;
import com.android.internal.accessibility.util.ShortcutUtils;
import com.android.settings.R;
import com.android.settings.testutils.AccessibilityTestUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@@ -45,7 +57,11 @@ public class VolumeKeysShortcutOptionControllerTest {
private static final String PREF_KEY = "prefKey";
private static final String TARGET =
new ComponentName("FakePackage", "FakeClass").flattenToString();
private final Context mContext = ApplicationProvider.getApplicationContext();
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private final Context mContext = spy(ApplicationProvider.getApplicationContext());
private AccessibilityManager mAccessibilityManager;
private VolumeKeysShortcutOptionController mController;
private ShortcutOptionPreference mShortcutOptionPreference;
@@ -53,6 +69,7 @@ public class VolumeKeysShortcutOptionControllerTest {
@Before
public void setUp() {
mAccessibilityManager = AccessibilityTestUtils.setupMockAccessibilityManager(mContext);
mController = new VolumeKeysShortcutOptionController(
mContext, PREF_KEY);
mController.setShortcutTargets(Set.of(TARGET));
@@ -94,6 +111,7 @@ public class VolumeKeysShortcutOptionControllerTest {
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_enableVolumeKeysShortcut_shortcutSet() {
mController.enableShortcutForTargets(true);
@@ -103,6 +121,21 @@ public class VolumeKeysShortcutOptionControllerTest {
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_enableVolumeKeysShortcut_callA11yManager() {
mController.enableShortcutForTargets(true);
verify(mAccessibilityManager).enableShortcutsForTargets(
/* enable= */ true,
ShortcutConstants.UserShortcutType.HARDWARE,
Set.of(TARGET),
UserHandle.myUserId()
);
verifyNoMoreInteractions(mAccessibilityManager);
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_disableVolumeKeysShortcut_shortcutNotSet() {
mController.enableShortcutForTargets(false);
@@ -110,4 +143,18 @@ public class VolumeKeysShortcutOptionControllerTest {
ShortcutUtils.isComponentIdExistingInSettings(
mContext, ShortcutConstants.UserShortcutType.HARDWARE, TARGET)).isFalse();
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void enableShortcutForTargets_disableVolumeKeysShortcut_callA11yManager() {
mController.enableShortcutForTargets(false);
verify(mAccessibilityManager).enableShortcutsForTargets(
/* enable= */ false,
ShortcutConstants.UserShortcutType.HARDWARE,
Set.of(TARGET),
UserHandle.myUserId()
);
verifyNoMoreInteractions(mAccessibilityManager);
}
}

View File

@@ -20,11 +20,25 @@ import static android.provider.Settings.Secure.ACCESSIBILITY_BUTTON_MODE_FLOATIN
import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON;
import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_GESTURAL;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.os.Build;
import android.provider.Settings;
import android.view.accessibility.AccessibilityManager;
import com.android.settings.testutils.shadow.SettingsShadowResources;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
/**
* Utility class for common methods used in the accessibility feature related tests
*/
@@ -47,4 +61,39 @@ public class AccessibilityTestUtils {
NAV_BAR_MODE_3BUTTON);
}
}
/**
* Returns a mock {@link AccessibilityManager}
*/
public static AccessibilityManager setupMockAccessibilityManager(Context mockContext) {
AccessibilityManager am = mock(AccessibilityManager.class);
when(mockContext.getSystemService(AccessibilityManager.class)).thenReturn(am);
return am;
}
public static AccessibilityServiceInfo createAccessibilityServiceInfo(
Context context, ComponentName componentName, boolean isAlwaysOnService) {
final ApplicationInfo applicationInfo = new ApplicationInfo();
applicationInfo.targetSdkVersion = Build.VERSION_CODES.R;
final ServiceInfo serviceInfo = new ServiceInfo();
applicationInfo.packageName = componentName.getPackageName();
serviceInfo.packageName = componentName.getPackageName();
serviceInfo.name = componentName.getClassName();
serviceInfo.applicationInfo = applicationInfo;
final ResolveInfo resolveInfo = new ResolveInfo();
resolveInfo.serviceInfo = serviceInfo;
try {
final AccessibilityServiceInfo info = new AccessibilityServiceInfo(resolveInfo,
context);
info.setComponentName(componentName);
if (isAlwaysOnService) {
info.flags |= AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON;
}
return info;
} catch (XmlPullParserException | IOException e) {
// Do nothing
}
return null;
}
}

View File

@@ -0,0 +1,42 @@
/*
* 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.testutils.shadow;
import android.annotation.UserIdInt;
import android.content.ComponentName;
import android.view.accessibility.AccessibilityManager;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import java.util.Collections;
import java.util.Map;
/**
* Shadow of {@link AccessibilityManager} with the hidden methods
*/
@Implements(AccessibilityManager.class)
public class ShadowAccessibilityManager extends org.robolectric.shadows.ShadowAccessibilityManager {
/**
* Implements a hidden method {@link AccessibilityManager.getA11yFeatureToTileMap} and returns
* an empty map.
*/
@Implementation
public Map<ComponentName, ComponentName> getA11yFeatureToTileMap(@UserIdInt int userId) {
return Collections.emptyMap();
}
}

View File

@@ -25,16 +25,22 @@ import static com.google.common.truth.Truth.assertThat;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import android.view.accessibility.Flags;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.internal.accessibility.common.ShortcutConstants;
import com.android.internal.accessibility.common.ShortcutConstants.UserShortcutType;
import com.android.internal.accessibility.util.ShortcutUtils;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -54,7 +60,8 @@ public class PreferredShortcutsTest {
CLASS_NAME_2);
private static final ContentResolver sContentResolver =
ApplicationProvider.getApplicationContext().getContentResolver();
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private final Context mContext = ApplicationProvider.getApplicationContext();
@Before
@@ -166,11 +173,48 @@ public class PreferredShortcutsTest {
.isEqualTo(target2ShortcutTypes);
}
@Test
@EnableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void updatePreferredShortcutFromSettings_colorInversionWithQsAndSoftwareShortcut_preferredShortcutsMatches() {
String target = COLOR_INVERSION_COMPONENT_NAME.flattenToString();
Settings.Secure.putString(sContentResolver,
Settings.Secure.ACCESSIBILITY_BUTTON_TARGETS, target);
Settings.Secure.putString(sContentResolver,
Settings.Secure.ACCESSIBILITY_QS_TARGETS, target);
PreferredShortcuts.updatePreferredShortcutsFromSettings(mContext, Set.of(target));
int savedPreferredShortcut = PreferredShortcuts.retrieveUserShortcutType(
mContext, target);
assertThat(savedPreferredShortcut).isEqualTo(
UserShortcutType.SOFTWARE | UserShortcutType.QUICK_SETTINGS);
}
@Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT)
public void updatePreferredShortcutFromSettings_colorInversionWithQsAndHardwareShortcut_qsShortcutNotSaved() {
String target = COLOR_INVERSION_COMPONENT_NAME.flattenToString();
Settings.Secure.putString(sContentResolver,
Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGET_SERVICE, target);
Settings.Secure.putString(sContentResolver,
Settings.Secure.ACCESSIBILITY_QS_TARGETS, target);
assertThat(!android.view.accessibility.Flags.a11yQsShortcut()).isTrue();
PreferredShortcuts.updatePreferredShortcutsFromSettings(mContext, Set.of(target));
int savedPreferredShortcut = PreferredShortcuts.retrieveUserShortcutType(
mContext, target);
assertThat(savedPreferredShortcut).isEqualTo(UserShortcutType.HARDWARE);
}
private static void clearShortcuts() {
Settings.Secure.putString(sContentResolver,
Settings.Secure.ACCESSIBILITY_BUTTON_TARGETS, "");
Settings.Secure.putString(sContentResolver,
Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGET_SERVICE, "");
Settings.Secure.putString(sContentResolver,
Settings.Secure.ACCESSIBILITY_QS_TARGETS, "");
Settings.Secure.putInt(
sContentResolver,
Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED,
@@ -179,5 +223,7 @@ public class PreferredShortcutsTest {
sContentResolver,
Settings.Secure.ACCESSIBILITY_MAGNIFICATION_TWO_FINGER_TRIPLE_TAP_ENABLED,
AccessibilityUtil.State.OFF);
PreferredShortcuts.clearPreferredShortcuts(ApplicationProvider.getApplicationContext());
}
}