Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing the Stopwatch #15

Merged
merged 17 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ internal fun GameContent(component: GameComponent, modifier: Modifier = Modifier
Counter(
value = timer,
modifier = Modifier.weight(1f).semantics {
this.contentDescription = "Stopwatch, current time: $timer"
this.contentDescription = "Timer is on $timer"
this.role = Role.Image
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import com.arkivanov.decompose.DefaultComponentContext
import com.arkivanov.essenty.lifecycle.LifecycleRegistry
import com.arkivanov.essenty.lifecycle.resume
import com.arkivanov.mvikotlin.main.store.DefaultStoreFactory
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestCoroutineScheduler
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.time.Duration.Companion.seconds

@Suppress("TestFunctionName")
class DefaultGameComponentTest {
Expand All @@ -18,7 +18,7 @@ class DefaultGameComponentTest {
private val gameComponent = DefaultGameComponent(
componentContext = DefaultComponentContext(lifecycle = lifecycle),
storeFactory = DefaultStoreFactory(),
settings = GameSettings(),
settings = GameSettings(width = 20, height = 20, maxMines = 398),
mainCoroutineContext = StandardTestDispatcher(scheduler = coroutineScheduler)
)

b0r1ngx marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -31,10 +31,46 @@ class DefaultGameComponentTest {
}

@Test
fun WHEN_created_THEN_stopwatch_on_START() {
assertEquals(GameStatus.INITIALIZED, state.gameStatus)
fun GIVEN_created_WHEN_cell_clicked_THEN_status_STARTED() {
clickCellPrimary(x = 0, y = 0)

assertEquals(GameStatus.STARTED, state.gameStatus)
}

@Test
fun GIVEN_created_WHEN_cell_clicked_THEN_timer_shows_0() {
clickCellPrimary(x = 0, y = 0)

assertEquals(0, state.timer)
}

// TODO: Write more tests (need a bit dive in to work with TestCoroutineScheduler
}
@Test
fun GIVEN_game_started_WHEN_one_second_passed_THEN_timer_shows_1() {
clickCellPrimary(x = 0, y = 0)

coroutineScheduler.advanceTimeBy(1.seconds)
coroutineScheduler.runCurrent()

assertEquals(1, state.timer)
}

@Test
fun GIVEN_game_started_WHEN_click_on_mine_cell_THEN_timer_STOPS() {
clickCellPrimary(x = 0, y = 0)

coroutineScheduler.advanceTimeBy(10.seconds)
coroutineScheduler.runCurrent()

clickCellPrimary(x = 10, y = 10)
b0r1ngx marked this conversation as resolved.
Show resolved Hide resolved

coroutineScheduler.advanceTimeBy(10.seconds)
coroutineScheduler.runCurrent()

assertEquals(10, state.timer)
}

private fun clickCellPrimary(x: Int, y: Int) {
gameComponent.onCellTouchedPrimary(x = x, y = y)
gameComponent.onCellReleased(x = y, y = y)
}
}