Home | History | Annotate | Download | only in testcommon
      1 /*
      2  * Copyright (C) 2017 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 package com.android.managedprovisioning.testcommon;
     17 
     18 import android.content.ContentResolver;
     19 import android.content.Context;
     20 import android.net.Uri;
     21 
     22 import com.android.managedprovisioning.common.StoreUtils;
     23 import java.io.ByteArrayOutputStream;
     24 import java.io.File;
     25 import java.io.IOException;
     26 import java.io.InputStream;
     27 
     28 public class TestUtils {
     29     public static Uri resourceToUri(Context context, int resID) {
     30         return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
     31                 + context.getResources().getResourcePackageName(resID) + '/'
     32                 + context.getResources().getResourceTypeName(resID) + '/'
     33                 + context.getResources().getResourceEntryName(resID) );
     34     }
     35 
     36     public static String stringFromUri(ContentResolver cr, Uri uri) throws IOException {
     37         try (final InputStream in = cr.openInputStream(uri)) {
     38             try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
     39                 StoreUtils.copyStream(in, out);
     40                 return out.toString();
     41             }
     42         }
     43     }
     44 
     45     public static void deleteRecursive(File fileOrDirectory) {
     46         if (fileOrDirectory.isDirectory())
     47             for (File child : fileOrDirectory.listFiles())
     48                 deleteRecursive(child);
     49 
     50         fileOrDirectory.delete();
     51     }
     52 }
     53