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
17package android.content;
18
19import android.content.res.AssetManager;
20import android.test.AndroidTestCase;
21import android.test.suitebuilder.annotation.SmallTest;
22
23import java.io.IOException;
24import java.io.InputStream;
25
26public class AssetTest extends AndroidTestCase {
27    private AssetManager mAssets;
28
29    @Override
30    protected void setUp() throws Exception {
31        super.setUp();
32        mAssets = mContext.getAssets();
33    }
34
35    public static void verifyTextAsset(InputStream is) throws IOException {
36        String expectedString = "OneTwoThreeFourFiveSixSevenEightNineTen";
37        byte[] buffer = new byte[10];
38
39        int readCount;
40        int curIndex = 0;
41        while ((readCount = is.read(buffer, 0, buffer.length)) > 0) {
42            for (int i = 0; i < readCount; i++) {
43                assertEquals("At index " + curIndex
44                            + " expected " + expectedString.charAt(curIndex)
45                            + " but found " + ((char) buffer[i]),
46                        buffer[i], expectedString.charAt(curIndex));
47                curIndex++;
48            }
49        }
50
51        readCount = is.read(buffer, 0, buffer.length);
52        assertEquals("Reading end of buffer: expected readCount=-1 but got " + readCount,
53                -1, readCount);
54
55        readCount = is.read(buffer, buffer.length, 0);
56        assertEquals("Reading end of buffer length 0: expected readCount=0 but got " + readCount,
57                0, readCount);
58
59        is.close();
60    }
61
62    @SmallTest
63    public void testReadToEnd() throws Exception {
64        InputStream is = mAssets.open("text.txt");
65        verifyTextAsset(is);
66    }
67
68    // XXX failing
69    public void xxtestListDir() throws Exception {
70        String[] files = mAssets.list("");
71        assertEquals(1, files.length);
72        assertEquals("test.txt", files[0]);
73    }
74}
75