Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ class KeyboardEditorViewModel @Inject constructor(
R.string.template_qwerty,
KeyboardDefaultLayouts.createQwertyTemplateLayout()
),
LayoutTemplate(
R.string.template_qwerty_number,
KeyboardDefaultLayouts.createQwertyNumberTemplateLayout()
),
LayoutTemplate(
R.string.template_qwerty_symbol,
KeyboardDefaultLayouts.createQwertySymbolTemplateLayout()
),
LayoutTemplate(
R.string.template_empty_5x4_flexible,
KeyboardDefaultLayouts.createEmpty5x4FlexibleTemplateLayout()
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,8 @@
<string name="template_toggle_kana">かな入力(トグル)</string>
<string name="template_flick_english_cursor">英語入力 (カーソル)</string>
<string name="template_qwerty">QWERTY</string>
<string name="template_qwerty_number">QWERTY(数字)</string>
<string name="template_qwerty_symbol">QWERTY(記号)</string>
<string name="template_empty_5x4_flexible">空のフレキシブルレイアウト</string>
<string name="template_number">数字入力</string>
<string name="editor_place_half_key">半マスキー</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,8 @@
<string name="template_toggle_kana">Kana input (toggle)</string>
<string name="template_flick_english_cursor">English input (cursor)</string>
<string name="template_qwerty">QWERTY</string>
<string name="template_qwerty_number">QWERTY (Numbers)</string>
<string name="template_qwerty_symbol">QWERTY (Symbols)</string>
<string name="template_empty_5x4_flexible">Empty 5x4 Flexible Layout</string>
<string name="template_number">Number input</string>
<string name="editor_place_half_key">Half key</string>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.kazumaproject.markdownhelperkeyboard.custom_keyboard.ui

import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import com.kazumaproject.custom_keyboard.data.KeyAction
import com.kazumaproject.custom_keyboard.data.KeyItem
import com.kazumaproject.custom_keyboard.data.KeyType
import com.kazumaproject.custom_keyboard.data.KeyboardLayout
import com.kazumaproject.custom_keyboard.data.QwertyPopupSlot
import com.kazumaproject.custom_keyboard.data.copyWithKeys
import com.kazumaproject.markdownhelperkeyboard.R
import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.import_export.KeyboardLayoutImportResult
import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.import_export.KeyboardLayoutJsonExporter
import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.import_export.KeyboardLayoutJsonImporter
import com.kazumaproject.markdownhelperkeyboard.database.AppDatabase
import com.kazumaproject.markdownhelperkeyboard.repository.KeyboardRepository
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [35])
class KeyboardEditorQwertyTemplateTest {
@Test
fun applyEditSaveReloadAndBackup_preserveAllThreeTemplatesAndExistingLayouts() = runBlocking {
val context = ApplicationProvider.getApplicationContext<Context>()
val db = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java)
.allowMainThreadQueries().build()
try {
val repository = KeyboardRepository(db.keyboardLayoutDao())
val editor = KeyboardEditorViewModel(repository)
val names = listOf(R.string.template_qwerty, R.string.template_qwerty_number, R.string.template_qwerty_symbol)
val start = editor.availableTemplates.indexOfFirst { it.nameResId == names.first() }
assertEquals(names, editor.availableTemplates.drop(start).take(3).map { it.nameResId })

// A layout saved before the template update must keep its original key type.
val original = editor.availableTemplates[start].layout.let { template ->
template.copyWithKeys(template.keys.map { it.copy(keyType = KeyType.NORMAL, qwertyConfig = null) })
}
val oldId = repository.saveLayout(original, "Existing QWERTY", null)
val saved = names.map { name ->
editor.applyTemplate(editor.availableTemplates.single { it.nameResId == name }.layout)
val layout = editor.uiState.value.layout
assertTrue(keyboardEditorCapabilities(layout).showHalfCellControls)
assertTrue(layout.keys.filterNot { it.isSpecialKey }.all { it.keyType == KeyType.QWERTY })
val key = layout.keys.first { !it.isSpecialKey }
val config = requireNotNull(key.qwertyConfig)
val normal = config.normal.copy(output = "custom text",
slots = listOf(QwertyPopupSlot("é"), QwertyPopupSlot(), QwertyPopupSlot("👩‍💻")))
val edited = key.copy(label = normal.displayLabel, action = KeyAction.Text(normal.output),
qwertyConfig = config.copy(normal = normal))
val expected = layout.copyWithKeys(layout.keys.map { if (it.keyId == key.keyId) edited else it })
val id = repository.saveLayout(expected, context.getString(name), null)
assertLayoutContent(expected, repository.getFullLayout(id).first())
expected
}
assertLayoutContent(original, repository.getFullLayout(oldId).first())
val backups = repository.getAllFullLayoutsForExport().filter { it.layout.layoutId != oldId }
val imported = KeyboardLayoutJsonImporter.parse(KeyboardLayoutJsonExporter.toJson(backups))
assertTrue(imported is KeyboardLayoutImportResult.Success)
repository.deleteLayoutConfirmed(oldId)
backups.forEach { repository.deleteLayoutConfirmed(it.layout.layoutId) }
assertTrue(repository.importLayouts((imported as KeyboardLayoutImportResult.Success).layouts)
is KeyboardLayoutImportResult.Success)
val restored = repository.getLayoutsNotFlow()
names.zip(saved).forEach { (name, expected) ->
val id = restored.single { it.name == context.getString(name) }.layoutId
assertLayoutContent(expected, repository.getFullLayout(id).first())
}
} finally {
db.close()
}
}

private fun assertLayoutContent(expected: KeyboardLayout, actual: KeyboardLayout) {
assertEquals(expected.columnUnitCount, actual.columnUnitCount)
assertEquals(expected.rowUnitCount, actual.rowUnitCount)
expected.keys.forEach { before ->
val after = actual.keys.single { it.keyId == before.keyId }
assertEquals(before.label, after.label)
assertEquals(before.action, after.action)
assertEquals(before.keyType, after.keyType)
assertEquals(before.qwertyConfig, after.qwertyConfig)
assertEquals(before.doubleTapBinding, after.doubleTapBinding)
}
fun placements(layout: KeyboardLayout) = layout.items.filterIsInstance<KeyItem>()
.associate { it.keyData.keyId to it.placement }
assertEquals(placements(expected), placements(actual))
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.kazumaproject.custom_keyboard.layout

import com.kazumaproject.core.data.qwerty.QWERTYKeys
import com.kazumaproject.core.domain.qwerty.QWERTYKeyInfo
import com.kazumaproject.core.domain.qwerty.QWERTYKeyMap
import com.kazumaproject.custom_keyboard.data.FlickAction
import com.kazumaproject.custom_keyboard.data.FlickDirection
import com.kazumaproject.custom_keyboard.data.DoubleTapBinding
Expand All @@ -12,6 +15,9 @@ import com.kazumaproject.custom_keyboard.data.KeyType
import com.kazumaproject.custom_keyboard.data.KeyboardInputMode
import com.kazumaproject.custom_keyboard.data.KeyboardLayout
import com.kazumaproject.custom_keyboard.data.KeyboardLayoutItem
import com.kazumaproject.custom_keyboard.data.QwertyKeyConfig
import com.kazumaproject.custom_keyboard.data.QwertyKeyLayer
import com.kazumaproject.custom_keyboard.data.QwertyPopupSlot
import com.kazumaproject.custom_keyboard.data.SpacerItem
import com.kazumaproject.custom_keyboard.data.TfbiFlickRotation
import com.kazumaproject.custom_keyboard.data.SumireFlickInputMode
Expand Down Expand Up @@ -4749,9 +4755,10 @@ object KeyboardDefaultLayouts {
val columnSpanUnits: Int
}

/** Plain character key (label is also the typed text). */
/** Character key with the standard QWERTY tap and long-press layers. */
private data class CharKeySpec(
val label: String,
val qwertyConfig: QwertyKeyConfig,
override val columnSpanUnits: Int = 2
) : TemplateItemSpec

Expand Down Expand Up @@ -4814,9 +4821,10 @@ object KeyboardDefaultLayouts {
action = KeyAction.Text(itemSpec.label),
rowSpan = approxRowSpan,
colSpan = approxColSpan,
keyType = KeyType.NORMAL,
keyType = KeyType.QWERTY,
isSpecialKey = false,
keyId = keyId
keyId = keyId,
qwertyConfig = itemSpec.qwertyConfig
)
items += KeyItem(id = keyId, keyData = keyData, placement = placement)
keys += keyData
Expand Down Expand Up @@ -4867,7 +4875,18 @@ object KeyboardDefaultLayouts {
)
}

private fun chars(vararg s: String): List<CharKeySpec> = s.map { CharKeySpec(it) }
private fun QWERTYKeyInfo.QWERTYVariation.toTemplateConfig(): QwertyKeyConfig {
fun layer(output: Char, candidates: List<Char>?) = QwertyKeyLayer(
output = output.toString(),
columns = candidates.orEmpty().size.coerceIn(1, 3),
slots = candidates.orEmpty().map { QwertyPopupSlot(output = it.toString()) }
)
val tap = requireNotNull(tap)
return QwertyKeyConfig(
normal = layer(tap, variations),
shifted = layer(capChar ?: tap, capVariations ?: variations)
)
}

private val shiftSpec
get() = SpecialKeySpec(
Expand Down Expand Up @@ -4918,33 +4937,66 @@ object KeyboardDefaultLayouts {
* Row 2: Shift | spacer | z x c v b n m | spacer | Delete
* Row 3: SwitchIme | Space | Enter
*/
fun createQwertyTemplateLayout(): KeyboardLayout {
fun createQwertyTemplateLayout(): KeyboardLayout = QWERTYKeyMap().let { keyMap ->
createQwertyTemplate(
"qwerty", "QWERTY", QWERTYKeys.DEFAULT_KEYS, middleRowSize = 9,
keyMap.keysDefault.map(keyMap::getKeyInfoDefault)
)
}

fun createQwertyNumberTemplateLayout(): KeyboardLayout = QWERTYKeyMap().let { keyMap ->
createQwertyTemplate(
"qwerty_number", "QWERTY (Numbers)", QWERTYKeys.NUMBER_KEYS, middleRowSize = 10,
keyMap.keysNumber.map(keyMap::getKeyInfoNumber)
)
}

fun createQwertySymbolTemplateLayout(): KeyboardLayout = QWERTYKeyMap().let { keyMap ->
createQwertyTemplate(
"qwerty_symbol", "QWERTY (Symbols)", QWERTYKeys.SYMBOL_KEYS, middleRowSize = 10,
keyMap.keysSymbol.map(keyMap::getKeyInfoSymbol)
)
}

private fun createQwertyTemplate(
idPrefix: String,
name: String,
characters: List<Char>,
middleRowSize: Int,
keyInfos: List<QWERTYKeyInfo>
): KeyboardLayout {
val configs = keyInfos.filterIsInstance<QWERTYKeyInfo.QWERTYVariation>()
.filter { it.tap != null }
.associate { it.tap!! to it.toTemplateConfig() }
fun chars(characters: List<Char>) = characters.map { char ->
CharKeySpec(char.toString(), configs.getValue(char))
}
val bottomRow = characters.drop(10 + middleRowSize)
val bottomGap = (16 - bottomRow.size * 2) / 2
val spec = TemplateLayoutSpec(
idPrefix = "qwerty",
name = "QWERTY",
idPrefix = idPrefix,
name = name,
rowUnitCount = 8,
columnUnitCount = 20,
rows = listOf(
TemplateRowSpec(
rowUnits = 0,
items = listOf(
*chars("q", "w", "e", "r", "t", "y", "u", "i", "o", "p").toTypedArray()
)
items = chars(characters.take(10))
),
TemplateRowSpec(
rowUnits = 2,
items = listOf(
SpacerSpec("row1_gap", columnSpanUnits = 1),
*chars("a", "s", "d", "f", "g", "h", "j", "k", "l").toTypedArray()
)
items = buildList {
if (middleRowSize == 9) add(SpacerSpec("row1_gap", columnSpanUnits = 1))
addAll(chars(characters.drop(10).take(middleRowSize)))
}
),
TemplateRowSpec(
rowUnits = 4,
items = listOf(
shiftSpec,
SpacerSpec("shift_gap", columnSpanUnits = 1),
*chars("z", "x", "c", "v", "b", "n", "m").toTypedArray(),
SpacerSpec("delete_gap", columnSpanUnits = 1),
SpacerSpec("shift_gap", columnSpanUnits = bottomGap),
*chars(bottomRow).toTypedArray(),
SpacerSpec("delete_gap", columnSpanUnits = bottomGap),
deleteSpec
)
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.kazumaproject.custom_keyboard.layout

import com.kazumaproject.core.data.qwerty.QWERTYKeys
import com.kazumaproject.core.domain.qwerty.QWERTYKeyInfo
import com.kazumaproject.core.domain.qwerty.QWERTYKeyMap
import com.kazumaproject.custom_keyboard.data.GridPlacement
import com.kazumaproject.custom_keyboard.data.DoubleTapPolicy
import com.kazumaproject.custom_keyboard.data.FlickAction
Expand Down Expand Up @@ -95,17 +98,18 @@ class AlphabetTemplateLayoutsTest {
)
}

private fun assertAllCharacterKeysAreNormalText(layout: KeyboardLayout) {
private fun assertAllCharacterKeysAreQwertyText(layout: KeyboardLayout) {
layout.keys.filter { !it.isSpecialKey }.forEach { key ->
val action = key.action
assertTrue(
"Character key '${key.label}' must use KeyAction.Text",
action is KeyAction.Text && action.text == key.label
)
assertEquals(
"Character key '${key.label}' must be NORMAL",
KeyType.NORMAL, key.keyType
"Character key '${key.label}' must be QWERTY",
KeyType.QWERTY, key.keyType
)
assertTrue(key.qwertyConfig?.isValid() == true)
assertFalse("Character key '${key.label}' must not be flickable", key.isFlickable)
assertNotNull("Character key '${key.label}' must have a keyId", key.keyId)
}
Expand All @@ -123,11 +127,62 @@ class AlphabetTemplateLayoutsTest {
row0Labels(layout)
)
assertHasSpecialKeys(layout, "QWERTY")
assertAllCharacterKeysAreNormalText(layout)
assertAllCharacterKeysAreQwertyText(layout)
assertNotNull(layout.keys.firstOrNull { it.keyId == "qwerty_key_q" })
assertNotNull(layout.keys.firstOrNull { it.keyId == "qwerty_key_p" })
}

@Test
fun qwertyTemplates_matchStandardTapAndLongPressLayers() {
val map = QWERTYKeyMap()
val modes = listOf(
Triple(KeyboardDefaultLayouts.createQwertyTemplateLayout(), QWERTYKeys.DEFAULT_KEYS,
map.keysDefault.map(map::getKeyInfoDefault)),
Triple(KeyboardDefaultLayouts.createQwertyNumberTemplateLayout(), QWERTYKeys.NUMBER_KEYS,
map.keysNumber.map(map::getKeyInfoNumber)),
Triple(KeyboardDefaultLayouts.createQwertySymbolTemplateLayout(), QWERTYKeys.SYMBOL_KEYS,
map.keysSymbol.map(map::getKeyInfoSymbol))
)
modes.forEach { (layout, characters, infos) ->
assertCommonStructure(layout)
assertHasSpecialKeys(layout, "QWERTY")
assertAllCharacterKeysAreQwertyText(layout)
assertFalse(hasPlacementIssues(layout.items, layout.rowUnitCount, layout.columnUnitCount))
assertEquals(layout.keys.size, layout.keys.map { it.keyId }.toSet().size)
val characterKeys = layout.items.filterIsInstance<KeyItem>()
.filter { !it.keyData.isSpecialKey }
.sortedWith(compareBy({ it.placement.rowUnits }, { it.placement.columnUnits }))
.map { it.keyData }
assertEquals(characters.map(Char::toString), characterKeys.map { it.label })
characterKeys.forEach { key ->
val info = infos.filterIsInstance<QWERTYKeyInfo.QWERTYVariation>()
.first { it.tap.toString() == key.label }
val config = key.qwertyConfig!!
val shifted = requireNotNull(config.shifted)
assertEquals(info.tap.toString(), config.normal.output)
assertEquals(info.variations.orEmpty().map(Char::toString), config.normal.slots.map { it.output })
assertEquals((info.capChar ?: info.tap).toString(), shifted.output)
assertEquals((info.capVariations ?: info.variations).orEmpty().map(Char::toString),
shifted.slots.map { it.output })
}
}
}

@Test
fun numberAndSymbolTemplates_haveTenTenFiveCharactersAndCenteredBottomRow() {
listOf(
"qwerty_number" to KeyboardDefaultLayouts.createQwertyNumberTemplateLayout(),
"qwerty_symbol" to KeyboardDefaultLayouts.createQwertySymbolTemplateLayout()
).forEach { (prefix, layout) ->
assertTrue(layout.keys.all { it.keyId!!.startsWith("${prefix}_") })
val characters = layout.items.filterIsInstance<KeyItem>().filter { !it.keyData.isSpecialKey }
assertEquals(listOf(10, 10, 5), listOf(0, 2, 4).map { row -> characters.count { it.placement.rowUnits == row } })
val bottom = characters.filter { it.placement.rowUnits == 4 }
assertEquals(listOf(5, 7, 9, 11, 13), bottom.map { it.placement.columnUnits })
assertTrue(bottom.all { it.placement.columnSpanUnits == 2 })
}
}

@Test
fun flickEnglishCursorTemplate_usesCommitAndInsertSpaceForTemplateSpaces() {
val layout = KeyboardDefaultLayouts.createFlickEnglishTemplateLayout(
Expand Down
Loading