Search in sources :

Example 21 with IMPORT

use of com.ichi2.async.CollectionTask.TASK_TYPE.IMPORT in project Anki-Android by ankidroid.

the class IntentHandler method handleFileImport.

private void handleFileImport(Intent intent, Intent reloadIntent, String action) {
    Timber.i("Handling file import");
    ImportResult importResult = ImportUtils.handleFileImport(this, intent);
    // Start DeckPicker if we correctly processed ACTION_VIEW
    if (importResult.isSuccess()) {
        Timber.d("onCreate() import successful");
        reloadIntent.setAction(action);
        reloadIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(reloadIntent);
        AnkiActivity.finishActivityWithFade(this);
    } else {
        Timber.i("File import failed");
        // Don't import the file if it didn't load properly or doesn't have apkg extension
        ImportUtils.showImportUnsuccessfulDialog(this, importResult.getHumanReadableMessage(), true);
    }
}
Also used : ImportResult(com.ichi2.utils.ImportUtils.ImportResult)

Example 22 with IMPORT

use of com.ichi2.async.CollectionTask.TASK_TYPE.IMPORT in project Anki-Android by ankidroid.

the class ImportTest method testAnki2DiffmodelTemplates.

@Test
public void testAnki2DiffmodelTemplates() throws IOException, JSONException, ImportExportException {
    // different from the above as this one tests only the template text being
    // changed, not the number of cards/fields
    // import the first version of the model
    String tmp = Shared.getTestFilePath(getTestContext(), "diffmodeltemplates-1.apkg");
    AnkiPackageImporter imp = new AnkiPackageImporter(mTestCol, tmp);
    imp.setDupeOnSchemaChange(true);
    imp.run();
    // then the version with updated template
    tmp = Shared.getTestFilePath(getTestContext(), "diffmodeltemplates-2.apkg");
    imp = new AnkiPackageImporter(mTestCol, tmp);
    imp.setDupeOnSchemaChange(true);
    imp.run();
    // collection should contain the note we imported
    assertEquals(1, mTestCol.noteCount());
    // the front template should contain the text added in the 2nd package
    Long tcid = mTestCol.findCards("").get(0);
    Note tnote = mTestCol.getCard(tcid).note();
    assertTrue(mTestCol.findTemplates(tnote).get(0).getString("qfmt").contains("Changed Front Template"));
}
Also used : AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) Note(com.ichi2.libanki.Note) Test(org.junit.Test) InstrumentedTest(com.ichi2.anki.tests.InstrumentedTest)

Example 23 with IMPORT

use of com.ichi2.async.CollectionTask.TASK_TYPE.IMPORT in project Anki-Android by ankidroid.

the class ImportTest method testCsv.

@Test
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
public void testCsv() throws IOException {
    String file = Shared.getTestFilePath(getTestContext(), "text-2fields.txt");
    TextImporter i = new TextImporter(mTestCol, file);
    i.initMapping();
    i.run();
    if (TestEnvironment.isDisplayingDefaultEnglishStrings()) {
        assertThat(i.getLog(), contains("‘多すぎる too many fields’ had 3 fields, expected 2", "‘not, enough, fields’ had 1 fields, expected 2", "Appeared twice in file: 飲む", "Empty first field:  to play", "5 notes added, 0 notes updated, 0 notes unchanged."));
    } else {
        assertThat(i.getLog(), hasSize(5));
    }
    assertEquals(5, i.getTotal());
    // if we run the import again, it should update instead
    i.run();
    if (TestEnvironment.isDisplayingDefaultEnglishStrings()) {
        assertThat(i.getLog(), contains("‘多すぎる too many fields’ had 3 fields, expected 2", "‘not, enough, fields’ had 1 fields, expected 2", "Appeared twice in file: 飲む", "Empty first field:  to play", "0 notes added, 0 notes updated, 5 notes unchanged.", "First field matched: 食べる", "First field matched: 飲む", "First field matched: テスト", "First field matched: to eat", "First field matched: 遊ぶ"));
    } else {
        assertThat(i.getLog(), hasSize(10));
    }
    assertEquals(5, i.getTotal());
    // but importing should not clobber tags if they're unmapped
    Note n = mTestCol.getNote(mTestCol.getDb().queryLongScalar("select id from notes"));
    n.addTag("test");
    n.flush();
    i.run();
    n.load();
    assertThat(n.getTags(), contains("test"));
    assertThat(n.getTags(), hasSize(1));
    // if add-only mode, count will be 0
    i.setImportMode(NoteImporter.ImportMode.IGNORE_MODE);
    i.run();
    assertEquals(0, i.getTotal());
    // and if dupes mode, will reimport everything
    assertEquals(5, mTestCol.cardCount());
    i.setImportMode(NoteImporter.ImportMode.ADD_MODE);
    i.run();
    // includes repeated field
    assertEquals(6, i.getTotal());
    assertEquals(11, mTestCol.cardCount());
}
Also used : Note(com.ichi2.libanki.Note) TextImporter(com.ichi2.libanki.importer.TextImporter) Test(org.junit.Test) InstrumentedTest(com.ichi2.anki.tests.InstrumentedTest) SdkSuppress(androidx.test.filters.SdkSuppress)

Example 24 with IMPORT

use of com.ichi2.async.CollectionTask.TASK_TYPE.IMPORT in project Anki-Android by ankidroid.

the class NoteServiceTest method tempAudioIsDeletedAfterImport.

/**
 * Sometimes media files cannot be imported directly to the media directory,
 * so they are copied to cache then imported and deleted.
 * This tests if cached media are properly deleted after import.
 */
@Test
public void tempAudioIsDeletedAfterImport() {
    File file = createTransientFile("foo");
    MediaClipField field = new MediaClipField();
    field.setAudioPath(file.getAbsolutePath());
    field.setHasTemporaryMedia(true);
    NoteService.importMediaToDirectory(mTestCol, field);
    assertFalse("Audio temporary file should have been deleted after importing", file.exists());
}
Also used : MediaClipField(com.ichi2.anki.multimediacard.fields.MediaClipField) File(java.io.File) FileSystemUtilsKt.createTransientFile(com.ichi2.testutils.FileSystemUtilsKt.createTransientFile) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Aggregations

File (java.io.File)11 Test (org.junit.Test)9 Collection (com.ichi2.libanki.Collection)8 AnkiPackageImporter (com.ichi2.libanki.importer.AnkiPackageImporter)7 IOException (java.io.IOException)7 Note (com.ichi2.libanki.Note)6 TextImporter (com.ichi2.libanki.importer.TextImporter)6 FileNotFoundException (java.io.FileNotFoundException)6 HashMap (java.util.HashMap)6 Resources (android.content.res.Resources)5 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)5 Anki2Importer (com.ichi2.libanki.importer.Anki2Importer)5 ArrayList (java.util.ArrayList)5 Cursor (android.database.Cursor)4 InstrumentedTest (com.ichi2.anki.tests.InstrumentedTest)4 Importer (com.ichi2.libanki.importer.Importer)4 NoteImporter (com.ichi2.libanki.importer.NoteImporter)4 FileOutputStream (java.io.FileOutputStream)4 ImportExportException (com.ichi2.anki.exception.ImportExportException)3 List (java.util.List)3