@@ -332,6 +332,14 @@ void main() async {
332332 testWidgets (
333333 '#durationEvent ${td .source }' ,
334334 (tester) async {
335+ // Wait for duration before event is emitted.
336+ final durationFuture = tester
337+ .getDurationFromEvent (
338+ playerId: playerId,
339+ platform: platform,
340+ )
341+ .timeout (_defaultTimeout);
342+
335343 await tester.prepareSource (
336344 playerId: playerId,
337345 platform: platform,
@@ -340,12 +348,7 @@ void main() async {
340348 );
341349
342350 expect (
343- await tester
344- .getDurationFromEvent (
345- playerId: playerId,
346- platform: platform,
347- )
348- .timeout (_defaultTimeout),
351+ await durationFuture,
349352 (Duration ? actual) => durationRangeMatcher (
350353 actual,
351354 td.duration,
@@ -471,6 +474,22 @@ extension on WidgetTester {
471474 required LibSourceTestData testData,
472475 bool waitForDurationEvent = true ,
473476 }) async {
477+ final Future <void >? durationFuture;
478+
479+ if (waitForDurationEvent &&
480+ testData.duration != null &&
481+ canDetermineDuration (testData)) {
482+ // Need to wait for the duration event,
483+ // otherwise it gets fired/received after the test has ended,
484+ // and therefore then ends up being received in the next test.
485+ durationFuture = getDurationFromEvent (
486+ playerId: playerId,
487+ platform: platform,
488+ );
489+ } else {
490+ durationFuture = null ;
491+ }
492+
474493 final eventStream = platform.getEventStream (playerId);
475494 final preparedFuture = eventStream
476495 .firstWhere (
@@ -499,16 +518,8 @@ extension on WidgetTester {
499518 // Wait simultaneously to ensure all errors are propagated through the same
500519 // future.
501520 await Future .wait ([setSourceFuture, preparedFuture]);
502- if (waitForDurationEvent &&
503- testData.duration != null &&
504- canDetermineDuration (testData)) {
505- // Need to wait for the duration event,
506- // otherwise it gets fired/received after the test has ended,
507- // and therefore then ends up being received in the next test.
508- await getDurationFromEvent (
509- playerId: playerId,
510- platform: platform,
511- );
521+ if (durationFuture != null ) {
522+ await durationFuture;
512523 }
513524 }
514525
0 commit comments