Search in sources :

Example 11 with START

use of com.ichi2.anim.ActivityTransitionAnimation.Direction.START in project AnkiChinaAndroid by ankichinateam.

the class CardTemplateEditorTest method testDeleteTemplate.

@Test
public void testDeleteTemplate() {
    String modelName = "Basic (and reversed card)";
    // Start the CardTemplateEditor with a specific model, and make sure the model starts unchanged
    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("Model should not have changed yet", testEditor.modelHasChanged());
    Assert.assertEquals("Model should have 2 templates now", 2, testEditor.getTempModel().getTemplateCount());
    // Try to delete the template - click delete, click confirm for card delete, click confirm again for full sync
    ShadowActivity shadowTestEditor = shadowOf(testEditor);
    Assert.assertTrue("Unable to click?", shadowTestEditor.clickMenuItem(R.id.action_delete));
    advanceRobolectricLooper();
    Assert.assertEquals("Wrong dialog shown?", "Delete the “Card 1” card type, and its 0 cards?", getDialogText(true));
    clickDialogButton(DialogAction.POSITIVE, true);
    advanceRobolectricLooper();
    Assert.assertTrue("Model should have changed", testEditor.modelHasChanged());
    Assert.assertEquals("Model should have 1 template now", 1, testEditor.getTempModel().getTemplateCount());
    // Try to delete the template again, but there's only one
    Assert.assertTrue("Unable to click?", shadowTestEditor.clickMenuItem(R.id.action_delete));
    advanceRobolectricLooper();
    Assert.assertEquals("Did not show dialog about deleting only card?", getResourceString(R.string.card_template_editor_cant_delete), getDialogText(true));
    Assert.assertEquals("Change already in database?", collectionBasicModelOriginal.toString().trim(), getCurrentDatabaseModelCopy(modelName).toString().trim());
    // Save the change to the database and make sure there's only one template 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());
}
Also used : JSONObject(com.ichi2.utils.JSONObject) ShadowActivity(org.robolectric.shadows.ShadowActivity) ShadowIntent(org.robolectric.shadows.ShadowIntent) Intent(android.content.Intent) Test(org.junit.Test)

Example 12 with START

use of com.ichi2.anim.ActivityTransitionAnimation.Direction.START in project AnkiChinaAndroid by ankichinateam.

the class CardTemplateEditorTest method testDeleteTemplateWithSelectivelyGeneratedCards.

/**
 * In a model with two card templates using different fields, some notes may only use card 1,
 * and some may only use card 2. If you delete the 2nd template,
 * it will cause the notes that only use card 2 to disappear.
 *
 * So the unit test would then be to make a model like the "basic (optional reverse card)"
 * with two fields Enable1 and Enable2, and two templates "card 1" and "card 2".
 * Both cards use selective generation, so they're empty unless the corresponding field is set.
 *
 * So then in the unit test you make the model, add the two templates, then you add two notes,
 * with Enable1 and Enable2 respectively set to "y".
 * Then you try to delete one of the templates and it should fail
 *
 * (question: but I thought deleting one should work - still one card left to maintain the note,
 * and second template delete should fail since we finally get to a place where no cards are left?
 * I am having trouble creating selectively generated cards though - I can do one optional field but not 2 ugh)
 */
