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