diff --git a/app/src/main/java/fr/free/nrw/commons/quiz/QuizController.java b/app/src/main/java/fr/free/nrw/commons/quiz/QuizController.java index 3937fe505d..a7b2c94ef2 100644 --- a/app/src/main/java/fr/free/nrw/commons/quiz/QuizController.java +++ b/app/src/main/java/fr/free/nrw/commons/quiz/QuizController.java @@ -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); } diff --git a/app/src/test/kotlin/fr/free/nrw/commons/quiz/QuizActivityUnitTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/quiz/QuizActivityUnitTest.kt new file mode 100644 index 0000000000..9f934828e8 --- /dev/null +++ b/app/src/test/kotlin/fr/free/nrw/commons/quiz/QuizActivityUnitTest.kt @@ -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) + } + +} \ No newline at end of file diff --git a/app/src/test/kotlin/fr/free/nrw/commons/quiz/QuizControllerTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/quiz/QuizControllerTest.kt new file mode 100644 index 0000000000..f1a86330c7 --- /dev/null +++ b/app/src/test/kotlin/fr/free/nrw/commons/quiz/QuizControllerTest.kt @@ -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()) + } + +} \ No newline at end of file diff --git a/app/src/test/kotlin/fr/free/nrw/commons/quiz/QuizQuestionTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/quiz/QuizQuestionTest.kt new file mode 100644 index 0000000000..f69bea174e --- /dev/null +++ b/app/src/test/kotlin/fr/free/nrw/commons/quiz/QuizQuestionTest.kt @@ -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)) + } + +} \ No newline at end of file