Commit d19c7892 by likorn

MainActivityTest; WIP response as a fragment

parent 23812f13
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -14,6 +14,7 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
......
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
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import org.hamcrest.Matcher
import org.junit.Before
class MainActivityTest {
@get:Rule
val testRule = ActivityTestRule<MainActivity>(MainActivity::class.java)
private lateinit var radioGroup: RadioGroup
@Before
fun init() {
radioGroup = testRule.activity.radio_group
}
@Test
fun test() {
testRule.activity.radio_group.getChildAt(0)
fun correct_answer_marked_as_correct() {
val correctAnswerIndex = getCorrectAnswerIndex()
val correctAnswerId = radioGroup[correctAnswerIndex].id
onView(withId(correctAnswerId)).perform(click())
onView(withId(R.id.response)).check(matches(withText(R.string.response_correct)))
}
@Test
fun wrong_answer_marked_as_wrong() {
val correctAnswerIndex = getCorrectAnswerIndex()
val wrongAnswerIndex = if (correctAnswerIndex == 3) 2 else 3
val wrongAnswerId = radioGroup[wrongAnswerIndex].id
onView(withId(wrongAnswerId)).perform(click())
onView(withId(R.id.response)).check(matches(withText(R.string.response_wrong)))
}
private fun getCorrectAnswerIndex(): Int {
val options = getOptions()
val correctAnswer = findSecondMax(options)
return options.indexOf(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 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
......@@ -28,8 +28,10 @@ class MainActivity : AppCompatActivity() {
}
private fun processAnswer(answer: Int) {
if (numberSet.isCorrect(answer))
if (numberSet.isCorrect(answer)) {
response.text = getString(R.string.response_correct)
}
else
response.text = getString(R.string.response_wrong)
}
......
package com.example.quickmax
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class ResponseFragment: Fragment() {
companion object {
fun newInstance(): ResponseFragment {
return ResponseFragment()
}
}
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_response, container, false)
}
}
\ No newline at end of file
<vector android:height="80dp" android:viewportHeight="24"
android:viewportWidth="24" android:width="80dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M15.5,5l-4.5,0l5,7l-5,7l4.5,0l5,-7z"/>
<path android:fillColor="#FF000000" android:pathData="M8.5,5l-4.5,0l5,7l-5,7l4.5,0l5,-7z"/>
</vector>
......@@ -7,15 +7,6 @@
tools:context=".MainActivity">
<TextView
android:id="@+id/response"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
......@@ -25,6 +16,42 @@
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/response_layout"
android:layout_width="0dp"
android:layout_height="120dp"
android:background="#4CAF50"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="8dp"
android:background="#009C27B0"
android:tint="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_double_arrow" />
<TextView
android:id="@+id/response"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:textColor="@android:color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="@string/response_correct" />
</androidx.constraintlayout.widget.ConstraintLayout>
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="wrap_content"
......
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/response_layout"
android:layout_width="0dp"
android:layout_height="120dp"
android:background="#4CAF50"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="8dp"
android:background="#009C27B0"
android:tint="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_double_arrow" />
<TextView
android:id="@+id/response"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:textColor="@android:color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="@string/response_correct" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
<resources>
<string name="app_name">QuickMax</string>
<string name="second_max">Выберите второе наибольшее число</string>
<string name="response_correct">Correct!</string>
<string name="response_wrong">Wrong!</string>
<string name="response_correct">CORRECT</string>
<string name="response_wrong">WRONG</string>
</resources>
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