Search in sources :

Example 1 with InstallException

use of com.android.ddmlib.InstallException in project buck by facebook.

the class AdbHelper method deviceUninstallPackage.

/**
   * Modified version of <a href="http://fburl.com/8840769">Device.uninstallPackage()</a>.
   *
   * @param device an {@link IDevice}
   * @param packageName application package name
   * @param keepData  true if user data is to be kept
   * @return error message or null if successful
   * @throws InstallException
   */
@Nullable
private String deviceUninstallPackage(IDevice device, String packageName, boolean keepData) throws InstallException {
    try {
        AdbHelper.ErrorParsingReceiver receiver = new AdbHelper.ErrorParsingReceiver() {

            @Override
            @Nullable
            protected String matchForError(String line) {
                return line.toLowerCase(Locale.US).contains("failure") ? line : null;
            }
        };
        device.executeShellCommand("pm uninstall " + (keepData ? "-k " : "") + packageName, receiver, AdbHelper.INSTALL_TIMEOUT, TimeUnit.MILLISECONDS);
        return receiver.getErrorMessage();
    } catch (AdbCommandRejectedException | IOException | ShellCommandUnresponsiveException | TimeoutException e) {
        throw new InstallException(e);
    }
}
Also used : ShellCommandUnresponsiveException(com.android.ddmlib.ShellCommandUnresponsiveException) AdbCommandRejectedException(com.android.ddmlib.AdbCommandRejectedException) IOException(java.io.IOException) InstallException(com.android.ddmlib.InstallException) TimeoutException(com.android.ddmlib.TimeoutException) Nullable(javax.annotation.Nullable)

Example 2 with InstallException

use of com.android.ddmlib.InstallException in project buck by facebook.

the class AdbHelper method uninstallApkFromDevice.

/**
   * Uninstalls apk from specific device. Reports success or failure to console.
   * It's currently here because it's used both by {@link com.facebook.buck.cli.InstallCommand} and
   * {@link com.facebook.buck.cli.UninstallCommand}.
   */
@SuppressWarnings("PMD.PrematureDeclaration")
@SuppressForbidden
private boolean uninstallApkFromDevice(IDevice device, String packageName, boolean keepData) {
    String name;
    if (device.isEmulator()) {
        name = device.getSerialNumber() + " (" + device.getAvdName() + ")";
    } else {
        name = device.getSerialNumber();
        String model = device.getProperty("ro.product.model");
        if (model != null) {
            name += " (" + model + ")";
        }
    }
    PrintStream stdOut = console.getStdOut();
    stdOut.printf("Removing apk from %s.\n", name);
    try {
        long start = System.currentTimeMillis();
        String reason = deviceUninstallPackage(device, packageName, keepData);
        long end = System.currentTimeMillis();
        if (reason != null) {
            console.printBuildFailure(String.format("Failed to uninstall apk from %s: %s.", name, reason));
            return false;
        }
        long delta = end - start;
        stdOut.printf("Uninstalled apk from %s in %d.%03ds.\n", name, delta / 1000, delta % 1000);
        return true;
    } catch (InstallException ex) {
        console.printBuildFailure(String.format("Failed to uninstall apk from %s.", name));
        ex.printStackTrace(console.getStdErr());
        return false;
    }
}
Also used : PrintStream(java.io.PrintStream) InstallException(com.android.ddmlib.InstallException) SuppressForbidden(com.facebook.buck.annotations.SuppressForbidden)

Example 3 with InstallException

use of com.android.ddmlib.InstallException in project buck by facebook.

the class AdbHelperTest method testFailedDeviceInstallWithReason.

/**
   * Verify that if failure reason is returned, installation is marked as failed.
   */
@Test
public void testFailedDeviceInstallWithReason() {
    File apk = new File("/some/file.apk");
    TestDevice device = new TestDevice() {

        @Override
        public void installPackage(String s, boolean b, String... strings) throws InstallException {
            throw new InstallException("[SOME REASON]");
        }
    };
    device.setSerialNumber("serial#1");
    device.setName("testDevice");
    assertFalse(basicAdbHelper.installApkOnDevice(device, apk, false, false));
}
Also used : File(java.io.File) InstallException(com.android.ddmlib.InstallException) Test(org.junit.Test)

Example 4 with InstallException

use of com.android.ddmlib.InstallException in project buck by facebook.

the class AdbHelperTest method testFailedDeviceInstallWithException.

/**
   * Verify that if exception is thrown during installation, installation is marked as failed.
   */
@Test
public void testFailedDeviceInstallWithException() {
    File apk = new File("/some/file.apk");
    TestDevice device = new TestDevice() {

        @Override
        public void installPackage(String s, boolean b, String... strings) throws InstallException {
            throw new InstallException("Failed to install on test device.", null);
        }
    };
    device.setSerialNumber("serial#1");
    device.setName("testDevice");
    assertFalse(basicAdbHelper.installApkOnDevice(device, apk, false, false));
}
Also used : File(java.io.File) InstallException(com.android.ddmlib.InstallException) Test(org.junit.Test)

Example 5 with InstallException

use of com.android.ddmlib.InstallException in project buck by facebook.

the class AdbHelperTest method testQuietDeviceInstall.

@Test
public void testQuietDeviceInstall() throws InterruptedException {
    final File apk = new File("/some/file.apk");
    final AtomicReference<String> apkPath = new AtomicReference<>();
    TestDevice device = new TestDevice() {

        @Override
        public void installPackage(String s, boolean b, String... strings) throws InstallException {
            apkPath.set(s);
        }
    };
    device.setSerialNumber("serial#1");
    device.setName("testDevice");
    final List<IDevice> deviceList = Lists.newArrayList((IDevice) device);
    TestConsole console = new TestConsole();
    BuckEventBus eventBus = BuckEventBusFactory.newInstance();
    AdbHelper adbHelper = new AdbHelper(new AdbOptions(), new TargetDeviceOptions(), TestExecutionContext.newInstance(), console, eventBus, true) {

        @Override
        protected boolean isDeviceTempWritable(IDevice device, String name) {
            return true;
        }

        @Override
        public List<IDevice> getDevices(boolean quiet) {
            return deviceList;
        }
    };
    boolean success = adbHelper.adbCall(new AdbHelper.AdbCallable() {

        @Override
        public boolean call(IDevice device) throws Exception {
            return basicAdbHelper.installApkOnDevice(device, apk, false, true);
        }

        @Override
        public String toString() {
            return "install apk";
        }
    }, true);
    assertTrue(success);
    assertEquals(apk.getAbsolutePath(), apkPath.get());
    assertEquals("", console.getTextWrittenToStdOut());
    assertEquals("", console.getTextWrittenToStdErr());
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) IDevice(com.android.ddmlib.IDevice) AtomicReference(java.util.concurrent.atomic.AtomicReference) AdbOptions(com.facebook.buck.step.AdbOptions) CmdLineException(org.kohsuke.args4j.CmdLineException) InstallException(com.android.ddmlib.InstallException) TargetDeviceOptions(com.facebook.buck.step.TargetDeviceOptions) TestConsole(com.facebook.buck.testutil.TestConsole) File(java.io.File) Test(org.junit.Test)

Aggregations

InstallException (com.android.ddmlib.InstallException)6 File (java.io.File)4 Test (org.junit.Test)4 IDevice (com.android.ddmlib.IDevice)2 BuckEventBus (com.facebook.buck.event.BuckEventBus)2 AdbOptions (com.facebook.buck.step.AdbOptions)2 TargetDeviceOptions (com.facebook.buck.step.TargetDeviceOptions)2 TestConsole (com.facebook.buck.testutil.TestConsole)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 CmdLineException (org.kohsuke.args4j.CmdLineException)2 AdbCommandRejectedException (com.android.ddmlib.AdbCommandRejectedException)1 ShellCommandUnresponsiveException (com.android.ddmlib.ShellCommandUnresponsiveException)1 TimeoutException (com.android.ddmlib.TimeoutException)1 SuppressForbidden (com.facebook.buck.annotations.SuppressForbidden)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 Nullable (javax.annotation.Nullable)1