Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +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 | |
| 19 | import android.content.Context; |
| 20 | import android.database.Cursor; |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 21 | import android.mtp.MtpObjectInfo; |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 22 | import android.net.Uri; |
| 23 | import android.provider.DocumentsContract; |
| 24 | import android.test.AndroidTestCase; |
Daichi Hirono | 4604b74 | 2015-11-12 12:12:48 +0900 | [diff] [blame] | 25 | import android.test.suitebuilder.annotation.MediumTest; |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 26 | |
| 27 | import java.io.IOException; |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 28 | import java.util.HashMap; |
| 29 | import java.util.Map; |
| 30 | import java.util.concurrent.CountDownLatch; |
| 31 | |
Daichi Hirono | 4604b74 | 2015-11-12 12:12:48 +0900 | [diff] [blame] | 32 | @MediumTest |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 33 | public class DocumentLoaderTest extends AndroidTestCase { |
Daichi Hirono | 47eb192 | 2015-11-16 13:01:31 +0900 | [diff] [blame] | 34 | private MtpDatabase mDatabase; |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 35 | private BlockableTestMtpManager mManager; |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 36 | private TestContentResolver mResolver; |
| 37 | private DocumentLoader mLoader; |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 38 | final private Identifier mParentIdentifier = new Identifier( |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 39 | 0, 0, 0, "2", MtpDatabaseConstants.DOCUMENT_TYPE_STORAGE); |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 40 | |
| 41 | @Override |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 42 | public void setUp() throws Exception { |
Daichi Hirono | 47eb192 | 2015-11-16 13:01:31 +0900 | [diff] [blame] | 43 | mDatabase = new MtpDatabase(getContext(), MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 44 | |
| 45 | mDatabase.getMapper().startAddingDocuments(null); |
| 46 | mDatabase.getMapper().putDeviceDocument( |
Daichi Hirono | 4e94b8d | 2016-02-21 22:42:41 +0900 | [diff] [blame] | 47 | new MtpDeviceRecord(0, "Device", null, true, new MtpRoot[0], null, null)); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 48 | mDatabase.getMapper().stopAddingDocuments(null); |
| 49 | |
| 50 | mDatabase.getMapper().startAddingDocuments("1"); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 51 | mDatabase.getMapper().putStorageDocuments("1", new int[0], new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 52 | new MtpRoot(0, 0, "Storage", 1000, 1000, "") |
Daichi Hirono | 47eb192 | 2015-11-16 13:01:31 +0900 | [diff] [blame] | 53 | }); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 54 | mDatabase.getMapper().stopAddingDocuments("1"); |
| 55 | |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 56 | mManager = new BlockableTestMtpManager(getContext()); |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 57 | mResolver = new TestContentResolver(); |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 58 | mLoader = new DocumentLoader( |
| 59 | new MtpDeviceRecord( |
| 60 | 0, "Device", "Key", true, new MtpRoot[0], |
| 61 | TestUtil.OPERATIONS_SUPPORTED, new int[0]), |
| 62 | mManager, |
| 63 | mResolver, |
| 64 | mDatabase); |
Daichi Hirono | 47eb192 | 2015-11-16 13:01:31 +0900 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
Daichi Hirono | 4e94b8d | 2016-02-21 22:42:41 +0900 | [diff] [blame] | 68 | public void tearDown() throws Exception { |
| 69 | mLoader.close(); |
Daichi Hirono | 47eb192 | 2015-11-16 13:01:31 +0900 | [diff] [blame] | 70 | mDatabase.close(); |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 71 | } |
| 72 | |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 73 | public void testBasic() throws Exception { |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 74 | final Uri uri = DocumentsContract.buildChildDocumentsUri( |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 75 | MtpDocumentsProvider.AUTHORITY, mParentIdentifier.mDocumentId); |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 76 | setUpDocument(mManager, 40); |
| 77 | mManager.blockDocument(0, 15); |
| 78 | mManager.blockDocument(0, 35); |
| 79 | |
| 80 | { |
| 81 | final Cursor cursor = mLoader.queryChildDocuments( |
| 82 | MtpDocumentsProvider.DEFAULT_DOCUMENT_PROJECTION, mParentIdentifier); |
| 83 | assertEquals(DocumentLoader.NUM_INITIAL_ENTRIES, cursor.getCount()); |
| 84 | } |
| 85 | |
| 86 | Thread.sleep(DocumentLoader.NOTIFY_PERIOD_MS); |
| 87 | mManager.unblockDocument(0, 15); |
| 88 | mResolver.waitForNotification(uri, 1); |
| 89 | |
| 90 | { |
| 91 | final Cursor cursor = mLoader.queryChildDocuments( |
| 92 | MtpDocumentsProvider.DEFAULT_DOCUMENT_PROJECTION, mParentIdentifier); |
| 93 | assertEquals( |
| 94 | DocumentLoader.NUM_INITIAL_ENTRIES + DocumentLoader.NUM_LOADING_ENTRIES, |
| 95 | cursor.getCount()); |
| 96 | } |
| 97 | |
| 98 | mManager.unblockDocument(0, 35); |
| 99 | mResolver.waitForNotification(uri, 2); |
| 100 | |
| 101 | { |
| 102 | final Cursor cursor = mLoader.queryChildDocuments( |
| 103 | MtpDocumentsProvider.DEFAULT_DOCUMENT_PROJECTION, mParentIdentifier); |
| 104 | assertEquals(40, cursor.getCount()); |
| 105 | } |
| 106 | |
| 107 | assertEquals(2, mResolver.getChangeCount(uri)); |
| 108 | } |
| 109 | |
| 110 | private void setUpDocument(TestMtpManager manager, int count) { |
| 111 | int[] childDocuments = new int[count]; |
| 112 | for (int i = 0; i < childDocuments.length; i++) { |
| 113 | final int objectHandle = i + 1; |
| 114 | childDocuments[i] = objectHandle; |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 115 | manager.setObjectInfo(0, new MtpObjectInfo.Builder() |
| 116 | .setObjectHandle(objectHandle) |
Daichi Hirono | 47eb192 | 2015-11-16 13:01:31 +0900 | [diff] [blame] | 117 | .setName(Integer.toString(i)) |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 118 | .build()); |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 119 | } |
| 120 | manager.setObjectHandles(0, 0, MtpManager.OBJECT_HANDLE_ROOT_CHILDREN, childDocuments); |
| 121 | } |
| 122 | |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 123 | private static class BlockableTestMtpManager extends TestMtpManager { |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 124 | final private Map<String, CountDownLatch> blockedDocuments = new HashMap<>(); |
| 125 | |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 126 | BlockableTestMtpManager(Context context) { |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 127 | super(context); |
| 128 | } |
| 129 | |
| 130 | void blockDocument(int deviceId, int objectHandle) { |
| 131 | blockedDocuments.put(pack(deviceId, objectHandle), new CountDownLatch(1)); |
| 132 | } |
| 133 | |
| 134 | void unblockDocument(int deviceId, int objectHandle) { |
| 135 | blockedDocuments.get(pack(deviceId, objectHandle)).countDown(); |
| 136 | } |
| 137 | |
| 138 | @Override |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 139 | MtpObjectInfo getObjectInfo(int deviceId, int objectHandle) throws IOException { |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 140 | final CountDownLatch latch = blockedDocuments.get(pack(deviceId, objectHandle)); |
| 141 | if (latch != null) { |
| 142 | try { |
| 143 | latch.await(); |
| 144 | } catch(InterruptedException e) { |
| 145 | fail(); |
| 146 | } |
| 147 | } |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 148 | return super.getObjectInfo(deviceId, objectHandle); |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } |