| |
| package dot.junit; |
| |
| |
| import com.android.ddmlib.IDevice; |
| |
| import java.io.File; |
| import java.io.FileOutputStream; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.io.UnsupportedEncodingException; |
| import java.util.Scanner; |
| |
| public class DeviceUtil { |
| |
| private static boolean DEBUG = System.getProperty("cts.vm-tests.debug") != null; |
| |
| /** |
| * Executes the command and its arguments in a native process. |
| * |
| * @param commandAndArgs a string array to be passed containing the |
| * executable and its arguments |
| * @param okIndicator if not null, this String must occur in the stdout of |
| * the executable (since only checking for the return code is not |
| * sufficient e.g. for adb shell cmd) |
| * @throws Exception thrown by the underlying command in case of an error. |
| */ |
| public static void digestCommand(String[] commandAndArgs, String okIndicator) { |
| RuntimeException re = null; |
| try { |
| if (DEBUG) { |
| String c = ""; |
| for (int i = 0; i < commandAndArgs.length; i++) { |
| c += commandAndArgs[i] + " "; |
| } |
| System.out.print("com: " + c); |
| } |
| StringBuilder sb = new StringBuilder(); |
| ProcessBuilder pb = new ProcessBuilder(commandAndArgs).redirectErrorStream(true); |
| Process p = pb.start(); |
| |
| InputStream is = p.getInputStream(); |
| Scanner scanner = new Scanner(is); |
| int retCode = p.waitFor(); |
| while (scanner.hasNextLine()) { |
| sb.append(scanner.nextLine()); |
| } |
| scanner.close(); |
| if (retCode != 0 || (okIndicator != null && !sb.toString().contains(okIndicator))) { |
| String msg = sb.toString() + "\nreturn code: " + retCode; |
| re = new RuntimeException(msg); |
| if (DEBUG) System.out.println("-> error! msg:"+msg); |
| } else { |
| if (DEBUG) System.out.println(" -> " + retCode); |
| } |
| } catch (Exception e) { |
| throw new RuntimeException("Exception occurred: " + e.getClass().getName() + ", msg:" |
| + e.getMessage()); |
| } finally { |
| if (re != null) { |
| throw re; |
| } |
| } |
| } |
| |
| public static void adbExec(IDevice device, String tmpdir, String classpath, String mainclass) { |
| DeviceUtil.digestCommand(new String[] {"adb", "-s", device.getSerialNumber(), "shell", |
| "ANDROID_DATA=" + tmpdir, "dalvikvm", "-Xint:portable", "-Xmx512M", "-Xss32K", |
| "-Djava.io.tmpdir=" + tmpdir, "-classpath", classpath, mainclass, "&&", |
| "echo", "mk_dalvikvmok" }, "mk_dalvikvmok"); |
| } |
| } |