Search in sources :

Example 26 with Sched

use of com.ichi2.libanki.sched.Sched 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 27 with Sched

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

the class AbstractSchedTest method ensureUndoCorrectCounts.

@Test
public void ensureUndoCorrectCounts() {
    Collection col = getCol();
    AbstractSched sched = col.getSched();
    DeckConfig dconf = col.getDecks().getConf(1);
    assertThat(dconf, notNullValue());
    dconf.getJSONObject("new").put("perDay", 10);
    col.getDecks().save(dconf);
    for (int i = 0; i < 20; i++) {
        Note note = col.newNote();
        note.setField(0, "a");
        col.addNote(note);
    }
    col.reset();
    assertThat(col.cardCount(), is(20));
    assertThat(sched.newCount(), is(10));
    Card card = sched.getCard();
    assertThat(sched.newCount(), is(9));
    assertThat(sched.counts(card).getNew(), is(10));
    sched.answerCard(card, sched.getGoodNewButton());
    sched.getCard();
    nonTaskUndo(col);
    card.load();
    assertThat(sched.newCount(), is(9));
    assertThat(sched.counts(card).getNew(), is(10));
}
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 28 with Sched

use of com.ichi2.libanki.sched.Sched 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)

Example 29 with Sched

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

the class ContentProviderTest method testQueryNextCard.

/**
 * Test that query for the next card in the schedule returns a valid result without any deck selector
 */
@Test
public void testQueryNextCard() {
    Collection col = getCol();
    AbstractSched sched = col.getSched();
    Cursor reviewInfoCursor = getContentResolver().query(FlashCardsContract.ReviewInfo.CONTENT_URI, null, null, null, null);
    assertNotNull(reviewInfoCursor);
    assertEquals("Check that we actually received one card", 1, reviewInfoCursor.getCount());
    reviewInfoCursor.moveToFirst();
    int cardOrd = reviewInfoCursor.getInt(reviewInfoCursor.getColumnIndex(FlashCardsContract.ReviewInfo.CARD_ORD));
    long noteID = reviewInfoCursor.getLong(reviewInfoCursor.getColumnIndex(FlashCardsContract.ReviewInfo.NOTE_ID));
    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();
        TaskManager.waitToFinish();
        if (nextCard != null && nextCard.note().getId() == noteID && nextCard.getOrd() == cardOrd)
            break;
        TaskManager.waitToFinish();
    }
    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);
}
Also used : AbstractSched(com.ichi2.libanki.sched.AbstractSched) Collection(com.ichi2.libanki.Collection) Cursor(android.database.Cursor) Card(com.ichi2.libanki.Card) Test(org.junit.Test)

Example 30 with Sched

use of com.ichi2.libanki.sched.Sched in project Anki-Android by Ramblurr.

the class DeckTask method doInBackgroundDismissNote.

private TaskData doInBackgroundDismissNote(TaskData... params) {
    Sched sched = params[0].getSched();
    Collection col = sched.getCol();
    Card card = params[0].getCard();
    Note note = card.note();
    int type = params[0].getInt();
    try {
        col.getDb().getDatabase().beginTransaction();
        try {
            switch(type) {
                case 4:
                    // collect undo information
                    col.markUndo(Collection.UNDO_BURY_CARD, new Object[] { col.getDirty(), note.cards(), card.getId() });
                    // then bury
                    sched.buryCards(new long[] { card.getId() });
                    sHadCardQueue = true;
                    break;
                case 0:
                    // collect undo information
                    col.markUndo(Collection.UNDO_BURY_NOTE, new Object[] { col.getDirty(), note.cards(), card.getId() });
                    // then bury
                    sched.buryNote(note.getId());
                    sHadCardQueue = true;
                    break;
                case 1:
                    // collect undo information
                    col.markUndo(Collection.UNDO_SUSPEND_CARD, new Object[] { card });
                    // suspend card
                    if (card.getQueue() == -1) {
                        sched.unsuspendCards(new long[] { card.getId() });
                    } else {
                        sched.suspendCards(new long[] { card.getId() });
                    }
                    sHadCardQueue = true;
                    break;
                case 2:
                    // collect undo information
                    ArrayList<Card> cards = note.cards();
                    long[] cids = new long[cards.size()];
                    for (int i = 0; i < cards.size(); i++) {
                        cids[i] = cards.get(i).getId();
                    }
                    col.markUndo(Collection.UNDO_SUSPEND_NOTE, new Object[] { cards, card.getId() });
                    // suspend note
                    sched.suspendCards(cids);
                    sHadCardQueue = true;
                    break;
                case 3:
                    // collect undo information
                    ArrayList<Card> allCs = note.cards();
                    long[] cardIds = new long[allCs.size()];
                    for (int i = 0; i < allCs.size(); i++) {
                        cardIds[i] = allCs.get(i).getId();
                    }
                    col.markUndo(Collection.UNDO_DELETE_NOTE, new Object[] { note, allCs, card.getId() });
                    // delete note
                    col.remNotes(new long[] { note.getId() });
                    sHadCardQueue = true;
                    break;
            }
            publishProgress(new TaskData(getCard(col.getSched()), 0));
            col.getDb().getDatabase().setTransactionSuccessful();
        } finally {
            col.getDb().getDatabase().endTransaction();
        }
    } catch (RuntimeException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundSuspendCard - RuntimeException on suspending card: " + e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundSuspendCard");
        return new TaskData(false);
    }
    return new TaskData(true);
}
Also used : Sched(com.ichi2.libanki.Sched) Note(com.ichi2.libanki.Note) Collection(com.ichi2.libanki.Collection) Card(com.ichi2.libanki.Card)

Aggregations

Card (com.ichi2.libanki.Card)44 Collection (com.ichi2.libanki.Collection)38 Test (org.junit.Test)35 RobolectricTest (com.ichi2.anki.RobolectricTest)28 Note (com.ichi2.libanki.Note)19 AbstractSched (com.ichi2.libanki.sched.AbstractSched)18 DeckConfig (com.ichi2.libanki.DeckConfig)11 JSONObject (com.ichi2.utils.JSONObject)9 Cursor (android.database.Cursor)7 Sched (com.ichi2.libanki.Sched)6 SchedV2 (com.ichi2.libanki.sched.SchedV2)6 JSONArray (com.ichi2.utils.JSONArray)6 SuppressLint (android.annotation.SuppressLint)4 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)4 DB (com.ichi2.libanki.DB)4 Model (com.ichi2.libanki.Model)4 Sched (com.ichi2.libanki.sched.Sched)4 ArrayList (java.util.ArrayList)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 ContentValues (android.content.ContentValues)3