New Settings preference for One Handed mode

Add toggles in Settings location:
Settings > System > Gesture > One-Handed

Bug: 151130376

Test: manual check Settings > System > Gesture > One-Handed
Test: make RunSettingsRoboTests ROBOTEST_FILTER=
      "com.android.settings.gestures.OneHandedSettingsTest"
Test: make RunSettingsRoboTests ROBOTEST_FILTER=
      "com.android.settings.gestures
      .OneHandedAppTapsExitPreferenceControllerTest"
Test: make RunSettingsRoboTests ROBOTEST_FILTER=
      "com.android.settings.gestures
      .OneHandedEnablePreferenceControllerTest"
Test: make RunSettingsRoboTests ROBOTEST_FILTER=
      "com.android.settings.gestures
      .OneHandedTimeoutPreferenceControllerTest"
Test: make RunSettingsRoboTests ROBOTEST_FILTER=
      "com.android.settings.gestures.OneHandedSettingsUtilsTest"

Change-Id: Iafbd3ad051ff0f9c2f4a1b6fcacdccc137b65d0c
This commit is contained in:
Jason Chang
2019-11-11 21:19:16 +08:00
parent c224e4c84b
commit 51a1c46c1c
12 changed files with 976 additions and 0 deletions

View File

@@ -53,6 +53,12 @@
android:fragment="com.android.settings.gestures.SystemNavigationGestureSettings"
settings:controller="com.android.settings.gestures.SystemNavigationPreferenceController" />
<Preference
android:key="gesture_one_handed"
android:title="@string/one_handed_title"
android:fragment="com.android.settings.gestures.OneHandedSettings"
settings:controller="com.android.settings.gestures.OneHandedEnablePreferenceController" />
<Preference
android:key="gesture_tap_screen_input_summary"
android:title="@string/ambient_display_tap_screen_title"

View File

@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res-auto"
android:persistent="false"
android:title="@string/one_handed_title">
<SwitchPreference
android:key="gesture_one_handed_mode_enabled"
android:title="@string/one_handed_mode_enabled"
settings:controller="com.android.settings.gestures.OneHandedEnablePreferenceController"/>
<SwitchPreference
android:key="gesture_app_taps_to_exit"
android:title="@string/one_handed_app_taps_to_exit"
settings:controller="com.android.settings.gestures.OneHandedAppTapsExitPreferenceController"/>
<ListPreference
android:entries="@array/one_handed_timeout_title"
android:entryValues="@array/one_handed_timeout_values"
android:key="one_handed_timeout_preference"
android:persistent="false"
android:title="@string/one_handed_timeout_title"
settings:controller="com.android.settings.gestures.OneHandedTimeoutPreferenceController"/>
</PreferenceScreen>

View File

@@ -0,0 +1,92 @@
/*
* 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.gestures;
import android.content.Context;
import android.net.Uri;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.TogglePreferenceController;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
/**
* The Controller to handle app taps to exit of one-handed mode
*/
public class OneHandedAppTapsExitPreferenceController extends TogglePreferenceController implements
LifecycleObserver, OnStart, OnStop, OneHandedSettingsUtils.TogglesCallback {
private Preference mPreference;
private final OneHandedSettingsUtils mUtils;
public OneHandedAppTapsExitPreferenceController(Context context, String key) {
super(context, key);
mUtils = new OneHandedSettingsUtils(context);
// By default, app taps to stop one-handed is enabled, this will get default value once.
OneHandedSettingsUtils.setSettingsTapsAppToExit(mContext, isChecked());
}
@Override
public int getAvailabilityStatus() {
return OneHandedSettingsUtils.isOneHandedModeEnabled(mContext)
? AVAILABLE : DISABLED_DEPENDENT_SETTING;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
final int availabilityStatus = getAvailabilityStatus();
preference.setEnabled(
availabilityStatus == AVAILABLE || availabilityStatus == AVAILABLE_UNSEARCHABLE);
}
@Override
public boolean setChecked(boolean isChecked) {
return OneHandedSettingsUtils.setSettingsTapsAppToExit(mContext, isChecked);
}
@Override
public boolean isChecked() {
return OneHandedSettingsUtils.getSettingsTapsAppToExit(mContext);
}
@Override
public void onStart() {
mUtils.registerToggleAwareObserver(this);
}
@Override
public void onStop() {
mUtils.unregisterToggleAwareObserver();
}
@Override
public void onChange(Uri uri) {
updateState(mPreference);
}
}

View File

