1
+ package fr.free.nrw.commons
2
+
3
+ import android.content.Intent
4
+ import android.widget.TextView
5
+ import fr.free.nrw.commons.quiz.QuizActivity
6
+ import junit.framework.Assert.assertEquals
7
+ import junit.framework.Assert.assertNotNull
8
+ import org.junit.Before
9
+ import org.junit.Test
10
+ import org.junit.runner.RunWith
11
+ import org.robolectric.Robolectric
12
+ import org.robolectric.RobolectricTestRunner
13
+ import org.robolectric.Shadows.shadowOf
14
+ import org.robolectric.annotation.Config
15
+ import org.robolectric.shadows.ShadowActivity
16
+ import org.robolectric.shadows.ShadowIntent
17
+
18
+ /* *
19
+ * Tests Welcome Activity Methods
20
+ */
21
+ @RunWith(RobolectricTestRunner ::class )
22
+ @Config(sdk = [21 ], application = TestCommonsApplication ::class )
23
+ class WelcomeActivityUnitTest {
24
+
25
+ private lateinit var activity: WelcomeActivity
26
+ private lateinit var finishTutorialButton: TextView
27
+
28
+ /* *
29
+ * Setup the Class and Views for Test
30
+ * Initialise the activity with isQuiz as true for Intent Extra
31
+ */
32
+ @Before
33
+ fun setUp () {
34
+ val intent = Intent ().putExtra(" isQuiz" , true )
35
+ activity = Robolectric .buildActivity(WelcomeActivity ::class .java, intent)
36
+ .get()
37
+ activity.onCreate(null )
38
+ finishTutorialButton = activity.findViewById(R .id.finishTutorialButton)
39
+ }
40
+
41
+ /* *
42
+ * Checks if the activity is not null and member variables are not null
43
+ */
44
+ @Test
45
+ @Throws(Exception ::class )
46
+ fun checkActivityNotNull () {
47
+ assertNotNull(activity)
48
+ assertNotNull(activity.pager)
49
+ assertNotNull(activity.indicator)
50
+ }
51
+
52
+ /* *
53
+ * Checks if the activity onDestroy method launches correct intent when isQuiz is true
54
+ */
55
+ @Test
56
+ @Throws(Exception ::class )
57
+ fun testOnDestroy () {
58
+ activity.onDestroy()
59
+ val shadowActivity: ShadowActivity = shadowOf(activity)
60
+ val startedIntent = shadowActivity.nextStartedActivity
61
+ val shadowIntent: ShadowIntent = shadowOf(startedIntent)
62
+ assertEquals(shadowIntent.intentClass, QuizActivity ::class .java)
63
+ }
64
+
65
+ /* *
66
+ * Checks if the finish Tutorial Button executes the finishTutorial method without any errors
67
+ */
68
+ @Test
69
+ @Throws(Exception ::class )
70
+ fun testFinishTutorial () {
71
+ finishTutorialButton.performClick()
72
+ }
73
+
74
+ /* *
75
+ * Checks if the onBackPressed method executes without any errors
76
+ */
77
+ @Test
78
+ @Throws(Exception ::class )
79
+ fun testOnBackPressed () {
80
+ activity.onBackPressed()
81
+ }
82
+
83
+ }
0 commit comments