Search in sources :

Example 26 with Counts

use of com.ichi2.libanki.sched.Counts in project Anki-Android by ankidroid.

the class Sched method counts.

@Override
@NonNull
public Counts counts(@NonNull Card card) {
    Counts counts = counts();
    Counts.Queue idx = countIdx(card);
    if (idx == LRN) {
        counts.addLrn(card.getLeft() / 1000);
    } else {
        counts.changeCount(idx, 1);
    }
    return counts;
}
Also used : Queue(com.ichi2.libanki.sched.Counts.Queue) NonNull(androidx.annotation.NonNull)

Example 27 with Counts

use of com.ichi2.libanki.sched.Counts in project Anki-Android by ankidroid.

the class ModelTest method test_modelChange.

@Test
public void test_modelChange() throws ConfirmModSchemaException {
    Collection col = getCol();
    Model cloze = col.getModels().byName("Cloze");
    // enable second template and add a note
    Model basic = col.getModels().current();
    ModelManager mm = col.getModels();
    JSONObject t = Models.newTemplate("Reverse");
    t.put("qfmt", "{{Back}}");
    t.put("afmt", "{{Front}}");
    mm.addTemplateModChanged(basic, t);
    mm.save(basic);
    Note note = col.newNote();
    note.setItem("Front", "note");
    note.setItem("Back", "b123");
    col.addNote(note);
    // switch fields
    Map<Integer, Integer> map = new HashMap<>();
    map.put(0, 1);
    map.put(1, 0);
    col.getModels().change(basic, note.getId(), basic, map, null);
    note.load();
    assertEquals("b123", note.getItem("Front"));
    assertEquals("note", note.getItem("Back"));
    // switch cards
    Card c0 = note.cards().get(0);
    Card c1 = note.cards().get(1);
    assertThat(c0.q(), containsString("b123"));
    assertThat(c1.q(), containsString("note"));
    assertEquals(0, c0.getOrd());
    assertEquals(1, c1.getOrd());
    col.getModels().change(basic, note.getId(), basic, null, map);
    note.load();
    c0.load();
    c1.load();
    assertThat(c0.q(), containsString("note"));
    assertThat(c1.q(), containsString("b123"));
    assertEquals(1, c0.getOrd());
    assertEquals(0, c1.getOrd());
    // .cards() returns cards in order
    assertEquals(c1.getId(), note.cards().get(0).getId());
    // delete first card
    map = new HashMap<>();
    map.put(0, null);
    map.put(1, 1);
    // if (isWin) {
    // // The low precision timer on Windows reveals a race condition
    // time.sleep(0.05);
    // }
    col.getModels().change(basic, note.getId(), basic, null, map);
    note.load();
    c0.load();
    // the card was deleted
    // but we have two cards, as a new one was generated
    assertEquals(2, note.numberOfCards());
    // an unmapped field becomes blank
    assertEquals("b123", note.getItem("Front"));
    assertEquals("note", note.getItem("Back"));
    col.getModels().change(basic, note.getId(), basic, map, null);
    note.load();
    assertEquals("", note.getItem("Front"));
    assertEquals("note", note.getItem("Back"));
    // another note to try model conversion
    note = col.newNote();
    note.setItem("Front", "f2");
    note.setItem("Back", "b2");
    col.addNote(note);
    // counts = col.getModels().all_use_counts();
    // Using older version of the test
    assertEquals(2, col.getModels().useCount(basic));
    assertEquals(0, col.getModels().useCount(cloze));
    // Identity map
    map = new HashMap<>();
    map.put(0, 0);
    map.put(1, 1);
    col.getModels().change(basic, note.getId(), cloze, map, map);
    note.load();
    assertEquals("f2", note.getItem("Text"));
    assertEquals(2, note.numberOfCards());
    // back the other way, with deletion of second ord
    col.getModels().remTemplate(basic, basic.getJSONArray("tmpls").getJSONObject(1));
    assertEquals(2, col.getDb().queryScalar("select count() from cards where nid = ?", note.getId()));
    map = new HashMap<>();
    map.put(0, 0);
    col.getModels().change(cloze, note.getId(), basic, map, map);
    assertEquals(1, col.getDb().queryScalar("select count() from cards where nid = ?", note.getId()));
}
Also used : JSONObject(com.ichi2.utils.JSONObject) HashMap(java.util.HashMap) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 28 with Counts

