1
+ package fr.free.nrw.commons.bookmarks.locations
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.ProgressBar
8
+ import android.widget.RelativeLayout
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.nhaarman.mockitokotlin2.whenever
14
+ import fr.free.nrw.commons.R
15
+ import fr.free.nrw.commons.TestAppAdapter
16
+ import fr.free.nrw.commons.TestCommonsApplication
17
+ import fr.free.nrw.commons.contributions.ContributionController
18
+ import fr.free.nrw.commons.kvstore.JsonKvStore
19
+ import fr.free.nrw.commons.nearby.Place
20
+ import fr.free.nrw.commons.nearby.fragments.CommonPlaceClickActions
21
+ import fr.free.nrw.commons.nearby.fragments.PlaceAdapter
22
+ import fr.free.nrw.commons.profile.ProfileActivity
23
+ import org.junit.Assert
24
+ import org.junit.Before
25
+ import org.junit.Test
26
+ import org.junit.runner.RunWith
27
+ import org.mockito.Mock
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.annotation.Config
34
+ import org.robolectric.annotation.LooperMode
35
+ import org.wikipedia.AppAdapter
36
+ import java.lang.reflect.Method
37
+ import java.util.ArrayList
38
+
39
+ @RunWith(RobolectricTestRunner ::class )
40
+ @Config(sdk = [21 ], application = TestCommonsApplication ::class )
41
+ @LooperMode(LooperMode .Mode .PAUSED )
42
+ class BookmarkLocationFragmentUnitTests {
43
+
44
+ private lateinit var fragment: BookmarkLocationsFragment
45
+ private lateinit var context: Context
46
+ private lateinit var view: View
47
+ private lateinit var statusTextView: TextView
48
+ private lateinit var progressBar: ProgressBar
49
+ private lateinit var recyclerView: RecyclerView
50
+ private lateinit var commonPlaceClickActions: CommonPlaceClickActions
51
+ private lateinit var layoutInflater: LayoutInflater
52
+
53
+ @Mock
54
+ lateinit var store: JsonKvStore
55
+
56
+ @Mock
57
+ private lateinit var parentLayout: RelativeLayout
58
+
59
+ @Mock
60
+ private lateinit var savedInstanceState: Bundle
61
+
62
+ @Mock
63
+ private lateinit var bookmarkLocationDao: BookmarkLocationsDao
64
+
65
+ @Mock
66
+ private lateinit var controller: BookmarkLocationsController
67
+
68
+ @Mock
69
+ private lateinit var contributionController: ContributionController
70
+
71
+ @Mock
72
+ private lateinit var adapter: PlaceAdapter
73
+
74
+ /* *
75
+ * Get Mock bookmark list.
76
+ */
77
+ private val mockBookmarkList: List <Place >
78
+ private get() {
79
+ val list = ArrayList <Place >()
80
+ list.add(Place (" en" ," a place" ,null ," a description" ,null ," a cat" ,null ,null ,true ))
81
+ return list
82
+ }
83
+
84
+ /* *
85
+ * fragment Setup
86
+ */
87
+ @Before
88
+ fun setUp () {
89
+ MockitoAnnotations .initMocks(this )
90
+ context = RuntimeEnvironment .application.applicationContext
91
+ AppAdapter .set(TestAppAdapter ())
92
+ val activity = Robolectric .buildActivity(ProfileActivity ::class .java).create().get()
93
+ fragment = BookmarkLocationsFragment .newInstance()
94
+ val fragmentManager: FragmentManager = activity.supportFragmentManager
95
+ val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()
96
+ fragmentTransaction.add(fragment, null )
97
+ fragmentTransaction.commit()
98
+
99
+ layoutInflater = LayoutInflater .from(activity)
100
+ view = layoutInflater
101
+ .inflate(R .layout.fragment_bookmarks_locations, null ) as View
102
+
103
+ statusTextView = view.findViewById(R .id.statusMessage)
104
+ progressBar = view.findViewById(R .id.loadingImagesProgressBar)
105
+ recyclerView = view.findViewById(R .id.listView)
106
+ commonPlaceClickActions = CommonPlaceClickActions (store,activity,contributionController)
107
+
108
+ fragment.statusTextView = statusTextView
109
+ fragment.progressBar = progressBar
110
+ fragment.recyclerView = recyclerView
111
+ fragment.parentLayout = parentLayout
112
+ fragment.bookmarkLocationDao = bookmarkLocationDao
113
+ fragment.controller = controller
114
+ fragment.commonPlaceClickActions = commonPlaceClickActions
115
+ Whitebox .setInternalState(fragment, " adapter" , adapter)
116
+
117
+ }
118
+
119
+ /* *
120
+ * test init places when non empty
121
+ */
122
+ @Test
123
+ fun testInitNonEmpty (){
124
+ whenever(controller.loadFavoritesLocations()).thenReturn(mockBookmarkList)
125
+ val method: Method =
126
+ BookmarkLocationsFragment ::class .java.getDeclaredMethod(" initList" )
127
+ method.isAccessible = true
128
+ method.invoke(fragment)
129
+ }
130
+
131
+ /* *
132
+ * test onCreateView
133
+ */
134
+ @Test
135
+ @Throws(Exception ::class )
136
+ fun testOnCreateView () {
137
+ fragment.onCreateView(layoutInflater,null ,savedInstanceState)
138
+ }
139
+
140
+ /* *
141
+ * check fragment notnull
142
+ */
143
+ @Test
144
+ @Throws(Exception ::class )
145
+ fun checkFragmentNotNull () {
146
+ Assert .assertNotNull(fragment)
147
+ }
148
+
149
+ /* *
150
+ * test onViewCreated
151
+ */
152
+ @Test
153
+ @Throws(Exception ::class )
154
+ fun testOnViewCreated () {
155
+ fragment.onViewCreated(view, savedInstanceState)
156
+ }
157
+
158
+ /* *
159
+ * test onResume
160
+ */
161
+ @Test
162
+ @Throws(Exception ::class )
163
+ fun testOnResume () {
164
+ fragment.onResume()
165
+ }
166
+ }
0 commit comments