Search in sources :

Example 1 with Deck

use of com.ichi2.libanki.Deck in project AnkiChinaAndroid by ankichinateam.

the class ContentProviderTest method testInsertAndRemoveNote.

/**
 * Check that inserting and removing a note into default deck works as expected
 */
@Test
public void testInsertAndRemoveNote() {
    // Get required objects for test
    final ContentResolver cr = InstrumentationRegistry.getInstrumentation().getTargetContext().getContentResolver();
    // Add the note
    ContentValues values = new ContentValues();
    values.put(FlashCardsContract.Note.MID, mModelId);
    values.put(FlashCardsContract.Note.FLDS, Utils.joinFields(TEST_NOTE_FIELDS));
    values.put(FlashCardsContract.Note.TAGS, TEST_TAG);
    Uri newNoteUri = cr.insert(FlashCardsContract.Note.CONTENT_URI, values);
    assertNotNull("Check that URI returned from addNewNote is not null", newNoteUri);
    // test that the changes are physically saved to the DB
    final Collection col = reopenCol();
    // Check that it looks as expected
    assertNotNull("check note URI path", newNoteUri.getLastPathSegment());
    Note addedNote = new Note(col, Long.parseLong(newNoteUri.getLastPathSegment()));
    addedNote.load();
    assertArrayEquals("Check that fields were set correctly", addedNote.getFields(), TEST_NOTE_FIELDS);
    assertEquals("Check that tag was set correctly", TEST_TAG, addedNote.getTags().get(0));
    JSONObject model = col.getModels().get(mModelId);
    assertNotNull("Check model", model);
    int expectedNumCards = model.getJSONArray("tmpls").length();
    assertEquals("Check that correct number of cards generated", expectedNumCards, addedNote.numberOfCards());
    // Now delete the note
    cr.delete(newNoteUri, null, null);
    try {
        addedNote.load();
        fail("Expected RuntimeException to be thrown when deleting note");
    } catch (RuntimeException e) {
    // Expect RuntimeException to be thrown when loading deleted note
    }
}
Also used : ContentValues(android.content.ContentValues) JSONObject(com.ichi2.utils.JSONObject) Note(com.ichi2.libanki.Note) Collection(com.ichi2.libanki.Collection) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) Test(org.junit.Test)

Example 2 with Deck

use of com.ichi2.libanki.Deck in project AnkiChinaAndroid by ankichinateam.

the class ContentProviderTest method setUp.

/**
 * Initially create one note for each model.
 */
@Before
public void setUp() throws Exception {
    Log.i(AnkiDroidApp.TAG, "setUp()");
    mCreatedNotes = new ArrayList<>();
    final Collection col = getCol();
    // We have parameterized the "schedVersion" variable, if we are on an emulator
    // (so it is safe) we will try to run with multiple scheduler versions
    tearDown = false;
    if (InstrumentedTest.isEmulator()) {
        col.changeSchedulerVer(schedVersion);
    } else {
        if (schedVersion == 1) {
            assumeThat(col.getSched().getName(), is("std"));
        } else {
            assumeThat(col.getSched().getName(), is("std2"));
        }
    }
    tearDown = true;
    // Do not teardown if setup was aborted
    // Add a new basic model that we use for testing purposes (existing models could potentially be corrupted)
    Model model = StdModels.basicModel.add(col, BASIC_MODEL_NAME);
    mModelId = model.getLong("id");
    ArrayList<String> fields = Models.fieldNames(model);
    // Use the names of the fields as test values for the notes which will be added
    mDummyFields = fields.toArray(new String[0]);
    // create test decks and add one note for every deck
    mNumDecksBeforeTest = col.getDecks().count();
    for (String fullName : TEST_DECKS) {
        String[] path = Decks.path(fullName);
        String partialName = "";
        /* Looping over all parents of full name. Adding them to
             * mTestDeckIds ensures the deck parents decks get deleted
             * too at tear-down.
             */
        for (String s : path) {
            partialName += s;
            /* If parent already exists, don't add the deck, so
                 * that we are sure it won't get deleted at
                 * set-down, */
            if (col.getDecks().byName(partialName) != null) {
                continue;
            }
            long did = col.getDecks().id(partialName);
            mTestDeckIds.add(did);
            mCreatedNotes.add(setupNewNote(col, mModelId, did, mDummyFields, TEST_TAG));
            partialName += "::";
        }
    }
    // Add a note to the default deck as well so that testQueryNextCard() works
    mCreatedNotes.add(setupNewNote(col, mModelId, 1, mDummyFields, TEST_TAG));
}
Also used : Model(com.ichi2.libanki.Model) Collection(com.ichi2.libanki.Collection) Before(org.junit.Before)

