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

Commit acbdee14 authored by Behnam Heydarshahi's avatar Behnam Heydarshahi Committed by Android (Google) Code Review
Browse files

Merge "Migrate rotation lock tile" into main

parents 8094ddfc 39013e2a
Loading
Loading
Loading
Loading
+125 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.systemui.camera.data.repository

import android.os.UserHandle
import android.provider.Settings
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectValues
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.testScope
import com.android.systemui.util.settings.fakeSettings
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith

@OptIn(ExperimentalCoroutinesApi::class)
@SmallTest
@RunWith(AndroidJUnit4::class)
@android.platform.test.annotations.EnabledOnRavenwood
class CameraAutoRotateRepositoryImplTest : SysuiTestCase() {
    private val kosmos = Kosmos()
    private val testScope = kosmos.testScope
    private val settings = kosmos.fakeSettings
    private val testUser = UserHandle.of(1)

    private val underTest =
        CameraAutoRotateRepositoryImpl(settings, testScope.testScheduler, testScope.backgroundScope)

    /** 3 changes => 3 change signals + 1 signal emitted at start => 4 signals */
    @Test
    fun isCameraAutoRotateSettingEnabled_3times() =
        testScope.runTest {
            settings.putIntForUser(SETTING_NAME, DISABLE, testUser.identifier)
            val isCameraAutoRotateSettingEnabled by
                collectValues(underTest.isCameraAutoRotateSettingEnabled(testUser))
            runCurrent()
            assertThat(isCameraAutoRotateSettingEnabled.last()).isFalse()

            settings.putIntForUser(SETTING_NAME, ENABLE, testUser.identifier)
            runCurrent()
            assertThat(isCameraAutoRotateSettingEnabled.last()).isTrue()

            settings.putIntForUser(SETTING_NAME, DISABLE, testUser.identifier)
            runCurrent()
            assertThat(isCameraAutoRotateSettingEnabled.last()).isFalse()

            settings.putIntForUser(SETTING_NAME, ENABLE, testUser.identifier)
            runCurrent()
            assertThat(isCameraAutoRotateSettingEnabled.last()).isTrue()

            assertThat(isCameraAutoRotateSettingEnabled).hasSize(4)
        }

    @Test
    fun isCameraAutoRotateSettingEnabled_emitsOnStart() =
        testScope.runTest {
            val isCameraAutoRotateSettingEnabled: List<Boolean> by
                collectValues(underTest.isCameraAutoRotateSettingEnabled(testUser))

            runCurrent()

            assertThat(isCameraAutoRotateSettingEnabled).hasSize(1)
        }

    /** 0 for 0 changes + 1 signal emitted on start => 1 signal */
    @Test
    fun isCameraAutoRotateSettingEnabled_0Times() =
        testScope.runTest {
            settings.putIntForUser(SETTING_NAME, DISABLE, testUser.identifier)
            val isCameraAutoRotateSettingEnabled: List<Boolean> by
                collectValues(underTest.isCameraAutoRotateSettingEnabled(testUser))
            runCurrent()

            settings.putIntForUser(SETTING_NAME, DISABLE, testUser.identifier)
            runCurrent()

            assertThat(isCameraAutoRotateSettingEnabled).hasSize(1)
            assertThat(isCameraAutoRotateSettingEnabled[0]).isFalse()
        }

    /** Maintain that flows are cached by user */
    @Test
    fun sameUserCallsIsCameraAutoRotateSettingEnabledTwice_getsSameFlow() =
        testScope.runTest {
            val flow1 = underTest.isCameraAutoRotateSettingEnabled(testUser)
            val flow2 = underTest.isCameraAutoRotateSettingEnabled(testUser)

            assertThat(flow1).isEqualTo(flow2)
        }

    @Test
    fun differentUsersCallIsCameraAutoRotateSettingEnabled_getDifferentFlow() =
        testScope.runTest {
            val user2 = UserHandle.of(2)
            val flow1 = underTest.isCameraAutoRotateSettingEnabled(testUser)
            val flow2 = underTest.isCameraAutoRotateSettingEnabled(user2)

            assertThat(flow1).isNotEqualTo(flow2)
        }

    private companion object {
        private const val SETTING_NAME = Settings.Secure.CAMERA_AUTOROTATE
        private const val DISABLE = 0
        private const val ENABLE = 1
    }
}
+104 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.systemui.camera.data.repository

import android.hardware.SensorPrivacyManager
import android.os.UserHandle
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.testScope
import com.android.systemui.util.mockito.mock
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentCaptor
import org.mockito.ArgumentMatchers
import org.mockito.Mockito

