Merge "Adding tests for KeyboardBacklightListener part of KeyboardRepository" into udc-dev am: 5b657848f8

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/22744821

Change-Id: Ic312669d311a4d6cc89524c8052e6c71ff7dd1fc
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Michał Brzeziński
2023-04-26 11:33:19 +00:00
committed by Automerger Merge Worker

View File

@@ -18,9 +18,12 @@
package com.android.systemui.keyboard.data.repository package com.android.systemui.keyboard.data.repository
import android.hardware.input.InputManager import android.hardware.input.InputManager
import android.hardware.input.InputManager.KeyboardBacklightListener
import android.hardware.input.KeyboardBacklightState
import android.view.InputDevice import android.view.InputDevice
import androidx.test.filters.SmallTest import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.FlowValue
import com.android.systemui.coroutines.collectLastValue import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.util.mockito.any import com.android.systemui.util.mockito.any
import com.android.systemui.util.mockito.mock import com.android.systemui.util.mockito.mock
@@ -29,9 +32,11 @@ import com.android.systemui.util.mockito.whenever
import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
@@ -50,6 +55,7 @@ class KeyboardRepositoryTest : SysuiTestCase() {
@Captor @Captor
private lateinit var deviceListenerCaptor: ArgumentCaptor<InputManager.InputDeviceListener> private lateinit var deviceListenerCaptor: ArgumentCaptor<InputManager.InputDeviceListener>
@Captor private lateinit var backlightListenerCaptor: ArgumentCaptor<KeyboardBacklightListener>
@Mock private lateinit var inputManager: InputManager @Mock private lateinit var inputManager: InputManager
private lateinit var underTest: KeyboardRepository private lateinit var underTest: KeyboardRepository
@@ -174,7 +180,71 @@ class KeyboardRepositoryTest : SysuiTestCase() {
@Test @Test
fun passesKeyboardBacklightValues_fromBacklightListener() { fun passesKeyboardBacklightValues_fromBacklightListener() {
// TODO(b/268645734): implement when implementing backlight listener testScope.runTest {
// we want to capture backlight listener but this can only be done after Flow is
// subscribed to and listener is actually registered in inputManager
val backlight by collectLastValueImmediately(underTest.backlight)
verify(inputManager)
.registerKeyboardBacklightListener(any(), backlightListenerCaptor.capture())
backlightListenerCaptor.value.onBacklightChanged(current = 1, max = 5)
assertThat(backlight?.level).isEqualTo(1)
assertThat(backlight?.maxLevel).isEqualTo(5)
}
}
private fun <T> TestScope.collectLastValueImmediately(flow: Flow<T>): FlowValue<T?> {
val lastValue = collectLastValue(flow)
// runCurrent() makes us wait for collect that happens in collectLastValue and ensures
// Flow is initialized
runCurrent()
return lastValue
}
@Test
fun keyboardBacklightValuesNotPassed_fromBacklightListener_whenNotTriggeredByKeyPress() {
testScope.runTest() {
val backlight by collectLastValueImmediately(underTest.backlight)
verify(inputManager)
.registerKeyboardBacklightListener(any(), backlightListenerCaptor.capture())
backlightListenerCaptor.value.onBacklightChanged(
current = 1,
max = 5,
triggeredByKeyPress = false
)
assertThat(backlight).isNull()
}
}
@Test
fun passesKeyboardBacklightValues_fromBacklightListener_whenTriggeredByKeyPress() {
testScope.runTest() {
val backlight by collectLastValueImmediately(underTest.backlight)
verify(inputManager)
.registerKeyboardBacklightListener(any(), backlightListenerCaptor.capture())
backlightListenerCaptor.value.onBacklightChanged(
current = 1,
max = 5,
triggeredByKeyPress = true
)
assertThat(backlight).isNotNull()
}
}
private fun KeyboardBacklightListener.onBacklightChanged(
current: Int,
max: Int,
triggeredByKeyPress: Boolean = true
) {
onKeyboardBacklightChanged(
/* deviceId= */ 0,
TestBacklightState(current, max),
triggeredByKeyPress
)
} }
private companion object { private companion object {
@@ -199,4 +269,12 @@ class KeyboardRepositoryTest : SysuiTestCase() {
whenever(it.isFullKeyboard).thenReturn(fullKeyboard) whenever(it.isFullKeyboard).thenReturn(fullKeyboard)
} }
} }
private class TestBacklightState(
private val brightnessLevel: Int,
private val maxBrightnessLevel: Int
) : KeyboardBacklightState() {
override fun getBrightnessLevel() = brightnessLevel
override fun getMaxBrightnessLevel() = maxBrightnessLevel
}
} }