Commit c3d96ce6 by likorn

Updated tests for MainActivity

parent 0888956b
package com.example.quickmax package com.example.quickmax
import android.view.View import android.view.View
import android.widget.RadioGroup
import androidx.test.espresso.Espresso.onView import androidx.test.espresso.Espresso.onView
import androidx.test.rule.ActivityTestRule import androidx.test.rule.ActivityTestRule
import kotlinx.android.synthetic.main.activity_main.*
import org.junit.Rule import org.junit.Rule
import org.junit.Test import org.junit.Test
import android.widget.TextView import android.widget.TextView
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
...@@ -21,18 +18,23 @@ import org.junit.Before ...@@ -21,18 +18,23 @@ 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, true, true)
// private lateinit var radioGroup: RadioGroup
private lateinit var answers: Map<Int, Int>
@Before @Before
fun init() { fun init() {
// radioGroup = testRule.activity.radio_group answers = mapOf(
getText(withId(R.id.btn_left_top)).toInt() to R.id.btn_left_top,
getText(withId(R.id.btn_right_top)).toInt() to R.id.btn_right_top,
getText(withId(R.id.btn_left_bottom)).toInt() to R.id.btn_left_bottom,
getText(withId(R.id.btn_right_bottom)).toInt() to R.id.btn_right_bottom
)
} }
@Test @Test
fun correct_answer_marked_as_correct() { fun correct_answer_marked_as_correct() {
val correctAnswerIndex = getCorrectAnswerIndex() val correctAnswerId = getCorrectAnswerId()
val correctAnswerId = radioGroup[correctAnswerIndex].id
onView(withId(correctAnswerId)).perform(click()) onView(withId(correctAnswerId)).perform(click())
onView(withId(R.id.tv_response)) onView(withId(R.id.tv_response))
.check(matches(withText(testRule.activity.resources.getString(R.string.response_correct)))) .check(matches(withText(testRule.activity.resources.getString(R.string.response_correct))))
...@@ -40,9 +42,7 @@ class MainActivityTest { ...@@ -40,9 +42,7 @@ class MainActivityTest {
@Test @Test
fun wrong_answer_marked_as_wrong() { fun wrong_answer_marked_as_wrong() {
val correctAnswerIndex = getCorrectAnswerIndex() val wrongAnswerId = getWrongAnswerId()
val wrongAnswerIndex = if (correctAnswerIndex == 3) 2 else 3
val wrongAnswerId = radioGroup[wrongAnswerIndex].id
onView(withId(wrongAnswerId)).perform(click()) onView(withId(wrongAnswerId)).perform(click())
onView(withId(R.id.tv_response)) onView(withId(R.id.tv_response))
.check(matches(withText(testRule.activity.resources.getString(R.string.response_wrong)))) .check(matches(withText(testRule.activity.resources.getString(R.string.response_wrong))))
...@@ -50,9 +50,9 @@ class MainActivityTest { ...@@ -50,9 +50,9 @@ class MainActivityTest {
@Test @Test
fun only_fist_answer_accepted() { fun only_fist_answer_accepted() {
onView(withId(radioGroup[0].id)).perform(click()) onView(withId(R.id.btn_right_bottom)).perform(click())
assertTrue(testRule.activity.supportFragmentManager.fragments.size == 1) assertTrue(testRule.activity.supportFragmentManager.fragments.size == 1)
onView(withId(radioGroup[1].id)).perform(click()) onView(withId(R.id.btn_left_bottom)).perform(click())
assertTrue(testRule.activity.supportFragmentManager.fragments.size == 1) assertTrue(testRule.activity.supportFragmentManager.fragments.size == 1)
} }
...@@ -71,17 +71,17 @@ class MainActivityTest { ...@@ -71,17 +71,17 @@ class MainActivityTest {
f -> f is TimeIsOverFragment }) f -> f is TimeIsOverFragment })
} }
private fun getCorrectAnswerIndex(): Int { private fun getCorrectAnswerId(): Int {
val options = getOptions() val correctAnswer = findSecondMax(answers.keys)
val correctAnswer = findSecondMax(options) return answers.getValue(correctAnswer)
return options.indexOf(correctAnswer)
} }
private fun getOptions(): MutableList<Int> { private fun getWrongAnswerId(): Int {
val options = mutableListOf<Int>() val correctAnswer = findSecondMax(answers.keys)
for (i in 0 until radioGroup.childCount) for (answer in answers)
options.add(getText(withId(radioGroup[i].id)).toInt()) if (answer.key != correctAnswer)
return options return answer.value
return answers.getValue(-1)
} }
private fun getText(matcher: Matcher<View>): String { private fun getText(matcher: Matcher<View>): String {
......
...@@ -3,7 +3,7 @@ package com.example.quickmax ...@@ -3,7 +3,7 @@ package com.example.quickmax
class AnswerSet(numDigits: Int): Iterable<Answer> { class AnswerSet(numDigits: Int): Iterable<Answer> {
val buttonIds = listOf(R.id.btn_left_top, R.id.btn_right_top, R.id.btn_left_bottom, R.id.btn_right_bottom) private val buttonIds = listOf(R.id.btn_left_top, R.id.btn_right_top, R.id.btn_left_bottom, R.id.btn_right_bottom)
private val numAnswers = 4 private val numAnswers = 4
private lateinit var correctAnswer: Answer private lateinit var correctAnswer: Answer
......
...@@ -9,14 +9,14 @@ fun generateRandom(numDigits: Int): Int { ...@@ -9,14 +9,14 @@ fun generateRandom(numDigits: Int): Int {
} }
fun generateNRandomNumbers(numDigits: Int, n: Int): Set<Int> { fun generateNRandomNumbers(numDigits: Int, n: Int): Set<Int> {
val randomNumbers = setOf<Int>() val randomNumbers = mutableSetOf<Int>()
while (randomNumbers.size < n) { while (randomNumbers.size < n) {
randomNumbers.plus(generateRandom(numDigits)) randomNumbers.add(generateRandom(numDigits))
} }
return randomNumbers return randomNumbers
} }
fun findSecondMax(numbers: List<Int>): Int { fun findSecondMax(numbers: Collection<Int>): Int {
var firstMax = 0 var firstMax = 0
var secondMax = 0 var secondMax = 0
......
...@@ -2,25 +2,23 @@ package com.example.quickmax ...@@ -2,25 +2,23 @@ package com.example.quickmax
import android.os.Bundle import android.os.Bundle
import android.os.CountDownTimer import android.os.CountDownTimer
import android.view.View
import android.widget.Button import android.widget.Button
import android.widget.RadioButton
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
private var answerSet = AnswerSet(4) private lateinit var answerSet: AnswerSet
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
addRadioButtons() answerSet = AnswerSet(3)
setUpAnswerButtons()
timer.start() timer.start()
} }
private fun addRadioButtons() { private fun setUpAnswerButtons() {
for (answer in answerSet) { for (answer in answerSet) {
findViewById<Button>(answer.buttonId).text = answer.value.toString() findViewById<Button>(answer.buttonId).text = answer.value.toString()
findViewById<Button>(answer.buttonId).setOnClickListener { processAnswer(answer.correct) } findViewById<Button>(answer.buttonId).setOnClickListener { processAnswer(answer.correct) }
......
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