UninstallIotLauncherAppsTask implements LaunchTask { @Override public LaunchResult run( @NotNull Executor executor, @NotNull IDevice device, @NotNull LaunchStatus launchStatus, @NotNull ConsolePrinter printer) { if (!device.supportsFeature(IDevice.HardwareFeature.EMBEDDED)) { return LaunchResult.success(); } Set<String> installedIotLauncherApps = myChecker.getInstalledIotLauncherApps(device); installedIotLauncherApps.remove(myPackageName); if (!installedIotLauncherApps.isEmpty()) { String otherApplicationIds = StringUtil.join(installedIotLauncherApps, "\n"); String reason = AndroidBundle.message("deployment.failed.uninstall.prompt.androidthings.text", otherApplicationIds); if (myPrompter.showQuestionPrompt(reason)) { Map<String, Throwable> failedUninstallApps = new TreeMap<>(); for (String app : installedIotLauncherApps) { try { device.uninstallPackage(app); } catch (InstallException e) { failedUninstallApps.put(app, e); } } if (!failedUninstallApps.isEmpty()) { StringBuffer sb = new StringBuffer(); for (Map.Entry a : failedUninstallApps.entrySet()) { sb.append(a.getKey()); sb.append(": "); sb.append(a.getValue()); sb.append("\n"); } String errorMessage = AndroidBundle.message("deployment.failed.uninstall.prompt.androidthings.errortext", sb.toString()); myPrompter.showErrorMessage(errorMessage); return LaunchResult.error("", getDescription()); } } else { printer.stdout("Installation aborted"); return LaunchResult.error("", getDescription()); } } return LaunchResult.success(); } UninstallIotLauncherAppsTask(Project project, String packageName); @VisibleForTesting  UninstallIotLauncherAppsTask(String packageName, IotInstallChecker checker, RetryingInstaller.Prompter prompter); @NotNull @Override String getDescription(); @Override int getDuration(); @Override LaunchResult run(
    @NotNull Executor executor, @NotNull IDevice device, @NotNull LaunchStatus launchStatus, @NotNull ConsolePrinter printer); @NotNull @Override String getId();  }
@Test public void testTaskSucceedsOnNonEmbeddedHardware() { UninstallIotLauncherAppsTask task = new UninstallIotLauncherAppsTask(myProject, PACKAGE_NAME); assertTrue(task.run(myExecutor, myDevice, myLaunchStatus, myPrinter).getSuccess()); }
@Test public void testTaskSucceedsIfNoIotPackageExists() { UninstallIotLauncherAppsTask task = new UninstallIotLauncherAppsTask(myProject, PACKAGE_NAME); assertTrue(task.run(myExecutor, myEmbeddedDevice, myLaunchStatus, myPrinter).getSuccess()); }
@Test public void testTaskPromptsUserIfIotPackageExists() { when(myChecker.getInstalledIotLauncherApps(myEmbeddedDevice)).thenReturn(Collections.singleton(OTHER_PACKAGE_NAME)); UninstallIotLauncherAppsTask task = new UninstallIotLauncherAppsTask(PACKAGE_NAME, myChecker, myPrompt); task.run(myExecutor, myEmbeddedDevice, myLaunchStatus, myPrinter); verify(myPrompt).showQuestionPrompt(Mockito.anyString()); }
@Test public void testTaskUninstallsPackagesIfUserSaysYesAndSucceeds() throws Throwable { when(myChecker.getInstalledIotLauncherApps(myEmbeddedDevice)).thenReturn(Collections.singleton(OTHER_PACKAGE_NAME)); when(myPrompt.showQuestionPrompt(Mockito.anyString())).thenReturn(true); UninstallIotLauncherAppsTask task = new UninstallIotLauncherAppsTask(PACKAGE_NAME, myChecker, myPrompt); assertTrue(task.run(myExecutor, myEmbeddedDevice, myLaunchStatus, myPrinter).getSuccess()); verify(myEmbeddedDevice).uninstallPackage(OTHER_PACKAGE_NAME); }
@Test public void testTaskShowsErrorMessageAndFailsIfUninstallFailed() throws Throwable { when(myChecker.getInstalledIotLauncherApps(myEmbeddedDevice)).thenReturn(Collections.singleton(OTHER_PACKAGE_NAME)); when(myPrompt.showQuestionPrompt(Mockito.anyString())).thenReturn(true); when(myEmbeddedDevice.uninstallPackage(OTHER_PACKAGE_NAME)).thenThrow(new InstallException("Error")); UninstallIotLauncherAppsTask task = new UninstallIotLauncherAppsTask(PACKAGE_NAME, myChecker, myPrompt); assertFalse(task.run(myExecutor, myEmbeddedDevice, myLaunchStatus, myPrinter).getSuccess()); verify(myPrompt).showErrorMessage(Mockito.anyString()); }
@Test public void testTaskFailsIfUserSaysNo() throws Throwable { when(myChecker.getInstalledIotLauncherApps(myEmbeddedDevice)).thenReturn(Collections.singleton(OTHER_PACKAGE_NAME)); when(myPrompt.showQuestionPrompt(Mockito.anyString())).thenReturn(false); UninstallIotLauncherAppsTask task = new UninstallIotLauncherAppsTask(PACKAGE_NAME, myChecker, myPrompt); assertFalse(task.run(myExecutor, myEmbeddedDevice, myLaunchStatus, myPrinter).getSuccess()); }