1 // Copyright 2019 The Amber Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package com.google.amber; 16 17 import static org.junit.Assert.assertEquals; 18 19 import android.content.Context; 20 import android.os.Bundle; 21 22 import androidx.test.platform.app.InstrumentationRegistry; 23 24 import org.junit.Test; 25 26 import java.io.File; 27 import java.util.ArrayList; 28 import java.util.List; 29 30 // Instrumented test used for launching Amber. 31 public class AmberLauncher { 32 @Test LaunchAmber()33 public void LaunchAmber() { 34 Context app_context = InstrumentationRegistry.getInstrumentation().getTargetContext(); 35 assertEquals("com.google.amber", app_context.getPackageName()); 36 37 Bundle args = InstrumentationRegistry.getArguments(); 38 39 int arg_index = 1; 40 List<String> args_list = new ArrayList<>(); 41 42 while (true) { 43 String arg = args.getString("arg" + arg_index); 44 if (arg == null) { 45 break; 46 } 47 args_list.add(arg); 48 ++arg_index; 49 } 50 51 File outputDir = app_context.getExternalCacheDir(); 52 53 // This will typically be: /sdcard/Android/data/com.google.amber/cache/amber_std{out,err}.txt 54 String stdout_file = args.getString("stdout", new File(outputDir, "amber_stdout.txt").toString()); 55 String stderr_file = args.getString("stderr", new File(outputDir, "amber_stderr.txt").toString()); 56 57 int res = Amber.androidHelper(args_list.toArray(new String[0]), stdout_file, stderr_file); 58 59 // If the process crashes during the above call or we call System.exit below, the output 60 // from `adb shell am instrument ...` will be: 61 // com.google.amber.AmberLauncher:INSTRUMENTATION_RESULT: shortMsg=Process crashed. 62 // INSTRUMENTATION_CODE: 0 63 if (res != 0) { 64 System.exit(res); 65 } 66 } 67 } 68