|
1 | 1 | package expo.modules.t3composereditor |
2 | 2 |
|
| 3 | +import android.app.Activity |
| 4 | +import android.content.ContentProvider |
| 5 | +import android.content.ContentValues |
| 6 | +import android.database.Cursor |
3 | 7 | import android.content.ClipData |
4 | 8 | import android.content.ClipDescription |
5 | 9 | import android.content.ClipboardManager |
6 | 10 | import android.content.Context |
| 11 | +import android.content.pm.ProviderInfo |
7 | 12 | import android.net.Uri |
| 13 | +import android.view.DragEvent |
| 14 | +import android.view.View |
8 | 15 | import android.view.KeyEvent |
9 | 16 | import org.junit.Assert.assertEquals |
10 | 17 | import org.junit.Assert.assertFalse |
11 | 18 | import org.junit.Assert.assertNull |
12 | 19 | import org.junit.Assert.assertTrue |
13 | 20 | import org.junit.Test |
14 | 21 | import org.junit.runner.RunWith |
| 22 | +import org.robolectric.Robolectric |
| 23 | +import org.robolectric.shadows.DragEventBuilder |
| 24 | +import org.robolectric.shadows.ShadowContentResolver |
15 | 25 | import org.robolectric.RobolectricTestRunner |
16 | 26 | import org.robolectric.RuntimeEnvironment |
17 | 27 | import org.robolectric.annotation.Config |
@@ -169,4 +179,140 @@ class ComposerPasteTest { |
169 | 179 | assertEquals("unchanged", editor.text.toString()) |
170 | 180 | assertNull(intercepted) |
171 | 181 | } |
| 182 | + |
| 183 | + private fun dropEditor(): SelectionAwareEditText { |
| 184 | + val activity = Robolectric.buildActivity(Activity::class.java).setup().get() |
| 185 | + return SelectionAwareEditText(activity).apply { |
| 186 | + activity.setContentView(this) |
| 187 | + isFocusableInTouchMode = true |
| 188 | + setText("before") |
| 189 | + setSelection(length()) |
| 190 | + measure( |
| 191 | + View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.EXACTLY), |
| 192 | + View.MeasureSpec.makeMeasureSpec(200, View.MeasureSpec.EXACTLY), |
| 193 | + ) |
| 194 | + layout(0, 0, 500, 200) |
| 195 | + requestFocus() |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + private fun imageClip(vararg names: String): ClipData { |
| 200 | + val provider = DropImageProvider().apply { |
| 201 | + attachInfo(this@ComposerPasteTest.context, ProviderInfo().apply { authority = "images" }) |
| 202 | + } |
| 203 | + ShadowContentResolver.registerProviderInternal("images", provider) |
| 204 | + return ClipData( |
| 205 | + "images", |
| 206 | + arrayOf("image/png", "image/jpeg"), |
| 207 | + ClipData.Item(Uri.parse("content://images/${names.first()}")), |
| 208 | + ).apply { |
| 209 | + names.drop(1).forEach { addItem(ClipData.Item(Uri.parse("content://images/$it"))) } |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + private fun dragEvent(clip: ClipData, action: Int = DragEvent.ACTION_DROP): DragEvent = |
| 214 | + DragEventBuilder.newBuilder() |
| 215 | + .setAction(action) |
| 216 | + .setX(400f) |
| 217 | + .setY(10f) |
| 218 | + .setClipDescription(clip.description) |
| 219 | + .setClipData(if (action == DragEvent.ACTION_DROP) clip else null) |
| 220 | + .build() |
| 221 | + |
| 222 | + @Test |
| 223 | + @Config(sdk = [24, 36]) |
| 224 | + fun imageDropAttachesEveryImageOnceWithoutInsertingUriText() { |
| 225 | + val target = dropEditor() |
| 226 | + val received = mutableListOf<List<String>>() |
| 227 | + target.pasteImagesListener = { received.add(it) } |
| 228 | + val clip = imageClip("screenshot.png", "photo.jpg") |
| 229 | + |
| 230 | + assertTrue(target.onDragEvent(dragEvent(clip, DragEvent.ACTION_DRAG_STARTED))) |
| 231 | + assertTrue(target.onDragEvent(dragEvent(clip))) |
| 232 | + assertEquals( |
| 233 | + listOf(listOf("content://images/screenshot.png", "content://images/photo.jpg")), |
| 234 | + received |
| 235 | + ) |
| 236 | + assertEquals("before", target.text.toString()) |
| 237 | + } |
| 238 | + |
| 239 | + @Test |
| 240 | + @Config(sdk = [24, 36]) |
| 241 | + fun imageDropIsRejectedWhenTheComposerLocksDuringTheDrag() { |
| 242 | + val target = dropEditor() |
| 243 | + target.pasteImagesListener = { error("A locked composer must not receive images") } |
| 244 | + val clip = imageClip("screenshot.png") |
| 245 | + assertTrue(target.onDragEvent(dragEvent(clip, DragEvent.ACTION_DRAG_STARTED))) |
| 246 | + target.readOnly = true |
| 247 | + |
| 248 | + assertFalse(target.onDragEvent(dragEvent(clip))) |
| 249 | + assertEquals("before", target.text.toString()) |
| 250 | + } |
| 251 | + |
| 252 | + @Test |
| 253 | + @Config(sdk = [24, 36]) |
| 254 | + fun disabledComposerRejectsImageDrags() { |
| 255 | + val target = dropEditor() |
| 256 | + target.isEnabled = false |
| 257 | + target.pasteImagesListener = { error("A disabled composer must not receive images") } |
| 258 | + val clip = imageClip("screenshot.png") |
| 259 | + |
| 260 | + assertFalse(target.onDragEvent(dragEvent(clip, DragEvent.ACTION_DRAG_STARTED))) |
| 261 | + assertFalse(target.onDragEvent(dragEvent(clip))) |
| 262 | + assertEquals("before", target.text.toString()) |
| 263 | + } |
| 264 | + |
| 265 | + @Test |
| 266 | + @Config(sdk = [24, 36]) |
| 267 | + fun mixedImageAndTextDropDoesNotSilentlyDiscardItems() { |
| 268 | + val target = dropEditor() |
| 269 | + target.pasteImagesListener = { error("Mixed drops must not partially attach") } |
| 270 | + val clip = imageClip("screenshot.png").apply { addItem(ClipData.Item("hello")) } |
| 271 | + |
| 272 | + assertFalse(target.onDragEvent(dragEvent(clip))) |
| 273 | + assertEquals("before", target.text.toString()) |
| 274 | + } |
| 275 | + |
| 276 | + @Test |
| 277 | + @Config(sdk = [24, 36]) |
| 278 | + fun unreadableImageDropDoesNotCrashOrInsertUriText() { |
| 279 | + val target = dropEditor() |
| 280 | + target.pasteImagesListener = { error("Unreadable images must not attach") } |
| 281 | + |
| 282 | + assertFalse(target.onDragEvent(dragEvent(imageClip("denied.png")))) |
| 283 | + assertEquals("before", target.text.toString()) |
| 284 | + } |
| 285 | + |
| 286 | + @Test |
| 287 | + @Config(sdk = [24, 36]) |
| 288 | + fun ordinaryTextDropsKeepNativeInsertion() { |
| 289 | + val target = dropEditor() |
| 290 | + target.pasteImagesListener = { error("Text drops must stay native") } |
| 291 | + |
| 292 | + assertTrue(target.onDragEvent(dragEvent(ClipData.newPlainText("text", "hello")))) |
| 293 | + assertEquals("beforehello", target.text.toString()) |
| 294 | + } |
| 295 | + |
| 296 | + private class DropImageProvider : ContentProvider() { |
| 297 | + override fun onCreate() = true |
| 298 | + override fun getType(uri: Uri): String { |
| 299 | + if (uri.lastPathSegment == "denied.png") throw SecurityException("No image read grant") |
| 300 | + return if (uri.lastPathSegment?.endsWith(".jpg") == true) "image/jpeg" else "image/png" |
| 301 | + } |
| 302 | + override fun query( |
| 303 | + uri: Uri, |
| 304 | + projection: Array<out String>?, |
| 305 | + selection: String?, |
| 306 | + selectionArgs: Array<out String>?, |
| 307 | + sortOrder: String? |
| 308 | + ): Cursor? = null |
| 309 | + override fun insert(uri: Uri, values: ContentValues?): Uri? = null |
| 310 | + override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?) = 0 |
| 311 | + override fun update( |
| 312 | + uri: Uri, |
| 313 | + values: ContentValues?, |
| 314 | + selection: String?, |
| 315 | + selectionArgs: Array<out String>? |
| 316 | + ) = 0 |
| 317 | + } |
172 | 318 | } |
0 commit comments