MtpDocumentsProviderTest.java revision 6baa16e9109046661fef8dcc25b8754ac68bcdae
1/*
2 * Copyright (C) 2015 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 com.android.mtp;
18
19import android.database.Cursor;
20import android.net.Uri;
21import android.provider.DocumentsContract;
22import android.provider.DocumentsContract.Root;
23import android.test.AndroidTestCase;
24import android.test.suitebuilder.annotation.SmallTest;
25
26import java.io.FileNotFoundException;
27import java.io.IOException;
28import java.util.Date;
29
30@SmallTest
31public class MtpDocumentsProviderTest extends AndroidTestCase {
32    private TestContentResolver mResolver;
33    private MtpDocumentsProvider mProvider;
34    private TestMtpManager mMtpManager;
35
36    @Override
37    public void setUp() {
38        mResolver = new TestContentResolver();
39        mMtpManager = new TestMtpManager(getContext());
40        mProvider = new MtpDocumentsProvider();
41        mProvider.onCreateForTesting(mMtpManager, mResolver);
42    }
43
44    public void testOpenAndCloseDevice() throws Exception {
45        final Uri uri = DocumentsContract.buildRootsUri(MtpDocumentsProvider.AUTHORITY);
46
47        mMtpManager.addValidDevice(0);
48        assertEquals(0, mResolver.getChangeCount(uri));
49
50        mProvider.openDevice(0);
51        assertEquals(1, mResolver.getChangeCount(uri));
52
53        mProvider.closeDevice(0);
54        assertEquals(2, mResolver.getChangeCount(uri));
55
56        int exceptionCounter = 0;
57        try {
58            mProvider.openDevice(1);
59        } catch (IOException error) {
60            exceptionCounter++;
61        }
62        assertEquals(2, mResolver.getChangeCount(uri));
63        try {
64            mProvider.closeDevice(1);
65        } catch (IOException error) {
66            exceptionCounter++;
67        }
68        assertEquals(2, mResolver.getChangeCount(uri));
69        assertEquals(2, exceptionCounter);
70    }
71
72    public void testCloseAllDevices() throws IOException {
73        final Uri uri = DocumentsContract.buildRootsUri(MtpDocumentsProvider.AUTHORITY);
74
75        mMtpManager.addValidDevice(0);
76
77        mProvider.closeAllDevices();
78        assertEquals(0, mResolver.getChangeCount(uri));
79
80        mProvider.openDevice(0);
81        assertEquals(1, mResolver.getChangeCount(uri));
82
83        mProvider.closeAllDevices();
84        assertEquals(2, mResolver.getChangeCount(uri));
85    }
86
87    public void testQueryRoots() throws Exception {
88        mMtpManager.addValidDevice(0);
89        mMtpManager.addValidDevice(1);
90        mMtpManager.setRoots(0, new MtpRoot[] {
91                new MtpRoot(
92                        1 /* storageId */,
93                        "Storage A" /* volume description */,
94                        1024 /* free space */,
95                        2048 /* total space */,
96                        "" /* no volume identifier */)
97        });
98        mMtpManager.setRoots(1, new MtpRoot[] {
99                new MtpRoot(
100                        1 /* storageId */,
101                        "Storage B" /* volume description */,
102                        2048 /* free space */,
103                        4096 /* total space */,
104                        "Identifier B" /* no volume identifier */)
105        });
106        assertEquals(0, mProvider.queryRoots(null).getCount());
107
108        {
109            mProvider.openDevice(0);
110            final Cursor cursor = mProvider.queryRoots(null);
111            assertEquals(1, cursor.getCount());
112            cursor.moveToNext();
113            assertEquals("0_1", cursor.getString(0));
114            assertEquals(Root.FLAG_SUPPORTS_IS_CHILD, cursor.getInt(1));
115            // TODO: Add storage icon for MTP devices.
116            assertTrue(cursor.isNull(2) /* icon */);
117            assertEquals("Storage A", cursor.getString(3));
118            assertEquals("0_1_0", cursor.getString(4));
119            assertEquals(1024, cursor.getInt(5));
120        }
121
122        {
123            mProvider.openDevice(1);
124            final Cursor cursor = mProvider.queryRoots(null);
125            assertEquals(2, cursor.getCount());
126            cursor.moveToNext();
127            cursor.moveToNext();
128            assertEquals("1_1", cursor.getString(0));
129            assertEquals(Root.FLAG_SUPPORTS_IS_CHILD, cursor.getInt(1));
130            // TODO: Add storage icon for MTP devices.
131            assertTrue(cursor.isNull(2) /* icon */);
132            assertEquals("Storage B", cursor.getString(3));
133            assertEquals("1_1_0", cursor.getString(4));
134            assertEquals(2048, cursor.getInt(5));
135        }
136
137        {
138            mProvider.closeAllDevices();
139            final Cursor cursor = mProvider.queryRoots(null);
140            assertEquals(0, cursor.getCount());
141        }
142    }
143
144    public void testQueryRoots_error() throws IOException {
145        mMtpManager.addValidDevice(0);
146        mMtpManager.addValidDevice(1);
147        // Not set roots for device 0 so that MtpManagerMock#getRoots throws IOException.
148        mMtpManager.setRoots(1, new MtpRoot[] {
149                new MtpRoot(
150                        1 /* storageId */,
151                        "Storage B" /* volume description */,
152                        2048 /* free space */,
153                        4096 /* total space */,
154                        "Identifier B" /* no volume identifier */)
155        });
156        {
157            mProvider.openDevice(0);
158            mProvider.openDevice(1);
159            final Cursor cursor = mProvider.queryRoots(null);
160            assertEquals(1, cursor.getCount());
161            cursor.moveToNext();
162            assertEquals("1_1", cursor.getString(0));
163            assertEquals(Root.FLAG_SUPPORTS_IS_CHILD, cursor.getInt(1));
164            // TODO: Add storage icon for MTP devices.
165            assertTrue(cursor.isNull(2) /* icon */);
166            assertEquals("Storage B", cursor.getString(3));
167            assertEquals("1_1_0", cursor.getString(4));
168            assertEquals(2048, cursor.getInt(5));
169        }
170    }
171
172    public void testQueryDocument() throws IOException {
173        mMtpManager.setDocument(0, 2, new MtpDocument(
174                2 /* object handle */,
175                0x3801 /* JPEG */,
176                "image.jpg" /* display name */,
177                new Date(1422716400000L) /* modified date */,
178                1024 * 1024 * 5 /* file size */,
179                1024 * 50 /* thumbnail size */));
180        final Cursor cursor = mProvider.queryDocument("0_1_2", null);
181        assertEquals(1, cursor.getCount());
182
183        cursor.moveToNext();
184        assertEquals("0_1_2", cursor.getString(0));
185        assertEquals("image/jpeg", cursor.getString(1));
186        assertEquals("image.jpg", cursor.getString(2));
187        assertEquals(1422716400000L, cursor.getLong(3));
188        assertEquals(
189                DocumentsContract.Document.FLAG_SUPPORTS_DELETE |
190                DocumentsContract.Document.FLAG_SUPPORTS_THUMBNAIL,
191                cursor.getInt(4));
192        assertEquals(1024 * 1024 * 5, cursor.getInt(5));
193    }
194
195    public void testQueryDocument_forRoot() throws IOException {
196        mMtpManager.setRoots(0, new MtpRoot[] {
197                new MtpRoot(
198                        1 /* storageId */,
199                        "Storage A" /* volume description */,
200                        1024 /* free space */,
201                        4096 /* total space */,
202                        "" /* no volume identifier */)
203        });
204        final Cursor cursor = mProvider.queryDocument("0_1_0", null);
205        assertEquals(1, cursor.getCount());
206
207        cursor.moveToNext();
208        assertEquals("0_1_0", cursor.getString(0));
209        assertEquals(DocumentsContract.Document.MIME_TYPE_DIR, cursor.getString(1));
210        assertEquals("Storage A", cursor.getString(2));
211        assertTrue(cursor.isNull(3));
212        assertEquals(0, cursor.getInt(4));
213        assertEquals(3072, cursor.getInt(5));
214    }
215
216    public void testQueryChildDocuments() throws Exception {
217        mMtpManager.setObjectHandles(0, 0, -1, new int[] { 1 });
218
219        mMtpManager.setDocument(0, 1, new MtpDocument(
220                1 /* object handle */,
221                0x3801 /* JPEG */,
222                "image.jpg" /* display name */,
223                new Date(0) /* modified date */,
224                1024 * 1024 * 5 /* file size */,
225                1024 * 50 /* thumbnail size */));
226
227        final Cursor cursor = mProvider.queryChildDocuments("0_0_0", null, null);
228        assertEquals(1, cursor.getCount());
229
230        assertTrue(cursor.moveToNext());
231        assertEquals("0_0_1", cursor.getString(0));
232        assertEquals("image/jpeg", cursor.getString(1));
233        assertEquals("image.jpg", cursor.getString(2));
234        assertEquals(0, cursor.getLong(3));
235        assertEquals(
236                DocumentsContract.Document.FLAG_SUPPORTS_DELETE |
237                DocumentsContract.Document.FLAG_SUPPORTS_THUMBNAIL,
238                cursor.getInt(4));
239        assertEquals(1024 * 1024 * 5, cursor.getInt(5));
240
241        assertFalse(cursor.moveToNext());
242    }
243
244    public void testQueryChildDocuments_cursorError() throws Exception {
245        try {
246            mProvider.queryChildDocuments("0_0_0", null, null);
247            fail();
248        } catch (Throwable error) {
249            assertTrue(error instanceof FileNotFoundException);
250        }
251    }
252
253    public void testQueryChildDocuments_documentError() throws Exception {
254        mMtpManager.setObjectHandles(0, 0, -1, new int[] { 1 });
255        try {
256            mProvider.queryChildDocuments("0_0_0", null, null);
257            fail();
258        } catch (Throwable error) {
259            assertTrue(error instanceof FileNotFoundException);
260        }
261    }
262
263    public void testDeleteDocument() throws FileNotFoundException {
264        mMtpManager.setDocument(0, 1, new MtpDocument(
265                1 /* object handle */,
266                0x3801 /* JPEG */,
267                "image.jpg" /* display name */,
268                new Date(1422716400000L) /* modified date */,
269                1024 * 1024 * 5 /* file size */,
270                1024 * 50 /* thumbnail size */));
271        mMtpManager.setParent(0, 1, 2);
272        mProvider.deleteDocument("0_0_1");
273        assertEquals(1, mResolver.getChangeCount(
274                DocumentsContract.buildChildDocumentsUri(
275                        MtpDocumentsProvider.AUTHORITY, "0_0_2")));
276    }
277
278    public void testDeleteDocument_error() {
279        mMtpManager.setParent(0, 1, 2);
280        try {
281            mProvider.deleteDocument("0_0_1");
282            fail();
283        } catch (Throwable e) {
284            assertTrue(e instanceof IOException);
285        }
286        assertEquals(0, mResolver.getChangeCount(
287                DocumentsContract.buildChildDocumentsUri(
288                        MtpDocumentsProvider.AUTHORITY, "0_0_2")));
289    }
290}
291