From 606bea4405c72f9f6dc289b307c1a5f22e6806f9 Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Wed, 6 Mar 2024 18:33:27 +0530 Subject: [PATCH] Improved our `instrumentation.sh` bash script, if our test fails then after restarting the emulator if our application is installed we clear its cache data so that test cases will perform properly. We have made this change to fix a scenario that comes locally after testing so many times where downloading is stuck due to lag of emulator. * * Improved the LibkiwixBookmarkTest. In this, before performing the test case we are removing the existing bookmarks if any so that the saved bookmark will not go outside the screen(We faced an occurrence of this type locally). * Improved the ZIM file path in many scenarios so that the created ZIM file will show in the LocalLibraryScreen and we can delete it so that we can free up the memory. --- .../ObjectBoxToLibkiwixMigratorTest.kt | 7 ++++++- .../kiwix/kiwixmobile/mimetype/MimeTypeTest.kt | 6 +++++- .../kiwixmobile/page/bookmarks/BookmarksRobot.kt | 9 +++++++++ .../page/bookmarks/LibkiwixBookmarkTest.kt | 13 ++++++++++++- .../page/history/NavigationHistoryTest.kt | 6 +++++- .../kiwixmobile/reader/KiwixReaderFragmentTest.kt | 6 +++++- .../kiwixmobile/search/SearchFragmentTest.kt | 6 +++++- .../kiwixmobile/webserver/ZimHostFragmentTest.kt | 3 +++ contrib/instrumentation.sh | 15 +++++++++++++-- 9 files changed, 63 insertions(+), 8 deletions(-) diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/ObjectBoxToLibkiwixMigratorTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/ObjectBoxToLibkiwixMigratorTest.kt index b3e06717a..fb44b6f0a 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/ObjectBoxToLibkiwixMigratorTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/ObjectBoxToLibkiwixMigratorTest.kt @@ -18,6 +18,7 @@ package org.kiwix.kiwixmobile +import androidx.core.content.ContextCompat import androidx.core.content.edit import androidx.lifecycle.Lifecycle import androidx.preference.PreferenceManager @@ -122,7 +123,10 @@ class ObjectBoxToLibkiwixMigratorTest : BaseActivityTest() { // 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") - zimFile = File(context.cacheDir, "testzim.zim") + zimFile = File( + ContextCompat.getExternalFilesDirs(context, null)[0], + "testzim.zim" + ) if (zimFile.exists()) zimFile.delete() zimFile.createNewFile() loadFileStream.use { inputStream -> @@ -306,6 +310,7 @@ class ObjectBoxToLibkiwixMigratorTest : BaseActivityTest() { .blockingFirst() as List ) box.removeAll() + zimFile.delete() // delete the temp ZIM file to free up the memory } @After diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/mimetype/MimeTypeTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/mimetype/MimeTypeTest.kt index 942447404..e2bace50c 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/mimetype/MimeTypeTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/mimetype/MimeTypeTest.kt @@ -18,6 +18,7 @@ package org.kiwix.kiwixmobile.mimetype +import androidx.core.content.ContextCompat import androidx.core.content.edit import androidx.lifecycle.Lifecycle import androidx.preference.PreferenceManager @@ -63,7 +64,10 @@ class MimeTypeTest : BaseActivityTest() { @Test fun testMimeType() { val loadFileStream = MimeTypeTest::class.java.classLoader.getResourceAsStream("testzim.zim") - val zimFile = File(context.cacheDir, "testzim.zim") + val zimFile = File( + ContextCompat.getExternalFilesDirs(context, null)[0], + "testzim.zim" + ) if (zimFile.exists()) zimFile.delete() zimFile.createNewFile() loadFileStream.use { inputStream -> diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/page/bookmarks/BookmarksRobot.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/page/bookmarks/BookmarksRobot.kt index 19584fd70..550ed53f6 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/page/bookmarks/BookmarksRobot.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/page/bookmarks/BookmarksRobot.kt @@ -53,6 +53,15 @@ class BookmarksRobot : BaseRobot() { isVisible(TextId(R.string.delete_bookmarks)) } + fun clickOnDeleteButton() { + pauseForBetterTestPerformance() + onView(withText("DELETE")).perform(click()) + } + + fun assertNoBookMarkTextDisplayed() { + isVisible(TextId(R.string.no_bookmarks)) + } + fun clickOnSaveBookmarkImage() { pauseForBetterTestPerformance() clickOn(ViewId(R.id.bottom_toolbar_bookmark)) diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/page/bookmarks/LibkiwixBookmarkTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/page/bookmarks/LibkiwixBookmarkTest.kt index d86b3aaf0..b4923528c 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/page/bookmarks/LibkiwixBookmarkTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/page/bookmarks/LibkiwixBookmarkTest.kt @@ -18,6 +18,7 @@ package org.kiwix.kiwixmobile.page.bookmarks +import androidx.core.content.ContextCompat import androidx.core.content.edit import androidx.core.net.toUri import androidx.lifecycle.Lifecycle @@ -75,7 +76,10 @@ class LibkiwixBookmarkTest : BaseActivityTest() { } val loadFileStream = LibkiwixBookmarkTest::class.java.classLoader.getResourceAsStream("testzim.zim") - val zimFile = File(context.cacheDir, "testzim.zim") + val zimFile = File( + ContextCompat.getExternalFilesDirs(context, null)[0], + "testzim.zim" + ) if (zimFile.exists()) zimFile.delete() zimFile.createNewFile() loadFileStream.use { inputStream -> @@ -96,6 +100,13 @@ class LibkiwixBookmarkTest : BaseActivityTest() { ) } bookmarks { + // delete any bookmark if already saved to properly perform this test case. + longClickOnSaveBookmarkImage() + clickOnTrashIcon() + assertDeleteBookmarksDialogDisplayed() + clickOnDeleteButton() + assertNoBookMarkTextDisplayed() + pressBack() // Test saving bookmark clickOnSaveBookmarkImage() clickOnOpenSavedBookmarkButton() diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/page/history/NavigationHistoryTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/page/history/NavigationHistoryTest.kt index ab2d1c89d..db6ee50e5 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/page/history/NavigationHistoryTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/page/history/NavigationHistoryTest.kt @@ -18,6 +18,7 @@ package org.kiwix.kiwixmobile.page.history +import androidx.core.content.ContextCompat import androidx.core.content.edit import androidx.core.net.toUri import androidx.lifecycle.Lifecycle @@ -78,7 +79,10 @@ class NavigationHistoryTest : BaseActivityTest() { } val loadFileStream = NavigationHistoryTest::class.java.classLoader.getResourceAsStream("testzim.zim") - val zimFile = File(context.cacheDir, "testzim.zim") + val zimFile = File( + ContextCompat.getExternalFilesDirs(context, null)[0], + "testzim.zim" + ) if (zimFile.exists()) zimFile.delete() zimFile.createNewFile() loadFileStream.use { inputStream -> diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/reader/KiwixReaderFragmentTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/reader/KiwixReaderFragmentTest.kt index 3590d25b0..3dcdbf3b6 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/reader/KiwixReaderFragmentTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/reader/KiwixReaderFragmentTest.kt @@ -18,6 +18,7 @@ package org.kiwix.kiwixmobile.reader +import androidx.core.content.ContextCompat import androidx.core.content.edit import androidx.core.net.toUri import androidx.lifecycle.Lifecycle @@ -77,7 +78,10 @@ class KiwixReaderFragmentTest : BaseActivityTest() { } val loadFileStream = KiwixReaderFragmentTest::class.java.classLoader.getResourceAsStream("testzim.zim") - val zimFile = File(context.cacheDir, "testzim.zim") + val zimFile = File( + ContextCompat.getExternalFilesDirs(context, null)[0], + "testzim.zim" + ) if (zimFile.exists()) zimFile.delete() zimFile.createNewFile() loadFileStream.use { inputStream -> diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchFragmentTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchFragmentTest.kt index eeda07645..0f4637862 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchFragmentTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchFragmentTest.kt @@ -17,6 +17,7 @@ */ package org.kiwix.kiwixmobile.search +import androidx.core.content.ContextCompat import androidx.core.content.edit import androidx.core.net.toUri import androidx.lifecycle.Lifecycle @@ -216,7 +217,10 @@ class SearchFragmentTest : BaseActivityTest() { private fun getTestZimFile(): File { val loadFileStream = SearchFragmentTest::class.java.classLoader.getResourceAsStream("testzim.zim") - val zimFile = File(context.cacheDir, "testzim.zim") + val zimFile = File( + ContextCompat.getExternalFilesDirs(context, null)[0], + "testzim.zim" + ) if (zimFile.exists()) zimFile.delete() zimFile.createNewFile() loadFileStream.use { inputStream -> diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/webserver/ZimHostFragmentTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/webserver/ZimHostFragmentTest.kt index 5019e7953..2545ec3d6 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/webserver/ZimHostFragmentTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/webserver/ZimHostFragmentTest.kt @@ -152,6 +152,9 @@ class ZimHostFragmentTest { // Check that only one ZIM file is hosted on the server after unselecting assertItemHostedOnServer(1) + + // finally close the server at the end of test case + stopServer() } LeakAssertions.assertNoLeaks() } diff --git a/contrib/instrumentation.sh b/contrib/instrumentation.sh index 96e6e443b..318fc0262 100644 --- a/contrib/instrumentation.sh +++ b/contrib/instrumentation.sh @@ -6,8 +6,7 @@ adb logcat -c # shellcheck disable=SC2035 adb logcat *:E -v color & retry=0 -while [ $retry -le 3 ] -do +while [ $retry -le 3 ]; do if ./gradlew jacocoInstrumentationTestReport; then echo "jacocoInstrumentationTestReport succeeded" >&2 break @@ -19,6 +18,18 @@ do adb logcat -c # shellcheck disable=SC2035 adb logcat *:E -v color & + + PACKAGE_NAME="org.kiwix.kiwixmobile" + + # Function to check if the application is installed + is_app_installed() { + adb shell pm list packages | grep -q "${PACKAGE_NAME}" + } + + if is_app_installed; then + # Clear application data to properly run the test cases. + adb shell pm clear "${PACKAGE_NAME}" + fi ./gradlew clean retry=$(( retry + 1 )) if [ $retry -eq 3 ]; then