Commit 2867fdfd by likorn

Only the first answer is accepted

parent b1e5f2a7
...@@ -12,18 +12,16 @@ import androidx.core.view.get ...@@ -12,18 +12,16 @@ import androidx.core.view.get
import androidx.test.espresso.ViewAction import androidx.test.espresso.ViewAction
import androidx.test.espresso.UiController import androidx.test.espresso.UiController
import androidx.test.espresso.action.ViewActions.click import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.* import androidx.test.espresso.matcher.ViewMatchers.*
import junit.framework.Assert.assertTrue
import org.hamcrest.Matcher import org.hamcrest.Matcher
import org.junit.Before import org.junit.Before
class MainActivityTest { class MainActivityTest {
@get:Rule @get:Rule
val testRule = ActivityTestRule<MainActivity>(MainActivity::class.java) val testRule = ActivityTestRule<MainActivity>(MainActivity::class.java)
private lateinit var radioGroup: RadioGroup private lateinit var radioGroup: RadioGroup
@Before @Before
fun init() { fun init() {
radioGroup = testRule.activity.radio_group radioGroup = testRule.activity.radio_group
...@@ -34,7 +32,9 @@ class MainActivityTest { ...@@ -34,7 +32,9 @@ class MainActivityTest {
val correctAnswerIndex = getCorrectAnswerIndex() val correctAnswerIndex = getCorrectAnswerIndex()
val correctAnswerId = radioGroup[correctAnswerIndex].id val correctAnswerId = radioGroup[correctAnswerIndex].id
onView(withId(correctAnswerId)).perform(click()) onView(withId(correctAnswerId)).perform(click())
onView(withId(R.id.response)).check(matches(withText(R.string.response_correct)))
assertTrue(testRule.activity.supportFragmentManager.fragments.size == 1)
assertTrue(testRule.activity.supportFragmentManager.fragments[0] is ResponseCorrectFragment)
} }
@Test @Test
...@@ -43,9 +43,20 @@ class MainActivityTest { ...@@ -43,9 +43,20 @@ class MainActivityTest {
val wrongAnswerIndex = if (correctAnswerIndex == 3) 2 else 3 val wrongAnswerIndex = if (correctAnswerIndex == 3) 2 else 3
val wrongAnswerId = radioGroup[wrongAnswerIndex].id val wrongAnswerId = radioGroup[wrongAnswerIndex].id
onView(withId(wrongAnswerId)).perform(click()) onView(withId(wrongAnswerId)).perform(click())
onView(withId(R.id.response)).check(matches(withText(R.string.response_wrong)))
assertTrue(testRule.activity.supportFragmentManager.fragments.size == 1)
assertTrue(testRule.activity.supportFragmentManager.fragments[0] is ResponseWrongFragment)
} }
@Test
fun only_fist_answer_accepted() {
onView(withId(radioGroup[0].id)).perform(click())
assertTrue(testRule.activity.supportFragmentManager.fragments.size == 1)
onView(withId(radioGroup[1].id)).perform(click())
assertTrue(testRule.activity.supportFragmentManager.fragments.size == 1)
}
private fun getCorrectAnswerIndex(): Int { private fun getCorrectAnswerIndex(): Int {
val options = getOptions() val options = getOptions()
val correctAnswer = findSecondMax(options) val correctAnswer = findSecondMax(options)
......
...@@ -10,6 +10,7 @@ import kotlinx.android.synthetic.main.activity_main.* ...@@ -10,6 +10,7 @@ import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
var numberSet = NumberSet(4, 3) var numberSet = NumberSet(4, 3)
private var toAcceptAnswer = true
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
...@@ -29,6 +30,9 @@ class MainActivity : AppCompatActivity() { ...@@ -29,6 +30,9 @@ class MainActivity : AppCompatActivity() {
} }
private fun processAnswer(answer: Int) { private fun processAnswer(answer: Int) {
if (!toAcceptAnswer)
return
toAcceptAnswer = false
val responseFragment:Fragment = if (numberSet.isCorrect(answer)) { val responseFragment:Fragment = if (numberSet.isCorrect(answer)) {
ResponseCorrectFragment.newInstance() ResponseCorrectFragment.newInstance()
} else } else
......
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
app:srcCompat="@drawable/ic_double_arrow" /> app:srcCompat="@drawable/ic_double_arrow" />
<TextView <TextView
android:id="@+id/response"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="24dp" android:layout_marginStart="24dp"
......
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
app:layout_constraintStart_toStartOf="parent"> app:layout_constraintStart_toStartOf="parent">
<ImageButton <ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
...@@ -25,7 +24,6 @@ ...@@ -25,7 +24,6 @@
app:srcCompat="@drawable/ic_double_arrow" /> app:srcCompat="@drawable/ic_double_arrow" />
<TextView <TextView
android:id="@+id/response"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="24dp" android:layout_marginStart="24dp"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment