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;
}
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"));
}
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);
}
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());
}
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);
}
}
}
Aggregations