Search in sources :

Example 71 with Model

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

the class CardTemplatePreviewerTest method testPreviewUnsavedTemplate_Basic.

@Test
public void testPreviewUnsavedTemplate_Basic() {
    String modelName = "Basic";
    Model collectionBasicModelOriginal = getCurrentDatabaseModelCopy(modelName);
    List<String> fields = collectionBasicModelOriginal.getFieldsNames();
    JSONObject template = collectionBasicModelOriginal.getJSONArray("tmpls").getJSONObject(0);
    template.put("qfmt", template.getString("qfmt") + "PREVIEWER_TEST");
    String tempModelPath = TemporaryModel.saveTempModel(getTargetContext(), collectionBasicModelOriginal);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.putExtra(TemporaryModel.INTENT_MODEL_FILENAME, tempModelPath);
    intent.putExtra("index", 0);
    ActivityController<TestCardTemplatePreviewer> previewerController = Robolectric.buildActivity(TestCardTemplatePreviewer.class, intent).create().start().resume().visible();
    saveControllerForCleanup((previewerController));
    TestCardTemplatePreviewer testCardTemplatePreviewer = previewerController.get();
    String[] arr = testCardTemplatePreviewer.getDummyCard(collectionBasicModelOriginal, 0).note().getFields();
    assertThat(arr[0], is("(" + fields.get(0) + ")"));
    assertThat(arr[1], is("(" + fields.get(1) + ")"));
}
Also used : JSONObject(com.ichi2.utils.JSONObject) Model(com.ichi2.libanki.Model) Intent(android.content.Intent) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 72 with Model

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

the class CardTemplatePreviewerTest method cardTemplatePreviewerNoCards_issue9687.

@Test
public void cardTemplatePreviewerNoCards_issue9687() {
    List<NoteService.NoteField> fields = new ArrayList<>();
    fields.add(new Field(0, ""));
    fields.add(new Field(1, ""));
    Model basicModel = getCurrentDatabaseModelCopy("Basic");
    String tempModelPath = TemporaryModel.saveTempModel(getTargetContext(), basicModel);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.putExtra(TemporaryModel.INTENT_MODEL_FILENAME, tempModelPath);
    Bundle noteEditorBundle = new Bundle();
    noteEditorBundle.putBundle("editFields", getFieldsAsBundleForPreview(fields));
    noteEditorBundle.putInt("ordinal", 0);
    noteEditorBundle.putLong("did", 1);
    intent.putExtra("noteEditorBundle", noteEditorBundle);
    TestCardTemplatePreviewer testCardTemplatePreviewer = super.startActivityNormallyOpenCollectionWithIntent(TestCardTemplatePreviewer.class, intent);
    assertThat("Activity should be finishing - no cards to show", testCardTemplatePreviewer.isFinishing(), is(true));
}
Also used : Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) Model(com.ichi2.libanki.Model) Intent(android.content.Intent) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 73 with Model

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

the class CardTemplatePreviewerTest method clozeFromEditorHasMultipleCards.

@Test
public void clozeFromEditorHasMultipleCards() {
    List<NoteService.NoteField> fields = new ArrayList<>();
    fields.add(new Field(0, "{{c1::Hello}} {{c3::World}}"));
    fields.add(new Field(1, "World"));
    Model basicModel = getCurrentDatabaseModelCopy("Cloze");
    String tempModelPath = TemporaryModel.saveTempModel(getTargetContext(), basicModel);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.putExtra(TemporaryModel.INTENT_MODEL_FILENAME, tempModelPath);
    Bundle noteEditorBundle = new Bundle();
    noteEditorBundle.putBundle("editFields", getFieldsAsBundleForPreview(fields));
    noteEditorBundle.putInt("ordinal", 0);
    noteEditorBundle.putLong("did", 1);
    intent.putExtra("noteEditorBundle", noteEditorBundle);
    TestCardTemplatePreviewer testCardTemplatePreviewer = super.startActivityNormallyOpenCollectionWithIntent(TestCardTemplatePreviewer.class, intent);
    assertTwoCards(testCardTemplatePreviewer);
}
Also used : Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) Model(com.ichi2.libanki.Model) Intent(android.content.Intent) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 74 with Model

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

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));
    saveNote(editor);
    RobolectricTest.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 75 with Model

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

the class NoteEditor method updateCards.

/**
 * Update the list of card templates for current note type
 */
private void updateCards(JSONObject model) {
    Timber.d("updateCards()");
    JSONArray tmpls = model.getJSONArray("tmpls");
    StringBuilder cardsList = new StringBuilder();
    // Build comma separated list of card names
    Timber.d("updateCards() template count is %s", tmpls.length());
    for (int i = 0; i < tmpls.length(); i++) {
        String name = tmpls.getJSONObject(i).optString("name");
        // If more than one card, and we have an existing card, underline existing card
        if (!mAddNote && tmpls.length() > 1 && model == mEditorNote.model() && mCurrentEditedCard != null && mCurrentEditedCard.template().optString("name").equals(name)) {
            name = "<u>" + name + "</u>";
        }
        cardsList.append(name);
        if (i < tmpls.length() - 1) {
            cardsList.append(", ");
        }
    }
    // Make cards list red if the number of cards is being reduced
    if (!mAddNote && tmpls.length() < mEditorNote.model().getJSONArray("tmpls").length()) {
        cardsList = new StringBuilder("<font color='red'>" + cardsList + "</font>");
    }
    mCardsButton.setText(HtmlCompat.fromHtml(getResources().getString(R.string.CardEditorCards, cardsList.toString()), HtmlCompat.FROM_HTML_MODE_LEGACY));
}
Also used : JSONArray(com.ichi2.utils.JSONArray) SuppressLint(android.annotation.SuppressLint)

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