private void runnerSelected() {
    selectedRunner = selectionManager.getRunner();
    if (selectedRunner == null) {
      showNoRunnerMessage(true);

      propertiesContainer.reset();
      return;
    }

    showNoRunnerMessage(false);

    if (SPLITTER_OFF.equals(panelState.getSplitterState())) {
      rightPropertiesContainer.showTab(selectedRunner.getActiveTab());
    }

    history.selectRunner(selectedRunner);

    if (locale.runnerTabTerminal().equals(selectedRunner.getActiveTab())
        || SPLITTER_ON.equals(panelState.getSplitterState())) {
      terminalContainer.update(selectedRunner);
    }

    update(selectedRunner);

    updateRunnerTimer();
  }
  @Test
  public void runnerShouldBeUpdated() {
    Runner runner1 = mock(Runner.class);
    when(runner1.getRAM()).thenReturn(MB_500.getValue());

    reset(view);
    presenter.update(runner1);

    verify(view).setName(runner1.getTitle());
    verify(view).setType(runner1.getType());
    verify(runner1).getRAM();
    verify(view).selectMemory(500);
    verify(view).selectScope(runner1.getScope());
  }
  @NotNull
  private Runner launchRunner(@NotNull Runner runner) {
    if (runActionPermit.isAllowed()) {

      CurrentProject currentProject = appContext.getCurrentProject();

      if (currentProject == null) {
        throw new IllegalStateException(
            "Can't launch runner for current project. Current project is absent...");
      }

      selectedEnvironment = null;

      panelState.setState(RUNNERS);
      view.showOtherButtons();

      history.addRunner(runner);

      runnerInQueueTimer.schedule(ONE_SEC.getValue());

      CheckRamAndRunAction checkRamAndRunAction = actionFactory.createCheckRamAndRun();
      checkRamAndRunAction.perform(runner);

      runnerActions.put(runner, checkRamAndRunAction);

      runner.resetCreationTime();
      runnerTimer.schedule(ONE_SEC.getValue());
    } else {
      runActionDenyAccessDialog.show();
    }
    return runner;
  }
  /**
   * Updates runner when runner state changed.
   *
   * @param runner runner which was changed
   */
  public void update(@NotNull Runner runner) {
    history.update(runner);

    if (runner.equals(selectedRunner) && history.isRunnerExist(runner)) {
      view.update(runner);
      changeURLDependingOnState(runner);
    }
  }
  private void updateRunnerTimer() {
    if (selectedRunner == null) {
      return;
    }
    view.setTimeout(selectedRunner.getTimeout());

    view.updateMoreInfoPopup(selectedRunner);
  }
  /** {@inheritDoc} */
  @Override
  public void onLogsButtonClicked() {
    Link logUrl = selectedRunner.getLogUrl();
    if (logUrl == null) {
      return;
    }

    view.showLog(logUrl.getHref());
  }
  /** {@inheritDoc} */
  @Override
  public void onRerunButtonClicked() {
    if (runActionPermit.isAllowed()) {

      selectedRunner.setStatus(IN_QUEUE);

      RunnerAction runnerAction = runnerActions.get(selectedRunner);
      if (runnerAction == null || runnerAction instanceof LaunchAction) {
        // Create new CheckRamAndRunAction and update selected runner
        launchRunner(selectedRunner);
      } else {
        runnerAction.perform(selectedRunner);

        update(selectedRunner);
        selectedRunner.resetCreationTime();
      }
    } else {
      runActionDenyAccessDialog.show();
    }
  }
  @Test
  public void shouldUpdateRunnerWithStatusDone() {
    when(runner.getStatus()).thenReturn(Runner.Status.DONE);
    when(css.blueColor()).thenReturn(TEXT);

    runnerWidget.update(runner);

    verify(css).greenColor();

    shouldUpdateItemWidgetParameter();
  }
  @Test
  public void shouldUpdateRunnerWithStatusStopped() {
    when(runner.getStatus()).thenReturn(Runner.Status.STOPPED);
    when(css.blueColor()).thenReturn(TEXT);

    runnerWidget.update(runner);

    verify(css).redColor();

    shouldUpdateItemWidgetParameter();
  }
  @Test
  public void shouldUpdateRunnerWithStatusTimeOut() {
    when(runner.getStatus()).thenReturn(Runner.Status.TIMEOUT);
    when(css.blueColor()).thenReturn(TEXT);

    runnerWidget.update(runner);

    verify(css).whiteColor();

    shouldUpdateItemWidgetParameter();
  }
  @Test
  public void shouldUpdateRunnerWithStatusQueue() {
    when(runner.getStatus()).thenReturn(Runner.Status.IN_QUEUE);
    when(css.blueColor()).thenReturn(TEXT);

    runnerWidget.update(runner);

    verify(css).yellowColor();

    shouldUpdateItemWidgetParameter();
  }
  @Test
  public void shouldUpdateRunnerWithStatusInProgress() {
    when(runner.getStatus()).thenReturn(IN_PROGRESS);
    when(css.blueColor()).thenReturn(TEXT);

    runnerWidget.update(runner);

    verify(css).blueColor();

    shouldUpdateItemWidgetParameter();
  }
 private void changeURLDependingOnState(@NotNull Runner runner) {
   switch (runner.getStatus()) {
     case IN_PROGRESS:
       view.setApplicationURl(locale.uplAppWaitingForBoot());
       break;
     case IN_QUEUE:
       view.setApplicationURl(locale.uplAppWaitingForBoot());
       break;
     case STOPPED:
       view.setApplicationURl(locale.urlAppRunnerStopped());
       break;
     case FAILED:
       view.setApplicationURl(null);
       break;
     default:
       String url = runner.getApplicationURL();
       view.setApplicationURl(url == null ? locale.urlAppRunning() : url);
       setDebugPort(runner);
   }
 }
  @Test
  public void clickEventShouldBePerformedWhenRunnerStatusIsNotFailedOrStopped() throws Exception {
    when(runner.getStatus()).thenReturn(DONE);
    runnerWidget.update(runner);
    reset(itemWidget);

    verify(imagePanel).addDomHandler(clickCaptor.capture(), eq(ClickEvent.getType()));

    clickCaptor.getValue().onClick(clickEvent);

    verify(delegate, never()).removeRunnerWidget(runner);
  }
  @Test
  public void onMouseOutEventShouldBePerformed() throws Exception {
    when(runner.getStatus()).thenReturn(IN_PROGRESS);
    runnerWidget.update(runner);
    reset(itemWidget);

    verify(imagePanel).addDomHandler(mouseOutCaptor.capture(), eq(MouseOutEvent.getType()));

    mouseOutCaptor.getValue().onMouseOut(mouseOutEvent);

    verify(itemWidget).setImage(any(SVGImage.class));
  }
  /**
   * Adds already running runner.
   *
   * @param processDescriptor The descriptor of new runner
   * @return instance of new runner
   */
  @NotNull
  public Runner addRunner(@NotNull ApplicationProcessDescriptor processDescriptor) {
    RunOptions runOptions = dtoFactory.createDto(RunOptions.class);
    Runner runner = modelsFactory.createRunner(runOptions);

    String environmentId = processDescriptor.getEnvironmentId();

    if (environmentId != null && environmentId.startsWith(PROJECT_PREFIX)) {
      runner.setScope(PROJECT);
    }

    runnersId.add(processDescriptor.getProcessId());

    runner.setProcessDescriptor(processDescriptor);
    runner.setRAM(processDescriptor.getMemorySize());
    runner.setStatus(Runner.Status.DONE);
    runner.resetCreationTime();

    history.addRunner(runner);

    onSelectionChanged(RUNNER);

    runnerTimer.schedule(ONE_SEC.getValue());

    LaunchAction launchAction = actionFactory.createLaunch();
    runnerActions.put(runner, launchAction);

    launchAction.perform(runner);

    selectHistoryTab();

    return runner;
  }
  @Test
  public void imageShouldNotBeSetWhenStatusIsNotFailedOrStopped() throws Exception {
    when(runner.getStatus()).thenReturn(DONE);

    runnerWidget.update(runner);
    reset(itemWidget);

    verify(imagePanel).addDomHandler(mouseOverCaptor.capture(), eq(MouseOverEvent.getType()));

    mouseOverCaptor.getValue().onMouseOver(mouseOverEvent);

    verify(itemWidget, never()).setImage(any(SVGImage.class));
  }
  /** {@inheritDoc} */
  @Override
  public Runner launchRunner() {
    CurrentProject currentProject = appContext.getCurrentProject();
    if (currentProject == null) {
      throw new IllegalStateException(
          "Can't launch runner for current project. Current project is absent...");
    }

    int ram = DEFAULT.getValue();

    RunnersDescriptor runnersDescriptor = currentProject.getProjectDescription().getRunners();
    String defaultRunner = runnersDescriptor.getDefault();

    if (!EnvironmentIdValidator.isValid(defaultRunner)) {
      defaultRunner = URL.encode(defaultRunner);
    }

    RunnerConfiguration defaultConfigs = runnersDescriptor.getConfigs().get(defaultRunner);

    if (defaultRunner != null && defaultConfigs != null) {
      ram = defaultConfigs.getRam();
    }

    RunOptions runOptions =
        dtoFactory
            .createDto(RunOptions.class)
            .withSkipBuild(Boolean.valueOf(currentProject.getAttributeValue("runner:skipBuild")))
            .withEnvironmentId(defaultRunner)
            .withMemorySize(ram);

    Environment environment = chooseRunnerAction.selectEnvironment();
    if (environment != null) {
      if (defaultRunner != null && defaultRunner.equals(environment.getId())) {
        Runner runner = modelsFactory.createRunner(runOptions);
        if (defaultRunner.startsWith(PROJECT_PREFIX)) {
          runner.setScope(PROJECT);
        }
        return launchRunner(runner);
      }
      runOptions =
          runOptions
              .withOptions(environment.getOptions())
              .withMemorySize(environment.getRam())
              .withEnvironmentId(environment.getId());
      Runner runner =
          modelsFactory.createRunner(runOptions, environment.getScope(), environment.getName());
      if (environment.getId().startsWith(PROJECT_PREFIX)) {
        runner.setScope(PROJECT);
      }
      return launchRunner(runner);
    }

    Runner runner = modelsFactory.createRunner(runOptions);
    if (defaultRunner != null && defaultRunner.startsWith(PROJECT_PREFIX)) {
      runner.setScope(PROJECT);
    }
    return launchRunner(modelsFactory.createRunner(runOptions));
  }
  @Before
  public void setUp() {
    when(resources.runnerInProgress()).thenReturn(svgResource);
    when(resources.runnerInQueue()).thenReturn(svgResource);
    when(resources.runnerFailed()).thenReturn(svgResource);
    when(resources.runnerTimeout()).thenReturn(svgResource);
    when(resources.runnerDone()).thenReturn(svgResource);
    when(resources.runnerDone()).thenReturn(svgResource);
    when(resources.erase()).thenReturn(svgResource);

    when(resources.runnerCss().whiteColor()).thenReturn(TEXT);

    when(itemWidget.getImagePanel()).thenReturn(imagePanel);

    runnerWidget = new RunnerWidget(itemWidget, resources, selectionManager);

    when(resources.runnerCss()).thenReturn(css);
    when(runner.getTitle()).thenReturn(TEXT);
    when(runner.getRAM()).thenReturn(512);
    when(runner.getCreationTime()).thenReturn(TEXT);

    runnerWidget.setDelegate(delegate);
  }
  /** {@inheritDoc} */
  @Override
  public void onRunButtonClicked() {
    if (State.TEMPLATE.equals(panelState.getState())) {
      RunOptions runOptions =
          dtoFactory
              .createDto(RunOptions.class)
              .withOptions(selectedEnvironment.getOptions())
              .withEnvironmentId(selectedEnvironment.getId())
              .withMemorySize(selectedEnvironment.getRam());

      Runner runner =
          modelsFactory.createRunner(
              runOptions, selectedEnvironment.getScope(), selectedEnvironment.getName());

      if (PROJECT.equals(selectedEnvironment.getScope())) {
        runner.setScope(PROJECT);
      }

      launchRunner(runner);
    } else {
      launchRunner();
    }
  }
 private void setDebugPort(Runner runner) {
   ApplicationProcessDescriptor runnerDescriptor = runner.getDescriptor();
   if (runnerDescriptor != null && runnerDescriptor.getDebugPort() != -1) {
     view.setDebugPort(String.valueOf(runnerDescriptor.getDebugPort()));
   }
 }
  @Test
  public void clickEventShouldBePerformedWhenRunnerStatusIsStopped() throws Exception {
    when(runner.getStatus()).thenReturn(STOPPED);

    verifyClickEvent();
  }
  @Test
  public void onMouseOverEventShouldBePerformedWhenRunnerStatusIsFailed() throws Exception {
    when(runner.getStatus()).thenReturn(FAILED);

    verifyMouseOverEvent();
  }