|
| 1 | +package expo.modules.t3composereditor |
| 2 | + |
| 3 | +import android.content.ClipData |
| 4 | +import android.content.ClipboardManager |
| 5 | +import android.content.Context |
| 6 | +import android.view.KeyEvent |
| 7 | +import org.junit.Assert.assertEquals |
| 8 | +import org.junit.Assert.assertFalse |
| 9 | +import org.junit.Assert.assertNull |
| 10 | +import org.junit.Assert.assertTrue |
| 11 | +import org.junit.Test |
| 12 | +import org.junit.runner.RunWith |
| 13 | +import org.robolectric.RobolectricTestRunner |
| 14 | +import org.robolectric.RuntimeEnvironment |
| 15 | +import org.robolectric.annotation.Config |
| 16 | + |
| 17 | +@RunWith(RobolectricTestRunner::class) |
| 18 | +@Config(sdk = [36], manifest = Config.NONE) |
| 19 | +class ComposerPasteTest { |
| 20 | + private val context = RuntimeEnvironment.getApplication() |
| 21 | + private val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager |
| 22 | + private val editor = SelectionAwareEditText(context).apply { |
| 23 | + textPasteThresholdBytes = 32 * 1024 |
| 24 | + maxInputChars = 120_000 |
| 25 | + } |
| 26 | + |
| 27 | + private fun pasteAsText(): Boolean = editor.onKeyShortcut( |
| 28 | + KeyEvent.KEYCODE_V, |
| 29 | + KeyEvent( |
| 30 | + 0, |
| 31 | + 0, |
| 32 | + KeyEvent.ACTION_DOWN, |
| 33 | + KeyEvent.KEYCODE_V, |
| 34 | + 0, |
| 35 | + KeyEvent.META_CTRL_ON or KeyEvent.META_SHIFT_ON |
| 36 | + ) |
| 37 | + ) |
| 38 | + |
| 39 | + @Test |
| 40 | + fun shortcutKeepsLargeTextInlineAndUndoable() { |
| 41 | + val pasted = "x".repeat(32 * 1024) |
| 42 | + clipboard.setPrimaryClip(ClipData.newPlainText("test", pasted)) |
| 43 | + editor.setText("before old after") |
| 44 | + editor.setSelection(10, 7) |
| 45 | + editor.pasteTextListener = { _, _, _ -> error("Inline paste must stay native") } |
| 46 | + |
| 47 | + assertTrue(pasteAsText()) |
| 48 | + assertEquals("before $pasted after", editor.text.toString()) |
| 49 | + assertTrue(editor.onTextContextMenuItem(android.R.id.undo)) |
| 50 | + assertEquals("before old after", editor.text.toString()) |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun shortcutInterceptsInputLimitOverflowWithoutChangingTheSelection() { |
| 55 | + clipboard.setPrimaryClip(ClipData.newPlainText("test", "hello")) |
| 56 | + editor.setText("x".repeat(119_999)) |
| 57 | + editor.setSelection(10, 7) |
| 58 | + var intercepted: Triple<String, Int, Int>? = null |
| 59 | + editor.pasteTextListener = { text, start, end -> intercepted = Triple(text, start, end) } |
| 60 | + |
| 61 | + assertTrue(pasteAsText()) |
| 62 | + assertEquals(Triple("hello", 7, 10), intercepted) |
| 63 | + assertEquals(119_999, editor.length()) |
| 64 | + assertEquals(10, editor.selectionStart) |
| 65 | + assertEquals(7, editor.selectionEnd) |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun shortcutAllowsReplacementAtTheInputLimit() { |
| 70 | + clipboard.setPrimaryClip(ClipData.newPlainText("test", "hello")) |
| 71 | + editor.setText("x".repeat(120_000)) |
| 72 | + editor.setSelection(12, 7) |
| 73 | + editor.pasteTextListener = { _, _, _ -> error("Replacement fits the input limit") } |
| 74 | + |
| 75 | + assertTrue(pasteAsText()) |
| 76 | + assertEquals("hello", editor.text.substring(7, 12)) |
| 77 | + assertEquals(120_000, editor.length()) |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun regularPasteStillFoldsAtTheUtf8Threshold() { |
| 82 | + val pasted = "é".repeat(16 * 1024) |
| 83 | + clipboard.setPrimaryClip(ClipData.newPlainText("test", pasted)) |
| 84 | + editor.setText("old") |
| 85 | + editor.setSelection(0, 3) |
| 86 | + var intercepted: String? = null |
| 87 | + editor.pasteTextListener = { text, _, _ -> intercepted = text } |
| 88 | + |
| 89 | + assertTrue(editor.onTextContextMenuItem(android.R.id.paste)) |
| 90 | + assertEquals(pasted, intercepted) |
| 91 | + assertEquals("old", editor.text.toString()) |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + fun shortcutPastesStructuredClipboardAsText() { |
| 96 | + clipboard.setPrimaryClip( |
| 97 | + ClipData.newHtmlText( |
| 98 | + "test", |
| 99 | + "plain text", |
| 100 | + "<pre data-t3-context-fragment=\"x\">plain text</pre>" |
| 101 | + ) |
| 102 | + ) |
| 103 | + editor.setSelection(0) |
| 104 | + editor.pasteContextListener = { error("Paste as Text must not import structured context") } |
| 105 | + |
| 106 | + assertTrue(pasteAsText()) |
| 107 | + assertEquals("plain text", editor.text.toString()) |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + fun readOnlyShortcutDoesNotPasteOrEmitAnEvent() { |
| 112 | + clipboard.setPrimaryClip(ClipData.newPlainText("test", "x".repeat(120_001))) |
| 113 | + editor.setText("unchanged") |
| 114 | + editor.setSelection(editor.length()) |
| 115 | + editor.readOnly = true |
| 116 | + var intercepted: String? = null |
| 117 | + editor.pasteTextListener = { text, _, _ -> intercepted = text } |
| 118 | + |
| 119 | + assertFalse(pasteAsText()) |
| 120 | + assertEquals("unchanged", editor.text.toString()) |
| 121 | + assertNull(intercepted) |
| 122 | + } |
| 123 | +} |
0 commit comments