Commit c3d96ce6 by likorn

Updated tests for MainActivity

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