Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
likorn
/
quick_max
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
c3d96ce6
authored
Sep 24, 2019
by
likorn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated tests for MainActivity
parent
0888956b
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
30 additions
and
32 deletions
app/src/androidTest/java/com/example/quickmax/MainActivityTest.kt
app/src/main/java/com/example/quickmax/AnswerSet.kt
app/src/main/java/com/example/quickmax/Calculator.kt
app/src/main/java/com/example/quickmax/MainActivity.kt
app/src/androidTest/java/com/example/quickmax/MainActivityTest.kt
View file @
c3d96ce6
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
{
...
...
app/src/main/java/com/example/quickmax/AnswerSet.kt
View file @
c3d96ce6
...
...
@@ -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
...
...
app/src/main/java/com/example/quickmax/Calculator.kt
View file @
c3d96ce6
...
...
@@ -9,14 +9,14 @@ fun generateRandom(numDigits: Int): Int {
}
fun
generateNRandomNumbers
(
numDigits
:
Int
,
n
:
Int
):
Set
<
Int
>
{
val
randomNumbers
=
s
etOf
<
Int
>()
val
randomNumbers
=
mutableS
etOf
<
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
...
...
app/src/main/java/com/example/quickmax/MainActivity.kt
View file @
c3d96ce6
...
...
@@ -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
addRadio
Buttons
()
{
private
fun
setUpAnswer
Buttons
()
{
for
(
answer
in
answerSet
)
{
findViewById
<
Button
>(
answer
.
buttonId
).
text
=
answer
.
value
.
toString
()
findViewById
<
Button
>(
answer
.
buttonId
).
setOnClickListener
{
processAnswer
(
answer
.
correct
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment