From 5c39f095c324314af2a26741d4d23db5d24aa55c Mon Sep 17 00:00:00 2001 From: MohitMali Date: Tue, 20 Sep 2022 14:21:05 +0530 Subject: [PATCH] Changes after review --- .../org/kiwix/kiwixmobile/DownloadTest.kt | 6 +- .../org/kiwix/kiwixmobile/NetworkTest.java | 6 +- .../kiwix/kiwixmobile/testutils/TestUtils.kt | 67 +++++++++++-------- .../core/entity/LibraryNetworkEntity.java | 2 +- 4 files changed, 48 insertions(+), 33 deletions(-) diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/DownloadTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/DownloadTest.kt index 1dcb0f87f..0057ed902 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/DownloadTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/DownloadTest.kt @@ -42,6 +42,8 @@ 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.allowStoragePermission +import org.kiwix.kiwixmobile.testutils.TestUtils.hasStoragePermission import org.kiwix.kiwixmobile.utils.KiwixIdlingResource.Companion.getInstance import org.kiwix.kiwixmobile.utils.StandardActions import java.util.concurrent.TimeUnit @@ -71,7 +73,9 @@ class DownloadTest { BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong()) clickMenu(TestUtils.getResourceString(R.string.library)) clickOn(R.string.local_zims) - TestUtils.allowPermissionsIfNeeded() + if (!hasStoragePermission()) { + allowStoragePermission() + } StandardActions.deleteZimIfExists("ray_charles", R.id.zimfilelist) clickOn(R.string.remote_zims) TestUtils.captureAndSaveScreenshot("Before-checking-for-ZimManager-Main-Activity") diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/NetworkTest.java b/app/src/androidTest/java/org/kiwix/kiwixmobile/NetworkTest.java index 911c05680..70365250e 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/NetworkTest.java +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/NetworkTest.java @@ -49,9 +49,7 @@ import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.action.ViewActions.click; import static androidx.test.espresso.action.ViewActions.longClick; import static androidx.test.espresso.matcher.ViewMatchers.withId; -import static com.adevinta.android.barista.interaction.BaristaClickInteractions.clickOn; import static com.adevinta.android.barista.interaction.BaristaDialogInteractions.clickDialogPositiveButton; -import static com.adevinta.android.barista.interaction.BaristaMenuClickInteractions.clickMenu; import static com.adevinta.android.barista.interaction.BaristaSwipeRefreshInteractions.refresh; import static org.hamcrest.CoreMatchers.allOf; import static org.kiwix.kiwixmobile.testutils.TestUtils.TEST_PAUSE_MS; @@ -120,7 +118,9 @@ public class NetworkTest { BaristaSleepInteractions.sleep(TEST_PAUSE_MS); BaristaMenuClickInteractions.clickMenu(getResourceString(R.string.library)); - TestUtils.allowPermissionsIfNeeded(); + if (!TestUtils.hasStoragePermission()) { + TestUtils.allowStoragePermission(); + } onData(withContent("wikipedia_ab_all_2017-03")).inAdapterView(withId(R.id.libraryList)) .perform(click()); 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 5306e4248..aa5679559 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/testutils/TestUtils.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/testutils/TestUtils.kt @@ -18,25 +18,26 @@ package org.kiwix.kiwixmobile.testutils import android.Manifest -import androidx.core.content.ContextCompat import android.content.pm.PackageManager import android.graphics.Bitmap import android.os.Build import android.os.Environment import android.util.Log -import androidx.test.uiautomator.UiDevice -import androidx.test.uiautomator.UiSelector -import androidx.test.uiautomator.UiObjectNotFoundException -import androidx.test.runner.screenshot.Screenshot +import androidx.annotation.RequiresApi +import androidx.core.content.ContextCompat import androidx.test.espresso.matcher.BoundedMatcher import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.runner.screenshot.Screenshot +import androidx.test.uiautomator.UiDevice +import androidx.test.uiautomator.UiObjectNotFoundException +import androidx.test.uiautomator.UiSelector import org.hamcrest.Description import org.hamcrest.Matcher import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity import java.io.File import java.io.FileNotFoundException import java.io.FileOutputStream -import java.io.IOException +import java.io.OutputStream import java.text.SimpleDateFormat import java.util.Date @@ -60,29 +61,39 @@ object TestUtils { however I'm unsure if it's speed related, or Android Version related. */ - private fun hasStoragePermission(): Boolean { - return ContextCompat.checkSelfPermission( + + private fun hasReadExternalStoragePermission(): Boolean = + ContextCompat.checkSelfPermission( InstrumentationRegistry.getInstrumentation().targetContext, Manifest.permission.READ_EXTERNAL_STORAGE - ) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission( + ) == PackageManager.PERMISSION_GRANTED + + private fun hasWriteExternalStoragePermission(): Boolean = + ContextCompat.checkSelfPermission( InstrumentationRegistry.getInstrumentation().targetContext, Manifest.permission.WRITE_EXTERNAL_STORAGE ) == PackageManager.PERMISSION_GRANTED - } - @JvmStatic fun allowPermissionsIfNeeded() { - if (Build.VERSION.SDK_INT >= 23 && !hasStoragePermission()) { - val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) - val allowPermissions = - device.findObject( - UiSelector().clickable(true) - .checkable(false).index(1) - ) - if (allowPermissions.exists()) { - try { - allowPermissions.click() - } catch (e: UiObjectNotFoundException) { - } + @RequiresApi(Build.VERSION_CODES.R) + private fun hasManageExternalStoragePermission(): Boolean = + Environment.isExternalStorageManager() + + @JvmStatic fun hasStoragePermission(): Boolean = + if (Build.VERSION.SDK_INT < 23) true + else hasReadExternalStoragePermission() && hasWriteExternalStoragePermission() + + @JvmStatic fun allowStoragePermission() { + val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) + val allowPermissions = + device.findObject( + UiSelector().clickable(true) + .checkable(false).index(1) + ) + if (allowPermissions.exists()) { + try { + allowPermissions.click() + } catch (e: UiObjectNotFoundException) { + Log.w(TAG, "Unable to find allow permission dialog", e) } } } @@ -101,14 +112,14 @@ object TestUtils { val fileName = "TEST_${timestamp}_$name.png" val outFile = File(screenshotDir.path + File.separator + fileName) val screenshot = Screenshot.capture().bitmap ?: return + var fos: OutputStream? = null try { - val fos = FileOutputStream(outFile) + fos = FileOutputStream(outFile) screenshot.compress(Bitmap.CompressFormat.PNG, 90, fos) - fos.close() } catch (e: FileNotFoundException) { - Log.w(TAG, "Failed to save Screenshot", e) - } catch (e: IOException) { - Log.w(TAG, "Failed to save Screenshot", e) + Log.w(TAG, "Unable to create file $outFile", e) + } finally { + fos?.close() } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/entity/LibraryNetworkEntity.java b/core/src/main/java/org/kiwix/kiwixmobile/core/entity/LibraryNetworkEntity.java index 6953de8d1..5f7d92d31 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/entity/LibraryNetworkEntity.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/entity/LibraryNetworkEntity.java @@ -91,7 +91,7 @@ public class LibraryNetworkEntity { public String tags; public int searchMatches = 0; - @Deprecated + public File file; @Deprecated public String remoteUrl;