1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.mediaframeworktest.unit; 18 19 import static android.media.MediaFile.getFormatCode; 20 import static android.media.MediaFile.getMimeType; 21 import static android.media.MediaFile.isAudioMimeType; 22 import static android.media.MediaFile.isDocumentMimeType; 23 import static android.media.MediaFile.isImageMimeType; 24 import static android.media.MediaFile.isPlayListMimeType; 25 import static android.media.MediaFile.isVideoMimeType; 26 27 import static org.junit.Assert.assertEquals; 28 import static org.junit.Assert.assertFalse; 29 import static org.junit.Assert.assertTrue; 30 31 import android.mtp.MtpConstants; 32 33 import androidx.test.runner.AndroidJUnit4; 34 35 import libcore.content.type.MimeMap; 36 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 40 @RunWith(AndroidJUnit4.class) 41 public class MediaFileTest { 42 @Test testCommon()43 public void testCommon() throws Exception { 44 assertConsistent("FOO.TXT", "text/plain", MtpConstants.FORMAT_TEXT); 45 assertConsistent("FOO.XML", "text/xml", MtpConstants.FORMAT_XML_DOCUMENT); 46 assertConsistent("FOO.HTML", "text/html", MtpConstants.FORMAT_HTML); 47 } 48 49 @Test testAudio()50 public void testAudio() throws Exception { 51 assertTrue(isAudioMimeType("audio/flac")); 52 assertTrue(isAudioMimeType("application/x-flac")); 53 assertFalse(isAudioMimeType("video/mpeg")); 54 55 assertConsistent("FOO.MP3", "audio/mpeg", MtpConstants.FORMAT_MP3); 56 assertConsistent("FOO.AAC", "audio/aac", MtpConstants.FORMAT_AAC); 57 assertConsistent("FOO.OGG", "audio/ogg", MtpConstants.FORMAT_OGG); 58 assertConsistent("FOO.FLAC", "audio/flac", MtpConstants.FORMAT_FLAC); 59 } 60 61 @Test testVideo()62 public void testVideo() throws Exception { 63 assertTrue(isVideoMimeType("video/x-msvideo")); 64 assertFalse(isVideoMimeType("audio/mpeg")); 65 66 assertConsistent("FOO.AVI", "video/avi", MtpConstants.FORMAT_AVI); 67 assertConsistent("FOO.MP4", "video/mp4", MtpConstants.FORMAT_MP4_CONTAINER); 68 assertConsistent("FOO.3GP", "video/3gpp", MtpConstants.FORMAT_3GP_CONTAINER); 69 } 70 71 @Test testImage()72 public void testImage() throws Exception { 73 assertTrue(isImageMimeType("image/jpeg")); 74 assertTrue(isImageMimeType("image/heif")); 75 assertTrue(isImageMimeType("image/webp")); 76 assertFalse(isImageMimeType("video/webm")); 77 78 assertConsistent("FOO.JPG", "image/jpeg", MtpConstants.FORMAT_EXIF_JPEG); 79 assertConsistent("FOO.PNG", "image/png", MtpConstants.FORMAT_PNG); 80 assertConsistent("FOO.HEIF", "image/heif", MtpConstants.FORMAT_HEIF); 81 assertConsistent("FOO.DNG", "image/x-adobe-dng", MtpConstants.FORMAT_DNG); 82 assertConsistent("FOO.TIFF", "image/tiff", MtpConstants.FORMAT_TIFF); 83 } 84 85 @Test testPlayList()86 public void testPlayList() throws Exception { 87 MimeMap mimeMap = MimeMap.getDefault(); 88 assertTrue(isPlayListMimeType(mimeMap.guessMimeTypeFromExtension("pls"))); 89 assertTrue(isPlayListMimeType(mimeMap.guessMimeTypeFromExtension("wpl"))); 90 assertTrue(isPlayListMimeType(mimeMap.guessMimeTypeFromExtension("m3u"))); 91 assertTrue(isPlayListMimeType(mimeMap.guessMimeTypeFromExtension("m3u8"))); 92 } 93 94 @Test testDocument()95 public void testDocument() throws Exception { 96 assertTrue(isDocumentMimeType("text/csv")); 97 assertTrue(isDocumentMimeType("text/plain")); 98 assertTrue(isDocumentMimeType("application/pdf")); 99 assertTrue(isDocumentMimeType("application/msword")); 100 assertTrue(isDocumentMimeType("application/vnd.ms-excel.addin.macroEnabled.12")); 101 assertTrue(isDocumentMimeType("application/vnd.ms-powerpoint.addin.macroEnabled.12")); 102 assertTrue(isDocumentMimeType("application/vnd.ms-word.document.macroEnabled.12")); 103 assertFalse(isDocumentMimeType("audio/mpeg")); 104 } 105 106 @Test testImageRaw()107 public void testImageRaw() throws Exception { 108 // We trust MIME types before filenames 109 assertHexEquals(MtpConstants.FORMAT_TIFF, getFormatCode("FOO.CR2", "image/x-canon-cr2")); 110 // We trust filenames before format codes 111 assertEquals("image/x-canon-cr2", getMimeType("FOO.CR2", MtpConstants.FORMAT_TIFF)); 112 } 113 114 @Test testConfusing()115 public void testConfusing() throws Exception { 116 // We trust MIME types before filenames 117 assertHexEquals(MtpConstants.FORMAT_MP3, getFormatCode("foo.avi", "audio/mpeg")); 118 // We trust filenames before format codes 119 assertEquals("video/avi", getMimeType("foo.avi", MtpConstants.FORMAT_MP3)); 120 } 121 122 @Test testUnknown()123 public void testUnknown() throws Exception { 124 assertHexEquals(MtpConstants.FORMAT_UNDEFINED, 125 getFormatCode("foo.example", "application/x-example")); 126 assertHexEquals(MtpConstants.FORMAT_UNDEFINED_AUDIO, 127 getFormatCode("foo.example", "audio/x-example")); 128 assertHexEquals(MtpConstants.FORMAT_UNDEFINED_VIDEO, 129 getFormatCode("foo.example", "video/x-example")); 130 assertHexEquals(MtpConstants.FORMAT_DEFINED, 131 getFormatCode("foo.example", "image/x-example")); 132 } 133 assertConsistent(String path, String mimeType, int formatCode)134 private static void assertConsistent(String path, String mimeType, int formatCode) { 135 assertHexEquals(formatCode, getFormatCode(path, null)); 136 assertHexEquals(formatCode, getFormatCode(null, mimeType)); 137 assertHexEquals(formatCode, getFormatCode(path, mimeType)); 138 139 assertEquals(mimeType, getMimeType(path, MtpConstants.FORMAT_UNDEFINED)); 140 assertEquals(mimeType, getMimeType(null, formatCode)); 141 assertEquals(mimeType, getMimeType(path, formatCode)); 142 } 143 assertHexEquals(int expected, int actual)144 private static void assertHexEquals(int expected, int actual) { 145 assertEquals(Integer.toHexString(expected), Integer.toHexString(actual)); 146 } 147 } 148