Merge changes I02f03ccb,I0ee46dd9
* changes: Extract UserShortcutType functions from ToggleFeaturePreferenceFragment Add data class PreferredShortcut to replace inner class UserShortcutType
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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 androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
/** Tests for {@link PreferredShortcut} */
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class PreferredShortcutTest {
|
||||
|
||||
private static final String STUB_COMPONENT_NAME = new ComponentName("com.example",
|
||||
"com.example.testActivity").flattenToString();
|
||||
private static final int STUB_TYPE = 3;
|
||||
|
||||
@Test
|
||||
public void fromString_matchMemberObject() {
|
||||
final String preferredShortcutString = STUB_COMPONENT_NAME + ":" + STUB_TYPE;
|
||||
|
||||
final PreferredShortcut shortcut = PreferredShortcut.fromString(preferredShortcutString);
|
||||
|
||||
assertThat(shortcut.getComponentName()).isEqualTo(STUB_COMPONENT_NAME);
|
||||
assertThat(shortcut.getType()).isEqualTo(STUB_TYPE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toString_matchString() {
|
||||
final PreferredShortcut shortcut = new PreferredShortcut(STUB_COMPONENT_NAME, STUB_TYPE);
|
||||
|
||||
final String preferredShortcutString = shortcut.toString();
|
||||
|
||||
assertThat(preferredShortcutString).isEqualTo(STUB_COMPONENT_NAME + ":" + STUB_TYPE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertSameObject() {
|
||||
final String preferredShortcutString = STUB_COMPONENT_NAME + ":" + STUB_TYPE;
|
||||
final PreferredShortcut targetShortcut = PreferredShortcut.fromString(
|
||||
preferredShortcutString);
|
||||
|
||||
assertThat(targetShortcut).isEqualTo(new PreferredShortcut(STUB_COMPONENT_NAME, STUB_TYPE));
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
@@ -43,7 +43,6 @@ import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType;
|
||||
import com.android.settings.accessibility.ToggleFeaturePreferenceFragment.AccessibilityUserShortcutType;
|
||||
import com.android.settings.testutils.shadow.ShadowFragment;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -55,21 +54,10 @@ 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;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/** Tests for {@link ToggleFeaturePreferenceFragment} */
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class ToggleFeaturePreferenceFragmentTest {
|
||||
|
||||
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;
|
||||
|
||||
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(
|
||||
@@ -97,59 +85,6 @@ public class ToggleFeaturePreferenceFragmentTest {
|
||||
doReturn(null).when(mFragment).getPreferenceScreen();
|
||||
}
|
||||
|
||||
@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.getType()).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.setType(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() {
|
||||
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());
|
||||
|
||||
final String str = (String) filtered.toArray()[0];
|
||||
final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(str);
|
||||
final int type = shortcut.getType();
|
||||
assertThat(type).isEqualTo(TEST_SERVICE_VALUE_1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createFragment_shouldOnlyAddPreferencesOnce() {
|
||||
FragmentController.setupFragment(mFragment, FragmentActivity.class,
|
||||
@@ -184,7 +119,7 @@ public class ToggleFeaturePreferenceFragmentTest {
|
||||
@Test
|
||||
public void updateShortcutPreferenceData_hasValueInSharedPreference_assignToVariable() {
|
||||
mFragment.mComponentName = PLACEHOLDER_COMPONENT_NAME;
|
||||
final AccessibilityUserShortcutType hardwareShortcut = new AccessibilityUserShortcutType(
|
||||
final PreferredShortcut hardwareShortcut = new PreferredShortcut(
|
||||
PLACEHOLDER_COMPONENT_NAME.flattenToString(), UserShortcutType.HARDWARE);
|
||||
|
||||
putUserShortcutTypeIntoSharedPreference(mContext, hardwareShortcut);
|
||||
@@ -219,10 +154,8 @@ public class ToggleFeaturePreferenceFragmentTest {
|
||||
}
|
||||
|
||||
private void putUserShortcutTypeIntoSharedPreference(Context context,
|
||||
AccessibilityUserShortcutType shortcut) {
|
||||
Set<String> value = new HashSet<>(Collections.singletonList(shortcut.flattenToString()));
|
||||
|
||||
SharedPreferenceUtils.setUserShortcutType(context, value);
|
||||
PreferredShortcut shortcut) {
|
||||
PreferredShortcuts.saveUserShortcutType(context, shortcut);
|
||||
}
|
||||
|
||||
private void callEmptyOnClicked(DialogInterface dialog, int which) {}
|
||||
|
@@ -44,7 +44,6 @@ import androidx.preference.PreferenceManager;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.accessibility.ToggleFeaturePreferenceFragment.AccessibilityUserShortcutType;
|
||||
import com.android.settings.testutils.shadow.ShadowFragment;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -55,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 {
|
||||
|
||||
@@ -183,7 +178,7 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
|
||||
|
||||
@Test
|
||||
public void updateShortcutPreferenceData_hasValueInSharedPreference_assignToVariable() {
|
||||
final AccessibilityUserShortcutType tripleTapShortcut = new AccessibilityUserShortcutType(
|
||||
final PreferredShortcut tripleTapShortcut = new PreferredShortcut(
|
||||
MAGNIFICATION_CONTROLLER_NAME, UserShortcutType.TRIPLETAP);
|
||||
|
||||
putUserShortcutTypeIntoSharedPreference(mContext, tripleTapShortcut);
|
||||
@@ -217,10 +212,8 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
|
||||
}
|
||||
|
||||
private void putUserShortcutTypeIntoSharedPreference(Context context,
|
||||
AccessibilityUserShortcutType shortcut) {
|
||||
Set<String> value = new HashSet<>(Collections.singletonList(shortcut.flattenToString()));
|
||||
|
||||
SharedPreferenceUtils.setUserShortcutType(context, value);
|
||||
PreferredShortcut shortcut) {
|
||||
PreferredShortcuts.saveUserShortcutType(context, shortcut);
|
||||
}
|
||||
|
||||
private void setMagnificationTripleTapEnabled(boolean enabled) {
|
||||
|
Reference in New Issue
Block a user