Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,11 @@
package com.ichi2.anki

import com.ichi2.anki.libanki.Card
import com.ichi2.anki.libanki.Collection
import com.ichi2.anki.libanki.Note
import com.ichi2.utils.HashUtil.hashSetInit

/**
* Utilities for working on multiple cards
*/
object CardUtils {
/**
* @return List of corresponding notes without duplicates, even if the input list has multiple cards of the same note.
*/
fun getNotes(
col: Collection,
cards: kotlin.collections.Collection<Card>,
): Set<Note> {
val notes: MutableSet<Note> = hashSetInit(cards.size)
for (card in cards) {
notes.add(card.note(col))
}
return notes
}

/**
* Returns the deck ID of the given [Card].
*
Expand Down
65 changes: 65 additions & 0 deletions AnkiDroid/src/test/java/com/ichi2/anki/CardUtilsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright (c) 2026 Ayush <ayushdevraj9@gmail.com>

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

import androidx.test.ext.junit.runners.AndroidJUnit4
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class CardUtilsTest : RobolectricTest() {
@Test
fun getDeckIdForCard_regularDeck_returnsDid() {
val note = addBasicNote("Front", "Back")
val card = note.firstCard()

assertThat("Card should be in a regular deck", card.oDid, equalTo(0L))

val deckId = CardUtils.getDeckIdForCard(card)

assertThat("Should return the card's did", deckId, equalTo(card.did))
}

@Test
fun getDeckIdForCard_cramDeck_returnsODid() {
val note = addBasicNote("Front", "Back")
val card = note.firstCard()

val filteredDid = addDynamicDeck("Filtered")
col.sched.rebuildFilteredDeck(filteredDid)
card.load()

assertThat("Card should have oDid set in filtered deck", card.oDid != 0L, equalTo(true))

val deckId = CardUtils.getDeckIdForCard(card)

assertThat("Should return the original deck ID (oDid)", deckId, equalTo(card.oDid))
}

@Test
fun getDeckIdForCard_zeroODid_returnsDid() {
val note = addBasicNote("Front", "Back")
val card = note.firstCard()

assertThat("Card should be in a regular deck", card.oDid, equalTo(0L))

val deckId = CardUtils.getDeckIdForCard(card)

assertThat(deckId, equalTo(card.did))
}
}
Loading