OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / frameworks / base / core / tests / coretests / src / android / content / MemoryFileProviderTest.java
1 /*
2  * Copyright (C) 2009 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;
18
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;
24
25 import java.io.InputStream;
26 import java.util.Arrays;
27
28 /**
29  * Tests reading a MemoryFile-based AssestFile from a ContentProvider running
30  * in a different process.
31  */
32 public class MemoryFileProviderTest extends AndroidTestCase {
33
34     // reads from a cross-process AssetFileDescriptor for a MemoryFile
35     @MediumTest
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);
41         assertNotNull(in);
42         int count = in.read(buf);
43         assertEquals(buf.length, count);
44         assertEquals(-1, in.read());
45         in.close();
46         assertTrue(Arrays.equals(MemoryFileProvider.TEST_BLOB, buf));
47     }
48
49     // tests that we don't leak file descriptors or virtual address space
50     @LargeTest
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
54         // or address space
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));
64             in.close();
65         }
66     }
67
68     // tests that we haven't broken AssestFileDescriptors for normal files.
69     @SmallTest
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);
75         assertNotNull(in);
76         int count = in.read(buf);
77         assertEquals(buf.length, count);
78         assertEquals(-1, in.read());
79         in.close();
80         assertTrue(Arrays.equals(MemoryFileProvider.TEST_BLOB, buf));
81     }
82
83 }