Mixed drops insert audio as an image

Aperta
#23,388 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
56/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
android, kotlin
Ambito
mobile

Direzione di ricerca

The issue includes a reproduction test in AnkiDroid/src/test/java/com/ichi2/anki/noteeditor/NoteEditorDropMixedMediaTest.kt and points to content handling in the note editor. Start by tracing how NoteEditorFragment receives mixed drag and drop content, then run the added test with both image and audio items. Done means the image is inserted as an image and the audio as [sound:...]; the issue notes this predates the referenced pull request and may need a separate issue.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Needs Triage

Assisted-by: Claude Opus 5.5 max

Subject: [EXISTING] Mixed drops insert audio as an image

Dropping an image and an audio file together inserts the audio as an image tag; with Paste as PNG on it is dropped.

⚠️ This issue existed before the PR and should likely be split off into a separate issue.

---
diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/noteeditor/NoteEditorDropMixedMediaTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/noteeditor/NoteEditorDropMixedMediaTest.kt
new file mode 100644
index 0000000000..85c8449237
--- /dev/null
+++ b/AnkiDroid/src/test/java/com/ichi2/anki/noteeditor/NoteEditorDropMixedMediaTest.kt
@@ -0,0 +1,75 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package com.ichi2.anki.noteeditor
+
+import android.content.ClipData
+import android.content.ClipDescription
+import android.content.ContentProvider
+import android.database.MatrixCursor
+import android.graphics.Bitmap
+import android.provider.OpenableColumns
+import androidx.core.graphics.createBitmap
+import androidx.core.net.toUri
+import androidx.core.view.ContentInfoCompat
+import androidx.core.view.ViewCompat
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import com.ichi2.anki.NoteEditorActivity
+import com.ichi2.anki.RobolectricTest
+import com.ichi2.anki.common.destinations.NoteEditorDestination
+import com.ichi2.anki.common.utils.ext.pasteImagesAsPng
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.mockito.kotlin.mock
+import org.mockito.kotlin.whenever
+import org.robolectric.Shadows.shadowOf
+import org.robolectric.annotation.GraphicsMode
+import org.robolectric.shadows.ShadowContentResolver
+import java.io.ByteArrayOutputStream
+import kotlin.test.assertContains
+
+@RunWith(AndroidJUnit4::class)
+@GraphicsMode(GraphicsMode.Mode.NATIVE)
+class NoteEditorDropMixedMediaTest : RobolectricTest() {
+    override fun getCollectionStorageMode() = CollectionStorageMode.ON_DISK
+
+    @Test
+    fun `dropping an image with audio inserts the audio as sound`() =
+        runTest {
+            col.config.pasteImagesAsPng = false
+            val note = addBasicNote("Front", "Back")
+            val editor =
+                startRegularActivity<NoteEditorActivity>(
+                    NoteEditorDestination.EditNoteFromPreviewer(note.firstCard().id).toIntent(targetContext),
+                ).getNoteEditorFragment()
+            val image = "content://files/photo.jpg".toUri()
+            val audio = "content://files/song.mp3".toUri()
+            val imageData =
+                ByteArrayOutputStream().use {
+                    createBitmap(1, 1).compress(Bitmap.CompressFormat.JPEG, 100, it)
+                    it.toByteArray()
+                }
+            val provider = mock<ContentProvider>()
+            whenever(provider.query(image, null, null, null, null))
+                .thenReturn(MatrixCursor(arrayOf(OpenableColumns.DISPLAY_NAME)).apply { addRow(arrayOf("photo.jpg")) })
+            whenever(provider.query(audio, null, null, null, null))
+                .thenReturn(MatrixCursor(arrayOf(OpenableColumns.DISPLAY_NAME)).apply { addRow(arrayOf("song.mp3")) })
+            whenever(provider.getType(image)).thenReturn("image/jpeg")
+            whenever(provider.getType(audio)).thenReturn("audio/mpeg")
+            ShadowContentResolver.registerProviderInternal("files", provider)
+            shadowOf(targetContext.contentResolver).registerInputStreamSupplier(image) { imageData.inputStream() }
+            shadowOf(targetContext.contentResolver).registerInputStreamSupplier(audio) { ByteArray(64) { it.toByte() }.inputStream() }
+            val clip =
+                ClipData(ClipDescription("files", arrayOf("image/jpeg", "audio/mpeg")), ClipData.Item(image)).apply {
+                    addItem(ClipData.Item(audio))
+                }
+            val payload = ContentInfoCompat.Builder(clip, ContentInfoCompat.SOURCE_DRAG_AND_DROP).build()
+
+            val field = editor.editFieldAt(1)!!
+            ViewCompat.performReceiveContent(field, payload)
+            advanceRobolectricLooper()
+
+            val text = field.text.toString()
+            assertContains(text, "<img src=\"photo", message = "the photo is inserted as an image")
+            assertContains(text, "[sound:song", message = "the audio is inserted as sound, not as an image")
+        }
+}

Originally posted by @criticalAY in https://github.com/ankidroid/Anki-Android/pull/23078#pullrequestreview-5944085365

Lingua principale
Kotlin
Stelle
12.3k
Fork
3.1k
Merge medio
1g 11h
PR unite (30g)
383

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ankidroid/Anki-Android

Tutte le issue di ankidroid/Anki-Android

Issue simili

Altre issue su Kotlin

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.