Search in sources :

Example 1 with Sched

use of com.ichi2.libanki.sched.Sched in project AnkiChinaAndroid by ankichinateam.

the class ContentProviderTest method testBuryCard.

/**
 * Test burying a card through the ReviewInfo endpoint
 */
@Test
public void testBuryCard() {
    // get the first card due
    // ----------------------
    Collection col = getCol();
    Card card = getFirstCardFromScheduler(col);
    // verify that the card is not already user-buried
    Assert.assertNotEquals("Card is not user-buried before test", Consts.QUEUE_TYPE_SIBLING_BURIED, card.getQueue());
    // retain the card id, we will lookup the card after the update
    long cardId = card.getId();
    // bury it through the API
    // -----------------------
    ContentResolver cr = InstrumentationRegistry.getInstrumentation().getTargetContext().getContentResolver();
    Uri reviewInfoUri = FlashCardsContract.ReviewInfo.CONTENT_URI;
    ContentValues values = new ContentValues();
    long noteId = card.note().getId();
    int cardOrd = card.getOrd();
    int bury = 1;
    values.put(FlashCardsContract.ReviewInfo.NOTE_ID, noteId);
    values.put(FlashCardsContract.ReviewInfo.CARD_ORD, cardOrd);
    values.put(FlashCardsContract.ReviewInfo.BURY, bury);
    int updateCount = cr.update(reviewInfoUri, values, null, null);
    assertEquals("Check if update returns 1", 1, updateCount);
    // verify that it did get buried
    // -----------------------------
    Card cardAfterUpdate = col.getCard(cardId);
    // QUEUE_TYPE_MANUALLY_BURIED was also used for SIBLING_BURIED in sched v1
    assertEquals("Card is user-buried", (schedVersion == 1) ? Consts.QUEUE_TYPE_SIBLING_BURIED : Consts.QUEUE_TYPE_MANUALLY_BURIED, cardAfterUpdate.getQueue());
    // cleanup, unbury cards
    // ---------------------
    col.getSched().unburyCards();
}
Also used : ContentValues(android.content.ContentValues) Collection(com.ichi2.libanki.Collection) Uri(android.net.Uri) Card(com.ichi2.libanki.Card) ContentResolver(android.content.ContentResolver) Test(org.junit.Test)

Example 2 with Sched

use of com.ichi2.libanki.sched.Sched in project AnkiChinaAndroid by ankichinateam.

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 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 = InstrumentationRegistry.getInstrumentation().getTargetContext().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.note().getId() == noteID && nextCard.getOrd() == cardOrd)
                break;
        }
        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) Card(com.ichi2.libanki.Card) Test(org.junit.Test)

Example 3 with Sched

use of com.ichi2.libanki.sched.Sched in project AnkiChinaAndroid by ankichinateam.

the class CardContentProvider method buryOrSuspendCard.

private void buryOrSuspendCard(Collection col, AbstractSched sched, Card card, boolean bury) {
    try {
        DB db = col.getDb();
        db.getDatabase().beginTransaction();
        try {
            if (card != null) {
                if (bury) {
                    // bury
                    sched.buryCards(new long[] { card.getId() });
                } else {
                    // suspend
                    sched.suspendCards(new long[] { card.getId() });
                }
            }
            db.getDatabase().setTransactionSuccessful();
        } finally {
            db.getDatabase().endTransaction();
        }
    } catch (RuntimeException e) {
        Timber.e(e, "buryOrSuspendCard - RuntimeException on burying or suspending card");
        AnkiDroidApp.sendExceptionReport(e, "doInBackgroundBurySuspendCard");
        return;
    }
}
Also used : DB(com.ichi2.libanki.DB)

Example 4 with Sched

use of com.ichi2.libanki.sched.Sched in project AnkiChinaAndroid by ankichinateam.

the class CardContentProvider method answerCard.

private void answerCard(Collection col, AbstractSched sched, Card cardToAnswer, @Consts.BUTTON_TYPE int ease, long timeTaken) {
    try {
        DB db = col.getDb();
        db.getDatabase().beginTransaction();
        try {
            if (cardToAnswer != null) {
                if (timeTaken != -1) {
                    cardToAnswer.setTimerStarted(col.getTime().intTime() - timeTaken / 1000);
                }
                sched.answerCard(cardToAnswer, ease);
            }
            db.getDatabase().setTransactionSuccessful();
        } finally {
            db.getDatabase().endTransaction();
        }
    } catch (RuntimeException e) {
        Timber.e(e, "answerCard - RuntimeException on answering card");
        AnkiDroidApp.sendExceptionReport(e, "doInBackgroundAnswerCard");
        return;
    }
}
Also used : DB(com.ichi2.libanki.DB)

Example 5 with Sched

use of com.ichi2.libanki.sched.Sched in project AnkiChinaAndroid by ankichinateam.

the class CollectionTask method doInBackgroundAnswerCard.

private TaskData doInBackgroundAnswerCard(TaskData param) {
    Collection col = getCol();
    AbstractSched sched = col.getSched();
    Card oldCard = param.getCard();
    @Consts.BUTTON_TYPE int ease = param.getInt();
    Card newCard = null;
    Timber.i(oldCard != null ? "Answering card" : "Obtaining card");
    try {
        DB db = col.getDb();
        db.getDatabase().beginTransaction();
        try {
            if (oldCard != null) {
                Timber.i("Answering card %d", oldCard.getId());
                sched.answerCard(oldCard, ease);
            }
            newCard = sched.getCard();
            if (newCard != null) {
                // render cards before locking database
                newCard._getQA(true);
            }
            publishProgress(new TaskData(newCard));
            db.getDatabase().setTransactionSuccessful();
        } finally {
            db.getDatabase().endTransaction();
        }
    } catch (RuntimeException e) {
        Timber.e(e, "doInBackgroundAnswerCard - RuntimeException on answering card");
        AnkiDroidApp.sendExceptionReport(e, "doInBackgroundAnswerCard");
        return new TaskData(false);
    }
    return new TaskData(true);
}
Also used : AbstractSched(com.ichi2.libanki.sched.AbstractSched) Collection(com.ichi2.libanki.Collection) DB(com.ichi2.libanki.DB) 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