Issue with completion feedback after studying unburied cards from Congrats

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

Research direction

The body is a patch adding AnkiDroid/src/test/java/com/ichi2/anki/UnburyStudyCompletionTest.kt, marked @ExistingIssue. Start by running that test, then follow the chain it drives: CongratsPage's "unbury" bridge command, StudyOptionsActivity forwarding the result, and Reviewer finishing. Done means the second review reopens CongratsPage and the @ExistingIssue annotation can be removed.

Written by the indexing model from the issue text.

Description

code-scan Needs Triage
Subject: [PATCH] [EXISTING] Show completion feedback after studying unburied cards from Congrats
---
Index: AnkiDroid/src/test/java/com/ichi2/anki/UnburyStudyCompletionTest.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/UnburyStudyCompletionTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/UnburyStudyCompletionTest.kt
new file mode 100644
--- /dev/null	(revision 7d325623fefa9fdcc5fe0099b9c9027db6739614)
+++ b/AnkiDroid/src/test/java/com/ichi2/anki/UnburyStudyCompletionTest.kt	(revision 7d325623fefa9fdcc5fe0099b9c9027db6739614)
@@ -0,0 +1,115 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package com.ichi2.anki
+
+import android.content.Intent
+import android.widget.Button
+import androidx.core.content.edit
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import anki.scheduler.CardAnswer.Rating
+import com.ichi2.anki.CollectionManager.withCol
+import com.ichi2.anki.pages.CongratsPage
+import com.ichi2.anki.settings.Prefs
+import com.ichi2.testutils.rules.ExistingIssue
+import com.ichi2.testutils.rules.ExistingIssueRule
+import kotlinx.coroutines.test.advanceUntilIdle
+import org.junit.Rule
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.Shadows.shadowOf
+import kotlin.test.assertEquals
+import kotlin.test.assertNotNull
+import kotlin.test.assertTrue
+
+@RunWith(AndroidJUnit4::class)
+class UnburyStudyCompletionTest : RobolectricTest() {
+    @get:Rule(order = Int.MIN_VALUE)
+    val existingIssue = ExistingIssueRule()
+
+    /**
+     * Using Unbury on Congrats and studying the unburied card should return to Congrats when
+     * the deck is finished again. Study Options forwards an activity result, but the flow
+     * currently starts without registering a receiver, so the second review closes silently.
+     */
+    @Test
+    @ExistingIssue
+    fun `finishing a review after unburying from Congrats reopens Congrats`() =
+        runTest {
+            Prefs.isNewStudyScreenEnabled = false
+            getPreferences().edit { putBoolean("new_congrats_screen", true) }
+            updateDeckConfig(col.decks.selected()) { autoplay = false }
+            val cardId = addBasicNote().firstCard().id
+            // A single manually buried card gives Congrats its Unbury action without a choice dialog.
+            withCol { sched.buryCards(listOf(cardId), manual = true) }
+            val congrats =
+                startActivityNormallyOpenCollectionWithIntent(
+                    SingleFragmentActivity::class.java,
+                    CongratsPage.getIntent(targetContext),
+                )
+            val congratsShadow = shadowOf(congrats)
+            val page = congrats.fragment as CongratsPage
+            // Run the actual bridge command that the Congrats page sends for Unbury.
+            page.bridgeCommands.getValue("unbury").invoke()
+            advanceRobolectricLooperUntil {
+                advanceUntilIdle()
+                congratsShadow.peekNextStartedActivity() != null
+            }
+            val studyOptionsLaunch = assertNotNull(congratsShadow.nextStartedActivityForResult)
+            assertEquals(StudyOptionsActivity::class.java.name, studyOptionsLaunch.intent.component?.className)
+            congratsShadow.clearNextStartedActivities()
+
+            val studyOptions =
+                startActivityNormallyOpenCollectionWithIntent(
+                    StudyOptionsActivity::class.java,
+                    studyOptionsLaunch.intent,
+                )
+            val studyOptionsShadow = shadowOf(studyOptions)
+            advanceRobolectricLooperUntil {
+                advanceUntilIdle()
+                studyOptions.findViewById<Button>(R.id.studyoptions_start)?.text ==
+                    studyOptions.getString(CommonString.studyoptions_start)
+            }
+            assertTrue(studyOptions.findViewById<Button>(R.id.studyoptions_start).performClick())
+            advanceRobolectricLooperUntil {
+                advanceUntilIdle()
+                studyOptionsShadow.peekNextStartedActivity() != null
+            }
+            val reviewLaunch = assertNotNull(studyOptionsShadow.nextStartedActivityForResult)
+            assertEquals(Reviewer::class.java.name, reviewLaunch.intent.component?.className)
+            studyOptionsShadow.clearNextStartedActivities()
+
+            val reviewer = startActivityNormallyOpenCollectionWithIntent(Reviewer::class.java, reviewLaunch.intent)
+            advanceRobolectricLooperUntil { reviewer.currentCardId == cardId }
+            reviewer.answerCardInner(Rating.EASY)
+            reviewer.updateCardAndRedraw()
+            advanceUntilIdle()
+            assertTrue(reviewer.isFinishing)
+
+            // Robolectric starts each activity explicitly. Deliver a registered activity result
+            // as Android would, including when Study Options forwards its caller's result.
+            val reviewerShadow = shadowOf(reviewer)
+            if (reviewLaunch.requestCode >= 0) {
+                studyOptions.activityResultRegistry.dispatchResult(
+                    reviewLaunch.requestCode,
+                    reviewerShadow.resultCode,
+                    reviewerShadow.resultIntent,
+                )
+            } else if (
+                reviewLaunch.intent.flags and Intent.FLAG_ACTIVITY_FORWARD_RESULT != 0 &&
+                studyOptionsLaunch.requestCode >= 0
+            ) {
+                congrats.activityResultRegistry.dispatchResult(
+                    studyOptionsLaunch.requestCode,
+                    reviewerShadow.resultCode,
+                    reviewerShadow.resultIntent,
+                )
+            }
+            advanceUntilIdle()
+            advanceRobolectricLooper()
+
+            // The saved launch chain has no receiver on the buggy revision, so none of these
+            // activities starts a second Congrats page when the reviewed card runs out.
+            val next = listOf(reviewerShadow, studyOptionsShadow, congratsShadow).firstNotNullOfOrNull { it.nextStartedActivity }
+            assertEquals(CongratsPage::class.java.name, next?.getStringExtra(SingleFragmentActivity.EXTRA_FRAGMENT_NAME))
+        }
+}
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.