Class FxToolkit

java.lang.Object
org.testfx.api.FxToolkit

public final class FxToolkit extends Object
Responsible for setup and cleanup of JavaFX fixtures that need the JavaFX thread.

Overview

This class methods cover three different kinds of fixtures:

  1. Container fixtures, which are registered as registeredStage.
  2. Content fixtures, which are attached to the registered registeredStage.
  3. Individual fixtures, which do not require a registeredStage.
Additionally, it keeps an internal context.

1. Container Fixtures

They can be registered as registeredStage and provide a top-level container, i.e. Stages.

The primary stage can be registered as registeredStage using registerPrimaryStage(). This call is mandatory before any other JavaFX fixture can be created.

Other stages can be registered as registeredStage using registerStage(Supplier<Stage>).

2. Content Fixtures

They can be attached to the registeredStage.

Either constructed by calling an Application.start(), by supplying Scenes, Parents, or by consuming a Stage.

Use: setupStage(Consumer<Stage>), setupApplication(Class<? extends Application>), setupScene(Supplier<Scene>) or setupSceneRoot(Supplier<Parent>)

3. Individual Fixtures

To setup individual Stages, Scenes or Nodes use setupFixture(Runnable) and setupFixture(Callable).

Internal Context

Is internally responsible for handle the registered Stage for attachments, handle timeouts, provide the Application for the Toolkit launch and execute the setup in the JavaFX thread. The primary Stage is constructed by the platform.

  • Field Details

  • Constructor Details

    • FxToolkit

      private FxToolkit()
  • Method Details

    • registerPrimaryStage

      public static javafx.stage.Stage registerPrimaryStage() throws TimeoutException
      Sets up the PrimaryStageApplication to use in tests, prevents it from shutting down when the last window is closed, and returns the Stage from Application.start(Stage).
      Throws:
      TimeoutException - if execution is not finished before FxToolkitContext.getLaunchTimeoutInMillis()
    • registerStage

      public static javafx.stage.Stage registerStage(Supplier<javafx.stage.Stage> stageSupplier) throws TimeoutException
      Runs the stageSupplier on the JavaFX Application Thread, registers the supplied stage, and returns that stage.
      Throws:
      TimeoutException - if execution is not finished before FxToolkitContext.getSetupTimeoutInMillis()
    • setupStage

      public static javafx.stage.Stage setupStage(Consumer<javafx.stage.Stage> stageConsumer) throws TimeoutException
      Sets up the registered stage by passing it into the given stageConsumer on the JavaFX Application Thread and returns the stage once finished.
      Throws:
      TimeoutException - if execution is not finished before FxToolkitContext.getSetupTimeoutInMillis()
    • setupApplication

      public static javafx.application.Application setupApplication(Class<? extends javafx.application.Application> applicationClass, String... applicationArgs) throws TimeoutException
      Sets up the given application with its given arguments and returns that application once finished.
      Throws:
      TimeoutException - if execution is not finished before FxToolkitContext.getSetupTimeoutInMillis()
    • setupApplication

      public static javafx.application.Application setupApplication(Supplier<javafx.application.Application> applicationSupplier) throws TimeoutException
      Sets up the supplied application and returns that application once finished.
      Throws:
      TimeoutException - if execution is not finished before FxToolkitContext.getSetupTimeoutInMillis()
    • cleanupApplication

      public static void cleanupApplication(javafx.application.Application application) throws TimeoutException
      Performs the cleanup of the application. This is done by calling ToolkitService.cleanupApplication(Application) (which usually calls the stop method of the application).
      Parameters:
      application - the application to clean up
      Throws:
      TimeoutException - if cleanup is not finished before FxToolkitContext.getSetupTimeoutInMillis() or the FX Application Thread is not running
    • cleanupAfterTest

      public static void cleanupAfterTest(FxRobot robot, javafx.application.Application application) throws TimeoutException
      Throws:
      TimeoutException
    • setupScene

      public static javafx.scene.Scene setupScene(Supplier<javafx.scene.Scene> sceneSupplier) throws TimeoutException
      Runs the sceneSupplier on the JavaFX Application Thread, sets the registered stage's scene to the supplied scene, and returns the supplied scene once finished.
      Throws:
      TimeoutException - if execution is not finished before FxToolkitContext.getSetupTimeoutInMillis()
    • setupSceneRoot

      public static javafx.scene.Parent setupSceneRoot(Supplier<javafx.scene.Parent> sceneRootSupplier) throws TimeoutException
      Runs the sceneRootSupplier on the JavaFX Application Thread, sets the registered stage's scene's root node to the supplied root node, and returns the supplied root node once finished.
      Throws:
      TimeoutException
    • setupFixture

      public static void setupFixture(Runnable runnable) throws TimeoutException
      Runs the given runnable on the JavaFX Application Thread and returns once finished.
      Throws:
      TimeoutException
    • setupFixture

      public static <T> T setupFixture(Callable<T> callable) throws TimeoutException
      Runs the given callable on the JavaFX Application Thread and returns once finished.
      Throws:
      TimeoutException
    • showStage

      public static void showStage() throws TimeoutException
      Runs on the JavaFX Application Thread: Shows the registered stage via Stage.show(), moves it to the front via Stage.toFront(), and returns once finished.
      Throws:
      TimeoutException
    • hideStage

      public static void hideStage() throws TimeoutException
      Runs on the JavaFX Application Thread: Hides the registered stage via Window.hide() and returns once finished.
      Throws:
      TimeoutException
    • cleanupStages

      public static void cleanupStages() throws TimeoutException
      Runs on the JavaFX Application Thread: Hides all windows returned from JavaVersionAdapter.getWindows() and returns once finished.
      Throws:
      TimeoutException
    • cleanupInput

      public static void cleanupInput(FxRobot robot)
      Runs on the JavaFX Application Thread: Releases remaining mouse and keyboard events. Not cleaning these events may have side effects on the next UI tests
    • toolkitContext

      public static FxToolkitContext toolkitContext()
      Returns the internal context.
    • waitForSetup

      private static <T> T waitForSetup(Future<T> future) throws TimeoutException
      Waits for the given future to be set before returning or times out after FxToolkitContext.getSetupTimeoutInMillis() is reached.
      Throws:
      TimeoutException
    • isFXApplicationThreadRunning

      public static boolean isFXApplicationThreadRunning()
      Detects if the JavaFx Application Thread is currently running.
      Returns:
      true if the FX Application Thread is running, false otherwise
    • handleCommonRuntimeExceptions

      static void handleCommonRuntimeExceptions(RuntimeException exception)