[automerge] Catch if an invalid constant is sent 2p: edd059b302

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

Bug: 227332691
Change-Id: I61d342218cbf71a9d658e33b31f6ed5a716cf6e5
Merged-In: I4c9fc065c7a4c96bf51a2d14a1eb1c7abe543a9a
This commit is contained in:
Presubmit Automerger Backend
2022-04-11 16:04:51 +00:00
2 changed files with 26 additions and 6 deletions

View File

@@ -17,6 +17,7 @@
package com.android.systemui.media.taptotransfer.receiver
import android.app.StatusBarManager
import android.util.Log
import com.android.internal.logging.UiEventLogger
/**
@@ -25,15 +26,15 @@ import com.android.internal.logging.UiEventLogger
*/
enum class ChipStateReceiver(
@StatusBarManager.MediaTransferSenderState val stateInt: Int,
val uiEvent: UiEventLogger.UiEventEnum,
val uiEvent: UiEventLogger.UiEventEnum
) {
CLOSE_TO_SENDER(
StatusBarManager.MEDIA_TRANSFER_RECEIVER_STATE_CLOSE_TO_SENDER,
MediaTttReceiverUiEvents.MEDIA_TTT_RECEIVER_CLOSE_TO_SENDER,
MediaTttReceiverUiEvents.MEDIA_TTT_RECEIVER_CLOSE_TO_SENDER
),
FAR_FROM_SENDER(
StatusBarManager.MEDIA_TRANSFER_RECEIVER_STATE_FAR_FROM_SENDER,
MediaTttReceiverUiEvents.MEDIA_TTT_RECEIVER_FAR_FROM_SENDER,
MediaTttReceiverUiEvents.MEDIA_TTT_RECEIVER_FAR_FROM_SENDER
);
companion object {
@@ -43,8 +44,13 @@ enum class ChipStateReceiver(
*/
fun getReceiverStateFromId(
@StatusBarManager.MediaTransferReceiverState displayState: Int
) : ChipStateReceiver = values().first { it.stateInt == displayState }
): ChipStateReceiver? =
try {
values().first { it.stateInt == displayState }
} catch (e: NoSuchElementException) {
Log.e(TAG, "Could not find requested state $displayState", e)
null
}
/**
* Returns the state int from [StatusBarManager] associated with the given sender state
@@ -56,3 +62,5 @@ enum class ChipStateReceiver(
fun getReceiverStateIdFromName(name: String): Int = valueOf(name).stateInt
}
}
private const val TAG = "ChipStateReceiver"

View File

@@ -102,7 +102,7 @@ class MediaTttChipControllerReceiverTest : SysuiTestCase() {
TapGestureDetector(context),
powerManager,
Handler.getMain(),
receiverUiEventLogger,
receiverUiEventLogger
)
val callbackCaptor = ArgumentCaptor.forClass(CommandQueue.Callbacks::class.java)
@@ -206,6 +206,18 @@ class MediaTttChipControllerReceiverTest : SysuiTestCase() {
assertThat(chipView.getAppIconView().measuredHeight).isEqualTo(expectedSize)
}
@Test
fun commandQueueCallback_invalidStateParam_noChipShown() {
commandQueueCallback.updateMediaTapToTransferReceiverDisplay(
StatusBarManager.MEDIA_TRANSFER_SENDER_STATE_TRANSFER_TO_THIS_DEVICE_SUCCEEDED,
routeInfo,
null,
APP_NAME
)
verify(windowManager, never()).addView(any(), any())
}
private fun getChipView(): ViewGroup {
val viewCaptor = ArgumentCaptor.forClass(View::class.java)
verify(windowManager).addView(viewCaptor.capture(), any())