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.provider.DocumentsContract;
     28 import android.test.AndroidTestCase;
     29 import android.test.suitebuilder.annotation.MediumTest;
     30 
     31 import com.android.documentsui.DocumentsProviderHelper;
     32 import com.android.documentsui.StubProvider;
     33 import com.android.documentsui.model.DocumentInfo;
     34 import com.android.documentsui.model.DocumentStack;
     35 import com.android.documentsui.model.RootInfo;
     36 
     37 import com.google.common.collect.Lists;
     38 
     39 import java.util.List;
     40 
     41 @MediumTest
     42 public abstract class AbstractJobTest<T extends Job> extends AndroidTestCase {
     43 
     44     static String AUTHORITY = StubProvider.DEFAULT_AUTHORITY;
     45     static final byte[] HAM_BYTES = "ham and cheese".getBytes();
     46     static final byte[] FRUITY_BYTES = "I love fruit cakes!".getBytes();
     47 
     48     Context mContext;
     49     ContentResolver mResolver;
     50     ContentProviderClient mClient;
     51     DocumentsProviderHelper mDocs;
     52     TestJobListener mJobListener;
     53     RootInfo mSrcRoot;
     54     RootInfo mDestRoot;
     55 
     56     @Override
     57     protected void setUp() throws Exception {
     58         super.setUp();
     59 
     60         mJobListener = new TestJobListener();
     61 
     62         // NOTE: Must be the "target" context, else security checks in content provider will fail.
     63         mContext = getContext();
     64         mResolver = mContext.getContentResolver();
     65 
     66         mClient = mResolver.acquireContentProviderClient(AUTHORITY);
     67         mDocs = new DocumentsProviderHelper(AUTHORITY, mClient);
     68 
     69         initTestFiles();
     70     }
     71 
     72     @Override
     73     protected void tearDown() throws Exception {
     74         resetStorage();
     75         mClient.release();
     76         super.tearDown();
     77     }
     78 
     79     private void resetStorage() throws RemoteException {
     80         mClient.call("clear", null, null);
     81     }
     82 
     83     private void initTestFiles() throws RemoteException {
     84         mSrcRoot = mDocs.getRoot(ROOT_0_ID);
     85         mDestRoot = mDocs.getRoot(ROOT_1_ID);
     86     }
     87 
     88     final T createJob(List<Uri> srcs, Uri srcParent, Uri destination) throws Exception {
     89         DocumentStack stack = new DocumentStack();
     90         stack.push(DocumentInfo.fromUri(mResolver, destination));
     91 
     92         List<DocumentInfo> srcDocs = Lists.newArrayList();
     93         for (Uri src : srcs) {
     94             srcDocs.add(DocumentInfo.fromUri(mResolver, src));
     95         }
     96 
     97         return createJob(srcDocs, DocumentInfo.fromUri(mResolver, srcParent), stack);
     98     }
     99 
    100     abstract T createJob(List<DocumentInfo> srcs, DocumentInfo srcParent, DocumentStack destination)
    101             throws Exception;
    102 }
    103