use of com.ichi2.libanki.sched.Counts in project Anki-Android by ankidroid.

the class AbstractSchedTest method siblingCorrectlyBuried.

@Test
public void siblingCorrectlyBuried() {
    // #6903
    Collection col = getCol();
    AbstractSched sched = col.getSched();
    DeckConfig dconf = col.getDecks().getConf(1);
    assertThat(dconf, notNullValue());
    dconf.getJSONObject("new").put("bury", true);
    getCol().getDecks().save(dconf);
    final int nbNote = 2;
    Note[] notes = new Note[nbNote];
    for (int i = 0; i < nbNote; i++) {
        Note note = addNoteUsingBasicAndReversedModel("front", "back");
        notes[i] = note;
    }
    col.reset();
    for (int i = 0; i < nbNote; i++) {
        Card card = sched.getCard();
        Counts counts = sched.counts(card);
        // imitate what the reviewer does
        sched.setCurrentCard(card);
        // Actual number of new card.
        assertThat(counts.getNew(), is(greaterThan(nbNote - i)));
        // Maximal number potentially shown,
        assertThat(counts.getNew(), is(lessThanOrEqualTo(nbNote * 2 - i)));
        // because decrementing does not consider burying sibling
        assertEquals(0, counts.getLrn());
        assertEquals(0, counts.getRev());
        assertEquals(notes[i].firstCard().getId(), card.getId());
        assertEquals(Consts.QUEUE_TYPE_NEW, card.getQueue());
        sched.answerCard(card, sched.answerButtons(card));
    }
    Card card = sched.getCard();
    assertNull(card);
}
Also used : Note(com.ichi2.libanki.Note) Collection(com.ichi2.libanki.Collection) DeckConfig(com.ichi2.libanki.DeckConfig) Card(com.ichi2.libanki.Card) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 29 with Counts

use of com.ichi2.libanki.sched.Counts in project Anki-Android by ankidroid.

the class AbstractSchedTest method undoAndRedo.

protected void undoAndRedo(boolean preload) {
    Collection col = getCol();
    DeckConfig conf = col.getDecks().confForDid(1);
    conf.getJSONObject("new").put("delays", new JSONArray(new double[] { 1, 3, 5, 10 }));
    col.getDecks().save(conf);
    col.set_config("collapseTime", 20 * 60);
    AbstractSched sched = col.getSched();
    addNoteUsingBasicModel("foo", "bar");
    col.reset();
    advanceRobolectricLooper();
    Card card = sched.getCard();
    assertNotNull(card);
    assertEquals(new Counts(1, 0, 0), sched.counts(card));
    if (preload) {
        sched.preloadNextCard();
    }
    sched.answerCard(card, sched.getGoodNewButton());
    advanceRobolectricLooper();
    card = sched.getCard();
    assertNotNull(card);
    assertEquals(new Counts(0, (schedVersion == 1) ? 3 : 1, 0), sched.counts(card));
    if (preload) {
        sched.preloadNextCard();
    }
    sched.answerCard(card, sched.getGoodNewButton());
    advanceRobolectricLooper();
    card = sched.getCard();
    assertNotNull(card);
    assertEquals(new Counts(0, (schedVersion == 1) ? 2 : 1, 0), sched.counts(card));
    if (preload) {
        sched.preloadNextCard();
        advanceRobolectricLooper();
    }
    assertNotNull(card);
    card = nonTaskUndo(col);
    advanceRobolectricLooper();
    assertNotNull(card);
    assertEquals(new Counts(0, (schedVersion == 1) ? 3 : 1, 0), sched.counts(card));
    sched.count();
    if (preload) {
        sched.preloadNextCard();
        advanceRobolectricLooper();
    }
    sched.answerCard(card, sched.getGoodNewButton());
    advanceRobolectricLooper();
    card = sched.getCard();
    assertNotNull(card);
    if (preload) {
        sched.preloadNextCard();
    }
    assertEquals(new Counts(0, (schedVersion == 1) ? 2 : 1, 0), sched.counts(card));
    assertNotNull(card);
}
Also used : JSONArray(com.ichi2.utils.JSONArray) Collection(com.ichi2.libanki.Collection) DeckConfig(com.ichi2.libanki.DeckConfig) Card(com.ichi2.libanki.Card)

