Skip to content

Commit

Permalink
Merge pull request #2154 from leancodepl/fix/android-server-ports
Browse files Browse the repository at this point in the history
Support app-server-port and test-server-port options on Android
  • Loading branch information
piotruela authored Mar 18, 2024
2 parents 9eaadd8 + b742b63 commit 20c1e88
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 30 deletions.
3 changes: 2 additions & 1 deletion packages/patrol/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 3.6.0

- Added the NativeAutomator2, which allows to interact with native using selectors designated for a specific platform (AndroidSelector and IOSSelector).
- Added the NativeAutomator2, which allows to interact with native using selectors designated for a specific platform (AndroidSelector and IOSSelector) (#2132).
- Add support for --app-server-port and --test-server-port on Android (#2154).

## 3.5.2

Expand Down
6 changes: 6 additions & 0 deletions packages/patrol/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,25 +10,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...")

Expand Down
3 changes: 3 additions & 0 deletions packages/patrol_cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
3 changes: 3 additions & 0 deletions packages/patrol_cli/lib/src/commands/build_android.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class BuildAndroidCommand extends PatrolCommand {
usesDartDefineOption();
usesLabelOption();
usesWaitOption();
usesPortOptions();

usesAndroidOptions();
}
Expand Down Expand Up @@ -124,6 +125,8 @@ class BuildAndroidCommand extends PatrolCommand {
final androidOpts = AndroidAppOptions(
flutter: flutterOpts,
packageName: packageName,
appServerPort: super.appServerPort,
testServerPort: super.testServerPort,
);

try {
Expand Down
1 change: 1 addition & 0 deletions packages/patrol_cli/lib/src/commands/build_ios.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class BuildIOSCommand extends PatrolCommand {
usesDartDefineOption();
usesLabelOption();
usesWaitOption();
usesPortOptions();

usesIOSOptions();
argParser.addFlag(
Expand Down
1 change: 1 addition & 0 deletions packages/patrol_cli/lib/src/commands/build_macos.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class BuildMacOSCommand extends PatrolCommand {
usesDartDefineOption();
usesLabelOption();
usesWaitOption();
usesPortOptions();

usesMacOSOptions();
}
Expand Down
3 changes: 3 additions & 0 deletions packages/patrol_cli/lib/src/commands/develop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class DevelopCommand extends PatrolCommand {
usesDartDefineOption();
usesLabelOption();
usesWaitOption();
usesPortOptions();

usesUninstallOption();

Expand Down Expand Up @@ -187,6 +188,8 @@ class DevelopCommand extends PatrolCommand {
final androidOpts = AndroidAppOptions(
flutter: flutterOpts,
packageName: packageName,
appServerPort: super.appServerPort,
testServerPort: super.testServerPort,
);

final iosOpts = IOSAppOptions(
Expand Down
3 changes: 3 additions & 0 deletions packages/patrol_cli/lib/src/commands/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class TestCommand extends PatrolCommand {
usesDartDefineOption();
usesLabelOption();
usesWaitOption();
usesPortOptions();

usesUninstallOption();

Expand Down Expand Up @@ -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(
Expand Down
9 changes: 9 additions & 0 deletions packages/patrol_cli/lib/src/crossplatform/app_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)}';

Expand Down Expand Up @@ -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;
}
}
Expand Down
30 changes: 14 additions & 16 deletions packages/patrol_cli/lib/src/runner/patrol_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ abstract class PatrolCommand extends Command<int> {
);
}

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',
Expand All @@ -118,7 +132,6 @@ abstract class PatrolCommand extends Command<int> {
help: 'Bundle identifier of the iOS app under test.',
valueHelp: 'pl.leancode.AwesomeApp',
);
_usesIOSPortOptions();
}

void usesMacOSOptions() {
Expand All @@ -127,21 +140,6 @@ abstract class PatrolCommand extends Command<int> {
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
Expand Down
32 changes: 28 additions & 4 deletions packages/patrol_cli/test/crossplatform/app_options_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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',
]),
);
});
Expand All @@ -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);
Expand All @@ -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',
]),
);
});
Expand All @@ -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);
Expand All @@ -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',
]),
);
});
Expand All @@ -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);
Expand All @@ -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',
]),
);
});
Expand Down

0 comments on commit 20c1e88

Please sign in to comment.