Merge "Refactor ToggleAutoclickPreferenceFragment to improve maintainability"

This commit is contained in:
Menghan Li
2022-09-05 23:39:44 +00:00
committed by Android (Google) Code Review
11 changed files with 486 additions and 423 deletions

View File

@@ -16,11 +16,17 @@
package com.android.settings.accessibility;
import static android.view.accessibility.AccessibilityManager.AUTOCLICK_DELAY_DEFAULT;
import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.provider.Settings;
import android.view.accessibility.AccessibilityManager;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
@@ -29,17 +35,16 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
/** Tests for {@link AutoclickPreferenceController}. */
@RunWith(RobolectricTestRunner.class)
public class AutoclickPreferenceControllerTest {
private Context mContext;
private final Context mContext = ApplicationProvider.getApplicationContext();
private AutoclickPreferenceController mController;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mController = new AutoclickPreferenceController(mContext, "auto_click");
}
@@ -52,7 +57,7 @@ public class AutoclickPreferenceControllerTest {
@Test
public void getSummary_disabledAutoclick_shouldReturnOffSummary() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, 0);
Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, OFF);
assertThat(mController.getSummary())
.isEqualTo(mContext.getText(R.string.off));
@@ -60,14 +65,15 @@ public class AutoclickPreferenceControllerTest {
@Test
public void getSummary_enabledAutoclick_shouldReturnOnSummary() {
final int autoclickDelayDefault = AccessibilityManager.AUTOCLICK_DELAY_DEFAULT;
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, 1);
Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, ON);
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_AUTOCLICK_DELAY, autoclickDelayDefault);
Settings.Secure.ACCESSIBILITY_AUTOCLICK_DELAY, AUTOCLICK_DELAY_DEFAULT);
assertThat(mController.getSummary())
.isEqualTo(ToggleAutoclickPreferenceFragment.getAutoclickPreferenceSummary(
mContext.getResources(), autoclickDelayDefault));
assertThat(mController.getSummary().toString())
.isEqualTo(AutoclickUtils.getAutoclickDelaySummary(
mContext.getResources(),
R.plurals.accessibilty_autoclick_preference_subtitle_medium_delay,
AUTOCLICK_DELAY_DEFAULT).toString());
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright (C) 2022 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.android.settings.accessibility.AutoclickUtils.MAX_AUTOCLICK_DELAY_MS;
import static com.android.settings.accessibility.AutoclickUtils.MIN_AUTOCLICK_DELAY_MS;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
/** Tests for {@link AutoclickUtils}. */
@RunWith(RobolectricTestRunner.class)
public final class AutoclickUtilsTest {
private final Context mContext = ApplicationProvider.getApplicationContext();
@Test
public void getAutoclickDelaySummary_minDelay_shouldReturnOnSummary() {
final CharSequence summary = AutoclickUtils.getAutoclickDelaySummary(
mContext.getResources(), R.plurals.accessibilty_autoclick_delay_unit_second,
MIN_AUTOCLICK_DELAY_MS);
assertThat(summary.toString()).isEqualTo("0.2 seconds");
}
@Test
public void getAutoclickDelaySummary_maxDelay_shouldReturnOnSummary() {
final CharSequence summary = AutoclickUtils.getAutoclickDelaySummary(
mContext.getResources(), R.plurals.accessibilty_autoclick_delay_unit_second,
MAX_AUTOCLICK_DELAY_MS);
assertThat(summary.toString()).isEqualTo("1 second");
}
}

View File

