Search in sources :

Example 41 with ConfirmModSchemaException

use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.

the class ReviewerTest method testMultipleCards.

@Test
public synchronized void testMultipleCards() throws ConfirmModSchemaException, InterruptedException {
    addNoteWithThreeCards();
    Collection col = getCol();
    JSONObject nw = col.getDecks().confForDid(1).getJSONObject("new");
    MockTime time = getCollectionTime();
    nw.put("delays", new JSONArray(new int[] { 1, 10, 60, 120 }));
    waitForAsyncTasksToComplete();
    Reviewer reviewer = startReviewer();
    waitForAsyncTasksToComplete();
    assertCounts(reviewer, 3, 0, 0);
    // card 1 is shown
    answerCardOrdinalAsGood(reviewer, 1);
    // card get scheduler in [10, 12.5] minutes
    time.addM(3);
    // We wait 3 minutes to ensure card 2 is scheduled after card 1
    // card 2 is shown
    answerCardOrdinalAsGood(reviewer, 2);
    // Same as above
    time.addM(3);
    // card 3 is shown
    answerCardOrdinalAsGood(reviewer, 3);
    undo(reviewer);
    assertCurrentOrdIs(reviewer, 3);
    // card 3 is shown
    answerCardOrdinalAsGood(reviewer, 3);
    // Anki Desktop shows "1"
    assertCurrentOrdIsNot(reviewer, 3);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray) Collection(com.ichi2.libanki.Collection) MockTime(com.ichi2.testutils.MockTime) Test(org.junit.Test)

Example 42 with ConfirmModSchemaException

use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.

the class ReviewerTest method cloneTemplate.

private void cloneTemplate(Models models, Model m) throws ConfirmModSchemaException {
    JSONArray tmpls = m.getJSONArray("tmpls");
    JSONObject defaultTemplate = tmpls.getJSONObject(0);
    JSONObject newTemplate = defaultTemplate.deepClone();
    newTemplate.put("ord", tmpls.length());
    newTemplate.put("name", "Card " + tmpls.length() + 1);
    models.addTemplate(m, newTemplate);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray)

Example 43 with ConfirmModSchemaException

use of com.ichi2.anki.exception.ConfirmModSchemaException 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
        }
    }
}
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 44 with ConfirmModSchemaException

use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.

the class ImportTest method testCsv2.

@Test
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
public void testCsv2() throws IOException, ConfirmModSchemaException {
    Models mm = testCol.getModels();
    Model m = mm.current();
    JSONObject f = mm.newField("Three");
    mm.addField(m, f);
    mm.save(m);
    Note n = testCol.newNote();
    n.setField(0, "1");
    n.setField(1, "2");
    n.setField(2, "3");
    testCol.addNote(n);
    // an update with unmapped fields should not clobber those fields
    String file = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "text-update.txt");
    TextImporter i = new TextImporter(testCol, file);
    i.initMapping();
    i.run();
    n.load();
    List<String> fields = Arrays.asList(n.getFields());
    assertThat(fields, contains("1", "x", "3"));
}
Also used : JSONObject(com.ichi2.utils.JSONObject) Note(com.ichi2.libanki.Note) Model(com.ichi2.libanki.Model) Models(com.ichi2.libanki.Models) TextImporter(com.ichi2.libanki.importer.TextImporter) Test(org.junit.Test) SdkSuppress(androidx.test.filters.SdkSuppress)

Example 45 with ConfirmModSchemaException

use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.

the class ImportTest method addFieldToCurrentModel.

private void addFieldToCurrentModel(String fieldName) throws ConfirmModSchemaException {
    Models mm = testCol.getModels();
    Model m = mm.current();
    JSONObject f = mm.newField(fieldName);
    mm.addField(m, f);
    mm.save(m);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) Model(com.ichi2.libanki.Model) Models(com.ichi2.libanki.Models)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)39 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)26 JSONArray (com.ichi2.utils.JSONArray)23 Test (org.junit.Test)22 RobolectricTest (com.ichi2.anki.RobolectricTest)14 ConfirmationDialog (com.ichi2.anki.dialogs.ConfirmationDialog)12 Collection (com.ichi2.libanki.Collection)12 Model (com.ichi2.libanki.Model)12 Note (com.ichi2.libanki.Note)8 ArrayList (java.util.ArrayList)8 JSONException (com.ichi2.utils.JSONException)6 HashMap (java.util.HashMap)5 SdkSuppress (androidx.test.filters.SdkSuppress)4 TaskData (com.ichi2.async.TaskData)4 ModelManager (com.ichi2.libanki.ModelManager)4 TextImporter (com.ichi2.libanki.importer.TextImporter)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Cursor (android.database.Cursor)3 Uri (android.net.Uri)3 Bundle (android.os.Bundle)3