1 /* 2 * Copyright (C) 2016 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.documentsui.services; 18 19 import static com.android.documentsui.StubProvider.ROOT_0_ID; 20 import static com.android.documentsui.StubProvider.ROOT_1_ID; 21 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.net.Uri; 25 import android.os.RemoteException; 26 import android.test.AndroidTestCase; 27 28 import androidx.test.InstrumentationRegistry; 29 import androidx.test.filters.MediumTest; 30 31 import com.android.documentsui.DocumentsProviderHelper; 32 import com.android.documentsui.R; 33 import com.android.documentsui.StubProvider; 34 import com.android.documentsui.base.DocumentInfo; 35 import com.android.documentsui.base.DocumentStack; 36 import com.android.documentsui.base.RootInfo; 37 import com.android.documentsui.base.UserId; 38 import com.android.documentsui.clipping.UrisSupplier; 39 import com.android.documentsui.services.FileOperationService.OpType; 40 import com.android.documentsui.testing.DocsProviders; 41 import com.android.documentsui.testing.TestFeatures; 42 43 import java.util.List; 44 45 @MediumTest 46 public abstract class AbstractJobTest<T extends Job> extends AndroidTestCase { 47 48 static final String AUTHORITY = StubProvider.DEFAULT_AUTHORITY; 49 static final byte[] HAM_BYTES = "ham and cheese".getBytes(); 50 static final byte[] FRUITY_BYTES = "I love fruit cakes!".getBytes(); 51 52 UserId mUserId; 53 Context mContext; 54 ContentResolver mResolver; 55 DocumentsProviderHelper mDocs; 56 TestJobListener mJobListener; 57 RootInfo mSrcRoot; 58 RootInfo mDestRoot; 59 60 private TestFeatures mFeatures; 61 62 @Override setUp()63 protected void setUp() throws Exception { 64 super.setUp(); 65 66 mFeatures = new TestFeatures(); 67 mFeatures.notificationChannel = InstrumentationRegistry.getTargetContext() 68 .getResources().getBoolean(R.bool.feature_notification_channel); 69 70 mJobListener = new TestJobListener(); 71 72 // NOTE: Must be the "target" context, else security checks in content provider will fail. 73 mUserId = UserId.DEFAULT_USER; 74 mContext = getContext(); 75 mResolver = mContext.getContentResolver(); 76 77 mDocs = new DocumentsProviderHelper(mUserId, AUTHORITY, mContext, AUTHORITY); 78 79 // Reset storage before starting the tests. 80 resetStorage(); 81 82 initTestFiles(); 83 } 84 85 @Override tearDown()86 protected void tearDown() throws Exception { 87 resetStorage(); 88 mDocs.cleanUp(); 89 super.tearDown(); 90 } 91 resetStorage()92 private void resetStorage() throws RemoteException { 93 mDocs.clear(null, null); 94 } 95 initTestFiles()96 private void initTestFiles() throws RemoteException { 97 mSrcRoot = mDocs.getRoot(ROOT_0_ID); 98 mDestRoot = mDocs.getRoot(ROOT_1_ID); 99 } 100 createOperation(@pType int opType, List<Uri> srcs, Uri srcParent, Uri destination)101 FileOperation createOperation(@OpType int opType, List<Uri> srcs, Uri srcParent, 102 Uri destination) throws Exception { 103 DocumentStack stack = 104 new DocumentStack(mSrcRoot, DocumentInfo.fromUri(mResolver, destination, mUserId)); 105 106 UrisSupplier urisSupplier = DocsProviders.createDocsProvider(srcs); 107 FileOperation operation = new FileOperation.Builder() 108 .withOpType(opType) 109 .withSrcs(urisSupplier) 110 .withDestination(stack) 111 .withSrcParent(srcParent) 112 .build(); 113 return operation; 114 } 115 createJob(FileOperation operation)116 final T createJob(FileOperation operation) { 117 return createJob(operation, mJobListener); 118 } 119 createJob(FileOperation operation, Job.Listener listener)120 final T createJob(FileOperation operation, Job.Listener listener) { 121 return (T) operation.createJob( 122 mContext, listener, FileOperations.createJobId(), mFeatures); 123 } 124 createJob(@pType int opType, List<Uri> srcs, Uri srcParent, Uri destination)125 final T createJob(@OpType int opType, List<Uri> srcs, Uri srcParent, Uri destination) 126 throws Exception { 127 DocumentStack stack = 128 new DocumentStack(mSrcRoot, DocumentInfo.fromUri(mResolver, destination, mUserId)); 129 130 UrisSupplier urisSupplier = DocsProviders.createDocsProvider(srcs); 131 FileOperation operation = new FileOperation.Builder() 132 .withOpType(opType) 133 .withSrcs(urisSupplier) 134 .withDestination(stack) 135 .withSrcParent(srcParent) 136 .build(); 137 return (T) operation.createJob( 138 mContext, mJobListener, FileOperations.createJobId(), mFeatures); 139 } 140 } 141