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

Commit 2c05813a authored by Beverly's avatar Beverly Committed by Beverly Tai
Browse files

Cancel pending tasks by callling underTest.onPause to avoid mockito-npe

KeyguardPinViewController extends KeyguardAbsKeyInputViewController
which can have mPendingLockCheck. Call #onPause to cancel the pending
lock check so we don't have it running after we finish running
the test and the mocks are null (causing npe).

Fixes: 424667843
Test: atest KeyguardPinViewControllerTest
Flag: EXEMPT bugfix & test only fix
Change-Id: I9b50e46ed6fbee245faf444e79b2c2bfa49bb1da
parent 5a480c2d
Loading
Loading
Loading
Loading
+48 −38
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ import com.android.systemui.testKosmos
import com.android.systemui.user.domain.interactor.SelectedUserInteractor
import com.android.systemui.util.mockito.whenever
import com.google.common.truth.Truth.assertThat
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@@ -110,6 +111,11 @@ class KeyguardPinViewControllerTest : SysuiTestCase() {
    @Captor lateinit var postureCallbackCaptor: ArgumentCaptor<DevicePostureController.Callback>

    private val kosmos = testKosmos()
    private var underTest: KeyguardPinViewController? = null
        set(value) {
            field?.onPause()
            field = value
        }

    @Before
    fun setup() {
@@ -136,10 +142,14 @@ class KeyguardPinViewControllerTest : SysuiTestCase() {
                .requireViewById(R.id.keyguard_pin_view) as KeyguardPINView
    }

    private fun constructPinViewController(
        mKeyguardPinView: KeyguardPINView
    ): KeyguardPinViewController {
        return KeyguardPinViewController(
    @After
    fun tearDown() {
        underTest?.onPause()
    }

    private fun constructPinViewController(mKeyguardPinView: KeyguardPINView) {
        underTest =
            KeyguardPinViewController(
                mKeyguardPinView,
                keyguardUpdateMonitor,
                securityMode,
@@ -162,22 +172,22 @@ class KeyguardPinViewControllerTest : SysuiTestCase() {

    @Test
    fun onViewAttached_deviceHalfFolded_propagatedToPatternView() {
        val pinViewController = constructPinViewController(objectKeyguardPINView)
        constructPinViewController(objectKeyguardPINView)
        overrideResource(R.dimen.half_opened_bouncer_height_ratio, 0.5f)
        whenever(postureController.devicePosture).thenReturn(DEVICE_POSTURE_HALF_OPENED)

        pinViewController.onViewAttached()
        underTest?.onViewAttached()

        assertThat(getPinTopGuideline()).isEqualTo(getHalfOpenedBouncerHeightRatio())
    }

    @Test
    fun onDevicePostureChanged_deviceOpened_propagatedToPatternView() {
        val pinViewController = constructPinViewController(objectKeyguardPINView)
        constructPinViewController(objectKeyguardPINView)
        overrideResource(R.dimen.half_opened_bouncer_height_ratio, 0.5f)

        whenever(postureController.devicePosture).thenReturn(DEVICE_POSTURE_HALF_OPENED)
        pinViewController.onViewAttached()
        underTest?.onViewAttached()

        // Verify view begins in posture state DEVICE_POSTURE_HALF_OPENED
        assertThat(getPinTopGuideline()).isEqualTo(getHalfOpenedBouncerHeightRatio())
@@ -211,9 +221,9 @@ class KeyguardPinViewControllerTest : SysuiTestCase() {

    @Test
    fun testOnViewAttached() {
        val pinViewController = constructPinViewController(mockKeyguardPinView)
        constructPinViewController(mockKeyguardPinView)

        pinViewController.onViewAttached()
        underTest?.onViewAttached()

        verify(keyguardMessageAreaController)
            .setMessage(context.resources.getString(R.string.keyguard_enter_your_pin), false)
@@ -221,23 +231,23 @@ class KeyguardPinViewControllerTest : SysuiTestCase() {

    @Test
    fun testOnViewAttached_withExistingMessage() {
        val pinViewController = constructPinViewController(mockKeyguardPinView)
        constructPinViewController(mockKeyguardPinView)
        Mockito.`when`(keyguardMessageAreaController.message).thenReturn("Unlock to continue.")

        pinViewController.onViewAttached()
        underTest?.onViewAttached()

        verify(keyguardMessageAreaController, Mockito.never()).setMessage(anyString(), anyBoolean())
    }

    @Test
    fun testOnViewAttached_withAutoPinConfirmationFailedPasswordAttemptsLessThan5() {
        val pinViewController = constructPinViewController(mockKeyguardPinView)
        constructPinViewController(mockKeyguardPinView)
        `when`(lockPatternUtils.getPinLength(anyInt())).thenReturn(6)
        `when`(lockPatternUtils.isAutoPinConfirmEnabled(anyInt())).thenReturn(true)
        `when`(lockPatternUtils.getCurrentFailedPasswordAttempts(anyInt())).thenReturn(3)
        `when`(passwordTextView.text).thenReturn("")

        pinViewController.onViewAttached()
        underTest?.onViewAttached()

        verify(deleteButton).visibility = View.INVISIBLE
        verify(enterButton).visibility = View.INVISIBLE
@@ -247,13 +257,13 @@ class KeyguardPinViewControllerTest : SysuiTestCase() {

    @Test
    fun testOnViewAttached_withAutoPinConfirmationFailedPasswordAttemptsMoreThan5() {
        val pinViewController = constructPinViewController(mockKeyguardPinView)
        constructPinViewController(mockKeyguardPinView)
        `when`(lockPatternUtils.getPinLength(anyInt())).thenReturn(6)
        `when`(lockPatternUtils.isAutoPinConfirmEnabled(anyInt())).thenReturn(true)
        `when`(lockPatternUtils.getCurrentFailedPasswordAttempts(anyInt())).thenReturn(6)
        `when`(passwordTextView.text).thenReturn("")

        pinViewController.onViewAttached()
        underTest?.onViewAttached()

        verify(deleteButton).visibility = View.VISIBLE
        verify(enterButton).visibility = View.VISIBLE
@@ -263,16 +273,16 @@ class KeyguardPinViewControllerTest : SysuiTestCase() {

    @Test
    fun handleLockout_readsNumberOfErrorAttempts() {
        val pinViewController = constructPinViewController(mockKeyguardPinView)
        constructPinViewController(mockKeyguardPinView)

        pinViewController.handleAttemptLockout(0)
        underTest?.handleAttemptLockout(0)

        verify(lockPatternUtils).getCurrentFailedPasswordAttempts(anyInt())
    }

    @Test
    fun onUserInput_autoConfirmation_attemptsUnlock() {
        val pinViewController = constructPinViewController(mockKeyguardPinView)
        constructPinViewController(mockKeyguardPinView)
        whenever(lockPatternUtils.getPinLength(anyInt())).thenReturn(6)
        whenever(lockPatternUtils.isAutoPinConfirmEnabled(anyInt())).thenReturn(true)
        whenever(passwordTextView.text).thenReturn("000000")
@@ -280,7 +290,7 @@ class KeyguardPinViewControllerTest : SysuiTestCase() {
        whenever(mockKeyguardPinView.enteredCredential)
            .thenReturn(LockscreenCredential.createPin("000000"))

        pinViewController.onUserInput()
        underTest?.onUserInput()

        verify(uiEventLogger).log(PinBouncerUiEvent.ATTEMPT_UNLOCK_WITH_AUTO_CONFIRM_FEATURE)
        verify(keyguardUpdateMonitor).setCredentialAttempted()