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