@OptIn(ExperimentalCoroutinesApi::class)
@SmallTest
@RunWith(AndroidJUnit4::class)
@android.platform.test.annotations.EnabledOnRavenwood
class CameraSensorPrivacyRepositoryImplTest : SysuiTestCase() {
    private val kosmos = Kosmos()
    private val testScope = kosmos.testScope
    private val testUser = UserHandle.of(1)
    private val privacyManager = mock<SensorPrivacyManager>()
    private val underTest =
        CameraSensorPrivacyRepositoryImpl(
            testScope.testScheduler,
            testScope.backgroundScope,
            privacyManager
        )

    @Test
    fun isEnabled_2TimesForSameUserReturnsCachedFlow() =
        testScope.runTest {
            val flow1 = underTest.isEnabled(testUser)
            val flow2 = underTest.isEnabled(testUser)
            runCurrent()

            assertThat(flow1).isEqualTo(flow2)
        }

    @Test
    fun isEnabled_2TimesForDifferentUsersReturnsTwoDifferentFlows() =
        testScope.runTest {
            val user2 = UserHandle.of(2)

            val flow1 = underTest.isEnabled(testUser)
            val flow2 = underTest.isEnabled(user2)
            runCurrent()

            assertThat(flow1).isNotEqualTo(flow2)
        }

    @Test
    fun isEnabled_dataMatchesSensorPrivacyManager() =
        testScope.runTest {
            val isEnabled = collectLastValue(underTest.isEnabled(testUser))

            val captor =
                ArgumentCaptor.forClass(
                    SensorPrivacyManager.OnSensorPrivacyChangedListener::class.java
                )
            runCurrent()
            assertThat(isEnabled()).isEqualTo(false)

            Mockito.verify(privacyManager)
                .addSensorPrivacyListener(
                    ArgumentMatchers.eq(SensorPrivacyManager.Sensors.CAMERA),
                    ArgumentMatchers.eq(testUser.identifier),
                    captor.capture()
                )
            val sensorPrivacyCallback = captor.value!!

            sensorPrivacyCallback.onSensorPrivacyChanged(SensorPrivacyManager.Sensors.CAMERA, true)
            runCurrent()
            assertThat(isEnabled()).isEqualTo(true)

            sensorPrivacyCallback.onSensorPrivacyChanged(SensorPrivacyManager.Sensors.CAMERA, false)
            runCurrent()
            assertThat(isEnabled()).isEqualTo(false)
        }
}
+78 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.systemui.camera.data.repository

import android.os.UserHandle
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectValues
import com.android.systemui.kosmos.Kosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith

@OptIn(ExperimentalCoroutinesApi::class)
@SmallTest
@RunWith(AndroidJUnit4::class)
@android.platform.test.annotations.EnabledOnRavenwood
class FakeCameraAutoRotateRepositoryTest : SysuiTestCase() {
    private val kosmos = Kosmos()
    private val underTest = kosmos.fakeCameraAutoRotateRepository
    private val testUser = UserHandle.of(1)

    @Test
    fun isCameraAutoRotateSettingEnabled_emitsFalseOnStart() = runTest {
        val isCameraAutoRotateSettingEnabled by
            collectValues(underTest.isCameraAutoRotateSettingEnabled(testUser))

        assertThat(isCameraAutoRotateSettingEnabled).hasSize(1)
        assertThat(isCameraAutoRotateSettingEnabled.first()).isFalse()
    }

    /**
     * The value explicitly set in this test is not distinct, therefore only 1 value is collected.
     */
    @Test
    fun isCameraAutoRotateSettingEnabled_emitsDistinctValueOnly() = runTest {
        val isCameraAutoRotateSettingEnabled by
            collectValues(underTest.isCameraAutoRotateSettingEnabled(testUser))
        underTest.setEnabled(testUser, false)
        runCurrent()

        assertThat(isCameraAutoRotateSettingEnabled).hasSize(1)
        assertThat(isCameraAutoRotateSettingEnabled.first()).isFalse()
    }

    @Test
    fun isCameraAutoRotateSettingEnabled_canSetValue3Times() = runTest {
        val isCameraAutoRotateSettingEnabled by
            collectValues(underTest.isCameraAutoRotateSettingEnabled(testUser))
        runCurrent()
        underTest.setEnabled(testUser, true)
        runCurrent()
        underTest.setEnabled(testUser, false)
        runCurrent()
        underTest.setEnabled(testUser, true)
        runCurrent()
        assertThat(isCameraAutoRotateSettingEnabled).hasSize(4)
        assertThat(isCameraAutoRotateSettingEnabled.last()).isTrue()
    }
}
+75 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.systemui.camera.data.repository

