Commit 138ec96f by likorn

Added TimeIsOverFragment

parent ae72ab10
...@@ -13,6 +13,7 @@ import androidx.test.espresso.ViewAction ...@@ -13,6 +13,7 @@ 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.matcher.ViewMatchers.* import androidx.test.espresso.matcher.ViewMatchers.*
import junit.framework.Assert.assertNotNull
import junit.framework.Assert.assertTrue import junit.framework.Assert.assertTrue
import org.hamcrest.Matcher import org.hamcrest.Matcher
import org.junit.Before import org.junit.Before
...@@ -57,8 +58,18 @@ class MainActivityTest { ...@@ -57,8 +58,18 @@ class MainActivityTest {
} }
@Test @Test
fun timer() { fun time_decreases() {
// TODO write test 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(3000)
assertNotNull(testRule.activity.supportFragmentManager.fragments.find {
f -> f is TimeIsOverFragment })
} }
private fun getCorrectAnswerIndex(): Int { private fun getCorrectAnswerIndex(): Int {
......
package com.example.quickmax package com.example.quickmax
import android.os.Bundle import android.os.Bundle
import android.os.CountDownTimer
import android.view.View import android.view.View
import android.widget.RadioButton import android.widget.RadioButton
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import android.os.CountDownTimer
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
...@@ -22,14 +22,17 @@ class MainActivity : AppCompatActivity() { ...@@ -22,14 +22,17 @@ class MainActivity : AppCompatActivity() {
} }
private fun setTimer() { private fun setTimer() {
object : CountDownTimer(3000, 500) { object : CountDownTimer(3000, 1000) {
override fun onTick(millisUntilFinished: Long) { override fun onTick(millisUntilFinished: Long) {
tv_time_left.text = (millisUntilFinished / 1000.0).toString() tv_time_left.text = (millisUntilFinished / 1000).toString()
} }
override fun onFinish() { override fun onFinish() {
tv_time_left.text = "OVER" supportFragmentManager
.beginTransaction()
.add(R.id.main_layout, TimeIsOverFragment.newInstance(), "time_is_over")
.commitAllowingStateLoss()
} }
}.start() }.start()
} }
...@@ -48,14 +51,14 @@ class MainActivity : AppCompatActivity() { ...@@ -48,14 +51,14 @@ class MainActivity : AppCompatActivity() {
if (!toAcceptAnswer) if (!toAcceptAnswer)
return return
toAcceptAnswer = false toAcceptAnswer = false
val responseFragment:Fragment = if (numberSet.isCorrect(answer)) { val responseFragment: Fragment = if (numberSet.isCorrect(answer)) {
ResponseCorrectFragment.newInstance() ResponseCorrectFragment.newInstance()
} else } else
ResponseWrongFragment.newInstance() ResponseWrongFragment.newInstance()
supportFragmentManager supportFragmentManager
.beginTransaction() .beginTransaction()
.add(R.id.main_layout, responseFragment) .add(R.id.main_layout, responseFragment, "response")
.commit() .commitAllowingStateLoss()
} }
} }
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 TimeIsOverFragment: Fragment() {
companion object {
fun newInstance(): TimeIsOverFragment {
return TimeIsOverFragment()
}
}
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_time_is_over, container, false)
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/response_layout" android:id="@+id/response_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="120dp" android:layout_height="120dp"
......
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