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

Commit 3cdfdeb1 authored by Matt Pietal's avatar Matt Pietal
Browse files

Controls UI - Do not clear callbacks on user change

Listeners would get into an invalid state when changing users.

Fixes: 155990179
Test: atest ControlsListingControllerImplTest
Change-Id: I9c48c767a7da47eca029a27f9c223e92c2359412
parent f3b5ba7a
Loading
Loading
Loading
Loading
+8 −2
Original line number Original line Diff line number Diff line
@@ -106,9 +106,15 @@ class ControlsListingControllerImpl @VisibleForTesting constructor(


    override fun changeUser(newUser: UserHandle) {
    override fun changeUser(newUser: UserHandle) {
        backgroundExecutor.execute {
        backgroundExecutor.execute {
            callbacks.clear()
            availableServices = emptyList()
            serviceListing.setListening(false)
            serviceListing.setListening(false)

            // Notify all callbacks in order to clear their existing state prior to attaching
            // a new listener
            availableServices = emptyList()
            callbacks.forEach {
                it.onServicesUpdated(emptyList())
            }

            currentUserId = newUser.identifier
            currentUserId = newUser.identifier
            val contextForUser = context.createContextAsUser(newUser, 0)
            val contextForUser = context.createContextAsUser(newUser, 0)
            serviceListing = serviceListingBuilder(contextForUser)
            serviceListing = serviceListingBuilder(contextForUser)
+28 −1
Original line number Original line Diff line number Diff line
@@ -177,4 +177,31 @@ class ControlsListingControllerImplTest : SysuiTestCase() {
        inOrder.verify(mockSL).setListening(true)
        inOrder.verify(mockSL).setListening(true)
        inOrder.verify(mockSL).reload()
        inOrder.verify(mockSL).reload()
    }
    }

    @Test
    fun testChangeUserResetsExistingCallbackServices() {
        val list = listOf(serviceInfo)
        controller.addCallback(mockCallback)

        @Suppress("unchecked_cast")
        val captor: ArgumentCaptor<List<ControlsServiceInfo>> =
                ArgumentCaptor.forClass(List::class.java)
                        as ArgumentCaptor<List<ControlsServiceInfo>>
        executor.runAllReady()
        reset(mockCallback)

        serviceListingCallbackCaptor.value.onServicesReloaded(list)

        executor.runAllReady()
        verify(mockCallback).onServicesUpdated(capture(captor))
        assertEquals(1, captor.value.size)

        reset(mockCallback)
        controller.changeUser(UserHandle.of(otherUser))
        executor.runAllReady()
        assertEquals(otherUser, controller.currentUserId)

        verify(mockCallback).onServicesUpdated(capture(captor))
        assertEquals(0, captor.value.size)
    }
}
}