@@ -2,13 +2,10 @@ package com.telefonica.androidsnaptesting
22
33import com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask
44import com.android.build.gradle.internal.testing.ConnectedDevice
5- import com.android.ddmlib.CollectingOutputReceiver
6- import com.android.ddmlib.FileListingService
7- import com.android.ddmlib.FileListingService.FileEntry
8- import com.android.ddmlib.IDevice
95import org.gradle.api.file.RegularFile
106import org.gradle.api.provider.ProviderFactory
117import java.io.File
8+ import java.util.concurrent.TimeUnit
129
1310fun DeviceProviderInstrumentTestTask.deviceFileManager (
1411 applicationId : String ,
@@ -23,39 +20,25 @@ class DeviceFileManager(
2320 private val providerFactory : ProviderFactory ,
2421) {
2522
26- fun pullRecordedSnapshots (
27- destinationPath : String ,
28- ) {
23+ fun pullRecordedSnapshots (destinationPath : String ) {
2924 pullSnapshots(" recorded" , destinationPath)
3025 }
3126
32- fun pullFailuresSnapshots (
33- destinationPath : String ,
34- ) {
27+ fun pullFailuresSnapshots (destinationPath : String ) {
3528 pullSnapshots(" failures" , destinationPath)
3629 }
3730
3831 fun clearAllSnapshots () {
3932 withConnectedDevices { devices ->
40- devices.forEach {
41- val receiver = CollectingOutputReceiver ()
42- it.iDevice.executeShellCommand(" rm -rf ${getDeviceAndroidSnaptestingRootAbsolutePath()} " , receiver)
43- println (receiver.output)
33+ devices.forEach { device ->
34+ runAdb(device.serialNumber, " shell" , " rm" , " -rf" , getDeviceAndroidSnaptestingRootAbsolutePath())
4435 }
4536 }
4637 }
4738
48- private fun String.toFileEntry (): FileEntry {
49- val parts = this .split(" /" )
50- var fileEntry = FileEntry (null , null , FileListingService .TYPE_DIRECTORY , true )
51- parts.forEach {
52- fileEntry = FileEntry (fileEntry, it, FileListingService .TYPE_DIRECTORY , false )
53- }
54- return fileEntry
55- }
56-
5739 private fun getDeviceAndroidSnaptestingRootAbsolutePath (): String =
58- " ${FileListingService .DIRECTORY_SDCARD } /Download/android-snaptesting/$applicationId "
40+ " /sdcard/Download/android-snaptesting/$applicationId "
41+
5942 private fun getDeviceAndroidSnaptestingSubfolderAbsolutePath (subFolder : String ): String =
6043 " ${getDeviceAndroidSnaptestingRootAbsolutePath()} /$subFolder "
6144
@@ -77,25 +60,61 @@ class DeviceFileManager(
7760 androidSnaptestingSubFolderInDevice : String ,
7861 destinationPath : String ,
7962 ) {
80- val fileEntry = getDeviceAndroidSnaptestingSubfolderAbsolutePath(androidSnaptestingSubFolderInDevice).toFileEntry( )
63+ val remotePath = getDeviceAndroidSnaptestingSubfolderAbsolutePath(androidSnaptestingSubFolderInDevice)
8164 withConnectedDevices { devices ->
82- devices.forEach {
83- pullFolderFiles(
84- fileEntry,
85- it.iDevice,
86- destinationPath,
87- )
65+ devices.forEach { device ->
66+ val serial = device.serialNumber
67+ val lsResult = runAdbCapture(serial, " shell" , " ls" , remotePath, logErrors = false )
68+ val fileNames = lsResult.output.lines()
69+ .map { it.trim() }
70+ .filter { it.isNotBlank() && ! it.startsWith(" ls:" ) && ! it.contains(" No such file" ) }
71+ fileNames.forEach { fileName ->
72+ runAdb(serial, " pull" , " $remotePath /$fileName " , " $destinationPath /$fileName " )
73+ }
8874 }
8975 }
9076 }
9177
92- private fun pullFolderFiles (
93- androidSnaptestingDeviceFolder : FileEntry ,
94- device : IDevice ,
95- destinationPath : String ,
96- ) {
97- device.fileListingService.getChildrenSync(androidSnaptestingDeviceFolder).forEach {
98- device.pullFile(it.fullPath, " $destinationPath /${it.name} " )
78+ private fun runAdb (serial : String , vararg args : String ) {
79+ val result = runAdbCapture(serial, * args, throwOnError = true )
80+ println (result.output)
81+ }
82+
83+ private fun runAdbCapture (
84+ serial : String ,
85+ vararg args : String ,
86+ throwOnError : Boolean = false,
87+ logErrors : Boolean = true,
88+ ): AdbResult {
89+ val command = buildList {
90+ add(adbExecutablePath)
91+ add(" -s" )
92+ add(serial)
93+ addAll(args.toList())
9994 }
95+ val result = try {
96+ val process = ProcessBuilder (command)
97+ .redirectErrorStream(false )
98+ .start()
99+ val output = process.inputStream.bufferedReader().readText()
100+ val error = process.errorStream.bufferedReader().readText()
101+ val finished = process.waitFor(60 , TimeUnit .SECONDS )
102+ val exitCode = process.exitValue()
103+ AdbResult (output, error, exitCode, finished)
104+ } catch (e: Exception ) {
105+ val message = " Exception running ADB command: ${command.joinToString(" " )} \n ${e.message} "
106+ if (throwOnError) throw RuntimeException (message, e)
107+ else if (logErrors) println (message)
108+ return AdbResult (" " , e.message ? : " " , - 1 )
109+ }
110+
111+ if (! result.finished || result.exitCode != 0 ) {
112+ val message = " ADB command failed: ${command.joinToString(" " )} \n Exit code: ${result.exitCode} \n Output: ${result.output} \n Error: ${result.error} "
113+ if (throwOnError) throw RuntimeException (message)
114+ else if (logErrors) println (message)
115+ }
116+ return result
100117 }
118+
119+ private data class AdbResult (val output : String , val error : String , val exitCode : Int , val finished : Boolean = true )
101120}
0 commit comments