1
+ package fr.free.nrw.commons.contributions
2
+
3
+ import android.net.Uri
4
+ import android.os.Looper
5
+ import android.view.LayoutInflater
6
+ import android.view.View
7
+ import android.widget.ImageButton
8
+ import android.widget.ProgressBar
9
+ import android.widget.RelativeLayout
10
+ import android.widget.TextView
11
+ import com.facebook.drawee.backends.pipeline.Fresco
12
+ import com.facebook.drawee.view.SimpleDraweeView
13
+ import com.facebook.soloader.SoLoader
14
+ import fr.free.nrw.commons.Media
15
+ import fr.free.nrw.commons.R
16
+ import fr.free.nrw.commons.TestCommonsApplication
17
+ import fr.free.nrw.commons.media.MediaClient
18
+ import fr.free.nrw.commons.profile.ProfileActivity
19
+ import io.reactivex.disposables.CompositeDisposable
20
+ import org.junit.Assert
21
+ import org.junit.Before
22
+ import org.junit.Test
23
+ import org.junit.runner.RunWith
24
+ import org.mockito.Mock
25
+ import org.mockito.Mockito.`when`
26
+ import org.mockito.MockitoAnnotations
27
+ import org.powermock.reflect.Whitebox
28
+ import org.robolectric.Robolectric
29
+ import org.robolectric.RobolectricTestRunner
30
+ import org.robolectric.RuntimeEnvironment
31
+ import org.robolectric.Shadows
32
+ import org.robolectric.annotation.Config
33
+ import org.robolectric.annotation.LooperMode
34
+ import java.lang.reflect.Method
35
+
36
+ @RunWith(RobolectricTestRunner ::class )
37
+ @Config(sdk = [21 ], application = TestCommonsApplication ::class )
38
+ @LooperMode(LooperMode .Mode .PAUSED )
39
+ class ContributionViewHolderUnitTests {
40
+
41
+ private lateinit var contributionViewHolder: ContributionViewHolder
42
+ private lateinit var activity: ProfileActivity
43
+ private lateinit var parent: View
44
+ private lateinit var pauseResumeButton: ImageButton
45
+ private lateinit var addToWikipediaButton: ImageButton
46
+ private lateinit var cancelButton: ImageButton
47
+ private lateinit var retryButton: ImageButton
48
+ private lateinit var imageOptions: RelativeLayout
49
+ private lateinit var imageView: SimpleDraweeView
50
+ private lateinit var titleView: TextView
51
+ private lateinit var authorView: TextView
52
+ private lateinit var stateView: TextView
53
+ private lateinit var seqNumView: TextView
54
+ private lateinit var progressView: ProgressBar
55
+
56
+ @Mock
57
+ private lateinit var callback: ContributionsListAdapter .Callback
58
+
59
+ @Mock
60
+ private lateinit var mediaClient: MediaClient
61
+
62
+ @Mock
63
+ private lateinit var uri: Uri
64
+
65
+ @Mock
66
+ private lateinit var contribution: Contribution
67
+
68
+ @Mock
69
+ private lateinit var compositeDisposable: CompositeDisposable
70
+
71
+ @Mock
72
+ private lateinit var media: Media
73
+
74
+ @Before
75
+ fun setUp () {
76
+ MockitoAnnotations .initMocks(this )
77
+ SoLoader .setInTestMode()
78
+ Fresco .initialize(RuntimeEnvironment .application.applicationContext)
79
+ activity = Robolectric .buildActivity(ProfileActivity ::class .java).create().get()
80
+ parent = LayoutInflater .from(activity).inflate(R .layout.layout_contribution, null )
81
+ contributionViewHolder = ContributionViewHolder (parent, callback, mediaClient)
82
+
83
+ pauseResumeButton = parent.findViewById(R .id.pauseResumeButton)
84
+ Whitebox .setInternalState(contributionViewHolder, " pauseResumeButton" , pauseResumeButton)
85
+
86
+ addToWikipediaButton = parent.findViewById(R .id.wikipediaButton)
87
+ Whitebox .setInternalState(
88
+ contributionViewHolder,
89
+ " addToWikipediaButton" ,
90
+ addToWikipediaButton
91
+ )
92
+
93
+ cancelButton = parent.findViewById(R .id.cancelButton)
94
+ Whitebox .setInternalState(contributionViewHolder, " cancelButton" , cancelButton)
95
+
96
+ retryButton = parent.findViewById(R .id.retryButton)
97
+ Whitebox .setInternalState(contributionViewHolder, " retryButton" , retryButton)
98
+
99
+ imageOptions = parent.findViewById(R .id.image_options)
100
+ Whitebox .setInternalState(contributionViewHolder, " imageOptions" , imageOptions)
101
+
102
+ imageView = parent.findViewById(R .id.contributionImage)
103
+ Whitebox .setInternalState(contributionViewHolder, " imageView" , imageView)
104
+
105
+ titleView = parent.findViewById(R .id.contributionTitle)
106
+ Whitebox .setInternalState(contributionViewHolder, " titleView" , titleView)
107
+
108
+ authorView = parent.findViewById(R .id.authorView)
109
+ Whitebox .setInternalState(contributionViewHolder, " authorView" , authorView)
110
+
111
+ stateView = parent.findViewById(R .id.contributionState)
112
+ Whitebox .setInternalState(contributionViewHolder, " stateView" , stateView)
113
+
114
+ seqNumView = parent.findViewById(R .id.contributionSequenceNumber)
115
+ Whitebox .setInternalState(contributionViewHolder, " seqNumView" , seqNumView)
116
+
117
+ progressView = parent.findViewById(R .id.contributionProgress)
118
+ Whitebox .setInternalState(contributionViewHolder, " progressView" , progressView)
119
+
120
+
121
+ Whitebox .setInternalState(
122
+ contributionViewHolder,
123
+ " compositeDisposable" ,
124
+ compositeDisposable
125
+ )
126
+
127
+ }
128
+
129
+ @Test
130
+ @Throws(Exception ::class )
131
+ fun checkNotNull () {
132
+ Assert .assertNotNull(contributionViewHolder)
133
+ }
134
+
135
+ @Test
136
+ @Throws(Exception ::class )
137
+ fun testSetResume () {
138
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
139
+ val method: Method = ContributionViewHolder ::class .java.getDeclaredMethod(
140
+ " setResume"
141
+ )
142
+ method.isAccessible = true
143
+ method.invoke(contributionViewHolder)
144
+ }
145
+
146
+ @Test
147
+ @Throws(Exception ::class )
148
+ fun testSetPaused () {
149
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
150
+ val method: Method = ContributionViewHolder ::class .java.getDeclaredMethod(
151
+ " setPaused"
152
+ )
153
+ method.isAccessible = true
154
+ method.invoke(contributionViewHolder)
155
+ }
156
+
157
+ @Test
158
+ @Throws(Exception ::class )
159
+ fun testPause () {
160
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
161
+ val method: Method = ContributionViewHolder ::class .java.getDeclaredMethod(
162
+ " pause"
163
+ )
164
+ method.isAccessible = true
165
+ method.invoke(contributionViewHolder)
166
+ }
167
+
168
+ @Test
169
+ @Throws(Exception ::class )
170
+ fun testResume () {
171
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
172
+ val method: Method = ContributionViewHolder ::class .java.getDeclaredMethod(
173
+ " resume"
174
+ )
175
+ method.isAccessible = true
176
+ method.invoke(contributionViewHolder)
177
+ }
178
+
179
+ @Test
180
+ @Throws(Exception ::class )
181
+ fun testOnPauseResumeButtonClickedCaseTrue () {
182
+ contributionViewHolder.onPauseResumeButtonClicked()
183
+ }
184
+
185
+ @Test
186
+ @Throws(Exception ::class )
187
+ fun testOnPauseResumeButtonClickedCaseFalse () {
188
+ pauseResumeButton.tag = " "
189
+ contributionViewHolder.onPauseResumeButtonClicked()
190
+ }
191
+
192
+ @Test
193
+ @Throws(Exception ::class )
194
+ fun testWikipediaButtonClicked () {
195
+ contributionViewHolder.wikipediaButtonClicked()
196
+ }
197
+
198
+ @Test
199
+ @Throws(Exception ::class )
200
+ fun testImageClicked () {
201
+ contributionViewHolder.imageClicked()
202
+ }
203
+
204
+ @Test
205
+ @Throws(Exception ::class )
206
+ fun testDeleteUpload () {
207
+ contributionViewHolder.deleteUpload()
208
+ }
209
+
210
+ @Test
211
+ @Throws(Exception ::class )
212
+ fun testRetryUpload () {
213
+ contributionViewHolder.retryUpload()
214
+ }
215
+
216
+ @Test
217
+ @Throws(Exception ::class )
218
+ fun testChooseImageSource () {
219
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
220
+ val method: Method = ContributionViewHolder ::class .java.getDeclaredMethod(
221
+ " chooseImageSource" ,
222
+ String ::class .java,
223
+ Uri ::class .java
224
+ )
225
+ method.isAccessible = true
226
+ method.invoke(contributionViewHolder, " " , uri)
227
+ }
228
+
229
+ @Test
230
+ @Throws(Exception ::class )
231
+ fun testDisplayWikipediaButton () {
232
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
233
+ val method: Method = ContributionViewHolder ::class .java.getDeclaredMethod(
234
+ " displayWikipediaButton" ,
235
+ Boolean ::class .javaObjectType
236
+ )
237
+ method.isAccessible = true
238
+ method.invoke(contributionViewHolder, false )
239
+ }
240
+
241
+ @Test
242
+ @Throws(Exception ::class )
243
+ fun testCheckIfMediaExistsOnWikipediaPageCaseNull () {
244
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
245
+ `when `(contribution.wikidataPlace).thenReturn(null )
246
+ val method: Method = ContributionViewHolder ::class .java.getDeclaredMethod(
247
+ " checkIfMediaExistsOnWikipediaPage" ,
248
+ Contribution ::class .java
249
+ )
250
+ method.isAccessible = true
251
+ method.invoke(contributionViewHolder, contribution)
252
+ }
253
+
254
+ @Test
255
+ @Throws(Exception ::class )
256
+ fun testInitCaseNull () {
257
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
258
+ contributionViewHolder.init (0 , null )
259
+ }
260
+
261
+ @Test
262
+ @Throws(Exception ::class )
263
+ fun testInitCaseNonNull_STATE_COMPLETED () {
264
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
265
+ `when `(contribution.state).thenReturn(Contribution .STATE_COMPLETED )
266
+ `when `(contribution.media).thenReturn(media)
267
+ `when `(media.mostRelevantCaption).thenReturn(" " )
268
+ `when `(media.author).thenReturn(" " )
269
+ contributionViewHolder.init (0 , contribution)
270
+ }
271
+
272
+ @Test
273
+ @Throws(Exception ::class )
274
+ fun testInitCaseNonNull_STATE_QUEUED () {
275
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
276
+ `when `(contribution.state).thenReturn(Contribution .STATE_QUEUED )
277
+ `when `(contribution.media).thenReturn(media)
278
+ `when `(media.mostRelevantCaption).thenReturn(" " )
279
+ `when `(media.author).thenReturn(" " )
280
+ contributionViewHolder.init (0 , contribution)
281
+ }
282
+
283
+ @Test
284
+ @Throws(Exception ::class )
285
+ fun testInitCaseNonNull_STATE_QUEUED_LIMITED_CONNECTION_MODE () {
286
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
287
+ `when `(contribution.state).thenReturn(Contribution .STATE_QUEUED_LIMITED_CONNECTION_MODE )
288
+ `when `(contribution.media).thenReturn(media)
289
+ `when `(media.mostRelevantCaption).thenReturn(" " )
290
+ `when `(media.author).thenReturn(" " )
291
+ contributionViewHolder.init (0 , contribution)
292
+ }
293
+
294
+ @Test
295
+ @Throws(Exception ::class )
296
+ fun testInitCaseNonNull_STATE_IN_PROGRESS () {
297
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
298
+ `when `(contribution.state).thenReturn(Contribution .STATE_IN_PROGRESS )
299
+ `when `(contribution.media).thenReturn(media)
300
+ `when `(media.mostRelevantCaption).thenReturn(" " )
301
+ `when `(media.author).thenReturn(" " )
302
+ contributionViewHolder.init (0 , contribution)
303
+ }
304
+
305
+ @Test
306
+ @Throws(Exception ::class )
307
+ fun testInitCaseNonNull_STATE_PAUSED () {
308
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
309
+ `when `(contribution.state).thenReturn(Contribution .STATE_PAUSED )
310
+ `when `(contribution.media).thenReturn(media)
311
+ `when `(media.mostRelevantCaption).thenReturn(" " )
312
+ `when `(media.author).thenReturn(" " )
313
+ contributionViewHolder.init (0 , contribution)
314
+ }
315
+
316
+ @Test
317
+ @Throws(Exception ::class )
318
+ fun testInitCaseNonNull_STATE_FAILED () {
319
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
320
+ `when `(contribution.state).thenReturn(Contribution .STATE_FAILED )
321
+ `when `(contribution.media).thenReturn(media)
322
+ `when `(media.mostRelevantCaption).thenReturn(" " )
323
+ `when `(media.author).thenReturn(" " )
324
+ contributionViewHolder.init (0 , contribution)
325
+ }
326
+
327
+ }
0 commit comments