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) + ")"));
}
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));
}
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);
}
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, ""));
}
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));
}
Aggregations