1/*
2 * Copyright (C) 2017 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.documentsui;
18
19import static junit.framework.Assert.assertEquals;
20
21import android.annotation.StringRes;
22import android.content.Context;
23import android.content.res.Resources;
24import android.support.test.InstrumentationRegistry;
25import android.support.test.filters.SmallTest;
26import android.support.test.runner.AndroidJUnit4;
27
28import libcore.net.MimeUtils;
29
30import org.junit.Before;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33
34@RunWith(AndroidJUnit4.class)
35@SmallTest
36public class FileTypeMapTest {
37
38    private Resources mRes;
39    private FileTypeMap mMap;
40
41    @Before
42    public void setUp() {
43        Context context = InstrumentationRegistry.getTargetContext();
44        mRes = context.getResources();
45        mMap = new FileTypeMap(context);
46    }
47
48    @Test
49    public void testPlainTextType() {
50        String expected = mRes.getString(R.string.txt_file_type);
51        assertEquals(expected, mMap.lookup("text/plain"));
52    }
53
54    @Test
55    public void testPortableDocumentFormatType() {
56        String expected = mRes.getString(R.string.pdf_file_type);
57        assertEquals(expected, mMap.lookup("application/pdf"));
58    }
59
60    @Test
61    public void testMsWordType() {
62        String expected = mRes.getString(R.string.word_file_type);
63        assertEquals(expected, mMap.lookup("application/msword"));
64    }
65
66    @Test
67    public void testGoogleDocType() {
68        String expected = mRes.getString(R.string.gdoc_file_type);
69        assertEquals(expected, mMap.lookup("application/vnd.google-apps.document"));
70    }
71
72    @Test
73    public void testZipType() {
74        final String mime = "application/zip";
75        String expected = getExtensionTypeFromExtension(R.string.archive_file_type, "Zip");
76        assertEquals(expected, mMap.lookup(mime));
77    }
78
79    @Test
80    public void testMp3Type() {
81        final String mime = "audio/mpeg";
82        String expected = getExtensionTypeFromMime(R.string.audio_extension_file_type, mime);
83        assertEquals(expected, mMap.lookup(mime));
84    }
85
86    @Test
87    public void testMkvType() {
88        final String mime = "video/avi";
89        String expected = getExtensionTypeFromMime(R.string.video_extension_file_type, mime);
90        assertEquals(expected, mMap.lookup(mime));
91    }
92
93    @Test
94    public void testJpgType() {
95        final String mime = "image/jpeg";
96        String expected = getExtensionTypeFromMime(R.string.image_extension_file_type, mime);
97        assertEquals(expected, mMap.lookup(mime));
98    }
99
100    @Test
101    public void testOggType() {
102        final String mime = "application/ogg";
103        String expected = getExtensionTypeFromMime(R.string.audio_extension_file_type, mime);
104        assertEquals(expected, mMap.lookup("application/ogg"));
105    }
106
107    @Test
108    public void testFlacType() {
109        final String mime = "application/x-flac";
110        String expected = getExtensionTypeFromMime(R.string.audio_extension_file_type, mime);
111        assertEquals(expected, mMap.lookup(mime));
112    }
113
114    private String getExtensionTypeFromMime(@StringRes int formatStringId, String mime) {
115        final String extension = MimeUtils.guessExtensionFromMimeType(mime).toUpperCase();
116        return getExtensionTypeFromExtension(formatStringId, extension);
117    }
118
119    private String getExtensionTypeFromExtension(@StringRes int formatStringId, String extension) {
120        final String format = mRes.getString(formatStringId);
121        return String.format(format, extension);
122    }
123}
124