forked from ankidroid/Anki-Android
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(empty-cards): undoable & correct result
* We use the backend string * We use `undoableOp` * we add 'undo' to the snackbar * We return the backend result, which may not be the supplied input - unit tested: duplicates empty_cards_deleted => TR.emptyCardsDeletedCount Fixes 16945 https://redirect.github.com/ankitects/anki/pull/3386
- Loading branch information
1 parent
e35b5b9
commit 3267a6b
Showing
5 changed files
with
147 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
AnkiDroid/src/test/java/com/ichi2/anki/deckpicker/DeckPickerViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright (c) 2025 David Allison <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free Software | ||
* Foundation; either version 3 of the License, or (at your option) any later | ||
* version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.ichi2.anki.deckpicker | ||
|
||
import androidx.annotation.CheckResult | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import app.cash.turbine.test | ||
import com.ichi2.anki.RobolectricTest | ||
import com.ichi2.libanki.CardId | ||
import com.ichi2.libanki.undoStatus | ||
import com.ichi2.testutils.ensureOpsExecuted | ||
import org.hamcrest.CoreMatchers.not | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.empty | ||
import org.hamcrest.Matchers.equalTo | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import timber.log.Timber | ||
|
||
/** Test of [DeckPickerViewModel] */ | ||
@RunWith(AndroidJUnit4::class) | ||
class DeckPickerViewModelTest : RobolectricTest() { | ||
private val viewModel = DeckPickerViewModel() | ||
|
||
@Test | ||
fun `empty cards - flow`() = | ||
runTest { | ||
val cardsToEmpty = createEmptyCards() | ||
|
||
viewModel.emptyCardsNotification.test { | ||
// test a 'normal' deletion | ||
viewModel.deleteEmptyCards(cardsToEmpty).join() | ||
|
||
expectMostRecentItem().also { | ||
assertThat("cards deleted", it.cardsDeleted, equalTo(1)) | ||
} | ||
|
||
// ensure a duplicate output is displayed to the user | ||
val newCardsToEmpty = createEmptyCards() | ||
viewModel.deleteEmptyCards(newCardsToEmpty).join() | ||
|
||
expectMostRecentItem().also { | ||
assertThat("cards deleted: duplicate output", it.cardsDeleted, equalTo(1)) | ||
} | ||
|
||
// send the same collection in, but with the same ids. | ||
// the output should only show 1 card deleted | ||
val emptyCardsSentTwice = createEmptyCards() | ||
viewModel.deleteEmptyCards(emptyCardsSentTwice + emptyCardsSentTwice).join() | ||
|
||
expectMostRecentItem().also { | ||
assertThat("cards deleted: duplicate input", it.cardsDeleted, equalTo(1)) | ||
} | ||
|
||
// test an empty list: a no-op should inform the user, rather than do nothing | ||
viewModel.deleteEmptyCards(listOf()).join() | ||
|
||
expectMostRecentItem().also { | ||
assertThat("'no cards deleted' is notified", it.cardsDeleted, equalTo(0)) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `empty cards - undoable`() = | ||
runTest { | ||
val cardsToEmpty = createEmptyCards() | ||
|
||
// ChangeManager assert | ||
ensureOpsExecuted(1) { | ||
viewModel.deleteEmptyCards(cardsToEmpty).join() | ||
} | ||
|
||
// backend assert | ||
assertThat("col undo status", col.undoStatus().undo, equalTo("Empty Cards")) | ||
} | ||
|
||
@CheckResult | ||
private suspend fun createEmptyCards(): List<CardId> { | ||
addNoteUsingNoteTypeName("Cloze", "{{c1::Hello}} {{c2::World}}", "").apply { | ||
setField(0, "{{c1::Hello}}") | ||
col.updateNote(this) | ||
} | ||
return viewModel.findEmptyCards().await().also { cardsToEmpty -> | ||
assertThat("there are empty cards", cardsToEmpty, not(empty())) | ||
Timber.d("created %d empty cards: [%s]", cardsToEmpty.size, cardsToEmpty) | ||
} | ||
} | ||
} |