Skip to content

Commit

Permalink
refactor: inline Card.flush() with updateCard()
Browse files Browse the repository at this point in the history
only used in tests
  • Loading branch information
BrayanDSO authored and mikehardy committed Nov 21, 2023
1 parent add1d1f commit c6fa351
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ class ContentProviderTest : InstrumentedTest() {
)
for (c in newNote.cards()) {
c.did = did
c.flush()
c.col.updateCard(c, skipUndoEntry = true)
}
return Uri.withAppendedPath(
FlashCardsContract.Note.CONTENT_URI,
Expand Down
31 changes: 0 additions & 31 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Card.kt
Original file line number Diff line number Diff line change
Expand Up @@ -211,37 +211,6 @@ open class Card : Cloneable {
return builder.build()
}

fun flush(changeModUsn: Boolean = true) {
if (changeModUsn) {
mod = TimeManager.time.intTime()
usn = col.usn()
}
assert(due < "4294967296".toLong())
col.db.execute(
"insert or replace into cards values " +
"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
this.id,
nid,
did,
ord,
mod,
usn,
this.type,
queue,
due,
ivl,
factor,
reps,
lapses,
left,
oDue,
oDid,
flags,
""
)
col.log(this)
}

fun question(reload: Boolean = false, browser: Boolean = false): String {
return renderOutput(reload, browser).questionAndStyle()
}
Expand Down
175 changes: 142 additions & 33 deletions AnkiDroid/src/test/java/com/ichi2/anki/CardBrowserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,26 @@ class CardBrowserTest : RobolectricTest() {
browser.rerenderAllCards()
// ASSERT
assertThat(browser.cardCount(), equalTo(6L))
assertThat("A checked card should have been removed", browser.checkedCardCount(), equalTo(3))
assertThat("Checked card before should not have changed", browser.hasCheckedCardAtPosition(1), equalTo(true))
assertThat("Checked card after should have changed by 2 places", browser.hasCheckedCardAtPosition(3), equalTo(true))
assertThat("Checked card after should have changed by 2 places", browser.hasCheckedCardAtPosition(4), equalTo(true))
assertThat(
"A checked card should have been removed",
browser.checkedCardCount(),
equalTo(3)
)
assertThat(
"Checked card before should not have changed",
browser.hasCheckedCardAtPosition(1),
equalTo(true)
)
assertThat(
"Checked card after should have changed by 2 places",
browser.hasCheckedCardAtPosition(3),
equalTo(true)
)
assertThat(
"Checked card after should have changed by 2 places",
browser.hasCheckedCardAtPosition(4),
equalTo(true)
)
}

@Test
Expand Down Expand Up @@ -218,7 +234,11 @@ class CardBrowserTest : RobolectricTest() {
val cardIds = b.checkedCardIds

for (cardId in cardIds) {
assertThat("Deck should have been changed yet", col.getCard(cardId).did, not(deckIdToChangeTo))
assertThat(
"Deck should have been changed yet",
col.getCard(cardId).did,
not(deckIdToChangeTo)
)
}

// act
Expand Down Expand Up @@ -269,19 +289,30 @@ class CardBrowserTest : RobolectricTest() {
val cardPosition = random.nextInt(numberOfNotes)
assumeThat("card position to select is 60", cardPosition, equalTo(60))
cardBrowser.checkCardsAtPositions(cardPosition)
assumeTrue("card at position 60 is selected", cardBrowser.hasCheckedCardAtPosition(cardPosition))
assumeTrue(
"card at position 60 is selected",
cardBrowser.hasCheckedCardAtPosition(cardPosition)
)

// flag the selected card with flag = 1
val flag = 1
cardBrowser.updateSelectedCardsFlag(flag)
// check if card flag turned to flag = 1
assertThat("Card should be flagged", getCheckedCard(cardBrowser).card.userFlag(), equalTo(flag))
assertThat(
"Card should be flagged",
getCheckedCard(cardBrowser).card.userFlag(),
equalTo(flag)
)

// unflag the selected card with flag = 0
val unflagFlag = 0
cardBrowser.updateSelectedCardsFlag(unflagFlag)
// check if card flag actually changed from flag = 1
assertThat("Card flag should be removed", getCheckedCard(cardBrowser).card.userFlag(), not(flag))
assertThat(
"Card flag should be removed",
getCheckedCard(cardBrowser).card.userFlag(),
not(flag)
)

// deselect and select all cards
cardBrowser.onSelectNone()
Expand Down Expand Up @@ -321,17 +352,26 @@ class CardBrowserTest : RobolectricTest() {
withNoWritePermission {
val inputIntent = Intent("android.intent.action.PROCESS_TEXT")

val browserController = Robolectric.buildActivity(CardBrowser::class.java, inputIntent).create()
val browserController =
Robolectric.buildActivity(CardBrowser::class.java, inputIntent).create()
val cardBrowser = browserController.get()
saveControllerForCleanup(browserController)

val shadowActivity = shadowOf(cardBrowser)
val outputIntent = shadowActivity.nextStartedActivity
val component = assertNotNull(outputIntent.component)

assertThat("Deck Picker currently handles permissions, so should be called", component.className, equalTo("com.ichi2.anki.DeckPicker"))
assertThat(
"Deck Picker currently handles permissions, so should be called",
component.className,
equalTo("com.ichi2.anki.DeckPicker")
)
assertThat("Activity should be finishing", cardBrowser.isFinishing)
assertThat("Activity should be cancelled as it did nothing", shadowActivity.resultCode, equalTo(Activity.RESULT_CANCELED))
assertThat(
"Activity should be cancelled as it did nothing",
shadowActivity.resultCode,
equalTo(Activity.RESULT_CANCELED)
)
}
}

Expand Down Expand Up @@ -379,7 +419,11 @@ class CardBrowserTest : RobolectricTest() {
b.checkCardsAtPositions(0)
val previewIntent = b.previewIntent
assertThat("before: index", previewIntent.getIntExtra("index", -100), equalTo(0))
assertThat("before: cards", previewIntent.getLongArrayExtra("cardList"), equalTo(longArrayOf(cid1, cid2)))
assertThat(
"before: cards",
previewIntent.getLongArrayExtra("cardList"),
equalTo(longArrayOf(cid1, cid2))
)

// reverse
b.changeCardOrder(1)
Expand All @@ -390,7 +434,11 @@ class CardBrowserTest : RobolectricTest() {
b.replaceSelectionWith(intArrayOf(0))
val intentAfterReverse = b.previewIntent
assertThat("after: index", intentAfterReverse.getIntExtra("index", -100), equalTo(0))
assertThat("after: cards", intentAfterReverse.getLongArrayExtra("cardList"), equalTo(longArrayOf(cid2, cid1)))
assertThat(
"after: cards",
intentAfterReverse.getLongArrayExtra("cardList"),
equalTo(longArrayOf(cid2, cid1))
)
}

/** 7420 */
Expand Down Expand Up @@ -418,7 +466,11 @@ class CardBrowserTest : RobolectricTest() {

val b = browserWithNoNewCards

assertThat("The target deck should not yet be selected", b.lastDeckId, not(equalTo(targetDid)))
assertThat(
"The target deck should not yet be selected",
b.lastDeckId,
not(equalTo(targetDid))
)

b.selectDeckAndSave(targetDid)

Expand Down Expand Up @@ -453,13 +505,21 @@ class CardBrowserTest : RobolectricTest() {

val card = getCheckedCard(b)

assertThat("Initial position of checked card", card.getColumnHeaderText(CardBrowser.Column.DUE), equalTo("1"))
assertThat(
"Initial position of checked card",
card.getColumnHeaderText(CardBrowser.Column.DUE),
equalTo("1")
)

b.repositionCardsNoValidation(listOf(card.id), 2)

card.reload()

assertThat("Position of checked card after reposition", card.getColumnHeaderText(CardBrowser.Column.DUE), equalTo("2"))
assertThat(
"Position of checked card after reposition",
card.getColumnHeaderText(CardBrowser.Column.DUE),
equalTo("2")
)
}

@Test
Expand All @@ -469,7 +529,7 @@ class CardBrowserTest : RobolectricTest() {
due = 5
queue = Consts.QUEUE_TYPE_REV
type = Consts.CARD_TYPE_REV
flush()
col.updateCard(this, skipUndoEntry = true)
}

val b = browserWithNoNewCards
Expand All @@ -478,13 +538,21 @@ class CardBrowserTest : RobolectricTest() {

val card = getCheckedCard(b)

assertThat("Initial due of checked card", card.getColumnHeaderText(CardBrowser.Column.DUE), equalTo("8/12/20"))
assertThat(
"Initial due of checked card",
card.getColumnHeaderText(CardBrowser.Column.DUE),
equalTo("8/12/20")
)

b.resetProgressNoConfirm(listOf(card.id))

card.reload()

assertThat("Position of checked card after reset", card.getColumnHeaderText(CardBrowser.Column.DUE), equalTo("2"))
assertThat(
"Position of checked card after reset",
card.getColumnHeaderText(CardBrowser.Column.DUE),
equalTo("2")
)
}

@Test
Expand All @@ -497,7 +565,11 @@ class CardBrowserTest : RobolectricTest() {

val card = getCheckedCard(b)

assertThat("Initial position of checked card", card.getColumnHeaderText(CardBrowser.Column.DUE), equalTo("1"))
assertThat(
"Initial position of checked card",
card.getColumnHeaderText(CardBrowser.Column.DUE),
equalTo("1")
)

b.rescheduleWithoutValidation(listOf(card.id), 5)

Expand All @@ -515,15 +587,27 @@ class CardBrowserTest : RobolectricTest() {

val card = getCheckedCard(b)

assertThat("Initial position of checked card", card.getColumnHeaderText(CardBrowser.Column.DUE), equalTo("1"))
assertThat(
"Initial position of checked card",
card.getColumnHeaderText(CardBrowser.Column.DUE),
equalTo("1")
)

b.repositionCardsNoValidation(listOf(card.id), 2)

assertThat("Position of checked card after reposition", card.getColumnHeaderText(CardBrowser.Column.DUE), equalTo("2"))
assertThat(
"Position of checked card after reposition",
card.getColumnHeaderText(CardBrowser.Column.DUE),
equalTo("2")
)

b.onUndo()

assertThat("Position of checked card after undo should be reset", card.getColumnHeaderText(CardBrowser.Column.DUE), equalTo("1"))
assertThat(
"Position of checked card after undo should be reset",
card.getColumnHeaderText(CardBrowser.Column.DUE),
equalTo("1")
)
}

@Test
Expand Down Expand Up @@ -564,13 +648,21 @@ class CardBrowserTest : RobolectricTest() {
col.decks.select(deck)
val c2 = addNoteUsingBasicModel("New", "world").firstCard()
c2.did = deck
c2.flush()
c2.col.updateCard(c2, skipUndoEntry = true)

val cardBrowser = browserWithNoNewCards
cardBrowser.searchCards("world or hello")

assertThat("Cardbrowser has Deck 1 as selected deck", cardBrowser.selectedDeckNameForUi, equalTo("Deck 1"))
assertThat("Results should only be from the selected deck", cardBrowser.cardCount, equalTo(1))
assertThat(
"Cardbrowser has Deck 1 as selected deck",
cardBrowser.selectedDeckNameForUi,
equalTo("Deck 1")
)
assertThat(
"Results should only be from the selected deck",
cardBrowser.cardCount,
equalTo(1)
)
}

/** PR #8553 */
Expand All @@ -583,7 +675,11 @@ class CardBrowserTest : RobolectricTest() {
saveControllerForCleanup(cardBrowserController)

// Make sure card has default value in sortType field
assertThat("Initially Card Browser has order = noteFld", col.config.get<String>("sortType"), equalTo("noteFld"))
assertThat(
"Initially Card Browser has order = noteFld",
col.config.get<String>("sortType"),
equalTo("noteFld")
)

// Change the display order of the card browser
cardBrowserController.get().changeCardOrder(7) // order no. 7 corresponds to "cardEase"
Expand All @@ -592,13 +688,19 @@ class CardBrowserTest : RobolectricTest() {
val outBundle = Bundle()
cardBrowserController.saveInstanceState(outBundle)
cardBrowserController.pause().stop().destroy()
cardBrowserController = Robolectric.buildActivity(CardBrowser::class.java).create(outBundle).start().resume().visible()
cardBrowserController =
Robolectric.buildActivity(CardBrowser::class.java).create(outBundle).start().resume()
.visible()
saveControllerForCleanup(cardBrowserController)

// Find the current (after database has been changed) Mod time

val updatedMod = col.mod
assertThat("Card Browser has the new sortType field", col.config.get<String>("sortType"), equalTo("cardEase"))
assertThat(
"Card Browser has the new sortType field",
col.config.get<String>("sortType"),
equalTo("cardEase")
)
assertNotEquals(0, updatedMod)
}

Expand All @@ -618,11 +720,15 @@ class CardBrowserTest : RobolectricTest() {
col.decks.select(deck)
val c2 = addNoteUsingBasicModel("Front", "Back").firstCard()
c2.did = deck
c2.flush()
c2.col.updateCard(c2, skipUndoEntry = true)

val cardBrowser = browserWithNoNewCards
cardBrowser.searchCards("Hello")
assertThat("Card browser should have Test Deck as the selected deck", cardBrowser.selectedDeckNameForUi, equalTo("Test Deck"))
assertThat(
"Card browser should have Test Deck as the selected deck",
cardBrowser.selectedDeckNameForUi,
equalTo("Test Deck")
)
assertThat("Result should be empty", cardBrowser.cardCount, equalTo(0))

cardBrowser.searchAllDecks()
Expand All @@ -647,7 +753,10 @@ class CardBrowserTest : RobolectricTest() {
val shadowActivity = shadowOf(browser)
val item = shadowActivity.optionsMenu.findItem(R.id.action_undo)
val expected = browser.getString(resId)
assertThat(item.title.toString(), not(containsString(expected.lowercase(Locale.getDefault()))))
assertThat(
item.title.toString(),
not(containsString(expected.lowercase(Locale.getDefault())))
)
}

private fun assertUndoContains(browser: CardBrowser, @StringRes resId: Int) {
Expand All @@ -666,7 +775,7 @@ class CardBrowserTest : RobolectricTest() {
private fun flagCardForNote(n: Note, flag: Int) {
val c = n.firstCard()
c.setUserFlag(flag)
c.flush()
c.col.updateCard(c, skipUndoEntry = true)
}

private fun selectDefaultDeck() {
Expand Down
Loading

0 comments on commit c6fa351

Please sign in to comment.