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 | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 48 | public void testReadDocument_basic() throws Exception { |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 49 | mtpManager.setImportFileBytes(0, 1, HELLO_BYTES); |
Daichi Hirono | 84dfb05 | 2015-08-31 12:32:39 +0900 | [diff] [blame] | 50 | final ParcelFileDescriptor descriptor = mPipeManager.readDocument( |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 51 | mtpManager, |
| 52 | new Identifier(0, 0, 1, null, MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT)); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 53 | assertDescriptor(descriptor, HELLO_BYTES); |
| 54 | } |
| 55 | |
| 56 | public void testReadDocument_error() throws Exception { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 57 | final ParcelFileDescriptor descriptor = mPipeManager.readDocument( |
| 58 | mtpManager, |
| 59 | new Identifier(0, 0, 1, null, MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT)); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 60 | assertDescriptorError(descriptor); |
| 61 | } |
| 62 | |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 63 | public void testWriteDocument_basic() throws Exception { |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 64 | TestUtil.addTestDevice(mDatabase); |
| 65 | TestUtil.addTestStorage(mDatabase, "1"); |
| 66 | |
| 67 | final MtpObjectInfo info = |
| 68 | new MtpObjectInfo.Builder().setObjectHandle(1).setName("note.txt").build(); |
| 69 | mDatabase.getMapper().startAddingDocuments("2"); |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 70 | mDatabase.getMapper().putChildDocuments( |
| 71 | 0, "2", TestUtil.OPERATIONS_SUPPORTED, |
| 72 | new MtpObjectInfo[] { info }); |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 73 | mDatabase.getMapper().stopAddingDocuments("2"); |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 74 | // 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] | 75 | mtpManager.setObjectInfo(0, info); |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 76 | |
| 77 | // Upload testing bytes. |
Tomasz Mikolajewski | 3edb420 | 2015-08-31 13:25:59 +0900 | [diff] [blame] | 78 | final ParcelFileDescriptor descriptor = mPipeManager.writeDocument( |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 79 | getContext(), |
| 80 | mtpManager, |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 81 | new Identifier(0, 0, 1, "2", MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT), |
| 82 | TestUtil.OPERATIONS_SUPPORTED); |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 83 | final ParcelFileDescriptor.AutoCloseOutputStream outputStream = |
| 84 | new ParcelFileDescriptor.AutoCloseOutputStream(descriptor); |
| 85 | outputStream.write(HELLO_BYTES, 0, HELLO_BYTES.length); |
| 86 | outputStream.close(); |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 87 | mExecutor.shutdown(); |
| 88 | assertTrue(mExecutor.awaitTermination(1000, TimeUnit.MILLISECONDS)); |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 89 | |
| 90 | // Check if the placeholder file is removed. |
| 91 | try { |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 92 | mtpManager.getObjectInfo(0, 1); |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 93 | fail(); // The placeholder file has not been deleted. |
| 94 | } catch (IOException e) { |
| 95 | // Expected error, as the file is gone. |
| 96 | } |
| 97 | |
| 98 | // Confirm that the target file is created. |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 99 | final MtpObjectInfo targetDocument = mtpManager.getObjectInfo( |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 100 | 0, TestMtpManager.CREATED_DOCUMENT_HANDLE); |
| 101 | assertTrue(targetDocument != null); |
| 102 | |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 103 | // Confirm the object handle is updated. |
| 104 | try (final Cursor cursor = mDatabase.queryDocument( |
| 105 | "2", new String[] { MtpDatabaseConstants.COLUMN_OBJECT_HANDLE })) { |
| 106 | assertEquals(1, cursor.getCount()); |
| 107 | cursor.moveToNext(); |
| 108 | assertEquals(TestMtpManager.CREATED_DOCUMENT_HANDLE, cursor.getInt(0)); |
| 109 | } |
| 110 | |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 111 | // Verify uploaded bytes. |
| 112 | final byte[] uploadedBytes = mtpManager.getImportFileBytes( |
| 113 | 0, TestMtpManager.CREATED_DOCUMENT_HANDLE); |
| 114 | assertEquals(HELLO_BYTES.length, uploadedBytes.length); |
| 115 | for (int i = 0; i < HELLO_BYTES.length; i++) { |
| 116 | assertEquals(HELLO_BYTES[i], uploadedBytes[i]); |
| 117 | } |
| 118 | } |
| 119 | |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 120 | public void testReadThumbnail_basic() throws Exception { |
| 121 | mtpManager.setThumbnail(0, 1, HELLO_BYTES); |
Daichi Hirono | 84dfb05 | 2015-08-31 12:32:39 +0900 | [diff] [blame] | 122 | final ParcelFileDescriptor descriptor = mPipeManager.readThumbnail( |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 123 | mtpManager, |
| 124 | new Identifier(0, 0, 1, null, MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT)); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 125 | assertDescriptor(descriptor, HELLO_BYTES); |
| 126 | } |
| 127 | |
| 128 | public void testReadThumbnail_error() throws Exception { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 129 | final ParcelFileDescriptor descriptor = mPipeManager.readThumbnail( |
| 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 | assertDescriptorError(descriptor); |
| 133 | } |
| 134 | |
| 135 | private void assertDescriptor(ParcelFileDescriptor descriptor, byte[] expectedBytes) |
| 136 | throws IOException, InterruptedException { |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 137 | mExecutor.shutdown(); |
| 138 | assertTrue(mExecutor.awaitTermination(1000, TimeUnit.MILLISECONDS)); |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 139 | try (final ParcelFileDescriptor.AutoCloseInputStream stream = |
| 140 | new ParcelFileDescriptor.AutoCloseInputStream(descriptor)) { |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 141 | byte[] results = new byte[100]; |
Tomasz Mikolajewski | 52652ac | 2015-08-05 17:33:33 +0900 | [diff] [blame] | 142 | assertEquals(expectedBytes.length, stream.read(results)); |
| 143 | for (int i = 0; i < expectedBytes.length; i++) { |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 144 | assertEquals(expectedBytes[i], results[i]); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 149 | private void assertDescriptorError(ParcelFileDescriptor descriptor) |
| 150 | throws InterruptedException { |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 151 | mExecutor.shutdown(); |
| 152 | assertTrue(mExecutor.awaitTermination(1000, TimeUnit.MILLISECONDS)); |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 153 | try { |
| 154 | descriptor.checkError(); |
| 155 | fail(); |
| 156 | } catch (Throwable error) { |
| 157 | assertTrue(error instanceof IOException); |
| 158 | } |
| 159 | } |
| 160 | } |