Home | History | Annotate | Download | only in services
      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.ContentProviderClient;
     23 import android.content.ContentResolver;
     24 import android.content.Context;
     25 import android.net.Uri;
     26 import android.os.RemoteException;
     27 import android.support.test.InstrumentationRegistry;
     28 import android.support.test.filters.MediumTest;
     29 import android.test.AndroidTestCase;
     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.clipping.UrisSupplier;
     38 import com.android.documentsui.services.FileOperationService.OpType;
     39 import com.android.documentsui.testing.DocsProviders;
     40 import com.android.documentsui.testing.TestFeatures;
     41 
     42 import java.util.List;
     43 
     44 @MediumTest
     45 public abstract class AbstractJobTest<T extends Job> extends AndroidTestCase {
     46 
     47     static String AUTHORITY = StubProvider.DEFAULT_AUTHORITY;
     48     static final byte[] HAM_BYTES = "ham and cheese".getBytes();
     49     static final byte[] FRUITY_BYTES = "I love fruit cakes!".getBytes();
     50 
     51     Context mContext;
     52     ContentResolver mResolver;
     53     ContentProviderClient mClient;
     54     DocumentsProviderHelper mDocs;
     55     TestJobListener mJobListener;
     56     RootInfo mSrcRoot;
     57     RootInfo mDestRoot;
     58 
     59     private TestFeatures mFeatures;
     60 
     61     @Override
     62     protected void setUp() throws Exception {
     63         super.setUp();
     64 
     65         mFeatures = new TestFeatures();
     66         mFeatures.notificationChannel = InstrumentationRegistry.getTargetContext()
     67                 .getResources().getBoolean(R.bool.feature_notification_channel);
     68 
     69         mJobListener = new TestJobListener();
     70 
     71         // NOTE: Must be the "target" context, else security checks in content provider will fail.
     72         mContext = getContext();
     73         mResolver = mContext.getContentResolver();
     74 
     75         mClient = mResolver.acquireContentProviderClient(AUTHORITY);
     76         mDocs = new DocumentsProviderHelper(AUTHORITY, mClient);
     77 
     78         initTestFiles();
     79     }
     80 
     81     @Override
     82     protected void tearDown() throws Exception {
     83         resetStorage();
     84         mClient.release();
     85         super.tearDown();
     86     }
     87 
     88     private void resetStorage() throws RemoteException {
     89         mClient.call("clear", null, null);
     90     }
     91 
     92     private void initTestFiles() throws RemoteException {
     93         mSrcRoot = mDocs.getRoot(ROOT_0_ID);
     94         mDestRoot = mDocs.getRoot(ROOT_1_ID);
     95     }
     96 
     97     final T createJob(@OpType int opType, List<Uri> srcs, Uri srcParent, Uri destination)
     98             throws Exception {
     99         DocumentStack stack =
    100                 new DocumentStack(mSrcRoot, DocumentInfo.fromUri(mResolver, destination));
    101 
    102         UrisSupplier urisSupplier = DocsProviders.createDocsProvider(srcs);
    103         FileOperation operation = new FileOperation.Builder()
    104                 .withOpType(opType)
    105                 .withSrcs(urisSupplier)
    106                 .withDestination(stack)
    107                 .withSrcParent(srcParent)
    108                 .build();
    109         return (T) operation.createJob(
    110                 mContext, mJobListener, FileOperations.createJobId(), mFeatures);
    111     }
    112 }
    113