mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Refactor BookmarkLocationsFragment and add tests for BookmarkLocationsDao
* Moved `initList` to `BookmarkLocationsFragment` for better lifecycle handling. * Added test case for `BookmarkLocationsFragment`'s onResume. * Added test cases for `BookmarkLocationsDao` operations like adding, retrieving, finding, deleting and updating bookmarks.
This commit is contained in:
parent
5940f92f01
commit
edd52debfb
3 changed files with 69 additions and 14 deletions
|
|
@ -129,7 +129,7 @@ class BookmarkLocationsFragment : DaggerFragment() {
|
||||||
initList()
|
initList()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initList() {
|
fun initList() {
|
||||||
var places: List<Place>
|
var places: List<Place>
|
||||||
viewLifecycleOwner.lifecycleScope.launch {
|
viewLifecycleOwner.lifecycleScope.launch {
|
||||||
places = controller.loadFavoritesLocations()
|
places = controller.loadFavoritesLocations()
|
||||||
|
|
|
||||||
|
|
@ -42,8 +42,7 @@ import org.robolectric.annotation.Config
|
||||||
@Config(sdk = [21], application = TestCommonsApplication::class)
|
@Config(sdk = [21], application = TestCommonsApplication::class)
|
||||||
class BookMarkLocationDaoTest {
|
class BookMarkLocationDaoTest {
|
||||||
|
|
||||||
@Mock
|
private lateinit var bookmarkLocationsDao: BookmarkLocationsDao
|
||||||
var bookmarkLocationsDao: BookmarkLocationsDao? = null
|
|
||||||
|
|
||||||
private lateinit var database: AppDatabase
|
private lateinit var database: AppDatabase
|
||||||
|
|
||||||
|
|
@ -92,17 +91,67 @@ class BookMarkLocationDaoTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun insertAndRetrieveBookmark() = runBlocking {
|
fun testForAddAndGetAllBookmarkLocations() = runBlocking {
|
||||||
// Insert a bookmark
|
bookmarkLocationsDao.addBookmarkLocation(examplePlaceBookmark.toBookmarksLocations())
|
||||||
bookmarkLocationsDao?.addBookmarkLocation(examplePlaceBookmark.toBookmarksLocations())
|
|
||||||
|
|
||||||
// Retrieve all bookmarks
|
val bookmarks = bookmarkLocationsDao.getAllBookmarksLocations()
|
||||||
val bookmarks = bookmarkLocationsDao?.getAllBookmarksLocations()
|
|
||||||
|
|
||||||
// Assert the bookmark exists
|
assertEquals(1, bookmarks.size)
|
||||||
assertEquals(1, bookmarks?.size)
|
val retrievedBookmark = bookmarks.first()
|
||||||
val retrievedBookmark = bookmarks?.first()
|
assertEquals(examplePlaceBookmark.name, retrievedBookmark.locationName)
|
||||||
assertEquals(examplePlaceBookmark.name, retrievedBookmark?.locationName)
|
assertEquals(examplePlaceBookmark.language, retrievedBookmark.locationLanguage)
|
||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import androidx.fragment.app.FragmentManager
|
||||||
import androidx.fragment.app.FragmentTransaction
|
import androidx.fragment.app.FragmentTransaction
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import androidx.test.core.app.ApplicationProvider
|
import androidx.test.core.app.ApplicationProvider
|
||||||
|
import com.nhaarman.mockitokotlin2.verify
|
||||||
import com.nhaarman.mockitokotlin2.whenever
|
import com.nhaarman.mockitokotlin2.whenever
|
||||||
import fr.free.nrw.commons.OkHttpConnectionFactory
|
import fr.free.nrw.commons.OkHttpConnectionFactory
|
||||||
import fr.free.nrw.commons.R
|
import fr.free.nrw.commons.R
|
||||||
|
|
@ -171,7 +172,12 @@ class BookmarkLocationFragmentUnitTests {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
fun testOnResume() {
|
fun testOnResume() = runBlocking {
|
||||||
|
whenever(controller.loadFavoritesLocations()).thenReturn(mockBookmarkList)
|
||||||
|
|
||||||
fragment.onResume()
|
fragment.onResume()
|
||||||
|
|
||||||
|
verify(fragment).initList()
|
||||||
|
verify(adapter).items = mockBookmarkList
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue