DocumentLoaderTest.java revision 6baa16e9109046661fef8dcc25b8754ac68bcdae
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.database.Cursor; 21import android.net.Uri; 22import android.provider.DocumentsContract; 23import android.test.AndroidTestCase; 24import android.test.suitebuilder.annotation.SmallTest; 25 26import java.io.IOException; 27import java.util.Date; 28import java.util.HashMap; 29import java.util.Map; 30import java.util.concurrent.CountDownLatch; 31 32@SmallTest 33public class DocumentLoaderTest extends AndroidTestCase { 34 private BlockableTestMtpMaanger mManager; 35 private TestContentResolver mResolver; 36 private DocumentLoader mLoader; 37 final private Identifier mParentIdentifier = new Identifier(0, 0, 0); 38 39 @Override 40 public void setUp() { 41 mManager = new BlockableTestMtpMaanger(getContext()); 42 mResolver = new TestContentResolver(); 43 mLoader = new DocumentLoader(mManager, mResolver); 44 } 45 46 public void testBasic() throws IOException, InterruptedException { 47 final Uri uri = DocumentsContract.buildChildDocumentsUri( 48 MtpDocumentsProvider.AUTHORITY, mParentIdentifier.toDocumentId()); 49 setUpDocument(mManager, 40); 50 mManager.blockDocument(0, 15); 51 mManager.blockDocument(0, 35); 52 53 { 54 final Cursor cursor = mLoader.queryChildDocuments( 55 MtpDocumentsProvider.DEFAULT_DOCUMENT_PROJECTION, mParentIdentifier); 56 assertEquals(DocumentLoader.NUM_INITIAL_ENTRIES, cursor.getCount()); 57 } 58 59 Thread.sleep(DocumentLoader.NOTIFY_PERIOD_MS); 60 mManager.unblockDocument(0, 15); 61 mResolver.waitForNotification(uri, 1); 62 63 { 64 final Cursor cursor = mLoader.queryChildDocuments( 65 MtpDocumentsProvider.DEFAULT_DOCUMENT_PROJECTION, mParentIdentifier); 66 assertEquals( 67 DocumentLoader.NUM_INITIAL_ENTRIES + DocumentLoader.NUM_LOADING_ENTRIES, 68 cursor.getCount()); 69 } 70 71 mManager.unblockDocument(0, 35); 72 mResolver.waitForNotification(uri, 2); 73 74 { 75 final Cursor cursor = mLoader.queryChildDocuments( 76 MtpDocumentsProvider.DEFAULT_DOCUMENT_PROJECTION, mParentIdentifier); 77 assertEquals(40, cursor.getCount()); 78 } 79 80 assertEquals(2, mResolver.getChangeCount(uri)); 81 } 82 83 private void setUpDocument(TestMtpManager manager, int count) { 84 int[] childDocuments = new int[count]; 85 for (int i = 0; i < childDocuments.length; i++) { 86 final int objectHandle = i + 1; 87 childDocuments[i] = objectHandle; 88 manager.setDocument(0, objectHandle, new MtpDocument( 89 objectHandle, 90 0 /* format */, 91 "file" + objectHandle, 92 new Date(), 93 1024, 94 0 /* thumbnail size */)); 95 } 96 manager.setObjectHandles(0, 0, MtpManager.OBJECT_HANDLE_ROOT_CHILDREN, childDocuments); 97 } 98 99 private static class BlockableTestMtpMaanger extends TestMtpManager { 100 final private Map<String, CountDownLatch> blockedDocuments = new HashMap<>(); 101 102 BlockableTestMtpMaanger(Context context) { 103 super(context); 104 } 105 106 void blockDocument(int deviceId, int objectHandle) { 107 blockedDocuments.put(pack(deviceId, objectHandle), new CountDownLatch(1)); 108 } 109 110 void unblockDocument(int deviceId, int objectHandle) { 111 blockedDocuments.get(pack(deviceId, objectHandle)).countDown(); 112 } 113 114 @Override 115 MtpDocument getDocument(int deviceId, int objectHandle) throws IOException { 116 final CountDownLatch latch = blockedDocuments.get(pack(deviceId, objectHandle)); 117 if (latch != null) { 118 try { 119 latch.await(); 120 } catch(InterruptedException e) { 121 fail(); 122 } 123 } 124 return super.getDocument(deviceId, objectHandle); 125 } 126 } 127} 128