1
+ package dev.gressier.pennydrop.fragments
2
+
3
+ import androidx.test.espresso.Espresso.onView
4
+ import androidx.test.espresso.action.ViewActions.click
5
+ import androidx.test.espresso.assertion.PositionAssertions.isCompletelyBelow
6
+ import androidx.test.espresso.assertion.ViewAssertions.matches
7
+ import androidx.test.espresso.matcher.ViewMatchers.*
8
+ import androidx.test.ext.junit.rules.activityScenarioRule
9
+ import dev.gressier.pennydrop.MainActivity
10
+ import dev.gressier.pennydrop.R
11
+ import dev.gressier.pennydrop.startSampleGame
12
+ import kotlinx.coroutines.ExperimentalCoroutinesApi
13
+ import kotlinx.coroutines.test.runBlockingTest
14
+ import org.hamcrest.Matchers.*
15
+ import org.junit.Assert.assertNotNull
16
+ import org.junit.Assert.assertNull
17
+ import org.junit.Before
18
+ import org.junit.Rule
19
+ import org.junit.Test
20
+ import dev.gressier.pennydrop.isActivated as isLastRolled
21
+
22
+ @ExperimentalCoroutinesApi
23
+ class GameFragmentTest {
24
+
25
+ @get:Rule var activityScenarioRule = activityScenarioRule<MainActivity >()
26
+
27
+ private val coinSlotMap = listOf (
28
+ R .id.coinSlot1, R .id.coinSlot2, R .id.coinSlot3, R .id.coinSlot4, R .id.coinSlot5, R .id.coinSlot6,
29
+ )
30
+ .mapIndexed { i, it -> " ${i + 1 } " to it }
31
+ .toMap()
32
+
33
+ @Before fun startNewGame () =
34
+ runBlockingTest {
35
+ startSampleGame(activityScenarioRule.scenario)
36
+ }
37
+
38
+ @Test fun Check_starting_slots () {
39
+ coinSlotMap.forEach { slotNumber, slotId ->
40
+ onView(
41
+ allOf(withId(R .id.slotNumberCoinSlot), withParent(withId(slotId)))
42
+ ).check(matches(withText(slotNumber)))
43
+
44
+ onView(
45
+ allOf(withId(R .id.coinImageCoinSlot), withParent(withId(slotId)))
46
+ ).check(matches(not (isDisplayed())))
47
+
48
+ if (slotId != R .id.coinSlot6)
49
+ onView(withId(R .id.coinSlot6)).check(isCompletelyBelow(withId(slotId)))
50
+ }
51
+ }
52
+
53
+ @Test fun Check_single_roll_result () =
54
+ runBlockingTest {
55
+ onView(withId(R .id.rollButton)).perform(click())
56
+
57
+ onView(withId(R .id.textCurrentPlayerName)).check(matches(withText(" Alex" )))
58
+ onView(withId(R .id.textCurrentPlayerCoinsLeft)).check(matches(withText(" 9" )))
59
+
60
+ onView(
61
+ allOf(
62
+ withId(R .id.bottomViewCoinSlot),
63
+ isLastRolled(),
64
+ anyOf(
65
+ hasSibling(allOf(withId(R .id.coinImageCoinSlot), isDisplayed())),
66
+ hasSibling(allOf(withId(R .id.slotNumberCoinSlot), withText(" 6" ))),
67
+ ),
68
+ )
69
+ ).check { view, noViewFoundException ->
70
+ assertNull(noViewFoundException)
71
+ assertNotNull(view)
72
+ }
73
+ }
74
+ }
0 commit comments