@@ -0,0 +1,49 @@
/*
* 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.gestures;
import android.content.Context;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.TogglePreferenceController;
/**
* The controller to handle one-handed mode enable or disable state.
**/
public class OneHandedEnablePreferenceController extends TogglePreferenceController {
public OneHandedEnablePreferenceController(Context context, String key) {
super(context, key);
}
@Override
public int getAvailabilityStatus() {
return BasePreferenceController.AVAILABLE;
}
@Override
public boolean setChecked(boolean isChecked) {
OneHandedSettingsUtils.setSettingsOneHandedModeEnabled(mContext,
isChecked);
return true;
}
@Override
public boolean isChecked() {
return OneHandedSettingsUtils.isOneHandedModeEnabled(mContext);
}
}

View File

@@ -0,0 +1,51 @@
/*
* 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.gestures;
import android.app.settings.SettingsEnums;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.SearchIndexable;
/**
* The Fragment for one-handed mode settings.
*/
@SearchIndexable
public class OneHandedSettings extends DashboardFragment {
private static final String TAG = "OneHandedSettings";
@Override
public int getMetricsCategory() {
return SettingsEnums.SETTINGS_ONE_HANDED;
}
@Override
protected String getLogTag() {
return TAG;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.one_handed_settings;
}
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.one_handed_settings);
}

View File

@@ -0,0 +1,173 @@
/*
* 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.gestures;
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
/**
* The Util to query one-handed mode settings config
*/
public class OneHandedSettingsUtils {
public enum OneHandedTimeout {
NEVER(0), SHORT(4), MEDIUM(8), LONG(12);
private final int mValue;
OneHandedTimeout(int value) {
this.mValue = value;
}
public int getValue() {
return mValue;
}
}
private final Context mContext;
private final SettingsObserver mSettingsObserver;
OneHandedSettingsUtils(Context context) {
mContext = context;
mSettingsObserver = new SettingsObserver(new Handler(Looper.getMainLooper()));
}
/**
* Get one-handed mode enable or disable flag from Settings provider.
*
* @param context App context
* @return enable or disable one-handed mode flag.
*/
public static boolean isOneHandedModeEnabled(Context context) {
return Settings.Secure.getInt(context.getContentResolver(),
Settings.Secure.ONE_HANDED_MODE_ENABLED, 0) == 1;
}
/**
* Set one-handed mode enable or disable flag to Settings provider.
*
* @param context App context
* @param enable enable or disable one-handed mode.
*/
public static void setSettingsOneHandedModeEnabled(Context context, boolean enable) {
Settings.Secure.putInt(context.getContentResolver(),
Settings.Secure.ONE_HANDED_MODE_ENABLED, enable ? 1 : 0);
}
/**
* Get enabling taps app to exit one-handed mode flag from Settings provider.
*
* @param context App context
* @return enable or disable taps app to exit.
*/
public static boolean getSettingsTapsAppToExit(Context context) {
return Settings.Secure.getInt(context.getContentResolver(),
Settings.Secure.TAPS_APP_TO_EXIT, 1) == 1;
}
/**
* Set enabling taps app to exit one-handed mode flag to Settings provider.
*
* @param context App context
* @param enable enable or disable when taping app to exit one-handed mode.
*/
public static boolean setSettingsTapsAppToExit(Context context, boolean enable) {
return Settings.Secure.putInt(context.getContentResolver(),
Settings.Secure.TAPS_APP_TO_EXIT, enable ? 1 : 0);
}
/**
* Get one-handed mode timeout value from Settings provider.
*
* @param context App context
* @return timeout value in seconds.
*/
public static int getSettingsOneHandedModeTimeout(Context context) {
return Settings.Secure.getInt(context.getContentResolver(),
Settings.Secure.ONE_HANDED_MODE_TIMEOUT,
OneHandedTimeout.MEDIUM.getValue() /* default MEDIUM(8) by UX */);
}
/**
* Set one-handed mode timeout value to Settings provider.
*
* @param context App context
* @param timeout timeout in seconds for exiting one-handed mode.
*/
public static void setSettingsOneHandedModeTimeout(Context context, int timeout) {
Settings.Secure.putInt(context.getContentResolver(),
Settings.Secure.ONE_HANDED_MODE_TIMEOUT, timeout);
}
/**
* Register callback for observing Settings.Secure.ONE_HANDED_MODE_ENABLED state.
* @param callback for state changes
*/
public void registerToggleAwareObserver(TogglesCallback callback) {
mSettingsObserver.observe();
mSettingsObserver.setCallback(callback);
}
/**
* Unregister callback for observing Settings.Secure.ONE_HANDED_MODE_ENABLED state.
*/
public void unregisterToggleAwareObserver() {
final ContentResolver resolver = mContext.getContentResolver();
resolver.unregisterContentObserver(mSettingsObserver);
}
private final class SettingsObserver extends ContentObserver {
private TogglesCallback mCallback;
private final Uri mOneHandedEnabledAware = Settings.Secure.getUriFor(
Settings.Secure.ONE_HANDED_MODE_ENABLED);
SettingsObserver(Handler handler) {
super(handler);
}
private void setCallback(TogglesCallback callback) {
mCallback = callback;
}
public void observe() {
final ContentResolver resolver = mContext.getContentResolver();
resolver.registerContentObserver(mOneHandedEnabledAware, true, this);
}
@Override
public void onChange(boolean selfChange, Uri uri) {
if (mCallback != null) mCallback.onChange(uri);
}
}
/**
* An interface for when Settings.Secure key state changes.
*/
public interface TogglesCallback {
/**
* Callback method for Settings.Secure key state changes.
* @param uri
*/
void onChange(Uri uri);
}
}

