Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 39ea6606 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Provide mock DeviceStateAutoRotateSettingManager to SmartAutoRotateControllerTest" into main

parents 71cf2232 ab5d3a4b
Loading
Loading
Loading
Loading
+11 −2
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.provider.Settings;
import android.service.rotationresolver.RotationResolverService;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.Preference;
@@ -80,12 +81,20 @@ public class SmartAutoRotateController extends TogglePreferenceController implem
    private RotationPolicy.RotationPolicyListener mRotationPolicyListener;

    public SmartAutoRotateController(Context context, String preferenceKey) {
        this(context, preferenceKey,
                DeviceStateAutoRotateSettingManagerProvider.getSingletonInstance(context));
    }

    @VisibleForTesting
    public SmartAutoRotateController(
            @NonNull Context context,
            @NonNull String preferenceKey,
            @NonNull DeviceStateAutoRotateSettingManager deviceStateAutoRotateSettingManager) {
        super(context, preferenceKey);
        mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
        mPrivacyManager = SensorPrivacyManager.getInstance(context);
        mPowerManager = context.getSystemService(PowerManager.class);
        mDeviceStateAutoRotateSettingsManager =
                DeviceStateAutoRotateSettingManagerProvider.getSingletonInstance(context);
        mDeviceStateAutoRotateSettingsManager = deviceStateAutoRotateSettingManager;
    }

    @Override
+8 −29
Original line number Diff line number Diff line
@@ -43,7 +43,6 @@ import android.provider.Settings;
import androidx.preference.Preference;

import com.android.settings.testutils.ResolveInfoBuilder;
import com.android.settings.testutils.shadow.ShadowDeviceStateAutoRotateSettingManager;
import com.android.settings.testutils.shadow.ShadowRotationPolicy;
import com.android.settings.testutils.shadow.ShadowSensorPrivacyManager;
import com.android.settings.testutils.shadow.ShadowSystemSettings;
@@ -58,14 +57,12 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;

import java.util.List;

@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowSensorPrivacyManager.class, ShadowSystemSettings.class})
public class SmartAutoRotateControllerTest {

    private static final String PACKAGE_NAME = "package_name";

    private SmartAutoRotateController mController;
@@ -77,8 +74,9 @@ public class SmartAutoRotateControllerTest {
    private DeviceStateManager mDeviceStateManager;
    @Mock
    private Resources mResources;
    private ContentResolver mContentResolver;
    @Mock
    private DeviceStateAutoRotateSettingManager mDeviceStateAutoRotateSettingManager;
    private ContentResolver mContentResolver;

    @Before
    public void setUp() {
@@ -98,9 +96,8 @@ public class SmartAutoRotateControllerTest {
        doReturn(mDeviceStateManager).when(context).getSystemService(DeviceStateManager.class);
        doReturn(getDeviceStateList()).when(mDeviceStateManager).getSupportedDeviceStates();
        setDeviceStateRotationLockEnabled(false, mResources);
        mDeviceStateAutoRotateSettingManager =
                DeviceStateAutoRotateSettingManagerProvider.getSingletonInstance(context);
        mController = Mockito.spy(new SmartAutoRotateController(context, "test_key"));
        mController = Mockito.spy(new SmartAutoRotateController(
                context, "test_key", mDeviceStateAutoRotateSettingManager));

        when(mController.isCameraLocked()).thenReturn(false);
        when(mController.isPowerSaveMode()).thenReturn(false);
@@ -150,13 +147,10 @@ public class SmartAutoRotateControllerTest {
    }

    @Test
    @Config(shadows = {
            ShadowDeviceStateAutoRotateSettingManager.class,
            ShadowRotationPolicy.class
    })
    @Config(shadows = {ShadowRotationPolicy.class})
    public void getAvailabilityStatus_deviceStateRotationLocked_returnDisableDependentSetting() {
        enableDeviceStateRotation();
        lockDeviceStateRotation();
        when(mDeviceStateAutoRotateSettingManager.isRotationLockedForAllStates()).thenReturn(true);

        int availabilityStatus = mController.getAvailabilityStatus();

@@ -164,13 +158,10 @@ public class SmartAutoRotateControllerTest {
    }

    @Test
    @Config(shadows = {
            ShadowDeviceStateAutoRotateSettingManager.class,
            ShadowRotationPolicy.class
    })
    @Config(shadows = {ShadowRotationPolicy.class})
    public void getAvailabilityStatus_deviceStateRotationUnlocked_returnAvailable() {
        enableDeviceStateRotation();
        unlockDeviceStateRotation();
        when(mDeviceStateAutoRotateSettingManager.isRotationLockedForAllStates()).thenReturn(false);

        int availabilityStatus = mController.getAvailabilityStatus();

@@ -192,18 +183,6 @@ public class SmartAutoRotateControllerTest {
        setDeviceStateRotationLockEnabled(true, mResources);
    }

    private void lockDeviceStateRotation() {
        ShadowDeviceStateAutoRotateSettingManager shadowManager =
                Shadow.extract(mDeviceStateAutoRotateSettingManager);
        shadowManager.setRotationLockedForAllStates(true);
    }

    private void unlockDeviceStateRotation() {
        ShadowDeviceStateAutoRotateSettingManager shadowManager =
                Shadow.extract(mDeviceStateAutoRotateSettingManager);
        shadowManager.setRotationLockedForAllStates(false);
    }

    /**
     * Returns a list that includes a singular default {@link DeviceState}. To be returned when
     * {@link DeviceStateManager#getSupportedDeviceStates()} is called.
+0 −37
Original line number Diff line number Diff line
/*
 * 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.testutils.shadow;

import com.android.settingslib.devicestate.DeviceStateRotationLockSettingsManager;

import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

@Implements(DeviceStateRotationLockSettingsManager.class)
public class ShadowDeviceStateAutoRotateSettingManager {

    private boolean mIsRotationLockedForAllStates;

    @Implementation
    public boolean isRotationLockedForAllStates() {
        return mIsRotationLockedForAllStates;
    }

    public void setRotationLockedForAllStates(boolean value) {
        mIsRotationLockedForAllStates = value;
    }
}