Search in sources :

Example 46 with Model

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

the class NoteEditorTest method stickyFieldsAreUnchangedAfterAdd.

@Test
public void stickyFieldsAreUnchangedAfterAdd() {
    // #6795 - newlines were converted to <br>
    Model basic = makeNoteForType(NoteType.BASIC);
    // Enable sticky "Front" field
    basic.getJSONArray("flds").getJSONObject(0).put("sticky", true);
    String initFirstField = "Hello";
    String initSecondField = "unused";
    // /r/n on Windows under Robolectric
    String newFirstField = "Hello" + FieldEditText.NEW_LINE + "World";
    NoteEditor editor = getNoteEditorAdding(NoteType.BASIC).withFirstField(initFirstField).withSecondField(initSecondField).build();
    assertThat(Arrays.asList(editor.getCurrentFieldStrings()), contains(initFirstField, initSecondField));
    editor.setFieldValueFromUi(0, newFirstField);
    assertThat(Arrays.asList(editor.getCurrentFieldStrings()), contains(newFirstField, initSecondField));
    editor.saveNote();
    this.waitForAsyncTasksToComplete();
    List<String> actual = Arrays.asList(editor.getCurrentFieldStrings());
    assertThat("newlines should be preserved, second field should be blanked", actual, contains(newFirstField, ""));
}
Also used : Model(com.ichi2.libanki.Model) Test(org.junit.Test)

Example 47 with Model

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

the class ReviewerTest method baseDeckName.

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

Example 48 with Model

use of com.ichi2.libanki.Model 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 49 with Model

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

the class RobolectricTest method addNonClozeModel.

protected String addNonClozeModel(String name, String[] fields, String qfmt, String afmt) {
    Model model = getCol().getModels().newModel(name);
    for (int i = 0; i < fields.length; i++) {
        addField(model, fields[i]);
    }
    model.put(FlashCardsContract.CardTemplate.QUESTION_FORMAT, qfmt);
    model.put(FlashCardsContract.CardTemplate.ANSWER_FORMAT, afmt);
    getCol().getModels().add(model);
    getCol().getModels().flush();
    return name;
}
Also used : Model(com.ichi2.libanki.Model)

Example 50 with Model

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

the class CardTest method test_genrem.

@Test
public void test_genrem() {
    Collection col = getCol();
    Note note = col.newNote();
    note.setItem("Front", "1");
    note.setItem("Back", "");
    col.addNote(note);
    assertEquals(1, note.numberOfCards());
    Model m = col.getModels().current();
    Models mm = col.getModels();
    // adding a new template should automatically create cards
    JSONObject t = Models.newTemplate("rev");
    t.put("qfmt", "{{Front}}");
    t.put("afmt", "");
    mm.addTemplateModChanged(m, t);
    mm.save(m, true);
    assertEquals(2, note.numberOfCards());
    // if the template is changed to remove cards, they'll be removed
    t = m.getJSONArray("tmpls").getJSONObject(1);
    t.put("qfmt", "{{Back}}");
    mm.save(m, true);
    List<Long> rep = col.emptyCids();
    col.remCards(rep);
    assertEquals(1, note.numberOfCards());
    // if we add to the note, a card should be automatically generated
    note.load();
    note.setItem("Back", "1");
    note.flush();
    assertEquals(2, note.numberOfCards());
}
Also used : JSONObject(com.ichi2.utils.JSONObject) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)124 Model (com.ichi2.libanki.Model)95 Test (org.junit.Test)82 JSONArray (com.ichi2.utils.JSONArray)79 Collection (com.ichi2.libanki.Collection)53 ArrayList (java.util.ArrayList)48 Note (com.ichi2.libanki.Note)40 RobolectricTest (com.ichi2.anki.RobolectricTest)38 JSONException (com.ichi2.utils.JSONException)32 Intent (android.content.Intent)30 Card (com.ichi2.libanki.Card)27 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)26 HashMap (java.util.HashMap)22 Bundle (android.os.Bundle)20 NonNull (androidx.annotation.NonNull)20 SuppressLint (android.annotation.SuppressLint)16 View (android.view.View)16 ConfirmationDialog (com.ichi2.anki.dialogs.ConfirmationDialog)15 IOException (java.io.IOException)15 Nullable (androidx.annotation.Nullable)14