View File

@@ -0,0 +1,132 @@
/*
* 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.gestures;
import android.content.Context;
import android.net.Uri;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
import java.util.HashMap;
import java.util.Map;
/**
* The Controller to handle one-handed mode timeout state.
**/
public class OneHandedTimeoutPreferenceController extends BasePreferenceController implements
Preference.OnPreferenceChangeListener, LifecycleObserver, OnStart, OnStop,
OneHandedSettingsUtils.TogglesCallback {
private final Map<String, String> mTimeoutMap;
private Preference mTimeoutPreference;
private final OneHandedSettingsUtils mUtils;
public OneHandedTimeoutPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mTimeoutMap = new HashMap<>();
initTimeoutMap();
mUtils = new OneHandedSettingsUtils(context);
}
@Override
public int getAvailabilityStatus() {
return OneHandedSettingsUtils.isOneHandedModeEnabled(mContext)
? AVAILABLE : DISABLED_DEPENDENT_SETTING;
}
@Override
public boolean onPreferenceChange(Preference preference, Object object) {
if (!(preference instanceof ListPreference)) {
return false;
}
final int newValue = Integer.parseInt((String) object);
OneHandedSettingsUtils.setSettingsOneHandedModeTimeout(mContext, newValue);
updateState(preference);
return true;
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
if (!(preference instanceof ListPreference)) {
return;
}
final ListPreference listPreference = (ListPreference) preference;
listPreference.setValue(getTimeoutValue());
final int availabilityStatus = getAvailabilityStatus();
preference.setEnabled(
availabilityStatus == AVAILABLE || availabilityStatus == AVAILABLE_UNSEARCHABLE);
}
@Override
public CharSequence getSummary() {
return mTimeoutMap.get(getTimeoutValue());
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mTimeoutPreference = screen.findPreference(mPreferenceKey);
}
@Override
public void onStart() {
mUtils.registerToggleAwareObserver(this);
}
@Override
public void onStop() {
mUtils.unregisterToggleAwareObserver();
}
@Override
public void onChange(Uri uri) {
updateState(mTimeoutPreference);
}
private String getTimeoutValue() {
return String.valueOf(OneHandedSettingsUtils.getSettingsOneHandedModeTimeout(mContext));
}
private void initTimeoutMap() {
if (mTimeoutMap.size() != 0) {
return;
}
final String[] timeoutValues = mContext.getResources().getStringArray(
R.array.one_handed_timeout_values);
final String[] timeoutTitles = mContext.getResources().getStringArray(
R.array.one_handed_timeout_title);
if (timeoutValues.length != timeoutTitles.length) {
return;
}
for (int i = 0; i < timeoutValues.length; i++) {
mTimeoutMap.put(timeoutValues[i], timeoutTitles[i]);
}
}
}

View File

