Search in sources :

Example 86 with JSONObject

use of com.ichi2.utils.JSONObject in project Anki-Android by ankidroid.

the class SchedTest method test_ordcycleV1.

@Test
public void test_ordcycleV1() throws Exception {
    Collection col = getColV1();
    // add two more templates and set second active
    Model m = col.getModels().current();
    ModelManager mm = col.getModels();
    JSONObject t = Models.newTemplate("Reverse");
    t.put("qfmt", "{{Back}}");
    t.put("afmt", "{{Front}}");
    mm.addTemplateModChanged(m, t);
    t = Models.newTemplate("f2");
    t.put("qfmt", "{{Front}}");
    t.put("afmt", "{{Back}}");
    mm.addTemplateModChanged(m, t);
    mm.save(m);
    // create a new note; it should have 3 cards
    Note note = col.newNote();
    note.setItem("Front", "1");
    note.setItem("Back", "1");
    col.addNote(note);
    assertEquals(3, col.cardCount());
    col.reset();
    // ordinals should arrive in order
    AbstractSched sched = col.getSched();
    Card c = sched.getCard();
    // not upstream. But we are not expecting multiple getCard without review
    sched.answerCard(c, sched.answerButtons(c) - 1);
    waitForAsyncTasksToComplete();
    assertEquals(0, c.getOrd());
    c = sched.getCard();
    // not upstream. But we are not expecting multiple getCard without review
    sched.answerCard(c, sched.answerButtons(c) - 1);
    waitForAsyncTasksToComplete();
    assertEquals(1, c.getOrd());
    c = sched.getCard();
    // not upstream. But we are not expecting multiple getCard without review
    sched.answerCard(c, sched.answerButtons(c) - 1);
    assertEquals(2, c.getOrd());
}
Also used : JSONObject(com.ichi2.utils.JSONObject) Note(com.ichi2.libanki.Note) Model(com.ichi2.libanki.Model) Collection(com.ichi2.libanki.Collection) ModelManager(com.ichi2.libanki.ModelManager) Card(com.ichi2.libanki.Card) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 87 with JSONObject

use of com.ichi2.utils.JSONObject in project Anki-Android by ankidroid.

the class SchedTest method test_new_v1.

public void test_new_v1() throws Exception {
    Collection col = getColV1();
    col.reset();
    assertEquals(0, col.getSched().newCount());
    // add a note
    Note note = col.newNote();
    note.setItem("Front", "one");
    note.setItem("Back", "two");
    col.addNote(note);
    col.reset();
    assertEquals(1, col.getSched().counts().getNew());
    // fetch it
    Card c = getCard();
    assertNotNull(c);
    assertEquals(QUEUE_TYPE_NEW, c.getQueue());
    assertEquals(CARD_TYPE_NEW, c.getType());
    // if we answer it, it should become a learn card
    long t = col.getTime().intTime();
    col.getSched().answerCard(c, BUTTON_ONE);
    assertEquals(QUEUE_TYPE_LRN, c.getQueue());
    assertEquals(CARD_TYPE_LRN, c.getType());
    assertThat(c.getDue(), is(greaterThanOrEqualTo(t)));
// disabled for now, as the learn fudging makes this randomly fail
// // the default order should ensure siblings are not seen together, and
// // should show all cards
// Model m = col.getModels().current(); Models mm = col.getModels()
// JSONObject t = mm.newTemplate("Reverse")
// t['qfmt'] = "{{Back}}"
// t['afmt'] = "{{Front}}"
// mm.addTemplateModChanged(m, t)
// mm.save(m)
// note = col.newNote()
// note['Front'] = u"2"; note['Back'] = u"2"
// col.addNote(note)
// note = col.newNote()
// note['Front'] = u"3"; note['Back'] = u"3"
// col.addNote(note)
// col.reset()
// qs = ("2", "3", "2", "3")
// for (int n = 0; n < 4; n++) {
// c = getCard()
// assertTrue(qs[n] in c.q())
// col.getSched().answerCard(c, 2)
// }
}
Also used : Note(com.ichi2.libanki.Note) Collection(com.ichi2.libanki.Collection) Card(com.ichi2.libanki.Card)

Example 88 with JSONObject

use of com.ichi2.utils.JSONObject in project Anki-Android by ankidroid.

the class ImportTest method testCsv2.

@Test
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
public void testCsv2() throws IOException, ConfirmModSchemaException {
    ModelManager mm = mTestCol.getModels();
    Model m = mm.current();
    JSONObject f = mm.newField("Three");
    mm.addField(m, f);
    mm.save(m);
    Note n = mTestCol.newNote();
    n.setField(0, "1");
    n.setField(1, "2");
    n.setField(2, "3");
    mTestCol.addNote(n);
    // an update with unmapped fields should not clobber those fields
    String file = Shared.getTestFilePath(getTestContext(), "text-update.txt");
    TextImporter i = new TextImporter(mTestCol, 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) ModelManager(com.ichi2.libanki.ModelManager) TextImporter(com.ichi2.libanki.importer.TextImporter) Test(org.junit.Test) InstrumentedTest(com.ichi2.anki.tests.InstrumentedTest) SdkSuppress(androidx.test.filters.SdkSuppress)

Example 89 with JSONObject

use of com.ichi2.utils.JSONObject in project Anki-Android by ankidroid.

the class CardTemplateEditor method onDeckSelected.

/**
 * When a deck is selected via Deck Override
 */
@Override
public void onDeckSelected(@Nullable SelectableDeck deck) {
    if (getTempModel().getModel().isCloze()) {
        Timber.w("Attempted to set deck for cloze model");
        UIUtils.showThemedToast(this, getString(R.string.multimedia_editor_something_wrong), true);
        return;
    }
    int ordinal = mViewPager.getCurrentItem();
    JSONObject template = getTempModel().getTemplate(ordinal);
    String templateName = template.getString("name");
    if (deck != null && Decks.isDynamic(getCol(), deck.getDeckId())) {
        Timber.w("Attempted to set default deck of %s to dynamic deck %s", templateName, deck.getName());
        UIUtils.showThemedToast(this, getString(R.string.multimedia_editor_something_wrong), true);
        return;
    }
    String message;
    if (deck == null) {
        Timber.i("Removing default template from template '%s'", templateName);
        template.put("did", JSONObject.NULL);
        message = getString(R.string.model_manager_deck_override_removed_message, templateName);
    } else {
        Timber.i("Setting template '%s' to '%s'", templateName, deck.getName());
        template.put("did", deck.getDeckId());
        message = getString(R.string.model_manager_deck_override_added_message, templateName, deck.getName());
    }
    UIUtils.showThemedToast(this, message, true);
    // Deck Override can change from "on" <-> "off"
    supportInvalidateOptionsMenu();
}
Also used : JSONObject(com.ichi2.utils.JSONObject)

Example 90 with JSONObject

use of com.ichi2.utils.JSONObject in project Anki-Android by ankidroid.

the class CardTemplatePreviewerTest method testPreviewUnsavedTemplate_Basic.

@Test
public void testPreviewUnsavedTemplate_Basic() {
    String modelName = "Basic";
    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("(" + fields.get(1) + ")"));
}
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)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)272 JSONArray (com.ichi2.utils.JSONArray)110 ArrayList (java.util.ArrayList)59 Test (org.junit.Test)55 Collection (com.ichi2.libanki.Collection)46 IOException (java.io.IOException)40 RobolectricTest (com.ichi2.anki.RobolectricTest)34 JSONException (com.ichi2.utils.JSONException)34 Note (com.ichi2.libanki.Note)33 Model (com.ichi2.libanki.Model)31 HashMap (java.util.HashMap)31 SuppressLint (android.annotation.SuppressLint)29 JSONObject (org.json.JSONObject)27 Response (okhttp3.Response)25 JSONException (org.json.JSONException)24 NonNull (androidx.annotation.NonNull)23 Card (com.ichi2.libanki.Card)21 File (java.io.File)21 Map (java.util.Map)20 Intent (android.content.Intent)19