2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.content;
19 import android.net.Uri;
20 import android.test.AndroidTestCase;
21 import android.test.suitebuilder.annotation.LargeTest;
22 import android.test.suitebuilder.annotation.MediumTest;
23 import android.test.suitebuilder.annotation.SmallTest;
25 import java.io.InputStream;
26 import java.util.Arrays;
29 * Tests reading a MemoryFile-based AssestFile from a ContentProvider running
30 * in a different process.
32 public class MemoryFileProviderTest extends AndroidTestCase {
34 // reads from a cross-process AssetFileDescriptor for a MemoryFile
36 public void testRead() throws Exception {
37 ContentResolver resolver = getContext().getContentResolver();
38 Uri uri = Uri.parse("content://android.content.MemoryFileProvider/data/1/blob");
39 byte[] buf = new byte[MemoryFileProvider.TEST_BLOB.length];
40 InputStream in = resolver.openInputStream(uri);
42 int count = in.read(buf);
43 assertEquals(buf.length, count);
44 assertEquals(-1, in.read());
46 assertTrue(Arrays.equals(MemoryFileProvider.TEST_BLOB, buf));
49 // tests that we don't leak file descriptors or virtual address space
51 public void testClose() throws Exception {
52 ContentResolver resolver = getContext().getContentResolver();
53 // open enough file descriptors that we will crash something if we leak FDs
55 for (int i = 0; i < 1025; i++) {
56 Uri uri = Uri.parse("content://android.content.MemoryFileProvider/huge");
57 InputStream in = resolver.openInputStream(uri);
58 assertNotNull("Failed to open stream number " + i, in);
59 assertEquals(1000000, in.skip(1000000));
60 byte[] buf = new byte[MemoryFileProvider.TEST_BLOB.length];
61 int count = in.read(buf);
62 assertEquals(buf.length, count);
63 assertTrue(Arrays.equals(MemoryFileProvider.TEST_BLOB, buf));
68 // tests that we haven't broken AssestFileDescriptors for normal files.
70 public void testFile() throws Exception {
71 ContentResolver resolver = getContext().getContentResolver();
72 Uri uri = Uri.parse("content://android.content.MemoryFileProvider/file");
73 byte[] buf = new byte[MemoryFileProvider.TEST_BLOB.length];
74 InputStream in = resolver.openInputStream(uri);
76 int count = in.read(buf);
77 assertEquals(buf.length, count);
78 assertEquals(-1, in.read());
80 assertTrue(Arrays.equals(MemoryFileProvider.TEST_BLOB, buf));