@@ -0,0 +1,93 @@
/*
* 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.gestures;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import androidx.preference.SwitchPreference;
import com.android.settings.core.TogglePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class OneHandedAppTapsExitPreferenceControllerTest {
private static final String KEY = "gesture_app_taps_to_exit";
private Context mContext;
private SwitchPreference mSwitchPreference;
private OneHandedAppTapsExitPreferenceController mController;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mController = new OneHandedAppTapsExitPreferenceController(mContext, KEY);
mSwitchPreference = new SwitchPreference(mContext);
mSwitchPreference.setKey(KEY);
}
@Test
public void setChecked_setBoolean_checkIsTrueOrFalse() {
mController.setChecked(false);
assertThat(mController.isChecked()).isFalse();
mController.setChecked(true);
assertThat(mController.isChecked()).isTrue();
}
@Test
public void getAvailabilityStatus_enabledOneHanded_shouldAvailable() {
OneHandedSettingsUtils.setSettingsOneHandedModeEnabled(mContext, true);
assertThat(mController.getAvailabilityStatus())
.isEqualTo(TogglePreferenceController.AVAILABLE);
}
@Test
public void getAvailabilityStatus_disabledOneHanded_shouldUnavailable() {
OneHandedSettingsUtils.setSettingsOneHandedModeEnabled(mContext, false);
assertThat(mController.getAvailabilityStatus())
.isEqualTo(TogglePreferenceController.DISABLED_DEPENDENT_SETTING);
}
@Test
public void updateState_enableOneHanded_switchShouldEnabled() {
OneHandedSettingsUtils.setSettingsOneHandedModeEnabled(mContext, true);
mController.updateState(mSwitchPreference);
assertThat(mSwitchPreference.isEnabled()).isTrue();
}
@Test
public void updateState_disableOneHanded_switchShouldDisabled() {
OneHandedSettingsUtils.setSettingsOneHandedModeEnabled(mContext, false);
mController.updateState(mSwitchPreference);
assertThat(mSwitchPreference.isEnabled()).isFalse();
}
}

View File

@@ -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.gestures;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class OneHandedEnablePreferenceControllerTest {
private static final String KEY = "gesture_one_handed_mode_enabled";
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Mock
private OneHandedEnablePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mController = new OneHandedEnablePreferenceController(mContext, KEY);
}
@Test
public void setChecked_setBoolean_checkIsTrueOrFalse() {
mController.setChecked(false);
assertThat(OneHandedSettingsUtils.isOneHandedModeEnabled(mContext)).isFalse();
mController.setChecked(true);
assertThat(OneHandedSettingsUtils.isOneHandedModeEnabled(mContext)).isTrue();
}
@Test
public void getAvailabilityStatus_byDefault_shouldAvailable() {
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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.gestures;
import static com.google.common.truth.Truth.assertThat;
import android.provider.SearchIndexableResource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class OneHandedSettingsTest {
private OneHandedSettings mSettings;
@Before
public void setUp() {
mSettings = new OneHandedSettings();
}
@Test
public void testSearchIndexProvider_shouldIndexResource() {
final List<SearchIndexableResource> indexRes =
OneHandedSettings.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex(
RuntimeEnvironment.application, true /* enabled */);
assertThat(indexRes).isNotNull();
assertThat(indexRes.get(0).xmlResId).isEqualTo(mSettings.getPreferenceScreenResId());
}
}

View File

