blob: 701454c32e4bee31ec7a8bf8e5a6d80b07731688 [file] [log] [blame]
Jeff Sharkey91e3cd42018-08-27 18:03:33 -06001/*
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
17package com.android.mediaframeworktest.unit;
18
19import static android.media.MediaFile.getFormatCode;
20import static android.media.MediaFile.getMimeType;
21import static android.media.MediaFile.isAudioMimeType;
22import static android.media.MediaFile.isImageMimeType;
23import static android.media.MediaFile.isPlayListMimeType;
24import static android.media.MediaFile.isVideoMimeType;
25
26import static org.junit.Assert.assertEquals;
27import static org.junit.Assert.assertFalse;
28import static org.junit.Assert.assertTrue;
29
30import android.mtp.MtpConstants;
Brett Chabot502ec7a2019-03-01 14:43:20 -080031
32import androidx.test.runner.AndroidJUnit4;
Jeff Sharkey91e3cd42018-08-27 18:03:33 -060033
34import libcore.net.MimeUtils;
35
36import org.junit.Test;
37import org.junit.runner.RunWith;
38
39@RunWith(AndroidJUnit4.class)
40public class MediaFileTest {
41 @Test
42 public void testCommon() throws Exception {
43 assertConsistent("FOO.TXT", "text/plain", MtpConstants.FORMAT_TEXT);
44 assertConsistent("FOO.XML", "text/xml", MtpConstants.FORMAT_XML_DOCUMENT);
45 assertConsistent("FOO.HTML", "text/html", MtpConstants.FORMAT_HTML);
46 }
47
48 @Test
49 public void testAudio() throws Exception {
50 assertTrue(isAudioMimeType("audio/flac"));
51 assertTrue(isAudioMimeType("application/x-flac"));
52 assertFalse(isAudioMimeType("video/mpeg"));
53
54 assertConsistent("FOO.MP3", "audio/mpeg", MtpConstants.FORMAT_MP3);
55 assertConsistent("FOO.AAC", "audio/aac", MtpConstants.FORMAT_AAC);
56 assertConsistent("FOO.OGG", "audio/ogg", MtpConstants.FORMAT_OGG);
57 assertConsistent("FOO.FLAC", "audio/flac", MtpConstants.FORMAT_FLAC);
58 }
59
60 @Test
61 public void testVideo() throws Exception {
62 assertTrue(isVideoMimeType("video/x-msvideo"));
63 assertFalse(isVideoMimeType("audio/mpeg"));
64
65 assertConsistent("FOO.AVI", "video/avi", MtpConstants.FORMAT_AVI);
66 assertConsistent("FOO.MP4", "video/mp4", MtpConstants.FORMAT_MP4_CONTAINER);
67 assertConsistent("FOO.3GP", "video/3gpp", MtpConstants.FORMAT_3GP_CONTAINER);
68 }
69
70 @Test
71 public void testImage() throws Exception {
72 assertTrue(isImageMimeType("image/jpeg"));
73 assertTrue(isImageMimeType("image/heif"));
74 assertTrue(isImageMimeType("image/webp"));
75 assertFalse(isImageMimeType("video/webm"));
76
77 assertConsistent("FOO.JPG", "image/jpeg", MtpConstants.FORMAT_EXIF_JPEG);
78 assertConsistent("FOO.PNG", "image/png", MtpConstants.FORMAT_PNG);
79 assertConsistent("FOO.HEIF", "image/heif", MtpConstants.FORMAT_HEIF);
80 assertConsistent("FOO.DNG", "image/x-adobe-dng", MtpConstants.FORMAT_DNG);
81 assertConsistent("FOO.TIFF", "image/tiff", MtpConstants.FORMAT_TIFF);
82 }
83
84 @Test
85 public void testPlayList() throws Exception {
86 assertTrue(isPlayListMimeType(MimeUtils.guessMimeTypeFromExtension("pls")));
87 assertTrue(isPlayListMimeType(MimeUtils.guessMimeTypeFromExtension("wpl")));
88 assertTrue(isPlayListMimeType(MimeUtils.guessMimeTypeFromExtension("m3u")));
89 assertTrue(isPlayListMimeType(MimeUtils.guessMimeTypeFromExtension("m3u8")));
90 assertTrue(isPlayListMimeType(MimeUtils.guessMimeTypeFromExtension("asf")));
91 }
92
93 @Test
94 public void testImageRaw() throws Exception {
95 // We trust MIME types before filenames
96 assertHexEquals(MtpConstants.FORMAT_TIFF, getFormatCode("FOO.CR2", "image/x-canon-cr2"));
97 // We trust filenames before format codes
98 assertEquals("image/x-canon-cr2", getMimeType("FOO.CR2", MtpConstants.FORMAT_TIFF));
99 }
100
101 @Test
102 public void testConfusing() throws Exception {
103 // We trust MIME types before filenames
104 assertHexEquals(MtpConstants.FORMAT_MP3, getFormatCode("foo.avi", "audio/mpeg"));
105 // We trust filenames before format codes
106 assertEquals("video/avi", getMimeType("foo.avi", MtpConstants.FORMAT_MP3));
107 }
108
109 @Test
110 public void testUnknown() throws Exception {
111 assertHexEquals(MtpConstants.FORMAT_UNDEFINED,
112 getFormatCode("foo.example", "application/x-example"));
113 assertHexEquals(MtpConstants.FORMAT_UNDEFINED_AUDIO,
114 getFormatCode("foo.example", "audio/x-example"));
115 assertHexEquals(MtpConstants.FORMAT_UNDEFINED_VIDEO,
116 getFormatCode("foo.example", "video/x-example"));
117 assertHexEquals(MtpConstants.FORMAT_DEFINED,
118 getFormatCode("foo.example", "image/x-example"));
119 }
120
121 private static void assertConsistent(String path, String mimeType, int formatCode) {
122 assertHexEquals(formatCode, getFormatCode(path, null));
123 assertHexEquals(formatCode, getFormatCode(null, mimeType));
124 assertHexEquals(formatCode, getFormatCode(path, mimeType));
125
126 assertEquals(mimeType, getMimeType(path, MtpConstants.FORMAT_UNDEFINED));
127 assertEquals(mimeType, getMimeType(null, formatCode));
128 assertEquals(mimeType, getMimeType(path, formatCode));
129 }
130
131 private static void assertHexEquals(int expected, int actual) {
132 assertEquals(Integer.toHexString(expected), Integer.toHexString(actual));
133 }
134}