Search in sources :

Example 46 with Media

use of com.ichi2.libanki.Media in project AnkiChinaAndroid by ankichinateam.

the class ImageFieldTest method collectionWithMediaFolder.

@CheckResult
protected Collection collectionWithMediaFolder(String dir) {
    Media media = mock(Media.class);
    when(media.dir()).thenReturn(dir);
    Collection collectionMock = mock(Collection.class);
    when(collectionMock.getMedia()).thenReturn(media);
    return collectionMock;
}
Also used : Media(com.ichi2.libanki.Media) Collection(com.ichi2.libanki.Collection) CheckResult(androidx.annotation.CheckResult)

Example 47 with Media

use of com.ichi2.libanki.Media in project AnkiChinaAndroid by ankichinateam.

the class ImageFieldTest method testNoImagePathConcat.

@Test
public void testNoImagePathConcat() {
    String goodImage = "<img src='1.png'/>";
    Collection col = collectionWithMediaFolder("media");
    String imageSrc = ImageField.getImageFullPath(col, goodImage);
    assertThat("Valid media should have path", imageSrc, equalTo("media/1.png"));
}
Also used : Collection(com.ichi2.libanki.Collection) Test(org.junit.Test)

Example 48 with Media

use of com.ichi2.libanki.Media in project AnkiChinaAndroid by ankichinateam.

the class ImportTest method testApkg.

@Test
public void testApkg() throws IOException, ImportExportException {
    List<String> expected;
    List<String> actual;
    String apkg = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "media.apkg");
    Importer imp = new AnkiPackageImporter(testCol, apkg);
    expected = Collections.emptyList();
    actual = Arrays.asList(new File(testCol.getMedia().dir()).list());
    actual.retainAll(expected);
    assertEquals(actual.size(), expected.size());
    imp.run();
    expected = Collections.singletonList("foo.wav");
    actual = Arrays.asList(new File(testCol.getMedia().dir()).list());
    actual.retainAll(expected);
    assertEquals(expected.size(), actual.size());
    // import again should be idempotent in terms of media
    testCol.remCards(testCol.getDb().queryLongList("select id from cards"));
    imp = new AnkiPackageImporter(testCol, apkg);
    imp.run();
    expected = Collections.singletonList("foo.wav");
    actual = Arrays.asList(new File(testCol.getMedia().dir()).list());
    actual.retainAll(expected);
    assertEquals(expected.size(), actual.size());
    // but if the local file has different data, it will rename
    testCol.remCards(testCol.getDb().queryLongList("select id from cards"));
    FileOutputStream os;
    os = new FileOutputStream(new File(testCol.getMedia().dir(), "foo.wav"), false);
    os.write("xyz".getBytes());
    os.close();
    imp = new AnkiPackageImporter(testCol, apkg);
    imp.run();
    assertEquals(2, new File(testCol.getMedia().dir()).list().length);
}
Also used : AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Anki2Importer(com.ichi2.libanki.importer.Anki2Importer) Importer(com.ichi2.libanki.importer.Importer) AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) NoteImporter(com.ichi2.libanki.importer.NoteImporter) TextImporter(com.ichi2.libanki.importer.TextImporter) Test(org.junit.Test)

Example 49 with Media

use of com.ichi2.libanki.Media in project AnkiChinaAndroid by ankichinateam.

the class MediaTest method testDeckIntegration.

@Test
public void testDeckIntegration() throws IOException {
    // create a media dir
    testCol.getMedia().dir();
    // Put a file into it
    File file = new File(Shared.getTestDir(InstrumentationRegistry.getInstrumentation().getTargetContext()), "fake.png");
    assertTrue(file.createNewFile());
    testCol.getMedia().addFile(file);
    // add a note which references it
    Note f = testCol.newNote();
    f.setField(0, "one");
    f.setField(1, "<img src='fake.png'>");
    testCol.addNote(f);
    // and one which references a non-existent file
    f = testCol.newNote();
    f.setField(0, "one");
    f.setField(1, "<img src='fake2.png'>");
    testCol.addNote(f);
    // and add another file which isn't used
    FileOutputStream os;
    os = new FileOutputStream(new File(testCol.getMedia().dir(), "foo.jpg"), false);
    os.write("test".getBytes());
    os.close();
    // check media
    List<List<String>> ret = testCol.getMedia().check();
    List<String> expected;
    List<String> actual;
    expected = Collections.singletonList("fake2.png");
    actual = ret.get(0);
    actual.retainAll(expected);
    assertEquals(expected.size(), actual.size());
    expected = Collections.singletonList("foo.jpg");
    actual = ret.get(1);
    actual.retainAll(expected);
    assertEquals(expected.size(), actual.size());
}
Also used : Note(com.ichi2.libanki.Note) FileOutputStream(java.io.FileOutputStream) List(java.util.List) File(java.io.File) Test(org.junit.Test)

Example 50 with Media

use of com.ichi2.libanki.Media in project AnkiChinaAndroid by ankichinateam.

the class MediaSyncer method _downloadFiles.

private void _downloadFiles(List<String> fnames) {
    mCol.log(fnames.size() + " files to fetch");
    while (fnames.size() > 0) {
        try {
            List<String> top = fnames.subList(0, Math.min(fnames.size(), Consts.SYNC_ZIP_COUNT));
            mCol.log("fetch " + top);
            ZipFile zipData = mServer.downloadFiles(top);
            int cnt = mCol.getMedia().addFilesFromZip(zipData);
            mDownloadCount += cnt;
            mCol.log("received " + cnt + " files");
            // if we've reached the end and clear the fnames list manually.
            if (cnt == fnames.size()) {
                fnames.clear();
            } else {
                fnames = fnames.subList(cnt, fnames.size());
            }
            mCon.publishProgress(String.format(AnkiDroidApp.getAppResources().getString(R.string.sync_media_downloaded_count), mDownloadCount));
        } catch (IOException | UnknownHttpResponseException e) {
            Timber.e(e, "Error downloading media files");
            throw new RuntimeException(e);
        }
    }
}
Also used : ZipFile(java.util.zip.ZipFile) IOException(java.io.IOException) UnknownHttpResponseException(com.ichi2.anki.exception.UnknownHttpResponseException)

Aggregations

File (java.io.File)43 IOException (java.io.IOException)26 Collection (com.ichi2.libanki.Collection)25 JSONObject (com.ichi2.utils.JSONObject)19 ArrayList (java.util.ArrayList)17 Test (org.junit.Test)17 ZipFile (java.util.zip.ZipFile)14 JSONException (com.ichi2.utils.JSONException)10 FileOutputStream (java.io.FileOutputStream)10 List (java.util.List)10 JSONArray (com.ichi2.utils.JSONArray)9 FileInputStream (java.io.FileInputStream)9 FileNotFoundException (java.io.FileNotFoundException)9 SharedPreferences (android.content.SharedPreferences)8 JSONObject (org.json.JSONObject)8 Resources (android.content.res.Resources)7 Uri (android.net.Uri)7 RobolectricTest (com.ichi2.anki.RobolectricTest)7 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)7 AnkiPackageImporter (com.ichi2.libanki.importer.AnkiPackageImporter)7