Fixed the failing LibkiwixBookmarkTest.

* Enhanced the process of adding books to the library to prevent unnecessary data loading from libkiwix.
* Released the memory occupied by bookmarks and archives to resolve potential issues when running migrations on lower-end devices.
This commit is contained in:
MohitMaliFtechiz 2024-01-11 17:26:04 +05:30 committed by MohitMaliFtechiz
parent 99cf75bf59
commit 50f92beeb1
4 changed files with 32 additions and 13 deletions

View File

@ -38,6 +38,7 @@ import org.junit.Before
import org.junit.BeforeClass
import org.junit.Rule
import org.junit.Test
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertTrue
@ -105,6 +106,9 @@ class ObjectBoxToLibkiwixMigratorTest : BaseActivityTest() {
}
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
onActivity {
it.navigate(R.id.libraryFragment)
}
}
CoreApp.coreComponent.inject(objectBoxToLibkiwixMigrator)
setUpObjectBoxAndData()
@ -143,7 +147,7 @@ class ObjectBoxToLibkiwixMigratorTest : BaseActivityTest() {
}
// clear the data before running the test case
clearBookmarks(box, objectBoxToLibkiwixMigrator.libkiwixBookmarks)
clearBookmarks()
}
@Test
@ -301,10 +305,11 @@ class ObjectBoxToLibkiwixMigratorTest : BaseActivityTest() {
)
}
private fun clearBookmarks(box: Box<BookmarkEntity>, libkiwixBookmark: LibkiwixBookmarks) {
private fun clearBookmarks() {
// delete bookmarks for testing other edge cases
libkiwixBookmark.deleteBookmarks(
libkiwixBookmark.bookmarks().blockingFirst() as List<LibkiwixBookmarkItem>
objectBoxToLibkiwixMigrator.libkiwixBookmarks.deleteBookmarks(
objectBoxToLibkiwixMigrator.libkiwixBookmarks.bookmarks()
.blockingFirst() as List<LibkiwixBookmarkItem>
)
box.removeAll()
}
@ -318,6 +323,12 @@ class ObjectBoxToLibkiwixMigratorTest : BaseActivityTest() {
}
}
@AfterAll
fun deleteBookmarks() {
// Clear the bookmarks list from device to not affect the other test cases.
clearBookmarks()
}
companion object {
@BeforeClass

View File

@ -34,7 +34,6 @@ import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.main.KiwixMainActivity
import org.kiwix.kiwixmobile.nav.destination.library.LocalLibraryFragmentDirections
import org.kiwix.kiwixmobile.search.SearchFragmentTest
import org.kiwix.kiwixmobile.testutils.RetryRule
import org.kiwix.kiwixmobile.testutils.TestUtils
import java.io.File
@ -74,7 +73,7 @@ class LibkiwixBookmarkTest : BaseActivityTest() {
kiwixMainActivity.navigate(R.id.libraryFragment)
}
val loadFileStream =
SearchFragmentTest::class.java.classLoader.getResourceAsStream("testzim.zim")
LibkiwixBookmarkTest::class.java.classLoader.getResourceAsStream("testzim.zim")
val zimFile = File(context.cacheDir, "testzim.zim")
if (zimFile.exists()) zimFile.delete()
zimFile.createNewFile()

View File

@ -140,17 +140,15 @@ class LibkiwixBookmarks @Inject constructor(
writeBookMarksAndSaveLibraryToFile()
updateFlowableBookmarkList()
}
// dispose the bookmark
bookmark.dispose()
}
}
}
private fun addBookToLibraryIfNotExist(libKiwixBook: Book?) {
libKiwixBook?.let { book ->
if (libraryBooksList.isEmpty()) {
// store booksIds in a list to avoid multiple data call on libkiwix
libraryBooksList = library.booksIds.toList()
}
if (!libraryBooksList.any { it == book.id }) {
if (!isBookAlreadyExistInLibrary(book.id)) {
library.addBook(libKiwixBook).also {
// now library has changed so update our library list.
libraryBooksList = library.booksIds.toList()
@ -168,6 +166,14 @@ class LibkiwixBookmarks @Inject constructor(
}
}
private fun isBookAlreadyExistInLibrary(bookId: String): Boolean {
if (libraryBooksList.isEmpty()) {
// store booksIds in a list to avoid multiple data call on libkiwix
libraryBooksList = library.booksIds.toList()
}
return libraryBooksList.any { it == bookId }
}
fun deleteBookmarks(bookmarks: List<LibkiwixBookmarkItem>) {
bookmarks.map { library.removeBookmark(it.zimId, it.bookmarkUrl) }
.also {

View File

@ -55,13 +55,15 @@ class ObjectBoxToLibkiwixMigrator {
try {
// for saving book to library, otherwise it does not save the
// favicon and zimFilePath in library.
val archive = Archive(bookmarkEntity.zimFilePath)
val libkiwixBook = Book().apply {
update(Archive(bookmarkEntity.zimFilePath))
update(archive)
}
libkiwixBookmarks.saveBookmark(
LibkiwixBookmarkItem(bookmarkEntity, libkiwixBook),
shouldWriteBookmarkToFile = index == bookMarksList.size - 1
)
archive.dispose()
// TODO should we remove data from objectBox?
// removing the single entity from the object box after migration.
// box.query {
@ -76,7 +78,8 @@ class ObjectBoxToLibkiwixMigrator {
"MIGRATING_BOOKMARKS",
"there is an error while migrating the bookmark for\n" +
" ZIM file = ${bookmarkEntity.zimFilePath} \n" +
"Bookmark Title = ${bookmarkEntity.bookmarkTitle}"
"Bookmark Title = ${bookmarkEntity.bookmarkTitle} \n" +
"Original exception is = $ignore"
)
}
}