From a79cb719a59446b099d12f5713ddb61463faad41 Mon Sep 17 00:00:00 2001 From: MohitMali Date: Fri, 25 Nov 2022 11:26:26 +0530 Subject: [PATCH] Re-enable download test --- .github/workflows/coverage.yml | 3 +- .../org/kiwix/kiwixmobile/DownloadTest.kt | 126 ------------------ .../kiwixmobile/download/DownloadRobot.kt | 113 ++++++++++++++++ .../kiwixmobile/download/DownloadTest.kt | 116 ++++++++++++++++ .../kiwix/kiwixmobile/testutils/TestUtils.kt | 1 + .../kiwixmobile/main/KiwixMainActivity.kt | 4 +- .../core/utils/SharedPreferenceUtil.kt | 2 +- lintConfig.xml | 1 + 8 files changed, 237 insertions(+), 129 deletions(-) delete mode 100644 app/src/androidTest/java/org/kiwix/kiwixmobile/DownloadTest.kt create mode 100644 app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadRobot.kt create mode 100644 app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadTest.kt diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index bfad87af5..64a384e1d 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -25,8 +25,9 @@ jobs: fetch-depth: 1 - name: Set up JDK 11 - uses: actions/setup-java@v1 + uses: actions/setup-java@v2 with: + distribution: temurin java-version: 11 - name: Restore Cache diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/DownloadTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/DownloadTest.kt deleted file mode 100644 index 409a9bbcb..000000000 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/DownloadTest.kt +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Kiwix Android - * Copyright (c) 2019 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 - -import android.Manifest -import android.util.Log -import androidx.test.espresso.Espresso -import androidx.test.espresso.IdlingPolicies -import androidx.test.espresso.IdlingRegistry -import androidx.test.espresso.matcher.ViewMatchers -import androidx.test.ext.junit.runners.AndroidJUnit4 -import androidx.test.filters.LargeTest -import androidx.test.rule.ActivityTestRule -import androidx.test.rule.GrantPermissionRule -import com.adevinta.android.barista.assertion.BaristaVisibilityAssertions.assertDisplayed -import com.adevinta.android.barista.interaction.BaristaClickInteractions.clickOn -import com.adevinta.android.barista.interaction.BaristaMenuClickInteractions.clickMenu -import com.adevinta.android.barista.interaction.BaristaSleepInteractions -import com.adevinta.android.barista.interaction.BaristaSwipeRefreshInteractions.refresh -import org.junit.After -import org.junit.Assert -import org.junit.Before -import org.junit.BeforeClass -import org.junit.Ignore -import org.junit.Rule -import org.junit.Test -import org.junit.runner.RunWith -import org.kiwix.kiwixmobile.main.KiwixMainActivity -import org.kiwix.kiwixmobile.testutils.TestUtils -import org.kiwix.kiwixmobile.testutils.TestUtils.allowStoragePermissionsIfNeeded -import org.kiwix.kiwixmobile.utils.KiwixIdlingResource.Companion.getInstance -import org.kiwix.kiwixmobile.utils.StandardActions -import java.util.concurrent.TimeUnit - -@LargeTest -@RunWith(AndroidJUnit4::class) -class DownloadTest { - @Rule - var activityTestRule = ActivityTestRule( - KiwixMainActivity::class.java - ) - - @Rule - var readPermissionRule: GrantPermissionRule = - GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE) - - @Rule - var writePermissionRule: GrantPermissionRule = - GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE) - - @Before fun setUp() { - // default - } - - @Test @Ignore("Broken in 2.5") // TODO: Fix in 3.0 - fun downloadTest() { - BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong()) - clickMenu(TestUtils.getResourceString(R.string.library)) - clickOn(R.string.local_zims) - allowStoragePermissionsIfNeeded() - StandardActions.deleteZimIfExists("ray_charles", R.id.zimfilelist) - clickOn(R.string.remote_zims) - TestUtils.captureAndSaveScreenshot("Before-checking-for-ZimManager-Main-Activity") - TestUtils.captureAndSaveScreenshot("After-the-check-completed") - BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong()) - try { - Espresso.onData(TestUtils.withContent("ray_charles")) - .inAdapterView(ViewMatchers.withId(R.id.libraryList)) - } catch (e: Exception) { - Assert.fail( - """ - Couldn't find downloaded file 'ray_charles' - Original Exception: - ${e.localizedMessage} - """.trimIndent() - ) - } - StandardActions.deleteZimIfExists("ray_charles", R.id.libraryList) - assertDisplayed(R.string.local_zims) - clickOn(R.string.local_zims) - try { - refresh(R.id.zim_swiperefresh) - } catch (e: RuntimeException) { - Log.w(KIWIX_DOWNLOAD_TEST, "Failed to refresh ZIM list: " + e.localizedMessage) - } - - /** - * Commented out the following as it uses another Activity. - * TODO Once we find a good way to run cross-activity re-implement - * this functionality in the tests. - - * onData(withContent("ray_charles")).inAdapterView(withId(R.id.zimfilelist)).perform(click()); - * openContextualActionModeOverflowMenu(); - * onView(withText("Get Content")).perform(click()); - */ - StandardActions.deleteZimIfExists("ray_charles", R.id.zimfilelist) - } - - @After fun finish() { - IdlingRegistry.getInstance().unregister(getInstance()) - } - - companion object { - private const val KIWIX_DOWNLOAD_TEST = "kiwixDownloadTest" - @BeforeClass fun beforeClass() { - IdlingPolicies.setMasterPolicyTimeout(180, TimeUnit.SECONDS) - IdlingPolicies.setIdlingResourceTimeout(180, TimeUnit.SECONDS) - IdlingRegistry.getInstance().register(getInstance()) - } - } -} diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadRobot.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadRobot.kt new file mode 100644 index 000000000..bf179e5b2 --- /dev/null +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadRobot.kt @@ -0,0 +1,113 @@ +/* + * 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.download + +import android.util.Log +import androidx.recyclerview.widget.RecyclerView.ViewHolder +import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.action.ViewActions.click +import androidx.test.espresso.assertion.ViewAssertions.doesNotExist +import androidx.test.espresso.contrib.RecyclerViewActions.scrollToPosition +import androidx.test.espresso.matcher.ViewMatchers.withId +import androidx.test.espresso.matcher.ViewMatchers.withText +import applyWithViewHierarchyPrinting +import com.adevinta.android.barista.interaction.BaristaSleepInteractions +import junit.framework.AssertionFailedError +import org.kiwix.kiwixmobile.BaseRobot +import org.kiwix.kiwixmobile.Findable.Text +import org.kiwix.kiwixmobile.Findable.ViewId +import org.kiwix.kiwixmobile.R +import org.kiwix.kiwixmobile.testutils.TestUtils + +fun downloadRobot(func: DownloadRobot.() -> Unit) = + DownloadRobot().applyWithViewHierarchyPrinting(func) + +class DownloadRobot : BaseRobot() { + + private var retryCountForDataToLoad = 5 + private var retryCountForCheckDownloadStart = 5 + private val zimFileTitle = "A little question a day" + + fun clickLibraryOnBottomNav() { + clickOn(ViewId(R.id.libraryFragment)) + } + + fun clickDownloadOnBottomNav() { + clickOn(ViewId(R.id.downloadsFragment)) + } + + fun deleteZimIfExists() { + try { + longClickOn(Text(zimFileTitle)) + clickOn(ViewId(R.id.zim_file_delete_item)) + onView(withText("DELETE")).perform(click()) + } catch (e: RuntimeException) { + Log.i( + "TEST_DELETE_ZIM", + "Failed to delete ZIM file with title [" + zimFileTitle + "]... " + + "Probably because it doesn't exist" + ) + } + } + + fun waitForDataToLoad() { + try { + isVisible(Text("Off the Grid")) + } catch (e: RuntimeException) { + if (retryCountForDataToLoad > 0) { + retryCountForDataToLoad-- + waitForDataToLoad() + } + } + } + + fun scrollToAlpineWikiZim() { + onView(withId(R.id.libraryList)).perform(scrollToPosition(15)) + } + + fun checkIfZimFileDownloaded() { + isVisible(Text(zimFileTitle)) + } + + fun downloadZimFile() { + clickOn(Text(zimFileTitle)) + } + + fun assertDownloadStart() { + try { + isVisible(ViewId(R.id.stop)) + } catch (e: RuntimeException) { + if (retryCountForCheckDownloadStart > 0) { + retryCountForCheckDownloadStart-- + assertDownloadStart() + } + } + } + + fun waitUntilDownloadComplete() { + try { + onView(withId(R.id.stop)).check(doesNotExist()) + Log.i("kiwixDownloadTest", "Download complete") + } catch (e: AssertionFailedError) { + BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS_FOR_DOWNLOAD_TEST.toLong()) + Log.i("kiwixDownloadTest", "Downloading in progress") + waitUntilDownloadComplete() + } + } +} diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadTest.kt new file mode 100644 index 000000000..e90ebf197 --- /dev/null +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadTest.kt @@ -0,0 +1,116 @@ +/* + * 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.download + +import android.util.Log +import androidx.core.content.edit +import androidx.preference.PreferenceManager +import androidx.test.core.app.ActivityScenario +import androidx.test.espresso.IdlingPolicies +import androidx.test.espresso.IdlingRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.filters.LargeTest +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.uiautomator.UiDevice +import com.adevinta.android.barista.interaction.BaristaSleepInteractions +import com.adevinta.android.barista.interaction.BaristaSwipeRefreshInteractions.refresh +import org.junit.After +import org.junit.Assert +import org.junit.Before +import org.junit.BeforeClass +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 +import org.kiwix.kiwixmobile.testutils.TestUtils +import org.kiwix.kiwixmobile.utils.KiwixIdlingResource.Companion.getInstance +import java.util.concurrent.TimeUnit + +@LargeTest +@RunWith(AndroidJUnit4::class) +class DownloadTest : BaseActivityTest() { + + @Before + override fun waitForIdle() { + UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).waitForIdle() + PreferenceManager.getDefaultSharedPreferences( + InstrumentationRegistry.getInstrumentation().targetContext.applicationContext + ).edit { + putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false) + putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false) + putBoolean(SharedPreferenceUtil.PREF_SHOW_STORAGE_OPTION, false) + putBoolean(SharedPreferenceUtil.IS_PLAY_STORE_BUILD, true) + putBoolean(SharedPreferenceUtil.PREF_IS_TEST, true) + } + } + + @Test + fun downloadTest() { + ActivityScenario.launch(KiwixMainActivity::class.java) + BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong()) + try { + downloadRobot { + clickLibraryOnBottomNav() + deleteZimIfExists() + clickDownloadOnBottomNav() + waitForDataToLoad() + scrollToAlpineWikiZim() + downloadZimFile() + assertDownloadStart() + waitUntilDownloadComplete() + clickLibraryOnBottomNav() + checkIfZimFileDownloaded() + } + } catch (e: Exception) { + Assert.fail( + """ + Couldn't find downloaded file 'A little question a day' + Original Exception: + ${e.localizedMessage} + """.trimIndent() + ) + } + try { + refresh(R.id.zim_swiperefresh) + } catch (e: RuntimeException) { + Log.w(KIWIX_DOWNLOAD_TEST, "Failed to refresh ZIM list: " + e.localizedMessage) + } + } + + @After + fun finish() { + IdlingRegistry.getInstance().unregister(getInstance()) + PreferenceManager.getDefaultSharedPreferences(context).edit { + putBoolean(SharedPreferenceUtil.IS_PLAY_STORE_BUILD, false) + putBoolean(SharedPreferenceUtil.PREF_IS_TEST, false) + } + } + + companion object { + private const val KIWIX_DOWNLOAD_TEST = "kiwixDownloadTest" + + @BeforeClass + fun beforeClass() { + IdlingPolicies.setMasterPolicyTimeout(180, TimeUnit.SECONDS) + IdlingPolicies.setIdlingResourceTimeout(180, TimeUnit.SECONDS) + IdlingRegistry.getInstance().register(getInstance()) + } + } +} diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/testutils/TestUtils.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/testutils/TestUtils.kt index 6a2be60de..2c8002ff7 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/testutils/TestUtils.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/testutils/TestUtils.kt @@ -48,6 +48,7 @@ object TestUtils { private const val TAG = "TESTUTILS" @JvmField var TEST_PAUSE_MS = 250 var TEST_PAUSE_MS_FOR_SEARCH_TEST = 1000 + var TEST_PAUSE_MS_FOR_DOWNLOAD_TEST = 10000 /* TEST_PAUSE_MS is used as such: diff --git a/app/src/main/java/org/kiwix/kiwixmobile/main/KiwixMainActivity.kt b/app/src/main/java/org/kiwix/kiwixmobile/main/KiwixMainActivity.kt index 8ea61b623..aaea6c044 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/KiwixMainActivity.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/KiwixMainActivity.kt @@ -122,7 +122,9 @@ class KiwixMainActivity : CoreMainActivity() { if (sharedPreferenceUtil.showIntro() && !isIntroScreenNotVisible()) { navigate(KiwixReaderFragmentDirections.actionReaderFragmentToIntroFragment()) } - sharedPreferenceUtil.setIsPlayStoreBuildType(BuildConfig.IS_PLAYSTORE) + if (!sharedPreferenceUtil.prefIsTest) { + sharedPreferenceUtil.setIsPlayStoreBuildType(BuildConfig.IS_PLAYSTORE) + } } private fun isIntroScreenNotVisible(): Boolean = 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 19738b8cf..48638be5e 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 @@ -235,6 +235,6 @@ class SharedPreferenceUtil @Inject constructor(val context: Context) { private const val TEXT_ZOOM = "true_text_zoom" private const val DEFAULT_ZOOM = 100 private const val PREF_MANAGE_EXTERNAL_FILES = "pref_manage_external_files" - private const val IS_PLAY_STORE_BUILD = "is_play_store_build" + const val IS_PLAY_STORE_BUILD = "is_play_store_build" } } diff --git a/lintConfig.xml b/lintConfig.xml index d62dc90d0..fdbdc1010 100644 --- a/lintConfig.xml +++ b/lintConfig.xml @@ -19,6 +19,7 @@ +