Home | History | Annotate | Download | only in archives
      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.archives;
     18 
     19 import com.android.documentsui.archives.Archive;
     20 import com.android.documentsui.tests.R;
     21 
     22 import android.content.Context;
     23 import android.net.Uri;
     24 import android.os.ParcelFileDescriptor;
     25 
     26 import java.io.File;
     27 import java.io.FileOutputStream;
     28 import java.io.IOException;
     29 import java.io.InputStream;
     30 import java.util.concurrent.ExecutorService;
     31 
     32 import android.util.Log;
     33 
     34 public class TestUtils {
     35     public static final Uri ARCHIVE_URI = Uri.parse("content://i/love/strawberries");
     36     public static final String NOTIFICATION_URI = "content://notification-uri";
     37 
     38     public final Context mTargetContext;
     39     public final Context mTestContext;
     40     public final ExecutorService mExecutor;
     41 
     42     public TestUtils(Context targetContext, Context testContext, ExecutorService executor) {
     43         mTargetContext = targetContext;
     44         mTestContext = testContext;
     45         mExecutor = executor;
     46     }
     47 
     48     /**
     49      * Creates an empty temporary file.
     50      */
     51     public File createTemporaryFile() throws IOException {
     52         return File.createTempFile("com.android.documentsui.archives.tests{",
     53                 "}.zip", mTargetContext.getCacheDir());
     54     }
     55 
     56     /**
     57      * Opens a resource and returns the contents via file descriptor to a local
     58      * snapshot file.
     59      */
     60     public ParcelFileDescriptor getSeekableDescriptor(int resource) {
     61         // Extract the file from resources.
     62         File file = null;
     63         try {
     64             file = File.createTempFile("com.android.documentsui.archives.tests{",
     65                     "}.zip", mTargetContext.getCacheDir());
     66             try (
     67                 final FileOutputStream outputStream =
     68                         new ParcelFileDescriptor.AutoCloseOutputStream(
     69                                 ParcelFileDescriptor.open(
     70                                         file, ParcelFileDescriptor.MODE_WRITE_ONLY));
     71                 final InputStream inputStream =
     72                         mTestContext.getResources().openRawResource(resource);
     73             ) {
     74                 final byte[] buffer = new byte[32 * 1024];
     75                 int bytes;
     76                 while ((bytes = inputStream.read(buffer)) != -1) {
     77                     outputStream.write(buffer, 0, bytes);
     78                 }
     79                 outputStream.flush();
     80             }
     81             return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
     82         } catch (IOException e) {
     83             throw new IllegalStateException("Creating a snapshot failed. ", e);
     84         } finally {
     85             // On UNIX the file will be still available for processes which opened it, even
     86             // after deleting it. Remove it ASAP, as it won't be used by anyone else.
     87             if (file != null) {
     88                 file.delete();
     89             }
     90         }
     91     }
     92 
     93     /**
     94      * Opens a resource and returns the contents via a pipe.
     95      */
     96     public ParcelFileDescriptor getNonSeekableDescriptor(int resource) {
     97         ParcelFileDescriptor[] pipe = null;
     98         try {
     99             pipe = ParcelFileDescriptor.createPipe();
    100             final ParcelFileDescriptor finalOutputPipe = pipe[1];
    101             mExecutor.execute(
    102                     new Runnable() {
    103                         @Override
    104                         public void run() {
    105                             try (
    106                                 final ParcelFileDescriptor.AutoCloseOutputStream outputStream =
    107                                         new ParcelFileDescriptor.
    108                                                 AutoCloseOutputStream(finalOutputPipe);
    109                                 final InputStream inputStream =
    110                                         mTestContext.getResources().openRawResource(resource);
    111                             ) {
    112                                 final byte[] buffer = new byte[32 * 1024];
    113                                 int bytes;
    114                                 while ((bytes = inputStream.read(buffer)) != -1) {
    115                                     outputStream.write(buffer, 0, bytes);
    116                                 }
    117                             } catch (IOException e) {
    118                               throw new IllegalStateException("Piping resource failed.", e);
    119                             }
    120                         }
    121                     });
    122             return pipe[0];
    123         } catch (IOException e) {
    124             throw new IllegalStateException("Failed to create a pipe.", e);
    125         }
    126     }
    127 }
    128