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 android.util;
18
19import android.media.ExifInterface;
20import android.os.Bundle;
21import android.provider.DocumentsContract;
22import android.provider.MetadataReader;
23
24import libcore.io.IoUtils;
25
26import junit.framework.TestCase;
27
28import org.junit.After;
29import org.junit.Before;
30import org.junit.Test;
31
32import java.io.FileInputStream;
33import java.io.FileNotFoundException;
34import java.io.IOException;
35import java.io.InputStream;
36
37public class MetadataReaderTest extends TestCase {
38
39    private InputStream mInputStream;
40    private Bundle mData;
41
42    @Before
43    protected void setUp() throws Exception {
44        mInputStream = getClass().getClassLoader().getResourceAsStream("res/drawable/image.jpg");
45        mData = new Bundle();
46    }
47
48    @After
49    protected void tearDown() throws Exception {
50        IoUtils.closeQuietly(mInputStream);
51    }
52
53    @Test
54    public void testGetMetadata() throws IOException {
55        MetadataReader.getMetadata(mData, mInputStream, "image/jpg", null);
56        Bundle exif = mData.getBundle(DocumentsContract.METADATA_EXIF);
57        assertEquals("3036", String.valueOf(exif.getInt(ExifInterface.TAG_IMAGE_WIDTH)));
58        assertEquals("4048", String.valueOf(exif.getInt(ExifInterface.TAG_IMAGE_LENGTH)));
59        assertEquals("2017:07:26 21:06:25", exif.getString(ExifInterface.TAG_DATETIME));
60        assertEquals("33/1,59/1,4530/100", exif.getString(ExifInterface.TAG_GPS_LATITUDE));
61        assertEquals("N", exif.getString(ExifInterface.TAG_GPS_LATITUDE_REF));
62        assertEquals("118/1,28/1,3124/100", exif.getString(ExifInterface.TAG_GPS_LONGITUDE));
63        assertEquals("W", exif.getString(ExifInterface.TAG_GPS_LONGITUDE_REF));
64        assertEquals("Google", exif.getString(ExifInterface.TAG_MAKE));
65        assertEquals("Pixel", exif.getString(ExifInterface.TAG_MODEL));
66        assertEquals(mData.getStringArray(DocumentsContract.METADATA_TYPES)[0],
67                DocumentsContract.METADATA_EXIF);
68    }
69
70    @Test
71    public void testGetMetadata_JpegOneTag() throws IOException {
72        String[] tags = {ExifInterface.TAG_MAKE};
73        MetadataReader.getMetadata(mData, mInputStream, "image/jpg", tags);
74        assertEquals("Google",
75                mData.getBundle(DocumentsContract.METADATA_EXIF).getString(tags[0]));
76    }
77
78    @Test
79    public void testGetMetadata_JpegNoResults() throws IOException {
80        String[] tags = {ExifInterface.TAG_SPECTRAL_SENSITIVITY};
81        assertEquals(0, mData.size());
82        MetadataReader.getMetadata(mData, mInputStream, "image/jpg", tags);
83        assertEquals(1, mData.size());
84        assertEquals(mData.getStringArray(DocumentsContract.METADATA_TYPES).length, 0);
85    }
86
87    @Test
88    public void testGetMetadata_BadFile() {
89        try {
90            InputStream stream = new FileInputStream("badString");
91            MetadataReader.getMetadata(mData, stream, "image/jpg", null);
92        } catch (IOException e) {
93            assertEquals(FileNotFoundException.class, e.getClass());
94        }
95    }
96
97    @Test
98    public void testGetMetadata_UnsupportedMimeType() throws IOException {
99        MetadataReader.getMetadata(mData, mInputStream, "no/metadata", null);
100        assertEquals(1, mData.size());
101    }
102
103    @Test
104    public void testGetMetadata_NoTags() throws IOException {
105        MetadataReader.getMetadata(mData, mInputStream, "image/jpg", new String[0]);
106        assertEquals(1, mData.size());
107    }
108
109    @Test
110    public void testGetMetadata_Png() throws IOException {
111        InputStream pngStream = getClass().getClassLoader().getResourceAsStream("res/drawable/png.png");
112        MetadataReader.getMetadata(mData, pngStream, "image/png", null);
113        assertEquals(1, mData.size());
114
115    }
116
117}
118