blob: 94f87ffe55adc7304328627cdbbb2c4818624955 [file] [log] [blame]
Daichi Hirono8ba41912015-07-30 21:22:57 +09001/*
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
Daichi Hironof578fa22016-02-19 18:05:42 +090019import android.database.Cursor;
Tomasz Mikolajewskibb430fa2015-08-25 18:34:30 +090020import android.mtp.MtpObjectInfo;
Daichi Hirono8ba41912015-07-30 21:22:57 +090021import android.os.ParcelFileDescriptor;
Daichi Hironof578fa22016-02-19 18:05:42 +090022import android.provider.DocumentsContract.Document;
Daichi Hirono8ba41912015-07-30 21:22:57 +090023import android.test.AndroidTestCase;
Daichi Hirono4604b742015-11-12 12:12:48 +090024import android.test.suitebuilder.annotation.MediumTest;
Daichi Hirono8ba41912015-07-30 21:22:57 +090025
26import java.io.IOException;
27import java.util.concurrent.ExecutorService;
28import java.util.concurrent.Executors;
29import java.util.concurrent.TimeUnit;
30
Daichi Hirono4604b742015-11-12 12:12:48 +090031@MediumTest
Daichi Hirono8ba41912015-07-30 21:22:57 +090032public class PipeManagerTest extends AndroidTestCase {
Daichi Hirono3faa43a2015-08-05 17:15:35 +090033 private static final byte[] HELLO_BYTES = new byte[] { 'h', 'e', 'l', 'l', 'o' };
34
35 private TestMtpManager mtpManager;
Daichi Hirono84dfb052015-08-31 12:32:39 +090036 private ExecutorService mExecutor;
37 private PipeManager mPipeManager;
Daichi Hironof578fa22016-02-19 18:05:42 +090038 private MtpDatabase mDatabase;
Daichi Hirono3faa43a2015-08-05 17:15:35 +090039
40 @Override
41 public void setUp() {
42 mtpManager = new TestMtpManager(getContext());
Daichi Hirono84dfb052015-08-31 12:32:39 +090043 mExecutor = Executors.newSingleThreadExecutor();
Daichi Hironof578fa22016-02-19 18:05:42 +090044 mDatabase = new MtpDatabase(getContext(), MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY);
45 mPipeManager = new PipeManager(mDatabase, mExecutor);
Daichi Hirono3faa43a2015-08-05 17:15:35 +090046 }
47
Daichi Hirono8ba41912015-07-30 21:22:57 +090048 public void testReadDocument_basic() throws Exception {
Daichi Hirono3faa43a2015-08-05 17:15:35 +090049 mtpManager.setImportFileBytes(0, 1, HELLO_BYTES);
Daichi Hirono84dfb052015-08-31 12:32:39 +090050 final ParcelFileDescriptor descriptor = mPipeManager.readDocument(
Daichi Hirono6a5ea7e2016-02-02 16:35:03 +090051 mtpManager,
52 new Identifier(0, 0, 1, null, MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT));
Daichi Hirono3faa43a2015-08-05 17:15:35 +090053 assertDescriptor(descriptor, HELLO_BYTES);
54 }
55
56 public void testReadDocument_error() throws Exception {
Daichi Hirono6a5ea7e2016-02-02 16:35:03 +090057 final ParcelFileDescriptor descriptor = mPipeManager.readDocument(
58 mtpManager,
59 new Identifier(0, 0, 1, null, MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT));
Daichi Hirono3faa43a2015-08-05 17:15:35 +090060 assertDescriptorError(descriptor);
61 }
62
Tomasz Mikolajewskib80a3cf2015-08-24 16:10:51 +090063 public void testWriteDocument_basic() throws Exception {
Daichi Hironof578fa22016-02-19 18:05:42 +090064 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 Mikolajewskib80a3cf2015-08-24 16:10:51 +090072 // Create a placeholder file which should be replaced by a real file later.
Daichi Hironof578fa22016-02-19 18:05:42 +090073 mtpManager.setObjectInfo(0, info);
Tomasz Mikolajewskib80a3cf2015-08-24 16:10:51 +090074
75 // Upload testing bytes.
Tomasz Mikolajewski3edb4202015-08-31 13:25:59 +090076 final ParcelFileDescriptor descriptor = mPipeManager.writeDocument(
Daichi Hirono6a5ea7e2016-02-02 16:35:03 +090077 getContext(),
78 mtpManager,
Daichi Hironof578fa22016-02-19 18:05:42 +090079 new Identifier(0, 0, 1, "2", MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT));
Tomasz Mikolajewskib80a3cf2015-08-24 16:10:51 +090080 final ParcelFileDescriptor.AutoCloseOutputStream outputStream =
81 new ParcelFileDescriptor.AutoCloseOutputStream(descriptor);
82 outputStream.write(HELLO_BYTES, 0, HELLO_BYTES.length);
83 outputStream.close();
Daichi Hironof578fa22016-02-19 18:05:42 +090084 mExecutor.shutdown();
85 assertTrue(mExecutor.awaitTermination(1000, TimeUnit.MILLISECONDS));
Tomasz Mikolajewskib80a3cf2015-08-24 16:10:51 +090086
87 // Check if the placeholder file is removed.
88 try {
Daichi Hironof578fa22016-02-19 18:05:42 +090089 mtpManager.getObjectInfo(0, 1);
Tomasz Mikolajewskib80a3cf2015-08-24 16:10:51 +090090 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 Mikolajewskibb430fa2015-08-25 18:34:30 +090096 final MtpObjectInfo targetDocument = mtpManager.getObjectInfo(
Tomasz Mikolajewskib80a3cf2015-08-24 16:10:51 +090097 0, TestMtpManager.CREATED_DOCUMENT_HANDLE);
98 assertTrue(targetDocument != null);
99
Daichi Hironof578fa22016-02-19 18:05:42 +0900100 // 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 Mikolajewskib80a3cf2015-08-24 16:10:51 +0900108 // 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 Hirono3faa43a2015-08-05 17:15:35 +0900117 public void testReadThumbnail_basic() throws Exception {
118 mtpManager.setThumbnail(0, 1, HELLO_BYTES);
Daichi Hirono84dfb052015-08-31 12:32:39 +0900119 final ParcelFileDescriptor descriptor = mPipeManager.readThumbnail(
Daichi Hirono6a5ea7e2016-02-02 16:35:03 +0900120 mtpManager,
121 new Identifier(0, 0, 1, null, MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT));
Daichi Hirono3faa43a2015-08-05 17:15:35 +0900122 assertDescriptor(descriptor, HELLO_BYTES);
123 }
124
125 public void testReadThumbnail_error() throws Exception {
Daichi Hirono6a5ea7e2016-02-02 16:35:03 +0900126 final ParcelFileDescriptor descriptor = mPipeManager.readThumbnail(
127 mtpManager,
128 new Identifier(0, 0, 1, null, MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT));
Daichi Hirono3faa43a2015-08-05 17:15:35 +0900129 assertDescriptorError(descriptor);
130 }
131
132 private void assertDescriptor(ParcelFileDescriptor descriptor, byte[] expectedBytes)
133 throws IOException, InterruptedException {
Daichi Hironof578fa22016-02-19 18:05:42 +0900134 mExecutor.shutdown();
135 assertTrue(mExecutor.awaitTermination(1000, TimeUnit.MILLISECONDS));
Daichi Hirono8ba41912015-07-30 21:22:57 +0900136 try (final ParcelFileDescriptor.AutoCloseInputStream stream =
137 new ParcelFileDescriptor.AutoCloseInputStream(descriptor)) {
Daichi Hirono3faa43a2015-08-05 17:15:35 +0900138 byte[] results = new byte[100];
Tomasz Mikolajewski52652ac2015-08-05 17:33:33 +0900139 assertEquals(expectedBytes.length, stream.read(results));
140 for (int i = 0; i < expectedBytes.length; i++) {
Daichi Hirono8ba41912015-07-30 21:22:57 +0900141 assertEquals(expectedBytes[i], results[i]);
142 }
143 }
144 }
145
Daichi Hirono3faa43a2015-08-05 17:15:35 +0900146 private void assertDescriptorError(ParcelFileDescriptor descriptor)
147 throws InterruptedException {
Daichi Hironof578fa22016-02-19 18:05:42 +0900148 mExecutor.shutdown();
149 assertTrue(mExecutor.awaitTermination(1000, TimeUnit.MILLISECONDS));
Daichi Hirono8ba41912015-07-30 21:22:57 +0900150 try {
151 descriptor.checkError();
152 fail();
153 } catch (Throwable error) {
154 assertTrue(error instanceof IOException);
155 }
156 }
157}