Search in sources :

Example 6 with CARD

use of com.ichi2.anki.CardBrowser.Column.CARD in project AnkiChinaAndroid by ankichinateam.

the class ContentProviderTest method setupNewNote.

private static Uri setupNewNote(Collection col, long mid, long did, String[] fields, @SuppressWarnings("SameParameterValue") String tag) {
    Note newNote = new Note(col, col.getModels().get(mid));
    for (int idx = 0; idx < fields.length; idx++) {
        newNote.setField(idx, fields[idx]);
    }
    newNote.addTag(tag);
    assertThat("At least one card added for note", col.addNote(newNote), is(greaterThanOrEqualTo(1)));
    for (Card c : newNote.cards()) {
        c.setDid(did);
        c.flush();
    }
    return Uri.withAppendedPath(FlashCardsContract.Note.CONTENT_URI, Long.toString(newNote.getId()));
}
Also used : Note(com.ichi2.libanki.Note) Card(com.ichi2.libanki.Card)

Example 7 with CARD

use of com.ichi2.anki.CardBrowser.Column.CARD in project AnkiChinaAndroid by ankichinateam.

the class ContentProviderTest method testAnswerCard.

/**
 * Test giving the answer for a reviewed card
 */
@Test
public void testAnswerCard() {
    Collection col = getCol();
    Card card = getFirstCardFromScheduler(col);
    long cardId = card.getId();
    // the card starts out being new
    assertEquals("card is initial new", Consts.CARD_TYPE_NEW, card.getQueue());
    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 earlyGraduatingEase = (schedVersion == 1) ? AbstractFlashcardViewer.EASE_3 : AbstractFlashcardViewer.EASE_4;
    // 5 seconds
    long timeTaken = 5000;
    values.put(FlashCardsContract.ReviewInfo.NOTE_ID, noteId);
    values.put(FlashCardsContract.ReviewInfo.CARD_ORD, cardOrd);
    values.put(FlashCardsContract.ReviewInfo.EASE, earlyGraduatingEase);
    values.put(FlashCardsContract.ReviewInfo.TIME_TAKEN, timeTaken);
    int updateCount = cr.update(reviewInfoUri, values, null, null);
    assertEquals("Check if update returns 1", 1, updateCount);
    try {
        Thread.currentThread().wait(500);
    } catch (Exception e) {
    /* do nothing */
    }
    col.reset();
    Card newCard = col.getSched().getCard();
    if (newCard != null) {
        if (newCard.note().getId() == card.note().getId() && newCard.getOrd() == card.getOrd()) {
            fail("Next scheduled card has not changed");
        }
    }
    // lookup the card after update, ensure it's not new anymore
    Card cardAfterReview = col.getCard(cardId);
    assertEquals("card is now type rev", Card.TYPE_REV, cardAfterReview.getQueue());
}
Also used : ContentValues(android.content.ContentValues) Collection(com.ichi2.libanki.Collection) Uri(android.net.Uri) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) Card(com.ichi2.libanki.Card) ContentResolver(android.content.ContentResolver) Test(org.junit.Test)

Example 8 with CARD

use of com.ichi2.anki.CardBrowser.Column.CARD in project AnkiChinaAndroid by ankichinateam.

the class ContentProviderTest method testUpdateTags.

/**
 * Update tags on a note
 */
@Test
public void testUpdateTags() {
    // get the first card due
    // ----------------------
    Collection col = getCol();
    Card card = getFirstCardFromScheduler(col);
    Note note = card.note();
    long noteId = note.getId();
    // make sure the tag is what we expect initially
    // ---------------------------------------------
    List<String> tagList = note.getTags();
    assertEquals("only one tag", 1, tagList.size());
    assertEquals("check tag value", TEST_TAG, tagList.get(0));
    // update tags
    // -----------
    String tag2 = "mynewtag";
    ContentResolver cr = InstrumentationRegistry.getInstrumentation().getTargetContext().getContentResolver();
    Uri updateNoteUri = Uri.withAppendedPath(FlashCardsContract.Note.CONTENT_URI, Long.toString(noteId));
    ContentValues values = new ContentValues();
    values.put(FlashCardsContract.Note.TAGS, TEST_TAG + " " + tag2);
    int updateCount = cr.update(updateNoteUri, values, null, null);
    assertEquals("updateCount is 1", 1, updateCount);
    // lookup the note now and verify tags
    // -----------------------------------
    Note noteAfterUpdate = col.getNote(noteId);
    List<String> newTagList = noteAfterUpdate.getTags();
    assertEquals("two tags", 2, newTagList.size());
    assertEquals("check first tag", TEST_TAG, newTagList.get(0));
    assertEquals("check second tag", tag2, newTagList.get(1));
}
Also used : ContentValues(android.content.ContentValues) Note(com.ichi2.libanki.Note) Collection(com.ichi2.libanki.Collection) Uri(android.net.Uri) Card(com.ichi2.libanki.Card) ContentResolver(android.content.ContentResolver) Test(org.junit.Test)

Example 9 with CARD

use of com.ichi2.anki.CardBrowser.Column.CARD 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 10 with CARD

use of com.ichi2.anki.CardBrowser.Column.CARD 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)

Aggregations

Card (com.ichi2.libanki.Card)222 Test (org.junit.Test)212 Collection (com.ichi2.libanki.Collection)179 Note (com.ichi2.libanki.Note)173 RobolectricTest (com.ichi2.anki.RobolectricTest)168 JSONObject (com.ichi2.utils.JSONObject)114 JSONArray (com.ichi2.utils.JSONArray)80 DeckConfig (com.ichi2.libanki.DeckConfig)72 ArrayList (java.util.ArrayList)48 NonNull (androidx.annotation.NonNull)33 Deck (com.ichi2.libanki.Deck)32 Intent (android.content.Intent)29 JSONException (com.ichi2.utils.JSONException)27 Model (com.ichi2.libanki.Model)26 Context (android.content.Context)23 SuppressLint (android.annotation.SuppressLint)22 Cursor (android.database.Cursor)22 HashMap (java.util.HashMap)21 Matchers.containsString (org.hamcrest.Matchers.containsString)20 List (java.util.List)17