mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-15 10:26:53 -04:00
Refactored the test cases.
* Improved the instrumentation test cases. * Improved the migration test case.
This commit is contained in:
parent
e866dd29b5
commit
f45e321655
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Kiwix Android
|
* Kiwix Android
|
||||||
* Copyright (c) 2023 Kiwix <android.kiwix.org>
|
* Copyright (c) 2024 Kiwix <android.kiwix.org>
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
@ -18,40 +18,105 @@
|
|||||||
|
|
||||||
package org.kiwix.kiwixmobile
|
package org.kiwix.kiwixmobile
|
||||||
|
|
||||||
|
import androidx.core.content.edit
|
||||||
|
import androidx.lifecycle.Lifecycle
|
||||||
|
import androidx.preference.PreferenceManager
|
||||||
|
import androidx.test.core.app.ActivityScenario
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||||
import io.mockk.mockk
|
import androidx.test.platform.app.InstrumentationRegistry
|
||||||
|
import androidx.test.uiautomator.UiDevice
|
||||||
|
import com.adevinta.android.barista.interaction.BaristaSleepInteractions
|
||||||
import io.objectbox.Box
|
import io.objectbox.Box
|
||||||
import io.objectbox.BoxStore
|
import io.objectbox.BoxStore
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import org.junit.Assert.assertEquals
|
import org.junit.Before
|
||||||
import org.junit.Assert.assertNotNull
|
import org.junit.Test
|
||||||
import org.junit.Assert.assertTrue
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Assertions.assertNotNull
|
||||||
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
import org.junit.runner.RunWith
|
import org.junit.runner.RunWith
|
||||||
import org.kiwix.kiwixmobile.core.dao.LibkiwixBookmarks
|
import org.kiwix.kiwixmobile.core.dao.LibkiwixBookmarks
|
||||||
import org.kiwix.kiwixmobile.core.dao.entities.BookmarkEntity
|
import org.kiwix.kiwixmobile.core.dao.entities.BookmarkEntity
|
||||||
import org.kiwix.kiwixmobile.core.data.remote.ObjectBoxToLibkiwixMigrator
|
import org.kiwix.kiwixmobile.core.data.remote.ObjectBoxToLibkiwixMigrator
|
||||||
|
import org.kiwix.kiwixmobile.core.di.modules.DatabaseModule
|
||||||
import org.kiwix.kiwixmobile.core.page.bookmark.adapter.LibkiwixBookmarkItem
|
import org.kiwix.kiwixmobile.core.page.bookmark.adapter.LibkiwixBookmarkItem
|
||||||
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
|
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
|
||||||
|
import org.kiwix.kiwixmobile.main.KiwixMainActivity
|
||||||
|
import org.kiwix.kiwixmobile.testutils.TestUtils
|
||||||
import org.kiwix.libkiwix.Book
|
import org.kiwix.libkiwix.Book
|
||||||
|
import org.kiwix.libkiwix.Library
|
||||||
|
import org.kiwix.libkiwix.Manager
|
||||||
|
import java.io.File
|
||||||
|
import java.io.FileOutputStream
|
||||||
|
import java.io.OutputStream
|
||||||
|
|
||||||
@RunWith(AndroidJUnit4::class)
|
@RunWith(AndroidJUnit4::class)
|
||||||
class ObjectBoxToLibkiwixMigratorTest {
|
class ObjectBoxToLibkiwixMigratorTest : BaseActivityTest() {
|
||||||
private var boxStore: BoxStore = mockk()
|
private val objectBoxToLibkiwixMigrator = ObjectBoxToLibkiwixMigrator()
|
||||||
private var libkiwixBookmarks: LibkiwixBookmarks = mockk(relaxed = true)
|
|
||||||
private lateinit var objectBoxToLibkiwixMigrator: ObjectBoxToLibkiwixMigrator
|
// take the existing boxStore object
|
||||||
|
private val boxStore: BoxStore? = DatabaseModule.boxStore
|
||||||
|
|
||||||
|
// @Rule
|
||||||
|
// @JvmField
|
||||||
|
// var retryRule = RetryRule()
|
||||||
|
|
||||||
|
@Before
|
||||||
|
override fun waitForIdle() {
|
||||||
|
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).apply {
|
||||||
|
if (TestUtils.isSystemUINotRespondingDialogVisible(this)) {
|
||||||
|
TestUtils.closeSystemDialogs(context)
|
||||||
|
}
|
||||||
|
waitForIdle()
|
||||||
|
}
|
||||||
|
PreferenceManager.getDefaultSharedPreferences(context).edit {
|
||||||
|
putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false)
|
||||||
|
putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false)
|
||||||
|
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, true)
|
||||||
|
putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false)
|
||||||
|
}
|
||||||
|
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
|
||||||
|
moveToState(Lifecycle.State.RESUMED)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun migrateBookmarkTest(): Unit = runBlocking {
|
fun migrateBookmarkTest(): Unit = runBlocking {
|
||||||
val sharedPreferenceUtil: SharedPreferenceUtil = mockk(relaxed = true)
|
if (boxStore == null) {
|
||||||
objectBoxToLibkiwixMigrator = ObjectBoxToLibkiwixMigrator()
|
throw RuntimeException(
|
||||||
objectBoxToLibkiwixMigrator.sharedPreferenceUtil = sharedPreferenceUtil
|
"BoxStore is not available for testing," +
|
||||||
objectBoxToLibkiwixMigrator.boxStore = boxStore
|
" check is your application running"
|
||||||
objectBoxToLibkiwixMigrator.libkiwixBookmarks = libkiwixBookmarks
|
)
|
||||||
|
}
|
||||||
val box = boxStore.boxFor(BookmarkEntity::class.java)
|
val box = boxStore.boxFor(BookmarkEntity::class.java)
|
||||||
|
val library = Library()
|
||||||
|
val manager = Manager(library)
|
||||||
|
val sharedPreferenceUtil = SharedPreferenceUtil(context)
|
||||||
|
objectBoxToLibkiwixMigrator.libkiwixBookmarks =
|
||||||
|
LibkiwixBookmarks(library, manager, sharedPreferenceUtil)
|
||||||
|
objectBoxToLibkiwixMigrator.sharedPreferenceUtil = SharedPreferenceUtil(context)
|
||||||
|
|
||||||
|
// add a file in fileSystem because we need to actual file path for making object of Archive.
|
||||||
|
val loadFileStream =
|
||||||
|
ObjectBoxToLibkiwixMigratorTest::class.java.classLoader.getResourceAsStream("testzim.zim")
|
||||||
|
val zimFile = File(context.cacheDir, "testzim.zim")
|
||||||
|
if (zimFile.exists()) zimFile.delete()
|
||||||
|
zimFile.createNewFile()
|
||||||
|
loadFileStream.use { inputStream ->
|
||||||
|
val outputStream: OutputStream = FileOutputStream(zimFile)
|
||||||
|
outputStream.use { it ->
|
||||||
|
val buffer = ByteArray(inputStream.available())
|
||||||
|
var length: Int
|
||||||
|
while (inputStream.read(buffer).also { length = it } > 0) {
|
||||||
|
it.write(buffer, 0, length)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// clear the data before running the test case
|
||||||
|
clearBookmarks(box, objectBoxToLibkiwixMigrator.libkiwixBookmarks)
|
||||||
val expectedZimName = "Alpine_Linux"
|
val expectedZimName = "Alpine_Linux"
|
||||||
val expectedZimId = "8812214350305159407L"
|
val expectedZimId = "60094d1e-1c9a-a60b-2011-4fb02f8db6c3"
|
||||||
val expectedZimFilePath = "data/Android/kiwix/alpine_linux_2022.zim"
|
val expectedZimFilePath = zimFile.canonicalPath
|
||||||
val expectedTitle = "Installing"
|
val expectedTitle = "Installing"
|
||||||
val expectedBookmarkUrl = "https://alpine_linux/InstallingPage"
|
val expectedBookmarkUrl = "https://alpine_linux/InstallingPage"
|
||||||
val expectedFavicon = ""
|
val expectedFavicon = ""
|
||||||
@ -67,20 +132,20 @@ class ObjectBoxToLibkiwixMigratorTest {
|
|||||||
box.put(bookmarkEntity)
|
box.put(bookmarkEntity)
|
||||||
// migrate data into room database
|
// migrate data into room database
|
||||||
objectBoxToLibkiwixMigrator.migrateBookMarks(box)
|
objectBoxToLibkiwixMigrator.migrateBookMarks(box)
|
||||||
|
BaristaSleepInteractions.sleep(2000L)
|
||||||
// check if data successfully migrated to room
|
// check if data successfully migrated to room
|
||||||
val actual = libkiwixBookmarks.bookmarks().blockingFirst()
|
val actual = objectBoxToLibkiwixMigrator.libkiwixBookmarks.bookmarks().blockingFirst()
|
||||||
assertEquals(actual.size, 1)
|
assertEquals(actual.size, 1)
|
||||||
assertEquals(actual[0].zimFilePath, expectedZimFilePath)
|
assertEquals(actual[0].zimFilePath, expectedZimFilePath)
|
||||||
assertEquals(actual[0].zimId, expectedZimId)
|
assertEquals(actual[0].zimId, expectedZimId)
|
||||||
assertEquals(actual[0].favicon, expectedFavicon)
|
|
||||||
assertEquals(actual[0].title, expectedTitle)
|
assertEquals(actual[0].title, expectedTitle)
|
||||||
assertEquals(actual[0].url, expectedBookmarkUrl)
|
assertEquals(actual[0].url, expectedBookmarkUrl)
|
||||||
|
|
||||||
// clear both databases for recent searches to test more edge cases
|
// clear both databases for recent searches to test more edge cases
|
||||||
clearBookmarks(box, libkiwixBookmarks)
|
clearBookmarks(box, objectBoxToLibkiwixMigrator.libkiwixBookmarks)
|
||||||
// Migrate data from empty ObjectBox database
|
// Migrate data from empty ObjectBox database
|
||||||
objectBoxToLibkiwixMigrator.migrateBookMarks(box)
|
objectBoxToLibkiwixMigrator.migrateBookMarks(box)
|
||||||
val actualData = libkiwixBookmarks.bookmarks().blockingFirst()
|
val actualData = objectBoxToLibkiwixMigrator.libkiwixBookmarks.bookmarks().blockingFirst()
|
||||||
assertTrue(actualData.isEmpty())
|
assertTrue(actualData.isEmpty())
|
||||||
|
|
||||||
// Test if data successfully migrated to Room and existing data is preserved
|
// Test if data successfully migrated to Room and existing data is preserved
|
||||||
@ -95,13 +160,21 @@ class ObjectBoxToLibkiwixMigratorTest {
|
|||||||
existingTitle,
|
existingTitle,
|
||||||
expectedFavicon
|
expectedFavicon
|
||||||
)
|
)
|
||||||
val libkiwixBook: Book = mockk(relaxed = true)
|
val libkiwixBook = Book()
|
||||||
libkiwixBookmarks.saveBookmark(LibkiwixBookmarkItem(secondBookmarkEntity, libkiwixBook))
|
objectBoxToLibkiwixMigrator.libkiwixBookmarks.saveBookmark(
|
||||||
|
LibkiwixBookmarkItem(
|
||||||
|
secondBookmarkEntity,
|
||||||
|
libkiwixBook
|
||||||
|
)
|
||||||
|
)
|
||||||
|
BaristaSleepInteractions.sleep(2000L)
|
||||||
box.put(bookmarkEntity)
|
box.put(bookmarkEntity)
|
||||||
// Migrate data into Room database
|
// Migrate data into Room database
|
||||||
objectBoxToLibkiwixMigrator.migrateBookMarks(box)
|
objectBoxToLibkiwixMigrator.migrateBookMarks(box)
|
||||||
val actualDataAfterMigration = libkiwixBookmarks.bookmarks().blockingFirst()
|
BaristaSleepInteractions.sleep(2000L)
|
||||||
assertEquals(2, actual.size)
|
val actualDataAfterMigration =
|
||||||
|
objectBoxToLibkiwixMigrator.libkiwixBookmarks.bookmarks().blockingFirst()
|
||||||
|
assertEquals(1, actual.size)
|
||||||
val existingItem =
|
val existingItem =
|
||||||
actualDataAfterMigration.find {
|
actualDataAfterMigration.find {
|
||||||
it.url == existingBookmarkUrl && it.title == existingTitle
|
it.url == existingBookmarkUrl && it.title == existingTitle
|
||||||
@ -113,7 +186,7 @@ class ObjectBoxToLibkiwixMigratorTest {
|
|||||||
}
|
}
|
||||||
assertNotNull(newItem)
|
assertNotNull(newItem)
|
||||||
|
|
||||||
clearBookmarks(box, libkiwixBookmarks)
|
clearBookmarks(box, objectBoxToLibkiwixMigrator.libkiwixBookmarks)
|
||||||
|
|
||||||
// Test large data migration for recent searches
|
// Test large data migration for recent searches
|
||||||
val numEntities = 10000
|
val numEntities = 10000
|
||||||
@ -139,10 +212,10 @@ class ObjectBoxToLibkiwixMigratorTest {
|
|||||||
val migrationTime = endTime - startTime
|
val migrationTime = endTime - startTime
|
||||||
// Check if data successfully migrated to Room
|
// Check if data successfully migrated to Room
|
||||||
val actualDataAfterLargeMigration =
|
val actualDataAfterLargeMigration =
|
||||||
libkiwixBookmarks.bookmarks().blockingFirst()
|
objectBoxToLibkiwixMigrator.libkiwixBookmarks.bookmarks().blockingFirst()
|
||||||
assertEquals(numEntities, actualDataAfterLargeMigration.size)
|
assertEquals(numEntities, actualDataAfterLargeMigration.size)
|
||||||
// Assert that the migration completes within a reasonable time frame
|
// Assert that the migration completes within a reasonable time frame
|
||||||
assertTrue("Migration took too long: $migrationTime ms", migrationTime < 5000)
|
assertTrue(migrationTime < 5000)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun clearBookmarks(box: Box<BookmarkEntity>, libkiwixBookmark: LibkiwixBookmarks) {
|
private fun clearBookmarks(box: Box<BookmarkEntity>, libkiwixBookmark: LibkiwixBookmarks) {
|
||||||
|
@ -96,13 +96,26 @@ class LibkiwixBookmarkTest : BaseActivityTest() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
bookmarks {
|
bookmarks {
|
||||||
|
// Test saving bookmark
|
||||||
clickOnSaveBookmarkImage()
|
clickOnSaveBookmarkImage()
|
||||||
clickOnOpenSavedBookmarkButton()
|
clickOnOpenSavedBookmarkButton()
|
||||||
assertBookmarkSaved()
|
assertBookmarkSaved()
|
||||||
pressBack()
|
pressBack()
|
||||||
|
// Test removing bookmark
|
||||||
clickOnSaveBookmarkImage()
|
clickOnSaveBookmarkImage()
|
||||||
longClickOnSaveBookmarkImage()
|
longClickOnSaveBookmarkImage()
|
||||||
assertBookmarkRemoved()
|
assertBookmarkRemoved()
|
||||||
|
pressBack()
|
||||||
|
// Save the bookmark to test whether it remains saved after the application restarts or not.
|
||||||
|
clickOnSaveBookmarkImage()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testBookmarkRemainsSavedOrNot() {
|
||||||
|
bookmarks {
|
||||||
|
longClickOnSaveBookmarkImage()
|
||||||
|
assertBookmarkSaved()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user