Settings for timeout to user 0 when docked.
Test: atest SettingsRoboTests:TimeoutToUserZeroSettingsTest Test: atest SettingsRoboTests:TimeoutToUserZeroPreferenceControllerTest Test: manually on device, video: http://shortn/_elZS1CdDtE Bug: 213906356 Change-Id: I519345cdf44eaf452e092adee78ccf9d79130d33
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* 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.users;
|
||||
|
||||
import static android.provider.Settings.Secure.TIMEOUT_TO_USER_ZERO;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.os.UserHandle;
|
||||
import android.provider.Settings;
|
||||
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.fuelgauge.BatteryBackupHelperTest.ShadowUserHandle;
|
||||
import com.android.settings.testutils.shadow.ShadowSecureSettings;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowSecureSettings.class, ShadowUserHandle.class})
|
||||
public class TimeoutToUserZeroPreferenceControllerTest {
|
||||
private Context mContext;
|
||||
private Resources mResources;
|
||||
private TimeoutToUserZeroPreferenceController mController;
|
||||
|
||||
private static final String FAKE_PREFERENCE_KEY = "timeout_to_user_zero_preference";
|
||||
|
||||
private String[] mEntries;
|
||||
private String[] mValues;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = spy(ApplicationProvider.getApplicationContext());
|
||||
mResources = spy(mContext.getResources());
|
||||
doReturn(mResources).when(mContext).getResources();
|
||||
|
||||
mEntries = mResources.getStringArray(
|
||||
R.array.switch_to_user_zero_when_docked_timeout_entries);
|
||||
mValues = mResources.getStringArray(
|
||||
R.array.switch_to_user_zero_when_docked_timeout_values);
|
||||
|
||||
mController = new TimeoutToUserZeroPreferenceController(mContext, FAKE_PREFERENCE_KEY);
|
||||
|
||||
// Feature enabled.
|
||||
when(mResources.getBoolean(
|
||||
com.android.internal.R.bool.config_enableTimeoutToUserZeroWhenDocked)).thenReturn(
|
||||
true);
|
||||
|
||||
// Multi-user feature enabled.
|
||||
Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.USER_SWITCHER_ENABLED,
|
||||
1);
|
||||
|
||||
// Set to user 1;
|
||||
ShadowUserHandle.setUid(1);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
ShadowUserHandle.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_featureFlagDisabled_returnUnsupportedOnDevice() {
|
||||
when(mResources.getBoolean(
|
||||
com.android.internal.R.bool.config_enableTimeoutToUserZeroWhenDocked)).thenReturn(
|
||||
false);
|
||||
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(
|
||||
BasePreferenceController.UNSUPPORTED_ON_DEVICE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_multiUserDisabled_returnConditionallyUnavailable() {
|
||||
Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.USER_SWITCHER_ENABLED,
|
||||
0);
|
||||
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(
|
||||
BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_isCurrentlyUserZero_returnDisabledForUser() {
|
||||
ShadowUserHandle.setUid(UserHandle.USER_SYSTEM);
|
||||
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(
|
||||
BasePreferenceController.DISABLED_FOR_USER);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_featureAndMultiUserEnabledAndNonUserZero_returnAvailable() {
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(
|
||||
BasePreferenceController.AVAILABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSummary_settingNotSet() {
|
||||
Settings.Secure.putStringForUser(mContext.getContentResolver(), TIMEOUT_TO_USER_ZERO,
|
||||
null, UserHandle.myUserId());
|
||||
|
||||
assertThat(mController.getSummary().toString()).isEqualTo(mEntries[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSummary_setToNever() {
|
||||
Settings.Secure.putStringForUser(mContext.getContentResolver(), TIMEOUT_TO_USER_ZERO,
|
||||
mValues[0], UserHandle.myUserId());
|
||||
|
||||
assertThat(mController.getSummary().toString()).isEqualTo(mEntries[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSummary_setToOneMinute() {
|
||||
Settings.Secure.putStringForUser(mContext.getContentResolver(), TIMEOUT_TO_USER_ZERO,
|
||||
mValues[1], UserHandle.myUserId());
|
||||
|
||||
assertThat(mController.getSummary().toString()).isEqualTo(mEntries[1]);
|
||||
}
|
||||
}
|
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* 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.users;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.testutils.shadow.ShadowFragment;
|
||||
import com.android.settingslib.widget.CandidateInfo;
|
||||
|
||||
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 java.util.List;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowFragment.class})
|
||||
public class TimeoutToUserZeroSettingsTest {
|
||||
@Mock
|
||||
private FragmentActivity mActivity;
|
||||
|
||||
private TimeoutToUserZeroSettings mSettings;
|
||||
|
||||
private String[] mEntries;
|
||||
private String[] mValues;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
final Context context = spy(ApplicationProvider.getApplicationContext());
|
||||
mEntries = context.getResources().getStringArray(
|
||||
R.array.switch_to_user_zero_when_docked_timeout_entries);
|
||||
mValues = context.getResources().getStringArray(
|
||||
R.array.switch_to_user_zero_when_docked_timeout_values);
|
||||
mSettings = spy(new TimeoutToUserZeroSettings());
|
||||
|
||||
doReturn(context).when(mSettings).getContext();
|
||||
doReturn(mActivity).when(mSettings).getActivity();
|
||||
|
||||
mSettings.onAttach(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCandidates_returnsACandidateForEachEntry() {
|
||||
final List<? extends CandidateInfo> candidates = mSettings.getCandidates();
|
||||
|
||||
for (int i = 0; i < mEntries.length; i++) {
|
||||
assertThat(candidates.get(i).loadLabel().toString()).isEqualTo(mEntries[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultKey_settingNotSet_shouldReturnFirstValueAsDefault() {
|
||||
assertThat(mSettings.getDefaultKey()).isEqualTo(mValues[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultKey_setToFirstValue_shouldSaveToSettings() {
|
||||
final String expectedKey = mValues[0];
|
||||
mSettings.setDefaultKey(expectedKey);
|
||||
assertThat(mSettings.getDefaultKey()).isEqualTo(expectedKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultKey_setToSecondValue_shouldSaveToSettings() {
|
||||
final String expectedKey = mValues[1];
|
||||
mSettings.setDefaultKey(expectedKey);
|
||||
assertThat(mSettings.getDefaultKey()).isEqualTo(expectedKey);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user