blob: 193fb50b06f372666e3adc984a70a216c6caa73d [file] [log] [blame]
Julian Mancinib6505152017-06-27 13:29:09 -07001/*
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:17 19:19:28", exif.getString(ExifInterface.TAG_DATETIME));
60 assertEquals(0.0, exif.getDouble(ExifInterface.TAG_GPS_LATITUDE));
61 assertEquals(0.0, exif.getDouble(ExifInterface.TAG_GPS_LONGITUDE));
62 assertEquals("Google", exif.getString(ExifInterface.TAG_MAKE));
63 assertEquals("Pixel", exif.getString(ExifInterface.TAG_MODEL));
64 assertEquals(mData.getStringArray(DocumentsContract.METADATA_TYPES)[0],
65 DocumentsContract.METADATA_EXIF);
66 }
67
68 @Test
69 public void testGetMetadata_JpegOneTag() throws IOException {
70 String[] tags = {ExifInterface.TAG_MAKE};
71 MetadataReader.getMetadata(mData, mInputStream, "image/jpg", tags);
72 assertEquals("Google",
73 mData.getBundle(DocumentsContract.METADATA_EXIF).getString(tags[0]));
74 }
75
76 @Test
77 public void testGetMetadata_JpegNoResults() throws IOException {
78 String[] tags = {ExifInterface.TAG_SPECTRAL_SENSITIVITY};
79 assertEquals(0, mData.size());
80 MetadataReader.getMetadata(mData, mInputStream, "image/jpg", tags);
81 assertEquals(1, mData.size());
82 assertEquals(mData.getStringArray(DocumentsContract.METADATA_TYPES).length, 0);
83 }
84
85 @Test
86 public void testGetMetadata_BadFile() {
87 try {
88 InputStream stream = new FileInputStream("badString");
89 MetadataReader.getMetadata(mData, stream, "image/jpg", null);
90 } catch (IOException e) {
91 assertEquals(FileNotFoundException.class, e.getClass());
92 }
93 }
94
95 @Test
96 public void testGetMetadata_UnsupportedMimeType() throws IOException {
97 MetadataReader.getMetadata(mData, mInputStream, "no/metadata", null);
98 assertEquals(1, mData.size());
99 }
100
101 @Test
102 public void testGetMetadata_NoTags() throws IOException {
103 MetadataReader.getMetadata(mData, mInputStream, "image/jpg", new String[0]);
104 assertEquals(1, mData.size());
105 }
106
107 @Test
108 public void testGetMetadata_Png() throws IOException {
109 InputStream pngStream = getClass().getClassLoader().getResourceAsStream("res/drawable/png.png");
110 MetadataReader.getMetadata(mData, pngStream, "image/png", null);
111 assertEquals(1, mData.size());
112
113 }
114
115}