@@ -57,20 +57,17 @@ void runTests({
5757 await recompile (hasEdits: true );
5858 }
5959
60- /// Wait for main to finish executing before checking expectations by checking
61- /// for a log output.
62- ///
63- /// If [debuggingEnabled] is false, we can't check for Chrome logs and instead
64- /// wait 1 second.
65- // TODO(srujzs): We should do something less prone to race conditions when
66- // debugging is disabled.
67- Future <void > waitForMainToExecute ({bool debuggingEnabled = true }) async {
68- if (! debuggingEnabled) return Future .delayed (const Duration (seconds: 1 ));
60+ /// Wait for `expectedStrings` to be printed in the console.
61+ Future <void > expectLogs (List <String > expectedStrings) async {
62+ final expectations = List <String >.from (expectedStrings);
6963 final completer = Completer <void >();
70- final expectedString = 'main executed' ;
7164 final subscription = context.webkitDebugger.onConsoleAPICalled.listen ((e) {
72- if (e.args.first.value == expectedString) {
73- completer.complete ();
65+ final value = e.args.first.value;
66+ if (expectations.contains (value)) {
67+ expectations.remove (value);
68+ if (expectations.isEmpty) {
69+ completer.complete ();
70+ }
7471 }
7572 });
7673 await completer.future;
@@ -98,14 +95,10 @@ void runTests({
9895 });
9996
10097 test ('can live reload changes ' , () async {
101- final mainDone = waitForMainToExecute ();
102- await makeEditAndRecompile ();
103- await mainDone;
104- final source = await context.webDriver.pageSource;
105-
10698 // A full reload should clear the state.
107- expect (source.contains (originalString), isFalse);
108- expect (source.contains (newString), isTrue);
99+ final logExpectation = expectLogs ([newString]);
100+ await makeEditAndRecompile ();
101+ await logExpectation;
109102 });
110103 });
111104
@@ -130,14 +123,10 @@ void runTests({
130123 });
131124
132125 test ('can live reload changes ' , () async {
133- final mainDone = waitForMainToExecute (debuggingEnabled: false );
134- await makeEditAndRecompile ();
135- await mainDone;
136- final source = await context.webDriver.pageSource;
137-
138126 // A full reload should clear the state.
139- expect (source.contains (originalString), isFalse);
140- expect (source.contains (newString), isTrue);
127+ final logExpectation = expectLogs ([newString]);
128+ await makeEditAndRecompile ();
129+ await logExpectation;
141130 });
142131 });
143132
@@ -163,14 +152,10 @@ void runTests({
163152 });
164153
165154 test ('can live reload changes ' , () async {
166- final mainDone = waitForMainToExecute (debuggingEnabled: false );
167- await makeEditAndRecompile ();
168- await mainDone;
169- final source = await context.webDriver.pageSource;
170-
171155 // A full reload should clear the state.
172- expect (source.contains (originalString), isFalse);
173- expect (source.contains (newString), isTrue);
156+ final logExpectation = expectLogs ([newString]);
157+ await makeEditAndRecompile ();
158+ await logExpectation;
174159 });
175160 });
176161 },
@@ -306,20 +291,16 @@ void runTests({
306291 ]),
307292 ),
308293 );
309- final mainDone = waitForMainToExecute ();
294+ // Main is re-invoked which shouldn't clear the state.
295+ final logExpectation = expectLogs (['$originalString $newString ' ]);
310296 final hotRestart = context.getRegisteredServiceExtension ('hotRestart' );
311297 expect (
312298 await fakeClient.callServiceExtension (hotRestart! ),
313299 const TypeMatcher <Success >(),
314300 );
315301
316302 await eventsDone;
317- await mainDone;
318-
319- final source = await context.webDriver.pageSource;
320- // Main is re-invoked which shouldn't clear the state.
321- expect (source, contains (originalString));
322- expect (source, contains (newString));
303+ await logExpectation;
323304 });
324305
325306 test ('can send events before and after hot restart' , () async {
@@ -352,7 +333,8 @@ void runTests({
352333 );
353334
354335 await recompile ();
355- final mainDone = waitForMainToExecute ();
336+ // Main is re-invoked which shouldn't clear the state.
337+ final logExpectation = expectLogs (['$originalString $originalString ' ]);
356338 final hotRestart = context.getRegisteredServiceExtension ('hotRestart' );
357339 expect (
358340 await fakeClient.callServiceExtension (hotRestart! ),
@@ -371,11 +353,7 @@ void runTests({
371353 );
372354
373355 await eventsDone;
374- await mainDone;
375-
376- final source = await context.webDriver.pageSource;
377- // Main is re-invoked which shouldn't clear the state.
378- expect (source, contains ('Hello World!' ));
356+ await logExpectation;
379357 });
380358
381359 test ('can refresh the page via the fullReload service extension' , () async {
@@ -393,20 +371,16 @@ void runTests({
393371 ]),
394372 ),
395373 );
396- final mainDone = waitForMainToExecute ();
374+ // Should see only the new text.
375+ final logExpectation = expectLogs ([newString]);
397376 final fullReload = context.getRegisteredServiceExtension ('fullReload' );
398377 expect (
399378 await fakeClient.callServiceExtension (fullReload! ),
400379 isA <Success >(),
401380 );
402381
403382 await eventsDone;
404- await mainDone;
405-
406- final source = await context.webDriver.pageSource;
407- // Should see only the new text
408- expect (source.contains (originalString), isFalse);
409- expect (source.contains (newString), isTrue);
383+ await logExpectation;
410384 });
411385
412386 test ('can hot restart while paused' , () async {
@@ -430,17 +404,11 @@ void runTests({
430404 );
431405
432406 await makeEditAndRecompile ();
433- final mainDone = waitForMainToExecute ();
407+ // Main is re-invoked which shouldn't clear the state.
408+ final logExpectation = expectLogs (['$originalString $newString ' ]);
434409 final hotRestart = context.getRegisteredServiceExtension ('hotRestart' );
435410 await fakeClient.callServiceExtension (hotRestart! );
436-
437- await mainDone;
438-
439- final source = await context.webDriver.pageSource;
440-
441- // Main is re-invoked which shouldn't clear the state.
442- expect (source.contains (originalString), isTrue);
443- expect (source.contains (newString), isTrue);
411+ await logExpectation;
444412
445413 vm = await client.getVM ();
446414 isolateId = vm.isolates! .first.id! ;
@@ -477,37 +445,27 @@ void runTests({
477445 test ('can hot restart with no changes, hot restart with changes, and '
478446 'hot restart again with no changes' , () async {
479447 // Empty hot restart.
480- var mainDone = waitForMainToExecute ( );
448+ var logExpectation = expectLogs ([ '$ originalString $ originalString ' ] );
481449 await recompile ();
482450 final hotRestart = context.getRegisteredServiceExtension ('hotRestart' );
483451 await fakeClient.callServiceExtension (hotRestart! );
484-
485- await mainDone;
486- var source = await context.webDriver.pageSource;
487- expect (source.contains (originalString), isTrue);
488- expect (source.contains (newString), isFalse);
452+ await logExpectation;
489453
490454 // Hot restart.
491- mainDone = waitForMainToExecute ();
455+ logExpectation = expectLogs ([
456+ '$originalString $originalString $newString ' ,
457+ ]);
492458 await makeEditAndRecompile ();
493459 await fakeClient.callServiceExtension (hotRestart);
494-
495- await mainDone;
496- source = await context.webDriver.pageSource;
497- // Main is re-invoked which shouldn't clear the state.
498- expect (source.contains (originalString), isTrue);
499- expect (source.contains (newString), isTrue);
460+ await logExpectation;
500461
501462 // Empty hot restart.
502- mainDone = waitForMainToExecute ();
463+ logExpectation = expectLogs ([
464+ '$originalString $originalString $newString $newString ' ,
465+ ]);
503466 await recompile ();
504467 await fakeClient.callServiceExtension (hotRestart);
505-
506- await mainDone;
507- source = await context.webDriver.pageSource;
508- expect (source.contains (originalString), isTrue);
509- // `newString` should now exist twice in the source.
510- expect (source.contains (RegExp ('$newString .*$newString ' )), isTrue);
468+ await logExpectation;
511469 });
512470 }, timeout: Timeout .factor (2 ));
513471
@@ -532,19 +490,15 @@ void runTests({
532490 });
533491
534492 test ('can hot restart changes ' , () async {
535- final mainDone = waitForMainToExecute ();
536- await makeEditAndRecompile ();
537- await mainDone;
538- final source = await context.webDriver.pageSource;
539-
540493 // Main is re-invoked which shouldn't clear the state.
541- expect (source.contains (originalString), isTrue);
542- expect (source.contains (newString), isTrue);
543- // The ext.flutter.disassemble callback is invoked and waited for.
544- expect (
545- source,
546- contains ('start disassemble end disassemble $newString ' ),
547- );
494+ final logExpectations = expectLogs ([
495+ '$originalString $newString ' ,
496+ // The ext.flutter.disassemble callback is invoked and waited for.
497+ 'start disassemble' ,
498+ 'end disassemble' ,
499+ ]);
500+ await makeEditAndRecompile ();
501+ await logExpectations;
548502 });
549503
550504 test (
@@ -592,19 +546,15 @@ void runTests({
592546 });
593547
594548 test ('can hot restart changes ' , () async {
595- final mainDone = waitForMainToExecute (debuggingEnabled: false );
596- await makeEditAndRecompile ();
597- await mainDone;
598- final source = await context.webDriver.pageSource;
599-
600549 // Main is re-invoked which shouldn't clear the state.
601- expect (source.contains (originalString), isTrue);
602- expect (source.contains (newString), isTrue);
603- // The ext.flutter.disassemble callback is invoked and waited for.
604- expect (
605- source,
606- contains ('start disassemble end disassemble $newString ' ),
607- );
550+ final logExpectations = expectLogs ([
551+ '$originalString $newString ' ,
552+ // The ext.flutter.disassemble callback is invoked and waited for.
553+ 'start disassemble' ,
554+ 'end disassemble' ,
555+ ]);
556+ await makeEditAndRecompile ();
557+ await logExpectations;
608558 });
609559 });
610560 },
@@ -653,7 +603,8 @@ void runTests({
653603 ),
654604 );
655605
656- final mainDone = waitForMainToExecute ();
606+ // Main is re-invoked which shouldn't clear the state.
607+ final logExpectation = expectLogs (['$originalString $newString ' ]);
657608 final hotRestart = context.getRegisteredServiceExtension ('hotRestart' );
658609 expect (
659610 await fakeClient.callServiceExtension (hotRestart! ),
@@ -662,24 +613,18 @@ void runTests({
662613
663614 await eventsDone;
664615
665- final sourceBeforeResume = await context.webDriver.pageSource;
666- expect (sourceBeforeResume.contains (newString), isFalse);
667-
668616 final vm = await client.getVM ();
669617 final isolateId = vm.isolates! .first.id! ;
670618 await client.resume (isolateId);
671619
672- await mainDone;
673-
674- final sourceAfterResume = await context.webDriver.pageSource;
675- expect (sourceAfterResume.contains (newString), isTrue);
620+ await logExpectation;
676621 },
677622 );
678623
679624 test (
680625 'after page refresh, does not run app until there is a resume event' ,
681626 () async {
682- final mainDone = waitForMainToExecute ( );
627+ final logExpectation = expectLogs ([newString] );
683628 await makeEditAndRecompile ();
684629 await context.webDriver.driver.refresh ();
685630
@@ -696,17 +641,11 @@ void runTests({
696641
697642 await eventsDone;
698643
699- final sourceBeforeResume = await context.webDriver.pageSource;
700- expect (sourceBeforeResume.contains (newString), isFalse);
701-
702644 final vm = await client.getVM ();
703645 final isolateId = vm.isolates! .first.id! ;
704646 await client.resume (isolateId);
705647
706- await mainDone;
707-
708- final sourceAfterResume = await context.webDriver.pageSource;
709- expect (sourceAfterResume.contains (newString), isTrue);
648+ await logExpectation;
710649 },
711650 );
712651 });
0 commit comments