| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.util; |
| 18 |
|
| 19 |
import android.media.ExifInterface; |
| 20 |
import android.os.Bundle; |
| 21 |
import android.provider.DocumentsContract; |
| 22 |
import android.provider.MetadataReader; |
| 23 |
|
| 24 |
import libcore.io.IoUtils; |
| 25 |
|
| 26 |
import junit.framework.TestCase; |
| 27 |
|
| 28 |
import org.junit.After; |
| 29 |
import org.junit.Before; |
| 30 |
import org.junit.Test; |
| 31 |
|
| 32 |
import java.io.FileInputStream; |
| 33 |
import java.io.FileNotFoundException; |
| 34 |
import java.io.IOException; |
| 35 |
import java.io.InputStream; |
| 36 |
|
| 37 |
public 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 |
} |