import android.os.UserHandle
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectValues
import com.android.systemui.kosmos.Kosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith

@OptIn(ExperimentalCoroutinesApi::class)
@SmallTest
@RunWith(AndroidJUnit4::class)
@android.platform.test.annotations.EnabledOnRavenwood
class FakeCameraSensorPrivacyRepositoryTest : SysuiTestCase() {
    private val kosmos = Kosmos()
    private val underTest = kosmos.fakeCameraSensorPrivacyRepository
    private val testUser = UserHandle.of(1)

    @Test
    fun isCameraSensorPrivacyEnabled_emitsFalseOnStart() = runTest {
        val isCameraSensorPrivacySettingEnabled by collectValues(underTest.isEnabled(testUser))

        assertThat(isCameraSensorPrivacySettingEnabled).hasSize(1)
        assertThat(isCameraSensorPrivacySettingEnabled.first()).isFalse()
    }

    /**
     * The value explicitly set in this test is not distinct, therefore only 1 value is collected.
     */
    @Test
    fun isCameraSensorPrivacyEnabled_emitsDistinctValueOnly() = runTest {
        val isCameraSensorPrivacySettingEnabled by collectValues(underTest.isEnabled(testUser))
        underTest.setEnabled(testUser, false)
        runCurrent()

        assertThat(isCameraSensorPrivacySettingEnabled).hasSize(1)
        assertThat(isCameraSensorPrivacySettingEnabled.first()).isFalse()
    }

    @Test
    fun isCameraSensorPrivacyEnabled_canSetValue3Times() = runTest {
        val isCameraSensorPrivacySettingEnabled by collectValues(underTest.isEnabled(testUser))
        runCurrent()
        underTest.setEnabled(testUser, true)
        runCurrent()
        underTest.setEnabled(testUser, false)
        runCurrent()
        underTest.setEnabled(testUser, true)
        runCurrent()
        assertThat(isCameraSensorPrivacySettingEnabled).hasSize(4)
        assertThat(isCameraSensorPrivacySettingEnabled.last()).isTrue()
    }
}
+227 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.systemui.qs.tiles.impl.rotation.domain.interactor

import android.Manifest
import android.content.packageManager
import android.content.pm.PackageManager
import android.os.UserHandle
import android.platform.test.annotations.EnabledOnRavenwood
import android.testing.LeakCheck
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.camera.data.repository.fakeCameraAutoRotateRepository
import com.android.systemui.camera.data.repository.fakeCameraSensorPrivacyRepository
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.testScope
import com.android.systemui.qs.tiles.base.interactor.DataUpdateTrigger
import com.android.systemui.util.mockito.eq
import com.android.systemui.util.mockito.whenever
import com.android.systemui.utils.leaks.FakeBatteryController
import com.android.systemui.utils.leaks.FakeRotationLockController
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.toCollection
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@OptIn(ExperimentalCoroutinesApi::class)
@SmallTest
@EnabledOnRavenwood
@RunWith(AndroidJUnit4::class)
class RotationLockTileDataInteractorTest : SysuiTestCase() {
    private val kosmos = Kosmos()
    private val testScope = kosmos.testScope
    private val batteryController = FakeBatteryController(LeakCheck())
    private val rotationController = FakeRotationLockController(LeakCheck())
    private val fakeCameraAutoRotateRepository = kosmos.fakeCameraAutoRotateRepository
    private val fakeCameraSensorPrivacyRepository = kosmos.fakeCameraSensorPrivacyRepository
    private val packageManager = kosmos.packageManager

    private val testUser = UserHandle.of(1)
    private lateinit var underTest: RotationLockTileDataInteractor

    @Before
    fun setup() {
        whenever(packageManager.rotationResolverPackageName).thenReturn(TEST_PACKAGE_NAME)
        whenever(
                packageManager.checkPermission(
                    eq(Manifest.permission.CAMERA),
                    eq(TEST_PACKAGE_NAME)
                )
            )
            .thenReturn(PackageManager.PERMISSION_GRANTED)

        underTest =
            RotationLockTileDataInteractor(
                rotationController,
                batteryController,
                fakeCameraAutoRotateRepository,
                fakeCameraSensorPrivacyRepository,
                packageManager,
                context.orCreateTestableResources
                    .apply {
                        addOverride(com.android.internal.R.bool.config_allowRotationResolver, true)
                    }
                    .resources
            )
    }

