[DEV] Attachment choices stop working after editor recreation

Open
#23,204 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
52/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
android, kotlin
Domain
mobile

Research direction

Start with the regression test in AnkiDroid/src/test/java/com/ichi2/anki/noteeditor/NoteEditorMultimediaControllerTest.kt, which reproduces the issue by recreating the Card Browser while the attachment menu is open and choosing the recording action. Trace how the editor restores the menu and its target field after recreation. Done means the selected action launches its screen and keeps the original field selected.

Written by the indexing model from the issue text.

Description

Help Wanted

Assisted-by: GPT-6-Astra xhigh & GPT-6.1-Sol xhigh

Subject: [EXISTING] Attachment choices stop working after editor recreation

Recreating the split Card Browser with its attachment menu open leaves every option unable to launch its screen.

⚠️ 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/NoteEditorMultimediaControllerTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/noteeditor/NoteEditorMultimediaControllerTest.kt
index 8187addde1adbf1da019b2a627a54313858b15d3..466b5344f4e344df96d0bf10567da67f64df4e7c 100644
--- a/AnkiDroid/src/test/java/com/ichi2/anki/noteeditor/NoteEditorMultimediaControllerTest.kt
+++ b/AnkiDroid/src/test/java/com/ichi2/anki/noteeditor/NoteEditorMultimediaControllerTest.kt
@@ -8,11 +8,15 @@ import android.content.Intent
 import android.net.Uri
 import android.os.Bundle
 import android.provider.MediaStore
+import android.view.View
+import android.view.ViewGroup
 import androidx.core.net.toUri
 import androidx.test.ext.junit.runners.AndroidJUnit4
+import com.ichi2.anki.CardBrowser
 import com.ichi2.anki.CollectionManager
 import com.ichi2.anki.NoteEditorActivity
 import com.ichi2.anki.NoteEditorFragment
+import com.ichi2.anki.R
 import com.ichi2.anki.RobolectricTest
 import com.ichi2.anki.common.destinations.NoteEditorDestination
 import com.ichi2.anki.compat.CompatHelper.Companion.getSerializableCompat
@@ -22,7 +26,10 @@ import com.ichi2.anki.multimedia.MultimediaBottomSheet
 import com.ichi2.anki.multimedia.MultimediaResult
 import com.ichi2.anki.multimediacard.fields.ImageField
 import com.ichi2.anki.noteeditor.NoteEditorMultimediaController.Companion.STATE_KEY_IMAGE_CACHE
+import com.ichi2.anki.setCardBrowserFragmented
 import com.ichi2.anki.throwOnShowError
+import com.ichi2.anki.waitForSearchResults
+import com.ichi2.testutils.withSplitPaneUi
 import io.mockk.every
 import io.mockk.mockkConstructor
 import io.mockk.verify
@@ -270,6 +277,39 @@ class NoteEditorMultimediaControllerTest : RobolectricTest() {
         }
     }
 
+    @Test
+    fun `restored attachment menu keeps its target field`() =
+        withSplitPaneUi {
+            ensureCollectionLoadIsSynchronous()
+            setCardBrowserFragmented(true)
+            addBasicNote("front", "back")
+            val controller = startActivityControllerNormallyOpenCollectionWithIntent(CardBrowser::class.java, Intent())
+            controller.get().viewModel.setQuery("deck:Default", forceRefresh = true)
+            controller.get().waitForSearchResults()
+            advanceRobolectricLooper()
+            val editor = requireNotNull(controller.get().fragment)
+            editor
+                .requireView()
+                .findViewById<ViewGroup>(R.id.CardEditorEditFieldsLayout)
+                .getChildAt(1)
+                .findViewById<View>(R.id.media_button)
+                .performClick()
+            advanceRobolectricLooper()
+
+            controller.recreate()
+            advanceRobolectricLooper()
+            val restoredEditor = requireNotNull(controller.get().fragment)
+            val sheet = restoredEditor.parentFragmentManager.findFragmentByTag("MultimediaBottomSheet") as MultimediaBottomSheet
+            assertThat("the attachment menu is restored", sheet.requireDialog().isShowing, equalTo(true))
+            sheet.requireView().findViewById<View>(R.id.multimedia_action_recording).performClick()
+            advanceRobolectricLooper()
+
+            val started = shadowOf(controller.get()).nextStartedActivity
+            assertThat("the chosen recorder opens", started?.component?.className, equalTo(MultimediaActivity::class.java.name))
+            val argsFile = started!!.extras!!.getSerializableCompat<File>(MultimediaActivity.EXTRA_FRAGMENT_ARGS)!!
+            assertThat("the original field remains selected", MultimediaArgsStorage.create(targetContext).read(argsFile).index, equalTo(1))
+        }
+
     private fun openNoteEditor(): NoteEditorFragment {
         ensureCollectionLoadIsSynchronous()
         return openNoteEditorWithArgs(NoteEditorFragment.addNoteArgs())

Originally posted by @david-allison in https://github.com/ankidroid/Anki-Android/pull/23200#pullrequestreview-5865033422

Dominant language
Kotlin
Stars
12.3k
Forks
3.1k
Avg merge
1d 11h
Merged PRs (30d)
383

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ankidroid/Anki-Android

All issues in ankidroid/Anki-Android

Similar issues

More Kotlin issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.