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

Commit feef99d7 authored by Aaron Liu's avatar Aaron Liu
Browse files

Add tests for Flexiglass sim pin.

Add SimPinInteractorTest and SimPinRepositoryTest. Also update all of
the test infra to accomodate simpin integration. Update all relevant
tests for upstream CL.

Fixes: 291970178
Flag: NONE
Test: atest SystemUiRoboTest

Change-Id: Ic9989693a5194695a59bbeddcf14f1a56ca888a4
parent b1465016
Loading
Loading
Loading
Loading
+3 −3
Original line number Original line Diff line number Diff line
@@ -246,11 +246,9 @@ filegroup {
    srcs: [
    srcs: [
        /* Status bar fakes */
        /* Status bar fakes */
        "tests/src/com/android/systemui/statusbar/pipeline/airplane/data/repository/FakeAirplaneModeRepository.kt",
        "tests/src/com/android/systemui/statusbar/pipeline/airplane/data/repository/FakeAirplaneModeRepository.kt",
        "tests/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/FakeMobileConnectionRepository.kt",
        "tests/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/FakeMobileConnectionsRepository.kt",
        "tests/src/com/android/systemui/statusbar/pipeline/mobile/util/FakeMobileMappingsProxy.kt",
        "tests/src/com/android/systemui/statusbar/pipeline/shared/data/repository/FakeConnectivityRepository.kt",
        "tests/src/com/android/systemui/statusbar/pipeline/shared/data/repository/FakeConnectivityRepository.kt",
        "tests/src/com/android/systemui/statusbar/pipeline/wifi/data/repository/FakeWifiRepository.kt",
        "tests/src/com/android/systemui/statusbar/pipeline/wifi/data/repository/FakeWifiRepository.kt",
        "tests/src/com/android/systemui/statusbar/pipeline/mobile/util/FakeSubscriptionManagerProxy.kt",


        /* QS fakes */
        /* QS fakes */
        "tests/src/com/android/systemui/qs/pipeline/domain/interactor/FakeQSTile.kt",
        "tests/src/com/android/systemui/qs/pipeline/domain/interactor/FakeQSTile.kt",
@@ -263,6 +261,7 @@ filegroup {
    srcs: [
    srcs: [
        /* Keyguard converted tests */
        /* Keyguard converted tests */
        // data
        // data
        "tests/src/com/android/systemui/bouncer/data/repository/SimBouncerRepositoryTest.kt",
        "tests/src/com/android/systemui/keyguard/data/quickaffordance/DoNotDisturbQuickAffordanceConfigTest.kt",
        "tests/src/com/android/systemui/keyguard/data/quickaffordance/DoNotDisturbQuickAffordanceConfigTest.kt",
        "tests/src/com/android/systemui/keyguard/data/quickaffordance/FlashlightQuickAffordanceConfigTest.kt",
        "tests/src/com/android/systemui/keyguard/data/quickaffordance/FlashlightQuickAffordanceConfigTest.kt",
        "tests/src/com/android/systemui/keyguard/data/quickaffordance/HomeControlsKeyguardQuickAffordanceConfigTest.kt",
        "tests/src/com/android/systemui/keyguard/data/quickaffordance/HomeControlsKeyguardQuickAffordanceConfigTest.kt",
@@ -285,6 +284,7 @@ filegroup {
        "tests/src/com/android/systemui/bouncer/domain/interactor/AlternateBouncerInteractorTest.kt",
        "tests/src/com/android/systemui/bouncer/domain/interactor/AlternateBouncerInteractorTest.kt",
        "tests/src/com/android/systemui/bouncer/domain/interactor/PrimaryBouncerCallbackInteractorTest.kt",
        "tests/src/com/android/systemui/bouncer/domain/interactor/PrimaryBouncerCallbackInteractorTest.kt",
        "tests/src/com/android/systemui/bouncer/domain/interactor/PrimaryBouncerInteractorWithCoroutinesTest.kt",
        "tests/src/com/android/systemui/bouncer/domain/interactor/PrimaryBouncerInteractorWithCoroutinesTest.kt",
        "tests/src/com/android/systemui/bouncer/domain/interactor/SimBouncerInteractorTest.kt",
        "tests/src/com/android/systemui/keyguard/domain/interactor/KeyguardInteractorTest.kt",
        "tests/src/com/android/systemui/keyguard/domain/interactor/KeyguardInteractorTest.kt",
        "tests/src/com/android/systemui/keyguard/domain/interactor/KeyguardLongPressInteractorTest.kt",
        "tests/src/com/android/systemui/keyguard/domain/interactor/KeyguardLongPressInteractorTest.kt",
        "tests/src/com/android/systemui/keyguard/domain/interactor/KeyguardQuickAffordanceInteractorTest.kt",
        "tests/src/com/android/systemui/keyguard/domain/interactor/KeyguardQuickAffordanceInteractorTest.kt",
+0 −0

File moved.

+14 −2
Original line number Original line Diff line number Diff line
@@ -29,7 +29,10 @@ import com.android.systemui.SysuiTestCase
import com.android.systemui.authentication.shared.model.AuthenticationMethodModel
import com.android.systemui.authentication.shared.model.AuthenticationMethodModel
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.coroutines.collectValues
import com.android.systemui.coroutines.collectValues
import com.android.systemui.log.table.TableLogBuffer
import com.android.systemui.scene.SceneTestUtils
import com.android.systemui.scene.SceneTestUtils
import com.android.systemui.statusbar.pipeline.mobile.data.repository.FakeMobileConnectionsRepository
import com.android.systemui.statusbar.pipeline.mobile.util.FakeMobileMappingsProxy
import com.android.systemui.user.data.repository.FakeUserRepository
import com.android.systemui.user.data.repository.FakeUserRepository
import com.android.systemui.util.mockito.whenever
import com.android.systemui.util.mockito.whenever
import com.google.common.truth.Truth.assertThat
import com.google.common.truth.Truth.assertThat
@@ -51,10 +54,12 @@ class AuthenticationRepositoryTest : SysuiTestCase() {


    @Mock private lateinit var lockPatternUtils: LockPatternUtils
    @Mock private lateinit var lockPatternUtils: LockPatternUtils
    @Mock private lateinit var getSecurityMode: Function<Int, KeyguardSecurityModel.SecurityMode>
    @Mock private lateinit var getSecurityMode: Function<Int, KeyguardSecurityModel.SecurityMode>
    @Mock private lateinit var tableLogger: TableLogBuffer


    private val testUtils = SceneTestUtils(this)
    private val testUtils = SceneTestUtils(this)
    private val testScope = testUtils.testScope
    private val testScope = testUtils.testScope
    private val userRepository = FakeUserRepository()
    private val userRepository = FakeUserRepository()
    private lateinit var mobileConnectionsRepository: FakeMobileConnectionsRepository


    private lateinit var underTest: AuthenticationRepository
    private lateinit var underTest: AuthenticationRepository


@@ -67,6 +72,8 @@ class AuthenticationRepositoryTest : SysuiTestCase() {
        userRepository.setUserInfos(USER_INFOS)
        userRepository.setUserInfos(USER_INFOS)
        runBlocking { userRepository.setSelectedUserInfo(USER_INFOS[0]) }
        runBlocking { userRepository.setSelectedUserInfo(USER_INFOS[0]) }
        whenever(getSecurityMode.apply(anyInt())).thenAnswer { currentSecurityMode }
        whenever(getSecurityMode.apply(anyInt())).thenAnswer { currentSecurityMode }
        mobileConnectionsRepository =
            FakeMobileConnectionsRepository(FakeMobileMappingsProxy(), tableLogger)


        underTest =
        underTest =
            AuthenticationRepositoryImpl(
            AuthenticationRepositoryImpl(
@@ -76,6 +83,7 @@ class AuthenticationRepositoryTest : SysuiTestCase() {
                userRepository = userRepository,
                userRepository = userRepository,
                lockPatternUtils = lockPatternUtils,
                lockPatternUtils = lockPatternUtils,
                broadcastDispatcher = fakeBroadcastDispatcher,
                broadcastDispatcher = fakeBroadcastDispatcher,
                mobileConnectionsRepository = mobileConnectionsRepository,
            )
            )
    }
    }


@@ -97,6 +105,11 @@ class AuthenticationRepositoryTest : SysuiTestCase() {
            assertThat(authMethod).isEqualTo(AuthenticationMethodModel.None)
            assertThat(authMethod).isEqualTo(AuthenticationMethodModel.None)
            assertThat(underTest.getAuthenticationMethod())
            assertThat(underTest.getAuthenticationMethod())
                .isEqualTo(AuthenticationMethodModel.None)
                .isEqualTo(AuthenticationMethodModel.None)

            currentSecurityMode = KeyguardSecurityModel.SecurityMode.SimPin
            mobileConnectionsRepository.isAnySimSecure.value = true
            assertThat(authMethod).isEqualTo(AuthenticationMethodModel.Sim)
            assertThat(underTest.getAuthenticationMethod()).isEqualTo(AuthenticationMethodModel.Sim)
        }
        }


    @Test
    @Test
@@ -157,7 +170,6 @@ class AuthenticationRepositoryTest : SysuiTestCase() {


            userRepository.setSelectedUserInfo(USER_INFOS[1])
            userRepository.setSelectedUserInfo(USER_INFOS[1])
            assertThat(values.last()).isTrue()
            assertThat(values.last()).isTrue()

        }
        }


    private fun setSecurityModeAndDispatchBroadcast(
    private fun setSecurityModeAndDispatchBroadcast(
+201 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2023 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.bouncer.data.repository

import android.telephony.TelephonyManager
import android.telephony.euicc.EuiccManager
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.keyguard.KeyguardUpdateMonitor
import com.android.keyguard.KeyguardUpdateMonitorCallback
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.statusbar.pipeline.mobile.util.FakeSubscriptionManagerProxy
import com.android.systemui.util.mockito.any
import com.android.systemui.util.mockito.whenever
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.anyInt
import org.mockito.MockitoAnnotations

@SmallTest
@RunWith(AndroidJUnit4::class)
class SimBouncerRepositoryTest : SysuiTestCase() {
    @Mock lateinit var euiccManager: EuiccManager
    @Mock lateinit var keyguardUpdateMonitor: KeyguardUpdateMonitor

    private val dispatcher = StandardTestDispatcher()
    private val testScope = TestScope(dispatcher)
    private val fakeSubscriptionManagerProxy = FakeSubscriptionManagerProxy()
    private val keyguardUpdateMonitorCallbacks = mutableListOf<KeyguardUpdateMonitorCallback>()

    private lateinit var underTest: SimBouncerRepositoryImpl

    @Before
    fun setup() {
        MockitoAnnotations.initMocks(/* testClass = */ this)
        whenever(keyguardUpdateMonitor.registerCallback(any())).thenAnswer {
            val cb = it.arguments[0] as KeyguardUpdateMonitorCallback
            keyguardUpdateMonitorCallbacks.add(cb)
        }
        whenever(keyguardUpdateMonitor.removeCallback(any())).thenAnswer {
            keyguardUpdateMonitorCallbacks.remove(it.arguments[0])
        }
        underTest =
            SimBouncerRepositoryImpl(
                applicationScope = testScope.backgroundScope,
                backgroundDispatcher = dispatcher,
                resources = context.resources,
                keyguardUpdateMonitor = keyguardUpdateMonitor,
                subscriptionManager = fakeSubscriptionManagerProxy,
                broadcastDispatcher = fakeBroadcastDispatcher,
                euiccManager = euiccManager,
            )
    }

    @Test
    fun subscriptionId() =
        testScope.runTest {
            val subscriptionId =
                emitSubscriptionIdAndCollectLastValue(underTest.subscriptionId, subId = 2)
            assertThat(subscriptionId).isEqualTo(2)
        }

    @Test
    fun activeSubscriptionInfo() =
        testScope.runTest {
            fakeSubscriptionManagerProxy.setActiveSubscriptionInfo(subId = 2)
            val activeSubscriptionInfo =
                emitSubscriptionIdAndCollectLastValue(underTest.activeSubscriptionInfo, subId = 2)

            assertThat(activeSubscriptionInfo?.subscriptionId).isEqualTo(2)
        }

    @Test
    fun isLockedEsim_initialValue_isNull() =
        testScope.runTest {
            val isLockedEsim by collectLastValue(underTest.isLockedEsim)
            assertThat(isLockedEsim).isNull()
        }

    @Test
    fun isLockedEsim() =
        testScope.runTest {
            whenever(euiccManager.isEnabled).thenReturn(true)
            fakeSubscriptionManagerProxy.setActiveSubscriptionInfo(subId = 2, isEmbedded = true)
            val isLockedEsim =
                emitSubscriptionIdAndCollectLastValue(underTest.isLockedEsim, subId = 2)
            assertThat(isLockedEsim).isTrue()
        }

    @Test
    fun isLockedEsim_notEmbedded() =
        testScope.runTest {
            fakeSubscriptionManagerProxy.setActiveSubscriptionInfo(subId = 2, isEmbedded = false)
            val isLockedEsim =
                emitSubscriptionIdAndCollectLastValue(underTest.isLockedEsim, subId = 2)
            assertThat(isLockedEsim).isFalse()
        }

    @Test
    fun isSimPukLocked() =
        testScope.runTest {
            val isSimPukLocked =
                emitSubscriptionIdAndCollectLastValue(
                    underTest.isSimPukLocked,
                    subId = 2,
                    isSimPuk = true
                )
            assertThat(isSimPukLocked).isTrue()
        }

    @Test
    fun setSimPukUserInput() {
        val pukCode = "00000000"
        val pinCode = "1234"
        underTest.setSimPukUserInput(pukCode, pinCode)
        assertThat(underTest.simPukInputModel.enteredSimPuk).isEqualTo(pukCode)
        assertThat(underTest.simPukInputModel.enteredSimPin).isEqualTo(pinCode)
    }

    @Test
    fun setSimPukUserInput_nullPuk() {
        val pukCode = null
        val pinCode = "1234"
        underTest.setSimPukUserInput(pukCode, pinCode)
        assertThat(underTest.simPukInputModel.enteredSimPuk).isNull()
        assertThat(underTest.simPukInputModel.enteredSimPin).isEqualTo(pinCode)
    }

    @Test
    fun setSimPukUserInput_nullPin() {
        val pukCode = "00000000"
        val pinCode = null
        underTest.setSimPukUserInput(pukCode, pinCode)
        assertThat(underTest.simPukInputModel.enteredSimPuk).isEqualTo(pukCode)
        assertThat(underTest.simPukInputModel.enteredSimPin).isNull()
    }

    @Test
    fun setSimPukUserInput_nullCodes() {
        underTest.setSimPukUserInput()
        assertThat(underTest.simPukInputModel.enteredSimPuk).isNull()
        assertThat(underTest.simPukInputModel.enteredSimPin).isNull()
    }

    @Test
    fun setSimPinVerificationErrorMessage() =
        testScope.runTest {
            val errorMsg = "error"
            underTest.setSimVerificationErrorMessage(errorMsg)
            val msg by collectLastValue(underTest.errorDialogMessage)
            assertThat(msg).isEqualTo(errorMsg)
        }

    /** Emits a new sim card state and collects the last value of the flow argument. */
    @OptIn(ExperimentalCoroutinesApi::class)
    private fun <T> TestScope.emitSubscriptionIdAndCollectLastValue(
        flow: Flow<T>,
        subId: Int = 1,
        isSimPuk: Boolean = false
    ): T? {
        val value by collectLastValue(flow)
        runCurrent()
        val simState =
            if (isSimPuk) {
                TelephonyManager.SIM_STATE_PUK_REQUIRED
            } else {
                TelephonyManager.SIM_STATE_PIN_REQUIRED
            }
        whenever(keyguardUpdateMonitor.getNextSubIdForState(anyInt())).thenReturn(-1)
        whenever(keyguardUpdateMonitor.getNextSubIdForState(simState)).thenReturn(subId)
        keyguardUpdateMonitorCallbacks.forEach {
            it.onSimStateChanged(subId, /* slotId= */ 0, simState)
        }
        runCurrent()
        return value
    }
}
+13 −0
Original line number Original line Diff line number Diff line
@@ -87,6 +87,19 @@ class BouncerInteractorTest : SysuiTestCase() {
                .isEqualTo(AuthenticationResult.SUCCEEDED)
                .isEqualTo(AuthenticationResult.SUCCEEDED)
        }
        }


    @Test
    fun pinAuthMethod_sim_skipsAuthentication() =
        testScope.runTest {
            utils.authenticationRepository.setAuthenticationMethod(AuthenticationMethodModel.Sim)
            runCurrent()

            // We rely on TelephonyManager to authenticate the sim card.
            // Additionally, authenticating the sim card does not unlock the device.
            // Thus, when auth method is sim, we expect to skip here.
            assertThat(underTest.authenticate(FakeAuthenticationRepository.DEFAULT_PIN))
                .isEqualTo(AuthenticationResult.SKIPPED)
        }

    @Test
    @Test
    fun pinAuthMethod_tryAutoConfirm_withAutoConfirmPin() =
    fun pinAuthMethod_tryAutoConfirm_withAutoConfirmPin() =
        testScope.runTest {
        testScope.runTest {
Loading