Home | History | Annotate | Download | only in cts
      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 package android.content.res.cts;
     17 
     18 import static org.junit.Assert.assertEquals;
     19 import static org.junit.Assert.assertTrue;
     20 import static org.junit.Assert.fail;
     21 
     22 import java.io.IOException;
     23 import java.io.InputStream;
     24 import java.nio.charset.StandardCharsets;
     25 
     26 import android.content.res.AssetManager;
     27 import android.support.test.InstrumentationRegistry;
     28 import android.support.test.filters.SmallTest;
     29 
     30 import org.junit.After;
     31 import org.junit.Before;
     32 import org.junit.Test;
     33 
     34 @SmallTest
     35 public class AssetManager_AssetInputStreamTest {
     36     private static final byte[] EXPECTED_BYTES = "OneTwoThreeFourFiveSixSevenEightNineTen".getBytes(
     37             StandardCharsets.UTF_8);
     38     private InputStream mAssetInputStream;
     39 
     40     @Before
     41     public void setUp() throws Exception {
     42         mAssetInputStream = InstrumentationRegistry.getContext().getAssets().open("text.txt");
     43     }
     44 
     45     @After
     46     public void tearDown() throws Exception {
     47         mAssetInputStream.close();
     48     }
     49 
     50     @Test
     51     public void testClose() throws Exception {
     52         mAssetInputStream.close();
     53         try {
     54             mAssetInputStream.read();
     55             fail("read after close should throw an exception");
     56         } catch (IllegalStateException e) {
     57             // expected
     58         }
     59     }
     60 
     61     @Test
     62     public void testGetAssetInt() {
     63         AssetManager.AssetInputStream assetInputStream =
     64                 (AssetManager.AssetInputStream) mAssetInputStream;
     65         try {
     66             // getAssetInt is no longer supported.
     67             assetInputStream.getAssetInt();
     68             fail("getAssetInt should throw an exception");
     69         } catch (UnsupportedOperationException expected) {
     70         }
     71     }
     72 
     73     @Test
     74     public void testMarkReset() throws IOException {
     75         assertTrue(mAssetInputStream.markSupported());
     76         for (int i = 0; i < 10; i++) {
     77             assertEquals(EXPECTED_BYTES[i], mAssetInputStream.read());
     78         }
     79         mAssetInputStream.mark(10);
     80         mAssetInputStream.reset();
     81         for (int i = 0; i < 10; i++) {
     82             assertEquals(EXPECTED_BYTES[10 + i], mAssetInputStream.read());
     83         }
     84     }
     85 
     86     @Test
     87     public void testSingleByteRead() throws Exception {
     88         assertEquals(EXPECTED_BYTES.length, mAssetInputStream.available());
     89         for (int i = 0; i < EXPECTED_BYTES.length; i++) {
     90             assertEquals(EXPECTED_BYTES[i], mAssetInputStream.read());
     91             assertEquals(EXPECTED_BYTES.length - i - 1, mAssetInputStream.available());
     92         }
     93 
     94         // Check end-of-file condition.
     95         assertEquals(-1, mAssetInputStream.read());
     96         assertEquals(0, mAssetInputStream.available());
     97     }
     98 
     99     @Test
    100     public void testByteArrayRead() throws Exception {
    101         byte[] buffer = new byte[10];
    102         assertEquals(10, mAssetInputStream.read(buffer));
    103         for (int i = 0; i < 10; i++) {
    104             assertEquals(EXPECTED_BYTES[i], buffer[i]);
    105         }
    106 
    107         buffer = new byte[5];
    108         assertEquals(5, mAssetInputStream.read(buffer));
    109         for (int i = 0; i < 5; i++) {
    110             assertEquals(EXPECTED_BYTES[10 + i], buffer[i]);
    111         }
    112 
    113         // Check end-of-file condition.
    114         buffer = new byte[EXPECTED_BYTES.length - 15];
    115         assertEquals(buffer.length, mAssetInputStream.read(buffer));
    116         assertEquals(-1, mAssetInputStream.read(buffer));
    117         assertEquals(0, mAssetInputStream.available());
    118     }
    119 
    120     @Test
    121     public void testByteArrayReadOffset() throws Exception {
    122         byte[] buffer = new byte[15];
    123         assertEquals(10, mAssetInputStream.read(buffer, 0, 10));
    124         assertEquals(EXPECTED_BYTES.length - 10, mAssetInputStream.available());
    125         for (int i = 0; i < 10; i++) {
    126             assertEquals(EXPECTED_BYTES[i], buffer[i]);
    127         }
    128 
    129         assertEquals(5, mAssetInputStream.read(buffer, 10, 5));
    130         assertEquals(EXPECTED_BYTES.length - 15, mAssetInputStream.available());
    131         for (int i = 0; i < 15; i++) {
    132             assertEquals(EXPECTED_BYTES[i], buffer[i]);
    133         }
    134 
    135         // Check end-of-file condition.
    136         buffer = new byte[EXPECTED_BYTES.length];
    137         assertEquals(EXPECTED_BYTES.length - 15,
    138                 mAssetInputStream.read(buffer, 15, EXPECTED_BYTES.length - 15));
    139         assertEquals(-1, mAssetInputStream.read(buffer, 0, 1));
    140         assertEquals(0, mAssetInputStream.available());
    141     }
    142 
    143     @Test
    144     public void testSkip() throws Exception {
    145         assertEquals(8, mAssetInputStream.skip(8));
    146         assertEquals(EXPECTED_BYTES.length - 8, mAssetInputStream.available());
    147         assertEquals(EXPECTED_BYTES[8], mAssetInputStream.read());
    148 
    149         // Check that skip respects the available space.
    150         assertEquals(EXPECTED_BYTES.length - 8 - 1, mAssetInputStream.skip(1000));
    151         assertEquals(0, mAssetInputStream.available());
    152     }
    153 
    154     @Test
    155     public void testArgumentEdgeCases() throws Exception {
    156         // test read(byte[]): byte[] is null
    157         try {
    158             mAssetInputStream.read(null);
    159             fail("should throw NullPointerException ");
    160         } catch (NullPointerException e) {
    161             // expected
    162         }
    163 
    164         // test read(byte[], int, int): byte[] is null
    165         try {
    166             mAssetInputStream.read(null, 0, mAssetInputStream.available());
    167             fail("should throw NullPointerException ");
    168         } catch (NullPointerException e) {
    169             // expected
    170         }
    171 
    172         // test read(byte[]): byte[] is len 0
    173         final int previousAvailable = mAssetInputStream.available();
    174         assertEquals(0, mAssetInputStream.read(new byte[0]));
    175         assertEquals(previousAvailable, mAssetInputStream.available());
    176 
    177         // test read(byte[]): byte[] is len 0
    178         assertEquals(0, mAssetInputStream.read(new byte[0], 0, 0));
    179         assertEquals(previousAvailable, mAssetInputStream.available());
    180 
    181         // test read(byte[], int, int): offset is negative
    182         try {
    183             byte[] data = new byte[10];
    184             mAssetInputStream.read(data, -1, mAssetInputStream.available());
    185             fail("should throw IndexOutOfBoundsException ");
    186         } catch (IndexOutOfBoundsException e) {
    187             // expected
    188         }
    189 
    190         // test read(byte[], int, int): len + offset greater than data length
    191         try {
    192             byte[] data = new byte[10];
    193             assertEquals(0, mAssetInputStream.read(data, 0, data.length + 2));
    194             fail("should throw IndexOutOfBoundsException ");
    195         } catch (IndexOutOfBoundsException e) {
    196             // expected
    197         }
    198     }
    199 }
    200