use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.
the class CardTemplatePreviewerTest method testPreviewUnsavedTemplate.
@Test
public void testPreviewUnsavedTemplate() throws Exception {
String modelName = "Basic";
Model collectionBasicModelOriginal = getCurrentDatabaseModelCopy(modelName);
JSONObject template = collectionBasicModelOriginal.getJSONArray("tmpls").getJSONObject(0);
template.put("qfmt", template.getString("qfmt").concat("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 = (TestCardTemplatePreviewer) previewerController.get();
Assert.assertTrue("model change did not show up?", testCardTemplatePreviewer.getDummyCard(collectionBasicModelOriginal, 0).q().contains("PREVIEWER_TEST") && testCardTemplatePreviewer.getDummyCard(collectionBasicModelOriginal, 0).a().contains("PREVIEWER_TEST"));
// 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) previewerController.get();
Assert.assertTrue("model change not preserved in lifecycle??", testCardTemplatePreviewer.getDummyCard(collectionBasicModelOriginal, 0).q().contains("PREVIEWER_TEST") && testCardTemplatePreviewer.getDummyCard(collectionBasicModelOriginal, 0).a().contains("PREVIEWER_TEST"));
// 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.utils.JSONObject 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());
}
use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.
the class CollectionTest method test_noteAddDelete.
/**
*****************
** autogenerated from https://github.com/ankitects/anki/blob/2c73dcb2e547c44d9e02c20a00f3c52419dc277b/pylib/tests/test_cards.py *
******************
*/
/*TODO
@Test
public void test_create_open(){
(fd, path) = tempfile.mkstemp(suffix=".anki2", prefix="test_attachNew");
try {
os.close(fd);
os.unlink(path);
} catch (OSError) {
}
Collection col = aopen(path);
// for open()
String newPath = col.getPath();
long newMod = col.getMod();
col.close();
// reopen
col = aopen(newPath);
assertEquals(newMod, col.getMod());
col.close();
// non-writeable dir
if (isWin) {
String dir = "c:\root.anki2";
} else {
String dir = "/attachroot.anki2";
}
assertException(Exception, lambda: aopen(dir));
// reuse tmp file from before, test non-writeable file
os.chmod(newPath, 0);
assertException(Exception, lambda: aopen(newPath));
os.chmod(newPath, 0o666);
os.unlink(newPath);
} */
@Test
public void test_noteAddDelete() {
Collection col = getCol();
// add a note
Note note = col.newNote();
note.setItem("Front", "one");
note.setItem("Back", "two");
int n = col.addNote(note);
assertEquals(1, n);
// test multiple cards - add another template
Model m = col.getModels().current();
Models mm = col.getModels();
JSONObject t = Models.newTemplate("Reverse");
t.put("qfmt", "{{Back}}");
t.put("afmt", "{{Front}}");
mm.addTemplateModChanged(m, t);
// todo: remove true which is not upstream
mm.save(m, true);
assertEquals(2, col.cardCount());
// creating new notes should use both cards
note = col.newNote();
note.setItem("Front", "three");
note.setItem("Back", "four");
n = col.addNote(note);
assertEquals(2, n);
assertEquals(4, col.cardCount());
// check q/a generation
Card c0 = note.cards().get(0);
assertThat(c0.q(), containsString("three"));
// it should not be a duplicate
assertEquals(note.dupeOrEmpty(), Note.DupeOrEmpty.CORRECT);
// now let's make a duplicate
Note note2 = col.newNote();
note2.setItem("Front", "one");
note2.setItem("Back", "");
assertNotEquals(note2.dupeOrEmpty(), Note.DupeOrEmpty.CORRECT);
// empty first field should not be permitted either
note2.setItem("Front", " ");
assertNotEquals(note2.dupeOrEmpty(), Note.DupeOrEmpty.CORRECT);
}
use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.
the class DecksTest method duplicateName.
@Test
public void duplicateName() {
Decks decks = getCol().getDecks();
decks.load("{2: {\"name\": \"A\", \"id\":2}, 3: {\"name\": \"A\", \"id\":3}, 4: {\"name\": \"A::B\", \"id\":4}}", "{}");
decks.checkIntegrity();
JSONObject deckA = decks.byName("A");
Asserts.notNull(deckA, "A deck with name \"A\" should still exists");
assertThat("A deck with name \"A\" should have name \"A\"", deckA.getString("name"), is("A"));
JSONObject deckAPlus = decks.byName("A+");
Asserts.notNull(deckAPlus, "A deck with name \"A+\" should still exists");
}
use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.
the class ModelTest method test_chained_mods.
@Test
public void test_chained_mods() throws ConfirmModSchemaException {
Collection col = getCol();
col.getModels().setCurrent(col.getModels().byName("Cloze"));
Model m = col.getModels().current();
Models mm = col.getModels();
// We replace the default Cloze template
JSONObject t = Models.newTemplate("ChainedCloze");
t.put("qfmt", "{{cloze:text:Text}}");
t.put("afmt", "{{cloze:text:Text}}");
mm.addTemplateModChanged(m, t);
mm.save(m);
col.getModels().remTemplate(m, m.getJSONArray("tmpls").getJSONObject(0));
Note note = col.newNote();
String q1 = "<span style=\"color:red\">phrase</span>";
String a1 = "<b>sentence</b>";
String q2 = "<span style=\"color:red\">en chaine</span>";
String a2 = "<i>chained</i>";
note.setItem("Text", "This {{c1::" + q1 + "::" + a1 + "}} demonstrates {{c1::" + q2 + "::" + a2 + "}} clozes.");
assertEquals(1, col.addNote(note));
String question = note.cards().get(0).q();
/* TODO: chained modifier
assertThat("Question «"+question+"» does not contain the expected string", question, containsString("This <span class=cloze>[sentence]</span> demonstrates <span class=cloze>[chained]</span> clozes.")
);
assertThat(note.cards().get(0).a(), containsString("This <span class=cloze>phrase</span> demonstrates <span class=cloze>en chaine</span> clozes."
));
*/
}
Aggregations