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