|
| 1 | +package expo.modules.t3terminal |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.view.KeyEvent |
| 5 | +import android.view.View |
| 6 | +import android.widget.Button |
| 7 | +import android.widget.EditText |
| 8 | +import android.widget.LinearLayout |
| 9 | +import org.junit.Assert.assertEquals |
| 10 | +import org.junit.Assert.assertTrue |
| 11 | +import org.junit.Test |
| 12 | +import org.junit.runner.RunWith |
| 13 | +import org.robolectric.Robolectric |
| 14 | +import org.robolectric.RobolectricTestRunner |
| 15 | +import org.robolectric.annotation.Config |
| 16 | + |
| 17 | +@RunWith(RobolectricTestRunner::class) |
| 18 | +@Config(sdk = [36], manifest = Config.NONE) |
| 19 | +class TerminalNavigationKeyListenerTest { |
| 20 | + @Test |
| 21 | + fun hardwareNavigationKeysReachThePtyAndKeepFocus() { |
| 22 | + val activity = Robolectric.buildActivity(Activity::class.java).setup().get() |
| 23 | + val sent = StringBuilder() |
| 24 | + val input = EditText(activity).apply { |
| 25 | + setSingleLine(true) |
| 26 | + isFocusableInTouchMode = true |
| 27 | + setOnKeyListener(TerminalNavigationKeyListener { sent.append(it) }) |
| 28 | + } |
| 29 | + val nextControl = Button(activity).apply { isFocusableInTouchMode = true } |
| 30 | + val root = LinearLayout(activity).apply { |
| 31 | + orientation = LinearLayout.VERTICAL |
| 32 | + addView(input) |
| 33 | + addView(nextControl) |
| 34 | + } |
| 35 | + activity.setContentView(root) |
| 36 | + // Focus search below the input only finds controls that have bounds. |
| 37 | + root.measure( |
| 38 | + View.MeasureSpec.makeMeasureSpec(1080, View.MeasureSpec.EXACTLY), |
| 39 | + View.MeasureSpec.makeMeasureSpec(1920, View.MeasureSpec.EXACTLY), |
| 40 | + ) |
| 41 | + root.layout(0, 0, 1080, 1920) |
| 42 | + assertTrue(input.requestFocus()) |
| 43 | + |
| 44 | + val shift = KeyEvent.META_SHIFT_ON or KeyEvent.META_SHIFT_LEFT_ON |
| 45 | + val keys = listOf( |
| 46 | + Triple(KeyEvent.KEYCODE_DPAD_UP, 0, "\u001b[A"), |
| 47 | + Triple(KeyEvent.KEYCODE_DPAD_DOWN, 0, "\u001b[B"), |
| 48 | + Triple(KeyEvent.KEYCODE_DPAD_RIGHT, 0, "\u001b[C"), |
| 49 | + Triple(KeyEvent.KEYCODE_DPAD_LEFT, 0, "\u001b[D"), |
| 50 | + Triple(KeyEvent.KEYCODE_TAB, 0, "\t"), |
| 51 | + Triple(KeyEvent.KEYCODE_TAB, shift, "\u001b[Z"), |
| 52 | + Triple(KeyEvent.KEYCODE_ESCAPE, 0, "\u001b"), |
| 53 | + ) |
| 54 | + for ((keyCode, meta, expected) in keys) { |
| 55 | + sent.clear() |
| 56 | + input.dispatchKeyEvent(KeyEvent(0, 0, KeyEvent.ACTION_DOWN, keyCode, 0, meta)) |
| 57 | + input.dispatchKeyEvent(KeyEvent(0, 0, KeyEvent.ACTION_UP, keyCode, 0, meta)) |
| 58 | + assertEquals("bytes for key $keyCode meta $meta", expected, sent.toString()) |
| 59 | + assertTrue("focus left the input after key $keyCode meta $meta", input.isFocused) |
| 60 | + } |
| 61 | + |
| 62 | + sent.clear() |
| 63 | + val ctrl = KeyEvent.META_CTRL_ON or KeyEvent.META_CTRL_LEFT_ON |
| 64 | + input.dispatchKeyEvent(KeyEvent(0, 0, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_UP, 0, ctrl)) |
| 65 | + assertEquals("modified arrows are left to the app, as on iOS", "", sent.toString()) |
| 66 | + } |
| 67 | +} |
0 commit comments