1 /* 2 * Copyright (C) 2008 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 android.content.res.cts; 18 19 import java.io.File; 20 import java.io.FileInputStream; 21 import java.io.FileNotFoundException; 22 import java.io.FileOutputStream; 23 import java.io.IOException; 24 import java.util.Arrays; 25 26 import android.content.res.AssetFileDescriptor; 27 import android.os.Bundle; 28 import android.os.Parcel; 29 import android.os.ParcelFileDescriptor; 30 import android.test.AndroidTestCase; 31 import android.test.suitebuilder.annotation.SmallTest; 32 33 public class AssetFileDescriptorTest extends AndroidTestCase { 34 private static final long START_OFFSET = 0; 35 private static final long LENGTH = 100; 36 private static final String FILE_NAME = "testAssetFileDescriptor"; 37 private static final byte[] FILE_DATA = 38 new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; 39 private static final int FILE_END = -1; 40 private AssetFileDescriptor mAssetFileDes; 41 private File mFile; 42 private ParcelFileDescriptor mFd; 43 private FileOutputStream mOutputStream; 44 private FileInputStream mInputStream; 45 46 @Override 47 protected void setUp() throws Exception { 48 super.setUp(); 49 mFile = new File(getContext().getFilesDir(), FILE_NAME); 50 mFile.createNewFile(); 51 initAssetFileDescriptor(); 52 } 53 54 private void initAssetFileDescriptor() throws FileNotFoundException { 55 mFd = ParcelFileDescriptor.open(mFile, ParcelFileDescriptor.MODE_READ_WRITE); 56 mAssetFileDes = new AssetFileDescriptor(mFd, START_OFFSET, LENGTH); 57 } 58 59 @Override 60 protected void tearDown() throws Exception { 61 super.tearDown(); 62 // As {@link AssetFileDescripter#createOutputStream()} 63 // and {@link AssetFileDescripter#createInputStream()} doc, 64 // the input and output stream will be auto closed when the AssetFileDescriptor closed. 65 if (mAssetFileDes != null) { 66 mAssetFileDes.close(); 67 } 68 getContext().deleteFile(FILE_NAME); 69 } 70 71 @SmallTest 72 public void testConstructor() throws IOException { 73 ParcelFileDescriptor fd = ParcelFileDescriptor.open( 74 mFile, ParcelFileDescriptor.MODE_READ_WRITE); 75 AssetFileDescriptor assetFileDes; 76 Bundle extras; 77 78 assetFileDes = new AssetFileDescriptor(fd, START_OFFSET, LENGTH); 79 assertNotNull(assetFileDes); 80 assetFileDes.close(); 81 82 extras = null; 83 assetFileDes = new AssetFileDescriptor(fd, START_OFFSET, LENGTH, extras); 84 assertEquals(extras, assetFileDes.getExtras()); 85 assertNotNull(assetFileDes); 86 assetFileDes.close(); 87 88 extras = new Bundle(); 89 assetFileDes = new AssetFileDescriptor(fd, START_OFFSET, LENGTH, extras); 90 assertEquals(extras, assetFileDes.getExtras()); 91 assertNotNull(assetFileDes); 92 assetFileDes.close(); 93 } 94 95 @SmallTest 96 public void testInputOutputStream() throws IOException { 97 /* 98 * test createOutputStream() and createInputStrean() 99 * test point 100 * 1. createOutputStream() and createInputStrean() should only call this once 101 * for a particular asset. 102 * 2. outputStream can write and inputStream can read. 103 * 3. auto close. 104 */ 105 mOutputStream = mAssetFileDes.createOutputStream(); 106 assertNotNull(mOutputStream); 107 mOutputStream.write(FILE_DATA); 108 mOutputStream.flush(); 109 mOutputStream.close(); 110 mOutputStream = null; 111 try { 112 mOutputStream = mAssetFileDes.createOutputStream(); 113 fail("Should throw IOException"); 114 } catch (IOException e) { 115 // expect 116 } 117 try { 118 mInputStream = mAssetFileDes.createInputStream(); 119 fail("Should throw IOException"); 120 } catch (IOException e) { 121 // expect 122 } 123 mAssetFileDes.close(); 124 mAssetFileDes = null; 125 126 initAssetFileDescriptor(); 127 mInputStream = mAssetFileDes.createInputStream(); 128 assertNotNull(mInputStream); 129 byte[] dataFromFile = new byte[FILE_DATA.length]; 130 int readLength = 0; 131 int readByte = 0; 132 while ((readByte != FILE_END) && (readLength < FILE_DATA.length)) { 133 readLength += readByte; 134 readByte = mInputStream.read(dataFromFile, 135 readLength, FILE_DATA.length - readLength); 136 } 137 assertEquals(FILE_DATA.length, readLength); 138 assertTrue(Arrays.equals(FILE_DATA, dataFromFile)); 139 assertEquals(FILE_END, mInputStream.read()); 140 mInputStream.close(); 141 mInputStream = null; 142 try { 143 mInputStream = mAssetFileDes.createInputStream(); 144 fail("Should throw IOException"); 145 } catch (IOException e) { 146 // expect 147 } 148 try { 149 mOutputStream = mAssetFileDes.createOutputStream(); 150 fail("Should throw IOException"); 151 } catch (IOException e) { 152 // expect 153 } 154 mAssetFileDes.close(); 155 mAssetFileDes = null; 156 157 initAssetFileDescriptor(); 158 mOutputStream = mAssetFileDes.createOutputStream(); 159 mAssetFileDes.close(); 160 mAssetFileDes = null; 161 try { 162 mOutputStream.write(FILE_DATA); 163 fail("Should throw IOException"); 164 } catch (IOException e) { 165 // expect 166 } 167 168 initAssetFileDescriptor(); 169 mInputStream = mAssetFileDes.createInputStream(); 170 mAssetFileDes.close(); 171 mAssetFileDes = null; 172 try { 173 mInputStream.read(); 174 fail("Should throw IOException"); 175 } catch (IOException e) { 176 // expect 177 } 178 } 179 180 @SmallTest 181 public void testMiscMethod() { 182 // test getLength() 183 assertEquals(LENGTH, mAssetFileDes.getLength()); 184 185 // test getStartOffset() 186 assertEquals(START_OFFSET, mAssetFileDes.getStartOffset()); 187 188 // test getParcelFileDescriptor() getFileDescriptor() toString() and describeContents() 189 assertSame(mFd, mAssetFileDes.getParcelFileDescriptor()); 190 assertSame(mFd.getFileDescriptor(), mAssetFileDes.getFileDescriptor()); 191 assertNotNull(mAssetFileDes.toString()); 192 assertEquals(mFd.describeContents(), mAssetFileDes.describeContents()); 193 194 // test writeToParcel(), test by assert source and out FileDescriptor content equals. 195 Parcel parcel = Parcel.obtain(); 196 mAssetFileDes.writeToParcel(parcel, 0); 197 parcel.setDataPosition(0); 198 AssetFileDescriptor out = AssetFileDescriptor.CREATOR.createFromParcel(parcel); 199 assertEquals(out.getStartOffset(), mAssetFileDes.getStartOffset()); 200 assertEquals(out.getDeclaredLength(), mAssetFileDes.getDeclaredLength()); 201 assertEquals(out.getParcelFileDescriptor().getStatSize(), 202 mAssetFileDes.getParcelFileDescriptor().getStatSize()); 203 204 parcel.recycle(); 205 } 206 } 207