- 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:
- Container fixtures, which are registered as
registeredStage. - Content fixtures, which are attached to the registered
registeredStage. - Individual fixtures, which do not require a
registeredStage.
1. Container Fixtures
They can be registered as
registeredStageand provide a top-level container, i.e.Stages.The primary stage can be registered as
registeredStageusingregisterPrimaryStage(). This call is mandatory before any other JavaFX fixture can be created.Other stages can be registered as
registeredStageusingregisterStage(Supplier<Stage>).2. Content Fixtures
They can be attached to the
registeredStage.Either constructed by calling an
Application.start(), by supplyingScenes,Parents, or by consuming aStage.Use:
setupStage(Consumer<Stage>),setupApplication(Class<? extends Application>),setupScene(Supplier<Scene>)orsetupSceneRoot(Supplier<Parent>)3. Individual Fixtures
To setup individual Stages, Scenes or Nodes use
setupFixture(Runnable)andsetupFixture(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.
- Container fixtures, which are registered as
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcleanupApplication(Application application)Performs the clean up of the application.static voidcleanupStages()Runs on theJavaFX Application Thread: Hides all windows returned fromJavaVersionAdapter.getWindows()and returns once finished.static voidhideStage()Runs on theJavaFX Application Thread: Hides the registered stage viaWindow.hide()and returns once finished.static booleanisFXApplicationThreadRunning()Detects if the JavaFx Application Thread is currently running.static StageregisterPrimaryStage()Sets up thePrimaryStageApplicationto use in tests, prevents it from shutting down when the last window is closed, and returns theStagefromApplication.start(Stage).static StageregisterStage(Supplier<Stage> stageSupplier)Runs the stageSupplier on theJavaFX Application Thread, registers the supplied stage, and returns that stage.static ApplicationsetupApplication(Class<? extends Application> applicationClass, String... applicationArgs)Sets up the given application with its given arguments and returns that application once finished.static ApplicationsetupApplication(Supplier<Application> applicationSupplier)Sets up the supplied application and returns that application once finished.static voidsetupFixture(Runnable runnable)Runs the givenrunnableon theJavaFX Application Threadand returns once finished.static <T> TsetupFixture(Callable<T> callable)Runs the givencallableon theJavaFX Application Threadand returns once finished.static ScenesetupScene(Supplier<Scene> sceneSupplier)Runs thesceneSupplieron theJavaFX Application Thread, sets the registered stage's scene to the supplied scene, and returns the supplied scene once finished.static ParentsetupSceneRoot(Supplier<Parent> sceneRootSupplier)Runs thesceneRootSupplieron theJavaFX Application Thread, sets the registered stage's scene's root node to the supplied root node, and returns the supplied root node once finished.static StagesetupStage(Consumer<Stage> stageConsumer)Sets up the registered stage by passing it into the givenstageConsumeron theJavaFX Application Threadand returns the stage once finished.static voidshowStage()Runs on theJavaFX Application Thread: Shows the registered stage viaStage.show(), moves it to the front viaStage.toFront(), and returns once finished.static FxToolkitContexttoolkitContext()Returns the internal context.
-
-
-
Method Detail
-
registerPrimaryStage
public static Stage registerPrimaryStage() throws TimeoutException
Sets up thePrimaryStageApplicationto use in tests, prevents it from shutting down when the last window is closed, and returns theStagefromApplication.start(Stage).- Throws:
TimeoutException- if execution is not finished beforeFxToolkitContext.getLaunchTimeoutInMillis()
-
registerStage
public static Stage registerStage(Supplier<Stage> stageSupplier) throws TimeoutException
Runs the stageSupplier on theJavaFX Application Thread, registers the supplied stage, and returns that stage.- Throws:
TimeoutException- if execution is not finished beforeFxToolkitContext.getSetupTimeoutInMillis()
-
setupStage
public static Stage setupStage(Consumer<Stage> stageConsumer) throws TimeoutException
Sets up the registered stage by passing it into the givenstageConsumeron theJavaFX Application Threadand returns the stage once finished.- Throws:
TimeoutException- if execution is not finished beforeFxToolkitContext.getSetupTimeoutInMillis()
-
setupApplication
public static Application setupApplication(Class<? extends 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 beforeFxToolkitContext.getSetupTimeoutInMillis()
-
setupApplication
public static Application setupApplication(Supplier<Application> applicationSupplier) throws TimeoutException
Sets up the supplied application and returns that application once finished.- Throws:
TimeoutException- if execution is not finished beforeFxToolkitContext.getSetupTimeoutInMillis()
-
cleanupApplication
public static void cleanupApplication(Application application) throws TimeoutException
Performs the clean up of the application. This is done by callingToolkitService.cleanupApplication(Application)(which usually calls thestopmethod of the application).- Parameters:
application- the application to clean up- Throws:
TimeoutException- if cleanup is not finished beforeFxToolkitContext.getSetupTimeoutInMillis()or the FX Application Thread is not running
-
setupScene
public static Scene setupScene(Supplier<Scene> sceneSupplier) throws TimeoutException
Runs thesceneSupplieron theJavaFX 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 beforeFxToolkitContext.getSetupTimeoutInMillis()
-
setupSceneRoot
public static Parent setupSceneRoot(Supplier<Parent> sceneRootSupplier) throws TimeoutException
Runs thesceneRootSupplieron theJavaFX 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 givenrunnableon theJavaFX Application Threadand returns once finished.- Throws:
TimeoutException
-
setupFixture
public static <T> T setupFixture(Callable<T> callable) throws TimeoutException
Runs the givencallableon theJavaFX Application Threadand returns once finished. before returning.- Throws:
TimeoutException
-
showStage
public static void showStage() throws TimeoutExceptionRuns on theJavaFX Application Thread: Shows the registered stage viaStage.show(), moves it to the front viaStage.toFront(), and returns once finished.- Throws:
TimeoutException
-
hideStage
public static void hideStage() throws TimeoutExceptionRuns on theJavaFX Application Thread: Hides the registered stage viaWindow.hide()and returns once finished.- Throws:
TimeoutException
-
cleanupStages
public static void cleanupStages() throws TimeoutExceptionRuns on theJavaFX Application Thread: Hides all windows returned fromJavaVersionAdapter.getWindows()and returns once finished.- Throws:
TimeoutException
-
toolkitContext
public static FxToolkitContext toolkitContext()
Returns the internal context.
-
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
-
-