Example 3 with Deck

use of com.ichi2.libanki.Deck in project AnkiChinaAndroid by ankichinateam.

the class ContentProviderTest method testQueryAllDecks.

/**
 * Test query to decks table
 */
@Test
public void testQueryAllDecks() {
    Collection col = getCol();
    Decks decks = col.getDecks();
    Cursor decksCursor = InstrumentationRegistry.getInstrumentation().getTargetContext().getContentResolver().query(FlashCardsContract.Deck.CONTENT_ALL_URI, FlashCardsContract.Deck.DEFAULT_PROJECTION, null, null, null);
    assertNotNull(decksCursor);
    try {
        assertEquals("Check number of results", decks.count(), decksCursor.getCount());
        while (decksCursor.moveToNext()) {
            long deckID = decksCursor.getLong(decksCursor.getColumnIndex(FlashCardsContract.Deck.DECK_ID));
            String deckName = decksCursor.getString(decksCursor.getColumnIndex(FlashCardsContract.Deck.DECK_NAME));
            Deck deck = decks.get(deckID);
            assertNotNull("Check that the deck we received actually exists", deck);
            assertEquals("Check that the received deck has the correct name", deck.getString("name"), deckName);
        }
    } finally {
        decksCursor.close();
    }
}
Also used : Decks(com.ichi2.libanki.Decks) Collection(com.ichi2.libanki.Collection) Deck(com.ichi2.libanki.Deck) Cursor(android.database.Cursor) Test(org.junit.Test)

Example 4 with Deck

use of com.ichi2.libanki.Deck 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 5 with Deck

use of com.ichi2.libanki.Deck in project AnkiChinaAndroid by ankichinateam.

the class ContentProviderTest method testInsertField.

/**
 * Check that inserting and removing a note into default deck works as expected
 */
@Test
public void testInsertField() throws Exception {
    // Get required objects for test
    final ContentResolver cr = InstrumentationRegistry.getInstrumentation().getTargetContext().getContentResolver();
    Collection col = getCol();
    Model model = StdModels.basicModel.add(col, BASIC_MODEL_NAME);
    long modelId = model.getLong("id");
    JSONArray initialFieldsArr = model.getJSONArray("flds");
    int initialFieldCount = initialFieldsArr.length();
    Uri noteTypeUri = ContentUris.withAppendedId(FlashCardsContract.Model.CONTENT_URI, modelId);
    ContentValues insertFieldValues = new ContentValues();
    insertFieldValues.put(FlashCardsContract.Model.FIELD_NAME, TEST_FIELD_NAME);
    Uri fieldUri = cr.insert(Uri.withAppendedPath(noteTypeUri, "fields"), insertFieldValues);
    assertNotNull("Check field uri", fieldUri);
    // Ensure that the changes are physically saved to the DB
    col = reopenCol();
    model = col.getModels().get(modelId);
    // Test the field is as expected
    long fieldId = ContentUris.parseId(fieldUri);
    assertEquals("Check field id", initialFieldCount, fieldId);
    assertNotNull("Check model", model);
    JSONArray fldsArr = model.getJSONArray("flds");
    assertEquals("Check fields length", initialFieldCount + 1, fldsArr.length());
    assertEquals("Check last field name", TEST_FIELD_NAME, fldsArr.getJSONObject(fldsArr.length() - 1).optString("name", ""));
    col.getModels().rem(model);
}
Also used : ContentValues(android.content.ContentValues) Model(com.ichi2.libanki.Model) JSONArray(com.ichi2.utils.JSONArray) Collection(com.ichi2.libanki.Collection) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) Test(org.junit.Test)

Aggregations

Deck (com.ichi2.libanki.Deck)100 Collection (com.ichi2.libanki.Collection)97 JSONObject (com.ichi2.utils.JSONObject)88 Test (org.junit.Test)80 JSONArray (com.ichi2.utils.JSONArray)55 Card (com.ichi2.libanki.Card)53 Note (com.ichi2.libanki.Note)50 ArrayList (java.util.ArrayList)47 RobolectricTest (com.ichi2.anki.RobolectricTest)44 DeckConfig (com.ichi2.libanki.DeckConfig)37 JSONException (com.ichi2.utils.JSONException)34 NonNull (androidx.annotation.NonNull)30 HashMap (java.util.HashMap)29 Model (com.ichi2.libanki.Model)23 Map (java.util.Map)22 Intent (android.content.Intent)21 Resources (android.content.res.Resources)18 TextView (android.widget.TextView)18 SharedPreferences (android.content.SharedPreferences)17 Cursor (android.database.Cursor)17