mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
Refactor BookmarkLocationsFragment to load favorites only when view is not null
* BookmarkLocationsFragment: load favorites locations only when view is not null. * BookmarkLocationsFragmentTest: added spy and verify to test onResume() call initList() method.
This commit is contained in:
parent
edd52debfb
commit
9c352b4ba2
2 changed files with 17 additions and 11 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package fr.free.nrw.commons.bookmarks.locations
|
||||
|
||||
import android.Manifest.permission
|
||||
import android.annotation.SuppressLint
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
|
|
@ -8,7 +9,9 @@ import android.view.ViewGroup
|
|||
import androidx.activity.result.ActivityResultLauncher
|
||||
import androidx.activity.result.contract.ActivityResultContracts.RequestMultiplePermissions
|
||||
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.lifecycle.repeatOnLifecycle
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import dagger.android.support.DaggerFragment
|
||||
import fr.free.nrw.commons.R
|
||||
|
|
@ -131,9 +134,11 @@ class BookmarkLocationsFragment : DaggerFragment() {
|
|||
|
||||
fun initList() {
|
||||
var places: List<Place>
|
||||
viewLifecycleOwner.lifecycleScope.launch {
|
||||
places = controller.loadFavoritesLocations()
|
||||
updateUIList(places)
|
||||
if(view != null) {
|
||||
viewLifecycleOwner.lifecycleScope.launch {
|
||||
places = controller.loadFavoritesLocations()
|
||||
updateUIList(places)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,12 +23,14 @@ import fr.free.nrw.commons.nearby.Place
|
|||
import fr.free.nrw.commons.nearby.fragments.CommonPlaceClickActions
|
||||
import fr.free.nrw.commons.nearby.fragments.PlaceAdapter
|
||||
import fr.free.nrw.commons.profile.ProfileActivity
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Assert
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Mock
|
||||
import org.mockito.Mockito.spy
|
||||
import org.mockito.MockitoAnnotations
|
||||
import org.powermock.reflect.Whitebox
|
||||
import org.robolectric.Robolectric
|
||||
|
|
@ -133,11 +135,11 @@ class BookmarkLocationFragmentUnitTests {
|
|||
fun testInitNonEmpty() {
|
||||
runBlocking {
|
||||
whenever(controller.loadFavoritesLocations()).thenReturn(mockBookmarkList)
|
||||
val method: Method =
|
||||
BookmarkLocationsFragment::class.java.getDeclaredMethod("initList")
|
||||
method.isAccessible = true
|
||||
method.invoke(fragment)
|
||||
}
|
||||
val method: Method =
|
||||
BookmarkLocationsFragment::class.java.getDeclaredMethod("initList")
|
||||
method.isAccessible = true
|
||||
method.invoke(fragment)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -173,11 +175,10 @@ class BookmarkLocationFragmentUnitTests {
|
|||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnResume() = runBlocking {
|
||||
val fragmentSpy = spy(fragment)
|
||||
whenever(controller.loadFavoritesLocations()).thenReturn(mockBookmarkList)
|
||||
|
||||
fragment.onResume()
|
||||
|
||||
verify(fragment).initList()
|
||||
verify(adapter).items = mockBookmarkList
|
||||
fragmentSpy.onResume()
|
||||
verify(fragmentSpy).initList()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue