study-screen: issues when custom scheduler callback takes too long

Open
#23,284 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

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

Research direction

The two proposed tests are in AnkiDroid/src/test/java/com/ichi2/anki/ui/windows/reviewer/ReviewerFragmentTest.kt, and the scenario depends on the custom scheduler set through col.config.cardStateCustomizer and on viewModel.onStateMutationCallback. Start by running the existing reviewer tests, then add the pause/stop/resume scenario for the queued Edit command and the typed-answer scenario. Done means both new tests pass against a real fix in the reviewer view model, and the work should be split because the typed-answer case predates the PR.

Written by the indexing model from the issue text.

Description

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

Patches are ordered; the second builds on the first.

Queued Edit command lost while the reviewer is stopped
Subject: Queued Edit command lost while the reviewer is stopped

An Edit command queued behind custom scheduling disappears if scheduling completes while the reviewer is stopped.

---
diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/ui/windows/reviewer/ReviewerFragmentTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/ui/windows/reviewer/ReviewerFragmentTest.kt
index 88b888816c..ebcdfc6629 100644
--- a/AnkiDroid/src/test/java/com/ichi2/anki/ui/windows/reviewer/ReviewerFragmentTest.kt
+++ b/AnkiDroid/src/test/java/com/ichi2/anki/ui/windows/reviewer/ReviewerFragmentTest.kt
@@ -18,7 +18,10 @@ import com.ichi2.anki.R
 import com.ichi2.anki.RobolectricTest
 import com.ichi2.anki.cardviewer.Gesture
 import com.ichi2.anki.cardviewer.TypeAnswerWebView
+import com.ichi2.anki.common.destinations.NoteEditorDestination
+import com.ichi2.anki.common.destinations.toIntent
 import com.ichi2.anki.common.preferences.sharedPrefs
+import com.ichi2.anki.common.utils.ext.cardStateCustomizer
 import com.ichi2.anki.dialogs.tags.TagsDialog
 import com.ichi2.anki.preferences.reviewer.ViewerAction
 import com.ichi2.anki.preferences.reviewer.WhiteboardAction
@@ -72,6 +75,31 @@ class ReviewerFragmentTest : RobolectricTest() {
         }
     }
 
+    @Test
+    fun `queued edit survives backgrounding before custom scheduler completes`() =
+        runTest {
+            val cardId = addBasicNote().firstCard().id
+            col.config.cardStateCustomizer = "await new Promise(resolve => setTimeout(resolve, 5000));"
+
+            withReviewer { controller ->
+                viewModel.onShowAnswer()
+                viewModel.executeAction(ViewerAction.EDIT)
+                advanceUntilIdle()
+                assertFalse(viewModel.showingAnswer.value)
+
+                controller.pause().stop()
+                advanceUntilIdle()
+                viewModel.onStateMutationCallback()
+                advanceUntilIdle()
+                controller.start().resume()
+                advanceUntilIdle()
+                advanceRobolectricLooper()
+
+                val intent = assertNotNull(shadowOf(controller.get()).nextStartedActivity, "The accepted Edit command must not be lost")
+                assertEquals(NoteEditorDestination.EditNoteFromPreviewer(cardId).toIntent().component, intent.component)
+            }
+        }
+
     @Test
     fun `recreated WebView retains keyboard support`() =
         runTest {
[EXISTING] Typed answer discarded when the custom scheduler finishes in the background
Subject: [EXISTING] Typed answer discarded when the custom scheduler finishes in the background

Backgrounding after submitting a typed answer but before the custom scheduler completes discards the entered text and skips its comparison.

⚠️ 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/ui/windows/reviewer/ReviewerFragmentTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/ui/windows/reviewer/ReviewerFragmentTest.kt
index ebcdfc6629..8a30f656fe 100644
--- a/AnkiDroid/src/test/java/com/ichi2/anki/ui/windows/reviewer/ReviewerFragmentTest.kt
+++ b/AnkiDroid/src/test/java/com/ichi2/anki/ui/windows/reviewer/ReviewerFragmentTest.kt
@@ -14,6 +14,7 @@ import androidx.core.content.getSystemService
 import androidx.fragment.app.DialogFragment
 import androidx.test.ext.junit.runners.AndroidJUnit4
 import anki.scheduler.CardAnswer.Rating
+import app.cash.turbine.test
 import com.ichi2.anki.R
 import com.ichi2.anki.RobolectricTest
 import com.ichi2.anki.cardviewer.Gesture
@@ -41,6 +42,7 @@ import com.ichi2.anki.utils.ext.DIALOG_FRAGMENT_TAG
 import com.ichi2.testutils.RecordingInputMethodManager
 import com.ichi2.testutils.ext.addNoSuggestNote
 import com.ichi2.testutils.ext.createInputConnection
+import kotlinx.coroutines.flow.filter
 import kotlinx.coroutines.test.TestScope
 import kotlinx.coroutines.test.advanceUntilIdle
 import org.junit.After
@@ -100,6 +102,31 @@ class ReviewerFragmentTest : RobolectricTest() {
             }
         }
 
+    @Test
+    fun `pending typed answer survives backgrounding before custom scheduler completes`() =
+        runTest {
+            targetContext.sharedPrefs().edit { putBoolean("useInputTag", false) }
+            addBasicWithTypingNote("front", "back")
+            col.config.cardStateCustomizer = "await new Promise(resolve => setTimeout(resolve, 5000));"
+
+            withReviewer { controller ->
+                binding.typeAnswerEditText.setText("back")
+                viewModel.onShowAnswer()
+                advanceUntilIdle()
+                assertFalse(viewModel.showingAnswer.value)
+
+                controller.pause().stop()
+                advanceUntilIdle()
+
+                viewModel.eval.filter { it.startsWith("_showAnswer(") }.test {
+                    viewModel.onStateMutationCallback()
+                    advanceUntilIdle()
+                    assertContains(awaitItem(), "typeGood", message = "The submitted answer must retain the text entered before backgrounding")
+                    cancelAndIgnoreRemainingEvents()
+                }
+            }
+        }
+
     @Test
     fun `recreated WebView retains keyboard support`() =
         runTest {

The scheduler-completion callback was simulated in local lifecycle checks; this scenario was not exercised on an Android device.
The PR changed during review; findings apply to the version pinned at the start.

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

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.