Deck Picker errors after a state save are lost on task restoration

Open
#23,768 0 comments 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
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
android, kotlin
Domain
mobile

Research direction

Start with the proposed test, AnkiDroid/src/test/java/com/ichi2/anki/DeckPickerUnburyErrorAfterSaveTest.kt, which drives DeckPicker's viewModel.unburyDeck through a state save and restore. Trace how the ViewModel's failure is handled after the activity is paused and saved, since the restored activity uses a new ViewModel. Done when that test passes, so the restored DeckPicker shows the backend error dialog.

Written by the indexing model from the issue text.

Description

code-scan Needs Triage

Assisted-by: GPT-6.2-Sol xhigh & GPT-6.2-Astra xhigh

[EXISTING] Deck Picker errors after a state save are lost on task restoration
Subject: [EXISTING] Deck Picker errors after a state save are lost on task restoration

A Deck Picker action that fails after Android saved the task state, such as Unbury, is never reported once the task is restored.

⚠️ 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/DeckPickerUnburyErrorAfterSaveTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/DeckPickerUnburyErrorAfterSaveTest.kt
new file mode 100644
index 0000000000..495890cae5
--- /dev/null
+++ b/AnkiDroid/src/test/java/com/ichi2/anki/DeckPickerUnburyErrorAfterSaveTest.kt
@@ -0,0 +1,103 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package com.ichi2.anki
+
+import android.os.Bundle
+import androidx.appcompat.app.AlertDialog
+import androidx.core.content.edit
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import anki.backend.BackendError
+import anki.backend.backendError
+import anki.collection.OpChanges
+import anki.scheduler.UnburyDeckRequest
+import com.ichi2.anki.common.preferences.sharedPrefs
+import com.ichi2.testutils.BackupManagerTestUtilities
+import com.ichi2.testutils.rules.BackendOverrideRule
+import kotlinx.coroutines.Dispatchers
+import net.ankiweb.rsdroid.Backend
+import net.ankiweb.rsdroid.exceptions.BackendIoException
+import org.junit.Rule
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.Robolectric
+import org.robolectric.shadows.ShadowDialog
+import java.util.concurrent.CountDownLatch
+import java.util.concurrent.TimeUnit
+import kotlin.test.assertEquals
+import kotlin.test.assertFalse
+import kotlin.test.assertNotSame
+import kotlin.test.assertTrue
+
+@RunWith(AndroidJUnit4::class)
+class DeckPickerUnburyErrorAfterSaveTest : RobolectricTest() {
+    @get:Rule
+    val backendOverride = BackendOverrideRule()
+
+    @Test
+    fun `a failure after state save survives task restoration`() {
+        val failure =
+            BackendIoException(
+                backendError {
+                    kind = BackendError.Kind.IO_ERROR
+                    message = "Could not unbury"
+                },
+            )
+        val unburyStarted = CountDownLatch(1)
+        val finishUnbury = CountDownLatch(1)
+        backendOverride.replaceWith {
+            object : Backend() {
+                override fun unburyDeck(
+                    deckId: Long,
+                    mode: UnburyDeckRequest.Mode,
+                ): OpChanges {
+                    unburyStarted.countDown()
+                    check(finishUnbury.await(30, TimeUnit.SECONDS))
+                    throw failure
+                }
+            }
+        }
+        ensureCollectionLoadIsSynchronous()
+        setIntroductionSlidesShown(true)
+        BackupManagerTestUtilities.setupSpaceForBackup(targetContext)
+        targetContext.sharedPrefs().edit { putBoolean("backupPromptDisabled", true) }
+        throwOnShowError = false
+
+        val controller = Robolectric.buildActivity(DeckPicker::class.java).setup()
+        saveControllerForCleanup(controller)
+        val picker = controller.get()
+        val originalViewModel = picker.viewModel
+        picker.awaitStartup()
+        val originalDispatcher =
+            CollectionManager.setTestDispatcher(Dispatchers.IO.limitedParallelism(1), useReentrantLock = false)
+        val savedState = Bundle()
+        try {
+            val unbury = originalViewModel.unburyDeck(1)
+            advanceRobolectricLooperUntil { unburyStarted.count == 0L }
+            controller.pause().saveInstanceState(savedState).stop()
+            assertFalse(unbury.isCompleted, "Unbury should still be running when state is saved")
+
+            finishUnbury.countDown()
+            advanceRobolectricLooperUntil { unbury.isCompleted }
+            assertFalse(unbury.isCancelled, "The original ViewModel should have caught the backend failure")
+            assertFalse(picker.isFinishing)
+            assertFalse(picker.isChangingConfigurations)
+            controller.destroy()
+        } finally {
+            finishUnbury.countDown()
+            CollectionManager.setTestDispatcher(originalDispatcher)
+        }
+
+        val restored = Robolectric.buildActivity(DeckPicker::class.java).setup(savedState)
+        saveControllerForCleanup(restored)
+        val restoredPicker = restored.get()
+        assertNotSame(originalViewModel, restoredPicker.viewModel)
+        restoredPicker.awaitStartup()
+        advanceRobolectricLooper()
+
+        assertTrue(
+            (ShadowDialog.getLatestDialog() as? AlertDialog)?.isShowing == true,
+            "The restored task should show the failure that occurred after state was saved",
+        )
+        assertEquals(failure.localizedMessage, getAlertDialogText(checkDismissed = true))
+    }
+}

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

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

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.