Support accessibility shortcut secondary action (7/n)

Implements sharedPreference to keep the user preferred shortcut for each
service.

Bug: 142530063
Bug: 142531156
Test: make RunSettingsRoboTests ROBOTEST_FILTER=ToggleFeaturePreferenceFragmentTest
Test: make RunSettingsRoboTests2
Change-Id: I2a7c54ddc32764f6d88f2efed87982d469acc183
This commit is contained in:
menghanli
2020-01-09 14:27:47 +08:00
parent b642f3cb10
commit 334dc4f20b
9 changed files with 461 additions and 249 deletions

View File

@@ -16,6 +16,8 @@
package com.android.settings.accessibility;
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;
@@ -29,6 +31,7 @@ import androidx.annotation.XmlRes;
import androidx.fragment.app.FragmentActivity;
import com.android.settings.R;
import com.android.settings.accessibility.ToggleFeaturePreferenceFragment.AccessibilityUserShortcutType;
import com.android.settings.widget.SwitchBar;
import org.junit.Test;
@@ -36,11 +39,77 @@ import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadows.androidx.fragment.FragmentController;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
@RunWith(RobolectricTestRunner.class)
public class ToggleFeaturePreferenceFragmentTest {
private ToggleFeaturePreferenceFragmentTestable mFragment;
private static final String TEST_SERVICE_KEY_1 = "abc:111";
private static final String TEST_SERVICE_KEY_2 = "mno:222";
private static final String TEST_SERVICE_KEY_3 = "xyz:333";
private static final String TEST_SERVICE_NAME_1 = "abc";
private static final int TEST_SERVICE_VALUE_1 = 111;
@Test
public void a11yUserShortcutType_setConcatString_shouldReturnTargetValue() {
final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(
TEST_SERVICE_KEY_1);
assertThat(shortcut.getComponentName()).isEqualTo(TEST_SERVICE_NAME_1);
assertThat(shortcut.getUserShortcutType()).isEqualTo(TEST_SERVICE_VALUE_1);
}
@Test
public void a11yUserShortcutType_shouldUpdateConcatString() {
final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(
TEST_SERVICE_KEY_2);
shortcut.setComponentName(TEST_SERVICE_NAME_1);
shortcut.setUserShortcutType(TEST_SERVICE_VALUE_1);
assertThat(shortcut.flattenToString()).isEqualTo(TEST_SERVICE_KEY_1);
}
@Test
public void stringSet_convertA11yPreferredShortcut_shouldRemoveTarget() {
Set<String> mySet = new HashSet<>();
mySet.add(TEST_SERVICE_KEY_1);
mySet.add(TEST_SERVICE_KEY_2);
mySet.add(TEST_SERVICE_KEY_3);
final Set<String> filtered = mySet.stream().filter(
str -> str.contains(TEST_SERVICE_NAME_1)).collect(
Collectors.toSet());
mySet.removeAll(filtered);
assertThat(mySet).doesNotContain(TEST_SERVICE_KEY_1);
assertThat(mySet).hasSize(/* expectedSize= */2);
}
@Test
public void stringSet_convertA11yUserShortcutType_shouldReturnPreferredShortcut() {
int type = 0;
Set<String> mySet = new HashSet<>();
mySet.add(TEST_SERVICE_KEY_1);
mySet.add(TEST_SERVICE_KEY_2);
mySet.add(TEST_SERVICE_KEY_3);
final Set<String> filtered = mySet.stream().filter(
str -> str.contains(TEST_SERVICE_NAME_1)).collect(
Collectors.toSet());
for (String str : filtered) {
final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(str);
type = shortcut.getUserShortcutType();
}
assertThat(type).isEqualTo(TEST_SERVICE_VALUE_1);
}
@Test
public void createFragment_shouldOnlyAddPreferencesOnce() {
mFragment = spy(new ToggleFeaturePreferenceFragmentTestable());