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
}
}
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));
}
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();
}
}
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);
}
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);
}
Aggregations