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
This commit is contained in:
Matt Pietal
2020-05-08 11:09:51 -04:00
parent f3b5ba7a98
commit 3cdfdeb15e
2 changed files with 36 additions and 3 deletions

View File

@@ -106,9 +106,15 @@ class ControlsListingControllerImpl @VisibleForTesting constructor(
override fun changeUser(newUser: UserHandle) {
backgroundExecutor.execute {
callbacks.clear()
availableServices = emptyList()
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
val contextForUser = context.createContextAsUser(newUser, 0)
serviceListing = serviceListingBuilder(contextForUser)

View File

@@ -177,4 +177,31 @@ class ControlsListingControllerImplTest : SysuiTestCase() {
inOrder.verify(mockSL).setListening(true)
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)
}
}