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