Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement patrolSetUp and patrolTearDown #1967

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/patrol/lib/src/binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ class PatrolBinding extends LiveTestWidgetsFlutterBinding {

setUp(() {
if (constants.hotRestartEnabled) {
// Sending results ends the test, which we don't want for Hot Restart
return;
}

if (global_state.currentTestIndividualName == 'patrol_test_explorer') {
return;
}

Expand All @@ -68,8 +71,7 @@ class PatrolBinding extends LiveTestWidgetsFlutterBinding {
}

final testName = global_state.currentTestIndividualName;
final isTestExplorer = testName == 'patrol_test_explorer';
if (isTestExplorer) {
if (testName == 'patrol_test_explorer') {
return;
} else {
logger(
Expand Down
57 changes: 33 additions & 24 deletions packages/patrol/lib/src/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,34 @@ import 'custom_finders/patrol_integration_tester.dart';
/// Signature for callback to [patrolTest].
typedef PatrolTesterCallback = Future<void> Function(PatrolIntegrationTester $);

/// A modification of [setUp] that works with Patrol's native automation.
void patrolSetUp(dynamic Function() body) {
setUp(() async {
final currentTest = global_state.currentTestFullName;

final requestedToExecute = await PatrolBinding.instance.patrolAppService
.waitForExecutionRequest(currentTest);

if (requestedToExecute) {
await body();
}
});
}

/// A modification of [tearDown] that works with Patrol's native automation.
void patrolTearDown(dynamic Function() body) {
tearDown(() async {
final currentTest = global_state.currentTestFullName;

final requestedToExecute = await PatrolBinding.instance.patrolAppService
.waitForExecutionRequest(currentTest);

if (requestedToExecute) {
await body();
}
});
}

/// Like [testWidgets], but with support for Patrol custom finders.
///
/// To customize the Patrol-specific configuration, set [config].
Expand Down Expand Up @@ -50,14 +78,9 @@ void patrolTest(
LiveTestWidgetsFlutterBindingFramePolicy framePolicy =
LiveTestWidgetsFlutterBindingFramePolicy.fadePointers,
}) {
NativeAutomator automator;

PatrolBinding? patrolBinding;

automator = NativeAutomator(config: nativeAutomatorConfig);
final binding =
patrolBinding = PatrolBinding.ensureInitialized(nativeAutomatorConfig)
..framePolicy = framePolicy;
final automator = NativeAutomator(config: nativeAutomatorConfig);
final patrolBinding = PatrolBinding.ensureInitialized(nativeAutomatorConfig)
..framePolicy = framePolicy;

testWidgets(
description,
Expand All @@ -67,23 +90,10 @@ void patrolTest(
variant: variant,
tags: tags,
(widgetTester) async {
if (patrolBinding != null && !constants.hotRestartEnabled) {
if (!constants.hotRestartEnabled) {
// If Patrol's native automation feature is enabled, then this test will
// be executed only if the native side requested it to be executed.
// Otherwise, it returns early.
//
// The assumption here is that this test doesn't have any extra parent
// groups. Every Dart test suite has an implict, unnamed, top-level
// group. An additional group is present in the bundled_test.dart, and
// its name is equal to the path to the Dart test file in the
// integration_test directory.
//
// In other words, the developer cannot use `group()` in the tests.
//
// Example: if this function is called from the Dart test file named
// "example_test.dart", and that file is located in the
// "integration_test/examples" directory, we assume that the name of the
// immediate parent group is "examples.example_test".

final requestedToExecute = await patrolBinding.patrolAppService
.waitForExecutionRequest(global_state.currentTestFullName);
Expand Down Expand Up @@ -114,7 +124,7 @@ void patrolTest(
final waitDuration = Duration(seconds: waitSeconds);

debugDefaultTargetPlatformOverride =
binding.workaroundDebugDefaultTargetPlatformOverride;
patrolBinding.workaroundDebugDefaultTargetPlatformOverride;

if (waitDuration > Duration.zero) {
final stopwatch = Stopwatch()..start();
Expand Down Expand Up @@ -175,7 +185,6 @@ DartGroupEntry createDartTestGroup(
);
} else if (entry is Test) {
if (entry.name == 'patrol_test_explorer') {
// throw StateError('Expected group, got test: ${entry.name}');
// Ignore the bogus test that is used to discover the test structure.
continue;
}
Expand Down
Loading