Modify flow in ToggleFeaturePreferenceFragment
* Show edit shortcut dialog should belong to the basic feature, so move them back to ToggleFeaturePreferenceFragment * Add test cases for UserShortcutType to prepare to further refactor Bug: 158540780 Test: atest ToggleFeaturePreferenceFragmentTest Change-Id: Ia84bea5581f85d199f395b2065652ce69738abbf
This commit is contained in:
@@ -16,44 +16,87 @@
|
||||
|
||||
package com.android.settings.accessibility;
|
||||
|
||||
import static com.android.settings.accessibility.ToggleFeaturePreferenceFragment.EXTRA_SHORTCUT_TYPE;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
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 android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.provider.Settings;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.annotation.XmlRes;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.preference.PreferenceManager;
|
||||
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;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
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 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;
|
||||
|
||||
private static final String PLACEHOLDER_PACKAGE_NAME = "com.placeholder.example";
|
||||
private static final String PLACEHOLDER_CLASS_NAME = PLACEHOLDER_PACKAGE_NAME + ".placeholder";
|
||||
private static final ComponentName PLACEHOLDER_COMPONENT_NAME = new ComponentName(
|
||||
PLACEHOLDER_PACKAGE_NAME, PLACEHOLDER_CLASS_NAME);
|
||||
|
||||
private static final String SOFTWARE_SHORTCUT_KEY =
|
||||
Settings.Secure.ACCESSIBILITY_BUTTON_TARGETS;
|
||||
private static final String HARDWARE_SHORTCUT_KEY =
|
||||
Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGET_SERVICE;
|
||||
|
||||
private TestToggleFeaturePreferenceFragment mFragment;
|
||||
private Context mContext = ApplicationProvider.getApplicationContext();
|
||||
|
||||
@Mock
|
||||
private PreferenceManager mPreferenceManager;
|
||||
|
||||
@Before
|
||||
public void setUpTestFragment() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mFragment = spy(new TestToggleFeaturePreferenceFragment());
|
||||
when(mFragment.getPreferenceManager()).thenReturn(mPreferenceManager);
|
||||
when(mFragment.getPreferenceManager().getContext()).thenReturn(mContext);
|
||||
when(mFragment.getContext()).thenReturn(mContext);
|
||||
doReturn(null).when(mFragment).getPreferenceScreen();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void a11yUserShortcutType_setConcatString_shouldReturnTargetValue() {
|
||||
final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(
|
||||
@@ -109,7 +152,6 @@ public class ToggleFeaturePreferenceFragmentTest {
|
||||
|
||||
@Test
|
||||
public void createFragment_shouldOnlyAddPreferencesOnce() {
|
||||
mFragment = spy(new ToggleFeaturePreferenceFragmentTestable());
|
||||
FragmentController.setupFragment(mFragment, FragmentActivity.class,
|
||||
/* containerViewId= */ 0, /* bundle= */null);
|
||||
|
||||
@@ -117,7 +159,75 @@ public class ToggleFeaturePreferenceFragmentTest {
|
||||
verify(mFragment).addPreferencesFromResource(R.xml.placeholder_prefs);
|
||||
}
|
||||
|
||||
public static class ToggleFeaturePreferenceFragmentTestable
|
||||
@Test
|
||||
public void updateShortcutPreferenceData_assignDefaultValueToVariable() {
|
||||
mFragment.mComponentName = PLACEHOLDER_COMPONENT_NAME;
|
||||
|
||||
mFragment.updateShortcutPreferenceData();
|
||||
|
||||
// Compare to default UserShortcutType
|
||||
assertThat(mFragment.mUserShortcutTypes).isEqualTo(UserShortcutType.SOFTWARE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateShortcutPreferenceData_hasValueInSettings_assignToVariable() {
|
||||
mFragment.mComponentName = PLACEHOLDER_COMPONENT_NAME;
|
||||
|
||||
putStringIntoSettings(SOFTWARE_SHORTCUT_KEY, PLACEHOLDER_COMPONENT_NAME.flattenToString());
|
||||
putStringIntoSettings(HARDWARE_SHORTCUT_KEY, PLACEHOLDER_COMPONENT_NAME.flattenToString());
|
||||
mFragment.updateShortcutPreferenceData();
|
||||
|
||||
assertThat(mFragment.mUserShortcutTypes).isEqualTo(
|
||||
UserShortcutType.SOFTWARE | UserShortcutType.HARDWARE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateShortcutPreferenceData_hasValueInSharedPreference_assignToVariable() {
|
||||
mFragment.mComponentName = PLACEHOLDER_COMPONENT_NAME;
|
||||
final AccessibilityUserShortcutType hardwareShortcut = new AccessibilityUserShortcutType(
|
||||
PLACEHOLDER_COMPONENT_NAME.flattenToString(), UserShortcutType.HARDWARE);
|
||||
|
||||
putUserShortcutTypeIntoSharedPreference(mContext, hardwareShortcut);
|
||||
mFragment.updateShortcutPreferenceData();
|
||||
|
||||
assertThat(mFragment.mUserShortcutTypes).isEqualTo(UserShortcutType.HARDWARE);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = ShadowFragment.class)
|
||||
public void restoreValueFromSavedInstanceState_assignToVariable() {
|
||||
mContext.setTheme(R.style.Theme_AppCompat);
|
||||
final String dialogTitle = "title";
|
||||
final AlertDialog dialog = AccessibilityEditDialogUtils.showEditShortcutDialog(
|
||||
mContext, dialogTitle, this::callEmptyOnClicked);
|
||||
final Bundle savedInstanceState = new Bundle();
|
||||
mFragment.mComponentName = PLACEHOLDER_COMPONENT_NAME;
|
||||
|
||||
savedInstanceState.putInt(EXTRA_SHORTCUT_TYPE,
|
||||
UserShortcutType.SOFTWARE | UserShortcutType.HARDWARE);
|
||||
mFragment.onCreate(savedInstanceState);
|
||||
mFragment.initializeDialogCheckBox(dialog);
|
||||
mFragment.updateUserShortcutType(true);
|
||||
|
||||
assertThat(mFragment.mUserShortcutTypes).isEqualTo(
|
||||
UserShortcutType.SOFTWARE | UserShortcutType.HARDWARE);
|
||||
|
||||
}
|
||||
|
||||
private void putStringIntoSettings(String key, String componentName) {
|
||||
Settings.Secure.putString(mContext.getContentResolver(), key, componentName);
|
||||
}
|
||||
|
||||
private void putUserShortcutTypeIntoSharedPreference(Context context,
|
||||
AccessibilityUserShortcutType shortcut) {
|
||||
Set<String> value = new HashSet<>(Collections.singletonList(shortcut.flattenToString()));
|
||||
|
||||
SharedPreferenceUtils.setUserShortcutType(context, value);
|
||||
}
|
||||
|
||||
private void callEmptyOnClicked(DialogInterface dialog, int which) {}
|
||||
|
||||
public static class TestToggleFeaturePreferenceFragment
|
||||
extends ToggleFeaturePreferenceFragment {
|
||||
|
||||
@Override
|
||||
@@ -145,14 +255,25 @@ public class ToggleFeaturePreferenceFragmentTest {
|
||||
return mock(View.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPreferencesFromResource(@XmlRes int preferencesResId) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
// do nothing
|
||||
protected void updateShortcutPreference() {
|
||||
// UI related function, do nothing in tests
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user