use of com.ichi2.libanki.template.Template in project AnkiChinaAndroid by ankichinateam.
the class CardTemplateEditorTest method testDeletePendingAddExistingCardCount.
/**
* Deleting a template you just added - but in the same ordinal as a previous pending delete - should get it's card count correct
*/
@SuppressWarnings("PMD.ExcessiveMethodLength")
@Test
public void testDeletePendingAddExistingCardCount() {
String modelName = "Basic (optional reversed card)";
Model collectionBasicModelOriginal = getCurrentDatabaseModelCopy(modelName);
// Start the CardTemplateEditor with a specific model, and make sure the model starts unchanged
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra("modelId", collectionBasicModelOriginal.getLong("id"));
ActivityController<CardTemplateEditor> templateEditorController = Robolectric.buildActivity(CardTemplateEditor.class, intent).create().start().resume().visible();
saveControllerForCleanup(templateEditorController);
CardTemplateEditor testEditor = (CardTemplateEditor) templateEditorController.get();
Assert.assertFalse("Model should not have changed yet", testEditor.modelHasChanged());
Assert.assertEquals("Model should have 2 templates now", 2, testEditor.getTempModel().getTemplateCount());
Assert.assertFalse("Ordinal pending add?", TemporaryModel.isOrdinalPendingAdd(testEditor.getTempModel(), 0));
Assert.assertFalse("Ordinal pending add?", TemporaryModel.isOrdinalPendingAdd(testEditor.getTempModel(), 1));
// Create note with forward and back info
Note selectiveGeneratedNote = getCol().newNote(collectionBasicModelOriginal);
selectiveGeneratedNote.setField(0, "TestFront");
selectiveGeneratedNote.setField(1, "TestBack");
selectiveGeneratedNote.setField(2, "y");
getCol().addNote(selectiveGeneratedNote);
Assert.assertEquals("card generation should result in two cards", 2, getModelCardCount(collectionBasicModelOriginal));
// Delete ord 1 / 'Card 2' and check the message
ShadowActivity shadowTestEditor = shadowOf(testEditor);
testEditor.mViewPager.setCurrentItem(1);
Assert.assertTrue("Unable to click?", shadowTestEditor.clickMenuItem(R.id.action_delete));
advanceRobolectricLooper();
Assert.assertEquals("Did not show dialog about deleting template and it's card?", getQuantityString(R.plurals.card_template_editor_confirm_delete, 1, 1, "Card 2"), getDialogText(true));
clickDialogButton(DialogAction.POSITIVE, true);
advanceRobolectricLooper();
Assert.assertTrue("Model should have changed", testEditor.modelHasChanged());
Assert.assertNotNull("Cannot delete template?", getCol().getModels().getCardIdsForModel(collectionBasicModelOriginal.getLong("id"), new int[] { 0 }));
Assert.assertNotNull("Cannot delete template?", getCol().getModels().getCardIdsForModel(collectionBasicModelOriginal.getLong("id"), new int[] { 1 }));
Assert.assertNull("Can delete both templates?", getCol().getModels().getCardIdsForModel(collectionBasicModelOriginal.getLong("id"), new int[] { 0, 1 }));
Assert.assertEquals("Change in database despite no save?", collectionBasicModelOriginal.toString().trim(), getCurrentDatabaseModelCopy(modelName).toString().trim());
Assert.assertEquals("Model should have 1 template", 1, testEditor.getTempModel().getTemplateCount());
// Add a template - click add, click confirm for card add, click confirm again for full sync
shadowTestEditor.clickMenuItem(R.id.action_add);
advanceRobolectricLooper();
Assert.assertTrue("Model should have changed", testEditor.modelHasChanged());
Assert.assertEquals("Change added but not adjusted correctly?", 1, TemporaryModel.getAdjustedAddOrdinalAtChangeIndex(testEditor.getTempModel(), 1));
Assert.assertFalse("Ordinal pending add?", TemporaryModel.isOrdinalPendingAdd(testEditor.getTempModel(), 0));
Assert.assertTrue("Ordinal not pending add?", TemporaryModel.isOrdinalPendingAdd(testEditor.getTempModel(), 1));
Assert.assertEquals("Model should have 2 templates", 2, testEditor.getTempModel().getTemplateCount());
// Delete ord 1 / 'Card 2' again and check the message - it's in the same spot as the pre-existing template but there are no cards actually associated
testEditor.mViewPager.setCurrentItem(1);
Assert.assertTrue("Unable to click?", shadowTestEditor.clickMenuItem(R.id.action_delete));
advanceRobolectricLooper();
Assert.assertEquals("Did not show dialog about deleting template and it's card?", getQuantityString(R.plurals.card_template_editor_confirm_delete, 0, 0, "Card 2"), getDialogText(true));
clickDialogButton(DialogAction.POSITIVE, true);
advanceRobolectricLooper();
Assert.assertTrue("Model should have changed", testEditor.modelHasChanged());
Assert.assertNotNull("Cannot delete template?", getCol().getModels().getCardIdsForModel(collectionBasicModelOriginal.getLong("id"), new int[] { 0 }));
Assert.assertNotNull("Cannot delete template?", getCol().getModels().getCardIdsForModel(collectionBasicModelOriginal.getLong("id"), new int[] { 1 }));
Assert.assertNull("Can delete both templates?", getCol().getModels().getCardIdsForModel(collectionBasicModelOriginal.getLong("id"), new int[] { 0, 1 }));
Assert.assertEquals("Change in database despite no save?", collectionBasicModelOriginal.toString().trim(), getCurrentDatabaseModelCopy(modelName).toString().trim());
Assert.assertEquals("Model should have 1 template", 1, testEditor.getTempModel().getTemplateCount());
// Save it out and make some assertions
Assert.assertTrue("Unable to click?", shadowTestEditor.clickMenuItem(R.id.action_confirm));
advanceRobolectricLooper();
Assert.assertFalse("Model should now be unchanged", testEditor.modelHasChanged());
Assert.assertEquals("card generation should result in 1 card", 1, getModelCardCount(collectionBasicModelOriginal));
}
use of com.ichi2.libanki.template.Template in project AnkiChinaAndroid by ankichinateam.
the class CardTemplateEditorTest method testTemplateAdd.
@Test
public void testTemplateAdd() throws Exception {
// Make sure we test previewing a new card template - not working for real yet
String modelName = "Basic";
JSONObject collectionBasicModelOriginal = getCurrentDatabaseModelCopy(modelName);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra("modelId", collectionBasicModelOriginal.getLong("id"));
ActivityController<CardTemplateEditor> templateEditorController = Robolectric.buildActivity(CardTemplateEditor.class, intent).create().start().resume().visible();
saveControllerForCleanup(templateEditorController);
CardTemplateEditor testEditor = (CardTemplateEditor) templateEditorController.get();
Assert.assertFalse("Ordinal pending add?", TemporaryModel.isOrdinalPendingAdd(testEditor.getTempModel(), 0));
// Try to add a template - click add, click confirm for card add, click confirm again for full sync
ShadowActivity shadowTestEditor = shadowOf(testEditor);
Assert.assertTrue("Unable to click?", shadowTestEditor.clickMenuItem(R.id.action_add));
advanceRobolectricLooper();
// if AnkiDroid moves to match AnkiDesktop it will pop a dialog to confirm card create
// Assert.assertEquals("Wrong dialog shown?", "This will create NN cards. Proceed?", getDialogText());
// clickDialogButton(DialogAction.POSITIVE);
Assert.assertTrue("Model should have changed", testEditor.modelHasChanged());
Assert.assertEquals("Change not pending add?", 1, TemporaryModel.getAdjustedAddOrdinalAtChangeIndex(testEditor.getTempModel(), 0));
Assert.assertFalse("Ordinal pending add?", TemporaryModel.isOrdinalPendingAdd(testEditor.getTempModel(), 0));
Assert.assertTrue("Ordinal not pending add?", TemporaryModel.isOrdinalPendingAdd(testEditor.getTempModel(), 1));
Assert.assertEquals("Model should have 2 templates now", 2, testEditor.getTempModel().getTemplateCount());
// Make sure we pass the new template to the Previewer
Assert.assertTrue("Unable to click?", shadowTestEditor.clickMenuItem(R.id.action_preview));
Intent startedIntent = shadowTestEditor.getNextStartedActivity();
ShadowIntent shadowIntent = shadowOf(startedIntent);
Assert.assertEquals("Previewer not started?", CardTemplatePreviewer.class.getName(), shadowIntent.getIntentClass().getName());
Assert.assertNotNull("intent did not have model JSON filename?", startedIntent.getStringExtra(TemporaryModel.INTENT_MODEL_FILENAME));
Assert.assertEquals("intent did not have ordinal?", 1, startedIntent.getIntExtra("ordinal", -1));
Assert.assertNotEquals("Model sent to Previewer is unchanged?", testEditor.getTempModel().getModel(), TemporaryModel.getTempModel(startedIntent.getStringExtra(TemporaryModel.INTENT_MODEL_FILENAME)));
Assert.assertEquals("Change already in database?", collectionBasicModelOriginal.toString().trim(), getCurrentDatabaseModelCopy(modelName).toString().trim());
// Save the change to the database and make sure there are two templates after
JSONObject testEditorModelEdited = testEditor.getTempModel().getModel();
Assert.assertTrue("Unable to click?", shadowTestEditor.clickMenuItem(R.id.action_confirm));
advanceRobolectricLooper();
JSONObject collectionBasicModelCopyEdited = getCurrentDatabaseModelCopy(modelName);
Assert.assertNotEquals("model is unchanged?", collectionBasicModelOriginal, collectionBasicModelCopyEdited);
Assert.assertEquals("model did not save?", testEditorModelEdited.toString().trim(), collectionBasicModelCopyEdited.toString().trim());
}
use of com.ichi2.libanki.template.Template in project AnkiChinaAndroid by ankichinateam.
the class CardTemplatePreviewerTest method testPreviewNormal.
@Test
public void testPreviewNormal() throws Exception {
// Make sure we test previewing a new card template
String modelName = "Basic (and reversed card)";
Model collectionBasicModelOriginal = getCurrentDatabaseModelCopy(modelName);
Card testCard1 = getSavedCard(collectionBasicModelOriginal, 0);
Card testCard2 = getSavedCard(collectionBasicModelOriginal, 1);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra("cardList", new long[] { testCard1.getId(), testCard2.getId() });
intent.putExtra("index", 0);
ActivityController<TestCardTemplatePreviewer> previewerController = Robolectric.buildActivity(TestCardTemplatePreviewer.class, intent).create().start().resume().visible();
saveControllerForCleanup(previewerController);
// Take it through a destroy/re-create lifecycle in order to test instance state persistence
Bundle outBundle = new Bundle();
previewerController.saveInstanceState(outBundle);
previewerController.pause().stop().destroy();
previewerController = Robolectric.buildActivity(TestCardTemplatePreviewer.class).create(outBundle).start().resume().visible();
saveControllerForCleanup((previewerController));
TestCardTemplatePreviewer testCardTemplatePreviewer = (TestCardTemplatePreviewer) previewerController.get();
// Make sure we can click
Assert.assertFalse("Showing the answer already?", testCardTemplatePreviewer.getShowingAnswer());
testCardTemplatePreviewer.disableDoubleClickPrevention();
View showAnswerButton = testCardTemplatePreviewer.findViewById(R.id.preview_buttons_layout);
showAnswerButton.performClick();
Assert.assertTrue("Not showing the answer?", testCardTemplatePreviewer.getShowingAnswer());
}
use of com.ichi2.libanki.template.Template in project AnkiChinaAndroid by ankichinateam.
the class ContentProviderTest method testInsertAndUpdateModel.
/**
* Check that inserting a new model works as expected
*/
@Test
public void testInsertAndUpdateModel() {
final ContentResolver cr = InstrumentationRegistry.getInstrumentation().getTargetContext().getContentResolver();
ContentValues cv = new ContentValues();
// Insert a new model
cv.put(FlashCardsContract.Model.NAME, TEST_MODEL_NAME);
cv.put(FlashCardsContract.Model.FIELD_NAMES, Utils.joinFields(TEST_MODEL_FIELDS));
cv.put(FlashCardsContract.Model.NUM_CARDS, TEST_MODEL_CARDS.length);
Uri modelUri = cr.insert(FlashCardsContract.Model.CONTENT_URI, cv);
assertNotNull("Check inserted model isn't null", modelUri);
assertNotNull("Check last path segment exists", modelUri.getLastPathSegment());
long mid = Long.parseLong(modelUri.getLastPathSegment());
Collection col = reopenCol();
try {
JSONObject model = col.getModels().get(mid);
assertNotNull("Check model", model);
assertEquals("Check model name", TEST_MODEL_NAME, model.getString("name"));
assertEquals("Check templates length", TEST_MODEL_CARDS.length, model.getJSONArray("tmpls").length());
assertEquals("Check field length", TEST_MODEL_FIELDS.length, model.getJSONArray("flds").length());
JSONArray fields = model.getJSONArray("flds");
for (int i = 0; i < fields.length(); i++) {
assertEquals("Check name of fields", TEST_MODEL_FIELDS[i], fields.getJSONObject(i).getString("name"));
}
// Test updating the model CSS (to test updating MODELS_ID Uri)
cv = new ContentValues();
cv.put(FlashCardsContract.Model.CSS, TEST_MODEL_CSS);
assertThat(cr.update(modelUri, cv, null, null), is(greaterThan(0)));
col = reopenCol();
model = col.getModels().get(mid);
assertNotNull("Check model", model);
assertEquals("Check css", TEST_MODEL_CSS, model.getString("css"));
// Update each of the templates in model (to test updating MODELS_ID_TEMPLATES_ID Uri)
for (int i = 0; i < TEST_MODEL_CARDS.length; i++) {
cv = new ContentValues();
cv.put(FlashCardsContract.CardTemplate.NAME, TEST_MODEL_CARDS[i]);
cv.put(FlashCardsContract.CardTemplate.QUESTION_FORMAT, TEST_MODEL_QFMT[i]);
cv.put(FlashCardsContract.CardTemplate.ANSWER_FORMAT, TEST_MODEL_AFMT[i]);
cv.put(FlashCardsContract.CardTemplate.BROWSER_QUESTION_FORMAT, TEST_MODEL_QFMT[i]);
cv.put(FlashCardsContract.CardTemplate.BROWSER_ANSWER_FORMAT, TEST_MODEL_AFMT[i]);
Uri tmplUri = Uri.withAppendedPath(Uri.withAppendedPath(modelUri, "templates"), Integer.toString(i));
assertThat("Update rows", cr.update(tmplUri, cv, null, null), is(greaterThan(0)));
col = reopenCol();
model = col.getModels().get(mid);
assertNotNull("Check model", model);
JSONObject template = model.getJSONArray("tmpls").getJSONObject(i);
assertEquals("Check template name", TEST_MODEL_CARDS[i], template.getString("name"));
assertEquals("Check qfmt", TEST_MODEL_QFMT[i], template.getString("qfmt"));
assertEquals("Check afmt", TEST_MODEL_AFMT[i], template.getString("afmt"));
assertEquals("Check bqfmt", TEST_MODEL_QFMT[i], template.getString("bqfmt"));
assertEquals("Check bafmt", TEST_MODEL_AFMT[i], template.getString("bafmt"));
}
} finally {
// Delete the model (this will force a full-sync)
col.modSchemaNoCheck();
try {
Model model = col.getModels().get(mid);
assertNotNull("Check model", model);
col.getModels().rem(model);
} catch (ConfirmModSchemaException e) {
// This will never happen
}
}
}
use of com.ichi2.libanki.template.Template in project AnkiChinaAndroid by ankichinateam.
the class ContentProviderTest method testInsertTemplate.
/**
* Check that inserting and removing a note into default deck works as expected
*/
@Test
public void testInsertTemplate() throws Exception {
// Get required objects for test
final ContentResolver cr = InstrumentationRegistry.getInstrumentation().getTargetContext().getContentResolver();
Collection col = getCol();
// Add a new basic model that we use for testing purposes (existing models could potentially be corrupted)
Model model = StdModels.basicModel.add(col, BASIC_MODEL_NAME);
long modelId = model.getLong("id");
// Add the note
Uri modelUri = ContentUris.withAppendedId(FlashCardsContract.Model.CONTENT_URI, modelId);
// choose the last one because not the same as the basic model template
int testIndex = TEST_MODEL_CARDS.length - 1;
int expectedOrd = model.getJSONArray("tmpls").length();
ContentValues cv = new ContentValues();
cv.put(FlashCardsContract.CardTemplate.NAME, TEST_MODEL_CARDS[testIndex]);
cv.put(FlashCardsContract.CardTemplate.QUESTION_FORMAT, TEST_MODEL_QFMT[testIndex]);
cv.put(FlashCardsContract.CardTemplate.ANSWER_FORMAT, TEST_MODEL_AFMT[testIndex]);
cv.put(FlashCardsContract.CardTemplate.BROWSER_QUESTION_FORMAT, TEST_MODEL_QFMT[testIndex]);
cv.put(FlashCardsContract.CardTemplate.BROWSER_ANSWER_FORMAT, TEST_MODEL_AFMT[testIndex]);
Uri templatesUri = Uri.withAppendedPath(modelUri, "templates");
Uri templateUri = cr.insert(templatesUri, cv);
// test that the changes are physically saved to the DB
col = reopenCol();
assertNotNull("Check template uri", templateUri);
assertEquals("Check template uri ord", expectedOrd, ContentUris.parseId(templateUri));
model = col.getModels().get(modelId);
assertNotNull("Check model", model);
JSONObject template = model.getJSONArray("tmpls").getJSONObject(expectedOrd);
assertEquals("Check template JSONObject ord", expectedOrd, template.getInt("ord"));
assertEquals("Check template name", TEST_MODEL_CARDS[testIndex], template.getString("name"));
assertEquals("Check qfmt", TEST_MODEL_QFMT[testIndex], template.getString("qfmt"));
assertEquals("Check afmt", TEST_MODEL_AFMT[testIndex], template.getString("afmt"));
assertEquals("Check bqfmt", TEST_MODEL_QFMT[testIndex], template.getString("bqfmt"));
assertEquals("Check bafmt", TEST_MODEL_AFMT[testIndex], template.getString("bafmt"));
col.getModels().rem(model);
}
Aggregations