RunOnMultipleDevicesAction extends AnAction { @Override public void update(@NotNull AnActionEvent event) { Project project = event.getProject(); Presentation presentation = event.getPresentation(); if (project == null) { presentation.setEnabled(false); return; } RunnerAndConfigurationSettings settings = myGetSelectedConfiguration.apply(project); if (settings == null) { presentation.setEnabled(false); return; } if (!isSupportedRunConfigurationType(settings.getType())) { presentation.setEnabled(false); return; } if (myGetDevices.apply(project).isEmpty()) { presentation.setEnabled(false); return; } presentation.setEnabled(true); } RunOnMultipleDevicesAction(); @VisibleForTesting  RunOnMultipleDevicesAction(@NotNull Function<Project, RunnerAndConfigurationSettings> getSelectedConfiguration,
                             @NotNull Function<Project, Collection<Device>> getDevices); @Override void update(@NotNull AnActionEvent event); @Override void actionPerformed(@NotNull AnActionEvent event);  }
@Test public void updateProjectIsNull() { myAction = new RunOnMultipleDevicesAction(); myAction.update(myEvent); assertFalse(myPresentation.isEnabled()); }
@Test public void updateSettingsAreNull() { myAction = new RunOnMultipleDevicesAction(); Mockito.when(myEvent.getProject()).thenReturn(myRule.getProject()); myAction.update(myEvent); assertFalse(myPresentation.isEnabled()); }
@Test public void updateWithNonSupportedRunConfigurationTypeShouldDisableAction() { RunnerAndConfigurationSettings settings = Mockito.mock(RunnerAndConfigurationSettings.class); Mockito.when(settings.getType()).thenReturn(Mockito.mock(ConfigurationType.class)); myAction = new RunOnMultipleDevicesAction(project -> settings, project -> Collections.emptyList()); Mockito.when(myEvent.getProject()).thenReturn(myRule.getProject()); myAction.update(myEvent); assertFalse(myPresentation.isEnabled()); }
@Test public void updateDevicesIsEmpty() { RunnerAndConfigurationSettings settings = Mockito.mock(RunnerAndConfigurationSettings.class); Mockito.when(settings.getType()).thenReturn(AndroidRunConfigurationType.getInstance()); myAction = new RunOnMultipleDevicesAction(project -> settings, project -> Collections.emptyList()); Mockito.when(myEvent.getProject()).thenReturn(myRule.getProject()); myAction.update(myEvent); assertFalse(myPresentation.isEnabled()); }
@Test public void update() { RunnerAndConfigurationSettings settings = Mockito.mock(RunnerAndConfigurationSettings.class); Mockito.when(settings.getType()).thenReturn(AndroidRunConfigurationType.getInstance()); Device device = new VirtualDevice.Builder() .setName("Pixel 3 API 29") .setKey(new Key("Pixel_3_API_29")) .setAndroidDevice(Mockito.mock(AndroidDevice.class)) .build(); myAction = new RunOnMultipleDevicesAction(project -> settings, project -> Collections.singletonList(device)); Mockito.when(myEvent.getProject()).thenReturn(myRule.getProject()); myAction.update(myEvent); assertTrue(myPresentation.isEnabled()); }