package app;

import javafx.scene.Node;
import javafx.stage.Stage;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.testfx.api.FxToolkit;
import org.testfx.framework.junit5.ApplicationTest;
import org.testfx.util.WaitForAsyncUtils;

import java.util.concurrent.TimeoutException;

public class FxTestTemplate extends ApplicationTest {

  private Stage stage;

  @BeforeEach
  public void runAppToTests() throws Exception {
    FxToolkit.registerPrimaryStage();
    FxToolkit.setupApplication(Main::new);
    FxToolkit.showStage();
    WaitForAsyncUtils.waitForFxEvents(100);
  }

  @AfterEach
  public void stopApp() throws TimeoutException {
    FxToolkit.cleanupStages();
  }

  @Override
  public void start(Stage primaryStage){
    this.stage = primaryStage;
    primaryStage.toFront();
  }

  public Stage getStage() {
    return stage;
  }

  public <T extends Node> T find(final String query) {
    /** TestFX provides many operations to retrieve elements from the loaded GUI. */
    return lookup(query).query();
  }
}