1 /*
2  * Copyright (C) 2019 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.providers.media.util;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 
23 import android.content.ClipDescription;
24 import android.mtp.MtpConstants;
25 import android.provider.MediaStore.Files.FileColumns;
26 
27 import androidx.test.runner.AndroidJUnit4;
28 
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 
32 import java.io.File;
33 
34 @RunWith(AndroidJUnit4.class)
35 public class MimeUtilsTest {
36     @Test
testConstructor()37     public void testConstructor() {
38         new MimeUtils();
39     }
40 
41     @Test
testResolveMimeType()42     public void testResolveMimeType() throws Exception {
43         assertEquals("image/jpeg",
44                 MimeUtils.resolveMimeType(new File("foo.jpg")));
45         assertEquals("image/jpeg",
46                 MimeUtils.resolveMimeType(new File(".hidden.jpg")));
47 
48         assertEquals(ClipDescription.MIMETYPE_UNKNOWN,
49                 MimeUtils.resolveMimeType(new File("foo")));
50         assertEquals(ClipDescription.MIMETYPE_UNKNOWN,
51                 MimeUtils.resolveMimeType(new File("foo.doesnotexist")));
52     }
53 
54     @Test
testExtractPrimaryType()55     public void testExtractPrimaryType() throws Exception {
56         assertEquals("image",
57                 MimeUtils.extractPrimaryType("image/jpeg"));
58     }
59 
60     @Test
testResolveMediaType()61     public void testResolveMediaType() throws Exception {
62         assertEquals(FileColumns.MEDIA_TYPE_AUDIO,
63                 MimeUtils.resolveMediaType("audio/mpeg"));
64         assertEquals(FileColumns.MEDIA_TYPE_VIDEO,
65                 MimeUtils.resolveMediaType("video/mpeg"));
66         assertEquals(FileColumns.MEDIA_TYPE_IMAGE,
67                 MimeUtils.resolveMediaType("image/jpeg"));
68         assertEquals(FileColumns.MEDIA_TYPE_DOCUMENT,
69                 MimeUtils.resolveMediaType("text/plain"));
70         assertEquals(FileColumns.MEDIA_TYPE_DOCUMENT,
71                 MimeUtils.resolveMediaType("application/pdf"));
72         assertEquals(FileColumns.MEDIA_TYPE_DOCUMENT,
73                 MimeUtils.resolveMediaType("application/msword"));
74         assertEquals(FileColumns.MEDIA_TYPE_DOCUMENT,
75                 MimeUtils.resolveMediaType("application/vnd.ms-excel"));
76         assertEquals(FileColumns.MEDIA_TYPE_DOCUMENT,
77                 MimeUtils.resolveMediaType("application/vnd.ms-powerpoint"));
78         assertEquals(FileColumns.MEDIA_TYPE_NONE,
79                 MimeUtils.resolveMediaType("application/x-does-not-exist"));
80 
81         // Make sure we catch playlists before audio
82         assertEquals(FileColumns.MEDIA_TYPE_PLAYLIST,
83                 MimeUtils.resolveMediaType("audio/mpegurl"));
84     }
85 
86     @Test
testIsDocumentMimeType()87     public void testIsDocumentMimeType() throws Exception {
88         assertTrue(MimeUtils.isDocumentMimeType(
89                 "application/vnd.ms-excel.addin.macroEnabled.12"));
90         assertTrue(MimeUtils.isDocumentMimeType(
91                 "application/vnd.ms-powerpoint.addin.macroEnabled.12"));
92         assertTrue(MimeUtils.isDocumentMimeType(
93                 "application/vnd.ms-word.document.macroEnabled.12"));
94 
95         assertFalse(MimeUtils.isDocumentMimeType(
96                 "application/zip"));
97     }
98 
99     @Test
testResolveFormatCode()100     public void testResolveFormatCode() throws Exception {
101         assertEquals(MtpConstants.FORMAT_UNDEFINED_AUDIO,
102                 MimeUtils.resolveFormatCode("audio/mpeg"));
103         assertEquals(MtpConstants.FORMAT_UNDEFINED_VIDEO,
104                 MimeUtils.resolveFormatCode("video/mpeg"));
105         assertEquals(MtpConstants.FORMAT_DEFINED,
106                 MimeUtils.resolveFormatCode("image/jpeg"));
107         assertEquals(MtpConstants.FORMAT_UNDEFINED,
108                 MimeUtils.resolveFormatCode("application/x-does-not-exist"));
109     }
110 
111     @Test
testGetExtensionFromMimeType()112     public void testGetExtensionFromMimeType() {
113         assertEquals(".svg", MimeUtils.getExtensionFromMimeType("image/svg+xml"));
114         assertEquals(MimeUtils.DEFAULT_IMAGE_FILE_EXTENSION,
115                 MimeUtils.getExtensionFromMimeType("image/foo"));
116 
117         assertEquals(".mpeg", MimeUtils.getExtensionFromMimeType("video/mpeg"));
118         assertEquals(MimeUtils.DEFAULT_VIDEO_FILE_EXTENSION,
119                 MimeUtils.getExtensionFromMimeType("video/foo"));
120 
121         assertEquals("", MimeUtils.getExtensionFromMimeType("foo/bar"));
122         assertEquals("", MimeUtils.getExtensionFromMimeType(null));
123     }
124 }
125