Skip to content

Added Test for Quiz Classes #3953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions app/src/main/java/fr/free/nrw/commons/quiz/QuizController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
Expand Down
103 changes: 103 additions & 0 deletions app/src/test/kotlin/fr/free/nrw/commons/quiz/QuizActivityUnitTest.kt
Original file line number Diff line number Diff line change
@@ -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)
}

}
39 changes: 39 additions & 0 deletions app/src/test/kotlin/fr/free/nrw/commons/quiz/QuizControllerTest.kt
Original file line number Diff line number Diff line change
@@ -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())
}

}
51 changes: 51 additions & 0 deletions app/src/test/kotlin/fr/free/nrw/commons/quiz/QuizQuestionTest.kt
Original file line number Diff line number Diff line change
@@ -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))
}

}