Class FxToolkit
- java.lang.Object
-
- org.testfx.api.FxToolkit
-
public final class FxToolkit extends java.lang.ObjectResponsible 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
-
-
Field Summary
Fields Modifier and Type Field Description private static ApplicationLauncherAPP_LAUNCHERprivate static ApplicationServiceAPP_SERVICEprivate static FxToolkitContextCONTEXT(package private) static java.lang.StringMISSING_LIBGTK_3_0_USER_MESSAGEprivate static ToolkitServiceSERVICE(package private) static java.lang.StringUNSUPPORTED_OPERATION_CALLING_CLASS(package private) static java.lang.StringUNSUPPORTED_OPERATION_ERROR_MESSAGE
-
Constructor Summary
Constructors Modifier Constructor Description privateFxToolkit()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcleanupAfterTest(FxRobot robot, javafx.application.Application application)static voidcleanupApplication(javafx.application.Application application)Performs the cleanup of the application.static voidcleanupInput(FxRobot robot)Runs on theJavaFX Application Thread: Releases remaining mouse and keyboard events.static voidcleanupStages()Runs on theJavaFX Application Thread: Hides all windows returned fromJavaVersionAdapter.getWindows()and returns once finished.(package private) static voidhandleCommonRuntimeExceptions(java.lang.RuntimeException exception)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 javafx.stage.StageregisterPrimaryStage()Sets up thePrimaryStageApplicationto use in tests, prevents it from shutting down when the last window is closed, and returns theStagefromApplication.start(Stage).static javafx.stage.StageregisterStage(java.util.function.Supplier<javafx.stage.Stage> stageSupplier)Runs the stageSupplier on theJavaFX Application Thread, registers the supplied stage, and returns that stage.static javafx.application.ApplicationsetupApplication(java.lang.Class<? extends javafx.application.Application> applicationClass, java.lang.String... applicationArgs)Sets up the given application with its given arguments and returns that application once finished.static javafx.application.ApplicationsetupApplication(java.util.function.Supplier<javafx.application.Application> applicationSupplier)Sets up the supplied application and returns that application once finished.static voidsetupFixture(java.lang.Runnable runnable)Runs the givenrunnableon theJavaFX Application Threadand returns once finished.static <T> TsetupFixture(java.util.concurrent.Callable<T> callable)Runs the givencallableon theJavaFX Application Threadand returns once finished.static javafx.scene.ScenesetupScene(java.util.function.Supplier<javafx.scene.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 javafx.scene.ParentsetupSceneRoot(java.util.function.Supplier<javafx.scene.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 javafx.stage.StagesetupStage(java.util.function.Consumer<javafx.stage.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.private static <T> TwaitForSetup(java.util.concurrent.Future<T> future)Waits for the given future to be set before returning or times out afterFxToolkitContext.getSetupTimeoutInMillis()is reached.
-
-
-
Field Detail
-
APP_LAUNCHER
private static final ApplicationLauncher APP_LAUNCHER
-
APP_SERVICE
private static final ApplicationService APP_SERVICE
-
CONTEXT
private static final FxToolkitContext CONTEXT
-
SERVICE
private static final ToolkitService SERVICE
-
UNSUPPORTED_OPERATION_ERROR_MESSAGE
static final java.lang.String UNSUPPORTED_OPERATION_ERROR_MESSAGE
- See Also:
- Constant Field Values
-
UNSUPPORTED_OPERATION_CALLING_CLASS
static final java.lang.String UNSUPPORTED_OPERATION_CALLING_CLASS
- See Also:
- Constant Field Values
-
MISSING_LIBGTK_3_0_USER_MESSAGE
static final java.lang.String MISSING_LIBGTK_3_0_USER_MESSAGE
- See Also:
- Constant Field Values
-
-
Method Detail
-
registerPrimaryStage
public static javafx.stage.Stage registerPrimaryStage() throws java.util.concurrent.TimeoutExceptionSets up thePrimaryStageApplicationto use in tests, prevents it from shutting down when the last window is closed, and returns theStagefromApplication.start(Stage).- Throws:
java.util.concurrent.TimeoutException- if execution is not finished beforeFxToolkitContext.getLaunchTimeoutInMillis()
-
registerStage
public static javafx.stage.Stage registerStage(java.util.function.Supplier<javafx.stage.Stage> stageSupplier) throws java.util.concurrent.TimeoutExceptionRuns the stageSupplier on theJavaFX Application Thread, registers the supplied stage, and returns that stage.- Throws:
java.util.concurrent.TimeoutException- if execution is not finished beforeFxToolkitContext.getSetupTimeoutInMillis()
-
setupStage
public static javafx.stage.Stage setupStage(java.util.function.Consumer<javafx.stage.Stage> stageConsumer) throws java.util.concurrent.TimeoutExceptionSets up the registered stage by passing it into the givenstageConsumeron theJavaFX Application Threadand returns the stage once finished.- Throws:
java.util.concurrent.TimeoutException- if execution is not finished beforeFxToolkitContext.getSetupTimeoutInMillis()
-
setupApplication
public static javafx.application.Application setupApplication(java.lang.Class<? extends javafx.application.Application> applicationClass, java.lang.String... applicationArgs) throws java.util.concurrent.TimeoutExceptionSets up the given application with its given arguments and returns that application once finished.- Throws:
java.util.concurrent.TimeoutException- if execution is not finished beforeFxToolkitContext.getSetupTimeoutInMillis()
-
setupApplication
public static javafx.application.Application setupApplication(java.util.function.Supplier<javafx.application.Application> applicationSupplier) throws java.util.concurrent.TimeoutExceptionSets up the supplied application and returns that application once finished.- Throws:
java.util.concurrent.TimeoutException- if execution is not finished beforeFxToolkitContext.getSetupTimeoutInMillis()
-
cleanupApplication
public static void cleanupApplication(javafx.application.Application application) throws java.util.concurrent.TimeoutExceptionPerforms the cleanup 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:
java.util.concurrent.TimeoutException- if cleanup is not finished beforeFxToolkitContext.getSetupTimeoutInMillis()or the FX Application Thread is not running
-
cleanupAfterTest
public static void cleanupAfterTest(FxRobot robot, javafx.application.Application application) throws java.util.concurrent.TimeoutException
- Throws:
java.util.concurrent.TimeoutException
-
setupScene
public static javafx.scene.Scene setupScene(java.util.function.Supplier<javafx.scene.Scene> sceneSupplier) throws java.util.concurrent.TimeoutExceptionRuns thesceneSupplieron theJavaFX Application Thread, sets the registered stage's scene to the supplied scene, and returns the supplied scene once finished.- Throws:
java.util.concurrent.TimeoutException- if execution is not finished beforeFxToolkitContext.getSetupTimeoutInMillis()
-
setupSceneRoot
public static javafx.scene.Parent setupSceneRoot(java.util.function.Supplier<javafx.scene.Parent> sceneRootSupplier) throws java.util.concurrent.TimeoutExceptionRuns 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:
java.util.concurrent.TimeoutException
-
setupFixture
public static void setupFixture(java.lang.Runnable runnable) throws java.util.concurrent.TimeoutExceptionRuns the givenrunnableon theJavaFX Application Threadand returns once finished.- Throws:
java.util.concurrent.TimeoutException
-
setupFixture
public static <T> T setupFixture(java.util.concurrent.Callable<T> callable) throws java.util.concurrent.TimeoutExceptionRuns the givencallableon theJavaFX Application Threadand returns once finished.- Throws:
java.util.concurrent.TimeoutException
-
showStage
public static void showStage() throws java.util.concurrent.TimeoutExceptionRuns on theJavaFX Application Thread: Shows the registered stage viaStage.show(), moves it to the front viaStage.toFront(), and returns once finished.- Throws:
java.util.concurrent.TimeoutException
-
hideStage
public static void hideStage() throws java.util.concurrent.TimeoutExceptionRuns on theJavaFX Application Thread: Hides the registered stage viaWindow.hide()and returns once finished.- Throws:
java.util.concurrent.TimeoutException
-
cleanupStages
public static void cleanupStages() throws java.util.concurrent.TimeoutExceptionRuns on theJavaFX Application Thread: Hides all windows returned fromJavaVersionAdapter.getWindows()and returns once finished.- Throws:
java.util.concurrent.TimeoutException
-
cleanupInput
public static void cleanupInput(FxRobot robot)
Runs on theJavaFX 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(java.util.concurrent.Future<T> future) throws java.util.concurrent.TimeoutExceptionWaits for the given future to be set before returning or times out afterFxToolkitContext.getSetupTimeoutInMillis()is reached.- Throws:
java.util.concurrent.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(java.lang.RuntimeException exception)
-
-