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

Commit d570a8a4 authored by Josh's avatar Josh
Browse files

Handled possible NPE in DefaultShortcutCategoriesRepository

API data could potentially send null keyboardShortcutGroup label to
Default ShortcutCategoriesRepository. When this happens we use an empty
string as the subcategory label.

Test: DefaultShortcutCategoriesRepositoryTest
Flag: NONE Trivial
Bug: 415762126
Change-Id: Iad605d6bfd5c9f92284bfee380ec59e3097bd797
parent ae1dc9ac
Loading
Loading
Loading
Loading
+21 −4
Original line number Diff line number Diff line
@@ -57,12 +57,11 @@ import com.android.systemui.keyboard.shortcut.shortcutHelperInputShortcutsSource
import com.android.systemui.keyboard.shortcut.shortcutHelperMultiTaskingShortcutsSource
import com.android.systemui.keyboard.shortcut.shortcutHelperSystemShortcutsSource
import com.android.systemui.keyboard.shortcut.shortcutHelperTestHelper
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.kosmos.testScope
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.res.R
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
@@ -80,8 +79,7 @@ class DefaultShortcutCategoriesRepositoryTest : SysuiTestCase() {
    private val fakeAppCategoriesSource = FakeKeyboardShortcutGroupsSource()

    private val kosmos =
        testKosmos().also {
            it.testDispatcher = UnconfinedTestDispatcher()
        testKosmos().useUnconfinedTestDispatcher().also {
            it.shortcutHelperSystemShortcutsSource = fakeSystemSource
            it.shortcutHelperMultiTaskingShortcutsSource = fakeMultiTaskingSource
            it.shortcutHelperAppCategoriesShortcutsSource = fakeAppCategoriesSource
@@ -122,6 +120,25 @@ class DefaultShortcutCategoriesRepositoryTest : SysuiTestCase() {
            assertThat(systemCategory).isEqualTo(expectedCategory)
        }

    @Test
    fun subcategoryLabel_isEmpty_whenKeyboardShortcutGroupLabelIsNull() =
        testScope.runTest {
            fakeSystemSource.setGroups(
                KeyboardShortcutGroup(
                    /* label= */ null,
                    /* items= */ listOf(simpleShortcutInfo(KEYCODE_1)),
                )
            )

            helper.toggle(deviceId = 123)
            val categories by collectLastValue(repo.categories)

            val systemCategory = categories?.firstOrNull { it.type == ShortcutCategoryType.System }

            // Also Tests will fail if NPE is thrown when KeyboardShortcutGroup.label is null
            assertThat(systemCategory?.subCategories?.firstOrNull()?.label).isEmpty()
        }

    @Test
    fun categories_keyCodeAndModifierHaveSameCode_codesAreMappedCorrectly() =
        testScope.runTest {
+1 −1
Original line number Diff line number Diff line
@@ -119,7 +119,7 @@ constructor(
    ): List<InternalKeyboardShortcutGroup> {
        return keyboardShortcutGroups.map { group ->
            InternalKeyboardShortcutGroup(
                label = group.label.toString(),
                label = group.label?.toString() ?: "",
                items = group.items.map { toInternalKeyboardShortcutInfo(it) },
                packageName = group.packageName?.toString(),
            )