@Test
public void testDeleteTemplateWithSelectivelyGeneratedCards() {
    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));
    // Try to delete Card 1 template - click delete, check confirm for card delete popup indicating it was possible, then dismiss it
    ShadowActivity shadowTestEditor = shadowOf(testEditor);
    Assert.assertTrue("Unable to click?", shadowTestEditor.clickMenuItem(R.id.action_delete));
    advanceRobolectricLooper();
    Assert.assertEquals("Wrong dialog shown?", "Delete the “Card 1” card type, and its 0 cards?", getDialogText(true));
    clickDialogButton(DialogAction.NEGATIVE, true);
    advanceRobolectricLooper();
    Assert.assertFalse("Model should not have changed", testEditor.modelHasChanged());
    // Create note with forward and back info, Add Reverse is empty, so should only be one card
    Note selectiveGeneratedNote = getCol().newNote(collectionBasicModelOriginal);
    selectiveGeneratedNote.setField(0, "TestFront");
    selectiveGeneratedNote.setField(1, "TestBack");
    String[] fields = selectiveGeneratedNote.getFields();
    for (String field : fields) {
        Timber.d("Got a field: %s", field);
    }
    getCol().addNote(selectiveGeneratedNote);
    Assert.assertEquals("selective generation should result in one card", 1, getModelCardCount(collectionBasicModelOriginal));
    // Try to delete the template again, but there's selective generation means it would orphan the note
    Assert.assertTrue("Unable to click?", shadowTestEditor.clickMenuItem(R.id.action_delete));
    advanceRobolectricLooper();
    Assert.assertEquals("Did not show dialog about deleting only card?", getResourceString(R.string.card_template_editor_would_delete_note), getDialogText(true));
    clickDialogButton(DialogAction.POSITIVE, true);
    advanceRobolectricLooper();
    Assert.assertNull("Can delete used template?", getCol().getModels().getCardIdsForModel(collectionBasicModelOriginal.getLong("id"), new int[] { 0 }));
    Assert.assertEquals("Change already in database?", collectionBasicModelOriginal.toString().trim(), getCurrentDatabaseModelCopy(modelName).toString().trim());
    Assert.assertFalse("Ordinal pending add?", TemporaryModel.isOrdinalPendingAdd(testEditor.getTempModel(), 0));
    Assert.assertEquals("Change incorrectly added to list?", 0, testEditor.getTempModel().getTemplateChanges().size());
    // Assert can delete 'Card 2'
    Assert.assertNotNull("Cannot delete unused template?", getCol().getModels().getCardIdsForModel(collectionBasicModelOriginal.getLong("id"), new int[] { 1 }));
    // Edit note to have Add Reverse set to 'y' so we get a second card
    selectiveGeneratedNote.setField(2, "y");
    selectiveGeneratedNote.flush();
    // - assert two cards
    Assert.assertEquals("should be two cards now", 2, getModelCardCount(collectionBasicModelOriginal));
    // - assert can delete either Card template but not both
    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 }));
    // A couple more notes to make sure things are okay
    Note secondNote = getCol().newNote(collectionBasicModelOriginal);
    secondNote.setField(0, "TestFront2");
    secondNote.setField(1, "TestBack2");
    secondNote.setField(2, "y");
    getCol().addNote(secondNote);
    // - assert can delete either Card template but not both
    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 }));
}
Also used : Note(com.ichi2.libanki.Note) Model(com.ichi2.libanki.Model) ShadowActivity(org.robolectric.shadows.ShadowActivity) ShadowIntent(org.robolectric.shadows.ShadowIntent) Intent(android.content.Intent) Test(org.junit.Test)

Example 13 with START

use of com.ichi2.anim.ActivityTransitionAnimation.Direction.START 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());
}
Also used : JSONObject(com.ichi2.utils.JSONObject) Bundle(android.os.Bundle) Model(com.ichi2.libanki.Model) Intent(android.content.Intent) View(android.view.View) Test(org.junit.Test)

Example 14 with START

use of com.ichi2.anim.ActivityTransitionAnimation.Direction.START in project AnkiChinaAndroid by ankichinateam.

the class SchedV2Test method test_reorder.

