Loading services/core/java/com/android/server/wm/DeviceStateAutoRotateSettingController.java +31 −8 Original line number Original line Diff line number Diff line Loading @@ -34,6 +34,7 @@ import android.os.Looper; import android.os.Message; import android.os.Message; import android.os.UserHandle; import android.os.UserHandle; import android.provider.Settings; import android.provider.Settings; import android.util.Log; import android.util.Slog; import android.util.Slog; import android.util.SparseIntArray; import android.util.SparseIntArray; Loading @@ -44,6 +45,8 @@ import com.android.server.wm.DeviceStateAutoRotateSettingController.Event.Update import com.android.server.wm.DeviceStateController.DeviceStateEnum; import com.android.server.wm.DeviceStateController.DeviceStateEnum; import com.android.settingslib.devicestate.DeviceStateAutoRotateSettingManager; import com.android.settingslib.devicestate.DeviceStateAutoRotateSettingManager; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.IntStream; Loading @@ -68,6 +71,7 @@ public class DeviceStateAutoRotateSettingController { private static final String SEPARATOR_REGEX = ":"; private static final String SEPARATOR_REGEX = ":"; private static final int ACCELEROMETER_ROTATION_OFF = 0; private static final int ACCELEROMETER_ROTATION_OFF = 0; private static final int ACCELEROMETER_ROTATION_ON = 1; private static final int ACCELEROMETER_ROTATION_ON = 1; private static final int INVALID_DEVICE_STATE = -1; // TODO(b/413598268): Disable debugging after the // TODO(b/413598268): Disable debugging after the // com.android.window.flags.enable_device_state_auto_rotate_setting_refactor flag is rolled-out // com.android.window.flags.enable_device_state_auto_rotate_setting_refactor flag is rolled-out private static final boolean DEBUG = true; private static final boolean DEBUG = true; Loading @@ -78,10 +82,9 @@ public class DeviceStateAutoRotateSettingController { private final DeviceStateAutoRotateSettingManager mDeviceStateAutoRotateSettingManager; private final DeviceStateAutoRotateSettingManager mDeviceStateAutoRotateSettingManager; private final ContentResolver mContentResolver; private final ContentResolver mContentResolver; private final DeviceStateController mDeviceStateController; private final DeviceStateController mDeviceStateController; private final List<Event> mPendingEvents = new ArrayList<>(); // TODO(b/413639166): Handle device state being missing until we receive first device state private int mDeviceState = INVALID_DEVICE_STATE; // update private int mDeviceState = -1; private boolean mAccelerometerSetting; private boolean mAccelerometerSetting; private SparseIntArray mDeviceStateAutoRotateSetting; private SparseIntArray mDeviceStateAutoRotateSetting; Loading @@ -103,10 +106,23 @@ public class DeviceStateAutoRotateSettingController { @Override @Override public void handleMessage(@NonNull Message msg) { public void handleMessage(@NonNull Message msg) { final Event event = (Event) msg.obj; final Event event = (Event) msg.obj; final boolean inMemoryStateUpdated = updateInMemoryState(event); if (mDeviceState == INVALID_DEVICE_STATE && !(event instanceof UpdateDeviceState)) { mPendingEvents.add(event); Log.w(TAG, "Trying to write into auto-rotate settings, while " + "device-state is unavailable.\n" + "Could not process the event=" + event.getClass().getSimpleName() + ".\n" + "This event will be queued and processed later once we receive " + "device-state update."); return; } if (inMemoryStateUpdated) { handleEvent(event); writeInMemoryStateIntoPersistedSetting(); if (!mPendingEvents.isEmpty()) { for (int i = 0; i < mPendingEvents.size(); i++) { handleEvent(mPendingEvents.get(i)); } mPendingEvents.clear(); } } } } }; }; Loading @@ -116,6 +132,14 @@ public class DeviceStateAutoRotateSettingController { registerDeviceStateObserver(); registerDeviceStateObserver(); } } private void handleEvent(@NonNull Event event) { final boolean inMemoryStateUpdated = updateInMemoryState(event); if (inMemoryStateUpdated) { writeInMemoryStateIntoPersistedSetting(); } } /** Request to change {@link DEVICE_STATE_ROTATION_LOCK} persisted setting. */ /** Request to change {@link DEVICE_STATE_ROTATION_LOCK} persisted setting. */ public void requestDeviceStateAutoRotateSettingChange(int deviceState, boolean autoRotate) { public void requestDeviceStateAutoRotateSettingChange(int deviceState, boolean autoRotate) { postUpdate(new UpdateDeviceStateAutoRotateSetting(deviceState, autoRotate)); postUpdate(new UpdateDeviceStateAutoRotateSetting(deviceState, autoRotate)); Loading @@ -128,8 +152,7 @@ public class DeviceStateAutoRotateSettingController { private void registerDeviceStateAutoRotateSettingObserver() { private void registerDeviceStateAutoRotateSettingObserver() { mDeviceStateAutoRotateSettingManager.registerListener( mDeviceStateAutoRotateSettingManager.registerListener( () -> postUpdate( () -> postUpdate(PersistedSettingUpdate.INSTANCE)); PersistedSettingUpdate.INSTANCE)); } } private void registerAccelerometerRotationSettingObserver() { private void registerAccelerometerRotationSettingObserver() { Loading services/tests/wmtests/src/com/android/server/wm/DeviceStateAutoRotateSettingControllerTests.java +118 −32 Original line number Original line Diff line number Diff line Loading @@ -17,7 +17,6 @@ package com.android.server.wm; package com.android.server.wm; import static android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK; import static android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK; import static android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK_LOCKED; import static android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK_UNLOCKED; import static android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK_UNLOCKED; import static android.provider.Settings.System.ACCELEROMETER_ROTATION; import static android.provider.Settings.System.ACCELEROMETER_ROTATION; import static android.provider.Settings.System.getUriFor; import static android.provider.Settings.System.getUriFor; Loading @@ -28,11 +27,13 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.mockito.Mockito.when; import android.content.ContentResolver; import android.content.ContentResolver; import android.content.Context; import android.content.Context; import android.content.res.Resources; import android.database.ContentObserver; import android.database.ContentObserver; import android.hardware.devicestate.DeviceState; import android.hardware.devicestate.DeviceState; import android.os.Handler; import android.os.Handler; Loading @@ -45,11 +46,14 @@ import android.util.SparseIntArray; import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest; import com.android.internal.R; import com.android.internal.util.test.FakeSettingsProvider; import com.android.internal.util.test.FakeSettingsProvider; import com.android.internal.util.test.FakeSettingsProviderRule; import com.android.internal.util.test.FakeSettingsProviderRule; import com.android.server.wm.utils.DeviceStateTestUtils; import com.android.server.wm.utils.DeviceStateTestUtils; import com.android.settingslib.devicestate.AndroidSecureSettings; import com.android.settingslib.devicestate.DeviceStateAutoRotateSettingManager.DeviceStateAutoRotateSettingListener; import com.android.settingslib.devicestate.DeviceStateAutoRotateSettingManager.DeviceStateAutoRotateSettingListener; import com.android.settingslib.devicestate.DeviceStateAutoRotateSettingManagerImpl; import com.android.settingslib.devicestate.DeviceStateAutoRotateSettingManagerImpl; import com.android.settingslib.devicestate.PostureDeviceStateConverter; import org.junit.Before; import org.junit.Before; import org.junit.Rule; import org.junit.Rule; Loading @@ -59,6 +63,8 @@ import org.mockito.Captor; import org.mockito.Mock; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations; import java.util.concurrent.Executor; /** /** * Test class for {@link DeviceStateAutoRotateSettingController}. * Test class for {@link DeviceStateAutoRotateSettingController}. * * Loading @@ -66,8 +72,6 @@ import org.mockito.MockitoAnnotations; */ */ @SmallTest @SmallTest @Presubmit @Presubmit // TODO(b/413639166): Add testcase to check if class under test behaves expectedly when // deviceState = -1 public class DeviceStateAutoRotateSettingControllerTests { public class DeviceStateAutoRotateSettingControllerTests { private static final int ACCELEROMETER_ROTATION_OFF = 0; private static final int ACCELEROMETER_ROTATION_OFF = 0; private static final int ACCELEROMETER_ROTATION_ON = 1; private static final int ACCELEROMETER_ROTATION_ON = 1; Loading @@ -79,8 +83,6 @@ public class DeviceStateAutoRotateSettingControllerTests { @Rule @Rule public final FakeSettingsProviderRule rule = FakeSettingsProvider.rule(); public final FakeSettingsProviderRule rule = FakeSettingsProvider.rule(); @Mock private DeviceStateAutoRotateSettingManagerImpl mMockAutoRotateSettingManager; @Mock @Mock private DeviceStateController mMockDeviceStateController; private DeviceStateController mMockDeviceStateController; @Mock @Mock Loading @@ -95,41 +97,48 @@ public class DeviceStateAutoRotateSettingControllerTests { private final TestLooper mTestLooper = new TestLooper(); private final TestLooper mTestLooper = new TestLooper(); private DeviceStateAutoRotateSettingController mDeviceStateAutoRotateSettingController; private DeviceStateAutoRotateSettingController mDeviceStateAutoRotateSettingController; private DeviceStateAutoRotateSettingManagerImpl mSpyAutoRotateSettingManager; @Before @Before public void setup() { public void setup() { MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this); final Context mockContext = mock(Context.class); final Context mockContext = mock(Context.class); when(mockContext.getContentResolver()).thenReturn(mMockResolver); final Resources mockResources = mock(Resources.class); final FakeSettingsProvider fakeSettingsProvider = new FakeSettingsProvider(); final FakeSettingsProvider fakeSettingsProvider = new FakeSettingsProvider(); when(mMockResolver.acquireProvider(Settings.AUTHORITY)) .thenReturn(fakeSettingsProvider.getIContentProvider()); final Looper looper = mTestLooper.getLooper(); final Looper looper = mTestLooper.getLooper(); final Handler handler = new Handler(looper); final Handler handler = new Handler(looper); when(mockContext.getContentResolver()).thenReturn(mMockResolver); when(mockContext.getResources()).thenReturn(mockResources); when(mockResources.getStringArray( R.array.config_perDeviceStateRotationLockDefaults)).thenReturn(new String[]{}); when(mMockResolver.acquireProvider(Settings.AUTHORITY)).thenReturn( fakeSettingsProvider.getIContentProvider()); mSpyAutoRotateSettingManager = spy( new DeviceStateAutoRotateSettingManagerImpl(mockContext, mock(Executor.class), new AndroidSecureSettings(mMockResolver), handler, mock(PostureDeviceStateConverter.class))); setAccelerometerRotationSetting(ACCELEROMETER_ROTATION_OFF); setAccelerometerRotationSetting(ACCELEROMETER_ROTATION_OFF); when(mMockAutoRotateSettingManager.getRotationLockSetting()).thenAnswer(invocation -> setDeviceStateAutoRotateSetting(FOLDED_LOCKED_OPEN_LOCKED_SETTING); createDeviceStateAutoRotateSettingMap(DEVICE_STATE_ROTATION_LOCK_LOCKED, DEVICE_STATE_ROTATION_LOCK_LOCKED)); mDeviceStateAutoRotateSettingController = new DeviceStateAutoRotateSettingController( mDeviceStateAutoRotateSettingController = new DeviceStateAutoRotateSettingController( mockContext, looper, handler, mMockDeviceStateController, mockContext, looper, handler, mMockDeviceStateController, mMockAutoRotateSettingManager); mSpyAutoRotateSettingManager); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); verify(mMockAutoRotateSettingManager).registerListener( verify(mSpyAutoRotateSettingManager).registerListener( mSettingListenerArgumentCaptor.capture()); mSettingListenerArgumentCaptor.capture()); verify(mMockResolver).registerContentObserver( verify(mMockResolver).registerContentObserver( eq(getUriFor(ACCELEROMETER_ROTATION)), anyBoolean(), eq(getUriFor(ACCELEROMETER_ROTATION)), anyBoolean(), mAccelerometerRotationSettingObserver.capture(), eq(UserHandle.USER_CURRENT)); mAccelerometerRotationSettingObserver.capture(), eq(UserHandle.USER_CURRENT)); verify(mMockDeviceStateController).registerDeviceStateCallback( verify(mMockDeviceStateController).registerDeviceStateCallback( mDeviceStateListenerCaptor.capture(), any()); mDeviceStateListenerCaptor.capture(), any()); setDeviceState(DeviceStateTestUtils.FOLDED); } } @Test @Test public void requestDSAutoRotateSettingChange_updatesDeviceStateAutoRotateSetting() { public void requestDSAutoRotateSettingChange_updatesDeviceStateAutoRotateSetting() { setDeviceState(DeviceStateTestUtils.FOLDED); mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( DeviceStateTestUtils.FOLDED.getIdentifier(), true); DeviceStateTestUtils.FOLDED.getIdentifier(), true); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); Loading @@ -140,6 +149,7 @@ public class DeviceStateAutoRotateSettingControllerTests { @Test @Test public void requestAccelerometerRotationChange_updatesAccelerometerRotation() { public void requestAccelerometerRotationChange_updatesAccelerometerRotation() { setDeviceState(DeviceStateTestUtils.FOLDED); mDeviceStateAutoRotateSettingController.requestAccelerometerRotationSettingChange(true); mDeviceStateAutoRotateSettingController.requestAccelerometerRotationSettingChange(true); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); Loading @@ -148,6 +158,7 @@ public class DeviceStateAutoRotateSettingControllerTests { @Test @Test public void requestDSAutoRotateSettingChange_curDeviceState_updatesAccelerometerRotation() { public void requestDSAutoRotateSettingChange_curDeviceState_updatesAccelerometerRotation() { setDeviceState(DeviceStateTestUtils.FOLDED); mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( DeviceStateTestUtils.FOLDED.getIdentifier(), true); DeviceStateTestUtils.FOLDED.getIdentifier(), true); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); Loading @@ -157,6 +168,7 @@ public class DeviceStateAutoRotateSettingControllerTests { @Test @Test public void requestAccelerometerRotationChange_updatesDSAutoRotateSetting() { public void requestAccelerometerRotationChange_updatesDSAutoRotateSetting() { setDeviceState(DeviceStateTestUtils.FOLDED); mDeviceStateAutoRotateSettingController.requestAccelerometerRotationSettingChange(true); mDeviceStateAutoRotateSettingController.requestAccelerometerRotationSettingChange(true); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); Loading @@ -165,6 +177,7 @@ public class DeviceStateAutoRotateSettingControllerTests { @Test @Test public void accelerometerRotationSettingChanged_updatesDSAutoRotateSetting() { public void accelerometerRotationSettingChanged_updatesDSAutoRotateSetting() { setDeviceState(DeviceStateTestUtils.FOLDED); setAccelerometerRotationSetting(ACCELEROMETER_ROTATION_ON); setAccelerometerRotationSetting(ACCELEROMETER_ROTATION_ON); mAccelerometerRotationSettingObserver.getValue().onChange(false); mAccelerometerRotationSettingObserver.getValue().onChange(false); Loading @@ -175,9 +188,8 @@ public class DeviceStateAutoRotateSettingControllerTests { @Test @Test public void dSAutoRotateSettingChanged_updatesAccelerometerRotation() { public void dSAutoRotateSettingChanged_updatesAccelerometerRotation() { when(mMockAutoRotateSettingManager.getRotationLockSetting()).thenAnswer(invocation -> setDeviceState(DeviceStateTestUtils.FOLDED); createDeviceStateAutoRotateSettingMap(DEVICE_STATE_ROTATION_LOCK_UNLOCKED, setDeviceStateAutoRotateSetting(FOLDED_UNLOCKED_OPEN_LOCKED_SETTING); DEVICE_STATE_ROTATION_LOCK_LOCKED)); mSettingListenerArgumentCaptor.getValue().onSettingsChanged(); mSettingListenerArgumentCaptor.getValue().onSettingsChanged(); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); Loading @@ -187,9 +199,8 @@ public class DeviceStateAutoRotateSettingControllerTests { @Test @Test public void onDeviceStateChange_updatesAccelerometerRotation() { public void onDeviceStateChange_updatesAccelerometerRotation() { when(mMockAutoRotateSettingManager.getRotationLockSetting()).thenAnswer(invocation -> setDeviceState(DeviceStateTestUtils.FOLDED); createDeviceStateAutoRotateSettingMap(DEVICE_STATE_ROTATION_LOCK_LOCKED, setDeviceStateAutoRotateSetting(FOLDED_LOCKED_OPEN_UNLOCKED_SETTING); DEVICE_STATE_ROTATION_LOCK_UNLOCKED)); mSettingListenerArgumentCaptor.getValue().onSettingsChanged(); mSettingListenerArgumentCaptor.getValue().onSettingsChanged(); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); Loading @@ -200,6 +211,7 @@ public class DeviceStateAutoRotateSettingControllerTests { @Test @Test public void requestDSAutoRotateSettingChange_nonCurDeviceState_noUpdateAccelerometerRotation() { public void requestDSAutoRotateSettingChange_nonCurDeviceState_noUpdateAccelerometerRotation() { setDeviceState(DeviceStateTestUtils.FOLDED); mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( DeviceStateTestUtils.OPEN.getIdentifier(), true); DeviceStateTestUtils.OPEN.getIdentifier(), true); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); Loading @@ -209,8 +221,9 @@ public class DeviceStateAutoRotateSettingControllerTests { @Test @Test public void dSAutoRotateCorrupted_writesDefaultSettingWhileRespectingAccelerometerRotation() { public void dSAutoRotateCorrupted_writesDefaultSettingWhileRespectingAccelerometerRotation() { when(mMockAutoRotateSettingManager.getRotationLockSetting()).thenAnswer(invocation -> null); setDeviceState(DeviceStateTestUtils.FOLDED); when(mMockAutoRotateSettingManager.getDefaultRotationLockSetting()).thenReturn( setDeviceStateAutoRotateSetting("invalid"); when(mSpyAutoRotateSettingManager.getDefaultRotationLockSetting()).thenReturn( createDeviceStateAutoRotateSettingMap(DEVICE_STATE_ROTATION_LOCK_UNLOCKED, createDeviceStateAutoRotateSettingMap(DEVICE_STATE_ROTATION_LOCK_UNLOCKED, DEVICE_STATE_ROTATION_LOCK_UNLOCKED)); DEVICE_STATE_ROTATION_LOCK_UNLOCKED)); Loading @@ -222,9 +235,8 @@ public class DeviceStateAutoRotateSettingControllerTests { @Test @Test public void multipleSettingChanges_accelerometerRotationSettingTakesPrecedenceWhenConflict() { public void multipleSettingChanges_accelerometerRotationSettingTakesPrecedenceWhenConflict() { when(mMockAutoRotateSettingManager.getRotationLockSetting()).thenAnswer(invocation -> setDeviceState(DeviceStateTestUtils.FOLDED); createDeviceStateAutoRotateSettingMap(DEVICE_STATE_ROTATION_LOCK_UNLOCKED, setDeviceStateAutoRotateSetting(FOLDED_UNLOCKED_OPEN_LOCKED_SETTING); DEVICE_STATE_ROTATION_LOCK_LOCKED)); setAccelerometerRotationSetting(ACCELEROMETER_ROTATION_ON); setAccelerometerRotationSetting(ACCELEROMETER_ROTATION_ON); mAccelerometerRotationSettingObserver.getValue().onChange(false); mAccelerometerRotationSettingObserver.getValue().onChange(false); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); Loading @@ -237,9 +249,7 @@ public class DeviceStateAutoRotateSettingControllerTests { verifyDeviceStateAutoRotateSettingSet(FOLDED_LOCKED_OPEN_LOCKED_SETTING); verifyDeviceStateAutoRotateSettingSet(FOLDED_LOCKED_OPEN_LOCKED_SETTING); // Change device state auto rotate setting to unlocked for both states // Change device state auto rotate setting to unlocked for both states when(mMockAutoRotateSettingManager.getRotationLockSetting()).thenAnswer(invocation -> setDeviceStateAutoRotateSetting(FOLDED_LOCKED_OPEN_UNLOCKED_SETTING); createDeviceStateAutoRotateSettingMap(DEVICE_STATE_ROTATION_LOCK_LOCKED, DEVICE_STATE_ROTATION_LOCK_UNLOCKED)); setAccelerometerRotationSetting(ACCELEROMETER_ROTATION_ON); setAccelerometerRotationSetting(ACCELEROMETER_ROTATION_ON); mSettingListenerArgumentCaptor.getValue().onSettingsChanged(); mSettingListenerArgumentCaptor.getValue().onSettingsChanged(); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); Loading @@ -249,9 +259,8 @@ public class DeviceStateAutoRotateSettingControllerTests { @Test @Test public void multipleDeviceStateChanges_updatesAccelerometerRotationForRespectiveDeviceState() { public void multipleDeviceStateChanges_updatesAccelerometerRotationForRespectiveDeviceState() { when(mMockAutoRotateSettingManager.getRotationLockSetting()).thenAnswer(invocation -> setDeviceState(DeviceStateTestUtils.FOLDED); createDeviceStateAutoRotateSettingMap(DEVICE_STATE_ROTATION_LOCK_UNLOCKED, setDeviceStateAutoRotateSetting(FOLDED_UNLOCKED_OPEN_LOCKED_SETTING); DEVICE_STATE_ROTATION_LOCK_LOCKED)); setAccelerometerRotationSetting(ACCELEROMETER_ROTATION_ON); setAccelerometerRotationSetting(ACCELEROMETER_ROTATION_ON); mSettingListenerArgumentCaptor.getValue().onSettingsChanged(); mSettingListenerArgumentCaptor.getValue().onSettingsChanged(); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); Loading @@ -266,6 +275,83 @@ public class DeviceStateAutoRotateSettingControllerTests { verifyDeviceStateAutoRotateSettingSet(FOLDED_UNLOCKED_OPEN_LOCKED_SETTING); verifyDeviceStateAutoRotateSettingSet(FOLDED_UNLOCKED_OPEN_LOCKED_SETTING); } } @Test public void requestAccelerometerRotationChange_dSUnavailable_noSettingUpdate() { mDeviceStateAutoRotateSettingController.requestAccelerometerRotationSettingChange(true); mTestLooper.dispatchAll(); verifyAccelerometerRotationSettingSet(ACCELEROMETER_ROTATION_OFF); verifyDeviceStateAutoRotateSettingSet(FOLDED_LOCKED_OPEN_LOCKED_SETTING); } @Test public void requestDSAutoRotateSettingChange_dSUnavailable_noSettingUpdate() { mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( DeviceStateTestUtils.FOLDED.getIdentifier(), true); mTestLooper.dispatchAll(); verifyAccelerometerRotationSettingSet(ACCELEROMETER_ROTATION_OFF); verifyDeviceStateAutoRotateSettingSet(FOLDED_LOCKED_OPEN_LOCKED_SETTING); } @Test public void requestAccelerometerRotationChange_dSUnavailable_writeAfterReceivingDSUpdate() { mDeviceStateAutoRotateSettingController.requestAccelerometerRotationSettingChange(true); mTestLooper.dispatchAll(); setDeviceState(DeviceStateTestUtils.FOLDED); verifyAccelerometerRotationSettingSet(ACCELEROMETER_ROTATION_ON); verifyDeviceStateAutoRotateSettingSet(FOLDED_UNLOCKED_OPEN_LOCKED_SETTING); } @Test public void requestDSAutoRotateSettingChange_dSUnavailable_writeAfterReceivingDSUpdate() { mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( DeviceStateTestUtils.FOLDED.getIdentifier(), true); mTestLooper.dispatchAll(); setDeviceState(DeviceStateTestUtils.FOLDED); verifyAccelerometerRotationSettingSet(ACCELEROMETER_ROTATION_ON); verifyDeviceStateAutoRotateSettingSet(FOLDED_UNLOCKED_OPEN_LOCKED_SETTING); } @Test public void dSUnavailable_sendMultipleRequests_accelerometerPrecedesAfterReceivingDSUpdate() { mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( DeviceStateTestUtils.FOLDED.getIdentifier(), true); mDeviceStateAutoRotateSettingController.requestAccelerometerRotationSettingChange(false); mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( DeviceStateTestUtils.OPEN.getIdentifier(), true); mTestLooper.dispatchAll(); setDeviceState(DeviceStateTestUtils.FOLDED); verifyAccelerometerRotationSettingSet(ACCELEROMETER_ROTATION_OFF); verifyDeviceStateAutoRotateSettingSet(FOLDED_LOCKED_OPEN_UNLOCKED_SETTING); } @Test public void dSUnavailable_sendMultipleRequests_dSAutoRotatePrecedesAfterReceivingDSUpdate() { mDeviceStateAutoRotateSettingController.requestAccelerometerRotationSettingChange(false); mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( DeviceStateTestUtils.OPEN.getIdentifier(), true); mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( DeviceStateTestUtils.FOLDED.getIdentifier(), true); mTestLooper.dispatchAll(); setDeviceState(DeviceStateTestUtils.FOLDED); verifyAccelerometerRotationSettingSet(ACCELEROMETER_ROTATION_ON); verifyDeviceStateAutoRotateSettingSet(FOLDED_UNLOCKED_OPEN_UNLOCKED_SETTING); } private void setDeviceStateAutoRotateSetting(String deviceStateAutoRotateSetting) { Settings.Secure.putStringForUser(mMockResolver, DEVICE_STATE_ROTATION_LOCK, deviceStateAutoRotateSetting, UserHandle.USER_CURRENT); } private void setAccelerometerRotationSetting(int accelerometerRotationSetting) { private void setAccelerometerRotationSetting(int accelerometerRotationSetting) { Settings.System.putIntForUser(mMockResolver, ACCELEROMETER_ROTATION, Settings.System.putIntForUser(mMockResolver, ACCELEROMETER_ROTATION, accelerometerRotationSetting, UserHandle.USER_CURRENT); accelerometerRotationSetting, UserHandle.USER_CURRENT); Loading Loading
services/core/java/com/android/server/wm/DeviceStateAutoRotateSettingController.java +31 −8 Original line number Original line Diff line number Diff line Loading @@ -34,6 +34,7 @@ import android.os.Looper; import android.os.Message; import android.os.Message; import android.os.UserHandle; import android.os.UserHandle; import android.provider.Settings; import android.provider.Settings; import android.util.Log; import android.util.Slog; import android.util.Slog; import android.util.SparseIntArray; import android.util.SparseIntArray; Loading @@ -44,6 +45,8 @@ import com.android.server.wm.DeviceStateAutoRotateSettingController.Event.Update import com.android.server.wm.DeviceStateController.DeviceStateEnum; import com.android.server.wm.DeviceStateController.DeviceStateEnum; import com.android.settingslib.devicestate.DeviceStateAutoRotateSettingManager; import com.android.settingslib.devicestate.DeviceStateAutoRotateSettingManager; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.IntStream; Loading @@ -68,6 +71,7 @@ public class DeviceStateAutoRotateSettingController { private static final String SEPARATOR_REGEX = ":"; private static final String SEPARATOR_REGEX = ":"; private static final int ACCELEROMETER_ROTATION_OFF = 0; private static final int ACCELEROMETER_ROTATION_OFF = 0; private static final int ACCELEROMETER_ROTATION_ON = 1; private static final int ACCELEROMETER_ROTATION_ON = 1; private static final int INVALID_DEVICE_STATE = -1; // TODO(b/413598268): Disable debugging after the // TODO(b/413598268): Disable debugging after the // com.android.window.flags.enable_device_state_auto_rotate_setting_refactor flag is rolled-out // com.android.window.flags.enable_device_state_auto_rotate_setting_refactor flag is rolled-out private static final boolean DEBUG = true; private static final boolean DEBUG = true; Loading @@ -78,10 +82,9 @@ public class DeviceStateAutoRotateSettingController { private final DeviceStateAutoRotateSettingManager mDeviceStateAutoRotateSettingManager; private final DeviceStateAutoRotateSettingManager mDeviceStateAutoRotateSettingManager; private final ContentResolver mContentResolver; private final ContentResolver mContentResolver; private final DeviceStateController mDeviceStateController; private final DeviceStateController mDeviceStateController; private final List<Event> mPendingEvents = new ArrayList<>(); // TODO(b/413639166): Handle device state being missing until we receive first device state private int mDeviceState = INVALID_DEVICE_STATE; // update private int mDeviceState = -1; private boolean mAccelerometerSetting; private boolean mAccelerometerSetting; private SparseIntArray mDeviceStateAutoRotateSetting; private SparseIntArray mDeviceStateAutoRotateSetting; Loading @@ -103,10 +106,23 @@ public class DeviceStateAutoRotateSettingController { @Override @Override public void handleMessage(@NonNull Message msg) { public void handleMessage(@NonNull Message msg) { final Event event = (Event) msg.obj; final Event event = (Event) msg.obj; final boolean inMemoryStateUpdated = updateInMemoryState(event); if (mDeviceState == INVALID_DEVICE_STATE && !(event instanceof UpdateDeviceState)) { mPendingEvents.add(event); Log.w(TAG, "Trying to write into auto-rotate settings, while " + "device-state is unavailable.\n" + "Could not process the event=" + event.getClass().getSimpleName() + ".\n" + "This event will be queued and processed later once we receive " + "device-state update."); return; } if (inMemoryStateUpdated) { handleEvent(event); writeInMemoryStateIntoPersistedSetting(); if (!mPendingEvents.isEmpty()) { for (int i = 0; i < mPendingEvents.size(); i++) { handleEvent(mPendingEvents.get(i)); } mPendingEvents.clear(); } } } } }; }; Loading @@ -116,6 +132,14 @@ public class DeviceStateAutoRotateSettingController { registerDeviceStateObserver(); registerDeviceStateObserver(); } } private void handleEvent(@NonNull Event event) { final boolean inMemoryStateUpdated = updateInMemoryState(event); if (inMemoryStateUpdated) { writeInMemoryStateIntoPersistedSetting(); } } /** Request to change {@link DEVICE_STATE_ROTATION_LOCK} persisted setting. */ /** Request to change {@link DEVICE_STATE_ROTATION_LOCK} persisted setting. */ public void requestDeviceStateAutoRotateSettingChange(int deviceState, boolean autoRotate) { public void requestDeviceStateAutoRotateSettingChange(int deviceState, boolean autoRotate) { postUpdate(new UpdateDeviceStateAutoRotateSetting(deviceState, autoRotate)); postUpdate(new UpdateDeviceStateAutoRotateSetting(deviceState, autoRotate)); Loading @@ -128,8 +152,7 @@ public class DeviceStateAutoRotateSettingController { private void registerDeviceStateAutoRotateSettingObserver() { private void registerDeviceStateAutoRotateSettingObserver() { mDeviceStateAutoRotateSettingManager.registerListener( mDeviceStateAutoRotateSettingManager.registerListener( () -> postUpdate( () -> postUpdate(PersistedSettingUpdate.INSTANCE)); PersistedSettingUpdate.INSTANCE)); } } private void registerAccelerometerRotationSettingObserver() { private void registerAccelerometerRotationSettingObserver() { Loading
services/tests/wmtests/src/com/android/server/wm/DeviceStateAutoRotateSettingControllerTests.java +118 −32 Original line number Original line Diff line number Diff line Loading @@ -17,7 +17,6 @@ package com.android.server.wm; package com.android.server.wm; import static android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK; import static android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK; import static android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK_LOCKED; import static android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK_UNLOCKED; import static android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK_UNLOCKED; import static android.provider.Settings.System.ACCELEROMETER_ROTATION; import static android.provider.Settings.System.ACCELEROMETER_ROTATION; import static android.provider.Settings.System.getUriFor; import static android.provider.Settings.System.getUriFor; Loading @@ -28,11 +27,13 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.mockito.Mockito.when; import android.content.ContentResolver; import android.content.ContentResolver; import android.content.Context; import android.content.Context; import android.content.res.Resources; import android.database.ContentObserver; import android.database.ContentObserver; import android.hardware.devicestate.DeviceState; import android.hardware.devicestate.DeviceState; import android.os.Handler; import android.os.Handler; Loading @@ -45,11 +46,14 @@ import android.util.SparseIntArray; import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest; import com.android.internal.R; import com.android.internal.util.test.FakeSettingsProvider; import com.android.internal.util.test.FakeSettingsProvider; import com.android.internal.util.test.FakeSettingsProviderRule; import com.android.internal.util.test.FakeSettingsProviderRule; import com.android.server.wm.utils.DeviceStateTestUtils; import com.android.server.wm.utils.DeviceStateTestUtils; import com.android.settingslib.devicestate.AndroidSecureSettings; import com.android.settingslib.devicestate.DeviceStateAutoRotateSettingManager.DeviceStateAutoRotateSettingListener; import com.android.settingslib.devicestate.DeviceStateAutoRotateSettingManager.DeviceStateAutoRotateSettingListener; import com.android.settingslib.devicestate.DeviceStateAutoRotateSettingManagerImpl; import com.android.settingslib.devicestate.DeviceStateAutoRotateSettingManagerImpl; import com.android.settingslib.devicestate.PostureDeviceStateConverter; import org.junit.Before; import org.junit.Before; import org.junit.Rule; import org.junit.Rule; Loading @@ -59,6 +63,8 @@ import org.mockito.Captor; import org.mockito.Mock; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations; import java.util.concurrent.Executor; /** /** * Test class for {@link DeviceStateAutoRotateSettingController}. * Test class for {@link DeviceStateAutoRotateSettingController}. * * Loading @@ -66,8 +72,6 @@ import org.mockito.MockitoAnnotations; */ */ @SmallTest @SmallTest @Presubmit @Presubmit // TODO(b/413639166): Add testcase to check if class under test behaves expectedly when // deviceState = -1 public class DeviceStateAutoRotateSettingControllerTests { public class DeviceStateAutoRotateSettingControllerTests { private static final int ACCELEROMETER_ROTATION_OFF = 0; private static final int ACCELEROMETER_ROTATION_OFF = 0; private static final int ACCELEROMETER_ROTATION_ON = 1; private static final int ACCELEROMETER_ROTATION_ON = 1; Loading @@ -79,8 +83,6 @@ public class DeviceStateAutoRotateSettingControllerTests { @Rule @Rule public final FakeSettingsProviderRule rule = FakeSettingsProvider.rule(); public final FakeSettingsProviderRule rule = FakeSettingsProvider.rule(); @Mock private DeviceStateAutoRotateSettingManagerImpl mMockAutoRotateSettingManager; @Mock @Mock private DeviceStateController mMockDeviceStateController; private DeviceStateController mMockDeviceStateController; @Mock @Mock Loading @@ -95,41 +97,48 @@ public class DeviceStateAutoRotateSettingControllerTests { private final TestLooper mTestLooper = new TestLooper(); private final TestLooper mTestLooper = new TestLooper(); private DeviceStateAutoRotateSettingController mDeviceStateAutoRotateSettingController; private DeviceStateAutoRotateSettingController mDeviceStateAutoRotateSettingController; private DeviceStateAutoRotateSettingManagerImpl mSpyAutoRotateSettingManager; @Before @Before public void setup() { public void setup() { MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this); final Context mockContext = mock(Context.class); final Context mockContext = mock(Context.class); when(mockContext.getContentResolver()).thenReturn(mMockResolver); final Resources mockResources = mock(Resources.class); final FakeSettingsProvider fakeSettingsProvider = new FakeSettingsProvider(); final FakeSettingsProvider fakeSettingsProvider = new FakeSettingsProvider(); when(mMockResolver.acquireProvider(Settings.AUTHORITY)) .thenReturn(fakeSettingsProvider.getIContentProvider()); final Looper looper = mTestLooper.getLooper(); final Looper looper = mTestLooper.getLooper(); final Handler handler = new Handler(looper); final Handler handler = new Handler(looper); when(mockContext.getContentResolver()).thenReturn(mMockResolver); when(mockContext.getResources()).thenReturn(mockResources); when(mockResources.getStringArray( R.array.config_perDeviceStateRotationLockDefaults)).thenReturn(new String[]{}); when(mMockResolver.acquireProvider(Settings.AUTHORITY)).thenReturn( fakeSettingsProvider.getIContentProvider()); mSpyAutoRotateSettingManager = spy( new DeviceStateAutoRotateSettingManagerImpl(mockContext, mock(Executor.class), new AndroidSecureSettings(mMockResolver), handler, mock(PostureDeviceStateConverter.class))); setAccelerometerRotationSetting(ACCELEROMETER_ROTATION_OFF); setAccelerometerRotationSetting(ACCELEROMETER_ROTATION_OFF); when(mMockAutoRotateSettingManager.getRotationLockSetting()).thenAnswer(invocation -> setDeviceStateAutoRotateSetting(FOLDED_LOCKED_OPEN_LOCKED_SETTING); createDeviceStateAutoRotateSettingMap(DEVICE_STATE_ROTATION_LOCK_LOCKED, DEVICE_STATE_ROTATION_LOCK_LOCKED)); mDeviceStateAutoRotateSettingController = new DeviceStateAutoRotateSettingController( mDeviceStateAutoRotateSettingController = new DeviceStateAutoRotateSettingController( mockContext, looper, handler, mMockDeviceStateController, mockContext, looper, handler, mMockDeviceStateController, mMockAutoRotateSettingManager); mSpyAutoRotateSettingManager); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); verify(mMockAutoRotateSettingManager).registerListener( verify(mSpyAutoRotateSettingManager).registerListener( mSettingListenerArgumentCaptor.capture()); mSettingListenerArgumentCaptor.capture()); verify(mMockResolver).registerContentObserver( verify(mMockResolver).registerContentObserver( eq(getUriFor(ACCELEROMETER_ROTATION)), anyBoolean(), eq(getUriFor(ACCELEROMETER_ROTATION)), anyBoolean(), mAccelerometerRotationSettingObserver.capture(), eq(UserHandle.USER_CURRENT)); mAccelerometerRotationSettingObserver.capture(), eq(UserHandle.USER_CURRENT)); verify(mMockDeviceStateController).registerDeviceStateCallback( verify(mMockDeviceStateController).registerDeviceStateCallback( mDeviceStateListenerCaptor.capture(), any()); mDeviceStateListenerCaptor.capture(), any()); setDeviceState(DeviceStateTestUtils.FOLDED); } } @Test @Test public void requestDSAutoRotateSettingChange_updatesDeviceStateAutoRotateSetting() { public void requestDSAutoRotateSettingChange_updatesDeviceStateAutoRotateSetting() { setDeviceState(DeviceStateTestUtils.FOLDED); mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( DeviceStateTestUtils.FOLDED.getIdentifier(), true); DeviceStateTestUtils.FOLDED.getIdentifier(), true); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); Loading @@ -140,6 +149,7 @@ public class DeviceStateAutoRotateSettingControllerTests { @Test @Test public void requestAccelerometerRotationChange_updatesAccelerometerRotation() { public void requestAccelerometerRotationChange_updatesAccelerometerRotation() { setDeviceState(DeviceStateTestUtils.FOLDED); mDeviceStateAutoRotateSettingController.requestAccelerometerRotationSettingChange(true); mDeviceStateAutoRotateSettingController.requestAccelerometerRotationSettingChange(true); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); Loading @@ -148,6 +158,7 @@ public class DeviceStateAutoRotateSettingControllerTests { @Test @Test public void requestDSAutoRotateSettingChange_curDeviceState_updatesAccelerometerRotation() { public void requestDSAutoRotateSettingChange_curDeviceState_updatesAccelerometerRotation() { setDeviceState(DeviceStateTestUtils.FOLDED); mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( DeviceStateTestUtils.FOLDED.getIdentifier(), true); DeviceStateTestUtils.FOLDED.getIdentifier(), true); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); Loading @@ -157,6 +168,7 @@ public class DeviceStateAutoRotateSettingControllerTests { @Test @Test public void requestAccelerometerRotationChange_updatesDSAutoRotateSetting() { public void requestAccelerometerRotationChange_updatesDSAutoRotateSetting() { setDeviceState(DeviceStateTestUtils.FOLDED); mDeviceStateAutoRotateSettingController.requestAccelerometerRotationSettingChange(true); mDeviceStateAutoRotateSettingController.requestAccelerometerRotationSettingChange(true); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); Loading @@ -165,6 +177,7 @@ public class DeviceStateAutoRotateSettingControllerTests { @Test @Test public void accelerometerRotationSettingChanged_updatesDSAutoRotateSetting() { public void accelerometerRotationSettingChanged_updatesDSAutoRotateSetting() { setDeviceState(DeviceStateTestUtils.FOLDED); setAccelerometerRotationSetting(ACCELEROMETER_ROTATION_ON); setAccelerometerRotationSetting(ACCELEROMETER_ROTATION_ON); mAccelerometerRotationSettingObserver.getValue().onChange(false); mAccelerometerRotationSettingObserver.getValue().onChange(false); Loading @@ -175,9 +188,8 @@ public class DeviceStateAutoRotateSettingControllerTests { @Test @Test public void dSAutoRotateSettingChanged_updatesAccelerometerRotation() { public void dSAutoRotateSettingChanged_updatesAccelerometerRotation() { when(mMockAutoRotateSettingManager.getRotationLockSetting()).thenAnswer(invocation -> setDeviceState(DeviceStateTestUtils.FOLDED); createDeviceStateAutoRotateSettingMap(DEVICE_STATE_ROTATION_LOCK_UNLOCKED, setDeviceStateAutoRotateSetting(FOLDED_UNLOCKED_OPEN_LOCKED_SETTING); DEVICE_STATE_ROTATION_LOCK_LOCKED)); mSettingListenerArgumentCaptor.getValue().onSettingsChanged(); mSettingListenerArgumentCaptor.getValue().onSettingsChanged(); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); Loading @@ -187,9 +199,8 @@ public class DeviceStateAutoRotateSettingControllerTests { @Test @Test public void onDeviceStateChange_updatesAccelerometerRotation() { public void onDeviceStateChange_updatesAccelerometerRotation() { when(mMockAutoRotateSettingManager.getRotationLockSetting()).thenAnswer(invocation -> setDeviceState(DeviceStateTestUtils.FOLDED); createDeviceStateAutoRotateSettingMap(DEVICE_STATE_ROTATION_LOCK_LOCKED, setDeviceStateAutoRotateSetting(FOLDED_LOCKED_OPEN_UNLOCKED_SETTING); DEVICE_STATE_ROTATION_LOCK_UNLOCKED)); mSettingListenerArgumentCaptor.getValue().onSettingsChanged(); mSettingListenerArgumentCaptor.getValue().onSettingsChanged(); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); Loading @@ -200,6 +211,7 @@ public class DeviceStateAutoRotateSettingControllerTests { @Test @Test public void requestDSAutoRotateSettingChange_nonCurDeviceState_noUpdateAccelerometerRotation() { public void requestDSAutoRotateSettingChange_nonCurDeviceState_noUpdateAccelerometerRotation() { setDeviceState(DeviceStateTestUtils.FOLDED); mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( DeviceStateTestUtils.OPEN.getIdentifier(), true); DeviceStateTestUtils.OPEN.getIdentifier(), true); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); Loading @@ -209,8 +221,9 @@ public class DeviceStateAutoRotateSettingControllerTests { @Test @Test public void dSAutoRotateCorrupted_writesDefaultSettingWhileRespectingAccelerometerRotation() { public void dSAutoRotateCorrupted_writesDefaultSettingWhileRespectingAccelerometerRotation() { when(mMockAutoRotateSettingManager.getRotationLockSetting()).thenAnswer(invocation -> null); setDeviceState(DeviceStateTestUtils.FOLDED); when(mMockAutoRotateSettingManager.getDefaultRotationLockSetting()).thenReturn( setDeviceStateAutoRotateSetting("invalid"); when(mSpyAutoRotateSettingManager.getDefaultRotationLockSetting()).thenReturn( createDeviceStateAutoRotateSettingMap(DEVICE_STATE_ROTATION_LOCK_UNLOCKED, createDeviceStateAutoRotateSettingMap(DEVICE_STATE_ROTATION_LOCK_UNLOCKED, DEVICE_STATE_ROTATION_LOCK_UNLOCKED)); DEVICE_STATE_ROTATION_LOCK_UNLOCKED)); Loading @@ -222,9 +235,8 @@ public class DeviceStateAutoRotateSettingControllerTests { @Test @Test public void multipleSettingChanges_accelerometerRotationSettingTakesPrecedenceWhenConflict() { public void multipleSettingChanges_accelerometerRotationSettingTakesPrecedenceWhenConflict() { when(mMockAutoRotateSettingManager.getRotationLockSetting()).thenAnswer(invocation -> setDeviceState(DeviceStateTestUtils.FOLDED); createDeviceStateAutoRotateSettingMap(DEVICE_STATE_ROTATION_LOCK_UNLOCKED, setDeviceStateAutoRotateSetting(FOLDED_UNLOCKED_OPEN_LOCKED_SETTING); DEVICE_STATE_ROTATION_LOCK_LOCKED)); setAccelerometerRotationSetting(ACCELEROMETER_ROTATION_ON); setAccelerometerRotationSetting(ACCELEROMETER_ROTATION_ON); mAccelerometerRotationSettingObserver.getValue().onChange(false); mAccelerometerRotationSettingObserver.getValue().onChange(false); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); Loading @@ -237,9 +249,7 @@ public class DeviceStateAutoRotateSettingControllerTests { verifyDeviceStateAutoRotateSettingSet(FOLDED_LOCKED_OPEN_LOCKED_SETTING); verifyDeviceStateAutoRotateSettingSet(FOLDED_LOCKED_OPEN_LOCKED_SETTING); // Change device state auto rotate setting to unlocked for both states // Change device state auto rotate setting to unlocked for both states when(mMockAutoRotateSettingManager.getRotationLockSetting()).thenAnswer(invocation -> setDeviceStateAutoRotateSetting(FOLDED_LOCKED_OPEN_UNLOCKED_SETTING); createDeviceStateAutoRotateSettingMap(DEVICE_STATE_ROTATION_LOCK_LOCKED, DEVICE_STATE_ROTATION_LOCK_UNLOCKED)); setAccelerometerRotationSetting(ACCELEROMETER_ROTATION_ON); setAccelerometerRotationSetting(ACCELEROMETER_ROTATION_ON); mSettingListenerArgumentCaptor.getValue().onSettingsChanged(); mSettingListenerArgumentCaptor.getValue().onSettingsChanged(); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); Loading @@ -249,9 +259,8 @@ public class DeviceStateAutoRotateSettingControllerTests { @Test @Test public void multipleDeviceStateChanges_updatesAccelerometerRotationForRespectiveDeviceState() { public void multipleDeviceStateChanges_updatesAccelerometerRotationForRespectiveDeviceState() { when(mMockAutoRotateSettingManager.getRotationLockSetting()).thenAnswer(invocation -> setDeviceState(DeviceStateTestUtils.FOLDED); createDeviceStateAutoRotateSettingMap(DEVICE_STATE_ROTATION_LOCK_UNLOCKED, setDeviceStateAutoRotateSetting(FOLDED_UNLOCKED_OPEN_LOCKED_SETTING); DEVICE_STATE_ROTATION_LOCK_LOCKED)); setAccelerometerRotationSetting(ACCELEROMETER_ROTATION_ON); setAccelerometerRotationSetting(ACCELEROMETER_ROTATION_ON); mSettingListenerArgumentCaptor.getValue().onSettingsChanged(); mSettingListenerArgumentCaptor.getValue().onSettingsChanged(); mTestLooper.dispatchAll(); mTestLooper.dispatchAll(); Loading @@ -266,6 +275,83 @@ public class DeviceStateAutoRotateSettingControllerTests { verifyDeviceStateAutoRotateSettingSet(FOLDED_UNLOCKED_OPEN_LOCKED_SETTING); verifyDeviceStateAutoRotateSettingSet(FOLDED_UNLOCKED_OPEN_LOCKED_SETTING); } } @Test public void requestAccelerometerRotationChange_dSUnavailable_noSettingUpdate() { mDeviceStateAutoRotateSettingController.requestAccelerometerRotationSettingChange(true); mTestLooper.dispatchAll(); verifyAccelerometerRotationSettingSet(ACCELEROMETER_ROTATION_OFF); verifyDeviceStateAutoRotateSettingSet(FOLDED_LOCKED_OPEN_LOCKED_SETTING); } @Test public void requestDSAutoRotateSettingChange_dSUnavailable_noSettingUpdate() { mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( DeviceStateTestUtils.FOLDED.getIdentifier(), true); mTestLooper.dispatchAll(); verifyAccelerometerRotationSettingSet(ACCELEROMETER_ROTATION_OFF); verifyDeviceStateAutoRotateSettingSet(FOLDED_LOCKED_OPEN_LOCKED_SETTING); } @Test public void requestAccelerometerRotationChange_dSUnavailable_writeAfterReceivingDSUpdate() { mDeviceStateAutoRotateSettingController.requestAccelerometerRotationSettingChange(true); mTestLooper.dispatchAll(); setDeviceState(DeviceStateTestUtils.FOLDED); verifyAccelerometerRotationSettingSet(ACCELEROMETER_ROTATION_ON); verifyDeviceStateAutoRotateSettingSet(FOLDED_UNLOCKED_OPEN_LOCKED_SETTING); } @Test public void requestDSAutoRotateSettingChange_dSUnavailable_writeAfterReceivingDSUpdate() { mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( DeviceStateTestUtils.FOLDED.getIdentifier(), true); mTestLooper.dispatchAll(); setDeviceState(DeviceStateTestUtils.FOLDED); verifyAccelerometerRotationSettingSet(ACCELEROMETER_ROTATION_ON); verifyDeviceStateAutoRotateSettingSet(FOLDED_UNLOCKED_OPEN_LOCKED_SETTING); } @Test public void dSUnavailable_sendMultipleRequests_accelerometerPrecedesAfterReceivingDSUpdate() { mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( DeviceStateTestUtils.FOLDED.getIdentifier(), true); mDeviceStateAutoRotateSettingController.requestAccelerometerRotationSettingChange(false); mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( DeviceStateTestUtils.OPEN.getIdentifier(), true); mTestLooper.dispatchAll(); setDeviceState(DeviceStateTestUtils.FOLDED); verifyAccelerometerRotationSettingSet(ACCELEROMETER_ROTATION_OFF); verifyDeviceStateAutoRotateSettingSet(FOLDED_LOCKED_OPEN_UNLOCKED_SETTING); } @Test public void dSUnavailable_sendMultipleRequests_dSAutoRotatePrecedesAfterReceivingDSUpdate() { mDeviceStateAutoRotateSettingController.requestAccelerometerRotationSettingChange(false); mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( DeviceStateTestUtils.OPEN.getIdentifier(), true); mDeviceStateAutoRotateSettingController.requestDeviceStateAutoRotateSettingChange( DeviceStateTestUtils.FOLDED.getIdentifier(), true); mTestLooper.dispatchAll(); setDeviceState(DeviceStateTestUtils.FOLDED); verifyAccelerometerRotationSettingSet(ACCELEROMETER_ROTATION_ON); verifyDeviceStateAutoRotateSettingSet(FOLDED_UNLOCKED_OPEN_UNLOCKED_SETTING); } private void setDeviceStateAutoRotateSetting(String deviceStateAutoRotateSetting) { Settings.Secure.putStringForUser(mMockResolver, DEVICE_STATE_ROTATION_LOCK, deviceStateAutoRotateSetting, UserHandle.USER_CURRENT); } private void setAccelerometerRotationSetting(int accelerometerRotationSetting) { private void setAccelerometerRotationSetting(int accelerometerRotationSetting) { Settings.System.putIntForUser(mMockResolver, ACCELEROMETER_ROTATION, Settings.System.putIntForUser(mMockResolver, ACCELEROMETER_ROTATION, accelerometerRotationSetting, UserHandle.USER_CURRENT); accelerometerRotationSetting, UserHandle.USER_CURRENT); Loading