@@ -18,13 +18,14 @@ package com.android.settings.accessibility;
import static android.content.Context.MODE_PRIVATE;
import static com.android.settings.accessibility.ToggleAutoclickCustomSeekbarController.KEY_CUSTOM_DELAY_VALUE;
import static com.android.settings.accessibility.ToggleAutoclickPreferenceController.KEY_DELAY_MODE;
import static com.android.settings.accessibility.AutoclickUtils.KEY_CUSTOM_DELAY_VALUE;
import static com.android.settings.accessibility.AutoclickUtils.KEY_DELAY_MODE;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -37,57 +38,52 @@ import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.lifecycle.LifecycleObserver;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.widget.LayoutPreference;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
/** Tests for {@link ToggleAutoclickCustomSeekbarController}. */
@RunWith(RobolectricTestRunner.class)
public class ToggleAutoclickCustomSeekbarControllerTest {
private static final String KEY_CUSTOM_SEEKBAR = "autoclick_custom_seekbar";
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock
private PreferenceScreen mScreen;
@Mock
private LayoutPreference mLayoutPreference;
@Mock
private Lifecycle mLifecycle;
@Spy
private Context mContext = ApplicationProvider.getApplicationContext();
private SharedPreferences mSharedPreferences;
private TextView mDelayLabel;
private ImageView mShorter;
private ImageView mLonger;
private SeekBar mSeekBar;
private ToggleAutoclickCustomSeekbarController mController;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
final String mPrefKey = "prefKey";
mContext = ApplicationProvider.getApplicationContext();
mSharedPreferences = mContext.getSharedPreferences(mContext.getPackageName(), MODE_PRIVATE);
mDelayLabel = new TextView(mContext);
mShorter = new ImageView(mContext);
mLonger = new ImageView(mContext);
mSeekBar = new SeekBar(mContext);
mController =
new ToggleAutoclickCustomSeekbarController(mContext, mLifecycle, mPrefKey);
doReturn(mLayoutPreference).when(mScreen).findPreference(mPrefKey);
mController = new ToggleAutoclickCustomSeekbarController(mContext, KEY_CUSTOM_SEEKBAR);
doReturn(mLayoutPreference).when(mScreen).findPreference(KEY_CUSTOM_SEEKBAR);
doReturn(mSeekBar).when(mLayoutPreference).findViewById(R.id.autoclick_delay);
doReturn(mDelayLabel).when(mLayoutPreference).findViewById(R.id.current_label);
doReturn(mShorter).when(mLayoutPreference).findViewById(R.id.shorter);
@@ -99,18 +95,13 @@ public class ToggleAutoclickCustomSeekbarControllerTest {
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void constructor_hasLifecycle_addObserver() {
verify(mLifecycle).addObserver(any(LifecycleObserver.class));
}
@Test
public void displayPreference_initSeekBar() {
mSharedPreferences.edit().putInt(KEY_CUSTOM_DELAY_VALUE, 700).apply();
mController.onResume();
mController.onStart();
mController.displayPreference(mScreen);
mController.onPause();
mController.onStop();
final SeekBar.OnSeekBarChangeListener mListener =
shadowOf(mSeekBar).getOnSeekBarChangeListener();
@@ -123,9 +114,9 @@ public class ToggleAutoclickCustomSeekbarControllerTest {
public void displayPreference_initDelayLabel() {
mSharedPreferences.edit().putInt(KEY_CUSTOM_DELAY_VALUE, 700).apply();
mController.onResume();
mController.onStart();
mController.displayPreference(mScreen);
mController.onPause();
mController.onStop();
assertThat(mDelayLabel.getText()).isEqualTo("0.7 seconds");
}
@@ -203,4 +194,28 @@ public class ToggleAutoclickCustomSeekbarControllerTest {
assertThat(actualDelayValue).isEqualTo(800);
assertThat(actualCustomDelayValue).isEqualTo(800);
}
@Test
public void onStart_registerOnSharedPreferenceChangeListener() {
final SharedPreferences prefs = mock(SharedPreferences.class);
doReturn(prefs).when(mContext).getSharedPreferences(anyString(), anyInt());
final ToggleAutoclickCustomSeekbarController controller =
new ToggleAutoclickCustomSeekbarController(mContext, KEY_CUSTOM_SEEKBAR);
controller.onStart();
verify(prefs).registerOnSharedPreferenceChangeListener(controller);
}
@Test
public void onStop_unregisterOnSharedPreferenceChangeListener() {
final SharedPreferences prefs = mock(SharedPreferences.class);
doReturn(prefs).when(mContext).getSharedPreferences(anyString(), anyInt());
final ToggleAutoclickCustomSeekbarController controller =
new ToggleAutoclickCustomSeekbarController(mContext, KEY_CUSTOM_SEEKBAR);
controller.onStop();
verify(prefs).unregisterOnSharedPreferenceChangeListener(controller);
}
}

View File

@@ -16,18 +16,19 @@
package com.android.settings.accessibility;
import static android.content.Context.MODE_PRIVATE;
import static com.android.settings.accessibility.ToggleAutoclickPreferenceController.AUTOCLICK_CUSTOM_MODE;
import static com.android.settings.accessibility.ToggleAutoclickPreferenceController.AUTOCLICK_OFF_MODE;
import static com.android.settings.accessibility.ToggleAutoclickPreferenceController.KEY_AUTOCLICK_CUSTOM_SEEKBAR;
import static com.android.settings.accessibility.ToggleAutoclickPreferenceController.KEY_DELAY_MODE;
import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
import static com.android.settings.accessibility.AutoclickUtils.KEY_DELAY_MODE;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -35,127 +36,137 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.provider.Settings.Secure;
import androidx.lifecycle.LifecycleObserver;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.accessibility.ToggleAutoclickPreferenceController.OnChangeListener;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.widget.LayoutPreference;
import com.android.settingslib.widget.SelectorWithWidgetPreference;
import com.android.settingslib.widget.SelectorWithWidgetPreference.OnClickListener;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import java.util.Map;
/** Tests for {@link ToggleAutoclickPreferenceController}. */
@RunWith(RobolectricTestRunner.class)
public class ToggleAutoclickPreferenceControllerTest {
private static final String KEY_PREF_DEFAULT = "accessibility_control_autoclick_default";
private static final String KEY_PREF_CUSTOM = "accessibility_control_autoclick_custom";
private static final String KEY_CUSTOM_SEEKBAR = "autoclick_custom_seekbar";
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock
private PreferenceScreen mScreen;
@Mock
private SelectorWithWidgetPreference mDelayModePref;
@Mock
private OnChangeListener mOnChangeListener;
@Mock
private LayoutPreference mSeekBarPref;
@Mock
private Map<String, Integer> mAccessibilityAutoclickKeyToValueMap;
private ToggleAutoclickPreferenceController mController;
private SharedPreferences mSharedPreferences;
private final String mPrefKey = "prefKey";
private final Context mContext = ApplicationProvider.getApplicationContext();
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mController = new ToggleAutoclickPreferenceController(mContext, mPrefKey);
mController.mAccessibilityAutoclickKeyToValueMap = mAccessibilityAutoclickKeyToValueMap;
mSharedPreferences =
mContext.getSharedPreferences(mContext.getPackageName(), MODE_PRIVATE);
when(mScreen.findPreference(mPrefKey)).thenReturn(mDelayModePref);
when(mScreen.findPreference(KEY_AUTOCLICK_CUSTOM_SEEKBAR)).thenReturn(mSeekBarPref);
when(mAccessibilityAutoclickKeyToValueMap.get(mDelayModePref.getKey())).thenReturn(
AUTOCLICK_OFF_MODE);
}
@Spy
private Context mContext = ApplicationProvider.getApplicationContext();
private ToggleAutoclickPreferenceController mController;
@Test
public void getAvailabilityStatus_available() {
setUpController(KEY_PREF_DEFAULT);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void setClickListenerOnDelayModePref_whenDisplay_success() {
setUpController(KEY_PREF_DEFAULT);
mController.displayPreference(mScreen);
verify(mDelayModePref).setOnClickListener(any(OnClickListener.class));
}
@Test
public void constructor_hasLifecycle_addObserver() {
final Lifecycle lifecycle = mock(Lifecycle.class);
mController = new ToggleAutoclickPreferenceController(mContext, lifecycle, mPrefKey);
public void onStart_registerOnSharedPreferenceChangeListener() {
doReturn(mSharedPreferences).when(mContext).getSharedPreferences(anyString(), anyInt());
setUpController(KEY_PREF_DEFAULT);
verify(lifecycle).addObserver(any(LifecycleObserver.class));
mController.onStart();
verify(mSharedPreferences).registerOnSharedPreferenceChangeListener(mController);
}
@Test
public void onStop_unregisterOnSharedPreferenceChangeListener() {
doReturn(mSharedPreferences).when(mContext).getSharedPreferences(anyString(), anyInt());
setUpController(KEY_PREF_DEFAULT);
mController.onStop();
verify(mSharedPreferences).unregisterOnSharedPreferenceChangeListener(mController);
}
@Test
public void onRadioButtonClicked_offMode_disableAutoClick() {
when(mAccessibilityAutoclickKeyToValueMap.get(mPrefKey)).thenReturn(AUTOCLICK_OFF_MODE);
setUpController(KEY_PREF_DEFAULT);
mController.displayPreference(mScreen);
mController.onRadioButtonClicked(any(SelectorWithWidgetPreference.class));
final boolean isEnabled = Secure.getInt(mContext.getContentResolver(),
Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, /* def= */ 0) == 1;
final int delayMs = Secure.getInt(mContext.getContentResolver(),
Secure.ACCESSIBILITY_AUTOCLICK_DELAY, /* def= */ 0);
final int keyDelayMode = mSharedPreferences.getInt(KEY_DELAY_MODE, AUTOCLICK_CUSTOM_MODE);
assertThat(keyDelayMode).isEqualTo(AUTOCLICK_OFF_MODE);
assertThat(delayMs).isEqualTo(/* expected= */ 0);
mController.onRadioButtonClicked(mDelayModePref);
final boolean isEnabled = Secure.getInt(mContext.getContentResolver(),
Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, OFF) == ON;
final int delayMs = Secure.getInt(mContext.getContentResolver(),
Secure.ACCESSIBILITY_AUTOCLICK_DELAY, 0);
assertThat(delayMs).isEqualTo(0);
assertThat(isEnabled).isFalse();
}
@Test
public void onRadioButtonClicked_customMode_enableAutoClick() {
when(mAccessibilityAutoclickKeyToValueMap.get(mDelayModePref.getKey())).thenReturn(
AUTOCLICK_CUSTOM_MODE);
when(mAccessibilityAutoclickKeyToValueMap.get(mPrefKey)).thenReturn(AUTOCLICK_CUSTOM_MODE);
setUpController(KEY_PREF_CUSTOM);
mController.displayPreference(mScreen);
mController.onRadioButtonClicked(any(SelectorWithWidgetPreference.class));
final boolean isEnabled = Secure.getInt(mContext.getContentResolver(),
Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, /* def= */ 0) == 1;
final int keyDelayMode = mSharedPreferences.getInt(KEY_DELAY_MODE, AUTOCLICK_CUSTOM_MODE);
assertThat(keyDelayMode).isEqualTo(AUTOCLICK_CUSTOM_MODE);
mController.onRadioButtonClicked(mDelayModePref);
final boolean isEnabled = Secure.getInt(mContext.getContentResolver(),
Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, OFF) == ON;
assertThat(isEnabled).isTrue();
}
@Test
public void onRadioButtonClicked_hasListener_runOnCheckedChanged() {
when(mAccessibilityAutoclickKeyToValueMap.get(mDelayModePref.getKey())).thenReturn(
AUTOCLICK_CUSTOM_MODE);
when(mAccessibilityAutoclickKeyToValueMap.get(mPrefKey)).thenReturn(AUTOCLICK_CUSTOM_MODE);
mController.setOnChangeListener(mOnChangeListener);
public void onSharedPreferenceChanged_customMode_shouldShowCustomSeekbar() {
setUpController(KEY_PREF_CUSTOM);
mController.displayPreference(mScreen);
mController.onRadioButtonClicked(any(SelectorWithWidgetPreference.class));
mController.onRadioButtonClicked(mDelayModePref);
when(mDelayModePref.isChecked()).thenReturn(true);
reset(mSeekBarPref);
verify(mOnChangeListener).onCheckedChanged(mDelayModePref);
mController.onSharedPreferenceChanged(mSharedPreferences, KEY_DELAY_MODE);
verify(mSeekBarPref).setVisible(true);
}
@Test
public void onSharedPreferenceChanged_offMode_shouldNotShowCustomSeekbar() {
setUpController(KEY_PREF_DEFAULT);
mController.displayPreference(mScreen);
mController.onRadioButtonClicked(mDelayModePref);
reset(mSeekBarPref);
mController.onSharedPreferenceChanged(mSharedPreferences, KEY_DELAY_MODE);
verify(mSeekBarPref, never()).setVisible(true);
}
private void setUpController(String preferenceKey) {
mController = new ToggleAutoclickPreferenceController(mContext, preferenceKey);
when(mScreen.findPreference(preferenceKey)).thenReturn(mDelayModePref);
when(mDelayModePref.getKey()).thenReturn(preferenceKey);
when(mScreen.findPreference(KEY_CUSTOM_SEEKBAR)).thenReturn(mSeekBarPref);
}
}

View File

@@ -0,0 +1,80 @@
/*
* Copyright (C) 2022 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.app.settings.SettingsEnums;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import com.android.settings.testutils.XmlTestUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import java.util.List;
/** Tests for {@link ToggleAutoclickPreferenceFragment}. */
@RunWith(RobolectricTestRunner.class)
public class ToggleAutoclickPreferenceFragmentTest {
private final Context mContext = ApplicationProvider.getApplicationContext();
private ToggleAutoclickPreferenceFragment mFragment;
@Before
public void setUp() {
mFragment = new ToggleAutoclickPreferenceFragment();
}
@Test
public void getMetricsCategory_returnsCorrectCategory() {
assertThat(mFragment.getMetricsCategory()).isEqualTo(
SettingsEnums.ACCESSIBILITY_TOGGLE_AUTOCLICK);
}
@Test
public void getPreferenceScreenResId_returnsCorrectXml() {
assertThat(mFragment.getPreferenceScreenResId()).isEqualTo(
R.xml.accessibility_autoclick_settings);
}
@Test
public void getHelpResource_returnsCorrectHelpResource() {
assertThat(mFragment.getHelpResource()).isEqualTo(R.string.help_url_autoclick);
}
@Test
public void getLogTag_returnsCorrectTag() {
assertThat(mFragment.getLogTag()).isEqualTo("AutoclickPrefFragment");
}
@Test
public void getNonIndexableKeys_existInXmlLayout() {
final List<String> niks = ToggleAutoclickPreferenceFragment.SEARCH_INDEX_DATA_PROVIDER
.getNonIndexableKeys(mContext);
final List<String> keys =
XmlTestUtils.getKeysFromPreferenceXml(mContext,
R.xml.accessibility_autoclick_settings);
assertThat(keys).containsAtLeastElementsIn(niks);
}
}