1
+ package fr.free.nrw.commons.explore.depictions
2
+
3
+ import android.content.Intent
4
+ import android.view.LayoutInflater
5
+ import android.view.View
6
+ import android.widget.FrameLayout
7
+ import androidx.fragment.app.FragmentManager
8
+ import androidx.viewpager.widget.ViewPager
9
+ import com.google.android.material.tabs.TabLayout
10
+ import fr.free.nrw.commons.R
11
+ import fr.free.nrw.commons.TestAppAdapter
12
+ import fr.free.nrw.commons.TestCommonsApplication
13
+ import fr.free.nrw.commons.explore.depictions.media.DepictedImagesFragment
14
+ import fr.free.nrw.commons.media.MediaDetailPagerFragment
15
+ import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
16
+ import org.junit.Assert
17
+ import org.junit.Before
18
+ import org.junit.Test
19
+ import org.junit.runner.RunWith
20
+ import org.mockito.Mock
21
+ import org.mockito.Mockito.`when`
22
+ import org.mockito.MockitoAnnotations
23
+ import org.powermock.reflect.Whitebox
24
+ import org.robolectric.Robolectric
25
+ import org.robolectric.RobolectricTestRunner
26
+ import org.robolectric.RuntimeEnvironment
27
+ import org.robolectric.annotation.Config
28
+ import org.robolectric.annotation.LooperMode
29
+ import org.robolectric.fakes.RoboMenu
30
+ import org.robolectric.fakes.RoboMenuItem
31
+ import org.wikipedia.AppAdapter
32
+
33
+ @RunWith(RobolectricTestRunner ::class )
34
+ @Config(sdk = [21 ], application = TestCommonsApplication ::class )
35
+ @LooperMode(LooperMode .Mode .PAUSED )
36
+ class WikidataItemDetailsActivityUnitTests {
37
+
38
+ private lateinit var activity: WikidataItemDetailsActivity
39
+ private lateinit var parent: View
40
+
41
+ @Mock
42
+ private lateinit var mediaDetailPagerFragment: MediaDetailPagerFragment
43
+
44
+ @Mock
45
+ private lateinit var depictionImagesListFragment: DepictedImagesFragment
46
+
47
+ @Mock
48
+ private lateinit var supportFragmentManager: FragmentManager
49
+
50
+ @Mock
51
+ private lateinit var depictedItem: DepictedItem
52
+
53
+ @Mock
54
+ private lateinit var mediaContainer: FrameLayout
55
+
56
+ @Mock
57
+ private lateinit var tabLayout: TabLayout
58
+
59
+ @Mock
60
+ private lateinit var viewPager: ViewPager
61
+
62
+ @Before
63
+ fun setUp () {
64
+ MockitoAnnotations .initMocks(this )
65
+ AppAdapter .set(TestAppAdapter ())
66
+ val intent = Intent (
67
+ RuntimeEnvironment .application.applicationContext,
68
+ WikidataItemDetailsActivity ::class .java
69
+ )
70
+ intent.putExtra(" wikidataItemName" , " depictionName" )
71
+ intent.putExtra(" entityId" , 0 )
72
+ activity =
73
+ Robolectric .buildActivity(WikidataItemDetailsActivity ::class .java, intent).create()
74
+ .get()
75
+ Whitebox .setInternalState(activity, " mediaDetailPagerFragment" , mediaDetailPagerFragment)
76
+ Whitebox .setInternalState(
77
+ activity,
78
+ " depictionImagesListFragment" ,
79
+ depictionImagesListFragment
80
+ )
81
+ Whitebox .setInternalState(activity, " supportFragmentManager" , supportFragmentManager)
82
+
83
+ parent =
84
+ LayoutInflater .from(activity).inflate(R .layout.activity_wikidata_item_details, null )
85
+
86
+ mediaContainer = parent.findViewById(R .id.mediaContainer)
87
+ Whitebox .setInternalState(activity, " mediaContainer" , mediaContainer)
88
+
89
+ tabLayout = parent.findViewById(R .id.tab_layout)
90
+ Whitebox .setInternalState(activity, " tabLayout" , tabLayout)
91
+
92
+ viewPager = parent.findViewById(R .id.viewPager)
93
+ Whitebox .setInternalState(activity, " viewPager" , viewPager)
94
+
95
+ }
96
+
97
+ @Test
98
+ @Throws(Exception ::class )
99
+ fun checkActivityNotNull () {
100
+ Assert .assertNotNull(activity)
101
+ }
102
+
103
+ @Test
104
+ @Throws(Exception ::class )
105
+ fun testViewPagerNotifyDataSetChanged () {
106
+ activity.viewPagerNotifyDataSetChanged()
107
+ }
108
+
109
+ @Test
110
+ @Throws(Exception ::class )
111
+ fun testGetMediaAtPosition () {
112
+ activity.getMediaAtPosition(0 )
113
+ }
114
+
115
+ @Test
116
+ @Throws(Exception ::class )
117
+ fun testOnBackPressed () {
118
+ `when `(supportFragmentManager.backStackEntryCount).thenReturn(1 )
119
+ activity.onBackPressed()
120
+ }
121
+
122
+ @Test
123
+ @Throws(Exception ::class )
124
+ fun testOnBackPressedCaseReturn () {
125
+ `when `(supportFragmentManager.backStackEntryCount).thenReturn(1 )
126
+ `when `(mediaDetailPagerFragment.backButtonClicked()).thenReturn(true )
127
+ activity.onBackPressed()
128
+ }
129
+
130
+ @Test
131
+ @Throws(Exception ::class )
132
+ fun testGetTotalMediaCount () {
133
+ activity.totalMediaCount
134
+ }
135
+
136
+ @Test
137
+ @Throws(Exception ::class )
138
+ fun testGetContributionStateAt () {
139
+ Assert .assertEquals(activity.getContributionStateAt(0 ), null )
140
+ }
141
+
142
+ @Test
143
+ @Throws(Exception ::class )
144
+ fun testRefreshNominatedMedia () {
145
+ activity.refreshNominatedMedia(0 )
146
+ }
147
+
148
+ @Test
149
+ @Throws(Exception ::class )
150
+ fun testOnCreateOptionsMenu () {
151
+ Assert .assertEquals(activity.onCreateOptionsMenu(RoboMenu ()), true )
152
+ }
153
+
154
+ @Test
155
+ @Throws(Exception ::class )
156
+ fun testOnOptionsItemSelectedCaseOne () {
157
+ Assert .assertEquals(
158
+ activity.onOptionsItemSelected(RoboMenuItem (R .id.browser_actions_menu_items)),
159
+ true
160
+ )
161
+ }
162
+
163
+ @Test
164
+ @Throws(Exception ::class )
165
+ fun testOnOptionsItemSelectedCaseTwo () {
166
+ Assert .assertEquals(activity.onOptionsItemSelected(RoboMenuItem (android.R .id.home)), true )
167
+ }
168
+
169
+ @Test
170
+ @Throws(Exception ::class )
171
+ fun testOnOptionsItemSelected () {
172
+ Assert .assertEquals(activity.onOptionsItemSelected(RoboMenuItem ()), false )
173
+ }
174
+
175
+ @Test
176
+ @Throws(Exception ::class )
177
+ fun testStartYourself () {
178
+ WikidataItemDetailsActivity .startYourself(activity, depictedItem)
179
+ }
180
+
181
+ @Test
182
+ @Throws(Exception ::class )
183
+ fun testOnMediaClicked () {
184
+ activity.onMediaClicked(0 )
185
+ }
186
+
187
+ }
0 commit comments