@Test
public void test_reorder() throws Exception {
    Collection col = getColV2();
    // add a note with default deck
    Note note = col.newNote();
    note.setItem("Front", "one");
    col.addNote(note);
    Note note2 = col.newNote();
    note2.setItem("Front", "two");
    col.addNote(note2);
    assertEquals(2, note2.cards().get(0).getDue());
    boolean found = false;
    // 50/50 chance of being reordered
    for (int i = 0; i < 20; i++) {
        col.getSched().randomizeCards(1);
        if (note.cards().get(0).getDue() != note.getId()) {
            found = true;
            break;
        }
    }
    assertTrue(found);
    col.getSched().orderCards(1);
    assertEquals(1, note.cards().get(0).getDue());
    // shifting
    Note note3 = col.newNote();
    note3.setItem("Front", "three");
    col.addNote(note3);
    Note note4 = col.newNote();
    note4.setItem("Front", "four");
    col.addNote(note4);
    assertEquals(1, note.cards().get(0).getDue());
    assertEquals(2, note2.cards().get(0).getDue());
    assertEquals(3, note3.cards().get(0).getDue());
    assertEquals(4, note4.cards().get(0).getDue());
/* todo: start
           col.getSched().sortCards(new long [] {note3.cards().get(0).getId(), note4.cards().get(0).getId()}, start=1, shift=true);
           assertEquals(3, note.cards().get(0).getDue());
           assertEquals(4, note2.cards().get(0).getDue());
           assertEquals(1, note3.cards().get(0).getDue());
           assertEquals(2, note4.cards().get(0).getDue());
        */
}
Also used : Note(com.ichi2.libanki.Note) Collection(com.ichi2.libanki.Collection) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 15 with START

use of com.ichi2.anim.ActivityTransitionAnimation.Direction.START in project AnkiChinaAndroid by ankichinateam.

the class SchedTest method test_reorderV1.

@Test
public void test_reorderV1() throws Exception {
    Collection col = getColV1();
    // add a note with default deck
    Note note = col.newNote();
    note.setItem("Front", "one");
    col.addNote(note);
    Note note2 = col.newNote();
    note2.setItem("Front", "two");
    col.addNote(note2);
    assertEquals(2, note2.cards().get(0).getDue());
    boolean found = false;
    // 50/50 chance of being reordered
    for (int i = 0; i < 20; i++) {
        col.getSched().randomizeCards(1);
        if (note.cards().get(0).getDue() != note.getId()) {
            found = true;
            break;
        }
    }
    assertTrue(found);
    col.getSched().orderCards(1);
    assertEquals(1, note.cards().get(0).getDue());
    // shifting
    Note note3 = col.newNote();
    note3.setItem("Front", "three");
    col.addNote(note3);
    Note note4 = col.newNote();
    note4.setItem("Front", "four");
    col.addNote(note4);
    assertEquals(1, note.cards().get(0).getDue());
    assertEquals(2, note2.cards().get(0).getDue());
    assertEquals(3, note3.cards().get(0).getDue());
    assertEquals(4, note4.cards().get(0).getDue());
/* todo sortCard
           col.getSched().sortCards(new long [] {note3.cards().get(0).getId(), note4.cards().get(0).getId()}, start=1, shift=true);
           assertEquals(3, note.cards().get(0).getDue());
           assertEquals(4, note2.cards().get(0).getDue());
           assertEquals(1, note3.cards().get(0).getDue());
           assertEquals(2, note4.cards().get(0).getDue());
        */
}
Also used : Note(com.ichi2.libanki.Note) Collection(com.ichi2.libanki.Collection) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)23 Intent (android.content.Intent)21 JSONObject (com.ichi2.utils.JSONObject)17 Model (com.ichi2.libanki.Model)14 View (android.view.View)13 ArrayList (java.util.ArrayList)13 Bundle (android.os.Bundle)12 Collection (com.ichi2.libanki.Collection)11 Note (com.ichi2.libanki.Note)9 File (java.io.File)9 JSONArray (com.ichi2.utils.JSONArray)8 SharedPreferences (android.content.SharedPreferences)7 TextView (android.widget.TextView)7 EditText (android.widget.EditText)6 Deck (com.ichi2.libanki.Deck)6 Dialog (android.app.Dialog)5 NonNull (androidx.annotation.NonNull)5 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)5 RobolectricTest (com.ichi2.anki.RobolectricTest)5 TaskData (com.ichi2.async.TaskData)5