Search in sources :

Example 66 with Model

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

the class ModelTest method bigQuery.

@Test
public void bigQuery() {
    assumeTrue("This test is flaky on API29, ignoring", Build.VERSION.SDK_INT != Build.VERSION_CODES.Q);
    ModelManager models = mTestCol.getModels();
    Model model = models.all().get(0);
    final String testString = "test";
    final int size = testString.length() * 1024 * 1024;
    StringBuilder buf = new StringBuilder((int) (size * 1.01));
    // * 1.01 for padding
    for (int i = 0; i < 1024 * 1024; ++i) {
        buf.append(testString);
    }
    model.put(testString, buf.toString());
    // Buf should be more than 4MB, so at least two chunks from database.
    models.flush();
    // Reload models
    mTestCol.load();
    Model newModel = models.all().get(0);
    assertEquals(newModel, model);
}
Also used : Model(com.ichi2.libanki.Model) ModelManager(com.ichi2.libanki.ModelManager) Test(org.junit.Test) InstrumentedTest(com.ichi2.anki.tests.InstrumentedTest)

Example 67 with Model

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

the class CardTemplateEditor method showDiscardChangesDialog.

@VisibleForTesting
public MaterialDialog showDiscardChangesDialog() {
    MaterialDialog discardDialog = DiscardChangesDialog.getDefault(this).onPositive((dialog, which) -> {
        Timber.i("TemplateEditor:: OK button pressed to confirm discard changes");
        // Clear the edited model from any cache files, and clear it from this objects memory to discard changes
        TemporaryModel.clearTempModelFiles();
        mTempModel = null;
        finishWithAnimation(END);
    }).build();
    discardDialog.show();
    return discardDialog;
}
Also used : RequiresApi(androidx.annotation.RequiresApi) ActionMode(android.view.ActionMode) Bundle(android.os.Bundle) Deck(com.ichi2.libanki.Deck) NonNull(androidx.annotation.NonNull) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog) FunctionalInterfaces(com.ichi2.utils.FunctionalInterfaces) Decks(com.ichi2.libanki.Decks) Matcher(java.util.regex.Matcher) Fragment(androidx.fragment.app.Fragment) JSONException(com.ichi2.utils.JSONException) View(android.view.View) TargetApi(android.annotation.TargetApi) BottomNavigationView(com.google.android.material.bottomnavigation.BottomNavigationView) TabLayout(com.google.android.material.tabs.TabLayout) JSONObject(com.ichi2.utils.JSONObject) ViewGroup(android.view.ViewGroup) Timber(timber.log.Timber) InsertFieldDialog(com.ichi2.anki.dialogs.InsertFieldDialog) StringRes(androidx.annotation.StringRes) List(java.util.List) Nullable(androidx.annotation.Nullable) ActivityResultContracts(androidx.activity.result.contract.ActivityResultContracts) TaskListenerWithContext(com.ichi2.async.TaskListenerWithContext) Pattern(java.util.regex.Pattern) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) FixedEditText(com.ichi2.ui.FixedEditText) DiscardChangesDialog(com.ichi2.anki.dialogs.DiscardChangesDialog) Models(com.ichi2.libanki.Models) TextWatcher(android.text.TextWatcher) Context(android.content.Context) KeyEvent(android.view.KeyEvent) Direction(com.ichi2.anim.ActivityTransitionAnimation.Direction) Pair(android.util.Pair) Intent(android.content.Intent) ViewPager2(androidx.viewpager2.widget.ViewPager2) HashMap(java.util.HashMap) Collection(com.ichi2.libanki.Collection) StyledProgressDialog(com.ichi2.themes.StyledProgressDialog) Editable(android.text.Editable) FragmentActivity(androidx.fragment.app.FragmentActivity) MenuItem(android.view.MenuItem) FragmentStateAdapter(androidx.viewpager2.adapter.FragmentStateAdapter) JSONArray(com.ichi2.utils.JSONArray) NOT_FOUND_NOTE_TYPE(com.ichi2.libanki.Models.NOT_FOUND_NOTE_TYPE) MenuInflater(android.view.MenuInflater) Menu(android.view.Menu) Model(com.ichi2.libanki.Model) Build(android.os.Build) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) CheckResult(androidx.annotation.CheckResult) ActivityResultLauncher(androidx.activity.result.ActivityResultLauncher) LayoutInflater(android.view.LayoutInflater) FixedTextView(com.ichi2.ui.FixedTextView) SelectableDeck(com.ichi2.anki.dialogs.DeckSelectionDialog.SelectableDeck) VisibleForTesting(androidx.annotation.VisibleForTesting) InsertFieldDialogFactory(com.ichi2.anki.dialogs.InsertFieldDialogFactory) TabLayoutMediator(com.google.android.material.tabs.TabLayoutMediator) Resources(android.content.res.Resources) DeckSelectionDialog(com.ichi2.anki.dialogs.DeckSelectionDialog) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) VisibleForTesting(androidx.annotation.VisibleForTesting)

