Support Screen Rotate in the Universal Settings API

Change extend from AbstractPreferenceController to TogglePreferenceControllear

Bug: 67997439
Test: make RunSettingsRoboTests
Change-Id: I86e59d00384fb131f2f2b92186e8536324db2e1b
This commit is contained in:
LexHuang
2018-03-23 17:51:54 +08:00
committed by Lex Huang
parent d483041078
commit 78df73f00a
6 changed files with 153 additions and 56 deletions

View File

@@ -21,7 +21,6 @@ import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
import android.arch.lifecycle.LifecycleOwner;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
@@ -29,10 +28,12 @@ import android.os.UserHandle;
import android.provider.Settings;
import android.support.v14.preference.SwitchPreference;
import com.android.internal.view.RotationPolicy;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowRotationPolicy;
import com.android.settings.testutils.shadow.ShadowSystemSettings;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.After;
import org.junit.Before;
@@ -52,8 +53,6 @@ public class AutoRotatePreferenceControllerTest {
private Context mContext;
@Mock
private PackageManager mPackageManager;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle;
private SwitchPreference mPreference;
private ContentResolver mContentResolver;
private AutoRotatePreferenceController mController;
@@ -63,13 +62,11 @@ public class AutoRotatePreferenceControllerTest {
MockitoAnnotations.initMocks(this);
FakeFeatureFactory.setupForTest();
mContentResolver = RuntimeEnvironment.application.getContentResolver();
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
mPreference = new SwitchPreference(RuntimeEnvironment.application);
when(mContext.getPackageManager()).thenReturn(mPackageManager);
when(mContext.getContentResolver()).thenReturn(mContentResolver);
mController = new AutoRotatePreferenceController(mContext, mLifecycle);
mController = new AutoRotatePreferenceController(mContext, "auto_rotate");
}
@After
@@ -81,18 +78,14 @@ public class AutoRotatePreferenceControllerTest {
public void isAvailableWhenPolicyAllows() {
assertThat(mController.isAvailable()).isFalse();
when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true);
when(mContext.getResources().getBoolean(anyInt())).thenReturn(true);
Settings.System.putInt(mContentResolver,
Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0);
enableAutoRotationPreference();
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void updatePreference_settingsIsOff_shouldTurnOffToggle() {
Settings.System.putIntForUser(mContentResolver,
Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT);
disableAutoRotation();
mController.updateState(mPreference);
@@ -101,11 +94,77 @@ public class AutoRotatePreferenceControllerTest {
@Test
public void updatePreference_settingsIsOn_shouldTurnOnToggle() {
Settings.System.putIntForUser(mContentResolver,
Settings.System.ACCELEROMETER_ROTATION, 1, UserHandle.USER_CURRENT);
enableAutoRotation();
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
}
@Test
public void testGetAvailabilityStatus() {
assertThat(mController.getAvailabilityStatus()).isEqualTo(BasePreferenceController
.DISABLED_UNSUPPORTED);
enableAutoRotationPreference();
assertThat(mController.getAvailabilityStatus()).isEqualTo(BasePreferenceController
.AVAILABLE);
disableAutoRotationPreference();
assertThat(mController.getAvailabilityStatus()).isEqualTo(BasePreferenceController
.DISABLED_UNSUPPORTED);
}
@Test
public void testIsCheck() {
assertThat(mController.isChecked()).isFalse();
enableAutoRotation();
assertThat(mController.isChecked()).isTrue();
disableAutoRotation();
assertThat(mController.isChecked()).isFalse();
}
@Test
@Config(shadows = {ShadowRotationPolicy.class})
public void testSetCheck() {
ShadowRotationPolicy.setRotationSupported(true);
mController.setChecked(false);
assertThat(mController.isChecked()).isFalse();
assertThat(RotationPolicy.isRotationLocked(mContext)).isTrue();
mController.setChecked(true);
assertThat(mController.isChecked()).isTrue();
assertThat(RotationPolicy.isRotationLocked(mContext)).isFalse();
}
private void enableAutoRotationPreference() {
when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true);
when(mContext.getResources().getBoolean(anyInt())).thenReturn(true);
Settings.System.putInt(mContentResolver,
Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0);
}
private void disableAutoRotationPreference() {
when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true);
when(mContext.getResources().getBoolean(anyInt())).thenReturn(true);
Settings.System.putInt(mContentResolver,
Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 1);
}
private void enableAutoRotation() {
Settings.System.putIntForUser(mContentResolver,
Settings.System.ACCELEROMETER_ROTATION, 1, UserHandle.USER_CURRENT);
}
private void disableAutoRotation() {
Settings.System.putIntForUser(mContentResolver,
Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT);
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright (C) 2018 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.testutils.shadow;
import android.content.Context;
import com.android.internal.view.RotationPolicy;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
@Implements(RotationPolicy.class)
public class ShadowRotationPolicy {
private static boolean rotationLockEnabled = false;
private static boolean rotationSupported = true;
@Implementation
public static void setRotationLock(Context context, final boolean enabled) {
rotationLockEnabled = enabled;
}
@Implementation
public static boolean isRotationLocked(Context context) {
return rotationLockEnabled;
}
@Implementation
public static boolean isRotationSupported(Context context) {
return rotationSupported;
}
public static void setRotationSupported(boolean supported) {
rotationSupported = supported;
}
}