    @Test
    fun availability_isTrue() =
        testScope.runTest {
            val availability = underTest.availability(testUser).toCollection(mutableListOf())

            assertThat(availability).hasSize(1)
            assertThat(availability.last()).isTrue()
        }

    @Test
    fun tileData_isRotationLockedMatchesRotationController() =
        testScope.runTest {
            val data by
                collectLastValue(
                    underTest.tileData(testUser, flowOf(DataUpdateTrigger.InitialRequest))
                )

            runCurrent()
            assertThat(data!!.isRotationLocked).isEqualTo(false)

            rotationController.setRotationLocked(true, CALLER)
            runCurrent()
            assertThat(data!!.isRotationLocked).isEqualTo(true)

            rotationController.setRotationLocked(false, CALLER)
            runCurrent()
            assertThat(data!!.isRotationLocked).isEqualTo(false)
        }

    @Test
    fun tileData_cameraRotationMatchesBatteryController() =
        testScope.runTest {
            setupControllersToEnableCameraRotation()
            val data by
                collectLastValue(
                    underTest.tileData(testUser, flowOf(DataUpdateTrigger.InitialRequest))
                )

            runCurrent()
            assertThat(data!!.isCameraRotationEnabled).isTrue()

            batteryController.setPowerSaveMode(true)
            runCurrent()
            assertThat(data!!.isCameraRotationEnabled).isFalse()

            batteryController.setPowerSaveMode(false)
            runCurrent()
            assertThat(data!!.isCameraRotationEnabled).isTrue()
        }

    @Test
    fun tileData_cameraRotationMatchesSensorPrivacyRepository() =
        testScope.runTest {
            setupControllersToEnableCameraRotation()
            val lastValue by
                this.collectLastValue(
                    underTest.tileData(testUser, flowOf(DataUpdateTrigger.InitialRequest))
                )
            runCurrent()
            assertThat(lastValue!!.isCameraRotationEnabled).isTrue()

            fakeCameraSensorPrivacyRepository.setEnabled(testUser, true)
            runCurrent()
            assertThat(lastValue!!.isCameraRotationEnabled).isFalse()

            fakeCameraSensorPrivacyRepository.setEnabled(testUser, false)
            runCurrent()
            assertThat(lastValue!!.isCameraRotationEnabled).isTrue()
        }

    @Test
    fun tileData_cameraRotationMatchesAutoRotateRepository() =
        testScope.runTest {
            setupControllersToEnableCameraRotation()

            val lastValue by
                collectLastValue(
                    underTest.tileData(testUser, flowOf(DataUpdateTrigger.InitialRequest))
                )
            runCurrent()
            assertThat(lastValue!!.isCameraRotationEnabled).isTrue()

            fakeCameraAutoRotateRepository.setEnabled(testUser, false)
            runCurrent()
            assertThat(lastValue!!.isCameraRotationEnabled).isFalse()

            fakeCameraAutoRotateRepository.setEnabled(testUser, true)
            runCurrent()
            assertThat(lastValue!!.isCameraRotationEnabled).isTrue()
        }

    @Test
    fun tileData_matchesPackageManagerPermissionDenied() =
        testScope.runTest {
            whenever(
                    packageManager.checkPermission(
                        eq(Manifest.permission.CAMERA),
                        eq(TEST_PACKAGE_NAME)
                    )
                )
                .thenReturn(PackageManager.PERMISSION_DENIED)

            val lastValue by
                collectLastValue(
                    underTest.tileData(testUser, flowOf(DataUpdateTrigger.InitialRequest))
                )
            runCurrent()
            assertThat(lastValue!!.isCameraRotationEnabled).isEqualTo(false)
        }

    @Test
    fun tileData_setConfigAllowRotationResolverToFalse_cameraRotationIsNotEnabled() =
        testScope.runTest {
            underTest.apply {
                overrideResource(com.android.internal.R.bool.config_allowRotationResolver, false)
            }
            setupControllersToEnableCameraRotation()
            val lastValue by
                collectLastValue(
                    underTest.tileData(testUser, flowOf(DataUpdateTrigger.InitialRequest))
                )
            runCurrent()

            assertThat(lastValue!!.isCameraRotationEnabled).isEqualTo(false)
        }

    private fun setupControllersToEnableCameraRotation() {
        rotationController.setRotationLocked(true, CALLER)
        batteryController.setPowerSaveMode(false)
        fakeCameraSensorPrivacyRepository.setEnabled(testUser, false)
        fakeCameraAutoRotateRepository.setEnabled(testUser, true)
    }

    private companion object {
        private const val CALLER = "RotationLockTileDataInteractorTest"
        private const val TEST_PACKAGE_NAME = "com.test"
    }
}
Loading