1
+ package fr.free.nrw.commons.upload.categories
2
+
3
+ import android.content.Context
4
+ import android.os.Looper
5
+ import android.text.Editable
6
+ import android.view.LayoutInflater
7
+ import android.view.View
8
+ import android.widget.ImageView
9
+ import android.widget.ProgressBar
10
+ import android.widget.TextView
11
+ import androidx.fragment.app.FragmentManager
12
+ import androidx.fragment.app.FragmentTransaction
13
+ import androidx.recyclerview.widget.RecyclerView
14
+ import com.google.android.material.textfield.TextInputEditText
15
+ import com.google.android.material.textfield.TextInputLayout
16
+ import fr.free.nrw.commons.R
17
+ import fr.free.nrw.commons.TestAppAdapter
18
+ import fr.free.nrw.commons.TestCommonsApplication
19
+ import fr.free.nrw.commons.upload.UploadActivity
20
+ import fr.free.nrw.commons.upload.UploadBaseFragment
21
+ import io.reactivex.disposables.Disposable
22
+ import org.junit.Assert
23
+ import org.junit.Before
24
+ import org.junit.Test
25
+ import org.junit.runner.RunWith
26
+ import org.mockito.Mock
27
+ import org.mockito.Mockito.`when`
28
+ import org.mockito.MockitoAnnotations
29
+ import org.powermock.reflect.Whitebox
30
+ import org.robolectric.Robolectric
31
+ import org.robolectric.RobolectricTestRunner
32
+ import org.robolectric.RuntimeEnvironment
33
+ import org.robolectric.Shadows
34
+ import org.robolectric.annotation.Config
35
+ import org.robolectric.annotation.LooperMode
36
+ import org.wikipedia.AppAdapter
37
+ import java.lang.reflect.Method
38
+
39
+ @RunWith(RobolectricTestRunner ::class )
40
+ @Config(sdk = [21 ], application = TestCommonsApplication ::class )
41
+ @LooperMode(LooperMode .Mode .PAUSED )
42
+ class UploadCategoriesFragmentUnitTests {
43
+
44
+ private lateinit var fragment: UploadCategoriesFragment
45
+ private lateinit var context: Context
46
+ private lateinit var fragmentManager: FragmentManager
47
+ private lateinit var layoutInflater: LayoutInflater
48
+ private lateinit var view: View
49
+
50
+ @Mock
51
+ private lateinit var subscribe: Disposable
52
+
53
+ @Mock
54
+ private lateinit var pbCategories: ProgressBar
55
+
56
+ @Mock
57
+ private lateinit var tilContainerEtSearch: TextInputLayout
58
+
59
+ @Mock
60
+ private lateinit var etSearch: TextInputEditText
61
+
62
+ @Mock
63
+ private lateinit var rvCategories: RecyclerView
64
+
65
+ @Mock
66
+ private lateinit var tvTitle: TextView
67
+
68
+ @Mock
69
+ private lateinit var tooltip: ImageView
70
+
71
+ @Mock
72
+ private lateinit var editable: Editable
73
+
74
+ @Mock
75
+ private lateinit var adapter: UploadCategoryAdapter
76
+
77
+ @Mock
78
+ private lateinit var callback: UploadBaseFragment .Callback
79
+
80
+ @Mock
81
+ private lateinit var presenter: CategoriesContract .UserActionListener
82
+
83
+ @Before
84
+ fun setUp () {
85
+ MockitoAnnotations .initMocks(this )
86
+ context = RuntimeEnvironment .application.applicationContext
87
+ AppAdapter .set(TestAppAdapter ())
88
+ val activity = Robolectric .buildActivity(UploadActivity ::class .java).create().get()
89
+ fragment = UploadCategoriesFragment ()
90
+ fragmentManager = activity.supportFragmentManager
91
+ val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()
92
+ fragmentTransaction.add(fragment, null )
93
+ fragmentTransaction.commit()
94
+ layoutInflater = LayoutInflater .from(activity)
95
+ view = LayoutInflater .from(activity)
96
+ .inflate(R .layout.upload_categories_fragment, null ) as View
97
+ Whitebox .setInternalState(fragment, " subscribe" , subscribe)
98
+ Whitebox .setInternalState(fragment, " pbCategories" , pbCategories)
99
+ Whitebox .setInternalState(fragment, " tilContainerEtSearch" , tilContainerEtSearch)
100
+ Whitebox .setInternalState(fragment, " adapter" , adapter)
101
+ Whitebox .setInternalState(fragment, " callback" , callback)
102
+ Whitebox .setInternalState(fragment, " presenter" , presenter)
103
+ Whitebox .setInternalState(fragment, " etSearch" , etSearch)
104
+ Whitebox .setInternalState(fragment, " rvCategories" , rvCategories)
105
+ Whitebox .setInternalState(fragment, " tvTitle" , tvTitle)
106
+ Whitebox .setInternalState(fragment, " tooltip" , tooltip)
107
+ }
108
+
109
+ @Test
110
+ @Throws(Exception ::class )
111
+ fun checkFragmentNotNull () {
112
+ Assert .assertNotNull(fragment)
113
+ }
114
+
115
+ @Test
116
+ @Throws(Exception ::class )
117
+ fun testOnCreateView () {
118
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
119
+ fragment.onCreateView(layoutInflater,null , null )
120
+ }
121
+
122
+ @Test
123
+ @Throws(Exception ::class )
124
+ fun testOnViewCreated () {
125
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
126
+ fragment.onViewCreated(view, null )
127
+ }
128
+
129
+ @Test
130
+ @Throws(Exception ::class )
131
+ fun testOnDestroyView () {
132
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
133
+ fragment.onDestroyView()
134
+ }
135
+
136
+ @Test
137
+ @Throws(Exception ::class )
138
+ fun testShowProgress () {
139
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
140
+ fragment.showProgress(true )
141
+ }
142
+
143
+ @Test
144
+ @Throws(Exception ::class )
145
+ fun testShowErrorString () {
146
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
147
+ fragment.showError(" " )
148
+ }
149
+
150
+ @Test
151
+ @Throws(Exception ::class )
152
+ fun testShowErrorInt () {
153
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
154
+ fragment.showError(R .string.no_categories_found)
155
+ }
156
+
157
+ @Test
158
+ @Throws(Exception ::class )
159
+ fun testSetCategoriesCaseNull () {
160
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
161
+ fragment.setCategories(null )
162
+ }
163
+
164
+ @Test
165
+ @Throws(Exception ::class )
166
+ fun testSetCategoriesCaseNonNull () {
167
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
168
+ fragment.setCategories(listOf ())
169
+ }
170
+
171
+ @Test
172
+ @Throws(Exception ::class )
173
+ fun testGoToNextScreen () {
174
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
175
+ fragment.goToNextScreen()
176
+ }
177
+
178
+ @Test
179
+ @Throws(Exception ::class )
180
+ fun testShowNoCategorySelected () {
181
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
182
+ fragment.showNoCategorySelected()
183
+ }
184
+
185
+ @Test
186
+ @Throws(Exception ::class )
187
+ fun testOnNextButtonClicked () {
188
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
189
+ fragment.onNextButtonClicked()
190
+ }
191
+
192
+ @Test
193
+ @Throws(Exception ::class )
194
+ fun testOnPreviousButtonClicked () {
195
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
196
+ fragment.onPreviousButtonClicked()
197
+ }
198
+
199
+ @Test
200
+ @Throws(Exception ::class )
201
+ fun testOnBecameVisible () {
202
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
203
+ `when `(etSearch.text).thenReturn(editable)
204
+ val method: Method = UploadCategoriesFragment ::class .java.getDeclaredMethod(
205
+ " onBecameVisible"
206
+ )
207
+ method.isAccessible = true
208
+ method.invoke(fragment)
209
+ }
210
+
211
+ @Test
212
+ @Throws(Exception ::class )
213
+ fun testAddTextChangeListenerToEtSearch () {
214
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
215
+ val method: Method = UploadCategoriesFragment ::class .java.getDeclaredMethod(
216
+ " addTextChangeListenerToEtSearch"
217
+ )
218
+ method.isAccessible = true
219
+ method.invoke(fragment)
220
+ }
221
+
222
+ @Test
223
+ @Throws(Exception ::class )
224
+ fun testSearchForCategory () {
225
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
226
+ val method: Method = UploadCategoriesFragment ::class .java.getDeclaredMethod(
227
+ " searchForCategory" ,
228
+ String ::class .java
229
+ )
230
+ method.isAccessible = true
231
+ method.invoke(fragment, " " )
232
+ }
233
+
234
+ @Test
235
+ @Throws(Exception ::class )
236
+ fun testInitRecyclerView () {
237
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
238
+ val method: Method = UploadCategoriesFragment ::class .java.getDeclaredMethod(
239
+ " initRecyclerView"
240
+ )
241
+ method.isAccessible = true
242
+ method.invoke(fragment)
243
+ }
244
+
245
+ @Test
246
+ @Throws(Exception ::class )
247
+ fun testInit () {
248
+ Shadows .shadowOf(Looper .getMainLooper()).idle()
249
+ val method: Method = UploadCategoriesFragment ::class .java.getDeclaredMethod(
250
+ " init"
251
+ )
252
+ method.isAccessible = true
253
+ method.invoke(fragment)
254
+ }
255
+
256
+ }
0 commit comments