Home | History | Annotate | Download | only in exif
      1 /*
      2  * Copyright (C) 2012 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.gallery3d.exif;
     18 
     19 import junit.framework.TestCase;
     20 
     21 import java.nio.ByteOrder;
     22 import java.util.HashMap;
     23 import java.util.List;
     24 import java.util.Map;
     25 
     26 public class ExifDataTest extends TestCase {
     27     Map<Integer, ExifTag> mTestTags;
     28     ExifInterface mInterface;
     29     private ExifTag mVersionTag;
     30     private ExifTag mGpsVersionTag;
     31     private ExifTag mModelTag;
     32     private ExifTag mDateTimeTag;
     33     private ExifTag mCompressionTag;
     34     private ExifTag mThumbnailFormatTag;
     35     private ExifTag mLongitudeTag;
     36     private ExifTag mShutterTag;
     37     private ExifTag mInteropIndex;
     38 
     39     @Override
     40     public void setUp() throws Exception {
     41         super.setUp();
     42 
     43         mInterface = new ExifInterface();
     44 
     45         // TYPE_UNDEFINED with 4 components
     46         mVersionTag = mInterface.buildTag(ExifInterface.TAG_EXIF_VERSION, new byte[] {
     47                 5, 4, 3, 2
     48         });
     49         // TYPE_UNSIGNED_BYTE with 4 components
     50         mGpsVersionTag = mInterface.buildTag(ExifInterface.TAG_GPS_VERSION_ID, new byte[] {
     51                 6, 7, 8, 9
     52         });
     53         // TYPE ASCII with arbitrary length
     54         mModelTag = mInterface.buildTag(ExifInterface.TAG_MODEL, "helloworld");
     55         // TYPE_ASCII with 20 components
     56         mDateTimeTag = mInterface.buildTag(ExifInterface.TAG_DATE_TIME, "2013:02:11 20:20:20");
     57         // TYPE_UNSIGNED_SHORT with 1 components
     58         mCompressionTag = mInterface.buildTag(ExifInterface.TAG_COMPRESSION, 100);
     59         // TYPE_UNSIGNED_LONG with 1 components
     60         mThumbnailFormatTag =
     61                 mInterface.buildTag(ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT, 100);
     62         // TYPE_UNSIGNED_RATIONAL with 3 components
     63         mLongitudeTag = mInterface.buildTag(ExifInterface.TAG_GPS_LONGITUDE, new Rational[] {
     64                 new Rational(2, 2), new Rational(11, 11),
     65                 new Rational(102, 102)
     66         });
     67         // TYPE_RATIONAL with 1 components
     68         mShutterTag = mInterface
     69                 .buildTag(ExifInterface.TAG_SHUTTER_SPEED_VALUE, new Rational(4, 6));
     70         // TYPE_ASCII with arbitrary length
     71         mInteropIndex = mInterface.buildTag(ExifInterface.TAG_INTEROPERABILITY_INDEX, "foo");
     72 
     73         mTestTags = new HashMap<Integer, ExifTag>();
     74 
     75         mTestTags.put(ExifInterface.TAG_EXIF_VERSION, mVersionTag);
     76         mTestTags.put(ExifInterface.TAG_GPS_VERSION_ID, mGpsVersionTag);
     77         mTestTags.put(ExifInterface.TAG_MODEL, mModelTag);
     78         mTestTags.put(ExifInterface.TAG_DATE_TIME, mDateTimeTag);
     79         mTestTags.put(ExifInterface.TAG_COMPRESSION, mCompressionTag);
     80         mTestTags.put(ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT, mThumbnailFormatTag);
     81         mTestTags.put(ExifInterface.TAG_GPS_LONGITUDE, mLongitudeTag);
     82         mTestTags.put(ExifInterface.TAG_SHUTTER_SPEED_VALUE, mShutterTag);
     83         mTestTags.put(ExifInterface.TAG_INTEROPERABILITY_INDEX, mInteropIndex);
     84     }
     85 
     86     @Override
     87     public void tearDown() throws Exception {
     88         super.tearDown();
     89         mInterface = null;
     90         mTestTags = null;
     91     }
     92 
     93     public void testAddTag() {
     94         ExifData exifData = new ExifData(ByteOrder.BIG_ENDIAN);
     95 
     96         // Add all test tags
     97         for (ExifTag t : mTestTags.values()) {
     98             assertTrue(exifData.addTag(t) == null);
     99         }
    100 
    101         // Make sure no initial thumbnails
    102         assertFalse(exifData.hasCompressedThumbnail());
    103         assertFalse(exifData.hasUncompressedStrip());
    104 
    105         // Check that we can set thumbnails
    106         exifData.setStripBytes(3, new byte[] {
    107                 1, 2, 3, 4, 5
    108         });
    109         assertTrue(exifData.hasUncompressedStrip());
    110         exifData.setCompressedThumbnail(new byte[] {
    111             1
    112         });
    113         assertTrue(exifData.hasCompressedThumbnail());
    114 
    115         // Check that we can clear thumbnails
    116         exifData.clearThumbnailAndStrips();
    117         assertFalse(exifData.hasCompressedThumbnail());
    118         assertFalse(exifData.hasUncompressedStrip());
    119 
    120         // Make sure ifds exist
    121         for (int i : IfdData.getIfds()) {
    122             assertTrue(exifData.getIfdData(i) != null);
    123         }
    124 
    125         // Get all test tags
    126         List<ExifTag> allTags = exifData.getAllTags();
    127         assertTrue(allTags != null);
    128 
    129         // Make sure all test tags are in data
    130         for (ExifTag t : mTestTags.values()) {
    131             boolean check = false;
    132             for (ExifTag i : allTags) {
    133                 if (t.equals(i)) {
    134                     check = true;
    135                     break;
    136                 }
    137             }
    138             assertTrue(check);
    139         }
    140 
    141         // Check if getting tags for a tid works
    142         List<ExifTag> tidTags = exifData.getAllTagsForTagId(ExifInterface
    143                 .getTrueTagKey(ExifInterface.TAG_SHUTTER_SPEED_VALUE));
    144         assertTrue(tidTags.size() == 1);
    145         assertTrue(tidTags.get(0).equals(mShutterTag));
    146 
    147         // Check if getting tags for an ifd works
    148         List<ExifTag> ifdTags = exifData.getAllTagsForIfd(IfdId.TYPE_IFD_INTEROPERABILITY);
    149         assertTrue(ifdTags.size() == 1);
    150         assertTrue(ifdTags.get(0).equals(mInteropIndex));
    151 
    152     }
    153 }
    154