forked from commons-app/apps-android-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MediaDetailPagerFragmentUnitTests.kt
202 lines (168 loc) · 5.38 KB
/
MediaDetailPagerFragmentUnitTests.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package fr.free.nrw.commons.media
import android.content.Context
import android.os.Bundle
import android.os.Looper
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import androidx.test.core.app.ApplicationProvider
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.soloader.SoLoader
import fr.free.nrw.commons.Media
import fr.free.nrw.commons.TestAppAdapter
import fr.free.nrw.commons.TestCommonsApplication
import fr.free.nrw.commons.auth.SessionManager
import fr.free.nrw.commons.explore.SearchActivity
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows
import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode
import org.wikipedia.AppAdapter
import java.lang.reflect.Field
import java.lang.reflect.Method
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [21], application = TestCommonsApplication::class)
@LooperMode(LooperMode.Mode.PAUSED)
class MediaDetailPagerFragmentUnitTests {
private lateinit var fragment: MediaDetailPagerFragment
private lateinit var context: Context
private lateinit var fragmentManager: FragmentManager
@Mock
private lateinit var outState: Bundle
@Mock
private lateinit var media: Media
@Mock
internal var sessionManager: SessionManager? = null
@InjectMocks
private lateinit var okHttpJsonApiClient: OkHttpJsonApiClient
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
context = ApplicationProvider.getApplicationContext()
AppAdapter.set(TestAppAdapter())
SoLoader.setInTestMode()
Fresco.initialize(context)
val activity = Robolectric.buildActivity(SearchActivity::class.java).create().get()
fragment = MediaDetailPagerFragment(false,true)
fragment = MediaDetailPagerFragment(false, false)
fragment = MediaDetailPagerFragment(false, false, 0)
fragmentManager = activity.supportFragmentManager
val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.add(fragment, null)
fragmentTransaction.commit()
val fieldContext: Field =
SessionManager::class.java.getDeclaredField("context")
fieldContext.isAccessible = true
fieldContext.set(sessionManager, context)
}
@Test
@Throws(Exception::class)
fun checkFragmentNotNull() {
Assert.assertNotNull(fragment)
}
@Test
@Throws(Exception::class)
fun testClearRemoved() {
fragment.clearRemoved()
}
@Test
@Throws(Exception::class)
fun testGetRemovedItems() {
fragment.removedItems
}
@Test
@Throws(Exception::class)
fun testOnCreateSaveInstanceNotNull() {
Shadows.shadowOf(Looper.getMainLooper()).idle()
fragment.onCreate(outState)
}
@Test
@Throws(Exception::class)
fun testGetMediaDetailProvider() {
fragment.mediaDetailProvider
}
@Test
@Throws(Exception::class)
fun testSetWallpaperCaseNull() {
val method: Method = MediaDetailPagerFragment::class.java.getDeclaredMethod(
"setWallpaper",
Media::class.java
)
method.isAccessible = true
method.invoke(fragment, media)
}
@Test
@Throws(Exception::class)
fun testSetWallpaperCaseNonNull() {
Shadows.shadowOf(Looper.getMainLooper()).idle()
`when`(media.imageUrl).thenReturn("url")
val method: Method = MediaDetailPagerFragment::class.java.getDeclaredMethod(
"setWallpaper",
Media::class.java
)
method.isAccessible = true
method.invoke(fragment, media)
}
@Test
@Throws(Exception::class)
fun testSetAvatarCaseNull() {
val method: Method = MediaDetailPagerFragment::class.java.getDeclaredMethod(
"setAvatar",
Media::class.java
)
method.isAccessible = true
method.invoke(fragment, media)
}
@Test
@Throws(Exception::class)
fun testShowImage() {
fragment.showImage(0, false)
}
@Test
@Throws(Exception::class)
fun testShowImageSingle() {
fragment.showImage(0)
}
@Test
@Throws(Exception::class)
fun testNotifyDataSetChangedCaseNull() {
fragment.notifyDataSetChanged()
}
@Test
@Throws(Exception::class)
fun testOnPageScrolled() {
fragment.onPageScrolled(0, 0.0F, 0)
}
@Test
@Throws(Exception::class)
fun testOnPageScrolledCaseNull() {
val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.remove(fragment)
fragmentTransaction.commit()
fragment.onPageScrolled(0, 0.0F, 0)
}
@Test
@Throws(Exception::class)
fun testOnPageSelected() {
fragment.onPageSelected(0)
}
@Test
@Throws(Exception::class)
fun testOnPageScrollStateChanged() {
fragment.onPageScrollStateChanged(0)
}
@Test
@Throws(Exception::class)
fun testOnDataSetChanged() {
fragment.onDataSetChanged()
}
}