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

Commit 1de53069 authored by Automerger Merge Worker's avatar Automerger Merge Worker Committed by Android (Google) Code Review
Browse files

Merge "Merge "Make ListingController always listening" into rvc-dev am:...

Merge "Merge "Make ListingController always listening" into rvc-dev am: 8cbfa875 am: 94417e0f" into rvc-d1-dev-plus-aosp
parents 8556210e 95208eed
Loading
Loading
Loading
Loading
+5 −10
Original line number Original line Diff line number Diff line
@@ -88,6 +88,8 @@ class ControlsListingControllerImpl @VisibleForTesting constructor(


    init {
    init {
        serviceListing.addCallback(serviceListingCallback)
        serviceListing.addCallback(serviceListingCallback)
        serviceListing.setListening(true)
        serviceListing.reload()
    }
    }


    override fun changeUser(newUser: UserHandle) {
    override fun changeUser(newUser: UserHandle) {
@@ -95,11 +97,12 @@ class ControlsListingControllerImpl @VisibleForTesting constructor(
            callbacks.clear()
            callbacks.clear()
            availableServices = emptyList()
            availableServices = emptyList()
            serviceListing.setListening(false)
            serviceListing.setListening(false)
            serviceListing.removeCallback(serviceListingCallback)
            currentUserId = newUser.identifier
            currentUserId = newUser.identifier
            val contextForUser = context.createContextAsUser(newUser, 0)
            val contextForUser = context.createContextAsUser(newUser, 0)
            serviceListing = serviceListingBuilder(contextForUser)
            serviceListing = serviceListingBuilder(contextForUser)
            serviceListing.addCallback(serviceListingCallback)
            serviceListing.addCallback(serviceListingCallback)
            serviceListing.setListening(true)
            serviceListing.reload()
        }
        }
    }
    }


@@ -118,14 +121,9 @@ class ControlsListingControllerImpl @VisibleForTesting constructor(
        backgroundExecutor.execute {
        backgroundExecutor.execute {
            Log.d(TAG, "Subscribing callback")
            Log.d(TAG, "Subscribing callback")
            callbacks.add(listener)
            callbacks.add(listener)
            if (callbacks.size == 1) {
                serviceListing.setListening(true)
                serviceListing.reload()
            } else {
            listener.onServicesUpdated(getCurrentServices())
            listener.onServicesUpdated(getCurrentServices())
        }
        }
    }
    }
    }


    /**
    /**
     * Removes a callback from this controller.
     * Removes a callback from this controller.
@@ -136,9 +134,6 @@ class ControlsListingControllerImpl @VisibleForTesting constructor(
        backgroundExecutor.execute {
        backgroundExecutor.execute {
            Log.d(TAG, "Unsubscribing callback")
            Log.d(TAG, "Unsubscribing callback")
            callbacks.remove(listener)
            callbacks.remove(listener)
            if (callbacks.size == 0) {
                serviceListing.setListening(false)
            }
        }
        }
    }
    }


+11 −61
Original line number Original line Diff line number Diff line
@@ -30,7 +30,6 @@ import com.android.systemui.util.concurrency.FakeExecutor
import com.android.systemui.util.time.FakeSystemClock
import com.android.systemui.util.time.FakeSystemClock
import org.junit.After
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Before
import org.junit.Test
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runner.RunWith
@@ -38,6 +37,7 @@ import org.mockito.ArgumentCaptor
import org.mockito.Mock
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.Mockito
import org.mockito.Mockito.`when`
import org.mockito.Mockito.`when`
import org.mockito.Mockito.inOrder
import org.mockito.Mockito.never
import org.mockito.Mockito.never
import org.mockito.Mockito.reset
import org.mockito.Mockito.reset
import org.mockito.Mockito.verify
import org.mockito.Mockito.verify
@@ -98,74 +98,18 @@ class ControlsListingControllerImplTest : SysuiTestCase() {
    }
    }


    @Test
    @Test
    fun testStartsOnUser() {
    fun testInitialStateListening() {
        assertEquals(user, controller.currentUserId)
    }

    @Test
    fun testNoServices_notListening() {
        assertTrue(controller.getCurrentServices().isEmpty())
    }

    @Test
    fun testStartListening_onFirstCallback() {
        controller.addCallback(mockCallback)
        executor.runAllReady()

        verify(mockSL).setListening(true)
    }

    @Test
    fun testStartListening_onlyOnce() {
        controller.addCallback(mockCallback)
        controller.addCallback(mockCallbackOther)

        executor.runAllReady()

        verify(mockSL).setListening(true)
        verify(mockSL).setListening(true)
        verify(mockSL).reload()
    }
    }


    @Test
    @Test
    fun testStopListening_callbackRemoved() {
    fun testStartsOnUser() {
        controller.addCallback(mockCallback)
        assertEquals(user, controller.currentUserId)

        executor.runAllReady()

        controller.removeCallback(mockCallback)

        executor.runAllReady()

        verify(mockSL).setListening(false)
    }

    @Test
    fun testStopListening_notWhileRemainingCallbacks() {
        controller.addCallback(mockCallback)
        controller.addCallback(mockCallbackOther)

        executor.runAllReady()

        controller.removeCallback(mockCallback)

        executor.runAllReady()

        verify(mockSL, never()).setListening(false)
    }

    @Test
    fun testReloadOnFirstCallbackAdded() {
        controller.addCallback(mockCallback)
        executor.runAllReady()

        verify(mockSL).reload()
    }
    }


    @Test
    @Test
    fun testCallbackCalledWhenAdded() {
    fun testCallbackCalledWhenAdded() {
        `when`(mockSL.reload()).then {
            serviceListingCallbackCaptor.value.onServicesReloaded(emptyList())
        }

        controller.addCallback(mockCallback)
        controller.addCallback(mockCallback)
        executor.runAllReady()
        executor.runAllReady()
        verify(mockCallback).onServicesUpdated(any())
        verify(mockCallback).onServicesUpdated(any())
@@ -209,5 +153,11 @@ class ControlsListingControllerImplTest : SysuiTestCase() {
        controller.changeUser(UserHandle.of(otherUser))
        controller.changeUser(UserHandle.of(otherUser))
        executor.runAllReady()
        executor.runAllReady()
        assertEquals(otherUser, controller.currentUserId)
        assertEquals(otherUser, controller.currentUserId)

        val inOrder = inOrder(mockSL)
        inOrder.verify(mockSL).setListening(false)
        inOrder.verify(mockSL).addCallback(any()) // We add a callback because we replaced the SL
        inOrder.verify(mockSL).setListening(true)
        inOrder.verify(mockSL).reload()
    }
    }
}
}
 No newline at end of file