From 530faa46bb8617672f7297c58544190825f36344 Mon Sep 17 00:00:00 2001 From: MohitMali Date: Thu, 28 Sep 2023 15:14:17 +0530 Subject: [PATCH] added a test case to evaluate the functionality of `LocalFileTransferFragment`. * This test case is designed to verify that the functionality works correctly, especially addressing the issue where `LocalFileTransferFragment` would shut down when attempting to search nearby devices for connection. --- .../LocalFileTransferRobot.kt | 22 +++- .../LocalFileTransferTest.kt | 120 ++++++++++++++++++ .../main/TopLevelDestinationTest.kt | 4 +- 3 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 app/src/androidTest/java/org/kiwix/kiwixmobile/localFileTransfer/LocalFileTransferTest.kt diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/localFileTransfer/LocalFileTransferRobot.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/localFileTransfer/LocalFileTransferRobot.kt index b43e2e131..153c41a8c 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/localFileTransfer/LocalFileTransferRobot.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/localFileTransfer/LocalFileTransferRobot.kt @@ -19,9 +19,12 @@ package org.kiwix.kiwixmobile.localFileTransfer import applyWithViewHierarchyPrinting +import com.adevinta.android.barista.interaction.BaristaSleepInteractions import org.kiwix.kiwixmobile.BaseRobot +import org.kiwix.kiwixmobile.Findable.ViewId import org.kiwix.kiwixmobile.Findable.StringId.TextId import org.kiwix.kiwixmobile.R +import org.kiwix.kiwixmobile.testutils.TestUtils /** * Authored by Ayush Shrivastava on 29/10/20 @@ -32,7 +35,24 @@ fun localFileTransfer(func: LocalFileTransferRobot.() -> Unit) = class LocalFileTransferRobot : BaseRobot() { - init { + fun assertReceiveFileTitleVisible() { isVisible(TextId(R.string.receive_files_title)) } + + fun assertSearchDeviceMenuItemVisible() { + isVisible(ViewId(R.id.menu_item_search_devices)) + } + + fun clickOnSearchDeviceMenuItem() { + clickOn(ViewId(R.id.menu_item_search_devices)) + } + + fun assertLocalFileTransferScreenVisible() { + BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS_FOR_DOWNLOAD_TEST.toLong()) + assertReceiveFileTitleVisible() + } + + fun assertLocalLibraryVisible() { + isVisible(TextId(R.string.library)) + } } diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/localFileTransfer/LocalFileTransferTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/localFileTransfer/LocalFileTransferTest.kt new file mode 100644 index 000000000..0f88eb81c --- /dev/null +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/localFileTransfer/LocalFileTransferTest.kt @@ -0,0 +1,120 @@ +/* + * 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.localFileTransfer + +import android.Manifest +import android.app.Instrumentation +import android.content.Context +import android.os.Build +import androidx.core.content.edit +import androidx.lifecycle.Lifecycle +import androidx.preference.PreferenceManager +import androidx.test.core.app.ActivityScenario +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.rule.GrantPermissionRule +import androidx.test.uiautomator.UiDevice +import leakcanary.LeakAssertions +import org.junit.After +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.kiwix.kiwixmobile.R +import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil +import org.kiwix.kiwixmobile.main.KiwixMainActivity +import org.kiwix.kiwixmobile.nav.destination.library.library +import org.kiwix.kiwixmobile.testutils.RetryRule +import org.kiwix.kiwixmobile.testutils.TestUtils + +class LocalFileTransferTest { + @Rule + @JvmField + var retryRule = RetryRule() + + private var context: Context? = null + private lateinit var activityScenario: ActivityScenario + + private val permissions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + arrayOf( + Manifest.permission.NEARBY_WIFI_DEVICES + ) + } else { + arrayOf( + Manifest.permission.READ_EXTERNAL_STORAGE, + Manifest.permission.WRITE_EXTERNAL_STORAGE, + Manifest.permission.ACCESS_COARSE_LOCATION, + Manifest.permission.ACCESS_FINE_LOCATION + ) + } + + @Rule + @JvmField + var permissionRules: GrantPermissionRule = + GrantPermissionRule.grant(*permissions) + + private val instrumentation: Instrumentation by lazy { + InstrumentationRegistry.getInstrumentation() + } + + @Before + fun setup() { + context = instrumentation.targetContext.applicationContext + UiDevice.getInstance(instrumentation).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 + fun localFileTransfer() { + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) { + activityScenario.onActivity { + it.navigate(R.id.libraryFragment) + } + library { + assertGetZimNearbyDeviceDisplayed() + clickFileTransferIcon { + assertReceiveFileTitleVisible() + assertSearchDeviceMenuItemVisible() + clickOnSearchDeviceMenuItem() + assertLocalFileTransferScreenVisible() + pressBack() + assertLocalLibraryVisible() + } + } + LeakAssertions.assertNoLeaks() + } + } + + @After + fun setIsTestPreference() { + PreferenceManager.getDefaultSharedPreferences(context).edit { + putBoolean(SharedPreferenceUtil.PREF_IS_TEST, false) + } + } +} diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/main/TopLevelDestinationTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/main/TopLevelDestinationTest.kt index feaab76db..5aaa492a1 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/main/TopLevelDestinationTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/main/TopLevelDestinationTest.kt @@ -31,6 +31,7 @@ import org.junit.Test import org.kiwix.kiwixmobile.BaseActivityTest import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil import org.kiwix.kiwixmobile.help.HelpRobot +import org.kiwix.kiwixmobile.localFileTransfer.LocalFileTransferRobot import org.kiwix.kiwixmobile.nav.destination.library.OnlineLibraryRobot import org.kiwix.kiwixmobile.settings.SettingsRobot import org.kiwix.kiwixmobile.testutils.RetryRule @@ -72,8 +73,7 @@ class TopLevelDestinationTest : BaseActivityTest() { } clickLibraryOnBottomNav { assertGetZimNearbyDeviceDisplayed() - clickFileTransferIcon { - } + clickFileTransferIcon(LocalFileTransferRobot::assertReceiveFileTitleVisible) } clickDownloadOnBottomNav(OnlineLibraryRobot::assertLibraryListDisplayed) clickBookmarksOnNavDrawer {