Daichi Hirono | 9a05d6b | 2015-07-09 15:36:42 +0900 | [diff] [blame] | 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 | |
| 17 | package com.android.mtp; |
| 18 | |
Daichi Hirono | 0910956 | 2015-07-28 20:57:05 +0900 | [diff] [blame] | 19 | import android.database.Cursor; |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 20 | import android.mtp.MtpConstants; |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 21 | import android.mtp.MtpObjectInfo; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 22 | import android.net.Uri; |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 23 | import android.os.ParcelFileDescriptor; |
| 24 | import android.os.storage.StorageManager; |
Daichi Hirono | 4e94b8d | 2016-02-21 22:42:41 +0900 | [diff] [blame] | 25 | import android.provider.DocumentsContract.Document; |
Daichi Hirono | 0910956 | 2015-07-28 20:57:05 +0900 | [diff] [blame] | 26 | import android.provider.DocumentsContract.Root; |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 27 | import android.system.Os; |
| 28 | import android.system.OsConstants; |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 29 | import android.provider.DocumentsContract; |
Daichi Hirono | 9a05d6b | 2015-07-09 15:36:42 +0900 | [diff] [blame] | 30 | import android.test.AndroidTestCase; |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 31 | import android.test.suitebuilder.annotation.MediumTest; |
Daichi Hirono | 9a05d6b | 2015-07-09 15:36:42 +0900 | [diff] [blame] | 32 | |
Daichi Hirono | c18f807 | 2016-02-10 14:59:52 -0800 | [diff] [blame] | 33 | import com.android.mtp.exceptions.BusyDeviceException; |
| 34 | |
Daichi Hirono | 5fecc6c | 2015-08-04 17:45:51 +0900 | [diff] [blame] | 35 | import java.io.FileNotFoundException; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 36 | import java.io.IOException; |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 37 | import java.util.Arrays; |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 38 | import java.util.concurrent.TimeoutException; |
| 39 | |
Daichi Hirono | 259ce80 | 2015-11-20 17:51:53 +0900 | [diff] [blame] | 40 | import static com.android.mtp.MtpDatabase.strings; |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 41 | import static com.android.mtp.TestUtil.OPERATIONS_SUPPORTED; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 42 | |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 43 | @MediumTest |
Daichi Hirono | 9a05d6b | 2015-07-09 15:36:42 +0900 | [diff] [blame] | 44 | public class MtpDocumentsProviderTest extends AndroidTestCase { |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 45 | private final static Uri ROOTS_URI = |
| 46 | DocumentsContract.buildRootsUri(MtpDocumentsProvider.AUTHORITY); |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 47 | private TestContentResolver mResolver; |
Daichi Hirono | beac218 | 2015-07-29 16:28:57 +0900 | [diff] [blame] | 48 | private MtpDocumentsProvider mProvider; |
Daichi Hirono | 6ee4a1c | 2015-07-31 10:30:05 +0900 | [diff] [blame] | 49 | private TestMtpManager mMtpManager; |
Daichi Hirono | b999b0c | 2015-10-27 16:29:18 +0900 | [diff] [blame] | 50 | private final TestResources mResources = new TestResources(); |
Daichi Hirono | dc47344 | 2015-11-13 15:42:28 +0900 | [diff] [blame] | 51 | private MtpDatabase mDatabase; |
Daichi Hirono | beac218 | 2015-07-29 16:28:57 +0900 | [diff] [blame] | 52 | |
| 53 | @Override |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 54 | public void setUp() throws IOException { |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 55 | mResolver = new TestContentResolver(); |
Daichi Hirono | 6ee4a1c | 2015-07-31 10:30:05 +0900 | [diff] [blame] | 56 | mMtpManager = new TestMtpManager(getContext()); |
Daichi Hirono | dc47344 | 2015-11-13 15:42:28 +0900 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void tearDown() { |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 61 | mProvider.shutdown(); |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 62 | MtpDatabase.deleteDatabase(getContext()); |
Daichi Hirono | beac218 | 2015-07-29 16:28:57 +0900 | [diff] [blame] | 63 | } |
| 64 | |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 65 | public void testOpenAndCloseDevice() throws Exception { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 66 | setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 67 | mMtpManager.addValidDevice(new MtpDeviceRecord( |
| 68 | 0, |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 69 | "Device A", |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 70 | null /* deviceKey */, |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 71 | false /* unopened */, |
| 72 | new MtpRoot[] { |
| 73 | new MtpRoot( |
| 74 | 0 /* deviceId */, |
| 75 | 1 /* storageId */, |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 76 | "Storage A" /* volume description */, |
| 77 | 1024 /* free space */, |
| 78 | 2048 /* total space */, |
| 79 | "" /* no volume identifier */) |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 80 | }, |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 81 | OPERATIONS_SUPPORTED, |
Daichi Hirono | 148954a | 2016-01-21 19:55:45 +0900 | [diff] [blame] | 82 | null)); |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 83 | |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 84 | mProvider.resumeRootScanner(); |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 85 | mResolver.waitForNotification(ROOTS_URI, 1); |
Daichi Hirono | 0910956 | 2015-07-28 20:57:05 +0900 | [diff] [blame] | 86 | |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 87 | mProvider.openDevice(0); |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 88 | mResolver.waitForNotification(ROOTS_URI, 2); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 89 | |
| 90 | mProvider.closeDevice(0); |
| 91 | mResolver.waitForNotification(ROOTS_URI, 3); |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 92 | } |
| 93 | |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 94 | public void testOpenAndCloseErrorDevice() throws Exception { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 95 | setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 96 | try { |
| 97 | mProvider.openDevice(1); |
| 98 | fail(); |
| 99 | } catch (Throwable error) { |
| 100 | assertTrue(error instanceof IOException); |
| 101 | } |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 102 | assertEquals(0, mProvider.getOpenedDeviceRecordsCache().length); |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 103 | |
| 104 | // Check if the following notification is the first one or not. |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 105 | mMtpManager.addValidDevice(new MtpDeviceRecord( |
| 106 | 0, |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 107 | "Device A", |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 108 | null /* deviceKey */, |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 109 | false /* unopened */, |
| 110 | new MtpRoot[] { |
| 111 | new MtpRoot( |
| 112 | 0 /* deviceId */, |
| 113 | 1 /* storageId */, |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 114 | "Storage A" /* volume description */, |
| 115 | 1024 /* free space */, |
| 116 | 2048 /* total space */, |
| 117 | "" /* no volume identifier */) |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 118 | }, |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 119 | OPERATIONS_SUPPORTED, |
Daichi Hirono | 148954a | 2016-01-21 19:55:45 +0900 | [diff] [blame] | 120 | null)); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 121 | mProvider.resumeRootScanner(); |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 122 | mResolver.waitForNotification(ROOTS_URI, 1); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 123 | mProvider.openDevice(0); |
| 124 | mResolver.waitForNotification(ROOTS_URI, 2); |
| 125 | } |
| 126 | |
| 127 | public void testOpenDeviceOnDemand() throws Exception { |
| 128 | setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
| 129 | mMtpManager.addValidDevice(new MtpDeviceRecord( |
| 130 | 0, |
| 131 | "Device A", |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 132 | null /* deviceKey */, |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 133 | false /* unopened */, |
| 134 | new MtpRoot[] { |
| 135 | new MtpRoot( |
| 136 | 0 /* deviceId */, |
| 137 | 1 /* storageId */, |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 138 | "Storage A" /* volume description */, |
| 139 | 1024 /* free space */, |
| 140 | 2048 /* total space */, |
| 141 | "" /* no volume identifier */) |
| 142 | }, |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 143 | OPERATIONS_SUPPORTED, |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 144 | null)); |
| 145 | mMtpManager.setObjectHandles(0, 1, -1, new int[0]); |
| 146 | mProvider.resumeRootScanner(); |
| 147 | mResolver.waitForNotification(ROOTS_URI, 1); |
| 148 | final String[] columns = new String[] { |
| 149 | DocumentsContract.Root.COLUMN_TITLE, |
| 150 | DocumentsContract.Root.COLUMN_DOCUMENT_ID |
| 151 | }; |
| 152 | try (final Cursor cursor = mProvider.queryRoots(columns)) { |
| 153 | assertEquals(1, cursor.getCount()); |
| 154 | assertTrue(cursor.moveToNext()); |
| 155 | assertEquals("Device A", cursor.getString(0)); |
| 156 | assertEquals(1, cursor.getLong(1)); |
| 157 | } |
| 158 | { |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 159 | final MtpDeviceRecord[] openedDevice = mProvider.getOpenedDeviceRecordsCache(); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 160 | assertEquals(0, openedDevice.length); |
| 161 | } |
| 162 | // Device is opened automatically when querying its children. |
| 163 | try (final Cursor cursor = mProvider.queryChildDocuments("1", null, null)) {} |
| 164 | |
| 165 | { |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 166 | final MtpDeviceRecord[] openedDevice = mProvider.getOpenedDeviceRecordsCache(); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 167 | assertEquals(1, openedDevice.length); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 168 | assertEquals(0, openedDevice[0].deviceId); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 169 | } |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 170 | } |
| 171 | |
Daichi Hirono | 0910956 | 2015-07-28 20:57:05 +0900 | [diff] [blame] | 172 | public void testQueryRoots() throws Exception { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 173 | setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 174 | mMtpManager.addValidDevice(new MtpDeviceRecord( |
| 175 | 0, |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 176 | "Device A", |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 177 | "Device key A", |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 178 | false /* unopened */, |
| 179 | new MtpRoot[] { |
| 180 | new MtpRoot( |
| 181 | 0 /* deviceId */, |
| 182 | 1 /* storageId */, |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 183 | "Storage A" /* volume description */, |
| 184 | 1024 /* free space */, |
| 185 | 2048 /* total space */, |
| 186 | "" /* no volume identifier */) |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 187 | }, |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 188 | OPERATIONS_SUPPORTED, |
Daichi Hirono | 148954a | 2016-01-21 19:55:45 +0900 | [diff] [blame] | 189 | null)); |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 190 | mMtpManager.addValidDevice(new MtpDeviceRecord( |
| 191 | 1, |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 192 | "Device B", |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 193 | "Device key B", |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 194 | false /* unopened */, |
| 195 | new MtpRoot[] { |
| 196 | new MtpRoot( |
| 197 | 1 /* deviceId */, |
| 198 | 1 /* storageId */, |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 199 | "Storage B" /* volume description */, |
| 200 | 2048 /* free space */, |
| 201 | 4096 /* total space */, |
| 202 | "Identifier B" /* no volume identifier */) |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 203 | }, |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 204 | new int[0] /* No operations supported */, |
Daichi Hirono | 148954a | 2016-01-21 19:55:45 +0900 | [diff] [blame] | 205 | null)); |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 206 | |
Daichi Hirono | 0910956 | 2015-07-28 20:57:05 +0900 | [diff] [blame] | 207 | { |
Daichi Hirono | beac218 | 2015-07-29 16:28:57 +0900 | [diff] [blame] | 208 | mProvider.openDevice(0); |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 209 | mResolver.waitForNotification(ROOTS_URI, 1); |
Daichi Hirono | beac218 | 2015-07-29 16:28:57 +0900 | [diff] [blame] | 210 | final Cursor cursor = mProvider.queryRoots(null); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 211 | assertEquals(2, cursor.getCount()); |
Daichi Hirono | 0910956 | 2015-07-28 20:57:05 +0900 | [diff] [blame] | 212 | cursor.moveToNext(); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 213 | assertEquals("1", cursor.getString(0)); |
Tomasz Mikolajewski | c0834cd | 2015-08-26 12:31:45 +0900 | [diff] [blame] | 214 | assertEquals(Root.FLAG_SUPPORTS_IS_CHILD | Root.FLAG_SUPPORTS_CREATE, cursor.getInt(1)); |
Daichi Hirono | 39795da | 2015-12-02 10:56:44 +0900 | [diff] [blame] | 215 | assertEquals(R.drawable.ic_root_mtp, cursor.getInt(2)); |
Daichi Hirono | 17c8d8b | 2015-10-12 11:28:46 -0700 | [diff] [blame] | 216 | assertEquals("Device A Storage A", cursor.getString(3)); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 217 | assertEquals("1", cursor.getString(4)); |
Daichi Hirono | 0910956 | 2015-07-28 20:57:05 +0900 | [diff] [blame] | 218 | assertEquals(1024, cursor.getInt(5)); |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 219 | } |
| 220 | |
Daichi Hirono | 0910956 | 2015-07-28 20:57:05 +0900 | [diff] [blame] | 221 | { |
Daichi Hirono | beac218 | 2015-07-29 16:28:57 +0900 | [diff] [blame] | 222 | mProvider.openDevice(1); |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 223 | mResolver.waitForNotification(ROOTS_URI, 2); |
Daichi Hirono | beac218 | 2015-07-29 16:28:57 +0900 | [diff] [blame] | 224 | final Cursor cursor = mProvider.queryRoots(null); |
Daichi Hirono | 0910956 | 2015-07-28 20:57:05 +0900 | [diff] [blame] | 225 | assertEquals(2, cursor.getCount()); |
| 226 | cursor.moveToNext(); |
| 227 | cursor.moveToNext(); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 228 | assertEquals("2", cursor.getString(0)); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 229 | assertEquals(Root.FLAG_SUPPORTS_IS_CHILD, cursor.getInt(1)); |
Daichi Hirono | 39795da | 2015-12-02 10:56:44 +0900 | [diff] [blame] | 230 | assertEquals(R.drawable.ic_root_mtp, cursor.getInt(2)); |
Daichi Hirono | 17c8d8b | 2015-10-12 11:28:46 -0700 | [diff] [blame] | 231 | assertEquals("Device B Storage B", cursor.getString(3)); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 232 | assertEquals("2", cursor.getString(4)); |
Daichi Hirono | 0910956 | 2015-07-28 20:57:05 +0900 | [diff] [blame] | 233 | assertEquals(2048, cursor.getInt(5)); |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 234 | } |
Daichi Hirono | 0910956 | 2015-07-28 20:57:05 +0900 | [diff] [blame] | 235 | } |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 236 | |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 237 | public void testQueryRoots_error() throws Exception { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 238 | setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 239 | mMtpManager.addValidDevice(new MtpDeviceRecord( |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 240 | 0, |
| 241 | "Device A", |
| 242 | "Device key A", |
| 243 | false /* unopened */, |
| 244 | new MtpRoot[0], |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 245 | OPERATIONS_SUPPORTED, |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 246 | null)); |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 247 | mMtpManager.addValidDevice(new MtpDeviceRecord( |
| 248 | 1, |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 249 | "Device B", |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 250 | "Device key B", |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 251 | false /* unopened */, |
| 252 | new MtpRoot[] { |
| 253 | new MtpRoot( |
| 254 | 1 /* deviceId */, |
| 255 | 1 /* storageId */, |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 256 | "Storage B" /* volume description */, |
| 257 | 2048 /* free space */, |
| 258 | 4096 /* total space */, |
| 259 | "Identifier B" /* no volume identifier */) |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 260 | }, |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 261 | OPERATIONS_SUPPORTED, |
Daichi Hirono | 148954a | 2016-01-21 19:55:45 +0900 | [diff] [blame] | 262 | null)); |
Daichi Hirono | 0910956 | 2015-07-28 20:57:05 +0900 | [diff] [blame] | 263 | { |
Daichi Hirono | beac218 | 2015-07-29 16:28:57 +0900 | [diff] [blame] | 264 | mProvider.openDevice(0); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 265 | mProvider.resumeRootScanner(); |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 266 | mResolver.waitForNotification(ROOTS_URI, 1); |
| 267 | |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 268 | mProvider.openDevice(1); |
| 269 | mProvider.resumeRootScanner(); |
| 270 | mResolver.waitForNotification(ROOTS_URI, 2); |
| 271 | |
Daichi Hirono | beac218 | 2015-07-29 16:28:57 +0900 | [diff] [blame] | 272 | final Cursor cursor = mProvider.queryRoots(null); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 273 | assertEquals(2, cursor.getCount()); |
| 274 | |
| 275 | cursor.moveToNext(); |
| 276 | assertEquals("1", cursor.getString(0)); |
| 277 | assertEquals(Root.FLAG_SUPPORTS_IS_CHILD | Root.FLAG_SUPPORTS_CREATE, cursor.getInt(1)); |
| 278 | assertEquals(R.drawable.ic_root_mtp, cursor.getInt(2)); |
| 279 | assertEquals("Device A", cursor.getString(3)); |
| 280 | assertEquals("1", cursor.getString(4)); |
| 281 | assertEquals(0, cursor.getInt(5)); |
| 282 | |
Daichi Hirono | 0910956 | 2015-07-28 20:57:05 +0900 | [diff] [blame] | 283 | cursor.moveToNext(); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 284 | assertEquals("2", cursor.getString(0)); |
Tomasz Mikolajewski | c0834cd | 2015-08-26 12:31:45 +0900 | [diff] [blame] | 285 | assertEquals(Root.FLAG_SUPPORTS_IS_CHILD | Root.FLAG_SUPPORTS_CREATE, cursor.getInt(1)); |
Daichi Hirono | 39795da | 2015-12-02 10:56:44 +0900 | [diff] [blame] | 286 | assertEquals(R.drawable.ic_root_mtp, cursor.getInt(2)); |
Daichi Hirono | 17c8d8b | 2015-10-12 11:28:46 -0700 | [diff] [blame] | 287 | assertEquals("Device B Storage B", cursor.getString(3)); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 288 | assertEquals("2", cursor.getString(4)); |
Daichi Hirono | 0910956 | 2015-07-28 20:57:05 +0900 | [diff] [blame] | 289 | assertEquals(2048, cursor.getInt(5)); |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 293 | public void testQueryDocument() throws IOException, InterruptedException, TimeoutException { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 294 | setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 295 | setupRoots(0, new MtpRoot[] { new MtpRoot(0, 0, "Storage", 1000, 1000, "") }); |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 296 | setupDocuments( |
| 297 | 0, |
| 298 | 0, |
| 299 | MtpManager.OBJECT_HANDLE_ROOT_CHILDREN, |
| 300 | "1", |
| 301 | new MtpObjectInfo[] { |
| 302 | new MtpObjectInfo.Builder() |
| 303 | .setObjectHandle(100) |
| 304 | .setFormat(MtpConstants.FORMAT_EXIF_JPEG) |
| 305 | .setName("image.jpg") |
| 306 | .setDateModified(1422716400000L) |
| 307 | .setCompressedSize(1024 * 1024 * 5) |
| 308 | .setThumbCompressedSize(50 * 1024) |
| 309 | .build() |
| 310 | }); |
| 311 | |
Daichi Hirono | b3fe72b | 2015-12-15 07:45:06 +0000 | [diff] [blame] | 312 | final Cursor cursor = mProvider.queryDocument("3", null); |
Daichi Hirono | e5323b7 | 2015-07-29 16:10:47 +0900 | [diff] [blame] | 313 | assertEquals(1, cursor.getCount()); |
| 314 | |
| 315 | cursor.moveToNext(); |
Daichi Hirono | 47eb192 | 2015-11-16 13:01:31 +0900 | [diff] [blame] | 316 | |
Daichi Hirono | b3fe72b | 2015-12-15 07:45:06 +0000 | [diff] [blame] | 317 | assertEquals("3", cursor.getString(0)); |
Daichi Hirono | e5323b7 | 2015-07-29 16:10:47 +0900 | [diff] [blame] | 318 | assertEquals("image/jpeg", cursor.getString(1)); |
| 319 | assertEquals("image.jpg", cursor.getString(2)); |
| 320 | assertEquals(1422716400000L, cursor.getLong(3)); |
| 321 | assertEquals( |
| 322 | DocumentsContract.Document.FLAG_SUPPORTS_DELETE | |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 323 | DocumentsContract.Document.FLAG_SUPPORTS_WRITE | |
Daichi Hirono | e5323b7 | 2015-07-29 16:10:47 +0900 | [diff] [blame] | 324 | DocumentsContract.Document.FLAG_SUPPORTS_THUMBNAIL, |
| 325 | cursor.getInt(4)); |
| 326 | assertEquals(1024 * 1024 * 5, cursor.getInt(5)); |
| 327 | } |
| 328 | |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 329 | public void testQueryDocument_directory() |
| 330 | throws IOException, InterruptedException, TimeoutException { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 331 | setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 332 | setupRoots(0, new MtpRoot[] { new MtpRoot(0, 0, "Storage", 1000, 1000, "") }); |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 333 | setupDocuments( |
| 334 | 0, |
| 335 | 0, |
| 336 | MtpManager.OBJECT_HANDLE_ROOT_CHILDREN, |
| 337 | "1", |
| 338 | new MtpObjectInfo[] { |
| 339 | new MtpObjectInfo.Builder() |
| 340 | .setObjectHandle(2) |
| 341 | .setStorageId(1) |
| 342 | .setFormat(MtpConstants.FORMAT_ASSOCIATION) |
| 343 | .setName("directory") |
| 344 | .setDateModified(1422716400000L) |
| 345 | .build() |
| 346 | }); |
| 347 | |
Daichi Hirono | b3fe72b | 2015-12-15 07:45:06 +0000 | [diff] [blame] | 348 | final Cursor cursor = mProvider.queryDocument("3", null); |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 349 | assertEquals(1, cursor.getCount()); |
| 350 | |
| 351 | cursor.moveToNext(); |
Daichi Hirono | b3fe72b | 2015-12-15 07:45:06 +0000 | [diff] [blame] | 352 | assertEquals("3", cursor.getString(0)); |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 353 | assertEquals(DocumentsContract.Document.MIME_TYPE_DIR, cursor.getString(1)); |
| 354 | assertEquals("directory", cursor.getString(2)); |
| 355 | assertEquals(1422716400000L, cursor.getLong(3)); |
| 356 | assertEquals( |
| 357 | DocumentsContract.Document.FLAG_SUPPORTS_DELETE | |
| 358 | DocumentsContract.Document.FLAG_SUPPORTS_WRITE | |
| 359 | DocumentsContract.Document.FLAG_DIR_SUPPORTS_CREATE, |
| 360 | cursor.getInt(4)); |
| 361 | assertEquals(0, cursor.getInt(5)); |
| 362 | } |
| 363 | |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 364 | public void testQueryDocument_forRoot() |
| 365 | throws IOException, InterruptedException, TimeoutException { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 366 | setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 367 | setupRoots(0, new MtpRoot[] { |
Daichi Hirono | e5323b7 | 2015-07-29 16:10:47 +0900 | [diff] [blame] | 368 | new MtpRoot( |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 369 | 0 /* deviceId */, |
Daichi Hirono | e5323b7 | 2015-07-29 16:10:47 +0900 | [diff] [blame] | 370 | 1 /* storageId */, |
| 371 | "Storage A" /* volume description */, |
| 372 | 1024 /* free space */, |
| 373 | 4096 /* total space */, |
| 374 | "" /* no volume identifier */) |
| 375 | }); |
Daichi Hirono | b3fe72b | 2015-12-15 07:45:06 +0000 | [diff] [blame] | 376 | final Cursor cursor = mProvider.queryDocument("2", null); |
Daichi Hirono | e5323b7 | 2015-07-29 16:10:47 +0900 | [diff] [blame] | 377 | assertEquals(1, cursor.getCount()); |
| 378 | |
| 379 | cursor.moveToNext(); |
Daichi Hirono | b3fe72b | 2015-12-15 07:45:06 +0000 | [diff] [blame] | 380 | assertEquals("2", cursor.getString(0)); |
Daichi Hirono | e5323b7 | 2015-07-29 16:10:47 +0900 | [diff] [blame] | 381 | assertEquals(DocumentsContract.Document.MIME_TYPE_DIR, cursor.getString(1)); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 382 | assertEquals("Storage A", cursor.getString(2)); |
Daichi Hirono | e5323b7 | 2015-07-29 16:10:47 +0900 | [diff] [blame] | 383 | assertTrue(cursor.isNull(3)); |
| 384 | assertEquals(0, cursor.getInt(4)); |
| 385 | assertEquals(3072, cursor.getInt(5)); |
| 386 | } |
| 387 | |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 388 | public void testQueryChildDocuments() throws Exception { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 389 | setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 390 | setupRoots(0, new MtpRoot[] { new MtpRoot(0, 0, "Storage", 1000, 1000, "") }); |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 391 | setupDocuments( |
| 392 | 0, |
| 393 | 0, |
| 394 | MtpManager.OBJECT_HANDLE_ROOT_CHILDREN, |
| 395 | "1", |
| 396 | new MtpObjectInfo[] { |
| 397 | new MtpObjectInfo.Builder() |
| 398 | .setObjectHandle(100) |
| 399 | .setFormat(MtpConstants.FORMAT_EXIF_JPEG) |
| 400 | .setName("image.jpg") |
| 401 | .setCompressedSize(1024 * 1024 * 5) |
| 402 | .setThumbCompressedSize(5 * 1024) |
| 403 | .setProtectionStatus(MtpConstants.PROTECTION_STATUS_READ_ONLY) |
| 404 | .build() |
| 405 | }); |
Daichi Hirono | 47eb192 | 2015-11-16 13:01:31 +0900 | [diff] [blame] | 406 | |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 407 | final Cursor cursor = mProvider.queryChildDocuments("1", null, null); |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 408 | assertEquals(1, cursor.getCount()); |
| 409 | |
| 410 | assertTrue(cursor.moveToNext()); |
Daichi Hirono | b3fe72b | 2015-12-15 07:45:06 +0000 | [diff] [blame] | 411 | assertEquals("3", cursor.getString(0)); |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 412 | assertEquals("image/jpeg", cursor.getString(1)); |
| 413 | assertEquals("image.jpg", cursor.getString(2)); |
| 414 | assertEquals(0, cursor.getLong(3)); |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 415 | assertEquals(Document.FLAG_SUPPORTS_THUMBNAIL, cursor.getInt(4)); |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 416 | assertEquals(1024 * 1024 * 5, cursor.getInt(5)); |
| 417 | |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 418 | cursor.close(); |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | public void testQueryChildDocuments_cursorError() throws Exception { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 422 | setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 423 | try { |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 424 | mProvider.queryChildDocuments("1", null, null); |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 425 | fail(); |
| 426 | } catch (Throwable error) { |
| 427 | assertTrue(error instanceof FileNotFoundException); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | public void testQueryChildDocuments_documentError() throws Exception { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 432 | setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 433 | setupRoots(0, new MtpRoot[] { new MtpRoot(0, 0, "Storage", 1000, 1000, "") }); |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 434 | mMtpManager.setObjectHandles(0, 0, -1, new int[] { 1 }); |
| 435 | try { |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 436 | mProvider.queryChildDocuments("1", null, null); |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 437 | fail(); |
| 438 | } catch (Throwable error) { |
| 439 | assertTrue(error instanceof FileNotFoundException); |
| 440 | } |
| 441 | } |
| 442 | |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 443 | public void testDeleteDocument() throws IOException, InterruptedException, TimeoutException { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 444 | setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 445 | setupRoots(0, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 446 | new MtpRoot(0, 0, "Storage", 0, 0, "") |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 447 | }); |
| 448 | setupDocuments(0, 0, MtpManager.OBJECT_HANDLE_ROOT_CHILDREN, "1", new MtpObjectInfo[] { |
| 449 | new MtpObjectInfo.Builder() |
| 450 | .setName("test.txt") |
| 451 | .setObjectHandle(1) |
| 452 | .setParent(-1) |
| 453 | .build() |
| 454 | }); |
| 455 | |
Daichi Hirono | b3fe72b | 2015-12-15 07:45:06 +0000 | [diff] [blame] | 456 | mProvider.deleteDocument("3"); |
Daichi Hirono | 5fecc6c | 2015-08-04 17:45:51 +0900 | [diff] [blame] | 457 | assertEquals(1, mResolver.getChangeCount( |
| 458 | DocumentsContract.buildChildDocumentsUri( |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 459 | MtpDocumentsProvider.AUTHORITY, "1"))); |
Daichi Hirono | 5fecc6c | 2015-08-04 17:45:51 +0900 | [diff] [blame] | 460 | } |
| 461 | |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 462 | public void testDeleteDocument_error() |
| 463 | throws IOException, InterruptedException, TimeoutException { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 464 | setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 465 | setupRoots(0, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 466 | new MtpRoot(0, 0, "Storage", 0, 0, "") |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 467 | }); |
| 468 | setupDocuments(0, 0, MtpManager.OBJECT_HANDLE_ROOT_CHILDREN, "1", new MtpObjectInfo[] { |
| 469 | new MtpObjectInfo.Builder() |
| 470 | .setName("test.txt") |
| 471 | .setObjectHandle(1) |
| 472 | .setParent(-1) |
| 473 | .build() |
| 474 | }); |
Daichi Hirono | 5fecc6c | 2015-08-04 17:45:51 +0900 | [diff] [blame] | 475 | try { |
Daichi Hirono | b3fe72b | 2015-12-15 07:45:06 +0000 | [diff] [blame] | 476 | mProvider.deleteDocument("4"); |
Daichi Hirono | 5fecc6c | 2015-08-04 17:45:51 +0900 | [diff] [blame] | 477 | fail(); |
| 478 | } catch (Throwable e) { |
| 479 | assertTrue(e instanceof IOException); |
| 480 | } |
| 481 | assertEquals(0, mResolver.getChangeCount( |
| 482 | DocumentsContract.buildChildDocumentsUri( |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 483 | MtpDocumentsProvider.AUTHORITY, "1"))); |
| 484 | } |
| 485 | |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 486 | public void testOpenDocument() throws Exception { |
| 487 | setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
| 488 | setupRoots(0, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 489 | new MtpRoot(0, 0, "Storage", 0, 0, "") |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 490 | }); |
| 491 | final byte[] bytes = "Hello world".getBytes(); |
| 492 | setupDocuments(0, 0, MtpManager.OBJECT_HANDLE_ROOT_CHILDREN, "1", new MtpObjectInfo[] { |
| 493 | new MtpObjectInfo.Builder() |
| 494 | .setName("test.txt") |
| 495 | .setObjectHandle(1) |
| 496 | .setCompressedSize(bytes.length) |
| 497 | .setParent(-1) |
| 498 | .build() |
| 499 | }); |
| 500 | mMtpManager.setImportFileBytes(0, 1, bytes); |
| 501 | try (final ParcelFileDescriptor fd = mProvider.openDocument("3", "r", null)) { |
| 502 | final byte[] readBytes = new byte[5]; |
| 503 | assertEquals(6, Os.lseek(fd.getFileDescriptor(), 6, OsConstants.SEEK_SET)); |
| 504 | assertEquals(5, Os.read(fd.getFileDescriptor(), readBytes, 0, 5)); |
| 505 | assertTrue(Arrays.equals("world".getBytes(), readBytes)); |
| 506 | |
| 507 | assertEquals(0, Os.lseek(fd.getFileDescriptor(), 0, OsConstants.SEEK_SET)); |
| 508 | assertEquals(5, Os.read(fd.getFileDescriptor(), readBytes, 0, 5)); |
| 509 | assertTrue(Arrays.equals("Hello".getBytes(), readBytes)); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | public void testOpenDocument_shortBytes() throws Exception { |
| 514 | mMtpManager = new TestMtpManager(getContext()) { |
| 515 | @Override |
| 516 | MtpObjectInfo getObjectInfo(int deviceId, int objectHandle) throws IOException { |
| 517 | if (objectHandle == 1) { |
| 518 | return new MtpObjectInfo.Builder(super.getObjectInfo(deviceId, objectHandle)) |
| 519 | .setObjectHandle(1).setCompressedSize(1024 * 1024).build(); |
| 520 | } |
| 521 | |
| 522 | return super.getObjectInfo(deviceId, objectHandle); |
| 523 | } |
| 524 | }; |
| 525 | setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
| 526 | setupRoots(0, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 527 | new MtpRoot(0, 0, "Storage", 0, 0, "") |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 528 | }); |
| 529 | final byte[] bytes = "Hello world".getBytes(); |
| 530 | setupDocuments(0, 0, MtpManager.OBJECT_HANDLE_ROOT_CHILDREN, "1", new MtpObjectInfo[] { |
| 531 | new MtpObjectInfo.Builder() |
| 532 | .setName("test.txt") |
| 533 | .setObjectHandle(1) |
| 534 | .setCompressedSize(bytes.length) |
| 535 | .setParent(-1) |
| 536 | .build() |
| 537 | }); |
| 538 | mMtpManager.setImportFileBytes(0, 1, bytes); |
| 539 | try (final ParcelFileDescriptor fd = mProvider.openDocument("3", "r", null)) { |
| 540 | final byte[] readBytes = new byte[1024 * 1024]; |
| 541 | assertEquals(11, Os.read(fd.getFileDescriptor(), readBytes, 0, readBytes.length)); |
| 542 | } |
| 543 | } |
| 544 | |
Daichi Hirono | c18f807 | 2016-02-10 14:59:52 -0800 | [diff] [blame] | 545 | public void testBusyDevice() throws Exception { |
| 546 | mMtpManager = new TestMtpManager(getContext()) { |
| 547 | @Override |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 548 | MtpDeviceRecord openDevice(int deviceId) throws IOException { |
Daichi Hirono | c18f807 | 2016-02-10 14:59:52 -0800 | [diff] [blame] | 549 | throw new BusyDeviceException(); |
| 550 | } |
| 551 | }; |
| 552 | setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
| 553 | mMtpManager.addValidDevice(new MtpDeviceRecord( |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 554 | 0, "Device A", null /* deviceKey */, false /* unopened */, new MtpRoot[0], |
| 555 | OPERATIONS_SUPPORTED, null)); |
Daichi Hirono | c18f807 | 2016-02-10 14:59:52 -0800 | [diff] [blame] | 556 | |
| 557 | mProvider.resumeRootScanner(); |
| 558 | mResolver.waitForNotification(ROOTS_URI, 1); |
| 559 | |
| 560 | try (final Cursor cursor = mProvider.queryRoots(null)) { |
| 561 | assertEquals(1, cursor.getCount()); |
| 562 | } |
| 563 | |
| 564 | try (final Cursor cursor = mProvider.queryChildDocuments("1", null, null)) { |
| 565 | assertEquals(0, cursor.getCount()); |
| 566 | assertEquals( |
| 567 | "error_busy_device", |
| 568 | cursor.getExtras().getString(DocumentsContract.EXTRA_ERROR)); |
| 569 | } |
| 570 | } |
| 571 | |
Daichi Hirono | 2965776 | 2016-02-10 16:55:37 -0800 | [diff] [blame] | 572 | public void testLockedDevice() throws Exception { |
| 573 | setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
| 574 | mMtpManager.addValidDevice(new MtpDeviceRecord( |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 575 | 0, "Device A", null, false /* unopened */, new MtpRoot[0], OPERATIONS_SUPPORTED, |
| 576 | null)); |
Daichi Hirono | 2965776 | 2016-02-10 16:55:37 -0800 | [diff] [blame] | 577 | |
| 578 | mProvider.resumeRootScanner(); |
| 579 | mResolver.waitForNotification(ROOTS_URI, 1); |
| 580 | |
| 581 | try (final Cursor cursor = mProvider.queryRoots(null)) { |
| 582 | assertEquals(1, cursor.getCount()); |
| 583 | } |
| 584 | |
| 585 | try (final Cursor cursor = mProvider.queryChildDocuments("1", null, null)) { |
| 586 | assertEquals(0, cursor.getCount()); |
| 587 | assertEquals( |
| 588 | "error_locked_device", |
| 589 | cursor.getExtras().getString(DocumentsContract.EXTRA_ERROR)); |
| 590 | } |
| 591 | } |
| 592 | |
Daichi Hirono | 4e94b8d | 2016-02-21 22:42:41 +0900 | [diff] [blame] | 593 | public void testMappingDisconnectedDocuments() throws Exception { |
| 594 | setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
| 595 | mMtpManager.addValidDevice(new MtpDeviceRecord( |
| 596 | 0, |
| 597 | "Device A", |
| 598 | "device key", |
| 599 | true /* unopened */, |
| 600 | new MtpRoot[] { |
| 601 | new MtpRoot( |
| 602 | 0 /* deviceId */, |
| 603 | 1 /* storageId */, |
| 604 | "Storage A" /* volume description */, |
| 605 | 1024 /* free space */, |
| 606 | 2048 /* total space */, |
| 607 | "" /* no volume identifier */) |
| 608 | }, |
| 609 | null, |
| 610 | null)); |
| 611 | |
| 612 | final String[] names = strings("Directory A", "Directory B", "Directory C"); |
| 613 | final int objectHandleOffset = 100; |
| 614 | for (int i = 0; i < names.length; i++) { |
| 615 | final int parentHandle = i == 0 ? |
| 616 | MtpManager.OBJECT_HANDLE_ROOT_CHILDREN : objectHandleOffset + i - 1; |
| 617 | final int objectHandle = i + objectHandleOffset; |
| 618 | mMtpManager.setObjectHandles(0, 1, parentHandle, new int[] { objectHandle }); |
| 619 | mMtpManager.setObjectInfo( |
| 620 | 0, |
| 621 | new MtpObjectInfo.Builder() |
| 622 | .setName(names[i]) |
| 623 | .setObjectHandle(objectHandle) |
| 624 | .setFormat(MtpConstants.FORMAT_ASSOCIATION) |
| 625 | .setStorageId(1) |
| 626 | .build()); |
| 627 | } |
| 628 | |
| 629 | mProvider.resumeRootScanner(); |
| 630 | mResolver.waitForNotification(ROOTS_URI, 1); |
| 631 | |
| 632 | final int documentIdOffset = 2; |
| 633 | for (int i = 0; i < names.length; i++) { |
| 634 | try (final Cursor cursor = mProvider.queryChildDocuments( |
| 635 | String.valueOf(documentIdOffset + i), |
| 636 | strings(Document.COLUMN_DOCUMENT_ID, Document.COLUMN_DISPLAY_NAME), |
| 637 | null)) { |
| 638 | assertEquals(1, cursor.getCount()); |
| 639 | cursor.moveToNext(); |
| 640 | assertEquals(String.valueOf(documentIdOffset + i + 1), cursor.getString(0)); |
| 641 | assertEquals(names[i], cursor.getString(1)); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | mProvider.closeDevice(0); |
| 646 | mResolver.waitForNotification(ROOTS_URI, 2); |
| 647 | |
| 648 | mProvider.openDevice(0); |
| 649 | mResolver.waitForNotification(ROOTS_URI, 3); |
| 650 | |
| 651 | for (int i = 0; i < names.length; i++) { |
| 652 | mResolver.waitForNotification(DocumentsContract.buildChildDocumentsUri( |
| 653 | MtpDocumentsProvider.AUTHORITY, |
| 654 | String.valueOf(documentIdOffset + i)), 1); |
| 655 | try (final Cursor cursor = mProvider.queryChildDocuments( |
| 656 | String.valueOf(documentIdOffset + i), |
| 657 | strings(Document.COLUMN_DOCUMENT_ID), |
| 658 | null)) { |
| 659 | assertEquals(1, cursor.getCount()); |
| 660 | cursor.moveToNext(); |
| 661 | assertEquals(String.valueOf(documentIdOffset + i + 1), cursor.getString(0)); |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 666 | public void testCreateDocument_noWritingSupport() throws Exception { |
| 667 | setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
| 668 | mMtpManager.addValidDevice(new MtpDeviceRecord( |
| 669 | 0, "Device A", null /* deviceKey */, false /* unopened */, |
| 670 | new MtpRoot[] { |
| 671 | new MtpRoot( |
| 672 | 0 /* deviceId */, |
| 673 | 1 /* storageId */, |
| 674 | "Storage A" /* volume description */, |
| 675 | 1024 /* free space */, |
| 676 | 2048 /* total space */, |
| 677 | "" /* no volume identifier */) |
| 678 | }, |
| 679 | new int[0] /* no operations supported */, null)); |
| 680 | mProvider.resumeRootScanner(); |
| 681 | mResolver.waitForNotification(ROOTS_URI, 1); |
| 682 | try { |
| 683 | mProvider.createDocument("1", "text/palin", "note.txt"); |
| 684 | fail(); |
| 685 | } catch (UnsupportedOperationException exception) {} |
| 686 | } |
| 687 | |
| 688 | public void testOpenDocument_noWritingSupport() throws Exception { |
| 689 | setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
| 690 | mMtpManager.addValidDevice(new MtpDeviceRecord( |
| 691 | 0, "Device A", null /* deviceKey */, false /* unopened */, |
| 692 | new MtpRoot[] { |
| 693 | new MtpRoot( |
| 694 | 0 /* deviceId */, |
| 695 | 1 /* storageId */, |
| 696 | "Storage A" /* volume description */, |
| 697 | 1024 /* free space */, |
| 698 | 2048 /* total space */, |
| 699 | "" /* no volume identifier */) |
| 700 | }, |
| 701 | new int[0] /* no operations supported */, null)); |
| 702 | mMtpManager.setObjectHandles( |
| 703 | 0, 1, MtpManager.OBJECT_HANDLE_ROOT_CHILDREN, new int[] { 100 }); |
| 704 | mMtpManager.setObjectInfo( |
| 705 | 0, new MtpObjectInfo.Builder().setObjectHandle(100).setName("note.txt").build()); |
| 706 | mProvider.resumeRootScanner(); |
| 707 | mResolver.waitForNotification(ROOTS_URI, 1); |
| 708 | try (final Cursor cursor = mProvider.queryChildDocuments( |
| 709 | "1", strings(Document.COLUMN_DOCUMENT_ID), null)) { |
| 710 | assertEquals(1, cursor.getCount()); |
| 711 | cursor.moveToNext(); |
| 712 | assertEquals("3", cursor.getString(0)); |
| 713 | } |
| 714 | try { |
| 715 | mProvider.openDocument("3", "w", null); |
| 716 | fail(); |
| 717 | } catch (UnsupportedOperationException exception) {} |
| 718 | } |
| 719 | |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 720 | private void setupProvider(int flag) { |
| 721 | mDatabase = new MtpDatabase(getContext(), flag); |
| 722 | mProvider = new MtpDocumentsProvider(); |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 723 | final StorageManager storageManager = getContext().getSystemService(StorageManager.class); |
| 724 | assertTrue(mProvider.onCreateForTesting( |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 725 | mResources, |
| 726 | mMtpManager, |
| 727 | mResolver, |
| 728 | mDatabase, |
| 729 | storageManager, |
| 730 | new TestServiceIntentSender())); |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 731 | } |
| 732 | |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 733 | private String[] getStrings(Cursor cursor) { |
| 734 | try { |
| 735 | final String[] results = new String[cursor.getCount()]; |
| 736 | for (int i = 0; cursor.moveToNext(); i++) { |
| 737 | results[i] = cursor.getString(0); |
| 738 | } |
| 739 | return results; |
| 740 | } finally { |
| 741 | cursor.close(); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | private String[] setupRoots(int deviceId, MtpRoot[] roots) |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 746 | throws InterruptedException, TimeoutException, IOException { |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 747 | final int changeCount = mResolver.getChangeCount(ROOTS_URI); |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 748 | mMtpManager.addValidDevice( |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 749 | new MtpDeviceRecord(deviceId, "Device", null /* deviceKey */, false /* unopened */, |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 750 | roots, OPERATIONS_SUPPORTED, null)); |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 751 | mProvider.openDevice(deviceId); |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 752 | mResolver.waitForNotification(ROOTS_URI, changeCount + 1); |
| 753 | return getStrings(mProvider.queryRoots(strings(DocumentsContract.Root.COLUMN_ROOT_ID))); |
| 754 | } |
| 755 | |
| 756 | private String[] setupDocuments( |
| 757 | int deviceId, |
| 758 | int storageId, |
| 759 | int parentHandle, |
| 760 | String parentDocumentId, |
| 761 | MtpObjectInfo[] objects) throws FileNotFoundException { |
| 762 | final int[] handles = new int[objects.length]; |
| 763 | int i = 0; |
| 764 | for (final MtpObjectInfo info : objects) { |
| 765 | handles[i] = info.getObjectHandle(); |
| 766 | mMtpManager.setObjectInfo(deviceId, info); |
| 767 | } |
| 768 | mMtpManager.setObjectHandles(deviceId, storageId, parentHandle, handles); |
| 769 | return getStrings(mProvider.queryChildDocuments( |
| 770 | parentDocumentId, strings(DocumentsContract.Document.COLUMN_DOCUMENT_ID), null)); |
Daichi Hirono | 5fecc6c | 2015-08-04 17:45:51 +0900 | [diff] [blame] | 771 | } |
Daichi Hirono | 9a05d6b | 2015-07-09 15:36:42 +0900 | [diff] [blame] | 772 | } |