Skip to content

Commit

Permalink
WIP (try out currentGroupFullName approach)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekpacia committed Oct 2, 2023
1 parent 0e9b69d commit 40bd9a4
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:flutter/material.dart';
import 'package:patrol/src/extensions.dart';
// ignore: depend_on_referenced_packages
import 'package:test_api/src/backend/invoker.dart';

import '../common.dart';

String get currentTest => Invoker.current!.fullCurrentTestName();

void _print(String text) => print('PATROL_DEBUG: $text');

void main() {
patrolSetUpAll(() async {
await Future<void>.delayed(Duration(seconds: 1));
_print('setting up all before $currentTest');
});

patrolTest('testA', nativeAutomation: true, _body);
patrolTest('testB', nativeAutomation: true, _body);
patrolTest('testC', nativeAutomation: true, _body);
}

Future<void> _body(PatrolTester $) async {
final testName = Invoker.current!.fullCurrentTestName();
_print('test body: name=$testName');

await createApp($);

await $(FloatingActionButton).tap();
expect($(#counterText).text, '1');

await $(#textField).enterText(testName);

await $.pumpAndSettle(duration: Duration(seconds: 2));
}
45 changes: 45 additions & 0 deletions packages/patrol/example/test/callbacks_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:patrol/src/extensions.dart';
// ignore: depend_on_referenced_packages
import 'package:test_api/src/backend/invoker.dart';

const String requestedTest = 'testA';

String get currentTest => Invoker.current!.fullCurrentTestName();

void main() {
group('alpha', () {
setUpAll(() {
final groupName = Invoker.current!.liveTest.groups.last.name;
final individualName = Invoker.current!.liveTest.individualName;
print(
'setUpAll: parentGroupName=$groupName, individualName=$individualName',
);
});

setUp(() {
final groupName = Invoker.current!.liveTest.groups.last.name;
final individualName = Invoker.current!.liveTest.individualName;
print(
'setUpAll: parentGroupName=$groupName, individualName=$individualName',
);
});

patrolTest('testA', _body);
patrolTest('testB', _body);
patrolTest('testC', _body);
});
}

Future<void> _body() async => print(Invoker.current!.fullCurrentTestName());

void patrolTest(String name, Future<void> Function() body) {
test(name, () async {
final currentTest = Invoker.current!.fullCurrentTestName();

if (currentTest == requestedTest) {
print('Requested test $currentTest, will execute it');
await body();
}
});
}
19 changes: 19 additions & 0 deletions packages/patrol/lib/src/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ void patrolTearDown(Future<void> Function() body) {
});
}

/// A modification of [setUpAll] that works with Patrol's native automation.
void patrolSetUpAll(Future<void> Function() body) {
setUpAll(() async {
final currentTest = global_state.currentTestFullName;

final parentGroups = global_state.currentGroupFullName;

final patrolAppService = PatrolBinding.instance.patrolAppService;
final currentSetUpAllIndex = patrolAppService.setUpAllCount += 1;

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
4 changes: 4 additions & 0 deletions packages/patrol/lib/src/global_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ String get currentTestFullName {
return nameCandidate;
}

String get currentGroupFullName {
return Invoker.current!.liveTest.groups.last.name;
}

/// Returns the individual name of the current test. Omits all ancestor groups.
String get currentTestIndividualName {
return Invoker.current!.liveTest.individualName;
Expand Down
11 changes: 11 additions & 0 deletions packages/patrol/lib/src/native/patrol_app_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import 'package:patrol/src/native/contracts/contracts.dart';
import 'package:patrol/src/native/contracts/patrol_app_service_server.dart';
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as shelf_io;
// ignore: implementation_imports
import 'package:test_api/src/backend/live_test.dart';

const _port = 8082;
const _idleTimeout = Duration(hours: 2);
Expand Down Expand Up @@ -55,6 +57,13 @@ class PatrolAppService extends PatrolAppServiceServer {
/// bundled Dart test file.
final DartGroupEntry topLevelDartTestGroup;

/// The number of all setUpAll callbacks.
///
/// setUpAlls, unlike setUps, aren't executed in the [LiveTest] context.
/// Because of this, we can't depend on the [LiveTest]'s name, so we identify
/// them by indexes instead.
int setUpAllCount = 0;

/// A completer that completes with the name of the Dart test file that was
/// requested to execute by the native side.
final _testExecutionRequested = Completer<String>();
Expand Down Expand Up @@ -136,6 +145,8 @@ class PatrolAppService extends PatrolAppServiceServer {
return ListDartTestsResponse(group: topLevelDartTestGroup);
}



@override
Future<RunDartTestResponse> runDartTest(RunDartTestRequest request) async {
assert(_testExecutionCompleted.isCompleted == false);
Expand Down

0 comments on commit 40bd9a4

Please sign in to comment.