diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/DownloadTest.java b/app/src/androidTest/java/org/kiwix/kiwixmobile/DownloadTest.java deleted file mode 100644 index 00670e26e..000000000 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/DownloadTest.java +++ /dev/null @@ -1,135 +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.IdlingPolicies; -import androidx.test.espresso.IdlingRegistry; -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.interaction.BaristaMenuClickInteractions; -import com.adevinta.android.barista.interaction.BaristaSleepInteractions; -import java.util.concurrent.TimeUnit; -import org.junit.After; -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.utils.KiwixIdlingResource; - -import static androidx.test.espresso.Espresso.onData; -import static androidx.test.espresso.matcher.ViewMatchers.withId; -import static com.adevinta.android.barista.assertion.BaristaVisibilityAssertions.assertDisplayed; -import static com.adevinta.android.barista.interaction.BaristaClickInteractions.clickOn; -import static com.adevinta.android.barista.interaction.BaristaSwipeRefreshInteractions.refresh; -import static junit.framework.Assert.fail; -import static org.kiwix.kiwixmobile.testutils.TestUtils.TEST_PAUSE_MS; -import static org.kiwix.kiwixmobile.testutils.TestUtils.allowPermissionsIfNeeded; -import static org.kiwix.kiwixmobile.testutils.TestUtils.captureAndSaveScreenshot; -import static org.kiwix.kiwixmobile.testutils.TestUtils.getResourceString; -import static org.kiwix.kiwixmobile.testutils.TestUtils.withContent; -import static org.kiwix.kiwixmobile.utils.StandardActions.deleteZimIfExists; - -@LargeTest -@RunWith(AndroidJUnit4.class) -public class DownloadTest { - - private static final String KIWIX_DOWNLOAD_TEST = "kiwixDownloadTest"; - @Rule - public ActivityTestRule activityTestRule = - new ActivityTestRule<>(KiwixMainActivity.class); - @Rule - public GrantPermissionRule readPermissionRule = - GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE); - @Rule - public GrantPermissionRule writePermissionRule = - GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE); - - @BeforeClass - public static void beforeClass() { - IdlingPolicies.setMasterPolicyTimeout(180, TimeUnit.SECONDS); - IdlingPolicies.setIdlingResourceTimeout(180, TimeUnit.SECONDS); - IdlingRegistry.getInstance().register(KiwixIdlingResource.getInstance()); - } - - @Before - public void setUp() { - } - - @Test - @Ignore("Broken in 2.5")//TODO: Fix in 3.0 - public void downloadTest() { - BaristaSleepInteractions.sleep(TEST_PAUSE_MS); - BaristaMenuClickInteractions.clickMenu(getResourceString(R.string.library)); - - clickOn(R.string.local_zims); - - allowPermissionsIfNeeded(); - - deleteZimIfExists("ray_charles", R.id.zimfilelist); - - clickOn(R.string.remote_zims); - - captureAndSaveScreenshot("Before-checking-for-ZimManager-Main-Activity"); - captureAndSaveScreenshot("After-the-check-completed"); - - BaristaSleepInteractions.sleep(TEST_PAUSE_MS); - - try { - onData(withContent("ray_charles")).inAdapterView(withId(R.id.libraryList)); - } catch (Exception e) { - fail("Couldn't find downloaded file 'ray_charles'\n\nOriginal Exception:\n" + - e.getLocalizedMessage() + "\n\n"); - } - - deleteZimIfExists("ray_charles", R.id.libraryList); - - assertDisplayed(R.string.local_zims); - clickOn(R.string.local_zims); - - try { - refresh(R.id.zim_swiperefresh); - } catch (RuntimeException e) { - Log.w(KIWIX_DOWNLOAD_TEST, "Failed to refresh ZIM list: " + e.getLocalizedMessage()); - } - -/* -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()); -*/ - - deleteZimIfExists("ray_charles", R.id.zimfilelist); - } - - @After - public void finish() { - IdlingRegistry.getInstance().unregister(KiwixIdlingResource.getInstance()); - } -} diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/DownloadTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/DownloadTest.kt new file mode 100644 index 000000000..1dcb0f87f --- /dev/null +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/DownloadTest.kt @@ -0,0 +1,125 @@ +/* + * 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.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) + TestUtils.allowPermissionsIfNeeded() + 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()) + } + } +}