Added Unit Tests for WelcomeActivity Class (#3933)

This commit is contained in:
Madhur Gupta 2020-09-18 20:35:42 +05:30 committed by GitHub
parent 93225149d8
commit 66469e006a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -0,0 +1,83 @@
package fr.free.nrw.commons
import android.content.Intent
import android.widget.TextView
import fr.free.nrw.commons.quiz.QuizActivity
import junit.framework.Assert.assertEquals
import junit.framework.Assert.assertNotNull
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
import org.robolectric.shadows.ShadowActivity
import org.robolectric.shadows.ShadowIntent
/**
* Tests Welcome Activity Methods
*/
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [21], application = TestCommonsApplication::class)
class WelcomeActivityUnitTest {
private lateinit var activity: WelcomeActivity
private lateinit var finishTutorialButton: TextView
/**
* Setup the Class and Views for Test
* Initialise the activity with isQuiz as true for Intent Extra
*/
@Before
fun setUp() {
val intent = Intent().putExtra("isQuiz", true)
activity = Robolectric.buildActivity(WelcomeActivity::class.java, intent)
.get()
activity.onCreate(null)
finishTutorialButton = activity.findViewById(R.id.finishTutorialButton)
}
/**
* Checks if the activity is not null and member variables are not null
*/
@Test
@Throws(Exception::class)
fun checkActivityNotNull() {
assertNotNull(activity)
assertNotNull(activity.pager)
assertNotNull(activity.indicator)
}
/**
* Checks if the activity onDestroy method launches correct intent when isQuiz is true
*/
@Test
@Throws(Exception::class)
fun testOnDestroy() {
activity.onDestroy()
val shadowActivity: ShadowActivity = shadowOf(activity)
val startedIntent = shadowActivity.nextStartedActivity
val shadowIntent: ShadowIntent = shadowOf(startedIntent)
assertEquals(shadowIntent.intentClass, QuizActivity::class.java)
}
/**
* Checks if the finish Tutorial Button executes the finishTutorial method without any errors
*/
@Test
@Throws(Exception::class)
fun testFinishTutorial() {
finishTutorialButton.performClick()
}
/**
* Checks if the onBackPressed method executes without any errors
*/
@Test
@Throws(Exception::class)
fun testOnBackPressed() {
activity.onBackPressed()
}
}