Search in sources :

Example 66 with Template

use of com.ichi2.libanki.template.Template in project Anki-Android by ankidroid.

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 = getContentResolver();
    Collection col = getCol();
    // Add a new basic model that we use for testing purposes (existing models could potentially be corrupted)
    Model model = StdModels.BASIC_MODEL.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);
}
Also used : ContentValues(android.content.ContentValues) JSONObject(com.ichi2.utils.JSONObject) Model(com.ichi2.libanki.Model) Collection(com.ichi2.libanki.Collection) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) Test(org.junit.Test)

Example 67 with Template

use of com.ichi2.libanki.template.Template in project Anki-Android by ankidroid.

the class ContentProviderTest method testInsertAndUpdateModel.

/**
 * Check that inserting a new model works as expected
 */
@Test
public void testInsertAndUpdateModel() {
    final ContentResolver cr = 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
        }
    }
}
Also used : ContentValues(android.content.ContentValues) JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray) Model(com.ichi2.libanki.Model) Collection(com.ichi2.libanki.Collection) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) Test(org.junit.Test)

Example 68 with Template

use of com.ichi2.libanki.template.Template in project Anki-Android by ankidroid.

the class CardTemplatePreviewerTest method testPreviewNormal.

@Test
public void testPreviewNormal() {
    // 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 = 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) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) View(android.view.View) Card(com.ichi2.libanki.Card) Test(org.junit.Test)

Example 69 with Template

use of com.ichi2.libanki.template.Template in project Anki-Android by ankidroid.

the class CardTemplatePreviewerTest method testPreviewUnsavedTemplate.

@Test
public void testPreviewUnsavedTemplate() {
    String modelName = "Basic";
    Model collectionBasicModelOriginal = getCurrentDatabaseModelCopy(modelName);
    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();
    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 = 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) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) View(android.view.View) Test(org.junit.Test)

Example 70 with Template

use of com.ichi2.libanki.template.Template in project Anki-Android by ankidroid.

the class CardTemplatePreviewerTest method testPreviewUnsavedTemplate_basic_answer.

@Test
@Config(qualifiers = "en")
public void testPreviewUnsavedTemplate_basic_answer() {
    String modelName = "Basic (type in the answer)";
    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(testCardTemplatePreviewer.getString(R.string.basic_answer_sample_text)));
}
Also used : JSONObject(com.ichi2.utils.JSONObject) Model(com.ichi2.libanki.Model) Intent(android.content.Intent) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)53 Test (org.junit.Test)35 JSONArray (com.ichi2.utils.JSONArray)28 Model (com.ichi2.libanki.Model)24 ArrayList (java.util.ArrayList)16 Intent (android.content.Intent)14 RobolectricTest (com.ichi2.anki.RobolectricTest)14 Collection (com.ichi2.libanki.Collection)12 Note (com.ichi2.libanki.Note)12 SuppressLint (android.annotation.SuppressLint)9 HashMap (java.util.HashMap)8 Bundle (android.os.Bundle)7 JSONException (com.ichi2.utils.JSONException)7 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)6 Card (com.ichi2.libanki.Card)6 ShadowActivity (org.robolectric.shadows.ShadowActivity)6 ShadowIntent (org.robolectric.shadows.ShadowIntent)6 View (android.view.View)5 ContentResolver (android.content.ContentResolver)4 ContentValues (android.content.ContentValues)4