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 a51d02b10..212c95105 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 @@ -129,7 +129,7 @@ class BookmarkLocationsFragment : DaggerFragment() { initList() } - private fun initList() { + fun initList() { var places: List viewLifecycleOwner.lifecycleScope.launch { places = controller.loadFavoritesLocations() diff --git a/app/src/test/kotlin/fr/free/nrw/commons/bookmarks/locations/BookMarkLocationDaoTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/bookmarks/locations/BookMarkLocationDaoTest.kt index f1c3f9798..4808d8518 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/bookmarks/locations/BookMarkLocationDaoTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/bookmarks/locations/BookMarkLocationDaoTest.kt @@ -42,8 +42,7 @@ import org.robolectric.annotation.Config @Config(sdk = [21], application = TestCommonsApplication::class) class BookMarkLocationDaoTest { - @Mock - var bookmarkLocationsDao: BookmarkLocationsDao? = null + private lateinit var bookmarkLocationsDao: BookmarkLocationsDao private lateinit var database: AppDatabase @@ -92,17 +91,67 @@ class BookMarkLocationDaoTest { } @Test - fun insertAndRetrieveBookmark() = runBlocking { - // Insert a bookmark - bookmarkLocationsDao?.addBookmarkLocation(examplePlaceBookmark.toBookmarksLocations()) + fun testForAddAndGetAllBookmarkLocations() = runBlocking { + bookmarkLocationsDao.addBookmarkLocation(examplePlaceBookmark.toBookmarksLocations()) - // Retrieve all bookmarks - val bookmarks = bookmarkLocationsDao?.getAllBookmarksLocations() + val bookmarks = bookmarkLocationsDao.getAllBookmarksLocations() - // Assert the bookmark exists - assertEquals(1, bookmarks?.size) - val retrievedBookmark = bookmarks?.first() - assertEquals(examplePlaceBookmark.name, retrievedBookmark?.locationName) - assertEquals(examplePlaceBookmark.language, retrievedBookmark?.locationLanguage) + assertEquals(1, bookmarks.size) + val retrievedBookmark = bookmarks.first() + assertEquals(examplePlaceBookmark.name, retrievedBookmark.locationName) + assertEquals(examplePlaceBookmark.language, retrievedBookmark.locationLanguage) + } + + @Test + fun testFindBookmarkByNameForTrue() = runBlocking { + bookmarkLocationsDao.addBookmarkLocation(examplePlaceBookmark.toBookmarksLocations()) + + val exists = bookmarkLocationsDao.findBookmarkLocation(examplePlaceBookmark.name) + assertTrue(exists) + } + + @Test + fun testFindBookmarkByNameForFalse() = runBlocking { + bookmarkLocationsDao.addBookmarkLocation(examplePlaceBookmark.toBookmarksLocations()) + + val exists = bookmarkLocationsDao.findBookmarkLocation("xyz") + assertFalse(exists) + } + + @Test + fun testDeleteBookmark() = runBlocking { + val bookmarkLocation = examplePlaceBookmark.toBookmarksLocations() + bookmarkLocationsDao.addBookmarkLocation(bookmarkLocation) + + bookmarkLocationsDao.deleteBookmarkLocation(bookmarkLocation) + + val bookmarks = bookmarkLocationsDao.getAllBookmarksLocations() + assertTrue(bookmarks.isEmpty()) + } + + @Test + fun testUpdateBookmarkForTrue() = runBlocking { + val exists = bookmarkLocationsDao.updateBookmarkLocation(examplePlaceBookmark) + + assertTrue(exists) + } + + @Test + fun testUpdateBookmarkForFalse() = runBlocking { + val newBookmark = examplePlaceBookmark.toBookmarksLocations() + bookmarkLocationsDao.addBookmarkLocation(newBookmark) + + val exists = bookmarkLocationsDao.updateBookmarkLocation(examplePlaceBookmark) + assertFalse(exists) + } + + @Test + fun testGetAllBookmarksLocationsPlace() = runBlocking { + val bookmarkLocation = examplePlaceBookmark.toBookmarksLocations() + bookmarkLocationsDao.addBookmarkLocation(bookmarkLocation) + + val bookmarks = bookmarkLocationsDao.getAllBookmarksLocationsPlace() + assertEquals(1, bookmarks.size) + assertEquals(examplePlaceBookmark.name, bookmarks.first().name) } } 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 d3d3f98be..d824c0005 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 @@ -10,6 +10,7 @@ import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentTransaction import androidx.recyclerview.widget.RecyclerView import androidx.test.core.app.ApplicationProvider +import com.nhaarman.mockitokotlin2.verify import com.nhaarman.mockitokotlin2.whenever import fr.free.nrw.commons.OkHttpConnectionFactory import fr.free.nrw.commons.R @@ -171,7 +172,12 @@ class BookmarkLocationFragmentUnitTests { */ @Test @Throws(Exception::class) - fun testOnResume() { + fun testOnResume() = runBlocking { + whenever(controller.loadFavoritesLocations()).thenReturn(mockBookmarkList) + fragment.onResume() + + verify(fragment).initList() + verify(adapter).items = mockBookmarkList } }