Search in sources :

Example 76 with Template

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

the class AbstractFlashcardViewer method displayCardAnswer.

protected void displayCardAnswer() {
    // #7294 Required in case the animation end action does not fire:
    actualHideEaseButtons();
    Timber.d("displayCardAnswer()");
    mMissingImageHandler.onCardSideChange();
    mBackButtonPressedToReturn = false;
    // prevent answering (by e.g. gestures) before card is loaded
    if (mCurrentCard == null) {
        return;
    }
    // TODO needs testing: changing a card's model without flipping it back to the front
    // (such as editing a card, then editing the card template)
    mTypeAnswer.updateInfo(mCurrentCard, getResources());
    // but sometimes failed to do so (e.g. if an OnKeyListener is attached).
    if (mTypeAnswer.validForEditText()) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(mAnswerField.getWindowToken(), 0);
    }
    sDisplayAnswer = true;
    mSoundPlayer.stopSounds();
    mAnswerField.setVisibility(View.GONE);
    // Clean up the user answer and the correct answer
    if (!mTypeAnswer.useInputTag()) {
        mTypeAnswer.setInput(mAnswerField.getText().toString());
    }
    mIsSelecting = false;
    CardHtml answerContent = mHtmlGenerator.generateHtml(mCurrentCard, false, Side.BACK);
    updateCard(answerContent);
    displayAnswerBottomBar();
    mAutomaticAnswer.onDisplayAnswer();
    // If Card-based TTS is enabled, we "automatic display" after the TTS has finished as we don't know the duration
    if (!mTTS.isEnabled()) {
        mAutomaticAnswer.scheduleAutomaticDisplayQuestion(mUseTimerDynamicMS);
    }
}
Also used : InputMethodManager(android.view.inputmethod.InputMethodManager) CardHtml(com.ichi2.anki.cardviewer.CardHtml)

Example 77 with Template

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

the class ImportTest method testAnki2DiffmodelTemplates.

@Test
public void testAnki2DiffmodelTemplates() throws IOException, JSONException, ImportExportException {
    // different from the above as this one tests only the template text being
    // changed, not the number of cards/fields
    // import the first version of the model
    String tmp = Shared.getTestFilePath(getTestContext(), "diffmodeltemplates-1.apkg");
    AnkiPackageImporter imp = new AnkiPackageImporter(mTestCol, tmp);
    imp.setDupeOnSchemaChange(true);
    imp.run();
    // then the version with updated template
    tmp = Shared.getTestFilePath(getTestContext(), "diffmodeltemplates-2.apkg");
    imp = new AnkiPackageImporter(mTestCol, tmp);
    imp.setDupeOnSchemaChange(true);
    imp.run();
    // collection should contain the note we imported
    assertEquals(1, mTestCol.noteCount());
    // the front template should contain the text added in the 2nd package
    Long tcid = mTestCol.findCards("").get(0);
    Note tnote = mTestCol.getCard(tcid).note();
    assertTrue(mTestCol.findTemplates(tnote).get(0).getString("qfmt").contains("Changed Front Template"));
}
Also used : AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) Note(com.ichi2.libanki.Note) Test(org.junit.Test) InstrumentedTest(com.ichi2.anki.tests.InstrumentedTest)

Example 78 with Template

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

the class ModelTest method test_frontSide_field.

@Test
public void test_frontSide_field() {
    // #8951 - Anki Special-cases {{FrontSide}} on the front to return empty string
    Collection col = getCol();
    Model m = col.getModels().current();
    m.getJSONArray("tmpls").getJSONObject(0).put("qfmt", "{{Front}}{{FrontSide}}");
    col.getModels().save(m);
    Note note = col.newNote();
    note.setItem("Front", "helloworld");
    col.addNote(note);
    Card card = note.firstCard();
    String q = card.q();
    assertThat("field should be at the end of the template - empty string for front", q, endsWith("helloworld"));
    assertThat("field should not have a problem", q, not(containsString("has a problem")));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 79 with Template

use of com.ichi2.libanki.template.Template 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

JSONObject (com.ichi2.utils.JSONObject)53 Test (org.junit.Test)35 JSONArray (com.ichi2.utils.JSONArray)28 Model (com.ichi2.libanki.Model)24 ArrayList (java.util.ArrayList)16 Intent (android.content.Intent)14 RobolectricTest (com.ichi2.anki.RobolectricTest)14 Collection (com.ichi2.libanki.Collection)12 Note (com.ichi2.libanki.Note)12 SuppressLint (android.annotation.SuppressLint)9 HashMap (java.util.HashMap)8 Bundle (android.os.Bundle)7 JSONException (com.ichi2.utils.JSONException)7 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)6 Card (com.ichi2.libanki.Card)6 ShadowActivity (org.robolectric.shadows.ShadowActivity)6 ShadowIntent (org.robolectric.shadows.ShadowIntent)6 View (android.view.View)5 ContentResolver (android.content.ContentResolver)4 ContentValues (android.content.ContentValues)4