diff --git a/app/src/main/java/fr/free/nrw/commons/bookmarks/locations/BookmarkLocationsFragment.kt b/app/src/main/java/fr/free/nrw/commons/bookmarks/locations/BookmarkLocationsFragment.kt index 212c95105..27fd6830d 100644 --- a/app/src/main/java/fr/free/nrw/commons/bookmarks/locations/BookmarkLocationsFragment.kt +++ b/app/src/main/java/fr/free/nrw/commons/bookmarks/locations/BookmarkLocationsFragment.kt @@ -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 - viewLifecycleOwner.lifecycleScope.launch { - places = controller.loadFavoritesLocations() - updateUIList(places) + if(view != null) { + viewLifecycleOwner.lifecycleScope.launch { + places = controller.loadFavoritesLocations() + updateUIList(places) + } } } diff --git a/app/src/test/kotlin/fr/free/nrw/commons/bookmarks/locations/BookmarkLocationFragmentUnitTests.kt b/app/src/test/kotlin/fr/free/nrw/commons/bookmarks/locations/BookmarkLocationFragmentUnitTests.kt index d824c0005..52121bf84 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/bookmarks/locations/BookmarkLocationFragmentUnitTests.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/bookmarks/locations/BookmarkLocationFragmentUnitTests.kt @@ -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() } }