Skip to content

Commit 67c0004

Browse files
Added Test for Quiz Classes (#3953)
1 parent cb3de85 commit 67c0004

File tree

4 files changed

+203
-10
lines changed

4 files changed

+203
-10
lines changed

app/src/main/java/fr/free/nrw/commons/quiz/QuizController.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,38 @@ public class QuizController {
2121

2222
public void initialize(Context context){
2323
QuizQuestion q1 = new QuizQuestion(1,
24-
context.getResources().getString(R.string.quiz_question_string),
24+
context.getString(R.string.quiz_question_string),
2525
URL_FOR_SELFIE,
2626
false,
27-
context.getResources().getString(R.string.selfie_answer));
27+
context.getString(R.string.selfie_answer));
2828
quiz.add(q1);
2929

3030
QuizQuestion q2 = new QuizQuestion(2,
31-
context.getResources().getString(R.string.quiz_question_string),
31+
context.getString(R.string.quiz_question_string),
3232
URL_FOR_TAJ_MAHAL,
3333
true,
34-
context.getResources().getString(R.string.taj_mahal_answer));
34+
context.getString(R.string.taj_mahal_answer));
3535
quiz.add(q2);
3636

3737
QuizQuestion q3 = new QuizQuestion(3,
38-
context.getResources().getString(R.string.quiz_question_string),
38+
context.getString(R.string.quiz_question_string),
3939
URL_FOR_BLURRY_IMAGE,
4040
false,
41-
context.getResources().getString(R.string.blurry_image_answer));
41+
context.getString(R.string.blurry_image_answer));
4242
quiz.add(q3);
4343

4444
QuizQuestion q4 = new QuizQuestion(4,
45-
context.getResources().getString(R.string.quiz_screenshot_question),
45+
context.getString(R.string.quiz_screenshot_question),
4646
URL_FOR_SCREENSHOT,
4747
false,
48-
context.getResources().getString(R.string.screenshot_answer));
48+
context.getString(R.string.screenshot_answer));
4949
quiz.add(q4);
5050

5151
QuizQuestion q5 = new QuizQuestion(5,
52-
context.getResources().getString(R.string.quiz_question_string),
52+
context.getString(R.string.quiz_question_string),
5353
URL_FOR_EVENT,
5454
true,
55-
context.getResources().getString(R.string.construction_event_answer));
55+
context.getString(R.string.construction_event_answer));
5656
quiz.add(q5);
5757

5858
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package fr.free.nrw.commons.quiz
2+
3+
import android.content.Context
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.widget.RadioButton
7+
import fr.free.nrw.commons.R
8+
import fr.free.nrw.commons.TestCommonsApplication
9+
import org.junit.Assert
10+
import org.junit.Before
11+
import org.junit.Test
12+
import org.junit.runner.RunWith
13+
import org.mockito.Mock
14+
import org.mockito.Mockito
15+
import org.mockito.MockitoAnnotations
16+
import org.powermock.api.mockito.PowerMockito.mock
17+
import org.powermock.reflect.Whitebox
18+
import org.robolectric.Robolectric
19+
import org.robolectric.RobolectricTestRunner
20+
import org.robolectric.annotation.Config
21+
import org.robolectric.annotation.LooperMode
22+
23+
24+
@RunWith(RobolectricTestRunner::class)
25+
@Config(sdk = [21], application = TestCommonsApplication::class)
26+
@LooperMode(LooperMode.Mode.PAUSED)
27+
class QuizActivityUnitTest {
28+
29+
private lateinit var activity: QuizActivity
30+
private lateinit var positiveAnswer: RadioButton
31+
private lateinit var negativeAnswer: RadioButton
32+
private lateinit var view: View
33+
34+
private val SAMPLE_ALERT_TITLE_VALUE = "Title"
35+
private val SAMPLE_ALERT_MESSAGE_VALUE = "Message"
36+
37+
@Mock
38+
private lateinit var quizController: QuizController
39+
40+
@Mock
41+
private lateinit var context: Context
42+
43+
44+
@Before
45+
fun setUp() {
46+
MockitoAnnotations.initMocks(this)
47+
activity = Robolectric.buildActivity(QuizActivity::class.java).get()
48+
49+
context = mock(Context::class.java)
50+
51+
view = LayoutInflater.from(activity)
52+
.inflate(R.layout.answer_layout, null) as View
53+
54+
Mockito.`when`(context.getString(Mockito.any(Int::class.java)))
55+
.thenReturn("")
56+
quizController = QuizController()
57+
58+
quizController.initialize(context)
59+
60+
positiveAnswer = view.findViewById(R.id.quiz_positive_answer)
61+
negativeAnswer = view.findViewById(R.id.quiz_negative_answer)
62+
63+
activity.positiveAnswer = positiveAnswer
64+
activity.negativeAnswer = negativeAnswer
65+
}
66+
67+
@Test
68+
@Throws(Exception::class)
69+
fun checkActivityNotNull() {
70+
Assert.assertNotNull(activity)
71+
Assert.assertNotNull(positiveAnswer)
72+
Assert.assertNotNull(negativeAnswer)
73+
}
74+
75+
@Test
76+
@Throws(Exception::class)
77+
fun testSetNextQuestion() {
78+
activity.setNextQuestion()
79+
}
80+
81+
@Test
82+
@Throws(Exception::class)
83+
fun testOnBackPressed() {
84+
activity.onBackPressed()
85+
}
86+
87+
@Test
88+
@Throws(Exception::class)
89+
fun testEvaluateScore() {
90+
91+
Whitebox.setInternalState(activity, "quiz", quizController.getQuiz())
92+
Whitebox.setInternalState(activity, "questionIndex", 0)
93+
94+
activity.evaluateScore()
95+
}
96+
97+
@Test
98+
@Throws(Exception::class)
99+
fun testCustomAlert() {
100+
activity.customAlert(SAMPLE_ALERT_TITLE_VALUE, SAMPLE_ALERT_MESSAGE_VALUE)
101+
}
102+
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package fr.free.nrw.commons.quiz
2+
3+
import android.content.Context
4+
import org.junit.Assert.assertNotNull
5+
import org.junit.Before
6+
import org.junit.Test
7+
import org.mockito.Mock
8+
import org.mockito.Mockito.`when`
9+
import org.mockito.Mockito.any
10+
import org.mockito.MockitoAnnotations
11+
12+
13+
class QuizControllerTest {
14+
15+
@Mock
16+
private lateinit var quizController: QuizController
17+
18+
@Mock
19+
private lateinit var context: Context
20+
21+
@Before
22+
fun setup() {
23+
MockitoAnnotations.initMocks(this)
24+
`when`(context.getString(any(Int::class.java)))
25+
.thenReturn("")
26+
quizController = QuizController()
27+
}
28+
29+
@Test
30+
fun testInitialise() {
31+
quizController.initialize(context)
32+
}
33+
34+
@Test
35+
fun testGetQuiz() {
36+
assertNotNull(quizController.getQuiz())
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package fr.free.nrw.commons.quiz
2+
3+
import android.net.Uri
4+
import fr.free.nrw.commons.TestCommonsApplication
5+
import org.junit.Assert.assertEquals
6+
import org.junit.Before
7+
import org.junit.Test
8+
import org.junit.runner.RunWith
9+
import org.mockito.Mock
10+
import org.mockito.MockitoAnnotations
11+
import org.robolectric.RobolectricTestRunner
12+
import org.robolectric.annotation.Config
13+
14+
@RunWith(RobolectricTestRunner::class)
15+
@Config(sdk = [21], application = TestCommonsApplication::class)
16+
class QuizQuestionTest {
17+
18+
@Mock
19+
private lateinit var quizQuestion: QuizQuestion
20+
21+
private val QUESTION_NUM_SAMPLE_VALUE = 1
22+
private val QUESTION_SAMPLE_VALUE = "Is this picture OK to upload?"
23+
private val QUESTION_URL_SAMPLE_VALUE_ONE = "https://i.imgur.com/0fMYcpM.jpg"
24+
private val QUESTION_URL_SAMPLE_VALUE_TWO = "https://example.com"
25+
private val IS_ANSWER_SAMPLE_VALUE = false
26+
private val ANSWER_MESSAGE_SAMPLE_VALUE = "Continue"
27+
28+
@Before
29+
fun setup() {
30+
MockitoAnnotations.initMocks(this)
31+
quizQuestion = QuizQuestion(
32+
QUESTION_NUM_SAMPLE_VALUE,
33+
QUESTION_SAMPLE_VALUE,
34+
QUESTION_URL_SAMPLE_VALUE_ONE,
35+
IS_ANSWER_SAMPLE_VALUE,
36+
ANSWER_MESSAGE_SAMPLE_VALUE
37+
)
38+
}
39+
40+
@Test
41+
fun testGetUrl() {
42+
assertEquals(quizQuestion.getUrl(), Uri.parse(QUESTION_URL_SAMPLE_VALUE_ONE))
43+
}
44+
45+
@Test
46+
fun testSetUrl() {
47+
quizQuestion.setUrl(QUESTION_URL_SAMPLE_VALUE_TWO)
48+
assertEquals(quizQuestion.getUrl(), Uri.parse(QUESTION_URL_SAMPLE_VALUE_TWO))
49+
}
50+
51+
}

0 commit comments

Comments
 (0)