diff --git a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/ime_service/IMEService.kt b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/ime_service/IMEService.kt index aef2df22b..54184ef05 100644 --- a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/ime_service/IMEService.kt +++ b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/ime_service/IMEService.kt @@ -15817,6 +15817,20 @@ class IMEService : InputMethodService(), LifecycleOwner, InputConnection, cancelOngoingLongPressForAction(action) } + override fun usesDefaultActionAfterLongPress(action: KeyAction): Boolean = when (action) { + KeyAction.Delete, + KeyAction.MoveCursorLeft, + KeyAction.MoveCursorRight, + KeyAction.Space, + KeyAction.Convert, + KeyAction.SwitchToNextIme, + KeyAction.Cancel, + KeyAction.DoNothing, + KeyAction.Backspace, + is KeyAction.Text -> false + else -> true + } + override fun onActionLongPress(action: KeyAction) { if (deferCursorInput { onActionLongPress(action) }) return flushPendingBackspacesBefore(action) diff --git a/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/ime_service/SumireInputLifecycleTest.kt b/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/ime_service/SumireInputLifecycleTest.kt index 8b9e3abfa..13d636636 100644 --- a/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/ime_service/SumireInputLifecycleTest.kt +++ b/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/ime_service/SumireInputLifecycleTest.kt @@ -2,29 +2,43 @@ package com.kazumaproject.markdownhelperkeyboard.ime_service import android.app.Activity import android.content.Context +import android.os.Looper +import android.os.SystemClock import android.view.ContextThemeWrapper import android.view.LayoutInflater +import android.view.MotionEvent import android.view.View import android.view.inputmethod.EditorInfo import com.kazumaproject.core.domain.state.InputMode import com.kazumaproject.core.domain.state.TenKeyQWERTYMode +import com.kazumaproject.custom_keyboard.data.FlickAction +import com.kazumaproject.custom_keyboard.data.FlickDirection import com.kazumaproject.custom_keyboard.data.KeyAction import com.kazumaproject.custom_keyboard.data.KeyData +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.view.FlickKeyboardView import com.kazumaproject.markdownhelperkeyboard.R +import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.CustomKeyboardLayout import com.kazumaproject.markdownhelperkeyboard.databinding.MainLayoutBinding +import com.kazumaproject.markdownhelperkeyboard.ime_service.models.SymbolKeyboardState +import com.kazumaproject.markdownhelperkeyboard.repository.KeyboardRepository import com.kazumaproject.markdownhelperkeyboard.setting_activity.AppPreference import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.emptyFlow import org.junit.Assert.* import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mockito.* import org.robolectric.Robolectric import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf import org.robolectric.annotation.Config import org.robolectric.util.ReflectionHelpers as Reflect import org.robolectric.util.ReflectionHelpers.ClassParameter +import java.time.Duration +import java.util.concurrent.atomic.AtomicBoolean @RunWith(RobolectricTestRunner::class) @Config(sdk = [35]) @@ -92,6 +106,58 @@ class SumireInputLifecycleTest { } } + @Test fun heldCustomKeyboardSwitchRunsOnReleaseInsideTheKey() { + val source = CustomKeyboardLayout(layoutId = 1L, name = "Source", columnCount = 1, rowCount = 1) + val target = source.copy(layoutId = 2L, name = "Target", stableId = "target") + Reflect.setField(service, "customLayouts", listOf(source, target)) + Reflect.setField(service, "currentCustomKeyboardStableId", source.stableId) + service.keyboardRepository = mock(KeyboardRepository::class.java).also { + doReturn(emptyFlow()).`when`(it).getFullLayout(target.layoutId) + } + val key = displayCustomActionKey(KeyAction.MoveToCustomKeyboard(target.stableId)) + + val draggedHold = hold(key) + touch(key, MotionEvent.ACTION_MOVE, draggedHold, y = -100f) + touch(key, MotionEvent.ACTION_UP, draggedHold, y = -100f) + assertEquals(source.stableId, Reflect.getField(service, "currentCustomKeyboardStableId")) + + val hold = hold(key) + assertEquals(source.stableId, Reflect.getField(service, "currentCustomKeyboardStableId")) + touch(key, MotionEvent.ACTION_UP, hold) + assertEquals(target.stableId, Reflect.getField(service, "currentCustomKeyboardStableId")) + } + + @Test fun heldCustomFlickActionRunsOnceOnRelease() { + val key = displayCustomActionKey( + KeyAction.DoNothing, + mapOf(FlickDirection.UP to FlickAction.Action(KeyAction.ShowEmojiKeyboard)) + ) + val symbols = Reflect.getField>(service, "_keyboardSymbolViewState") + + val hold = hold(key) + touch(key, MotionEvent.ACTION_MOVE, hold, y = -100f) + assertFalse(symbols.value.isShown) + touch(key, MotionEvent.ACTION_UP, hold, y = -100f) + assertTrue(symbols.value.isShown) + } + + @Test fun heldCustomDeleteRepeatsUntilRelease() { + val key = displayCustomActionKey(KeyAction.Delete) + val repeat = Reflect.getField(service, "deleteKeyLongKeyPressed") + val input = Reflect.getField>(service, "_inputString") + input.value = "abcd" + + val hold = hold(key) + assertTrue(repeat.get()) + val textAtRelease = input.value + assertTrue(textAtRelease.length < "abcd".length) + + touch(key, MotionEvent.ACTION_UP, hold) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(300)) + assertFalse(repeat.get()) + assertEquals(textAtRelease, input.value) + } + @Test fun renderingCurrentSurfaceKeepsControllersAndAnActiveKeyPressed() { binding.customLayoutDefault.visibility = View.VISIBLE val key = binding.customLayoutDefault.getChildAt(0) @@ -103,6 +169,47 @@ class SumireInputLifecycleTest { assertEquals(controllers, Reflect.getField>(binding.customLayoutDefault, "crossFlickControllers")) } + private fun displayCustomActionKey( + action: KeyAction, + flicks: Map = emptyMap() + ): View { + Reflect.getField>(service, "_tenKeyQWERTYMode").value = TenKeyQWERTYMode.Custom + val id = "custom_special_key" + val key = KeyData( + label = "Special", row = 0, column = 0, isFlickable = flicks.isNotEmpty(), + action = action, isSpecialKey = true, keyId = id, + keyType = if (flicks.isEmpty()) KeyType.NORMAL else KeyType.CROSS_FLICK + ) + val maps = if (flicks.isEmpty()) emptyMap() + else mapOf(id to listOf(flicks + (FlickDirection.TAP to FlickAction.Action(action)))) + val keyboard = binding.customLayoutDefault + keyboard.clearSumireSpecialKeyActionResolver() + keyboard.visibility = View.VISIBLE + keyboard.setKeyboard(KeyboardLayout(listOf(key), maps, columnCount = 1, rowCount = 1)) + keyboard.measure( + View.MeasureSpec.makeMeasureSpec(200, View.MeasureSpec.EXACTLY), + View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY) + ) + keyboard.layout(0, 0, 200, 100) + return keyboard.getChildAt(0) + } + + private fun hold(key: View): Long { + val downTime = SystemClock.uptimeMillis() + touch(key, MotionEvent.ACTION_DOWN, downTime) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(310)) + return downTime + } + + private fun touch(key: View, action: Int, downTime: Long, y: Float = key.height / 2f) { + val event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), action, key.width / 2f, y, 0) + try { + key.dispatchTouchEvent(event) + } finally { + event.recycle() + } + } + private fun keys() = Reflect.getField>(binding.customLayoutDefault, "keyInfos").map { Reflect.getField(it, "keyData") } diff --git a/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/controller/CrossFlickInputController.kt b/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/controller/CrossFlickInputController.kt index e7efe3d56..7080c92d1 100644 --- a/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/controller/CrossFlickInputController.kt +++ b/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/controller/CrossFlickInputController.kt @@ -144,7 +144,7 @@ class CrossFlickInputController( private val controllerScope = CoroutineScope(Dispatchers.Main + SupervisorJob()) private var longPressJob: Job? = null private var isLongPressMode = false - private var isLongPressTriggered = false + private var longPressSelection: Pair? = null private var popupColorTheme: FlickPopupColorTheme? = null private var inputTextTransform: (String) -> String = { it } @@ -196,10 +196,13 @@ class CrossFlickInputController( // コントローラを破棄する。ビューのデタッチやキーボードビューの再構築時に FlickKeyboardView から呼ばれる。 fun cancel() { - listener?.onCanceled() - activeGestureConfig = null longPressJob?.cancel() + longPressJob = null controllerScope.cancel() + isLongPressMode = false + cancelLongPressAction() + listener?.onCanceled() + activeGestureConfig = null restoreOriginalButtonText() dismissAllPopups(clearDirectionalCache = true) } @@ -235,13 +238,14 @@ class CrossFlickInputController( private fun handleTouchEvent(view: View, event: MotionEvent): Boolean { when (event.action) { MotionEvent.ACTION_DOWN -> { + longPressJob?.cancel() + isLongPressMode = false + cancelLongPressAction() val gestureConfig = gestureConfigSource.snapshot() activeGestureConfig = gestureConfig view.isPressed = true view.drawableHotspotChanged(event.x, event.y) - isLongPressMode = false - isLongPressTriggered = false anchorView = view initialTouchPoint.set(event.rawX, event.rawY) currentDirection = FlickDirection.TAP @@ -256,10 +260,8 @@ class CrossFlickInputController( showDirectionalPopup(FlickDirection.TAP) } - longPressJob?.cancel() longPressJob = controllerScope.launch { delay(gestureConfig.longPressTimeoutMillis) - isLongPressTriggered = true isLongPressMode = true onLongPressTriggered() } @@ -312,12 +314,14 @@ class CrossFlickInputController( longPressJob = null if (event.action == MotionEvent.ACTION_UP) { + longPressSelection = null commitAction() } else { - notifyLongPressCanceledIfNeeded() + cancelLongPressAction() listener?.onCanceled() } + isLongPressMode = false restoreOriginalButtonText() dismissAllPopups() anchorView = null @@ -345,7 +349,7 @@ class CrossFlickInputController( } else { dismissAllActionPopups() } - notifyLongPressActionPreview() + updateLongPressAction() } InputMode.TEXT -> { @@ -390,7 +394,7 @@ class CrossFlickInputController( commitCrossFlickAction( currentDirection = currentDirection, flickActionMap = flickActionMap, - isLongPressTriggered = isLongPressTriggered, + isLongPressTriggered = isLongPressMode, listener = listener ) } @@ -410,18 +414,10 @@ class CrossFlickInputController( } } - private fun notifyLongPressCanceledIfNeeded() { - if (!isLongPressTriggered || inputMode != InputMode.ACTION) { - isLongPressTriggered = false - isLongPressMode = false - return - } - - val action = resolveCrossFlickAction(currentDirection, flickActionMap)?.toKeyAction() - ?: KeyAction.Cancel - listener?.onFlickLongPressCanceled(action, currentDirection) - isLongPressTriggered = false - isLongPressMode = false + private fun cancelLongPressAction() { + val (action, direction) = longPressSelection ?: return + longPressSelection = null + listener?.onFlickLongPressCanceled(action, direction) } // 通常フリック中(長押し前)に方向が変わったときのポップアップ更新。ACTION_MOVE から呼ばれる。 @@ -442,7 +438,7 @@ class CrossFlickInputController( when (inputMode) { InputMode.ACTION -> { highlightActionPopup(direction) - notifyLongPressActionPreview() + updateLongPressAction() } InputMode.TEXT -> { @@ -456,10 +452,14 @@ class CrossFlickInputController( } } - // ACTION モードの長押し中、現在ハイライトされている方向のアクションを listener へプレビュー通知する。 - private fun notifyLongPressActionPreview() { - resolveAction(currentDirection)?.let { - listener?.onFlickLongPress(it.toKeyAction(), currentDirection) + private fun updateLongPressAction() { + val action = resolveAction(currentDirection)?.toKeyAction() + if (action != longPressSelection?.first) { + cancelLongPressAction() + } + if (action != null && isLongPressMode) { + longPressSelection = action to currentDirection + listener?.onFlickLongPress(action, currentDirection) } } diff --git a/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/controller/TapLongPressInputController.kt b/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/controller/TapLongPressInputController.kt index 15871b20d..7bc11bee0 100644 --- a/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/controller/TapLongPressInputController.kt +++ b/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/controller/TapLongPressInputController.kt @@ -22,7 +22,7 @@ class TapLongPressInputController( fun onPress() fun onTap() fun onLongPress() - fun onUpAfterLongPress() + fun onUpAfterLongPress(tapCanceled: Boolean) fun onLongPressCanceled() } @@ -114,12 +114,13 @@ class TapLongPressInputController( MotionEvent.ACTION_UP -> { view.removeCallbacks(longPressRunnable) view.isPressed = false - isTouchActive = false + val wasLongPress = isLongPressTriggered + val tapCanceled = isTapCanceled + clearGesture() when { - isLongPressTriggered -> listener?.onUpAfterLongPress() - !isTapCanceled -> view.performClick() + wasLongPress -> listener?.onUpAfterLongPress(tapCanceled) + !tapCanceled -> view.performClick() } - clearGesture() return true } diff --git a/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/view/FlickKeyboardView.kt b/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/view/FlickKeyboardView.kt index 1eb6e39b7..dde0bf479 100644 --- a/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/view/FlickKeyboardView.kt +++ b/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/view/FlickKeyboardView.kt @@ -142,6 +142,9 @@ class FlickKeyboardView @JvmOverloads constructor( fun onFlickActionLongPress(action: KeyAction) fun onFlickActionUpAfterLongPress(action: KeyAction, isFlick: Boolean) fun onLongPressActionCanceled(action: KeyAction) {} + + /** Whether a custom key should commit its default action on release after a hold. */ + fun usesDefaultActionAfterLongPress(action: KeyAction): Boolean = false fun onToggleText(keyIdentity: String, values: List) { values.firstOrNull()?.let { onAction(KeyAction.Text(it), false) } } @@ -2086,30 +2089,33 @@ class FlickKeyboardView @JvmOverloads constructor( } override fun onFlickLongPress(action: KeyAction) { - if (action !is KeyAction.Text) { - doubleTapActionDispatcher.interrupt() - longPressOwnerKeyData = keyData - this@FlickKeyboardView.listener?.onFlickActionLongPress(action) - } + if (action is KeyAction.Text) return + doubleTapActionDispatcher.interrupt() + if (usesDefaultActionAfterLongPress(action)) return + longPressOwnerKeyData = keyData + this@FlickKeyboardView.listener?.onFlickActionLongPress(action) } override fun onFlickUpAfterLongPress( action: KeyAction, isFlick: Boolean ) { - if (action !is KeyAction.Text) { - doubleTapActionDispatcher.interrupt() - this@FlickKeyboardView.listener?.onFlickActionUpAfterLongPress( - action, isFlick = isFlick - ) + if (action is KeyAction.Text) return + if (usesDefaultActionAfterLongPress(action)) { + dispatchNonTapAction(action, isFlick) + return } + doubleTapActionDispatcher.interrupt() + this@FlickKeyboardView.listener?.onFlickActionUpAfterLongPress( + action, isFlick + ) } override fun onFlickLongPressCanceled( action: KeyAction, direction: FlickDirection ) { - if (action !is KeyAction.Text) { + if (action !is KeyAction.Text && !usesDefaultActionAfterLongPress(action)) { this@FlickKeyboardView.listener?.onLongPressActionCanceled( action ) @@ -2640,23 +2646,26 @@ class FlickKeyboardView @JvmOverloads constructor( override fun onLongPress() { doubleTapActionDispatcher.interrupt() + val action = currentAction() + if (usesDefaultActionAfterLongPress(action)) return longPressOwnerKeyData = keyData - this@FlickKeyboardView.listener?.onActionLongPress( - currentAction() - ) + this@FlickKeyboardView.listener?.onActionLongPress(action) } - override fun onUpAfterLongPress() { + override fun onUpAfterLongPress(tapCanceled: Boolean) { + val action = currentAction() + if (usesDefaultActionAfterLongPress(action)) { + if (!tapCanceled) dispatchNonTapAction(action, isFlick = false) + return + } doubleTapActionDispatcher.interrupt() - this@FlickKeyboardView.listener?.onActionUpAfterLongPress( - currentAction() - ) + this@FlickKeyboardView.listener?.onActionUpAfterLongPress(action) } override fun onLongPressCanceled() { - this@FlickKeyboardView.listener?.onLongPressActionCanceled( - currentAction() - ) + val action = currentAction() + if (usesDefaultActionAfterLongPress(action)) return + this@FlickKeyboardView.listener?.onLongPressActionCanceled(action) } } ) @@ -2995,6 +3004,10 @@ class FlickKeyboardView @JvmOverloads constructor( dispatchNonTapActionWithoutPreviewCancel(action, isFlick) } + private fun usesDefaultActionAfterLongPress(action: KeyAction): Boolean = + layoutOrigin == KeyboardLayoutOrigin.CUSTOM && + listener?.usesDefaultActionAfterLongPress(action) == true + private fun dispatchNonTapActionWithoutPreviewCancel(action: KeyAction, isFlick: Boolean) { doubleTapActionDispatcher.interrupt() listener?.onAction(action, isFlick) diff --git a/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/controller/RuntimeGestureControllerTest.kt b/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/controller/RuntimeGestureControllerTest.kt index 07b2813e0..8d141a52a 100644 --- a/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/controller/RuntimeGestureControllerTest.kt +++ b/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/controller/RuntimeGestureControllerTest.kt @@ -94,7 +94,7 @@ class RuntimeGestureControllerTest { events += "long" } - override fun onUpAfterLongPress() { + override fun onUpAfterLongPress(tapCanceled: Boolean) { events += "upAfterLong" } diff --git a/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/view/FlickKeyboardViewTouchDispatchTest.kt b/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/view/FlickKeyboardViewTouchDispatchTest.kt index a5956973a..a03d6f00e 100644 --- a/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/view/FlickKeyboardViewTouchDispatchTest.kt +++ b/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/view/FlickKeyboardViewTouchDispatchTest.kt @@ -1,8 +1,10 @@ package com.kazumaproject.custom_keyboard.view +import android.app.Activity import android.content.Context import android.graphics.Rect import android.os.Looper +import android.os.SystemClock import android.view.ContextThemeWrapper import android.view.MotionEvent import android.view.View @@ -16,10 +18,15 @@ import com.kazumaproject.custom_keyboard.data.KeyCharacterCase import com.kazumaproject.custom_keyboard.data.KeyData import com.kazumaproject.custom_keyboard.data.KeyType import com.kazumaproject.custom_keyboard.data.KeyboardLayout +import com.kazumaproject.custom_keyboard.data.copyWithItems +import com.kazumaproject.custom_keyboard.data.toKeyItem +import com.kazumaproject.custom_keyboard.layout.KeyboardDefaultLayouts +import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Test import org.junit.runner.RunWith +import org.robolectric.Robolectric import org.robolectric.RobolectricTestRunner import org.robolectric.Shadows.shadowOf import org.robolectric.annotation.Config @@ -29,6 +36,22 @@ import java.util.concurrent.TimeUnit @Config(sdk = [35]) class FlickKeyboardViewTouchDispatchTest { + @Test + fun deleteLongPressStopsWhenFlickChangesAction() { + val mappings = listOf( + FlickAction.Action(KeyAction.DoNothing) to listOf(KeyAction.DoNothing), + FlickAction.Input("") to emptyList(), + null to listOf(KeyAction.Cancel), + FlickAction.Action(KeyAction.DeleteAfterCursor) to listOf(KeyAction.DeleteAfterCursor) + ) + + for ((mapping, expectedRelease) in mappings) { + for (direction in listOf(FlickDirection.UP, FlickDirection.UP_RIGHT)) { + assertDeleteLongPressCanceled(direction, mapping, expectedRelease) + } + } + } + @Test fun setKeyboard_clearsActiveMotionTarget_doesNotDispatchStaleActionUp() { val listener = RecordingKeyboardActionListener() @@ -156,13 +179,14 @@ class FlickKeyboardViewTouchDispatchTest { } private fun keyboardView( - listener: FlickKeyboardView.OnKeyboardActionListener + listener: FlickKeyboardView.OnKeyboardActionListener, + context: Context = ApplicationProvider.getApplicationContext() ): FlickKeyboardView { - val context = ContextThemeWrapper( - ApplicationProvider.getApplicationContext(), + val themedContext = ContextThemeWrapper( + context, com.google.android.material.R.style.Theme_Material3_DayNight_NoActionBar ) - return FlickKeyboardView(context).apply { + return FlickKeyboardView(themedContext).apply { setOnKeyboardActionListener(listener) } } @@ -190,6 +214,83 @@ class FlickKeyboardViewTouchDispatchTest { ) } + private fun assertDeleteLongPressCanceled( + direction: FlickDirection, + mapping: FlickAction?, + expectedRelease: List + ) { + Robolectric.buildActivity(Activity::class.java).setup().use { activity -> + val listener = RecordingKeyboardActionListener() + val keyboard = keyboardView(listener, activity.get()).apply { + setLongPressTimeout(200L) + setKeyboard(flexibleDeleteKeyLayout(direction, mapping)) + } + activity.get().setContentView(keyboard) + layoutKeyboard(keyboard) + listener.canceledLongPresses.clear() + + val delete = keyboard.getChildAt(1) + val origin = IntArray(2).also(keyboard::getLocationOnScreen) + val x = origin[0] + delete.centerX() + val y = origin[1] + delete.centerY() + val targetX = if (direction == FlickDirection.UP) x else x + 100f + val targetY = if (direction == FlickDirection.UP) y - 100f else y + val downTime = SystemClock.uptimeMillis() + val scenario = "$direction with $mapping" + + keyboard.dispatchTouch(MotionEvent.ACTION_DOWN, x, y, downTime, downTime) + shadowOf(Looper.getMainLooper()).idleFor(210, TimeUnit.MILLISECONDS) + assertEquals(scenario, listOf(KeyAction.Delete), listener.flickLongPresses) + + keyboard.dispatchTouch(MotionEvent.ACTION_MOVE, targetX, targetY, downTime, downTime + 210L) + assertEquals(scenario, listOf(KeyAction.Delete), listener.canceledLongPresses) + keyboard.dispatchTouch(MotionEvent.ACTION_UP, targetX, targetY, downTime, downTime + 220L) + assertEquals(scenario, expectedRelease, listener.flickLongPressReleases) + + val letter = keyboard.getChildAt(0) + val letterX = origin[0] + letter.centerX() + val letterY = origin[1] + letter.centerY() + val letterDownTime = downTime + 230L + keyboard.dispatchTouch(MotionEvent.ACTION_DOWN, letterX, letterY, letterDownTime, letterDownTime) + keyboard.dispatchTouch(MotionEvent.ACTION_UP, letterX, letterY, letterDownTime, letterDownTime + 20L) + assertEquals(scenario, listOf(KeyAction.Text("a")), listener.actions) + assertEquals(scenario, listOf(KeyAction.Delete), listener.canceledLongPresses) + } + } + + private fun flexibleDeleteKeyLayout( + direction: FlickDirection, + mapping: FlickAction? + ): KeyboardLayout { + val deleteKeyId = "custom_delete" + val letterKey = KeyData( + label = "a", + row = 0, + column = 0, + isFlickable = false, + action = KeyAction.Text("a"), + keyType = KeyType.QWERTY + ) + val deleteKey = KeyData( + label = "Delete", + row = 0, + column = 1, + isFlickable = true, + action = KeyAction.Delete, + isSpecialKey = true, + drawableResId = com.kazumaproject.core.R.drawable.backspace_24px, + keyId = deleteKeyId, + keyType = KeyType.CROSS_FLICK + ) + val actions = buildMap { + put(FlickDirection.TAP, FlickAction.Action(KeyAction.Delete)) + mapping?.let { put(direction, it) } + } + return KeyboardDefaultLayouts.createEmpty5x4FlexibleTemplateLayout() + .copyWithItems(listOf(letterKey.toKeyItem(), deleteKey.toKeyItem())) + .copy(flickKeyMaps = mapOf(deleteKeyId to listOf(actions))) + } + private fun shiftKeyLayout(): KeyboardLayout { val shiftKey = KeyData( label = "", @@ -263,6 +364,9 @@ class FlickKeyboardViewTouchDispatchTest { private val afterAction: (KeyAction) -> Unit = {} ) : FlickKeyboardView.OnKeyboardActionListener { val actions = mutableListOf() + val flickLongPresses = mutableListOf() + val flickLongPressReleases = mutableListOf() + val canceledLongPresses = mutableListOf() override fun onPress(action: KeyAction) = Unit @@ -277,9 +381,17 @@ class FlickKeyboardViewTouchDispatchTest { override fun onFlickDirectionChanged(direction: FlickDirection) = Unit - override fun onFlickActionLongPress(action: KeyAction) = Unit + override fun onFlickActionLongPress(action: KeyAction) { + flickLongPresses += action + } - override fun onFlickActionUpAfterLongPress(action: KeyAction, isFlick: Boolean) = Unit + override fun onFlickActionUpAfterLongPress(action: KeyAction, isFlick: Boolean) { + flickLongPressReleases += action + } + + override fun onLongPressActionCanceled(action: KeyAction) { + canceledLongPresses += action + } } private companion object {