@@ -0,0 +1,124 @@
/*
* 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.gestures;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.R;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class OneHandedSettingsUtilsTest {
private static final int TIMEOUT_INDEX_NEVER = 0;
private static final int TIMEOUT_INDEX_SHORT = 1;
private static final int TIMEOUT_INDEX_MEDIUM = 2;
private static final int TIMEOUT_INDEX_LONG = 3;
private Context mContext;
private String[] mConfigTimeout;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mConfigTimeout = mContext.getResources().getStringArray(R.array.one_handed_timeout_values);
}
@Test
public void setSettingsOneHandedModeEnabled_setEnable_shouldReturnEnabled() {
OneHandedSettingsUtils.setSettingsOneHandedModeEnabled(mContext, true);
assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ONE_HANDED_MODE_ENABLED, 0)).isEqualTo(1);
}
@Test
public void setSettingsOneHandedModeEnabled_setDisable_shouldReturnDisabled() {
OneHandedSettingsUtils.setSettingsOneHandedModeEnabled(mContext, false);
assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ONE_HANDED_MODE_ENABLED, 0)).isEqualTo(0);
}
@Test
public void setSettingsTapsAppToExitEnabled_setEnable_shouldReturnEnabled() {
OneHandedSettingsUtils.setSettingsTapsAppToExit(mContext, true);
assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.TAPS_APP_TO_EXIT, 1)).isEqualTo(1);
}
@Test
public void setSettingsTapsAppToExitEnabled_setDisable_shouldReturnDisabled() {
OneHandedSettingsUtils.setSettingsTapsAppToExit(mContext, false);
assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.TAPS_APP_TO_EXIT, 1)).isEqualTo(0);
}
@Test
public void setSettingsTimeout_setNever_shouldReturnNeverValue() {
OneHandedSettingsUtils.setSettingsOneHandedModeTimeout(mContext,
OneHandedSettingsUtils.OneHandedTimeout.NEVER.getValue());
assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ONE_HANDED_MODE_TIMEOUT,
OneHandedSettingsUtils.OneHandedTimeout.NEVER.getValue()))
.isEqualTo(Integer.parseInt(mConfigTimeout[TIMEOUT_INDEX_NEVER]));
}
@Test
public void setSettingsTimeout_setShort_shouldReturnShortValue() {
OneHandedSettingsUtils.setSettingsOneHandedModeTimeout(mContext,
OneHandedSettingsUtils.OneHandedTimeout.SHORT.getValue());
assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ONE_HANDED_MODE_TIMEOUT,
OneHandedSettingsUtils.OneHandedTimeout.SHORT.getValue()))
.isEqualTo(Integer.parseInt(mConfigTimeout[TIMEOUT_INDEX_SHORT]));
}
@Test
public void setSettingsTimeout_setMedium_shouldReturnMediumValue() {
OneHandedSettingsUtils.setSettingsOneHandedModeTimeout(mContext,
OneHandedSettingsUtils.OneHandedTimeout.MEDIUM.getValue());
assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ONE_HANDED_MODE_TIMEOUT,
OneHandedSettingsUtils.OneHandedTimeout.MEDIUM.getValue()))
.isEqualTo(Integer.parseInt(mConfigTimeout[TIMEOUT_INDEX_MEDIUM]));
}
@Test
public void setSettingsTimeout_setLong_shouldReturnLongValue() {
OneHandedSettingsUtils.setSettingsOneHandedModeTimeout(mContext,
OneHandedSettingsUtils.OneHandedTimeout.LONG.getValue());
assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ONE_HANDED_MODE_TIMEOUT,
OneHandedSettingsUtils.OneHandedTimeout.LONG.getValue()))
.isEqualTo(Integer.parseInt(mConfigTimeout[TIMEOUT_INDEX_LONG]));
}
}

View File

@@ -0,0 +1,102 @@
/*
* 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.gestures;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import androidx.preference.ListPreference;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class OneHandedTimeoutPreferenceControllerTest {
private static final String KEY = "one_handed_timeout_preference";
private Context mContext;
private OneHandedTimeoutPreferenceController mController;
private ListPreference mPreference;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mController = new OneHandedTimeoutPreferenceController(mContext, KEY);
mPreference = new ListPreference(mContext);
mPreference.setKey(KEY);
}
@Test
public void getAvailabilityStatus_enabledOneHanded_shouldAvailable() {
OneHandedSettingsUtils.setSettingsOneHandedModeEnabled(mContext, true);
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void getAvailabilityStatus_disableOneHanded_shouldUnavailable() {
OneHandedSettingsUtils.setSettingsOneHandedModeEnabled(mContext, false);
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.DISABLED_DEPENDENT_SETTING);
}
@Test
public void updateState_enableOneHanded_switchShouldEnabled() {
OneHandedSettingsUtils.setSettingsOneHandedModeEnabled(mContext, true);
mController.updateState(mPreference);
assertThat(mPreference.isEnabled()).isTrue();
}
@Test
public void updateState_disableOneHanded_switchShouldDisabled() {
OneHandedSettingsUtils.setSettingsOneHandedModeEnabled(mContext, false);
mController.updateState(mPreference);
assertThat(mPreference.isEnabled()).isFalse();
}
@Test
public void getSummary_setTimeout_shouldReturnSummary() {
final String[] timeoutTitles = mContext.getResources().getStringArray(
R.array.one_handed_timeout_title);
OneHandedSettingsUtils.setSettingsOneHandedModeTimeout(mContext,
OneHandedSettingsUtils.OneHandedTimeout.NEVER.getValue());
assertThat(mController.getSummary()).isEqualTo(
timeoutTitles[OneHandedSettingsUtils.OneHandedTimeout.NEVER.ordinal()]);
OneHandedSettingsUtils.setSettingsOneHandedModeTimeout(mContext,
OneHandedSettingsUtils.OneHandedTimeout.SHORT.getValue());
assertThat(mController.getSummary()).isEqualTo(
timeoutTitles[OneHandedSettingsUtils.OneHandedTimeout.SHORT.ordinal()]);
}
}