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 android.content.res.Resources;
     20 import android.test.InstrumentationTestCase;
     21 import android.util.Xml;
     22 
     23 import org.xmlpull.v1.XmlPullParser;
     24 
     25 import java.io.FileInputStream;
     26 import java.io.InputStream;
     27 import java.io.InputStreamReader;
     28 
     29 public class ExifXmlDataTestCase extends InstrumentationTestCase {
     30 
     31     private static final String RES_ID_TITLE = "Resource ID: %x";
     32 
     33     private InputStream mImageInputStream;
     34     private InputStream mXmlInputStream;
     35     private XmlPullParser mXmlParser;
     36     private final String mImagePath;
     37     private final String mXmlPath;
     38     private final int mImageResourceId;
     39     private final int mXmlResourceId;
     40 
     41     public ExifXmlDataTestCase(int imageRes, int xmlRes) {
     42         mImagePath = null;
     43         mXmlPath = null;
     44         mImageResourceId = imageRes;
     45         mXmlResourceId = xmlRes;
     46     }
     47 
     48     public ExifXmlDataTestCase(String imagePath, String xmlPath) {
     49         mImagePath = imagePath;
     50         mXmlPath = xmlPath;
     51         mImageResourceId = 0;
     52         mXmlResourceId = 0;
     53     }
     54 
     55     protected InputStream getImageInputStream() {
     56         return mImageInputStream;
     57     }
     58 
     59     protected XmlPullParser getXmlParser() {
     60         return mXmlParser;
     61     }
     62 
     63     @Override
     64     public void setUp() throws Exception {
     65         try {
     66             if (mImagePath != null) {
     67                 mImageInputStream = new FileInputStream(mImagePath);
     68                 mXmlInputStream = new FileInputStream(mXmlPath);
     69                 mXmlParser = Xml.newPullParser();
     70                 mXmlParser.setInput(new InputStreamReader(mXmlInputStream));
     71             } else {
     72                 Resources res = getInstrumentation().getContext().getResources();
     73                 mImageInputStream = res.openRawResource(mImageResourceId);
     74                 mXmlParser = res.getXml(mXmlResourceId);
     75             }
     76         } catch (Exception e) {
     77             throw new Exception(getImageTitle(), e);
     78         }
     79     }
     80 
     81     @Override
     82     public void tearDown() throws Exception {
     83         Util.closeSilently(mImageInputStream);
     84         Util.closeSilently(mXmlInputStream);
     85         mXmlParser = null;
     86     }
     87 
     88     protected String getImageTitle() {
     89         if (mImagePath != null) {
     90             return mImagePath;
     91         } else {
     92             return String.format(RES_ID_TITLE, mImageResourceId);
     93         }
     94     }
     95 
     96     protected InputStream reopenFileStream() throws Exception {
     97         try {
     98             if (mImagePath != null) {
     99                 return new FileInputStream(mImagePath);
    100             } else {
    101                 Resources res = getInstrumentation().getContext().getResources();
    102                 return res.openRawResource(mImageResourceId);
    103             }
    104         } catch (Exception e) {
    105             throw new Exception(getImageTitle(), e);
    106         }
    107     }
    108 }
    109