Skip to content

Commit

Permalink
Added ability to stop Stirry and restart app afterwards
Browse files Browse the repository at this point in the history
  • Loading branch information
lunivore committed Jun 9, 2017
1 parent 5b39863 commit 854b1fe
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'com.lunivore'
version '0.0.1-20170509'
version '0.0.2-20170609'

buildscript {
ext.kotlinVersion = '1.1.1'
Expand Down
16 changes: 12 additions & 4 deletions src/main/kotlin/com/lunivore/stirry/Stirry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.lunivore.stirry
import javafx.application.Application
import javafx.application.Platform
import javafx.beans.value.ChangeListener
import javafx.embed.swing.JFXPanel
import javafx.event.ActionEvent
import javafx.event.EventHandler
import javafx.event.EventType
import javafx.scene.Node
import javafx.scene.Parent
import javafx.scene.control.Button
Expand All @@ -25,16 +25,19 @@ class Stirry {
private var stage: Stage? = null

fun initialize() {
JFXPanel() // Initialize platform
if (stage == null) {
Thread({ Application.launch(StageGrabbingApp::class.java) }).start()
Platform.runLater { StageGrabbingApp().start(Stage())}
Platform.setImplicitExit(false)
waitForPlatform()
}
stage = StageGrabbingApp.capturedStage()
}

private fun waitForPlatform() {
val queue = ArrayBlockingQueue<Boolean>(1)
Platform.runLater({ queue.put(true) })
queue.poll(10L, TimeUnit.SECONDS)
queue.poll(1L, TimeUnit.SECONDS)
}

fun rootNode(): Node? {
Expand All @@ -43,7 +46,7 @@ class Stirry {


fun startApp(app: Application) {
if (stage == null) throw IllegalStateException("Stirry must be initialized!")
if (stage == null) { initialize() }
Platform.runLater({ app.start(stage) })
waitForPlatform()
}
Expand Down Expand Up @@ -106,5 +109,10 @@ class Stirry {
waitForPlatform()
return result
}

fun stop() {
Platform.runLater{ stage?.close() }
waitForPlatform()
}
}
}
32 changes: 22 additions & 10 deletions src/test/kotlin/com/lunivore/stirry/StirryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ class StirryTest {

@Test
fun shouldBeAbleToFindAComponentWithPredicate() {
val button = find<Button>({b -> b is Button && b.text == "One"})
val button = find<Button>({ b -> b is Button && b.text == "One" })
assertTrue(button?.text == "One")
}

@Test
fun shouldBeAbleToClickAButton() {

// Given a button that's going to set a flag when it's clicked
var clicked : Boolean = false
val button = find<Button>({it is Button && it.text == "Two"})
var clicked: Boolean = false
val button = find<Button>({ it is Button && it.text == "Two" })
button?.onAction.also { clicked = true }

// When we ask Stirry to click the button for us
Stirry.buttonClick({it.text == "Two"})
Stirry.buttonClick({ it.text == "Two" })

// Then it should wait for the button to be clicked
assertTrue(clicked)
Expand All @@ -52,12 +52,12 @@ class StirryTest {
@Test
fun shouldBeAbleToEnterTextIntoATextBox() {
// Given a text box that's going to set a flag when text is entered
var textSet : Boolean = false
val textField = find<TextField>({it is TextField})
textField?.textProperty()?.addListener({it -> textSet = true })
var textSet: Boolean = false
val textField = find<TextField>({ it is TextField })
textField?.textProperty()?.addListener({ it -> textSet = true })

// When we ask Stirry to set the text for us
Stirry.setText({it is TextField}, "Hello!")
Stirry.setText({ it is TextField }, "Hello!")

// Then it should wait for the text to be set
assertTrue(textSet)
Expand All @@ -70,12 +70,24 @@ class StirryTest {
// and text has been put into the clipboard
val content = mutableMapOf<DataFormat, Any>()
content[DataFormat.PLAIN_TEXT] = "Clipped!"
Platform.runLater { Clipboard.getSystemClipboard().setContent(content)}
Platform.runLater { Clipboard.getSystemClipboard().setContent(content) }

// When we ask Stirry to get the text
val result = Stirry.getClipboard(DataFormat.PLAIN_TEXT)

// Then it should be able to recover it for us
assertEquals("Clipped!", result)
}
}

@Test
fun shouldBeAbleToStopStirryAndStartAgain() {
// Given the platform is initialized (which it is)

// When we stop Stirry
Stirry.stop()

// Then we should be able to start it again
Stirry.initialize()
Stirry.startApp(ExampleApp())
}
}

0 comments on commit 854b1fe

Please sign in to comment.