Skip to content

Commit

Permalink
tutorial panel tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Apr 10, 2024
1 parent 3a3cdd4 commit b09f707
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/main/kotlin/dartzee/screen/game/TutorialPanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ class TutorialPanel(private val parent: DartsGameScreen) :
lblScoredText.setFontSize(24)
lblRemaining.setFontSize(18)
lblScored.setFontSize(18)
lblRemaining.name = "RemainingLabel"
lblScored.name = "ScoredLabel"

dartboard.addDartboardListener(this)
btnStartGame.addActionListener(this)
Expand Down
7 changes: 4 additions & 3 deletions src/test/kotlin/dartzee/e2e/GameHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import io.kotest.matchers.types.shouldBeInstanceOf
import io.mockk.mockk
import io.mockk.verifySequence
import javax.swing.JButton
import javax.swing.JPanel
import javax.swing.JToggleButton
import javax.swing.border.LineBorder

Expand Down Expand Up @@ -109,13 +110,13 @@ fun GameSetupPlayerSelector.selectTopPlayer() {
clickButton("Select")
}

fun DartsGamePanel<*, *>.throwHumanRound(vararg darts: Dart) {
fun JPanel.throwHumanRound(vararg darts: Dart) {
darts.forEach { throwHumanDart(it.score, it.segmentType) }

confirmRound()
}

fun DartsGamePanel<*, *>.throwHumanDart(score: Int, segmentType: SegmentType) {
fun JPanel.throwHumanDart(score: Int, segmentType: SegmentType) {
val computedPt =
if (segmentType == SegmentType.MISS) AI_DARTBOARD.getDeliberateMissPoint()
else {
Expand All @@ -126,7 +127,7 @@ fun DartsGamePanel<*, *>.throwHumanDart(score: Int, segmentType: SegmentType) {
getChild<GameplayDartboard>().dartThrown(computedPt)
}

fun DartsGamePanel<*, *>.confirmRound() {
fun JPanel.confirmRound() {
clickChild<JButton> { it.toolTipText == "Confirm round" }
}

Expand Down
96 changes: 94 additions & 2 deletions src/test/kotlin/dartzee/screen/game/TestTutorialPanel.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
package dartzee.screen.game

import dartzee.clickButton
import com.github.alyssaburlton.swingtest.findAll
import com.github.alyssaburlton.swingtest.getChild
import com.github.alyssaburlton.swingtest.shouldBeDisabled
import com.github.alyssaburlton.swingtest.shouldBeEnabled
import dartzee.*
import dartzee.bean.DartLabel
import dartzee.e2e.throwHumanDart
import dartzee.e2e.throwHumanRound
import dartzee.helper.AbstractTest
import dartzee.`object`.SegmentType
import dartzee.screen.GameplayDartboard
import io.kotest.matchers.collections.shouldBeEmpty
import io.kotest.matchers.collections.shouldNotBeEmpty
import io.kotest.matchers.shouldBe
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Test
import java.awt.Dimension
import javax.swing.JButton
import javax.swing.JLabel

class TestTutorialPanel: AbstractTest() {
@Test
Expand All @@ -13,11 +28,88 @@ class TestTutorialPanel: AbstractTest() {
val panel = TutorialPanel(parentWindow)
panel.clickButton(text = "I'm ready - let's play!")

verify { parentWindow.startNewGame() }
verify { parentWindow.tutorialFinished() }
}

@Test
fun `Should enable round controls appropriately`() {
val panel = TutorialPanel(mockk(relaxed = true))

val confirmButton = panel.getChild<JButton> { it.toolTipText == "Confirm round" }
val resetButton = panel.getChild<JButton> { it.toolTipText == "Reset round" }
confirmButton.shouldBeDisabled()
resetButton.shouldBeDisabled()

panel.throwHumanDart(20, SegmentType.TREBLE)
confirmButton.shouldBeEnabled()
resetButton.shouldBeEnabled()

resetButton.doClick()
confirmButton.shouldBeDisabled()
resetButton.shouldBeDisabled()

panel.throwHumanDart(20, SegmentType.TREBLE)
confirmButton.doClick()
confirmButton.shouldBeDisabled()
resetButton.shouldBeDisabled()
}

@Test
fun `Should reset darts thrown`() {
val panel = TutorialPanel(mockk(relaxed = true))
val dartboard = panel.getChild<GameplayDartboard>()
dartboard.size = Dimension(500, 500)

panel.throwHumanDart(20, SegmentType.TREBLE)
panel.throwHumanDart(20, SegmentType.OUTER_SINGLE)
dartboard.findAll<DartLabel>().shouldNotBeEmpty()

panel.clickButton { it.toolTipText == "Reset round" }
dartboard.findAll<DartLabel>().shouldBeEmpty()

panel.getChild<JLabel>("ScoredLabel").text shouldBe "0"
}

@Test
fun `Should accurately track the score of the darts thrown`() {
val panel = TutorialPanel(mockk(relaxed = true))
val scoredLabel = panel.getChild<JLabel>("ScoredLabel")

panel.throwHumanDart(20, SegmentType.TREBLE)
scoredLabel.text shouldBe "60"

panel.throwHumanDart(20, SegmentType.OUTER_SINGLE)
scoredLabel.text shouldBe "T20 + 20 = 80"

panel.clickButton { it.toolTipText == "Confirm round" }
scoredLabel.text shouldBe "0"
}

@Test
fun `Should accurately record busts in the demo`() {
val panel = TutorialPanel(mockk(relaxed = true))

panel.throwHumanRound(drtTrebleTwenty(), drtDoubleTwenty(), drtOuterTwenty()) // 181
panel.throwHumanRound(drtTrebleTwenty(), drtDoubleTwenty(), drtDoubleTwenty()) // 41

val label = panel.getChild<JLabel>("RemainingLabel")
label.text shouldBe "41"

panel.throwHumanRound(drtOuterTwenty(), drtOuterTwenty(), drtOuterTwo())
label.text shouldBe "41"
}

@Test
fun `Should accurately record finishes in the demo`() {
val panel = TutorialPanel(mockk(relaxed = true))

panel.throwHumanRound(drtTrebleTwenty(), drtDoubleTwenty(), drtOuterTwenty()) // 181
panel.throwHumanRound(drtTrebleTwenty(), drtDoubleTwenty(), drtDoubleTwenty()) // 41

val label = panel.getChild<JLabel>("RemainingLabel")
label.text shouldBe "41"

panel.throwHumanRound(drtOuterTwenty(), drtOuterTwenty(), drtOuterOne())
label.text shouldBe "You win!"
}
}

0 comments on commit b09f707

Please sign in to comment.