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);
}
}
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;
}
}
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));
}
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));
}
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());
}
Aggregations