Shift+Delete can remove a deck while the tablet pane loads

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

Research direction

The reproduction is the included DeckPickerStartupShortcutDeletionTest.kt, which pauses InitialActivity.getStartupFailureType while DeckPicker is in tablet mode. Start in DeckPicker's onKeyUp handling of KEYCODE_DEL with META_SHIFT_ON, and check what it does before the study options fragment exists. Done when the test passes: Shift+Delete shows the confirmation dialog naming the deck and the deck is not deleted before that.

Written by the indexing model from the issue text.

Description

code-scan

Assisted-by: GPT-6.2-Sol xhigh

[EXISTING] Shift+Delete can remove a deck while the tablet pane loads
Subject: [EXISTING] Shift+Delete can remove a deck while the tablet pane loads

Pressing Shift+Delete before the tablet study pane loads removes the selected deck and its cards without confirmation.

---
diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/DeckPickerStartupShortcutDeletionTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/DeckPickerStartupShortcutDeletionTest.kt
new file mode 100644
index 0000000000..a4125cc520
--- /dev/null
+++ b/AnkiDroid/src/test/java/com/ichi2/anki/DeckPickerStartupShortcutDeletionTest.kt
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package com.ichi2.anki
+
+import android.content.Intent
+import android.content.SharedPreferences
+import android.view.KeyEvent
+import androidx.appcompat.app.AlertDialog
+import androidx.core.content.edit
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import com.ichi2.anki.common.preferences.sharedPrefs
+import com.ichi2.anki.dialogs.DeckPickerConfirmDeleteDeckDialog
+import com.ichi2.anki.dialogs.utils.message
+import com.ichi2.testutils.BackupManagerTestUtilities
+import io.mockk.every
+import io.mockk.mockkObject
+import io.mockk.unmockkObject
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.Robolectric
+import org.robolectric.annotation.Config
+import java.util.concurrent.CountDownLatch
+import java.util.concurrent.TimeUnit.SECONDS
+import kotlin.test.assertContains
+import kotlin.test.assertNotNull
+import kotlin.test.assertNull
+import kotlin.test.assertTrue
+
+@RunWith(AndroidJUnit4::class)
+@Config(qualifiers = "xlarge")
+class DeckPickerStartupShortcutDeletionTest : RobolectricTest() {
+    @Test
+    fun `Shift Delete asks before deleting while the tablet pane is loading`() {
+        val deckName = "Selected deck"
+        val target = addDeck(deckName, setAsSelected = true)
+        addNoteToDeck(target)
+        setIntroductionSlidesShown(true)
+        BackupManagerTestUtilities.setupSpaceForBackup(targetContext)
+        targetContext.sharedPrefs().edit { putBoolean("backupPromptDisabled", true) }
+        val startupStarted = CountDownLatch(1)
+        val finishStartup = CountDownLatch(1)
+
+        mockkObject(InitialActivity)
+        try {
+            every { InitialActivity.getStartupFailureType(any<SharedPreferences>(), any<() -> Boolean>()) } answers {
+                startupStarted.countDown()
+                check(finishStartup.await(30, SECONDS)) { "Startup was not released" }
+                callOriginal()
+            }
+            val controller = Robolectric.buildActivity(DeckPicker::class.java, Intent()).setup()
+            saveControllerForCleanup(controller)
+            val picker = controller.get()
+            try {
+                assertTrue(startupStarted.await(10, SECONDS))
+                assertTrue(picker.fragmented)
+                assertNull(picker.supportFragmentManager.findFragmentById(R.id.studyoptions_fragment))
+
+                picker.onKeyUp(
+                    KeyEvent.KEYCODE_DEL,
+                    KeyEvent(0, 0, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL, 0, KeyEvent.META_SHIFT_ON),
+                )
+
+                fun confirmation() =
+                    picker.supportFragmentManager.fragments
+                        .filterIsInstance<DeckPickerConfirmDeleteDeckDialog>()
+                        .firstOrNull { it.dialog?.isShowing == true }
+
+                advanceRobolectricLooperUntil { confirmation() != null || col.decks.getLegacy(target) == null }
+                assertNotNull(col.decks.getLegacy(target), "The selected deck was deleted before its name was shown")
+                val dialog = assertNotNull(confirmation()).requireDialog() as AlertDialog
+                assertContains(dialog.message, deckName)
+            } finally {
+                finishStartup.countDown()
+                picker.viewModel.awaitStartup()
+            }
+        } finally {
+            finishStartup.countDown()
+            unmockkObject(InitialActivity)
+        }
+    }
+}

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

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

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.