1
+ package fr.free.nrw.commons.upload.depicts
2
+
3
+ import android.content.Context
4
+ import android.os.Bundle
5
+ import android.view.LayoutInflater
6
+ import android.view.View
7
+ import android.widget.ImageView
8
+ import android.widget.ProgressBar
9
+ import android.widget.TextView
10
+ import androidx.fragment.app.FragmentManager
11
+ import androidx.fragment.app.FragmentTransaction
12
+ import androidx.recyclerview.widget.RecyclerView
13
+ import com.google.android.material.textfield.TextInputEditText
14
+ import com.google.android.material.textfield.TextInputLayout
15
+ import fr.free.nrw.commons.R
16
+ import fr.free.nrw.commons.TestAppAdapter
17
+ import fr.free.nrw.commons.TestCommonsApplication
18
+ import fr.free.nrw.commons.upload.UploadActivity
19
+ import fr.free.nrw.commons.upload.UploadBaseFragment
20
+ import io.reactivex.disposables.Disposable
21
+ import org.junit.Assert
22
+ import org.junit.Before
23
+ import org.junit.Test
24
+ import org.junit.runner.RunWith
25
+ import org.mockito.Mock
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.annotation.Config
32
+ import org.robolectric.annotation.LooperMode
33
+ import org.wikipedia.AppAdapter
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 DepictsFragmentUnitTests {
40
+
41
+ private lateinit var fragment: DepictsFragment
42
+ private lateinit var fragmentManager: FragmentManager
43
+ private lateinit var layoutInflater: LayoutInflater
44
+ private lateinit var view: View
45
+ private lateinit var context: Context
46
+
47
+ @Mock
48
+ private lateinit var savedInstanceState: Bundle
49
+
50
+ @Mock
51
+ private lateinit var textView: TextView
52
+
53
+ @Mock
54
+ private lateinit var imageView: ImageView
55
+
56
+ @Mock
57
+ private lateinit var recyclerView: RecyclerView
58
+
59
+ @Mock
60
+ private lateinit var textInputEditText: TextInputEditText
61
+
62
+ @Mock
63
+ private lateinit var progressBar: ProgressBar
64
+
65
+ @Mock
66
+ private lateinit var textInputLayout: TextInputLayout
67
+
68
+ @Mock
69
+ private lateinit var callback: UploadBaseFragment .Callback
70
+
71
+ @Mock
72
+ private lateinit var disposable: Disposable
73
+
74
+ @Mock
75
+ private lateinit var adapter: UploadDepictsAdapter
76
+
77
+ @Before
78
+ fun setUp () {
79
+ MockitoAnnotations .initMocks(this )
80
+ context = RuntimeEnvironment .application.applicationContext
81
+ AppAdapter .set(TestAppAdapter ())
82
+
83
+ val activity = Robolectric .buildActivity(UploadActivity ::class .java).create().get()
84
+ fragment = DepictsFragment ()
85
+ fragmentManager = activity.supportFragmentManager
86
+ val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()
87
+ fragmentTransaction.add(fragment, null )
88
+ fragmentTransaction.commitNowAllowingStateLoss()
89
+
90
+ layoutInflater = LayoutInflater .from(activity)
91
+
92
+ view = LayoutInflater .from(activity)
93
+ .inflate(R .layout.upload_depicts_fragment, null ) as View
94
+
95
+ Whitebox .setInternalState(fragment, " depictsTitle" , textView)
96
+ Whitebox .setInternalState(fragment, " callback" , callback)
97
+ Whitebox .setInternalState(fragment, " tooltip" , imageView)
98
+ Whitebox .setInternalState(fragment, " depictsSubTitle" , textView)
99
+ Whitebox .setInternalState(fragment, " depictsRecyclerView" , recyclerView)
100
+ Whitebox .setInternalState(fragment, " depictsSearch" , textInputEditText)
101
+ Whitebox .setInternalState(fragment, " depictsSearchContainer" , textInputLayout)
102
+ Whitebox .setInternalState(fragment, " depictsSearchInProgress" , progressBar)
103
+ Whitebox .setInternalState(fragment, " subscribe" , disposable)
104
+ Whitebox .setInternalState(fragment, " adapter" , adapter)
105
+ }
106
+
107
+ @Test
108
+ @Throws(Exception ::class )
109
+ fun checkFragmentNotNull () {
110
+ Assert .assertNotNull(fragment)
111
+ }
112
+
113
+ @Test
114
+ @Throws(Exception ::class )
115
+ fun testOnCreateView () {
116
+ fragment.onCreateView(layoutInflater, null , savedInstanceState)
117
+ }
118
+
119
+ @Test
120
+ @Throws(Exception ::class )
121
+ fun testInit () {
122
+ val method: Method = DepictsFragment ::class .java.getDeclaredMethod(
123
+ " init"
124
+ )
125
+ method.isAccessible = true
126
+ method.invoke(fragment)
127
+ }
128
+
129
+ @Test
130
+ @Throws(Exception ::class )
131
+ fun testOnBecameVisible () {
132
+ val method: Method = DepictsFragment ::class .java.getDeclaredMethod(
133
+ " onBecameVisible"
134
+ )
135
+ method.isAccessible = true
136
+ method.invoke(fragment)
137
+ }
138
+
139
+ @Test
140
+ @Throws(Exception ::class )
141
+ fun testGoToNextScreen () {
142
+ fragment.goToNextScreen()
143
+ }
144
+
145
+ @Test
146
+ @Throws(Exception ::class )
147
+ fun testGoToPreviousScreen () {
148
+ fragment.goToPreviousScreen()
149
+ }
150
+
151
+ @Test
152
+ @Throws(Exception ::class )
153
+ fun testNoDepictionSelected () {
154
+ fragment.noDepictionSelected()
155
+ }
156
+
157
+ @Test
158
+ @Throws(Exception ::class )
159
+ fun testOnDestroyView () {
160
+ fragment.onDestroyView()
161
+ }
162
+
163
+ @Test
164
+ @Throws(Exception ::class )
165
+ fun testShowProgress () {
166
+ fragment.showProgress(true )
167
+ }
168
+
169
+ @Test
170
+ @Throws(Exception ::class )
171
+ fun testShowErrorCaseTrue () {
172
+ fragment.showError(true )
173
+ }
174
+
175
+ @Test
176
+ @Throws(Exception ::class )
177
+ fun testShowErrorCaseFalse () {
178
+ fragment.showError(false )
179
+ }
180
+
181
+ @Test
182
+ @Throws(Exception ::class )
183
+ fun testSetDepictsList () {
184
+ fragment.setDepictsList(listOf ())
185
+ }
186
+
187
+ @Test
188
+ @Throws(Exception ::class )
189
+ fun testOnNextButtonClicked () {
190
+ fragment.onNextButtonClicked()
191
+ }
192
+
193
+ @Test
194
+ @Throws(Exception ::class )
195
+ fun testOnPreviousButtonClicked () {
196
+ fragment.onPreviousButtonClicked()
197
+ }
198
+
199
+ @Test
200
+ @Throws(Exception ::class )
201
+ fun testSearchForDepictions () {
202
+ val method: Method = DepictsFragment ::class .java.getDeclaredMethod(
203
+ " searchForDepictions" ,
204
+ String ::class .java
205
+ )
206
+ method.isAccessible = true
207
+ method.invoke(fragment, " " )
208
+ }
209
+
210
+ }
0 commit comments