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 com.android.cts.content.R; 19 20 import org.xmlpull.v1.XmlPullParser; 21 import org.xmlpull.v1.XmlPullParserException; 22 23 import android.content.cts.util.XmlUtils; 24 import android.content.res.AssetFileDescriptor; 25 import android.content.res.AssetManager; 26 import android.content.res.Resources; 27 import android.content.res.XmlResourceParser; 28 import android.test.AndroidTestCase; 29 import android.util.TypedValue; 30 31 import java.io.BufferedReader; 32 import java.io.IOException; 33 import java.io.InputStream; 34 import java.io.InputStreamReader; 35 36 37 public class AssetManagerTest extends AndroidTestCase{ 38 private AssetManager mAssets; 39 40 @Override 41 protected void setUp() throws Exception { 42 super.setUp(); 43 mAssets = mContext.getAssets(); 44 } 45 46 public void testAssetOperations() throws IOException, XmlPullParserException { 47 final Resources res = getContext().getResources(); 48 final TypedValue value = new TypedValue(); 49 res.getValue(R.raw.text, value, true); 50 final String fileName = "text.txt"; 51 InputStream inputStream = mAssets.open(fileName); 52 assertNotNull(inputStream); 53 final String expect = "OneTwoThreeFourFiveSixSevenEightNineTen"; 54 assertContextEquals(expect, inputStream); 55 inputStream = mAssets.open(fileName, AssetManager.ACCESS_BUFFER); 56 assertNotNull(inputStream); 57 assertContextEquals(expect, inputStream); 58 59 AssetFileDescriptor assetFileDes = mAssets.openFd(fileName); 60 assertNotNull(assetFileDes); 61 assertContextEquals(expect, assetFileDes.createInputStream()); 62 assetFileDes = mAssets.openNonAssetFd(value.string.toString()); 63 assertNotNull(assetFileDes); 64 assertContextEquals(expect, assetFileDes.createInputStream()); 65 assetFileDes = mAssets.openNonAssetFd(value.assetCookie, value.string.toString()); 66 assertNotNull(assetFileDes); 67 assertContextEquals(expect, assetFileDes.createInputStream()); 68 69 XmlResourceParser parser = mAssets.openXmlResourceParser("AndroidManifest.xml"); 70 assertNotNull(parser); 71 XmlUtils.beginDocument(parser, "manifest"); 72 parser = mAssets.openXmlResourceParser(0, "AndroidManifest.xml"); 73 assertNotNull(parser); 74 beginDocument(parser, "manifest"); 75 76 String[] files = mAssets.list(""); 77 boolean result = false; 78 for (int i = 0; i < files.length; i++) { 79 if (files[i].equals(fileName)) { 80 result = true; 81 break; 82 } 83 } 84 assertTrue(result); 85 86 try { 87 mAssets.open("notExistFile.txt", AssetManager.ACCESS_BUFFER); 88 fail("test open(String, int) failed"); 89 } catch (IOException e) { 90 // expected 91 } 92 93 try { 94 mAssets.openFd("notExistFile.txt"); 95 fail("test openFd(String) failed"); 96 } catch (IOException e) { 97 // expected 98 } 99 100 try { 101 mAssets.openNonAssetFd(0, "notExistFile.txt"); 102 fail("test openNonAssetFd(int, String) failed"); 103 } catch (IOException e) { 104 // expected 105 } 106 107 try { 108 mAssets.openXmlResourceParser(0, "notExistFile.txt"); 109 fail("test openXmlResourceParser(int, String) failed"); 110 } catch (IOException e) { 111 // expected 112 } 113 114 assertNotNull(mAssets.getLocales()); 115 116 } 117 118 private void assertContextEquals(final String expect, final InputStream inputStream) 119 throws IOException { 120 final BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream)); 121 final String result = bf.readLine(); 122 inputStream.close(); 123 assertNotNull(result); 124 assertEquals(expect, result); 125 } 126 127 private void beginDocument(final XmlPullParser parser,final String firstElementName) 128 throws XmlPullParserException, IOException { 129 int type; 130 while ((type = parser.next()) != XmlPullParser.START_TAG) { 131 } 132 if (type != XmlPullParser.START_TAG) { 133 fail("No start tag found"); 134 } 135 assertEquals(firstElementName, parser.getName()); 136 } 137 138 } 139