From 4eda0f9df52d850539eeeaad68e40fa111bacb7f Mon Sep 17 00:00:00 2001 From: Gergely Hegedus Date: Wed, 13 Jul 2022 15:04:34 +0300 Subject: [PATCH] PR#101 Adjustments so older API versions save to External Storage --- gradlescripts/pull-screenshots.gradle | 69 ++++++++++++------- .../screenshot/basicScreenCaptureProcessor.kt | 9 ++- 2 files changed, 51 insertions(+), 27 deletions(-) diff --git a/gradlescripts/pull-screenshots.gradle b/gradlescripts/pull-screenshots.gradle index 6c5f572..f04dacc 100644 --- a/gradlescripts/pull-screenshots.gradle +++ b/gradlescripts/pull-screenshots.gradle @@ -25,41 +25,58 @@ def findAdbFromLocal = { } } -task pullScreenshots(type: Exec) { - group = 'Test' - description = 'Pull screenshots' +def packageName = propertyOrNull("screenshotsPackageName") ?: "$android.defaultConfig.applicationId" +def screenshotDirectory = propertyOrNull("screenshotsDirectory") ?: "test-screenshots" +def savePath = propertyOrNull("screenshotsSavePath") ?: "build/testscreenshots/" +def adb = propertyOrNull("adbPath") ?: findAdbFromLocal() +def internalFullPath = "/sdcard/Android/data/$packageName/files/Pictures/$screenshotDirectory/" +def deprecatedFullPath = "/sdcard/Pictures/$packageName/$screenshotDirectory/" - def packageName = propertyOrNull("screenshotsPackageName") ?: "$android.defaultConfig.applicationId" - def screenshotDirectory = propertyOrNull("screenshotsDirectory") ?: "test-screenshots" - def fullPath = "/sdcard/Android/data/$packageName/files/Pictures/$screenshotDirectory/" - def savePath = propertyOrNull("screenshotsSavePath") ?: "build/testscreenshots/" - def adb = propertyOrNull("adbPath") ?: findAdbFromLocal() +task pullScreenshotsInternal(type: Exec) { + group = 'Test-Screenshots' + description = 'Pull screenshots From internal Storage' - try { - commandLine "$adb", 'pull', "$fullPath", "$savePath/" - } catch (Throwable throwable) { - throwable.printStackTrace() - } + ignoreExitValue(true) + commandLine "$adb", 'pull', "$internalFullPath", "$savePath/" } -task removeScreenshotsFromDevice(type: Exec) { - group = 'Test' - description = 'Delete screenshots From Device' +task pullScreenshotsDeprecated(type: Exec) { + group = 'Test-Screenshots' + description = 'Pull screenshots From deprecated External Storage' - def packageName = propertyOrNull("screenshotsPackageName") ?: "$android.defaultConfig.applicationId" - def screenshotDirectory = propertyOrNull("screenshotsDirectory") ?: "test-screenshots" - def fullPath = "/sdcard/Android/data/$packageName/files/Pictures/$screenshotDirectory/" - def adb = propertyOrNull("adbPath") ?: findAdbFromLocal() + ignoreExitValue(true) + commandLine "$adb", 'pull', "$deprecatedFullPath", "$savePath/" +} - try { - commandLine "$adb", 'shell', 'rm', '-r', "$fullPath" - } catch (Throwable throwable) { - throwable.printStackTrace() - } +task pullScreenshots(dependsOn: [pullScreenshotsInternal, pullScreenshotsDeprecated]) { + group = 'Test-Screenshots' + description = 'Pull screenshots From Device' +} + +task removeScreenshotsFromDeviceInternal(type: Exec) { + group = 'Test-Screenshots' + description = 'Remove screenshots From internal Storage' + + ignoreExitValue(true) + commandLine "$adb", 'shell', 'rm', '-r', "$internalFullPath" +} + +task removeScreenshotsFromDeviceDeprecated(type: Exec) { + group = 'Test-Screenshots' + description = 'Remove screenshots From deprecated External Storage' + + ignoreExitValue(true) + commandLine "$adb", 'shell', 'rm', '-r', "$deprecatedFullPath" +} + +task removeScreenshotsFromDevice(dependsOn: [removeScreenshotsFromDeviceInternal, removeScreenshotsFromDeviceDeprecated]) { + group = 'Test-Screenshots' + description = 'Remove screenshots From Device' } task removeLocalScreenshots(type: Delete) { - def savePath = propertyOrNull("screenshotsSavePath") ?: "build/testscreenshots/" + group = 'Test-Screenshots' + description = 'Remove screenshots From Local Machine' delete files("$savePath") } diff --git a/test-util-android/src/main/java/org/fnives/test/showcase/android/testutil/screenshot/basicScreenCaptureProcessor.kt b/test-util-android/src/main/java/org/fnives/test/showcase/android/testutil/screenshot/basicScreenCaptureProcessor.kt index 982902c..be6375d 100644 --- a/test-util-android/src/main/java/org/fnives/test/showcase/android/testutil/screenshot/basicScreenCaptureProcessor.kt +++ b/test-util-android/src/main/java/org/fnives/test/showcase/android/testutil/screenshot/basicScreenCaptureProcessor.kt @@ -1,5 +1,6 @@ package org.fnives.test.showcase.android.testutil.screenshot +import android.os.Build import android.os.Environment import androidx.test.platform.app.InstrumentationRegistry import androidx.test.runner.screenshot.basicScreenCaptureProcessor @@ -8,5 +9,11 @@ import java.io.File fun basicScreenCaptureProcessor(subDir: String = "test-screenshots") = basicScreenCaptureProcessor(File(getTestPicturesDir(), subDir)) +@Suppress("DEPRECATION") fun getTestPicturesDir() = - InstrumentationRegistry.getInstrumentation().targetContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + InstrumentationRegistry.getInstrumentation().targetContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES) + } else { + val packageName = InstrumentationRegistry.getInstrumentation().targetContext.packageName + File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), packageName) + }