GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — taglib/src/androidTest/kotlin/Tests.kt at main · rRemix/taglib Expand file tree/ Copy pathTests.ktMore file actions 170 lines (138 loc) · 6.34 KB / Copy pathTests.ktFile metadata and controls170 lines (138 loc) · 6.34 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 import android.content.Context import android.graphics.Bitmap import android.graphics.BitmapFactory import android.os.ParcelFileDescriptor import androidx.test.platform.app.InstrumentationRegistry import com.kyant.taglib.Picture import com.kyant.taglib.TagLib import org.junit.Assert import org.junit.Test import java.io.ByteArrayOutputStream import java.io.File import java.nio.charset.Charset class Tests { private val context: Context = InstrumentationRegistry.getInstrumentation().targetContext @Test fun test_all() { read_and_write_m4a() read_and_write_pictures_flac() read_flac_multiple_pictures() ensure_utf8() bad_encoding() } private fun read_and_write_m4a() { getFdFromAssets(context, "Sample_BeeMoved_48kHz16bit.m4a").use { fd -> // Read audio properties val audioProperties = TagLib.getAudioProperties(fd.dup().detachFd())!! Assert.assertEquals(39936, audioProperties.length) // Read metadata val metadata = TagLib.getMetadata(fd = fd.dup().detachFd(), readPictures = true)!! Assert.assertEquals("Bee Moved", metadata.propertyMap["TITLE"]!!.single()) Assert.assertEquals(58336, metadata.pictures.single().data.size) // Read single metadata val artists = TagLib.getMetadataPropertyValues(fd.dup().detachFd(), "ARTIST")!! Assert.assertEquals("Blue Monday FM", artists.single()) val lyrics = TagLib.getMetadataPropertyValues(fd.dup().detachFd(), "LYRICS")!! Assert.assertEquals(0, lyrics.size) // Save metadata val newTitle = "Bee Moved (Remix)" val newPropertyMap = metadata.propertyMap.apply { this["TITLE"] = arrayOf(newTitle) } val saved = TagLib.savePropertyMap(fd.dup().detachFd(), newPropertyMap) Assert.assertTrue(saved) val newMetadata = TagLib.getMetadata(fd.dup().detachFd())!! Assert.assertEquals(newTitle, newMetadata.propertyMap["TITLE"]!!.single()) } } private fun read_and_write_pictures_flac() { getFdFromAssets(context, "是什么让我遇见这样的你 - 白安.flac").use { fd -> val metadata = TagLib.getMetadata(fd.dup().detachFd())!! Assert.assertEquals("是什么让我遇见这样的你", metadata.propertyMap["TITLE"]!!.single()) val pictures = TagLib.getPictures(fd.dup().detachFd()) val originalPicture = pictures.single() Assert.assertEquals(42716, originalPicture.data.size) // Save the original picture val saved = TagLib.savePictures(fd.dup().detachFd(), arrayOf(originalPicture)) Assert.assertTrue(saved) Assert.assertEquals(originalPicture, TagLib.getPictures(fd.dup().detachFd()).single()) // Save new multiple pictures val newPicture1 = ByteArrayOutputStream().use { BitmapFactory .decodeStream(context.assets.open("699_200x200.jpg")) .compress(Bitmap.CompressFormat.JPEG, 100, it) Picture( data = it.toByteArray(), description = "Front Cover", pictureType = "Front Cover", mimeType = "image/jpeg", ) } val newPicture2 = ByteArrayOutputStream().use { BitmapFactory .decodeStream(context.assets.open("947_200x200.jpg")) .compress(Bitmap.CompressFormat.JPEG, 100, it) Picture( data = it.toByteArray(), description = "Back Cover", pictureType = "Back Cover", mimeType = "image/jpeg", ) } TagLib.savePictures(fd.dup().detachFd(), arrayOf(newPicture1, newPicture2)) val newPictures = TagLib.getPictures(fd.dup().detachFd()) Assert.assertEquals(2, newPictures.size) Assert.assertEquals(newPicture1, newPictures[0]) Assert.assertEquals(newPicture2, newPictures[1]) } } private fun read_flac_multiple_pictures() { getFdFromAssets(context, "multiple_album_art.flac").use { fd -> val pictures = TagLib.getPictures(fd.dup().detachFd()) Assert.assertEquals(3, pictures.size) Assert.assertEquals(29766, pictures[2].data.size) } } private fun ensure_utf8() { getFdFromAssets(context, "bladeenc.mp3").use { fd -> // Read metadata val metadata = TagLib.getMetadata(fd.dup().detachFd())!! Assert.assertEquals("Test", metadata.propertyMap["TITLE"]!!.single()) val pictures = TagLib.getPictures(fd.dup().detachFd()) Assert.assertEquals(0, pictures.size) // Save metadata val newTitleKey = "TITLE ú" val newTitles = arrayOf("Test ú", "Test") val newPropertyMap = metadata.propertyMap.apply { this[newTitleKey] = newTitles } val saved = TagLib.savePropertyMap(fd.dup().detachFd(), newPropertyMap) Assert.assertTrue(saved) val newMetadata = TagLib.getMetadata(fd.dup().detachFd())!! Assert.assertArrayEquals(newTitles, newMetadata.propertyMap[newTitleKey]) } } private fun bad_encoding() { getFdFromAssets(context, "Honor.mp3").use { fd -> val metadata = TagLib.getMetadata(fd.dup().detachFd())!! Assert.assertEquals( "荣耀".toByteArray(Charset.forName("GB2312")).toString(Charsets.ISO_8859_1), metadata.propertyMap["TITLE"]!!.single() ) } } private fun getFdFromAssets(context: Context, fileName: String): ParcelFileDescriptor { val file = getFileFromAssets(context, fileName) return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE) } private fun getFileFromAssets(context: Context, fileName: String): File { return File(context.cacheDir, fileName).apply { outputStream().use { cache -> context.assets.open(fileName).use { inputStream -> inputStream.copyTo(cache) } } } } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |