Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +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 | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 19 | import android.database.Cursor; |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 20 | import android.mtp.MtpObjectInfo; |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 21 | import android.os.ParcelFileDescriptor; |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 22 | import android.provider.DocumentsContract.Document; |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 23 | import android.test.AndroidTestCase; |
Daichi Hirono | 4604b74 | 2015-11-12 12:12:48 +0900 | [diff] [blame] | 24 | import android.test.suitebuilder.annotation.MediumTest; |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 25 | |
| 26 | import java.io.IOException; |
| 27 | import java.util.concurrent.ExecutorService; |
| 28 | import java.util.concurrent.Executors; |
| 29 | import java.util.concurrent.TimeUnit; |
| 30 | |
Daichi Hirono | 4604b74 | 2015-11-12 12:12:48 +0900 | [diff] [blame] | 31 | @MediumTest |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 32 | public class PipeManagerTest extends AndroidTestCase { |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 33 | private static final byte[] HELLO_BYTES = new byte[] { 'h', 'e', 'l', 'l', 'o' }; |
| 34 | |
| 35 | private TestMtpManager mtpManager; |
Daichi Hirono | 84dfb05 | 2015-08-31 12:32:39 +0900 | [diff] [blame] | 36 | private ExecutorService mExecutor; |
| 37 | private PipeManager mPipeManager; |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 38 | private MtpDatabase mDatabase; |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 39 | |
| 40 | @Override |
| 41 | public void setUp() { |
| 42 | mtpManager = new TestMtpManager(getContext()); |
Daichi Hirono | 84dfb05 | 2015-08-31 12:32:39 +0900 | [diff] [blame] | 43 | mExecutor = Executors.newSingleThreadExecutor(); |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 44 | mDatabase = new MtpDatabase(getContext(), MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
| 45 | mPipeManager = new PipeManager(mDatabase, mExecutor); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 46 | } |
| 47 | |
Daichi Hirono | 24ab92a | 2016-03-04 17:53:03 +0900 | [diff] [blame] | 48 | @Override |
| 49 | protected void tearDown() throws Exception { |
| 50 | assertTrue(mPipeManager.close()); |
Daichi Hirono | acb0e27 | 2016-03-14 21:49:14 +0900 | [diff] [blame] | 51 | mDatabase.close(); |
Daichi Hirono | 24ab92a | 2016-03-04 17:53:03 +0900 | [diff] [blame] | 52 | } |
| 53 | |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 54 | public void testReadDocument_basic() throws Exception { |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 55 | mtpManager.setImportFileBytes(0, 1, HELLO_BYTES); |
Daichi Hirono | 84dfb05 | 2015-08-31 12:32:39 +0900 | [diff] [blame] | 56 | final ParcelFileDescriptor descriptor = mPipeManager.readDocument( |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 57 | mtpManager, |
| 58 | new Identifier(0, 0, 1, null, MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT)); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 59 | assertDescriptor(descriptor, HELLO_BYTES); |
| 60 | } |
| 61 | |
| 62 | public void testReadDocument_error() throws Exception { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 63 | final ParcelFileDescriptor descriptor = mPipeManager.readDocument( |
| 64 | mtpManager, |
| 65 | new Identifier(0, 0, 1, null, MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT)); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 66 | assertDescriptorError(descriptor); |
| 67 | } |
| 68 | |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 69 | public void testWriteDocument_basic() throws Exception { |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 70 | TestUtil.addTestDevice(mDatabase); |
| 71 | TestUtil.addTestStorage(mDatabase, "1"); |
| 72 | |
| 73 | final MtpObjectInfo info = |
| 74 | new MtpObjectInfo.Builder().setObjectHandle(1).setName("note.txt").build(); |
| 75 | mDatabase.getMapper().startAddingDocuments("2"); |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 76 | mDatabase.getMapper().putChildDocuments( |
| 77 | 0, "2", TestUtil.OPERATIONS_SUPPORTED, |
Daichi Hirono | 64111e0 | 2016-03-24 21:07:38 +0900 | [diff] [blame] | 78 | new MtpObjectInfo[] { info }, |
| 79 | new long[] { 0L }); |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 80 | mDatabase.getMapper().stopAddingDocuments("2"); |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 81 | // Create a placeholder file which should be replaced by a real file later. |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 82 | mtpManager.setObjectInfo(0, info); |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 83 | |
| 84 | // Upload testing bytes. |
Tomasz Mikolajewski | 3edb420 | 2015-08-31 13:25:59 +0900 | [diff] [blame] | 85 | final ParcelFileDescriptor descriptor = mPipeManager.writeDocument( |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 86 | getContext(), |
| 87 | mtpManager, |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 88 | new Identifier(0, 0, 1, "2", MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT), |
| 89 | TestUtil.OPERATIONS_SUPPORTED); |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 90 | final ParcelFileDescriptor.AutoCloseOutputStream outputStream = |
| 91 | new ParcelFileDescriptor.AutoCloseOutputStream(descriptor); |
| 92 | outputStream.write(HELLO_BYTES, 0, HELLO_BYTES.length); |
| 93 | outputStream.close(); |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 94 | mExecutor.shutdown(); |
| 95 | assertTrue(mExecutor.awaitTermination(1000, TimeUnit.MILLISECONDS)); |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 96 | |
| 97 | // Check if the placeholder file is removed. |
| 98 | try { |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 99 | mtpManager.getObjectInfo(0, 1); |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 100 | fail(); // The placeholder file has not been deleted. |
| 101 | } catch (IOException e) { |
| 102 | // Expected error, as the file is gone. |
| 103 | } |
| 104 | |
| 105 | // Confirm that the target file is created. |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 106 | final MtpObjectInfo targetDocument = mtpManager.getObjectInfo( |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 107 | 0, TestMtpManager.CREATED_DOCUMENT_HANDLE); |
| 108 | assertTrue(targetDocument != null); |
| 109 | |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 110 | // Confirm the object handle is updated. |
| 111 | try (final Cursor cursor = mDatabase.queryDocument( |
| 112 | "2", new String[] { MtpDatabaseConstants.COLUMN_OBJECT_HANDLE })) { |
| 113 | assertEquals(1, cursor.getCount()); |
| 114 | cursor.moveToNext(); |
| 115 | assertEquals(TestMtpManager.CREATED_DOCUMENT_HANDLE, cursor.getInt(0)); |
| 116 | } |
| 117 | |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 118 | // Verify uploaded bytes. |
| 119 | final byte[] uploadedBytes = mtpManager.getImportFileBytes( |
| 120 | 0, TestMtpManager.CREATED_DOCUMENT_HANDLE); |
| 121 | assertEquals(HELLO_BYTES.length, uploadedBytes.length); |
| 122 | for (int i = 0; i < HELLO_BYTES.length; i++) { |
| 123 | assertEquals(HELLO_BYTES[i], uploadedBytes[i]); |
| 124 | } |
| 125 | } |
| 126 | |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 127 | public void testReadThumbnail_basic() throws Exception { |
| 128 | mtpManager.setThumbnail(0, 1, HELLO_BYTES); |
Daichi Hirono | 84dfb05 | 2015-08-31 12:32:39 +0900 | [diff] [blame] | 129 | final ParcelFileDescriptor descriptor = mPipeManager.readThumbnail( |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 130 | mtpManager, |
| 131 | new Identifier(0, 0, 1, null, MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT)); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 132 | assertDescriptor(descriptor, HELLO_BYTES); |
| 133 | } |
| 134 | |
| 135 | public void testReadThumbnail_error() throws Exception { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 136 | final ParcelFileDescriptor descriptor = mPipeManager.readThumbnail( |
| 137 | mtpManager, |
| 138 | new Identifier(0, 0, 1, null, MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT)); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 139 | assertDescriptorError(descriptor); |
| 140 | } |
| 141 | |
| 142 | private void assertDescriptor(ParcelFileDescriptor descriptor, byte[] expectedBytes) |
| 143 | throws IOException, InterruptedException { |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 144 | mExecutor.shutdown(); |
| 145 | assertTrue(mExecutor.awaitTermination(1000, TimeUnit.MILLISECONDS)); |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 146 | try (final ParcelFileDescriptor.AutoCloseInputStream stream = |
| 147 | new ParcelFileDescriptor.AutoCloseInputStream(descriptor)) { |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 148 | byte[] results = new byte[100]; |
Tomasz Mikolajewski | 52652ac | 2015-08-05 17:33:33 +0900 | [diff] [blame] | 149 | assertEquals(expectedBytes.length, stream.read(results)); |
| 150 | for (int i = 0; i < expectedBytes.length; i++) { |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 151 | assertEquals(expectedBytes[i], results[i]); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 156 | private void assertDescriptorError(ParcelFileDescriptor descriptor) |
| 157 | throws InterruptedException { |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 158 | mExecutor.shutdown(); |
| 159 | assertTrue(mExecutor.awaitTermination(1000, TimeUnit.MILLISECONDS)); |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 160 | try { |
| 161 | descriptor.checkError(); |
| 162 | fail(); |
| 163 | } catch (Throwable error) { |
| 164 | assertTrue(error instanceof IOException); |
| 165 | } |
| 166 | } |
| 167 | } |