1 /* 2 * Copyright 2018 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 androidx.slice.render; 18 19 import static android.os.Build.VERSION.SDK_INT; 20 21 import static androidx.slice.render.SliceRenderer.SCREENSHOT_DIR; 22 23 import android.content.BroadcastReceiver; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.IntentFilter; 27 import android.os.Build; 28 import android.support.test.InstrumentationRegistry; 29 import android.support.test.filters.SmallTest; 30 import android.support.test.runner.AndroidJUnit4; 31 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 35 import java.io.File; 36 import java.io.FileInputStream; 37 import java.io.FileOutputStream; 38 import java.io.InputStream; 39 import java.io.OutputStream; 40 import java.util.concurrent.CountDownLatch; 41 import java.util.concurrent.TimeUnit; 42 43 @SmallTest 44 @RunWith(AndroidJUnit4.class) 45 public class RenderTest { 46 47 private final Context mContext = InstrumentationRegistry.getContext(); 48 49 @Test 50 public void testRender() throws Exception { 51 final CountDownLatch latch = new CountDownLatch(1); 52 BroadcastReceiver receiver = new BroadcastReceiver() { 53 @Override 54 public void onReceive(Context context, Intent intent) { 55 latch.countDown(); 56 } 57 }; 58 mContext.registerReceiver(receiver, 59 new IntentFilter(SliceRenderActivity.ACTION_RENDER_DONE)); 60 mContext.startActivity(new Intent(mContext, SliceRenderActivity.class) 61 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); 62 63 latch.await(30000, TimeUnit.MILLISECONDS); 64 String path = new File(mContext.getFilesDir(), SCREENSHOT_DIR).getAbsolutePath(); 65 if (SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 66 InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand( 67 "mv " + path + " " + "/sdcard/"); 68 } else { 69 deleteDir(new File("/sdcard/" + SCREENSHOT_DIR)); 70 copyDirectory(new File(path), new File("/sdcard/" + SCREENSHOT_DIR)); 71 } 72 } 73 74 75 public static void copyDirectory(File sourceLocation, File targetLocation) throws Exception { 76 if (sourceLocation.isDirectory()) { 77 if (!targetLocation.exists()) { 78 targetLocation.mkdirs(); 79 } 80 81 String[] children = sourceLocation.list(); 82 for (int i = 0; i < children.length; i++) { 83 copyDirectory(new File(sourceLocation, children[i]), new File( 84 targetLocation, children[i])); 85 } 86 } else { 87 copyFile(sourceLocation, targetLocation); 88 } 89 } 90 91 public static void copyFile(File sourceLocation, File targetLocation) throws Exception { 92 InputStream in = new FileInputStream(sourceLocation); 93 OutputStream out = new FileOutputStream(targetLocation); 94 95 byte[] buf = new byte[1024]; 96 int len; 97 while ((len = in.read(buf)) > 0) { 98 out.write(buf, 0, len); 99 } 100 in.close(); 101 out.close(); 102 } 103 104 static void deleteDir(File file) { 105 File[] contents = file.listFiles(); 106 if (contents != null) { 107 for (File f : contents) { 108 deleteDir(f); 109 } 110 } 111 file.delete(); 112 } 113 } 114