From 9efb22bd0c5c9a76d9429b4b825a80cc1dec4fb4 Mon Sep 17 00:00:00 2001 From: Helium314 Date: Mon, 17 Jun 2024 16:05:22 +0200 Subject: [PATCH] fix #881 and improve related tests --- .../keyboard_parser/floris/TextKeyData.kt | 7 +-- .../helium314/keyboard/KeyboardParserTest.kt | 52 ++++++++++--------- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/floris/TextKeyData.kt b/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/floris/TextKeyData.kt index d34d589d..5c01ed8f 100644 --- a/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/floris/TextKeyData.kt +++ b/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/floris/TextKeyData.kt @@ -298,9 +298,10 @@ sealed interface KeyData : AbstractKeyData { fun getPopupLabel(params: KeyboardParams): String { val newLabel = processLabel(params) if (code == KeyCode.UNSPECIFIED) { - return if (newLabel == label) label - else if (newLabel.endsWith("|")) "${newLabel}!code/${processCode()}" // for toolbar keys - else "${newLabel}|!code/${processCode()}" + if (newLabel == label) return label + val newCode = processCode() + if (newLabel.endsWith("|")) return "${newLabel}!code/$newCode" // for toolbar keys + return if (newCode == code) newLabel else "${newLabel}|!code/$newCode" } if (code >= 32) return "${newLabel}|${StringUtils.newSingleCodePointString(code)}" diff --git a/app/src/test/java/helium314/keyboard/KeyboardParserTest.kt b/app/src/test/java/helium314/keyboard/KeyboardParserTest.kt index 9af66006..4929237a 100644 --- a/app/src/test/java/helium314/keyboard/KeyboardParserTest.kt +++ b/app/src/test/java/helium314/keyboard/KeyboardParserTest.kt @@ -127,30 +127,30 @@ f""", // no newline at the end params.mId = KeyboardLayoutSet.getFakeKeyboardId(KeyboardId.ELEMENT_ALPHABET) params.mPopupKeyTypes.add(POPUP_KEYS_LAYOUT) addLocaleKeyTextsToParams(latinIME, params, POPUP_KEYS_NORMAL) - data class Expected(val label: String?, val text: String?, val code: Int, val popups: List? = null) + data class Expected(val label: String?, val icon: String?, val text: String?, val code: Int, val popups: List>? = null) val expected = listOf( - Expected("a", null, 'a'.code, null), - Expected("a", null, 'a'.code, null), - Expected("a", null, 'b'.code, listOf("b")), // todo: should also check whether code is "a" - Expected("$", null, '$'.code, listOf("£", "€", "¢", "¥", "₱")), - Expected("$", null, '¥'.code, listOf("£", "€", "¢", "¥", "₱")), - Expected("i", null, 105, null), - Expected("্র", "্র", KeyCode.MULTIPLE_CODE_POINTS, null), - Expected("x", "্র", KeyCode.MULTIPLE_CODE_POINTS, null), - Expected(";", null, ';'.code, listOf(":")), - Expected(".", null, '.'.code, listOf(">")), - Expected("'", null, '\''.code, listOf("!", "\"")), - Expected("9", null, '9'.code, null), // todo (later): also should have different background or whatever is related to type - Expected(null, null, -7, null), // todo: expect an icon - Expected("?123", "?123", -202, null), - Expected(null, null, ' '.code, null), - Expected("(", null, '('.code, listOf("<", "[", "{")), - Expected("$", null, '$'.code, listOf("£", "₱", "€", "¢", "¥", "¥")), - Expected("a", null, ' '.code, null), - Expected("a", null, ' '.code, null), - Expected(null, null, KeyCode.CLIPBOARD, null), // todo: expect an icon - Expected(null, null, KeyCode.MULTIPLE_CODE_POINTS, null), // todo: this works here, but crashes on phone - Expected("p", null, 'p'.code, null), + Expected("a", null, null, 'a'.code, null), + Expected("a", null, null, 'a'.code, null), + Expected("a", null, null, 'b'.code, listOf("b" to 'a'.code)), + Expected("$", null, null, '$'.code, listOf("£", "€", "¢", "¥", "₱").map { it to it.first().code }), + Expected("$", null, null, '¥'.code, listOf("£", "€", "¢", "¥", "₱").map { it to it.first().code }), + Expected("i", null, null, 105, null), + Expected("্র", null, "্র", KeyCode.MULTIPLE_CODE_POINTS, null), + Expected("x", null, "্র", KeyCode.MULTIPLE_CODE_POINTS, null), + Expected(";", null, null, ';'.code, listOf(":").map { it to it.first().code }), + Expected(".", null, null, '.'.code, listOf(">").map { it to it.first().code }), + Expected("'", null, null, '\''.code, listOf("!", "\"").map { it to it.first().code }), + Expected("9", null, null, '9'.code, null), // todo (later): also should have different background or whatever is related to type + Expected(null, "delete_key", null, -7, null), + Expected("?123", null, "?123", -202, null), + Expected(null, "space_key", null, ' '.code, null), + Expected("(", null, null, '('.code, listOf("<", "[", "{").map { it to it.first().code }), + Expected("$", null, null, '$'.code, listOf("£" to '£'.code, "₱" to '₱'.code, "€" to '€'.code, "¢" to '¢'.code, "¥" to '¥'.code, "¥" to '€'.code)), + Expected("a", null, null, ' '.code, null), + Expected("a", null, null, ' '.code, null), + Expected(null, "clipboard_action_key", null, KeyCode.CLIPBOARD, null), + Expected(null, "clipboard_action_key", null, KeyCode.MULTIPLE_CODE_POINTS, null), // todo: this works here, but crashes on phone + Expected("p", null, null, 'p'.code, listOf("$" to '$'.code)), ) val layoutString = """ [ @@ -242,7 +242,7 @@ f""", // no newline at the end { "code": 32, "label": "a|b" }, { "label": "!icon/clipboard_action_key|!code/key_clipboard" }, { "label": "!icon/clipboard_action_key" }, - { "label": "p" } + { "label": "p", "popup": { "main": { "label": "$$$" } } } ], [ { "label": "q" }, @@ -278,8 +278,10 @@ f""", // no newline at the end val keyParams = keyData.toKeyParams(params) println("params: key ${keyParams.mLabel}: code ${keyParams.mCode}, popups: ${keyParams.mPopupKeys?.toList()}") assertEquals(expected[index].label, keyParams.mLabel) + assertEquals(expected[index].icon, keyParams.mIconName) assertEquals(expected[index].code, keyParams.mCode) - assertEquals(expected[index].popups?.sorted(), keyParams.mPopupKeys?.mapNotNull { it.mLabel }?.sorted()) // todo (later): what's wrong with order? + // todo (later): what's wrong with popup order? + assertEquals(expected[index].popups?.sortedBy { it.first }, keyParams.mPopupKeys?.mapNotNull { it.mLabel to it.mCode }?.sortedBy { it.first }) assertEquals(expected[index].text, keyParams.outputText) } assertEquals("!", keys.last()[0].toKeyParams(params).mPopupKeys?.first()?.mLabel)