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 df59cd09a..19584fd70 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 @@ -18,18 +18,29 @@ package org.kiwix.kiwixmobile.page.bookmarks +import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.action.ViewActions.click +import androidx.test.espresso.assertion.ViewAssertions +import androidx.test.espresso.matcher.ViewMatchers.withText import applyWithViewHierarchyPrinting import com.adevinta.android.barista.assertion.BaristaVisibilityAssertions.assertDisplayed +import com.adevinta.android.barista.interaction.BaristaSleepInteractions import org.kiwix.kiwixmobile.BaseRobot import org.kiwix.kiwixmobile.Findable.StringId.ContentDesc import org.kiwix.kiwixmobile.Findable.StringId.TextId +import org.kiwix.kiwixmobile.Findable.Text +import org.kiwix.kiwixmobile.Findable.ViewId import org.kiwix.kiwixmobile.R +import org.kiwix.kiwixmobile.testutils.TestUtils +import java.util.concurrent.TimeUnit fun bookmarks(func: BookmarksRobot.() -> Unit) = BookmarksRobot().applyWithViewHierarchyPrinting(func) class BookmarksRobot : BaseRobot() { + private var retryCountForBookmarkAddedButton = 5 + fun assertBookMarksDisplayed() { assertDisplayed(R.string.bookmarks_from_current_book) } @@ -41,4 +52,45 @@ class BookmarksRobot : BaseRobot() { fun assertDeleteBookmarksDialogDisplayed() { isVisible(TextId(R.string.delete_bookmarks)) } + + fun clickOnSaveBookmarkImage() { + pauseForBetterTestPerformance() + clickOn(ViewId(R.id.bottom_toolbar_bookmark)) + } + + fun longClickOnSaveBookmarkImage() { + // wait for disappearing the snack-bar after removing the bookmark + BaristaSleepInteractions.sleep(5L, TimeUnit.SECONDS) + longClickOn(ViewId(R.id.bottom_toolbar_bookmark)) + } + + fun clickOnOpenSavedBookmarkButton() { + try { + onView(withText("OPEN")).perform(click()) + } catch (runtimeException: RuntimeException) { + if (retryCountForBookmarkAddedButton > 0) { + retryCountForBookmarkAddedButton-- + clickOnOpenSavedBookmarkButton() + } else { + throw RuntimeException( + "Unable to save the bookmark, original exception is" + + " ${runtimeException.localizedMessage}" + ) + } + } + } + + fun assertBookmarkSaved() { + pauseForBetterTestPerformance() + isVisible(Text("Test Zim")) + } + + fun assertBookmarkRemoved() { + pauseForBetterTestPerformance() + onView(withText("Test Zim")).check(ViewAssertions.doesNotExist()) + } + + private fun pauseForBetterTestPerformance() { + BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS_FOR_SEARCH_TEST.toLong()) + } } 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 efd778dfb..27a411b5d 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,26 +18,91 @@ package org.kiwix.kiwixmobile.page.bookmarks -import io.mockk.mockk -import io.mockk.verify -import org.junit.jupiter.api.Test -import org.kiwix.kiwixmobile.core.dao.LibkiwixBookmarks -import org.kiwix.kiwixmobile.core.page.bookmark.adapter.LibkiwixBookmarkItem +import androidx.core.content.edit +import androidx.core.net.toUri +import androidx.lifecycle.Lifecycle +import androidx.preference.PreferenceManager +import androidx.test.core.app.ActivityScenario +import androidx.test.internal.runner.junit4.statement.UiThreadStatement +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.uiautomator.UiDevice +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.kiwix.kiwixmobile.BaseActivityTest +import org.kiwix.kiwixmobile.R import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil -import org.kiwix.libkiwix.Bookmark -import org.kiwix.libkiwix.Library -import org.kiwix.libkiwix.Manager +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 +import java.io.FileOutputStream +import java.io.OutputStream -internal class LibkiwixBookmarkTest { - private val library: Library = mockk(relaxed = true) - private val manager = Manager(library) - private val sharedPreferenceUtil: SharedPreferenceUtil = mockk(relaxed = true) - private val libkiwixBookmarks = LibkiwixBookmarks(library, manager, sharedPreferenceUtil) +class LibkiwixBookmarkTest : BaseActivityTest() { + + @Rule + @JvmField + var retryRule = RetryRule() + + private lateinit var kiwixMainActivity: KiwixMainActivity + + @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) + } + activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply { + moveToState(Lifecycle.State.RESUMED) + } + } @Test - internal fun saveBookmark() { - val bookmark: Bookmark = mockk(relaxed = true) - libkiwixBookmarks.saveBookmark(LibkiwixBookmarkItem(bookmark, null, null)) - verify { library.addBookmark(bookmark) } + fun testBookmarks() { + activityScenario.onActivity { + kiwixMainActivity = it + kiwixMainActivity.navigate(R.id.libraryFragment) + } + val loadFileStream = + SearchFragmentTest::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) + } + } + } + + UiThreadStatement.runOnUiThread { + kiwixMainActivity.navigate( + LocalLibraryFragmentDirections.actionNavigationLibraryToNavigationReader() + .apply { zimFileUri = zimFile.toUri().toString() } + ) + } + bookmarks { + clickOnSaveBookmarkImage() + clickOnOpenSavedBookmarkButton() + assertBookmarkSaved() + pressBack() + clickOnSaveBookmarkImage() + longClickOnSaveBookmarkImage() + assertBookmarkRemoved() + } } } diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/libkiwix_wrapper/BookmarkWrapper.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/libkiwix_wrapper/BookmarkWrapper.kt deleted file mode 100644 index ecdd9fcdd..000000000 --- a/core/src/test/java/org/kiwix/kiwixmobile/core/libkiwix_wrapper/BookmarkWrapper.kt +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Kiwix Android - * Copyright (c) 2023 Kiwix - * 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 - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -package org.kiwix.kiwixmobile.core.libkiwix_wrapper - -import org.kiwix.libkiwix.Bookmark - -class BookmarkWrapper : Bookmark() { - override fun getBookId(): String = super.getBookId() - override fun getDate(): String = super.getDate() - override fun getBookTitle(): String = super.getBookTitle() - override fun getLanguage(): String = super.getLanguage() - override fun getTitle(): String = super.getTitle() - override fun getUrl(): String = super.getUrl() - - override fun setBookId(bookId: String?) { - super.setBookId(bookId) - } - - override fun setBookTitle(bookTitle: String?) { - super.setBookTitle(bookTitle) - } - - override fun setDate(Date: String?) { - super.setDate(Date) - } - - override fun setLanguage(language: String?) { - super.setLanguage(language) - } - - override fun setUrl(url: String?) { - super.setUrl(url) - } - - override fun setTitle(title: String?) { - super.setTitle(title) - } -} diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/libkiwix_wrapper/LibraryWrapper.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/libkiwix_wrapper/LibraryWrapper.kt deleted file mode 100644 index 789d0de83..000000000 --- a/core/src/test/java/org/kiwix/kiwixmobile/core/libkiwix_wrapper/LibraryWrapper.kt +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Kiwix Android - * Copyright (c) 2023 Kiwix - * 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 - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -package org.kiwix.kiwixmobile.core.libkiwix_wrapper - -import org.kiwix.libkiwix.Bookmark -import org.kiwix.libkiwix.Library - -class LibraryWrapper : Library() { - - override fun addBookmark(bookmark: Bookmark?) = - super.addBookmark(bookmark) -} diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/libkiwix_wrapper/ManagerWrapper.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/libkiwix_wrapper/ManagerWrapper.kt deleted file mode 100644 index 55933d785..000000000 --- a/core/src/test/java/org/kiwix/kiwixmobile/core/libkiwix_wrapper/ManagerWrapper.kt +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Kiwix Android - * Copyright (c) 2023 Kiwix - * 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 - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -package org.kiwix.kiwixmobile.core.libkiwix_wrapper - -import org.kiwix.libkiwix.Manager - -class ManagerWrapper(library: LibraryWrapper) : Manager(library)