Commit d6d9a2b4 by likorn

Selected number of digits is stored in memory

parent 2c5fa167
package com.example.quickmax
import android.view.View
import androidx.test.espresso.Espresso.onView
import androidx.test.rule.ActivityTestRule
import org.junit.Rule
import org.junit.Test
import android.widget.TextView
import androidx.test.espresso.ViewAction
import androidx.test.espresso.UiController
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import com.example.quickmax.answers.findSecondMax
import junit.framework.Assert.assertNotNull
import junit.framework.Assert.assertTrue
import org.hamcrest.Matcher
import org.junit.Before
class TaskActivityTest {
@get:Rule
val testRule = ActivityTestRule<TaskActivity>(TaskActivity::class.java, true, true)
private lateinit var answers: Map<Int, Int>
@Before
fun init() {
answers = mapOf(
getText(withId(R.id.card_left_top)).toInt() to R.id.card_left_top,
getText(withId(R.id.card_right_top)).toInt() to R.id.card_right_top,
getText(withId(R.id.card_left_bottom)).toInt() to R.id.card_left_bottom,
getText(withId(R.id.card_right_bottom)).toInt() to R.id.card_right_bottom
)
}
@Test
fun correct_answer_marked_as_correct() {
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))))
}
@Test
fun wrong_answer_marked_as_wrong() {
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))))
}
@Test
fun only_fist_answer_accepted() {
onView(withId(R.id.card_right_bottom)).perform(click())
assertTrue(testRule.activity.supportFragmentManager.fragments.size == 1)
onView(withId(R.id.card_left_bottom)).perform(click())
assertTrue(testRule.activity.supportFragmentManager.fragments.size == 1)
}
@Test
fun time_decreases() {
val time1 = getText(withId(R.id.tv_time_left)).toInt()
Thread.sleep(1000)
val time2 = getText(withId(R.id.tv_time_left)).toInt()
assertTrue(time2 < time1)
}
@Test
fun fragment_when_time_is_over() {
Thread.sleep(4000)
assertNotNull(testRule.activity.supportFragmentManager.fragments.find {
f -> f is TimeIsOverFragment })
}
private fun getCorrectAnswerId(): Int {
val correctAnswer = findSecondMax(answers.keys)
return answers.getValue(correctAnswer)
}
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 {
var text = ""
onView(matcher).perform(object : ViewAction {
override fun getConstraints(): Matcher<View> {
return isAssignableFrom(TextView::class.java)
}
override fun getDescription(): String { return "" }
override fun perform(uiController: UiController, view: View) {
val tv = view as TextView
text = tv.text.toString()
}
})
return text
}
}
\ No newline at end of file
...@@ -5,33 +5,47 @@ import android.content.Intent ...@@ -5,33 +5,47 @@ import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.card.MaterialCardView
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
class MainActivity: AppCompatActivity() { class MainActivity : AppCompatActivity() {
private var secToSolve: Int = 4 private var secToSolve: Int = 4
private var numDigits: Int = 3
private lateinit var checkedCard: MaterialCardView
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)
secToSolve = retrieveSecondsToSolve() retrieveSharedPrefs()
seek_bar.setMinStartValue(secToSolve.toFloat()).apply() seek_bar.setMinStartValue(secToSolve.toFloat()).apply()
seek_bar.setOnSeekbarChangeListener { seek_bar.setOnSeekbarChangeListener { n ->
n -> seek_bar_value.text = resources.getString(R.string.time_to_solve, n.toString()) seek_bar_value.text = resources.getString(R.string.time_to_solve, n.toString())
secToSolve = n.toInt() secToSolve = n.toInt()
} }
card_2_digits.setOnClickListener(cardOnClickListener) card_2_digits.setOnClickListener(cardOnClickListener)
card_3_digits.setOnClickListener(cardOnClickListener) card_3_digits.setOnClickListener(cardOnClickListener)
card_4_digits.setOnClickListener(cardOnClickListener) card_4_digits.setOnClickListener(cardOnClickListener)
btn_play.setOnClickListener(playClickListener)
} }
private val cardOnClickListener = View.OnClickListener { card -> private val cardOnClickListener = View.OnClickListener { card ->
val numDigits = getTextView(card).text.toString().toInt() val cardM = card as MaterialCardView
if (!cardM.isChecked) {
cardM.isChecked = true
numDigits = getTextView(card).text.toString().toInt()
listOf(card_2_digits, card_3_digits, card_4_digits).forEach { c ->
if (c.id != cardM.id)
c.isChecked = false
}
}
}
private val playClickListener = View.OnClickListener {
saveSelectedValues() saveSelectedValues()
val intent = Intent(this, TaskActivity::class.java).also { i -> val intent = Intent(this, TaskActivity::class.java).also { i ->
i.putExtra("num_digits", numDigits) i.putExtra("num_digits", numDigits)
i.putExtra("sec_to_solve", secToSolve) i.putExtra("sec_to_solve", secToSolve)
...@@ -45,8 +59,10 @@ class MainActivity: AppCompatActivity() { ...@@ -45,8 +59,10 @@ class MainActivity: AppCompatActivity() {
editor.apply() editor.apply()
} }
private fun retrieveSecondsToSolve(): Int { private fun retrieveSharedPrefs() {
val prefs = getSharedPreferences("my_prefs", Context.MODE_PRIVATE) val prefs = getSharedPreferences("my_prefs", Context.MODE_PRIVATE)
return prefs.getInt("sec_to_solve", 4) secToSolve = prefs.getInt("sec_to_solve", 4)
checkedCard = findViewById(prefs.getInt("checked_num_id", R.id.card_3_digits))
checkedCard.isChecked = true
} }
} }
\ No newline at end of file
...@@ -106,4 +106,15 @@ ...@@ -106,4 +106,15 @@
app:layout_constraintStart_toStartOf="@+id/layout_num_digits" app:layout_constraintStart_toStartOf="@+id/layout_num_digits"
app:layout_constraintTop_toBottomOf="@+id/seek_bar" /> app:layout_constraintTop_toBottomOf="@+id/seek_bar" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="@string/play"
app:backgroundTint="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
...@@ -11,4 +11,5 @@ ...@@ -11,4 +11,5 @@
<string name="time_to_solve">Time to solve: %1$s seconds</string> <string name="time_to_solve">Time to solve: %1$s seconds</string>
<string name="btn_next">Next</string> <string name="btn_next">Next</string>
<string name="tap_to_continue">Tap anywhere to continue</string> <string name="tap_to_continue">Tap anywhere to continue</string>
<string name="play">Play</string>
</resources> </resources>
\ No newline at end of file
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
<item name="android:clickable">true</item> <item name="android:clickable">true</item>
<item name="android:focusable">true</item> <item name="android:focusable">true</item>
<item name="android:checkable">true</item> <item name="android:checkable">true</item>
<item name="android:selectable">true</item>
<item name="cardCornerRadius">6dp</item> <item name="cardCornerRadius">6dp</item>
</style> </style>
......
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