Search in sources :

Example 26 with Models

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

the class Syncer method changes.

/**
 * Bundle up small objects.
 */
public JSONObject changes() {
    JSONObject o = new JSONObject();
    o.put("models", getModels());
    o.put("decks", getDecks());
    o.put("tags", getTags());
    if (mLNewer) {
        o.put("conf", getConf());
        o.put("crt", mCol.getCrt());
    }
    return o;
}
Also used : JSONObject(com.ichi2.utils.JSONObject)

Example 27 with Models

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

the class ReviewerTest method cloneTemplate.

private void cloneTemplate(ModelManager models, Model m) throws ConfirmModSchemaException {
    JSONArray tmpls = m.getJSONArray("tmpls");
    JSONObject defaultTemplate = tmpls.getJSONObject(0);
    JSONObject newTemplate = defaultTemplate.deepClone();
    newTemplate.put("ord", tmpls.length());
    String card_name = getTargetContext().getString(R.string.card_n_name, tmpls.length() + 1);
    newTemplate.put("name", card_name);
    models.addTemplate(m, newTemplate);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray) Matchers.emptyString(org.hamcrest.Matchers.emptyString)

Example 28 with Models

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

the class ReviewerTest method jsAnkiGetDeckName.

@Test
public void jsAnkiGetDeckName() {
    Collection col = getCol();
    ModelManager models = col.getModels();
    DeckManager decks = col.getDecks();
    Long didAb = addDeck("A::B");
    Model basic = models.byName(AnkiDroidApp.getAppResources().getString(R.string.basic_model_name));
    basic.put("did", didAb);
    addNoteUsingBasicModel("foo", "bar");
    Long didA = addDeck("A");
    decks.select(didA);
    Reviewer reviewer = startReviewer();
    AnkiDroidJsAPI javaScriptFunction = reviewer.javaScriptFunction();
    waitForAsyncTasksToComplete();
    assertThat(javaScriptFunction.ankiGetDeckName(), is("B"));
}
Also used : Model(com.ichi2.libanki.Model) Collection(com.ichi2.libanki.Collection) DeckManager(com.ichi2.libanki.DeckManager) ModelManager(com.ichi2.libanki.ModelManager) Test(org.junit.Test)

Example 29 with Models

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

the class ContentProviderTest method testProviderProvidesDefaultForEmptyModelDeck.

/**
 * Test that a null did will not crash the provider (#6378)
 */
@Test
public void testProviderProvidesDefaultForEmptyModelDeck() {
    assumeTrue("This causes mild data corruption - should not be run on a collection you care about", isEmulator());
    Collection col = getCol();
    col.getModels().all().get(0).put("did", JSONObject.NULL);
    col.save();
    final ContentResolver cr = getContentResolver();
    // Query all available models
    final Cursor allModels = cr.query(FlashCardsContract.Model.CONTENT_URI, null, null, null, null);
    assertNotNull(allModels);
}
Also used : Collection(com.ichi2.libanki.Collection) Cursor(android.database.Cursor) ContentResolver(android.content.ContentResolver) Test(org.junit.Test)

Example 30 with Models

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

the class ContentProviderTest method setUp.

/**
 * Initially create one note for each model.
 */
@Before
public void setUp() throws Exception {
    Timber.i("setUp()");
    mCreatedNotes = new ArrayList<>();
    final Collection col = getCol();
    // We have parameterized the "schedVersion" variable, if we are on an emulator
    // (so it is safe) we will try to run with multiple scheduler versions
    mTearDown = false;
    if (InstrumentedTest.isEmulator()) {
        col.changeSchedulerVer(schedVersion);
    } else {
        if (schedVersion == 1) {
            assumeThat(col.getSched().getName(), is("std"));
        } else {
            assumeThat(col.getSched().getName(), is("std2"));
        }
    }
    mTearDown = true;
    // Do not teardown if setup was aborted
    // 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);
    mModelId = model.getLong("id");
    List<String> fields = model.getFieldsNames();
    // Use the names of the fields as test values for the notes which will be added
    mDummyFields = fields.toArray(new String[0]);
    // create test decks and add one note for every deck
    mNumDecksBeforeTest = col.getDecks().count();
    for (String fullName : TEST_DECKS) {
        String[] path = Decks.path(fullName);
        String partialName = "";
        /* Looping over all parents of full name. Adding them to
             * mTestDeckIds ensures the deck parents decks get deleted
             * too at tear-down.
             */
        for (String s : path) {
            partialName += s;
            /* If parent already exists, don't add the deck, so
                 * that we are sure it won't get deleted at
                 * set-down, */
            if (col.getDecks().byName(partialName) != null) {
                continue;
            }
            long did = col.getDecks().id(partialName);
            mTestDeckIds.add(did);
            mCreatedNotes.add(setupNewNote(col, mModelId, did, mDummyFields, TEST_TAG));
            partialName += "::";
        }
    }
    // Add a note to the default deck as well so that testQueryNextCard() works
    mCreatedNotes.add(setupNewNote(col, mModelId, 1, mDummyFields, TEST_TAG));
}
Also used : Model(com.ichi2.libanki.Model) Collection(com.ichi2.libanki.Collection) Before(org.junit.Before)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)48 Model (com.ichi2.libanki.Model)28 JSONArray (com.ichi2.utils.JSONArray)26 Test (org.junit.Test)26 Collection (com.ichi2.libanki.Collection)25 RobolectricTest (com.ichi2.anki.RobolectricTest)16 Note (com.ichi2.libanki.Note)16 JSONException (com.ichi2.utils.JSONException)13 ArrayList (java.util.ArrayList)13 Card (com.ichi2.libanki.Card)11 Models (com.ichi2.libanki.Models)11 SuppressLint (android.annotation.SuppressLint)10 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)7 ModelManager (com.ichi2.libanki.ModelManager)7 File (java.io.File)6 HashMap (java.util.HashMap)6 List (java.util.List)6 Map (java.util.Map)6 ContentValues (android.content.ContentValues)5 NonNull (androidx.annotation.NonNull)5