Extract UserShortcutType functions from ToggleFeaturePreferenceFragment

* Simplify StringJoiner to '+' since it only have one case
* Simplify Collections usage to use removeIf() and findFirst() directly
* Change class name SharedPreferenceUtils to PreferredShortcuts

Bug: 158540780
Test: atest ToggleFeaturePreferenceFragmentTest
Test: atest PreferredShortcutsTest
Change-Id: I02f03ccba09b6d7edaa5c0c8223ab3561a5e976b
This commit is contained in:
jasonwshsu
2020-07-19 23:46:54 +08:00
committed by Jason Hsu
parent 26f1b5f65c
commit 48b546eccf
9 changed files with 225 additions and 164 deletions

View File

@@ -0,0 +1,74 @@
/*
* Copyright (C) 2020 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.google.common.truth.Truth.assertThat;
import android.content.ComponentName;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
/** Tests for {@link PreferredShortcuts} */
@RunWith(RobolectricTestRunner.class)
public class PreferredShortcutsTest {
private static final String PACKAGE_NAME_1 = "com.test1.example";
private static final String CLASS_NAME_1 = PACKAGE_NAME_1 + ".test1";
private static final ComponentName COMPONENT_NAME_1 = new ComponentName(PACKAGE_NAME_1,
CLASS_NAME_1);
private static final String PACKAGE_NAME_2 = "com.test2.example";
private static final String CLASS_NAME_2 = PACKAGE_NAME_2 + ".test2";
private static final ComponentName COMPONENT_NAME_2 = new ComponentName(PACKAGE_NAME_2,
CLASS_NAME_2);
private Context mContext = ApplicationProvider.getApplicationContext();
@Test
public void retrieveUserShortcutType_fromSingleData_matchSavedType() {
final int type = 1;
final PreferredShortcut shortcut = new PreferredShortcut(COMPONENT_NAME_1.flattenToString(),
type);
PreferredShortcuts.saveUserShortcutType(mContext, shortcut);
final int retrieveType = PreferredShortcuts.retrieveUserShortcutType(mContext,
COMPONENT_NAME_1.flattenToString(), 0);
assertThat(retrieveType).isEqualTo(type);
}
@Test
public void retrieveUserShortcutType_fromMultiData_matchSavedType() {
final int type1 = 1;
final int type2 = 2;
final PreferredShortcut shortcut1 = new PreferredShortcut(
COMPONENT_NAME_1.flattenToString(), type1);
final PreferredShortcut shortcut2 = new PreferredShortcut(
COMPONENT_NAME_2.flattenToString(), type2);
PreferredShortcuts.saveUserShortcutType(mContext, shortcut1);
PreferredShortcuts.saveUserShortcutType(mContext, shortcut2);
final int retrieveType = PreferredShortcuts.retrieveUserShortcutType(mContext,
COMPONENT_NAME_1.flattenToString(), 0);
assertThat(retrieveType).isEqualTo(type1);
}
}

View File

@@ -54,10 +54,6 @@ import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.androidx.fragment.FragmentController;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/** Tests for {@link ToggleFeaturePreferenceFragment} */
@RunWith(RobolectricTestRunner.class)
public class ToggleFeaturePreferenceFragmentTest {
@@ -159,9 +155,7 @@ public class ToggleFeaturePreferenceFragmentTest {
private void putUserShortcutTypeIntoSharedPreference(Context context,
PreferredShortcut shortcut) {
Set<String> value = new HashSet<>(Collections.singletonList(shortcut.toString()));
SharedPreferenceUtils.setUserShortcutType(context, value);
PreferredShortcuts.saveUserShortcutType(context, shortcut);
}
private void callEmptyOnClicked(DialogInterface dialog, int which) {}

View File

@@ -54,10 +54,6 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
@RunWith(RobolectricTestRunner.class)
public class ToggleScreenMagnificationPreferenceFragmentTest {
@@ -217,9 +213,7 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
private void putUserShortcutTypeIntoSharedPreference(Context context,
PreferredShortcut shortcut) {
Set<String> value = new HashSet<>(Collections.singletonList(shortcut.toString()));
SharedPreferenceUtils.setUserShortcutType(context, value);
PreferredShortcuts.saveUserShortcutType(context, shortcut);
}
private void setMagnificationTripleTapEnabled(boolean enabled) {