From a1c3490611ce9ac70dd869aa5f5a97aaeddeafac Mon Sep 17 00:00:00 2001 From: Mateusz Wojtczak Date: Sun, 17 Mar 2024 20:43:47 +0100 Subject: [PATCH 1/5] Pass app-server-port and test-server-port as Gradle project properties --- packages/patrol/android/build.gradle | 6 ++++++ .../leancode/patrol/PatrolAppServiceClient.kt | 18 ++++++++++++++++-- .../kotlin/pl/leancode/patrol/PatrolServer.kt | 8 +++----- .../lib/src/crossplatform/app_options.dart | 9 +++++++++ 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/packages/patrol/android/build.gradle b/packages/patrol/android/build.gradle index 8a38598e2..b966e6d97 100644 --- a/packages/patrol/android/build.gradle +++ b/packages/patrol/android/build.gradle @@ -52,6 +52,12 @@ android { defaultConfig { minSdk 21 + buildConfigField("String", "PATROL_APP_PORT", project.hasProperty('app-server-port') ? '"' + project.getProperty('app-server-port') + '"' : '""') + buildConfigField("String", "PATROL_TEST_PORT", project.hasProperty('test-server-port') ? '"' + project.getProperty('test-server-port') + '"' : '""') + } + + buildFeatures { + buildConfig true } dependencies { diff --git a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt index 7bdfa7bad..be71e0be7 100644 --- a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt +++ b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt @@ -16,13 +16,27 @@ class PatrolAppServiceClient { private val timeout = 2L private val timeUnit = TimeUnit.HOURS + private val defaultPort = 8082 + val port: Int + get() { + val portStr = BuildConfig.PATROL_APP_PORT + if (portStr == null) { + Logger.i("PATROL_APP_PORT is null, falling back to default ($defaultPort)") + return defaultPort + } + return portStr.toIntOrNull() ?: run { + Logger.i("PATROL_APP_PORT is not a valid integer, falling back to default ($defaultPort)") + defaultPort + } + } + constructor() { - client = Client(address = "localhost", port = 8082, timeout = timeout, timeUnit = timeUnit) + client = Client(address = "localhost", port = port, timeout = timeout, timeUnit = timeUnit) Logger.i("Created PatrolAppServiceClient: ${client.serverUrl}") } constructor(address: String) { - client = Client(address = address, port = 8082, timeout = timeout, timeUnit = timeUnit) + client = Client(address = address, port = port, timeout = timeout, timeUnit = timeUnit) Logger.i("Created PatrolAppServiceClient: ${client.serverUrl}") } diff --git a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolServer.kt b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolServer.kt index c97ed851f..86bbc470d 100644 --- a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolServer.kt +++ b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolServer.kt @@ -11,24 +11,22 @@ import org.http4k.server.asServer class PatrolServer { private val defaultPort = 8081 - private val envPortKey = "PATROL_PORT" private var server: Http4kServer? = null private var automatorServer: AutomatorServer? = null val port: Int get() { - val portStr = arguments.getString(envPortKey) + val portStr = BuildConfig.PATROL_TEST_PORT if (portStr == null) { - Logger.i("$envPortKey is null, falling back to default ($defaultPort)") + Logger.i("PATROL_TEST_PORT is null, falling back to default ($defaultPort)") return defaultPort } return portStr.toIntOrNull() ?: run { - Logger.i("$envPortKey is not a valid integer, falling back to default ($defaultPort)") + Logger.i("PATROL_TEST_PORT is not a valid integer, falling back to default ($defaultPort)") defaultPort } } - private val arguments get() = InstrumentationRegistry.getArguments() fun start() { Logger.i("Starting server...") diff --git a/packages/patrol_cli/lib/src/crossplatform/app_options.dart b/packages/patrol_cli/lib/src/crossplatform/app_options.dart index 998f73002..a24376ea8 100644 --- a/packages/patrol_cli/lib/src/crossplatform/app_options.dart +++ b/packages/patrol_cli/lib/src/crossplatform/app_options.dart @@ -41,10 +41,14 @@ class AndroidAppOptions { const AndroidAppOptions({ required this.flutter, this.packageName, + required this.appServerPort, + required this.testServerPort, }); final FlutterAppOptions flutter; final String? packageName; + final int appServerPort; + final int testServerPort; String get description => 'apk with entrypoint ${basename(flutter.target)}'; @@ -116,6 +120,11 @@ class AndroidAppOptions { cmd.add('-Pdart-defines=$dartDefinesString'); } + // Add app and test server ports + cmd + ..add('-Papp-server-port=$appServerPort') + ..add('-Ptest-server-port=$testServerPort'); + return cmd; } } From 718e7ac381d19dba589531cd57a16a4ca66f4034 Mon Sep 17 00:00:00 2001 From: Mateusz Wojtczak Date: Sun, 17 Mar 2024 20:45:14 +0100 Subject: [PATCH 2/5] Use port options in patrol_cli commands for all platforms --- .../lib/src/commands/build_android.dart | 3 ++ .../lib/src/commands/build_ios.dart | 1 + .../lib/src/commands/build_macos.dart | 1 + .../patrol_cli/lib/src/commands/develop.dart | 3 ++ .../patrol_cli/lib/src/commands/test.dart | 3 ++ .../lib/src/runner/patrol_command.dart | 30 +++++++++---------- 6 files changed, 25 insertions(+), 16 deletions(-) diff --git a/packages/patrol_cli/lib/src/commands/build_android.dart b/packages/patrol_cli/lib/src/commands/build_android.dart index 3f8ce1a2f..7f48d97cf 100644 --- a/packages/patrol_cli/lib/src/commands/build_android.dart +++ b/packages/patrol_cli/lib/src/commands/build_android.dart @@ -33,6 +33,7 @@ class BuildAndroidCommand extends PatrolCommand { usesDartDefineOption(); usesLabelOption(); usesWaitOption(); + usesPortOptions(); usesAndroidOptions(); } @@ -124,6 +125,8 @@ class BuildAndroidCommand extends PatrolCommand { final androidOpts = AndroidAppOptions( flutter: flutterOpts, packageName: packageName, + appServerPort: super.appServerPort, + testServerPort: super.testServerPort, ); try { diff --git a/packages/patrol_cli/lib/src/commands/build_ios.dart b/packages/patrol_cli/lib/src/commands/build_ios.dart index f984dbf72..7b896438a 100644 --- a/packages/patrol_cli/lib/src/commands/build_ios.dart +++ b/packages/patrol_cli/lib/src/commands/build_ios.dart @@ -34,6 +34,7 @@ class BuildIOSCommand extends PatrolCommand { usesDartDefineOption(); usesLabelOption(); usesWaitOption(); + usesPortOptions(); usesIOSOptions(); argParser.addFlag( diff --git a/packages/patrol_cli/lib/src/commands/build_macos.dart b/packages/patrol_cli/lib/src/commands/build_macos.dart index 892e01d20..2ff8b7a5b 100644 --- a/packages/patrol_cli/lib/src/commands/build_macos.dart +++ b/packages/patrol_cli/lib/src/commands/build_macos.dart @@ -34,6 +34,7 @@ class BuildMacOSCommand extends PatrolCommand { usesDartDefineOption(); usesLabelOption(); usesWaitOption(); + usesPortOptions(); usesMacOSOptions(); } diff --git a/packages/patrol_cli/lib/src/commands/develop.dart b/packages/patrol_cli/lib/src/commands/develop.dart index f96d29756..9b9f862ee 100644 --- a/packages/patrol_cli/lib/src/commands/develop.dart +++ b/packages/patrol_cli/lib/src/commands/develop.dart @@ -50,6 +50,7 @@ class DevelopCommand extends PatrolCommand { usesDartDefineOption(); usesLabelOption(); usesWaitOption(); + usesPortOptions(); usesUninstallOption(); @@ -187,6 +188,8 @@ class DevelopCommand extends PatrolCommand { final androidOpts = AndroidAppOptions( flutter: flutterOpts, packageName: packageName, + appServerPort: super.appServerPort, + testServerPort: super.testServerPort, ); final iosOpts = IOSAppOptions( diff --git a/packages/patrol_cli/lib/src/commands/test.dart b/packages/patrol_cli/lib/src/commands/test.dart index 0c18dfd4c..4eae92dc8 100644 --- a/packages/patrol_cli/lib/src/commands/test.dart +++ b/packages/patrol_cli/lib/src/commands/test.dart @@ -46,6 +46,7 @@ class TestCommand extends PatrolCommand { usesDartDefineOption(); usesLabelOption(); usesWaitOption(); + usesPortOptions(); usesUninstallOption(); @@ -178,6 +179,8 @@ See https://github.com/leancodepl/patrol/issues/1316 to learn more. final androidOpts = AndroidAppOptions( flutter: flutterOpts, packageName: packageName, + appServerPort: super.appServerPort, + testServerPort: super.testServerPort, ); final iosOpts = IOSAppOptions( diff --git a/packages/patrol_cli/lib/src/runner/patrol_command.dart b/packages/patrol_cli/lib/src/runner/patrol_command.dart index 6bbcce237..688687567 100644 --- a/packages/patrol_cli/lib/src/runner/patrol_command.dart +++ b/packages/patrol_cli/lib/src/runner/patrol_command.dart @@ -104,6 +104,20 @@ abstract class PatrolCommand extends Command { ); } + void usesPortOptions() { + argParser + ..addOption( + 'test-server-port', + help: 'Port to use for server running in the test instrumentation app.', + defaultsTo: _defaultTestServerPort.toString(), + ) + ..addOption( + 'app-server-port', + help: 'Port to use for server running in the app under test.', + defaultsTo: _defaultAppServerPort.toString(), + ); + } + void usesAndroidOptions() { argParser.addOption( 'package-name', @@ -118,7 +132,6 @@ abstract class PatrolCommand extends Command { help: 'Bundle identifier of the iOS app under test.', valueHelp: 'pl.leancode.AwesomeApp', ); - _usesIOSPortOptions(); } void usesMacOSOptions() { @@ -127,21 +140,6 @@ abstract class PatrolCommand extends Command { help: 'Bundle identifier of the MacOS app under test.', valueHelp: 'pl.leancode.macos.AwesomeApp', ); - _usesIOSPortOptions(); - } - - void _usesIOSPortOptions() { - argParser - ..addOption( - 'test-server-port', - help: 'Port to use for server running in the test instrumentation app.', - defaultsTo: _defaultTestServerPort.toString(), - ) - ..addOption( - 'app-server-port', - help: 'Port to use for server running in the app under test.', - defaultsTo: _defaultAppServerPort.toString(), - ); } // Runtime-only options From 9b869a108f25dc28ab902295d50a8abfc10b7dae Mon Sep 17 00:00:00 2001 From: Mateusz Wojtczak Date: Sun, 17 Mar 2024 20:51:30 +0100 Subject: [PATCH 3/5] Fix app_options_test --- .../test/crossplatform/app_options_test.dart | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/patrol_cli/test/crossplatform/app_options_test.dart b/packages/patrol_cli/test/crossplatform/app_options_test.dart index 8c886f05c..5a09cbd35 100644 --- a/packages/patrol_cli/test/crossplatform/app_options_test.dart +++ b/packages/patrol_cli/test/crossplatform/app_options_test.dart @@ -16,7 +16,11 @@ void main() { flavor: null, dartDefines: {}, ); - options = const AndroidAppOptions(flutter: flutterOptions); + options = const AndroidAppOptions( + flutter: flutterOptions, + appServerPort: 1, + testServerPort: 2, + ); final invocation = options.toGradleAssembleTestInvocation(isWindows: true); @@ -26,6 +30,8 @@ void main() { r'.\gradlew.bat', ':app:assembleDebugAndroidTest', r'-Ptarget=C:\Users\john\app\integration_test\app_test.dart', + '-Papp-server-port=1', + '-Ptest-server-port=2', ]), ); }); @@ -37,7 +43,11 @@ void main() { flavor: null, dartDefines: {}, ); - options = const AndroidAppOptions(flutter: flutterOpts); + options = const AndroidAppOptions( + flutter: flutterOpts, + appServerPort: 1, + testServerPort: 2, + ); final invocation = options.toGradleAssembleTestInvocation(isWindows: false); @@ -47,6 +57,8 @@ void main() { './gradlew', ':app:assembleReleaseAndroidTest', '-Ptarget=/Users/john/app/integration_test/app_test.dart', + '-Papp-server-port=1', + '-Ptest-server-port=2', ]), ); }); @@ -66,7 +78,11 @@ void main() { flavor: 'dev', dartDefines: dartDefines, ); - options = const AndroidAppOptions(flutter: flutterOpts); + options = const AndroidAppOptions( + flutter: flutterOpts, + appServerPort: 1, + testServerPort: 2, + ); final invocation = options.toGradleAssembleTestInvocation(isWindows: true); @@ -77,6 +93,8 @@ void main() { ':app:assembleDevReleaseAndroidTest', r'-Ptarget=C:\Users\john\app\integration_test\app_test.dart', '-Pdart-defines=RU1BSUw9dXNlckBleGFtcGxlLmNvbQ==,UEFTU1dPUkQ9bnk0bmNhdA==,Zm9vPWJhcg==', + '-Papp-server-port=1', + '-Ptest-server-port=2', ]), ); }); @@ -88,7 +106,11 @@ void main() { flavor: 'dev', dartDefines: dartDefines, ); - options = const AndroidAppOptions(flutter: flutterOpts); + options = const AndroidAppOptions( + flutter: flutterOpts, + appServerPort: 1, + testServerPort: 2, + ); final invocation = options.toGradleAssembleTestInvocation(isWindows: false); @@ -99,6 +121,8 @@ void main() { ':app:assembleDevDebugAndroidTest', '-Ptarget=/Users/john/app/integration_test/app_test.dart', '-Pdart-defines=RU1BSUw9dXNlckBleGFtcGxlLmNvbQ==,UEFTU1dPUkQ9bnk0bmNhdA==,Zm9vPWJhcg==', + '-Papp-server-port=1', + '-Ptest-server-port=2', ]), ); }); From fdb901166ec05764d8e2258645c2b534108442eb Mon Sep 17 00:00:00 2001 From: Mateusz Wojtczak Date: Sun, 17 Mar 2024 22:18:23 +0100 Subject: [PATCH 4/5] Fix KtLint --- .../android/src/main/kotlin/pl/leancode/patrol/PatrolServer.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolServer.kt b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolServer.kt index 86bbc470d..73288bcf5 100644 --- a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolServer.kt +++ b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolServer.kt @@ -1,7 +1,6 @@ package pl.leancode.patrol import android.os.ConditionVariable -import androidx.test.platform.app.InstrumentationRegistry import org.http4k.core.ContentType import org.http4k.filter.ServerFilters import org.http4k.server.Http4kServer @@ -27,7 +26,6 @@ class PatrolServer { } } - fun start() { Logger.i("Starting server...") From 0e0557afc4669aa686d4ed8e7c1b79187cb2efa4 Mon Sep 17 00:00:00 2001 From: Mateusz Wojtczak Date: Mon, 18 Mar 2024 08:49:19 +0100 Subject: [PATCH 5/5] Update changelogs --- packages/patrol/CHANGELOG.md | 4 ++++ packages/patrol_cli/CHANGELOG.md | 3 +++ 2 files changed, 7 insertions(+) diff --git a/packages/patrol/CHANGELOG.md b/packages/patrol/CHANGELOG.md index eab8e8fe5..5246bd56a 100644 --- a/packages/patrol/CHANGELOG.md +++ b/packages/patrol/CHANGELOG.md @@ -1,3 +1,7 @@ +## 3.5.3 + +- Add support for --app-server-port and --test-server-port on Android (#2154). + ## 3.5.2 - Fix reporting fail cause to native on ios in release mode (#2114). diff --git a/packages/patrol_cli/CHANGELOG.md b/packages/patrol_cli/CHANGELOG.md index 9bac1f17e..50629613d 100644 --- a/packages/patrol_cli/CHANGELOG.md +++ b/packages/patrol_cli/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2.6.5 +- Add support for --app-server-port and --test-server-port on Android (#2154). + ## 2.6.4 - Fix compatibility_checker getting stuck (#2091).