Skip to content

fix(card-template): reject blank card type names - #22117

Open
CapThunder19 wants to merge 1 commit into
ankidroid:mainfrom
CapThunder19:fix/blank-card-type-name
Open

CapThunder19 wants to merge 1 commit into
ankidroid:mainfrom
CapThunder19:fix/blank-card-type-name

Conversation

@CapThunder19

@CapThunder19 CapThunder19 commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Note

Assisted-by: Claude sonnet 5 (tests)

Purpose / Description

Renaming a card type to only spaces (or only " characters) was accepted by the dialog, but saving the note type then failed with "Empty template name" / "Template name contains only quotes", and the other template changes were lost.

Fixes

Approach

The Rename button is now disabled when the name is blank after removing ", the same as for an empty field. This matches Anki Desktop, which ignores blank renames (clayout.py, onRename).

How Has This Been Tested?

  • Added RenameCardTypeDialogTest: blank and quotes-only names are rejected, and a normal name is still accepted and trimmed. The two rejection tests failed before the fix.
  • CardTemplateEditorTest, CardTemplateNotetypeTest and the notetype tests pass, and so does ktlintCheck.

Checklist

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

@david-allison

Copy link
Copy Markdown
Member
  • Physical device: <your phone model>, Android <version>. Rename stays disabled for spaces or "", and a normal rename still saves.

uhhh?

@david-allison david-allison left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least as a start (GPT-6)

Subject: [PATCH] test: use withRenameDialog for dialog assertions
---
Index: AnkiDroid/src/test/java/com/ichi2/anki/notetype/RenameCardTypeDialogTest.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/notetype/RenameCardTypeDialogTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/notetype/RenameCardTypeDialogTest.kt
--- a/AnkiDroid/src/test/java/com/ichi2/anki/notetype/RenameCardTypeDialogTest.kt	(revision 52fbf9855269bf726ab3a701817eb2a33e8be6c3)
+++ b/AnkiDroid/src/test/java/com/ichi2/anki/notetype/RenameCardTypeDialogTest.kt	(revision bd699834bf74ebc064314b56c27d1853a993c54f)
@@ -21,33 +21,33 @@
     private var renamedTo: CardTypeName? = null
 
     @Test
-    fun `whitespace-only name is rejected`() {
-        val dialog = showRenameDialog()
-        dialog.getInputField().setText("   ")
-        assertFalse(dialog.positiveButton.isEnabled, "Rename should be disabled for a blank name")
-    }
+    fun `whitespace-only name is rejected`() =
+        withRenameDialog {
+            getInputField().setText("   ")
+            assertFalse(positiveButton.isEnabled, "Rename should be disabled for a blank name")
+        }
 
     @Test
-    fun `quotes-only name is rejected`() {
-        val dialog = showRenameDialog()
-        dialog.getInputField().setText("\"\"")
-        assertFalse(dialog.positiveButton.isEnabled, "Rename should be disabled for a quotes-only name")
+    fun `quotes-only name is rejected`() =
+        withRenameDialog {
+            getInputField().setText("\"\"")
+            assertFalse(positiveButton.isEnabled, "Rename should be disabled for a quotes-only name")
 
-        dialog.getInputField().setText(" \" \" ")
-        assertFalse(dialog.positiveButton.isEnabled, "Rename should be disabled for quotes and whitespace")
-    }
+            getInputField().setText(" \" \" ")
+            assertFalse(positiveButton.isEnabled, "Rename should be disabled for quotes and whitespace")
+        }
 
     @Test
-    fun `valid name is accepted`() {
-        val dialog = showRenameDialog()
-        dialog.getInputField().setText("  Reverse  ")
-        assertTrue(dialog.positiveButton.isEnabled, "Rename should be enabled for a valid name")
+    fun `valid name is accepted`() =
+        withRenameDialog {
+            getInputField().setText("  Reverse  ")
+            assertTrue(positiveButton.isEnabled, "Rename should be enabled for a valid name")
 
-        dialog.positiveButton.performClick()
-        assertThat(renamedTo?.value, equalTo("Reverse"))
-    }
+            positiveButton.performClick()
+            assertThat(renamedTo?.value, equalTo("Reverse"))
+        }
 
-    private fun showRenameDialog(): AlertDialog {
+    private fun withRenameDialog(block: AlertDialog.() -> Unit) {
         val currentName = CardTypeName.fromString("Card 1")
         RenameCardTypeDialog.showInstance(
             startRegularActivity<AnkiActivity>(),
@@ -55,6 +55,6 @@
             currentName = currentName,
             existingNames = listOf(currentName),
         ) { renamedTo = it }
-        return ShadowDialog.getLatestDialog() as AlertDialog
+        block(ShadowDialog.getLatestDialog() as AlertDialog)
     }
 }

@CapThunder19
CapThunder19 force-pushed the fix/blank-card-type-name branch from 0984acf to 0dae7c4 Compare September 27, 2026 11:47
@CapThunder19

Copy link
Copy Markdown
Contributor Author

@david-allison applied the test changes and also fixed the description sorry about the leftover placeholder

Comment thread AnkiDroid/src/test/java/com/ichi2/anki/notetype/RenameCardTypeDialogTest.kt Outdated

@mikehardy mikehardy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tight dialog guard and for applying the test-helper changes.

The production change is the right one: allowEmpty = false only treats isEmpty(), so whitespace/quotes-only still enabled Rename and the failure showed up on save (Fixes #22114). Matching desktop’s “blank after stripping "” via ValidationResult.REJECTED is correct. No #22024-style R.string compile issue on this HEAD.

Request changes (AI_POLICY)

Tests are disclosed as Claude-assisted in the PR note, but commit b026d15d has no Assisted-by: trailer, and Claude sonnet is not a tool version. Please amend/rebase, e.g.

fix(card-template): reject blank card type names

Assisted-by: Claude Sonnet 4.x [tests]

Local on rebased tip: lint / package / emulator green. Full unit suite only hit unrelated PrefsSearchBar / ReminderLogTree order pollution (solo probes for those + RenameCardTypeDialogTest all green).

@mikehardy mikehardy added the Needs Author Reply Waiting for a reply from the original author label Sep 30, 2026
Assisted-by: Claude Sonnet 5 [tests]
@CapThunder19
CapThunder19 force-pushed the fix/blank-card-type-name branch from b026d15 to 72d3d1e Compare October 1, 2026 05:53
@CapThunder19
CapThunder19 requested a review from mikehardy October 1, 2026 05:54
@mikehardy mikehardy added Needs Second Approval Has one approval, one more approval to merge and removed Needs Author Reply Waiting for a reply from the original author Needs Review labels Oct 1, 2026

@mikehardy mikehardy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Anirudh 👋

Thanks for adding the Assisted-by: trailer with a tool version. That was the only hold from last time.

The production change still looks right to me: allowEmpty = false only catches isEmpty(), so whitespace/quotes-only still enabled Rename and the failure showed up on save (Fixes #22114). Rejecting after trim + strip " matches desktop’s blank-rename ignore. Tests cover the cases that used to bite.

Happy to Approve. Needs a second reviewer.

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Second Approval Has one approval, one more approval to merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rename card type accepts a name with only spaces, then saving fails

3 participants