1
+ package fr.free.nrw.commons.contributions
2
+
3
+ import android.content.Context
4
+ import android.os.Bundle
5
+ import android.os.Looper
6
+ import android.view.*
7
+ import android.widget.LinearLayout
8
+ import android.widget.TextView
9
+ import androidx.fragment.app.FragmentManager
10
+ import androidx.fragment.app.FragmentTransaction
11
+ import fr.free.nrw.commons.R
12
+ import fr.free.nrw.commons.TestAppAdapter
13
+ import fr.free.nrw.commons.TestCommonsApplication
14
+ import fr.free.nrw.commons.campaigns.Campaign
15
+ import fr.free.nrw.commons.campaigns.CampaignView
16
+ import fr.free.nrw.commons.kvstore.JsonKvStore
17
+ import fr.free.nrw.commons.media.MediaDetailPagerFragment
18
+ import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient
19
+ import fr.free.nrw.commons.nearby.NearbyNotificationCardView
20
+ import fr.free.nrw.commons.notification.Notification
21
+ import fr.free.nrw.commons.notification.NotificationController
22
+ import fr.free.nrw.commons.notification.NotificationType
23
+ import io.reactivex.Single
24
+ import io.reactivex.disposables.CompositeDisposable
25
+ import org.junit.Assert
26
+ import org.junit.Before
27
+ import org.junit.Test
28
+ import org.junit.runner.RunWith
29
+ import org.mockito.ArgumentMatchers.*
30
+ import org.mockito.Mock
31
+ import org.mockito.Mockito.`when`
32
+ import org.mockito.MockitoAnnotations
33
+ import org.powermock.reflect.Whitebox
34
+ import org.robolectric.Robolectric
35
+ import org.robolectric.RobolectricTestRunner
36
+ import org.robolectric.RuntimeEnvironment
37
+ import org.robolectric.Shadows
38
+ import org.robolectric.annotation.Config
39
+ import org.robolectric.annotation.LooperMode
40
+ import org.wikipedia.AppAdapter
41
+ import java.lang.reflect.Method
42
+
43
+ @RunWith(RobolectricTestRunner ::class )
44
+ @Config(sdk = [21 ], application = TestCommonsApplication ::class )
45
+ @LooperMode(LooperMode .Mode .PAUSED )
46
+ class ContributionsFragmentUnitTests {
47
+
48
+ @Mock
49
+ private lateinit var mediaDetailPagerFragment: MediaDetailPagerFragment
50
+
51
+ @Mock
52
+ private lateinit var contributionsListFragment: ContributionsListFragment
53
+
54
+ @Mock
55
+ private lateinit var layoutInflater: LayoutInflater
56
+
57
+ @Mock
58
+ private lateinit var menuInflater: MenuInflater
59
+
60
+ @Mock
61
+ private lateinit var menu: Menu
62
+
63
+ @Mock
64
+ private lateinit var menuItem: MenuItem
65
+
66
+ @Mock
67
+ private lateinit var notification: View
68
+
69
+ @Mock
70
+ private lateinit var store: JsonKvStore
71
+
72
+ @Mock
73
+ private lateinit var notificationController: NotificationController
74
+
75
+ @Mock
76
+ private lateinit var limitedConnectionEnabledLayout: LinearLayout
77
+
78
+ @Mock
79
+ private lateinit var notificationCount: TextView
80
+
81
+ @Mock
82
+ private lateinit var singleNotification: Single <List <Notification >>
83
+
84
+ @Mock
85
+ private lateinit var compositeDisposable: CompositeDisposable
86
+
87
+ @Mock
88
+ private lateinit var okHttpJsonApiClient: OkHttpJsonApiClient
89
+
90
+ private lateinit var fragment: ContributionsFragment
91
+ private lateinit var context: Context
92
+ private lateinit var view: View
93
+ private lateinit var activity: MainActivity
94
+ private lateinit var nearbyNotificationCardView: NearbyNotificationCardView
95
+ private lateinit var campaignView: CampaignView
96
+
97
+ @Before
98
+ fun setUp () {
99
+ MockitoAnnotations .initMocks(this )
100
+
101
+ AppAdapter .set(TestAppAdapter ())
102
+
103
+ context = RuntimeEnvironment .application.applicationContext
104
+ activity = Robolectric .buildActivity(MainActivity ::class .java).create().get()
105
+
106
+
107
+ fragment = ContributionsFragment .newInstance()
108
+ val fragmentManager: FragmentManager = activity.supportFragmentManager
109
+ val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()
110
+ fragmentTransaction.add(fragment, null )
111
+ fragmentTransaction.commit()
112
+
113
+ layoutInflater = LayoutInflater .from(activity)
114
+ view = LayoutInflater .from(activity)
115
+ .inflate(R .layout.fragment_contributions, null ) as View
116
+
117
+ nearbyNotificationCardView = view.findViewById(R .id.card_view_nearby)
118
+ campaignView = view.findViewById(R .id.campaigns_view)
119
+
120
+ Whitebox .setInternalState(fragment, " contributionsListFragment" , contributionsListFragment)
121
+ Whitebox .setInternalState(fragment, " store" , store)
122
+ Whitebox .setInternalState(
123
+ fragment,
124
+ " limitedConnectionEnabledLayout" ,
125
+ limitedConnectionEnabledLayout
126
+ )
127
+ Whitebox .setInternalState(fragment, " notificationCount" , notificationCount)
128
+ Whitebox .setInternalState(fragment, " notificationController" , notificationController)
129
+ Whitebox .setInternalState(fragment, " compositeDisposable" , compositeDisposable)
130
+ Whitebox .setInternalState(fragment, " okHttpJsonApiClient" , okHttpJsonApiClient)
131
+ Whitebox .setInternalState(
132
+ fragment,
133
+ " nearbyNotificationCardView" ,
134
+ nearbyNotificationCardView
135
+ )
136
+ Whitebox .setInternalState(fragment, " campaignView" , campaignView)
137
+ }
138
+
139
+ @Test
140
+ @Throws(Exception ::class )
141
+ fun checkFragmentNotNull () {
142
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
143
+ Assert .assertNotNull(fragment)
144
+ }
145
+
146
+ @Test
147
+ @Throws(Exception ::class )
148
+ fun testGetMediaDetailPagerFragment () {
149
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
150
+ Whitebox .setInternalState(fragment, " mediaDetailPagerFragment" , mediaDetailPagerFragment)
151
+ Assert .assertEquals(fragment.mediaDetailPagerFragment, mediaDetailPagerFragment)
152
+ }
153
+
154
+ @Test
155
+ @Throws(Exception ::class )
156
+ fun testOnCreateOptionsMenu () {
157
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
158
+ `when `(menu.findItem(anyInt())).thenReturn(menuItem)
159
+ `when `(menuItem.actionView).thenReturn(notification)
160
+ fragment.onCreateOptionsMenu(menu, menuInflater)
161
+ }
162
+
163
+ @Test
164
+ @Throws(Exception ::class )
165
+ fun testSetNotificationCount () {
166
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
167
+ `when `(notificationController.getNotifications(anyBoolean())).thenReturn(singleNotification)
168
+ `when `(notificationController.getNotifications(anyBoolean()).subscribeOn(any())).thenReturn(
169
+ singleNotification
170
+ )
171
+ `when `(
172
+ notificationController.getNotifications(anyBoolean()).subscribeOn(any()).observeOn(
173
+ any()
174
+ )
175
+ ).thenReturn(singleNotification)
176
+ `when `(
177
+ notificationController.getNotifications(anyBoolean()).subscribeOn(any()).observeOn(
178
+ any()
179
+ ).subscribe()
180
+ ).thenReturn(compositeDisposable)
181
+ fragment.setNotificationCount()
182
+ }
183
+
184
+ @Test
185
+ @Throws(Exception ::class )
186
+ fun testInitNotificationViewsCaseEmptyList () {
187
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
188
+ val method: Method = ContributionsFragment ::class .java.getDeclaredMethod(
189
+ " initNotificationViews" ,
190
+ List ::class .java
191
+ )
192
+ method.isAccessible = true
193
+ method.invoke(fragment, listOf<Notification >())
194
+ }
195
+
196
+ @Test
197
+ @Throws(Exception ::class )
198
+ fun testInitNotificationViewsCaseNonEmptyList () {
199
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
200
+ val list: List <Notification > =
201
+ listOf (Notification (NotificationType .UNKNOWN , " " , " " , " " , " " , " " ))
202
+ val method: Method = ContributionsFragment ::class .java.getDeclaredMethod(
203
+ " initNotificationViews" ,
204
+ List ::class .java
205
+ )
206
+ method.isAccessible = true
207
+ method.invoke(fragment, list)
208
+ }
209
+
210
+ @Test
211
+ @Throws(Exception ::class )
212
+ fun testUpdateLimitedConnectionToggleCaseIsEnabled () {
213
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
214
+ `when `(menu.findItem(anyInt())).thenReturn(menuItem)
215
+ `when `(menuItem.actionView).thenReturn(notification)
216
+ `when `(store.getBoolean(anyString(), anyBoolean())).thenReturn(true )
217
+ fragment.updateLimitedConnectionToggle(menu)
218
+ }
219
+
220
+ @Test
221
+ @Throws(Exception ::class )
222
+ fun testOnAttach () {
223
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
224
+ fragment.onAttach(context)
225
+ }
226
+
227
+ @Test
228
+ @Throws(Exception ::class )
229
+ fun testNotifyDataSetChanged () {
230
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
231
+ Whitebox .setInternalState(fragment, " mediaDetailPagerFragment" , mediaDetailPagerFragment)
232
+ fragment.notifyDataSetChanged()
233
+ }
234
+
235
+ @Test
236
+ @Throws(Exception ::class )
237
+ fun testOnDestroyView () {
238
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
239
+ fragment.onDestroyView()
240
+ }
241
+
242
+ @Test
243
+ @Throws(Exception ::class )
244
+ fun testShowMessage () {
245
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
246
+ fragment.showMessage(" " )
247
+ }
248
+
249
+ @Test
250
+ @Throws(Exception ::class )
251
+ fun testShowCampaigns () {
252
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
253
+ fragment.showCampaigns(Campaign (" " , " " , " 2000-01-01" , " 2000-01-02" , " " ))
254
+ }
255
+
256
+ @Test
257
+ @Throws(Exception ::class )
258
+ fun testOnPause () {
259
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
260
+ fragment.onPause()
261
+ }
262
+
263
+ @Test
264
+ @Throws(Exception ::class )
265
+ fun testOnSaveInstanceState () {
266
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
267
+ fragment.onSaveInstanceState(Bundle ())
268
+ }
269
+
270
+ @Test
271
+ @Throws(Exception ::class )
272
+ fun testOnViewCreated () {
273
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
274
+ fragment.onViewCreated(campaignView, Bundle ())
275
+ }
276
+
277
+ @Test
278
+ @Throws(Exception ::class )
279
+ fun testOnResumeCaseNonNull () {
280
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
281
+ Whitebox .setInternalState(fragment, " mediaDetailPagerFragment" , mediaDetailPagerFragment)
282
+ fragment.onResume()
283
+ }
284
+
285
+ @Test
286
+ @Throws(Exception ::class )
287
+ fun testOnResumeCaseNullReady () {
288
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
289
+ fragment.onResume()
290
+ }
291
+
292
+ @Test
293
+ @Throws(Exception ::class )
294
+ fun testOnResumeCaseNullCaseIf () {
295
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
296
+ nearbyNotificationCardView.cardViewVisibilityState =
297
+ NearbyNotificationCardView .CardViewVisibilityState .READY
298
+ fragment.onResume()
299
+ }
300
+
301
+ @Test
302
+ @Throws(Exception ::class )
303
+ fun testOnResumeCaseNullCaseElse () {
304
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
305
+ `when `(store.getBoolean(" displayNearbyCardView" , true )).thenReturn(false )
306
+ fragment.onResume()
307
+ }
308
+
309
+ @Test
310
+ @Throws(Exception ::class )
311
+ fun testGetContributionStateAt () {
312
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
313
+ fragment.getContributionStateAt(0 )
314
+ }
315
+
316
+ @Test
317
+ @Throws(Exception ::class )
318
+ fun testViewPagerNotifyDataSetChanged () {
319
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
320
+ Whitebox .setInternalState(fragment, " mediaDetailPagerFragment" , mediaDetailPagerFragment)
321
+ fragment.viewPagerNotifyDataSetChanged()
322
+ }
323
+
324
+ @Test
325
+ @Throws(Exception ::class )
326
+ fun testGetMediaAtPosition () {
327
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
328
+ fragment.getMediaAtPosition(0 )
329
+ }
330
+
331
+ @Test
332
+ @Throws(Exception ::class )
333
+ fun testGetTotalMediaCount () {
334
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
335
+ fragment.totalMediaCount
336
+ }
337
+
338
+ @Test
339
+ @Throws(Exception ::class )
340
+ fun testRefreshNominatedMedia () {
341
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
342
+ fragment.refreshNominatedMedia(0 )
343
+ }
344
+
345
+ @Test
346
+ @Throws(Exception ::class )
347
+ fun testShowDetail () {
348
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
349
+ Whitebox .setInternalState(fragment, " mediaDetailPagerFragment" , mediaDetailPagerFragment)
350
+ `when `(mediaDetailPagerFragment.isVisible).thenReturn(false )
351
+ fragment.showDetail(0 , false )
352
+ }
353
+
354
+ }
0 commit comments