Create a setting for Assist invocation via long press power button

Adds a setting which allows the user to enable invoking the Assist app via long pressing the power button. The availability of setting is controlled by config_longPressOnPowerForAssistantSettingAvailable configuration value.

Bug: 179175321
Bug: 182983853
Test: make RunSettingsRoboTests
Change-Id: I2eb23e5b7539b2fb8e5bc85d23ca5795a08366c5
This commit is contained in:
Jernej Virag
2021-03-18 18:11:31 +00:00
parent 2a959d470a
commit 4f1e17eba1
6 changed files with 412 additions and 4 deletions

View File

@@ -0,0 +1,129 @@
/*
* Copyright (C) 2021 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 static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.app.Application;
import android.content.res.Resources;
import android.provider.Settings;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class LongPressPowerButtonPreferenceControllerTest {
private static final String KEY_LONG_PRESS_POWER_BUTTON =
"gesture_power_menu_long_press_for_assist";
private Application mContext;
private Resources mResources;
private LongPressPowerButtonPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mResources = mock(Resources.class);
when(mContext.getResources()).thenReturn(mResources);
when(mResources.getBoolean(
com.android.internal.R.bool.config_longPressOnPowerForAssistantSettingAvailable))
.thenReturn(true);
mController = new LongPressPowerButtonPreferenceController(mContext,
KEY_LONG_PRESS_POWER_BUTTON);
}
@Test
public void isAvailable_configIsTrue_shouldReturnTrue() {
when(mResources.getBoolean(
com.android.internal.R.bool.config_longPressOnPowerForAssistantSettingAvailable))
.thenReturn(true);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void isAvailable_configIsFalse_shouldReturnFalse() {
when(mResources.getBoolean(
com.android.internal.R.bool.config_longPressOnPowerForAssistantSettingAvailable))
.thenReturn(false);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void preferenceChecked_longPressPowerSettingSetToAssistant() {
mController.onPreferenceChange(null, true);
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.POWER_BUTTON_LONG_PRESS, -1)).isEqualTo(
LongPressPowerButtonPreferenceController.LONG_PRESS_POWER_ASSISTANT_VALUE);
}
@Test
public void preferenceUnchecked_longPressPowerSettingSetToDefaultValue() {
// Value out of range chosen deliberately.
when(mResources.getInteger(
com.android.internal.R.integer.config_longPressOnPowerBehavior))
.thenReturn(8);
mController.onPreferenceChange(null, false);
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.POWER_BUTTON_LONG_PRESS, -1)).isEqualTo(8);
}
@Test
public void preferenceUnchecked_assistDefault_setShutOff() {
// Value out of range chosen deliberately.
when(mResources.getInteger(
com.android.internal.R.integer.config_longPressOnPowerBehavior))
.thenReturn(
LongPressPowerButtonPreferenceController.LONG_PRESS_POWER_ASSISTANT_VALUE);
mController.onPreferenceChange(null, false);
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.POWER_BUTTON_LONG_PRESS, -1)).isEqualTo(
LongPressPowerButtonPreferenceController.LONG_PRESS_POWER_SHUT_OFF);
}
@Test
public void preferenceUnchecked_assistDefaultGlobalActionsEnabled_setGlobalActions() {
// Value out of range chosen deliberately.
when(mResources.getInteger(
com.android.internal.R.integer.config_longPressOnPowerBehavior))
.thenReturn(
LongPressPowerButtonPreferenceController.LONG_PRESS_POWER_ASSISTANT_VALUE);
Settings.Secure.putInt(mContext.getContentResolver(),
LongPressPowerButtonPreferenceController.CARDS_AVAILABLE_KEY, 1);
Settings.Secure.putInt(mContext.getContentResolver(),
LongPressPowerButtonPreferenceController.CARDS_ENABLED_KEY, 1);
mController.onPreferenceChange(null, false);
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.POWER_BUTTON_LONG_PRESS, -1)).isEqualTo(
LongPressPowerButtonPreferenceController.LONG_PRESS_POWER_GLOBAL_ACTIONS);
}
}