TestMtpManager.java revision 52652ac7a5f479f7f5e24f78778203bd88c0c4f4
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
17package com.android.mtp;
18
19import android.content.Context;
20import android.os.ParcelFileDescriptor;
21
22import java.io.IOException;
23import java.util.Arrays;
24import java.util.HashMap;
25import java.util.HashSet;
26import java.util.Map;
27import java.util.Set;
28import java.util.TreeSet;
29
30public class TestMtpManager extends MtpManager {
31    private static String pack(int... args) {
32        return Arrays.toString(args);
33    }
34
35    private final Set<Integer> mValidDevices = new HashSet<Integer>();
36    private final Set<Integer> mOpenedDevices = new TreeSet<Integer>();
37    private final Map<Integer, MtpRoot[]> mRoots = new HashMap<Integer, MtpRoot[]>();
38    private final Map<String, MtpDocument> mDocuments = new HashMap<String, MtpDocument>();
39    private final Map<String, Integer> mParents = new HashMap<String, Integer>();
40    private final Map<String, byte[]> mImportFileBytes = new HashMap<String, byte[]>();
41
42    TestMtpManager(Context context) {
43        super(context);
44    }
45
46    void addValidDevice(int deviceId) {
47        mValidDevices.add(deviceId);
48    }
49
50    void setRoots(int deviceId, MtpRoot[] roots) {
51        mRoots.put(deviceId, roots);
52    }
53
54    void setDocument(int deviceId, int objectHandle, MtpDocument document) {
55        mDocuments.put(pack(deviceId, objectHandle), document);
56    }
57
58    void setImportFileBytes(int deviceId, int objectHandle, byte[] bytes) {
59        mImportFileBytes.put(pack(deviceId, objectHandle), bytes);
60    }
61
62    void setParent(int deviceId, int objectHandle, int parentObjectHandle) {
63        mParents.put(pack(deviceId, objectHandle), parentObjectHandle);
64    }
65
66    @Override
67    void openDevice(int deviceId) throws IOException {
68        if (!mValidDevices.contains(deviceId) || mOpenedDevices.contains(deviceId)) {
69            throw new IOException();
70        }
71        mOpenedDevices.add(deviceId);
72    }
73
74    @Override
75    void closeDevice(int deviceId) throws IOException {
76        if (!mValidDevices.contains(deviceId) || !mOpenedDevices.contains(deviceId)) {
77            throw new IOException();
78        }
79        mOpenedDevices.remove(deviceId);
80    }
81
82    @Override
83    MtpRoot[] getRoots(int deviceId) throws IOException {
84        if (mRoots.containsKey(deviceId)) {
85            return mRoots.get(deviceId);
86        } else {
87            throw new IOException("getRoots error");
88        }
89    }
90
91    @Override
92    MtpDocument getDocument(int deviceId, int objectHandle) {
93        return mDocuments.get(pack(deviceId, objectHandle));
94    }
95
96    @Override
97    void importFile(int deviceId, int storageId, ParcelFileDescriptor target) throws IOException {
98        final String key = pack(deviceId, storageId);
99        if (mImportFileBytes.containsKey(key)) {
100            try (final ParcelFileDescriptor.AutoCloseOutputStream outputStream =
101                    new ParcelFileDescriptor.AutoCloseOutputStream(target)) {
102                outputStream.write(mImportFileBytes.get(key));
103            }
104        } else {
105            throw new IOException("importFile error: " + key);
106        }
107    }
108
109    @Override
110    void deleteDocument(int deviceId, int objectHandle) throws IOException {
111        final String key = pack(deviceId, objectHandle);
112        if (mDocuments.containsKey(key)) {
113            mDocuments.remove(key);
114        } else {
115            throw new IOException();
116        }
117    }
118
119    @Override
120    synchronized int getParent(int deviceId, int objectHandle) throws IOException {
121        final String key = pack(deviceId, objectHandle);
122        if (mParents.containsKey(key)) {
123            return mParents.get(key);
124        } else {
125            throw new IOException();
126        }
127    }
128
129    @Override
130    int[] getOpenedDeviceIds() {
131        int i = 0;
132        final int[] result = new int[mOpenedDevices.size()];
133        for (int deviceId : mOpenedDevices) {
134            result[i++] = deviceId;
135        }
136        return result;
137    }
138}
139