Search in sources :

Example 36 with START

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

the class CardTemplateEditorTest method testEditTemplateContents.

@Test
@SuppressWarnings("PMD.NPathComplexity")
public void testEditTemplateContents() throws Exception {
    String modelName = "Basic";
    // 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());
    // Change the model and make sure it registers as changed, but the database is unchanged
    EditText templateFront = testEditor.findViewById(R.id.front_edit);
    String TEST_MODEL_QFMT_EDIT = "!@#$%^&*TEST*&^%$#@!";
    templateFront.getText().append(TEST_MODEL_QFMT_EDIT);
    advanceRobolectricLooper();
    Assert.assertTrue("Model did not change after edit?", testEditor.modelHasChanged());
    Assert.assertEquals("Change already in database?", collectionBasicModelOriginal.toString().trim(), getCurrentDatabaseModelCopy(modelName).toString().trim());
    // Kill and restart the Activity, make sure model edit is preserved
    Bundle outBundle = new Bundle();
    templateEditorController.saveInstanceState(outBundle);
    templateEditorController.pause().stop().destroy();
    templateEditorController = Robolectric.buildActivity(CardTemplateEditor.class).create(outBundle).start().resume().visible();
    saveControllerForCleanup(templateEditorController);
    testEditor = (CardTemplateEditor) templateEditorController.get();
    ShadowActivity shadowTestEditor = shadowOf(testEditor);
    Assert.assertTrue("model change not preserved across activity lifecycle?", testEditor.modelHasChanged());
    Assert.assertEquals("Change already in database?", collectionBasicModelOriginal.toString().trim(), getCurrentDatabaseModelCopy(modelName).toString().trim());
    // Make sure we get a confirmation dialog if we hit the back button
    Assert.assertTrue("Unable to click?", shadowTestEditor.clickMenuItem(android.R.id.home));
    advanceRobolectricLooper();
    Assert.assertEquals("Wrong dialog shown?", getDialogText(true), getResourceString(R.string.discard_unsaved_changes));
    clickDialogButton(DialogAction.NEGATIVE, true);
    advanceRobolectricLooper();
    Assert.assertTrue("model change not preserved despite canceling back button?", testEditor.modelHasChanged());
    // Make sure we things are cleared out after a cancel
    Assert.assertTrue("Unable to click?", shadowTestEditor.clickMenuItem(android.R.id.home));
    Assert.assertEquals("Wrong dialog shown?", getDialogText(true), getResourceString(R.string.discard_unsaved_changes));
    clickDialogButton(DialogAction.POSITIVE, true);
    advanceRobolectricLooper();
    Assert.assertFalse("model change not cleared despite discarding changes?", testEditor.modelHasChanged());
    // Get going for content edit assertions again...
    templateEditorController = Robolectric.buildActivity(CardTemplateEditor.class, intent).create().start().resume().visible();
    saveControllerForCleanup(templateEditorController);
    testEditor = (CardTemplateEditor) templateEditorController.get();
    shadowTestEditor = shadowOf(testEditor);
    templateFront = testEditor.findViewById(R.id.front_edit);
    templateFront.getText().append(TEST_MODEL_QFMT_EDIT);
    advanceRobolectricLooper();
    Assert.assertTrue("Model did not change after edit?", testEditor.modelHasChanged());
    // Make sure we pass the edit to the Previewer
    Assert.assertTrue("Unable to click?", shadowTestEditor.clickMenuItem(R.id.action_preview));
    advanceRobolectricLooper();
    Intent startedIntent = shadowTestEditor.getNextStartedActivity();
    ShadowIntent shadowIntent = shadowOf(startedIntent);
    advanceRobolectricLooper();
    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.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());
    shadowTestEditor.receiveResult(startedIntent, Activity.RESULT_OK, new Intent());
    // Save the template then fetch it from the collection to see if it was saved correctly
    JSONObject testEditorModelEdited = testEditor.getTempModel().getModel();
    advanceRobolectricLooper();
    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());
    Assert.assertTrue("model does not have our change?", collectionBasicModelCopyEdited.toString().contains(TEST_MODEL_QFMT_EDIT));
}
Also used : EditText(android.widget.EditText) ShadowIntent(org.robolectric.shadows.ShadowIntent) JSONObject(com.ichi2.utils.JSONObject) Bundle(android.os.Bundle) ShadowActivity(org.robolectric.shadows.ShadowActivity) ShadowIntent(org.robolectric.shadows.ShadowIntent) Intent(android.content.Intent) Test(org.junit.Test)

Example 37 with START

use of com.ichi2.anim.ActivityTransitionAnimation.Direction.START 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));
}
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 38 with START

use of com.ichi2.anim.ActivityTransitionAnimation.Direction.START 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());
}
Also used : ShadowIntent(org.robolectric.shadows.ShadowIntent) JSONObject(com.ichi2.utils.JSONObject) ShadowActivity(org.robolectric.shadows.ShadowActivity) ShadowIntent(org.robolectric.shadows.ShadowIntent) Intent(android.content.Intent) Test(org.junit.Test)

Example 39 with START

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

Example 40 with START

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

the class AbstractFlashcardViewerTest method getViewer.

private NonAbstractFlashcardViewer getViewer() {
    Note n = getCol().newNote();
    n.setField(0, "a");
    getCol().addNote(n);
    ActivityController<NonAbstractFlashcardViewer> multimediaController = Robolectric.buildActivity(NonAbstractFlashcardViewer.class, new Intent()).create().start().resume().visible();
    saveControllerForCleanup((multimediaController));
    NonAbstractFlashcardViewer viewer = (NonAbstractFlashcardViewer) multimediaController.get();
    viewer.onCollectionLoaded(getCol());
    viewer.loadInitialCard();
    // AsyncTasks spawned by by loading the viewer finish. Is there a way to synchronize these things while under test?
    try {
        Thread.sleep(2000);
    } catch (Throwable t) {
    /* nothing */
    }
    shadowOf(getMainLooper()).idle();
    return viewer;
}
Also used : Note(com.ichi2.libanki.Note) Intent(android.content.Intent)

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