-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#208: adds basic TestFX support and an example test
- Loading branch information
1 parent
a2e6f3f
commit 6136153
Showing
6 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package app.logorrr | ||
|
||
import app.logorrr.testfx.TestFxSpec | ||
import javafx.scene.Scene | ||
import javafx.scene.control.Button | ||
import javafx.scene.layout.StackPane | ||
import javafx.stage.Stage | ||
import org.testfx.api.FxAssert | ||
import org.testfx.matcher.control.LabeledMatchers | ||
|
||
class HelloButtonSpec extends TestFxSpec { | ||
|
||
lazy val button = new Button("click me!") | ||
|
||
override def start(stage: Stage): Unit = { | ||
button.setId("myButton") | ||
button.setOnAction(_ => button.setText("clicked!")) | ||
stage.setScene(new Scene(new StackPane(button), 100, 100)) | ||
stage.show() | ||
} | ||
|
||
"foo" in { | ||
FxAssert.verifyThat(button, LabeledMatchers.hasText("click me!")) | ||
FxAssert.verifyThat("#myButton", LabeledMatchers.hasText("click me!")) | ||
FxAssert.verifyThat(".button", LabeledMatchers.hasText("click me!")) | ||
} | ||
"click on button" in { | ||
// when: | ||
clickOn(".button") | ||
|
||
// then: | ||
FxAssert.verifyThat(button, LabeledMatchers.hasText("clicked!")) | ||
// or (lookup by css id): | ||
FxAssert.verifyThat("#myButton", LabeledMatchers.hasText("clicked!")) | ||
// or (lookup by css class): | ||
FxAssert.verifyThat(".button", LabeledMatchers.hasText("clicked!")) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
app/src/test/scala/app/logorrr/testfx/ApplicationAdapter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package app.logorrr.testfx | ||
|
||
import javafx.application.Application | ||
import javafx.stage.Stage | ||
|
||
class ApplicationAdapter(val applicationFixture: ApplicationFixture) extends Application { | ||
|
||
@throws[Exception] | ||
override def init(): Unit = { | ||
applicationFixture.init() | ||
} | ||
|
||
@throws[Exception] | ||
override def start(primaryStage: Stage): Unit = { | ||
applicationFixture.start(primaryStage) | ||
} | ||
|
||
@throws[Exception] | ||
override def stop(): Unit = { | ||
applicationFixture.stop() | ||
} | ||
|
||
override def hashCode: Int = applicationFixture.hashCode | ||
|
||
override def equals(obj: Any): Boolean = applicationFixture == obj.asInstanceOf[ApplicationAdapter].applicationFixture | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
app/src/test/scala/app/logorrr/testfx/ApplicationFixture.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package app.logorrr.testfx | ||
|
||
import javafx.stage.Stage | ||
|
||
trait ApplicationFixture { | ||
@throws[Exception] | ||
def init(): Unit | ||
|
||
@throws[Exception] | ||
def start(stage: Stage): Unit | ||
|
||
@throws[Exception] | ||
def stop(): Unit | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package app.logorrr.testfx | ||
|
||
import javafx.application.{Application, HostServices, Preloader} | ||
import javafx.stage.Stage | ||
import org.scalatest.BeforeAndAfterAll | ||
import org.scalatest.wordspec.AnyWordSpecLike | ||
import org.scalatestplus.scalacheck.Checkers | ||
import org.testfx.api.{FxRobot, FxToolkit} | ||
|
||
abstract class TestFxSpec extends FxRobot with AnyWordSpecLike with Checkers with BeforeAndAfterAll with ApplicationFixture { | ||
me => | ||
|
||
override def beforeAll(): Unit = { | ||
// Initialize JavaFX Toolkit | ||
javafx.application.Platform.startup(() => {}) | ||
FxToolkit.registerPrimaryStage() | ||
FxToolkit.setupApplication(() => new ApplicationAdapter(me)) | ||
|
||
} | ||
|
||
override def afterAll(): Unit = { | ||
FxToolkit.cleanupAfterTest(me, new ApplicationAdapter(me)) | ||
} | ||
|
||
def launch(appClass: Class[_ <: Application], appArgs: String*): Application = { | ||
FxToolkit.registerPrimaryStage | ||
FxToolkit.setupApplication(appClass, appArgs: _*) | ||
} | ||
|
||
@throws[Exception] | ||
override def init(): Unit = {} | ||
|
||
@throws[Exception] | ||
override def start(stage: Stage): Unit = {} | ||
|
||
@throws[Exception] | ||
override def stop(): Unit = {} | ||
|
||
@deprecated def getHostServices: HostServices = throw new UnsupportedOperationException | ||
|
||
@deprecated def getParameters: Application.Parameters = throw new UnsupportedOperationException | ||
|
||
@deprecated def notifyPreloader(notification: Preloader.PreloaderNotification): Unit = { | ||
throw new UnsupportedOperationException | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters