Search in sources :

Example 21 with ModelManager

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

the class ReviewerTest method baseDeckName.

@Test
public void baseDeckName() {
    Collection col = getCol();
    ModelManager models = col.getModels();
    DeckManager decks = col.getDecks();
    Long didAb = addDeck("A::B");
    Model basic = models.byName(AnkiDroidApp.getAppResources().getString(R.string.basic_model_name));
    basic.put("did", didAb);
    addNoteUsingBasicModel("foo", "bar");
    Long didA = addDeck("A");
    decks.select(didA);
    Reviewer reviewer = startReviewer();
    waitForAsyncTasksToComplete();
    assertThat(reviewer.getSupportActionBar().getTitle(), is("B"));
}
Also used : Model(com.ichi2.libanki.Model) Collection(com.ichi2.libanki.Collection) DeckManager(com.ichi2.libanki.DeckManager) ModelManager(com.ichi2.libanki.ModelManager) Test(org.junit.Test)

Example 22 with ModelManager

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

the class ReviewerTest method addNoteWithThreeCards.

private void addNoteWithThreeCards() throws ConfirmModSchemaException {
    ModelManager models = getCol().getModels();
    Model m = models.copy(models.current());
    m.put("name", "Three");
    models.add(m);
    m = models.byName("Three");
    models.flush();
    cloneTemplate(models, m);
    cloneTemplate(models, m);
    @NonNull Note newNote = getCol().newNote();
    newNote.setField(0, "Hello");
    assertThat(newNote.model().get("name"), is("Three"));
    assertThat(getCol().addNote(newNote), is(3));
}
Also used : NonNull(androidx.annotation.NonNull) Note(com.ichi2.libanki.Note) Model(com.ichi2.libanki.Model) ModelManager(com.ichi2.libanki.ModelManager)

Example 23 with ModelManager

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

the class ModelTest method test_templates.

@Test
public void test_templates() throws ConfirmModSchemaException {
    Collection col = getCol();
    Model m = col.getModels().current();
    ModelManager mm = col.getModels();
    JSONObject t = Models.newTemplate("Reverse");
    t.put("qfmt", "{{Back}}");
    t.put("afmt", "{{Front}}");
    mm.addTemplateModChanged(m, t);
    mm.save(m);
    Note note = col.newNote();
    note.setItem("Front", "1");
    note.setItem("Back", "2");
    col.addNote(note);
    assertEquals(2, col.cardCount());
    List<Card> cards = note.cards();
    assertEquals(2, cards.size());
    Card c = cards.get(0);
    Card c2 = cards.get(1);
    // first card should have first ord
    assertEquals(0, c.getOrd());
    assertEquals(1, c2.getOrd());
    // switch templates
    col.getModels().moveTemplate(m, c.template(), 1);
    c.load();
    c2.load();
    assertEquals(1, c.getOrd());
    assertEquals(0, c2.getOrd());
    // removing a template should delete its cards
    col.getModels().remTemplate(m, m.getJSONArray("tmpls").getJSONObject(0));
    assertEquals(1, col.cardCount());
    // and should have updated the other cards' ordinals
    c = note.cards().get(0);
    assertEquals(0, c.getOrd());
    assertEquals("1", stripHTML(c.q()));
    // it shouldn't be possible to orphan notes by removing templates
    t = Models.newTemplate("template name");
    mm.addTemplateModChanged(m, t);
    col.getModels().remTemplate(m, m.getJSONArray("tmpls").getJSONObject(0));
    assertEquals(0, col.getDb().queryLongScalar("select count() from cards where nid not in (select id from notes)"));
}
Also used : JSONObject(com.ichi2.utils.JSONObject) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)16 JSONObject (com.ichi2.utils.JSONObject)14 RobolectricTest (com.ichi2.anki.RobolectricTest)12 Model (com.ichi2.libanki.Model)12 ModelManager (com.ichi2.libanki.ModelManager)11 Collection (com.ichi2.libanki.Collection)7 Note (com.ichi2.libanki.Note)6 JSONArray (com.ichi2.utils.JSONArray)5 JSONException (com.ichi2.utils.JSONException)4 Card (com.ichi2.libanki.Card)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 MatrixCursor (android.database.MatrixCursor)2 NonNull (androidx.annotation.NonNull)2 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)2 InstrumentedTest (com.ichi2.anki.tests.InstrumentedTest)2 List (java.util.List)2 Manifest (android.Manifest)1 BroadcastReceiver (android.content.BroadcastReceiver)1 Context (android.content.Context)1 DialogInterface (android.content.DialogInterface)1