1 /*
2  * Copyright (C) 2019 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 android.graphics.cts;
18 
19 import static org.junit.Assert.assertNull;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22 
23 import android.content.ContentResolver;
24 import android.content.res.Resources;
25 import android.net.Uri;
26 
27 import androidx.test.InstrumentationRegistry;
28 
29 import java.io.File;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 
34 public class Utils {
getResources()35     private static Resources getResources() {
36         return InstrumentationRegistry.getTargetContext().getResources();
37     }
38 
obtainInputStream(int resId)39     private static InputStream obtainInputStream(int resId) {
40         return getResources().openRawResource(resId);
41     }
42 
getAsResourceUri(int resId)43     public static Uri getAsResourceUri(int resId) {
44         Resources res = getResources();
45         return new Uri.Builder()
46                 .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
47                 .authority(res.getResourcePackageName(resId))
48                 .appendPath(res.getResourceTypeName(resId))
49                 .appendPath(res.getResourceEntryName(resId))
50                 .build();
51     }
52 
53     /*
54      * Create a new file and return a path to it.
55      * @param resId Original file. It will be copied into the new file.
56      * @param offset Number of zeroes to write to the new file before the
57      *               copied file. This allows testing decodeFileDescriptor
58      *               with an offset. Must be less than or equal to 1024
59      */
obtainPath(int resId, long offset)60     static String obtainPath(int resId, long offset) {
61         return obtainFile(resId, offset).getPath();
62     }
63 
64     /*
65      * Create and return a new file.
66      * @param resId Original file. It will be copied into the new file.
67      * @param offset Number of zeroes to write to the new file before the
68      *               copied file. This allows testing decodeFileDescriptor
69      *               with an offset. Must be less than or equal to 1024
70      */
obtainFile(int resId, long offset)71     static File obtainFile(int resId, long offset) {
72         assertTrue(offset >= 0);
73         File dir = InstrumentationRegistry.getTargetContext().getFilesDir();
74         dir.mkdirs();
75 
76         String name = getResources().getResourceEntryName(resId).toString();
77         if (offset > 0) {
78             name = name + "_" + String.valueOf(offset);
79         }
80 
81         File file = new File(dir, name);
82         if (file.exists()) {
83             return file;
84         }
85 
86         try {
87             file.createNewFile();
88         } catch (IOException e) {
89             e.printStackTrace();
90             // If the file does not exist it will be handled below.
91         }
92         if (!file.exists()) {
93             fail("Failed to create new File for " + name + "!");
94         }
95 
96         InputStream is = obtainInputStream(resId);
97 
98         try {
99             FileOutputStream fOutput = new FileOutputStream(file);
100             byte[] dataBuffer = new byte[1024];
101             // Write a bunch of zeroes before the image.
102             assertTrue(offset <= 1024);
103             fOutput.write(dataBuffer, 0, (int) offset);
104             int readLength = 0;
105             while ((readLength = is.read(dataBuffer)) != -1) {
106                 fOutput.write(dataBuffer, 0, readLength);
107             }
108             is.close();
109             fOutput.close();
110         } catch (IOException e) {
111             e.printStackTrace();
112             fail("Failed to create file \"" + name + "\" with exception " + e);
113         }
114         return file;
115     }
116 
117     /**
118      * Helper for JUnit-Params tests to combine inputs.
119      */
crossProduct(Object[] a, Object[] b)120     static Object[] crossProduct(Object[] a, Object[] b) {
121         final int length = a.length * b.length;
122         Object[] ret = new Object[length];
123         for (int i = 0; i < a.length; i++) {
124             for (int j = 0; j < b.length; j++) {
125                 int index = i * b.length + j;
126                 assertNull(ret[index]);
127                 ret[index] = new Object[] { a[i], b[j] };
128             }
129         }
130         return ret;
131     }
132 }
133