Loading packages/SystemUI/multivalentTests/src/com/android/systemui/keyevent/domain/interactor/KeyEventInteractorTest.kt +13 −0 Original line number Diff line number Diff line Loading @@ -57,4 +57,17 @@ class KeyEventInteractorTest : SysuiTestCase() { repository.setPowerButtonDown(true) assertThat(isPowerDown).isTrue() } @Test fun testPowerButtonBeingLongPressedInteractor() = runTest { val isPowerButtonLongPressed by collectLastValue( underTest.isPowerButtonLongPressed) repository.setPowerButtonBeingLongPressed(false) assertThat(isPowerButtonLongPressed).isFalse() repository.setPowerButtonBeingLongPressed(true) assertThat(isPowerButtonLongPressed).isTrue() } } packages/SystemUI/multivalentTests/src/com/android/systemui/keyguard/data/repository/KeyEventRepositoryTest.kt +61 −1 Original line number Diff line number Diff line Loading @@ -25,6 +25,7 @@ import com.android.systemui.coroutines.collectLastValue import com.android.systemui.keyevent.data.repository.KeyEventRepositoryImpl import com.android.systemui.statusbar.CommandQueue import com.google.common.truth.Truth.assertThat import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.runCurrent import kotlinx.coroutines.test.runTest Loading @@ -34,10 +35,10 @@ import org.junit.runner.RunWith import org.mockito.ArgumentCaptor import org.mockito.Captor import org.mockito.Mock import org.mockito.Mockito.times import org.mockito.Mockito.verify import org.mockito.MockitoAnnotations @OptIn(ExperimentalCoroutinesApi::class) @SmallTest @RunWith(AndroidJUnit4::class) class KeyEventRepositoryTest : SysuiTestCase() { Loading @@ -61,6 +62,15 @@ class KeyEventRepositoryTest : SysuiTestCase() { assertThat(isPowerButtonDown).isFalse() } @Test fun isPowerButtonBeingLongPressed_initialValueFalse() = testScope.runTest { val isPowerButtonLongPressed by collectLastValue( underTest.isPowerButtonLongPressed) runCurrent() assertThat(isPowerButtonLongPressed).isFalse() } @Test fun isPowerButtonDown_onChange() = testScope.runTest { Loading @@ -77,4 +87,54 @@ class KeyEventRepositoryTest : SysuiTestCase() { ) assertThat(isPowerButtonDown).isFalse() } @Test fun isPowerButtonBeingLongPressed_onPowerButtonDown() = testScope.runTest { val isPowerButtonLongPressed by collectLastValue( underTest.isPowerButtonLongPressed) runCurrent() verify(commandQueue).addCallback(commandQueueCallbacks.capture()) val keyEvent = KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_POWER) commandQueueCallbacks.value.handleSystemKey(keyEvent) assertThat(isPowerButtonLongPressed).isFalse() } @Test fun isPowerButtonBeingLongPressed_onPowerButtonUp() = testScope.runTest { val isPowerButtonLongPressed by collectLastValue( underTest.isPowerButtonLongPressed) runCurrent() verify(commandQueue).addCallback(commandQueueCallbacks.capture()) val keyEvent = KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_POWER) commandQueueCallbacks.value.handleSystemKey(keyEvent) assertThat(isPowerButtonLongPressed).isFalse() } @Test fun isPowerButtonBeingLongPressed_onPowerButtonDown_longPressFlagSet() = testScope.runTest { val isPowerButtonBeingLongPressed by collectLastValue( underTest.isPowerButtonLongPressed) runCurrent() verify(commandQueue).addCallback(commandQueueCallbacks.capture()) val keyEvent = KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_POWER) keyEvent.setFlags(KeyEvent.FLAG_LONG_PRESS) commandQueueCallbacks.value.handleSystemKey(keyEvent) assertThat(isPowerButtonBeingLongPressed).isTrue() } } packages/SystemUI/src/com/android/systemui/keyevent/data/repository/KeyEventRepository.kt +19 −1 Original line number Diff line number Diff line Loading @@ -18,7 +18,7 @@ package com.android.systemui.keyevent.data.repository import android.view.KeyEvent import com.android.systemui.common.coroutine.ChannelExt.trySendWithFailureLogging import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow import com.android.systemui.dagger.SysUISingleton import com.android.systemui.statusbar.CommandQueue import javax.inject.Inject Loading @@ -29,6 +29,9 @@ import kotlinx.coroutines.flow.Flow interface KeyEventRepository { /** Observable for whether the power button key is pressed/down or not. */ val isPowerButtonDown: Flow<Boolean> /** Observable for when the power button is being pressed but till the duration of long press */ val isPowerButtonLongPressed: Flow<Boolean> } @SysUISingleton Loading @@ -51,6 +54,21 @@ constructor( awaitClose { commandQueue.removeCallback(callback) } } override val isPowerButtonLongPressed: Flow<Boolean> = conflatedCallbackFlow { val callback = object : CommandQueue.Callbacks { override fun handleSystemKey(event: KeyEvent) { if (event.keyCode == KeyEvent.KEYCODE_POWER) { trySendWithFailureLogging(event.action == KeyEvent.ACTION_DOWN && event.isLongPress, TAG, "updated isPowerButtonLongPressed") } } } trySendWithFailureLogging(false, TAG, "init isPowerButtonLongPressed") commandQueue.addCallback(callback) awaitClose { commandQueue.removeCallback(callback) } } companion object { private const val TAG = "KeyEventRepositoryImpl" } Loading packages/SystemUI/src/com/android/systemui/keyevent/domain/interactor/KeyEventInteractor.kt +1 −0 Original line number Diff line number Diff line Loading @@ -32,4 +32,5 @@ constructor( repository: KeyEventRepository, ) { val isPowerButtonDown = repository.isPowerButtonDown val isPowerButtonLongPressed = repository.isPowerButtonLongPressed } packages/SystemUI/tests/utils/src/com/android/systemui/keyevent/data/repository/FakeKeyEventRepository.kt +7 −0 Original line number Diff line number Diff line Loading @@ -27,9 +27,16 @@ class FakeKeyEventRepository @Inject constructor() : KeyEventRepository { private val _isPowerButtonDown = MutableStateFlow(false) override val isPowerButtonDown: Flow<Boolean> = _isPowerButtonDown.asStateFlow() private val _isPowerButtonLongPressed = MutableStateFlow(false) override val isPowerButtonLongPressed = _isPowerButtonLongPressed.asStateFlow() fun setPowerButtonDown(isDown: Boolean) { _isPowerButtonDown.value = isDown } fun setPowerButtonBeingLongPressed(isLongPressed: Boolean) { _isPowerButtonLongPressed.value = isLongPressed } } @Module Loading Loading
packages/SystemUI/multivalentTests/src/com/android/systemui/keyevent/domain/interactor/KeyEventInteractorTest.kt +13 −0 Original line number Diff line number Diff line Loading @@ -57,4 +57,17 @@ class KeyEventInteractorTest : SysuiTestCase() { repository.setPowerButtonDown(true) assertThat(isPowerDown).isTrue() } @Test fun testPowerButtonBeingLongPressedInteractor() = runTest { val isPowerButtonLongPressed by collectLastValue( underTest.isPowerButtonLongPressed) repository.setPowerButtonBeingLongPressed(false) assertThat(isPowerButtonLongPressed).isFalse() repository.setPowerButtonBeingLongPressed(true) assertThat(isPowerButtonLongPressed).isTrue() } }
packages/SystemUI/multivalentTests/src/com/android/systemui/keyguard/data/repository/KeyEventRepositoryTest.kt +61 −1 Original line number Diff line number Diff line Loading @@ -25,6 +25,7 @@ import com.android.systemui.coroutines.collectLastValue import com.android.systemui.keyevent.data.repository.KeyEventRepositoryImpl import com.android.systemui.statusbar.CommandQueue import com.google.common.truth.Truth.assertThat import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.runCurrent import kotlinx.coroutines.test.runTest Loading @@ -34,10 +35,10 @@ import org.junit.runner.RunWith import org.mockito.ArgumentCaptor import org.mockito.Captor import org.mockito.Mock import org.mockito.Mockito.times import org.mockito.Mockito.verify import org.mockito.MockitoAnnotations @OptIn(ExperimentalCoroutinesApi::class) @SmallTest @RunWith(AndroidJUnit4::class) class KeyEventRepositoryTest : SysuiTestCase() { Loading @@ -61,6 +62,15 @@ class KeyEventRepositoryTest : SysuiTestCase() { assertThat(isPowerButtonDown).isFalse() } @Test fun isPowerButtonBeingLongPressed_initialValueFalse() = testScope.runTest { val isPowerButtonLongPressed by collectLastValue( underTest.isPowerButtonLongPressed) runCurrent() assertThat(isPowerButtonLongPressed).isFalse() } @Test fun isPowerButtonDown_onChange() = testScope.runTest { Loading @@ -77,4 +87,54 @@ class KeyEventRepositoryTest : SysuiTestCase() { ) assertThat(isPowerButtonDown).isFalse() } @Test fun isPowerButtonBeingLongPressed_onPowerButtonDown() = testScope.runTest { val isPowerButtonLongPressed by collectLastValue( underTest.isPowerButtonLongPressed) runCurrent() verify(commandQueue).addCallback(commandQueueCallbacks.capture()) val keyEvent = KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_POWER) commandQueueCallbacks.value.handleSystemKey(keyEvent) assertThat(isPowerButtonLongPressed).isFalse() } @Test fun isPowerButtonBeingLongPressed_onPowerButtonUp() = testScope.runTest { val isPowerButtonLongPressed by collectLastValue( underTest.isPowerButtonLongPressed) runCurrent() verify(commandQueue).addCallback(commandQueueCallbacks.capture()) val keyEvent = KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_POWER) commandQueueCallbacks.value.handleSystemKey(keyEvent) assertThat(isPowerButtonLongPressed).isFalse() } @Test fun isPowerButtonBeingLongPressed_onPowerButtonDown_longPressFlagSet() = testScope.runTest { val isPowerButtonBeingLongPressed by collectLastValue( underTest.isPowerButtonLongPressed) runCurrent() verify(commandQueue).addCallback(commandQueueCallbacks.capture()) val keyEvent = KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_POWER) keyEvent.setFlags(KeyEvent.FLAG_LONG_PRESS) commandQueueCallbacks.value.handleSystemKey(keyEvent) assertThat(isPowerButtonBeingLongPressed).isTrue() } }
packages/SystemUI/src/com/android/systemui/keyevent/data/repository/KeyEventRepository.kt +19 −1 Original line number Diff line number Diff line Loading @@ -18,7 +18,7 @@ package com.android.systemui.keyevent.data.repository import android.view.KeyEvent import com.android.systemui.common.coroutine.ChannelExt.trySendWithFailureLogging import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow import com.android.systemui.dagger.SysUISingleton import com.android.systemui.statusbar.CommandQueue import javax.inject.Inject Loading @@ -29,6 +29,9 @@ import kotlinx.coroutines.flow.Flow interface KeyEventRepository { /** Observable for whether the power button key is pressed/down or not. */ val isPowerButtonDown: Flow<Boolean> /** Observable for when the power button is being pressed but till the duration of long press */ val isPowerButtonLongPressed: Flow<Boolean> } @SysUISingleton Loading @@ -51,6 +54,21 @@ constructor( awaitClose { commandQueue.removeCallback(callback) } } override val isPowerButtonLongPressed: Flow<Boolean> = conflatedCallbackFlow { val callback = object : CommandQueue.Callbacks { override fun handleSystemKey(event: KeyEvent) { if (event.keyCode == KeyEvent.KEYCODE_POWER) { trySendWithFailureLogging(event.action == KeyEvent.ACTION_DOWN && event.isLongPress, TAG, "updated isPowerButtonLongPressed") } } } trySendWithFailureLogging(false, TAG, "init isPowerButtonLongPressed") commandQueue.addCallback(callback) awaitClose { commandQueue.removeCallback(callback) } } companion object { private const val TAG = "KeyEventRepositoryImpl" } Loading
packages/SystemUI/src/com/android/systemui/keyevent/domain/interactor/KeyEventInteractor.kt +1 −0 Original line number Diff line number Diff line Loading @@ -32,4 +32,5 @@ constructor( repository: KeyEventRepository, ) { val isPowerButtonDown = repository.isPowerButtonDown val isPowerButtonLongPressed = repository.isPowerButtonLongPressed }
packages/SystemUI/tests/utils/src/com/android/systemui/keyevent/data/repository/FakeKeyEventRepository.kt +7 −0 Original line number Diff line number Diff line Loading @@ -27,9 +27,16 @@ class FakeKeyEventRepository @Inject constructor() : KeyEventRepository { private val _isPowerButtonDown = MutableStateFlow(false) override val isPowerButtonDown: Flow<Boolean> = _isPowerButtonDown.asStateFlow() private val _isPowerButtonLongPressed = MutableStateFlow(false) override val isPowerButtonLongPressed = _isPowerButtonLongPressed.asStateFlow() fun setPowerButtonDown(isDown: Boolean) { _isPowerButtonDown.value = isDown } fun setPowerButtonBeingLongPressed(isLongPressed: Boolean) { _isPowerButtonLongPressed.value = isLongPressed } } @Module Loading