Example 30 with Counts

use of com.ichi2.libanki.sched.Counts in project Anki-Android by ankidroid.

the class ContentProviderTest method testQueryCardFromCertainDeck.

/**
 * Test that query for the next card in the schedule returns a valid result WITH a deck selector
 */
@Test
public synchronized void testQueryCardFromCertainDeck() {
    long deckToTest = mTestDeckIds.get(0);
    String deckSelector = "deckID=?";
    String[] deckArguments = { Long.toString(deckToTest) };
    Collection col = getCol();
    AbstractSched sched = col.getSched();
    long selectedDeckBeforeTest = col.getDecks().selected();
    // select Default deck
    col.getDecks().select(1);
    Cursor reviewInfoCursor = getContentResolver().query(FlashCardsContract.ReviewInfo.CONTENT_URI, null, deckSelector, deckArguments, null);
    assertNotNull(reviewInfoCursor);
    assertEquals("Check that we actually received one card", 1, reviewInfoCursor.getCount());
    try {
        reviewInfoCursor.moveToFirst();
        int cardOrd = reviewInfoCursor.getInt(reviewInfoCursor.getColumnIndex(FlashCardsContract.ReviewInfo.CARD_ORD));
        long noteID = reviewInfoCursor.getLong(reviewInfoCursor.getColumnIndex(FlashCardsContract.ReviewInfo.NOTE_ID));
        assertEquals("Check that the selected deck has not changed", 1, col.getDecks().selected());
        col.getDecks().select(deckToTest);
        Card nextCard = null;
        for (int i = 0; i < 10; i++) {
            // minimizing fails, when sched.reset() randomly chooses between multiple cards
            col.reset();
            nextCard = sched.getCard();
            if (nextCard != null && nextCard.note().getId() == noteID && nextCard.getOrd() == cardOrd)
                break;
            // Reset counts is executed in background.
            try {
                Thread.sleep(500);
            } catch (Exception e) {
                Timber.e(e);
            }
        }
        assertNotNull("Check that there actually is a next scheduled card", nextCard);
        assertEquals("Check that received card and actual card have same note id", nextCard.note().getId(), noteID);
        assertEquals("Check that received card and actual card have same card ord", nextCard.getOrd(), cardOrd);
    } finally {
        reviewInfoCursor.close();
    }
    col.getDecks().select(selectedDeckBeforeTest);
}
Also used : AbstractSched(com.ichi2.libanki.sched.AbstractSched) Collection(com.ichi2.libanki.Collection) Cursor(android.database.Cursor) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) Card(com.ichi2.libanki.Card) Test(org.junit.Test)

Aggregations

Collection (com.ichi2.libanki.Collection)47 Card (com.ichi2.libanki.Card)43 Test (org.junit.Test)39 RobolectricTest (com.ichi2.anki.RobolectricTest)38 Note (com.ichi2.libanki.Note)38 Deck (com.ichi2.libanki.Deck)12 DeckConfig (com.ichi2.libanki.DeckConfig)11 JSONArray (com.ichi2.utils.JSONArray)11 JSONObject (com.ichi2.utils.JSONObject)10 JSONException (com.ichi2.utils.JSONException)6 HashMap (java.util.HashMap)6 Resources (android.content.res.Resources)4 Nullable (androidx.annotation.Nullable)4 Model (com.ichi2.libanki.Model)4 AbstractDeckTreeNode (com.ichi2.libanki.sched.AbstractDeckTreeNode)4 IOException (java.io.IOException)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 JSONObject (org.json.JSONObject)4 Cursor (android.database.Cursor)3 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)3