Skip to content

Commit bd7fea8

Browse files
Add UploadActivity Unit Tests (commons-app#4636)
* Add UploadActivity Unit Tests * Fix failing issue
1 parent 388e828 commit bd7fea8

File tree

1 file changed

+298
-0
lines changed

1 file changed

+298
-0
lines changed
Lines changed: 298 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
1+
package fr.free.nrw.commons.upload
2+
3+
import android.content.Context
4+
import android.content.Intent
5+
import fr.free.nrw.commons.CommonsApplication
6+
import fr.free.nrw.commons.R
7+
import fr.free.nrw.commons.TestAppAdapter
8+
import fr.free.nrw.commons.TestCommonsApplication
9+
import fr.free.nrw.commons.contributions.ContributionController
10+
import fr.free.nrw.commons.filepicker.UploadableFile
11+
import fr.free.nrw.commons.upload.categories.UploadCategoriesFragment
12+
import fr.free.nrw.commons.upload.license.MediaLicenseFragment
13+
import org.junit.Assert
14+
import org.junit.Before
15+
import org.junit.Test
16+
import org.junit.runner.RunWith
17+
import org.mockito.Mock
18+
import org.mockito.Mockito.mock
19+
import org.mockito.MockitoAnnotations
20+
import org.powermock.reflect.Whitebox
21+
import org.robolectric.Robolectric
22+
import org.robolectric.RobolectricTestRunner
23+
import org.robolectric.RuntimeEnvironment
24+
import org.robolectric.annotation.Config
25+
import org.robolectric.annotation.LooperMode
26+
import org.wikipedia.AppAdapter
27+
import java.lang.reflect.Method
28+
29+
30+
@RunWith(RobolectricTestRunner::class)
31+
@Config(sdk = [21], application = TestCommonsApplication::class)
32+
@LooperMode(LooperMode.Mode.PAUSED)
33+
class UploadActivityUnitTests {
34+
35+
private lateinit var activity: UploadActivity
36+
private lateinit var context: Context
37+
38+
@Mock
39+
private lateinit var uploadBaseFragment: UploadBaseFragment
40+
41+
@Mock
42+
private lateinit var uploadableFile: UploadableFile
43+
44+
@Mock
45+
private lateinit var presenter: UploadContract.UserActionListener
46+
47+
@Mock
48+
private lateinit var contributionController: ContributionController
49+
50+
@Before
51+
fun setUp() {
52+
MockitoAnnotations.initMocks(this)
53+
AppAdapter.set(TestAppAdapter())
54+
val intent = Intent()
55+
val list = ArrayList<UploadableFile>()
56+
list.add(uploadableFile)
57+
intent.putParcelableArrayListExtra(UploadActivity.EXTRA_FILES, list)
58+
activity = Robolectric.buildActivity(UploadActivity::class.java, intent).create().get()
59+
context = RuntimeEnvironment.application.applicationContext
60+
61+
Whitebox.setInternalState(activity, "fragments", mutableListOf(uploadBaseFragment))
62+
Whitebox.setInternalState(activity, "presenter", presenter)
63+
Whitebox.setInternalState(activity, "contributionController", contributionController)
64+
}
65+
66+
@Test
67+
@Throws(Exception::class)
68+
fun checkActivityNotNull() {
69+
Assert.assertNotNull(activity)
70+
}
71+
72+
@Test
73+
@Throws(Exception::class)
74+
fun testIsLoggedIn() {
75+
activity.isLoggedIn
76+
}
77+
78+
@Test
79+
@Throws(Exception::class)
80+
fun testOnResume() {
81+
val method: Method = UploadActivity::class.java.getDeclaredMethod(
82+
"onResume"
83+
)
84+
method.isAccessible = true
85+
method.invoke(activity)
86+
}
87+
88+
@Test
89+
@Throws(Exception::class)
90+
fun testOnStop() {
91+
val method: Method = UploadActivity::class.java.getDeclaredMethod(
92+
"onStop"
93+
)
94+
method.isAccessible = true
95+
method.invoke(activity)
96+
}
97+
98+
@Test
99+
@Throws(Exception::class)
100+
fun testShowProgressCaseTrue() {
101+
activity.showProgress(true)
102+
}
103+
104+
@Test
105+
@Throws(Exception::class)
106+
fun testShowProgressCaseFalse() {
107+
activity.showProgress(false)
108+
}
109+
110+
@Test
111+
@Throws(Exception::class)
112+
fun testGetIndexInViewFlipper() {
113+
activity.getIndexInViewFlipper(uploadBaseFragment)
114+
}
115+
116+
@Test
117+
@Throws(Exception::class)
118+
fun testGetTotalNumberOfSteps() {
119+
activity.totalNumberOfSteps
120+
}
121+
122+
@Test
123+
@Throws(Exception::class)
124+
fun testIsWLMUpload() {
125+
activity.isWLMUpload
126+
}
127+
128+
@Test
129+
@Throws(Exception::class)
130+
fun testShowMessage() {
131+
activity.showMessage(R.string.uploading_started)
132+
}
133+
134+
@Test
135+
@Throws(Exception::class)
136+
fun testGetUploadableFiles() {
137+
activity.uploadableFiles
138+
}
139+
140+
@Test
141+
@Throws(Exception::class)
142+
fun testShowHideTopCard() {
143+
activity.showHideTopCard(true)
144+
}
145+
146+
@Test
147+
@Throws(Exception::class)
148+
fun testOnUploadMediaDeleted() {
149+
Whitebox.setInternalState(activity, "uploadableFiles", mutableListOf(uploadableFile))
150+
activity.onUploadMediaDeleted(0)
151+
}
152+
153+
@Test
154+
@Throws(Exception::class)
155+
fun testUpdateTopCardTitle() {
156+
activity.updateTopCardTitle()
157+
}
158+
159+
@Test
160+
@Throws(Exception::class)
161+
fun testMakeUploadRequest() {
162+
activity.makeUploadRequest()
163+
}
164+
165+
@Test
166+
@Throws(Exception::class)
167+
fun testOnActivityResult() {
168+
val method: Method = UploadActivity::class.java.getDeclaredMethod(
169+
"onActivityResult",
170+
Int::class.java,
171+
Int::class.java,
172+
Intent::class.java
173+
)
174+
method.isAccessible = true
175+
method.invoke(activity, CommonsApplication.OPEN_APPLICATION_DETAIL_SETTINGS, 0, Intent())
176+
}
177+
178+
@Test
179+
@Throws(Exception::class)
180+
fun testReceiveSharedItems() {
181+
val method: Method = UploadActivity::class.java.getDeclaredMethod(
182+
"receiveSharedItems"
183+
)
184+
method.isAccessible = true
185+
method.invoke(activity)
186+
}
187+
188+
@Test
189+
@Throws(Exception::class)
190+
fun testReceiveExternalSharedItems() {
191+
val method: Method = UploadActivity::class.java.getDeclaredMethod(
192+
"receiveExternalSharedItems"
193+
)
194+
method.isAccessible = true
195+
method.invoke(activity)
196+
}
197+
198+
@Test
199+
@Throws(Exception::class)
200+
fun testReceiveInternalSharedItems() {
201+
val method: Method = UploadActivity::class.java.getDeclaredMethod(
202+
"receiveInternalSharedItems"
203+
)
204+
method.isAccessible = true
205+
method.invoke(activity)
206+
}
207+
208+
@Test
209+
@Throws(Exception::class)
210+
fun testGetIsMultipleFilesSelected() {
211+
activity.isMultipleFilesSelected
212+
}
213+
214+
@Test
215+
@Throws(Exception::class)
216+
fun testResetDirectPrefs() {
217+
activity.resetDirectPrefs()
218+
}
219+
220+
@Test
221+
@Throws(Exception::class)
222+
fun testHandleNullMedia() {
223+
val method: Method = UploadActivity::class.java.getDeclaredMethod(
224+
"handleNullMedia"
225+
)
226+
method.isAccessible = true
227+
method.invoke(activity)
228+
}
229+
230+
@Test
231+
@Throws(Exception::class)
232+
fun testShowInfoAlert() {
233+
val method: Method = UploadActivity::class.java.getDeclaredMethod(
234+
"showInfoAlert",
235+
Int::class.java,
236+
Int::class.java,
237+
Runnable::class.java,
238+
Array<String>::class.java
239+
)
240+
method.isAccessible = true
241+
method.invoke(
242+
activity,
243+
R.string.block_notification_title,
244+
R.string.block_notification,
245+
mock(Runnable::class.java),
246+
arrayOf("")
247+
)
248+
}
249+
250+
@Test
251+
@Throws(Exception::class)
252+
fun testOnNextButtonClicked() {
253+
activity.onNextButtonClicked(-1)
254+
}
255+
256+
@Test
257+
@Throws(Exception::class)
258+
fun testOnNextButtonClickedCaseFalse() {
259+
activity.onNextButtonClicked(0)
260+
}
261+
262+
@Test
263+
@Throws(Exception::class)
264+
fun testOnPreviousButtonClicked() {
265+
activity.onPreviousButtonClicked(1)
266+
}
267+
268+
@Test
269+
@Throws(Exception::class)
270+
fun testOnDestroy() {
271+
Whitebox.setInternalState(
272+
activity,
273+
"mediaLicenseFragment",
274+
mock(MediaLicenseFragment::class.java)
275+
)
276+
Whitebox.setInternalState(
277+
activity, "uploadCategoriesFragment", mock(
278+
UploadCategoriesFragment::class.java
279+
)
280+
)
281+
val method: Method = UploadActivity::class.java.getDeclaredMethod(
282+
"onDestroy"
283+
)
284+
method.isAccessible = true
285+
method.invoke(activity)
286+
}
287+
288+
@Test
289+
@Throws(Exception::class)
290+
fun testOnBackPressed() {
291+
val method: Method = UploadActivity::class.java.getDeclaredMethod(
292+
"onBackPressed"
293+
)
294+
method.isAccessible = true
295+
method.invoke(activity)
296+
}
297+
298+
}

0 commit comments

Comments
 (0)