mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
Added Test for Quiz Classes (#3953)
This commit is contained in:
parent
cb3de85646
commit
67c0004062
4 changed files with 203 additions and 10 deletions
|
|
@ -21,38 +21,38 @@ public class QuizController {
|
|||
|
||||
public void initialize(Context context){
|
||||
QuizQuestion q1 = new QuizQuestion(1,
|
||||
context.getResources().getString(R.string.quiz_question_string),
|
||||
context.getString(R.string.quiz_question_string),
|
||||
URL_FOR_SELFIE,
|
||||
false,
|
||||
context.getResources().getString(R.string.selfie_answer));
|
||||
context.getString(R.string.selfie_answer));
|
||||
quiz.add(q1);
|
||||
|
||||
QuizQuestion q2 = new QuizQuestion(2,
|
||||
context.getResources().getString(R.string.quiz_question_string),
|
||||
context.getString(R.string.quiz_question_string),
|
||||
URL_FOR_TAJ_MAHAL,
|
||||
true,
|
||||
context.getResources().getString(R.string.taj_mahal_answer));
|
||||
context.getString(R.string.taj_mahal_answer));
|
||||
quiz.add(q2);
|
||||
|
||||
QuizQuestion q3 = new QuizQuestion(3,
|
||||
context.getResources().getString(R.string.quiz_question_string),
|
||||
context.getString(R.string.quiz_question_string),
|
||||
URL_FOR_BLURRY_IMAGE,
|
||||
false,
|
||||
context.getResources().getString(R.string.blurry_image_answer));
|
||||
context.getString(R.string.blurry_image_answer));
|
||||
quiz.add(q3);
|
||||
|
||||
QuizQuestion q4 = new QuizQuestion(4,
|
||||
context.getResources().getString(R.string.quiz_screenshot_question),
|
||||
context.getString(R.string.quiz_screenshot_question),
|
||||
URL_FOR_SCREENSHOT,
|
||||
false,
|
||||
context.getResources().getString(R.string.screenshot_answer));
|
||||
context.getString(R.string.screenshot_answer));
|
||||
quiz.add(q4);
|
||||
|
||||
QuizQuestion q5 = new QuizQuestion(5,
|
||||
context.getResources().getString(R.string.quiz_question_string),
|
||||
context.getString(R.string.quiz_question_string),
|
||||
URL_FOR_EVENT,
|
||||
true,
|
||||
context.getResources().getString(R.string.construction_event_answer));
|
||||
context.getString(R.string.construction_event_answer));
|
||||
quiz.add(q5);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,103 @@
|
|||
package fr.free.nrw.commons.quiz
|
||||
|
||||
import android.content.Context
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.widget.RadioButton
|
||||
import fr.free.nrw.commons.R
|
||||
import fr.free.nrw.commons.TestCommonsApplication
|
||||
import org.junit.Assert
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Mock
|
||||
import org.mockito.Mockito
|
||||
import org.mockito.MockitoAnnotations
|
||||
import org.powermock.api.mockito.PowerMockito.mock
|
||||
import org.powermock.reflect.Whitebox
|
||||
import org.robolectric.Robolectric
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.LooperMode
|
||||
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [21], application = TestCommonsApplication::class)
|
||||
@LooperMode(LooperMode.Mode.PAUSED)
|
||||
class QuizActivityUnitTest {
|
||||
|
||||
private lateinit var activity: QuizActivity
|
||||
private lateinit var positiveAnswer: RadioButton
|
||||
private lateinit var negativeAnswer: RadioButton
|
||||
private lateinit var view: View
|
||||
|
||||
private val SAMPLE_ALERT_TITLE_VALUE = "Title"
|
||||
private val SAMPLE_ALERT_MESSAGE_VALUE = "Message"
|
||||
|
||||
@Mock
|
||||
private lateinit var quizController: QuizController
|
||||
|
||||
@Mock
|
||||
private lateinit var context: Context
|
||||
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MockitoAnnotations.initMocks(this)
|
||||
activity = Robolectric.buildActivity(QuizActivity::class.java).get()
|
||||
|
||||
context = mock(Context::class.java)
|
||||
|
||||
view = LayoutInflater.from(activity)
|
||||
.inflate(R.layout.answer_layout, null) as View
|
||||
|
||||
Mockito.`when`(context.getString(Mockito.any(Int::class.java)))
|
||||
.thenReturn("")
|
||||
quizController = QuizController()
|
||||
|
||||
quizController.initialize(context)
|
||||
|
||||
positiveAnswer = view.findViewById(R.id.quiz_positive_answer)
|
||||
negativeAnswer = view.findViewById(R.id.quiz_negative_answer)
|
||||
|
||||
activity.positiveAnswer = positiveAnswer
|
||||
activity.negativeAnswer = negativeAnswer
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun checkActivityNotNull() {
|
||||
Assert.assertNotNull(activity)
|
||||
Assert.assertNotNull(positiveAnswer)
|
||||
Assert.assertNotNull(negativeAnswer)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testSetNextQuestion() {
|
||||
activity.setNextQuestion()
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnBackPressed() {
|
||||
activity.onBackPressed()
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testEvaluateScore() {
|
||||
|
||||
Whitebox.setInternalState(activity, "quiz", quizController.getQuiz())
|
||||
Whitebox.setInternalState(activity, "questionIndex", 0)
|
||||
|
||||
activity.evaluateScore()
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testCustomAlert() {
|
||||
activity.customAlert(SAMPLE_ALERT_TITLE_VALUE, SAMPLE_ALERT_MESSAGE_VALUE)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package fr.free.nrw.commons.quiz
|
||||
|
||||
import android.content.Context
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mockito.Mock
|
||||
import org.mockito.Mockito.`when`
|
||||
import org.mockito.Mockito.any
|
||||
import org.mockito.MockitoAnnotations
|
||||
|
||||
|
||||
class QuizControllerTest {
|
||||
|
||||
@Mock
|
||||
private lateinit var quizController: QuizController
|
||||
|
||||
@Mock
|
||||
private lateinit var context: Context
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
MockitoAnnotations.initMocks(this)
|
||||
`when`(context.getString(any(Int::class.java)))
|
||||
.thenReturn("")
|
||||
quizController = QuizController()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInitialise() {
|
||||
quizController.initialize(context)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetQuiz() {
|
||||
assertNotNull(quizController.getQuiz())
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
package fr.free.nrw.commons.quiz
|
||||
|
||||
import android.net.Uri
|
||||
import fr.free.nrw.commons.TestCommonsApplication
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Mock
|
||||
import org.mockito.MockitoAnnotations
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [21], application = TestCommonsApplication::class)
|
||||
class QuizQuestionTest {
|
||||
|
||||
@Mock
|
||||
private lateinit var quizQuestion: QuizQuestion
|
||||
|
||||
private val QUESTION_NUM_SAMPLE_VALUE = 1
|
||||
private val QUESTION_SAMPLE_VALUE = "Is this picture OK to upload?"
|
||||
private val QUESTION_URL_SAMPLE_VALUE_ONE = "https://i.imgur.com/0fMYcpM.jpg"
|
||||
private val QUESTION_URL_SAMPLE_VALUE_TWO = "https://example.com"
|
||||
private val IS_ANSWER_SAMPLE_VALUE = false
|
||||
private val ANSWER_MESSAGE_SAMPLE_VALUE = "Continue"
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
MockitoAnnotations.initMocks(this)
|
||||
quizQuestion = QuizQuestion(
|
||||
QUESTION_NUM_SAMPLE_VALUE,
|
||||
QUESTION_SAMPLE_VALUE,
|
||||
QUESTION_URL_SAMPLE_VALUE_ONE,
|
||||
IS_ANSWER_SAMPLE_VALUE,
|
||||
ANSWER_MESSAGE_SAMPLE_VALUE
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetUrl() {
|
||||
assertEquals(quizQuestion.getUrl(), Uri.parse(QUESTION_URL_SAMPLE_VALUE_ONE))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSetUrl() {
|
||||
quizQuestion.setUrl(QUESTION_URL_SAMPLE_VALUE_TWO)
|
||||
assertEquals(quizQuestion.getUrl(), Uri.parse(QUESTION_URL_SAMPLE_VALUE_TWO))
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue