From 8617cc00ed297506806aabca81072bd15ff8de3d Mon Sep 17 00:00:00 2001 From: MohitMali Date: Thu, 22 Sep 2022 15:49:21 +0530 Subject: [PATCH] Add test case for Initial Download --- .../initial/download/InitialDownloadRobot.kt | 80 +++++++++++++++++++ .../initial/download/InitialDownloadTest.kt | 74 +++++++++++++++++ .../core/utils/SharedPreferenceUtil.kt | 2 +- lintConfig.xml | 1 + 4 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 app/src/androidTest/java/org/kiwix/kiwixmobile/initial/download/InitialDownloadRobot.kt create mode 100644 app/src/androidTest/java/org/kiwix/kiwixmobile/initial/download/InitialDownloadTest.kt diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/initial/download/InitialDownloadRobot.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/initial/download/InitialDownloadRobot.kt new file mode 100644 index 000000000..583caaf82 --- /dev/null +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/initial/download/InitialDownloadRobot.kt @@ -0,0 +1,80 @@ +/* + * Kiwix Android + * Copyright (c) 2022 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.initial.download + +import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.action.ViewActions.click +import androidx.test.espresso.matcher.ViewMatchers.withText +import applyWithViewHierarchyPrinting +import com.adevinta.android.barista.interaction.BaristaSwipeRefreshInteractions.refresh +import org.kiwix.kiwixmobile.BaseRobot +import org.kiwix.kiwixmobile.Findable.Text +import org.kiwix.kiwixmobile.Findable.ViewId +import org.kiwix.kiwixmobile.R + +fun initialDownload(func: InitialDownloadRobot.() -> Unit) = + InitialDownloadRobot().applyWithViewHierarchyPrinting(func) + +class InitialDownloadRobot : BaseRobot() { + + private var retryCountForDataToLoad = 5 + private var retryCountForCheckDownloadStart = 5 + + fun assertLibraryListDisplayed() { + isVisible(ViewId(R.id.libraryList)) + } + + fun refreshList() { + refresh(R.id.librarySwipeRefresh) + } + + fun waitForDataToLoad() { + try { + isVisible(Text("Off the Grid")) + } catch (e: RuntimeException) { + if (retryCountForDataToLoad > 0) { + retryCountForDataToLoad-- + waitForDataToLoad() + } + } + } + + fun downloadZimFile() { + clickOn(Text("Off the Grid")) + } + + fun assertStorageConfigureDialogDisplayed() { + isVisible(Text("Download book to internal storage?")) + } + + fun clickYesToConfigureStorage() { + onView(withText("YES")).perform(click()) + } + + fun assertDownloadStart() { + try { + isVisible(ViewId(R.id.stop)) + } catch (e: RuntimeException) { + if (retryCountForCheckDownloadStart > 0) { + retryCountForCheckDownloadStart-- + assertDownloadStart() + } + } + } +} diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/initial/download/InitialDownloadTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/initial/download/InitialDownloadTest.kt new file mode 100644 index 000000000..9021a4ff3 --- /dev/null +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/initial/download/InitialDownloadTest.kt @@ -0,0 +1,74 @@ +/* + * Kiwix Android + * Copyright (c) 2022 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.initial.download + +import androidx.core.content.edit +import androidx.preference.PreferenceManager +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.filters.LargeTest +import androidx.test.internal.runner.junit4.statement.UiThreadStatement +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.rule.ActivityTestRule +import androidx.test.uiautomator.UiDevice +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.kiwix.kiwixmobile.BaseActivityTest +import org.kiwix.kiwixmobile.R +import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil +import org.kiwix.kiwixmobile.main.KiwixMainActivity + +@LargeTest +@RunWith(AndroidJUnit4::class) +class InitialDownloadTest : BaseActivityTest() { + override var activityRule: ActivityTestRule = activityTestRule { + PreferenceManager.getDefaultSharedPreferences(context).edit { + putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false) + putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false) + putBoolean(SharedPreferenceUtil.PREF_SHOW_STORAGE_OPTION, true) + } + } + + @Before + fun waitForIdle() { + UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).waitForIdle() + } + + @Test + fun initialDownloadTest() { + UiThreadStatement.runOnUiThread { activityRule.activity.navigate(R.id.downloadsFragment) } + initialDownload { + assertLibraryListDisplayed() + refreshList() + waitForDataToLoad() + downloadZimFile() + assertStorageConfigureDialogDisplayed() + clickYesToConfigureStorage() + assertDownloadStart() + } + } + + @After + fun setPrefStorageOption() { + PreferenceManager.getDefaultSharedPreferences(context).edit { + putBoolean(SharedPreferenceUtil.PREF_SHOW_STORAGE_OPTION, false) + } + } +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.kt index e78eadd8b..029ebebc1 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.kt @@ -218,7 +218,7 @@ class SharedPreferenceUtil @Inject constructor(val context: Context) { private const val PREF_NEW_TAB_BACKGROUND = "pref_newtab_background" private const val PREF_STORAGE_TITLE = "pref_selected_title" private const val PREF_EXTERNAL_LINK_POPUP = "pref_external_link_popup" - private const val PREF_SHOW_STORAGE_OPTION = "show_storgae_option" + const val PREF_SHOW_STORAGE_OPTION = "show_storgae_option" private const val PREF_IS_FIRST_RUN = "isFirstRun" private const val PREF_SHOW_BOOKMARKS_ALL_BOOKS = "show_bookmarks_current_book" private const val PREF_SHOW_HISTORY_ALL_BOOKS = "show_history_current_book" diff --git a/lintConfig.xml b/lintConfig.xml index 4ab2543df..d62dc90d0 100644 --- a/lintConfig.xml +++ b/lintConfig.xml @@ -18,6 +18,7 @@ +