Search in sources :

Example 11 with ConfirmModSchemaException

use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.

the class ReviewerTest method addNoteWithThreeCards.

private void addNoteWithThreeCards() throws ConfirmModSchemaException {
    Models 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);
    Note newNote = getCol().newNote();
    newNote.setField(0, "Hello");
    assertThat(newNote.model().get("name"), is("Three"));
    assertThat(getCol().addNote(newNote), is(3));
}
Also used : Note(com.ichi2.libanki.Note) Model(com.ichi2.libanki.Model) Models(com.ichi2.libanki.Models)

Example 12 with ConfirmModSchemaException

use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.

the class ModelTest method test_chained_mods.

@Test
public void test_chained_mods() throws ConfirmModSchemaException {
    Collection col = getCol();
    col.getModels().setCurrent(col.getModels().byName("Cloze"));
    Model m = col.getModels().current();
    Models mm = col.getModels();
    // We replace the default Cloze template
    JSONObject t = Models.newTemplate("ChainedCloze");
    t.put("qfmt", "{{cloze:text:Text}}");
    t.put("afmt", "{{cloze:text:Text}}");
    mm.addTemplateModChanged(m, t);
    mm.save(m);
    col.getModels().remTemplate(m, m.getJSONArray("tmpls").getJSONObject(0));
    Note note = col.newNote();
    String q1 = "<span style=\"color:red\">phrase</span>";
    String a1 = "<b>sentence</b>";
    String q2 = "<span style=\"color:red\">en chaine</span>";
    String a2 = "<i>chained</i>";
    note.setItem("Text", "This {{c1::" + q1 + "::" + a1 + "}} demonstrates {{c1::" + q2 + "::" + a2 + "}} clozes.");
    assertEquals(1, col.addNote(note));
    String question = note.cards().get(0).q();
/* TODO: chained modifier
        assertThat("Question «"+question+"» does not contain the expected string", question, containsString("This <span class=cloze>[sentence]</span> demonstrates <span class=cloze>[chained]</span> clozes.")
                   );
        assertThat(note.cards().get(0).a(), containsString("This <span class=cloze>phrase</span> demonstrates <span class=cloze>en chaine</span> clozes."
                                                    ));

         */
}
Also used : JSONObject(com.ichi2.utils.JSONObject) Matchers.containsString(org.hamcrest.Matchers.containsString) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 13 with ConfirmModSchemaException

use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.

the class ModelTest method test_templates.

@Test
public void test_templates() throws ConfirmModSchemaException {
    Collection col = getCol();
    Model m = col.getModels().current();
    Models 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)

Example 14 with ConfirmModSchemaException

use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.

the class SchedTest method getColV1.

/**
 ***************
 ** autogenerated from https://github.com/ankitects/anki/blob/2c73dcb2e547c44d9e02c20a00f3c52419dc277b/pylib/tests/test_cards.py*
 ****************
 */
private Collection getColV1() throws ConfirmModSchemaException {
    Collection col = getCol();
    col.changeSchedulerVer(1);
    return col;
}
Also used : Collection(com.ichi2.libanki.Collection)

Example 15 with ConfirmModSchemaException

use of com.ichi2.anki.exception.ConfirmModSchemaException in project Anki-Android by ankidroid.

the class SchedV2Test method handlesSmallSteps.

@Test
public void handlesSmallSteps() throws ConfirmModSchemaException {
    // a delay of 0 crashed the app (step of 0.01).
    getCol().changeSchedulerVer(2);
    addNoteUsingBasicModel("Hello", "World");
    getCol().getDecks().allConf().get(0).getJSONObject("new").put("delays", new JSONArray(Arrays.asList(0.01, 10)));
    Card c = getCol().getSched().getCard();
    assertThat(c, notNullValue());
    getCol().getSched().answerCard(c, BUTTON_ONE);
}
Also used : JSONArray(com.ichi2.utils.JSONArray) Card(com.ichi2.libanki.Card) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)39 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)26 JSONArray (com.ichi2.utils.JSONArray)23 Test (org.junit.Test)22 RobolectricTest (com.ichi2.anki.RobolectricTest)14 ConfirmationDialog (com.ichi2.anki.dialogs.ConfirmationDialog)12 Collection (com.ichi2.libanki.Collection)12 Model (com.ichi2.libanki.Model)12 Note (com.ichi2.libanki.Note)8 ArrayList (java.util.ArrayList)8 JSONException (com.ichi2.utils.JSONException)6 HashMap (java.util.HashMap)5 SdkSuppress (androidx.test.filters.SdkSuppress)4 TaskData (com.ichi2.async.TaskData)4 ModelManager (com.ichi2.libanki.ModelManager)4 TextImporter (com.ichi2.libanki.importer.TextImporter)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Cursor (android.database.Cursor)3 Uri (android.net.Uri)3 Bundle (android.os.Bundle)3