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

Commit 1a478625 authored by Fabian Kozynski's avatar Fabian Kozynski
Browse files

Prevent NPE in ControlsListingControllerImpl

Move initialization of callbacks list to before the ServiceListing is
reloaded. That way, it will always be initialized by the time the
listener is called.

Fixes: 153448765
Test: atest ControlsListingControllerImpl
Change-Id: I67ba7f3d4bd74f38d0e6f69857135864dcf4a573
parent 5ef61d2f
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -66,6 +66,8 @@ class ControlsListingControllerImpl @VisibleForTesting constructor(
    )

    private var serviceListing = serviceListingBuilder(context)
    // All operations in background thread
    private val callbacks = mutableSetOf<ControlsListingController.ControlsListingCallback>()

    companion object {
        private const val TAG = "ControlsListingControllerImpl"
@@ -116,9 +118,6 @@ class ControlsListingControllerImpl @VisibleForTesting constructor(
        }
    }

    // All operations in background thread
    private val callbacks = mutableSetOf<ControlsListingController.ControlsListingCallback>()

    /**
     * Adds a callback to this controller.
     *
+17 −0
Original line number Diff line number Diff line
@@ -38,10 +38,12 @@ import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.Mockito.`when`
import org.mockito.Mockito.inOrder
import org.mockito.Mockito.mock
import org.mockito.Mockito.never
import org.mockito.Mockito.reset
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations
import java.util.concurrent.Executor

@SmallTest
@RunWith(AndroidTestingRunner::class)
@@ -103,6 +105,21 @@ class ControlsListingControllerImplTest : SysuiTestCase() {
        verify(mockSL).reload()
    }

    @Test
    fun testImmediateListingReload_doesNotCrash() {
        val exec = Executor { it.run() }
        val mockServiceListing = mock(ServiceListing::class.java)
        var callback: ServiceListing.Callback? = null
        `when`(mockServiceListing.addCallback(any<ServiceListing.Callback>())).then {
            callback = it.getArgument(0)
            Unit
        }
        `when`(mockServiceListing.reload()).then {
            callback?.onServicesReloaded(listOf(serviceInfo))
        }
        ControlsListingControllerImpl(mContext, exec, { mockServiceListing })
    }

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