Example 68 with Model

use of com.ichi2.libanki.Model 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 69 with Model

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

the class CardTemplatePreviewerTest method singleTemplateFromNoteEditorHasNoNavigation.

@Test
public void singleTemplateFromNoteEditorHasNoNavigation() {
    List<NoteService.NoteField> fields = new ArrayList<>();
    fields.add(new Field(0, "Hello"));
    fields.add(new Field(1, "World"));
    Model basicModel = getCurrentDatabaseModelCopy("Basic");
    String tempModelPath = TemporaryModel.saveTempModel(getTargetContext(), basicModel);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.putExtra(TemporaryModel.INTENT_MODEL_FILENAME, tempModelPath);
    Bundle noteEditorBundle = new Bundle();
    noteEditorBundle.putBundle("editFields", getFieldsAsBundleForPreview(fields));
    noteEditorBundle.putInt("ordinal", 0);
    noteEditorBundle.putLong("did", 1);
    intent.putExtra("noteEditorBundle", noteEditorBundle);
    TestCardTemplatePreviewer testCardTemplatePreviewer = super.startActivityNormallyOpenCollectionWithIntent(TestCardTemplatePreviewer.class, intent);
    assertThat("prev should not be visible", testCardTemplatePreviewer.previousButtonVisible(), is(false));
    assertThat("next should not be visible", testCardTemplatePreviewer.nextButtonVisible(), is(false));
}
Also used : Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) Model(com.ichi2.libanki.Model) Intent(android.content.Intent) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 70 with Model

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

the class CardTemplatePreviewerTest method nonClozeFromNoteEditorHasMultipleCards.

@Test
public void nonClozeFromNoteEditorHasMultipleCards() {
    List<NoteService.NoteField> fields = new ArrayList<>();
    fields.add(new Field(0, "Hello"));
    fields.add(new Field(1, "World"));
    Model basicModel = getCurrentDatabaseModelCopy("Basic (and reversed card)");
    String tempModelPath = TemporaryModel.saveTempModel(getTargetContext(), basicModel);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.putExtra(TemporaryModel.INTENT_MODEL_FILENAME, tempModelPath);
    Bundle noteEditorBundle = new Bundle();
    noteEditorBundle.putBundle("editFields", getFieldsAsBundleForPreview(fields));
    noteEditorBundle.putInt("ordinal", 0);
    noteEditorBundle.putLong("did", 1);
    intent.putExtra("noteEditorBundle", noteEditorBundle);
    TestCardTemplatePreviewer testCardTemplatePreviewer = super.startActivityNormallyOpenCollectionWithIntent(TestCardTemplatePreviewer.class, intent);
    assertTwoCards(testCardTemplatePreviewer);
}
Also used : Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) 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)124 Model (com.ichi2.libanki.Model)95 Test (org.junit.Test)82 JSONArray (com.ichi2.utils.JSONArray)79 Collection (com.ichi2.libanki.Collection)53 ArrayList (java.util.ArrayList)48 Note (com.ichi2.libanki.Note)40 RobolectricTest (com.ichi2.anki.RobolectricTest)38 JSONException (com.ichi2.utils.JSONException)32 Intent (android.content.Intent)30 Card (com.ichi2.libanki.Card)27 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)26 HashMap (java.util.HashMap)22 Bundle (android.os.Bundle)20 NonNull (androidx.annotation.NonNull)20 SuppressLint (android.annotation.SuppressLint)16 View (android.view.View)16 ConfirmationDialog (com.ichi2.anki.dialogs.ConfirmationDialog)15 IOException (java.io.IOException)15 Nullable (androidx.annotation.Nullable)14