From 57a95f15d11f006e78910ff5f9ff5b6a14549fdf Mon Sep 17 00:00:00 2001 From: MohitMali Date: Wed, 27 Sep 2023 18:05:54 +0530 Subject: [PATCH 1/4] Fixed, The File transfer feature does not work in android 12, android 13. * In Android 13, we previously used `intent.getParcelableArrayExtra` to retrieve the status of `P2P_DEVICE_CHANGED`. However, this method returned null because it's not actually an array. To resolve this issue, we have updated it to use `intent.getParcelableExtra`. * For Android 12, we used to check the network state through `ConnectivityManager` every time the `WIFI_P2P_CONNECTION_CHANGED_ACTION` action was triggered. However, this approach wasn't reliable, as the action being triggered didn't guarantee a change in the network state. Instead, it sends an intent that we should now check for network changes. As a result, we have modified our code to utilize this intent within the `KiwixWifiP2pBroadcastReceiver`. --- .../KiwixWifiP2pBroadcastReceiver.kt | 22 +++++++++++-------- .../localFileTransfer/WifiDirectManager.kt | 6 ++--- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/KiwixWifiP2pBroadcastReceiver.kt b/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/KiwixWifiP2pBroadcastReceiver.kt index 032bdf436..79625b483 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/KiwixWifiP2pBroadcastReceiver.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/KiwixWifiP2pBroadcastReceiver.kt @@ -16,15 +16,16 @@ * */ -@file:Suppress("PackageNaming") +@file:Suppress("PackageNaming", "DEPRECATION") package org.kiwix.kiwixmobile.localFileTransfer import android.content.BroadcastReceiver import android.content.Context import android.content.Intent -import android.net.ConnectivityManager +import android.net.NetworkInfo import android.net.wifi.p2p.WifiP2pDevice +import android.net.wifi.p2p.WifiP2pManager import android.net.wifi.p2p.WifiP2pManager.EXTRA_WIFI_P2P_DEVICE import android.net.wifi.p2p.WifiP2pManager.EXTRA_WIFI_STATE import android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION @@ -33,7 +34,6 @@ import android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION import android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_ENABLED import android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION import android.os.Build -import org.kiwix.kiwixmobile.core.compat.CompatHelper.Companion.isNetworkAvailable /** * Helper class for the local file sharing module. @@ -43,8 +43,7 @@ import org.kiwix.kiwixmobile.core.compat.CompatHelper.Companion.isNetworkAvailab */ class KiwixWifiP2pBroadcastReceiver( - private val p2pEventListener: P2pEventListener, - private val connectivityManager: ConnectivityManager + private val p2pEventListener: P2pEventListener ) : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { @@ -55,16 +54,21 @@ class KiwixWifiP2pBroadcastReceiver( } WIFI_P2P_PEERS_CHANGED_ACTION -> p2pEventListener.onPeersChanged() WIFI_P2P_CONNECTION_CHANGED_ACTION -> { - p2pEventListener.onConnectionChanged(connectivityManager.isNetworkAvailable()) + val networkInfo = + intent.getParcelableExtra( + WifiP2pManager.EXTRA_NETWORK_INFO + ) + networkInfo?.let { + p2pEventListener.onConnectionChanged(networkInfo.isConnected) + } } WIFI_P2P_THIS_DEVICE_CHANGED_ACTION -> { val userDevice = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - intent.getParcelableArrayExtra( + intent.getParcelableExtra( EXTRA_WIFI_P2P_DEVICE, WifiP2pDevice::class.java - )?.get(0) + ) } else { - @Suppress("DEPRECATION") intent.getParcelableExtra(EXTRA_WIFI_P2P_DEVICE) } p2pEventListener.onDeviceChanged(userDevice) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/WifiDirectManager.kt b/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/WifiDirectManager.kt index b7333bb3a..4a1b48747 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/WifiDirectManager.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/WifiDirectManager.kt @@ -20,7 +20,6 @@ package org.kiwix.kiwixmobile.localFileTransfer import android.content.BroadcastReceiver import android.content.Context import android.content.IntentFilter -import android.net.ConnectivityManager import android.net.Uri import android.net.wifi.WpsInfo import android.net.wifi.p2p.WifiP2pConfig @@ -62,8 +61,7 @@ class WifiDirectManager @Inject constructor( private val context: Context, private val sharedPreferenceUtil: SharedPreferenceUtil, private val alertDialogShower: AlertDialogShower, - private val manager: WifiP2pManager?, - private val connectivityManager: ConnectivityManager + private val manager: WifiP2pManager? ) : ChannelListener, PeerListListener, ConnectionInfoListener, P2pEventListener { var callbacks: Callbacks? = null @@ -98,7 +96,7 @@ class WifiDirectManager @Inject constructor( } private fun registerWifiDirectBroadcastReceiver() { - receiver = KiwixWifiP2pBroadcastReceiver(this, connectivityManager) + receiver = KiwixWifiP2pBroadcastReceiver(this) // For specifying broadcasts (of the P2P API) that the module needs to respond to val intentFilter = IntentFilter() From e4d27e19a188d835bb7166e9e3697a2c757bd145 Mon Sep 17 00:00:00 2001 From: MohitMali Date: Wed, 27 Sep 2023 18:56:18 +0530 Subject: [PATCH 2/4] Improved code for android 13. --- .../KiwixWifiP2pBroadcastReceiver.kt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/KiwixWifiP2pBroadcastReceiver.kt b/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/KiwixWifiP2pBroadcastReceiver.kt index 79625b483..48f07e21a 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/KiwixWifiP2pBroadcastReceiver.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/KiwixWifiP2pBroadcastReceiver.kt @@ -25,7 +25,7 @@ import android.content.Context import android.content.Intent import android.net.NetworkInfo import android.net.wifi.p2p.WifiP2pDevice -import android.net.wifi.p2p.WifiP2pManager +import android.net.wifi.p2p.WifiP2pManager.EXTRA_NETWORK_INFO import android.net.wifi.p2p.WifiP2pManager.EXTRA_WIFI_P2P_DEVICE import android.net.wifi.p2p.WifiP2pManager.EXTRA_WIFI_STATE import android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION @@ -54,10 +54,14 @@ class KiwixWifiP2pBroadcastReceiver( } WIFI_P2P_PEERS_CHANGED_ACTION -> p2pEventListener.onPeersChanged() WIFI_P2P_CONNECTION_CHANGED_ACTION -> { - val networkInfo = - intent.getParcelableExtra( - WifiP2pManager.EXTRA_NETWORK_INFO + val networkInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + intent.getParcelableExtra( + EXTRA_NETWORK_INFO, + NetworkInfo::class.java ) + } else { + intent.getParcelableExtra(EXTRA_NETWORK_INFO) + } networkInfo?.let { p2pEventListener.onConnectionChanged(networkInfo.isConnected) } From 530faa46bb8617672f7297c58544190825f36344 Mon Sep 17 00:00:00 2001 From: MohitMali Date: Thu, 28 Sep 2023 15:14:17 +0530 Subject: [PATCH 3/4] 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 { From 51c3ce3d51aeaceaa033086641fc55c2cff3e764 Mon Sep 17 00:00:00 2001 From: MohitMali Date: Thu, 5 Oct 2023 14:08:24 +0530 Subject: [PATCH 4/4] Fixed unit coverage --- .../kiwix/kiwixmobile/core/remote/BasicAuthInterceptorTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/remote/BasicAuthInterceptorTest.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/remote/BasicAuthInterceptorTest.kt index 828a779bd..2ab44cf5d 100644 --- a/core/src/test/java/org/kiwix/kiwixmobile/core/remote/BasicAuthInterceptorTest.kt +++ b/core/src/test/java/org/kiwix/kiwixmobile/core/remote/BasicAuthInterceptorTest.kt @@ -74,7 +74,7 @@ class BasicAuthInterceptorTest { @Test fun testUrlWithFormattingVariations() { - val formattedUrl1 = "https:// {{ BASIC_AUTH_KEY } } @example.com/kiwix/f/somefile.zim" + val formattedUrl1 = "https:// {{ BASIC_AUTH_KEY }} @example.com/kiwix/f/somefile.zim" val formattedUrl2 = "https://{{BASIC_AUTH_KEY}}@example.com/kiwix/f/somefile.zim " assertEquals(true, formattedUrl1.isAuthenticationUrl) assertEquals(true, formattedUrl2.isAuthenticationUrl)