@@ -57,20 +57,17 @@ void runTests({
57
57
await recompile (hasEdits: true );
58
58
}
59
59
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);
69
63
final completer = Completer <void >();
70
- final expectedString = 'main executed' ;
71
64
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
+ }
74
71
}
75
72
});
76
73
await completer.future;
@@ -98,14 +95,10 @@ void runTests({
98
95
});
99
96
100
97
test ('can live reload changes ' , () async {
101
- final mainDone = waitForMainToExecute ();
102
- await makeEditAndRecompile ();
103
- await mainDone;
104
- final source = await context.webDriver.pageSource;
105
-
106
98
// 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;
109
102
});
110
103
});
111
104
@@ -130,14 +123,10 @@ void runTests({
130
123
});
131
124
132
125
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
-
138
126
// 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;
141
130
});
142
131
});
143
132
@@ -163,14 +152,10 @@ void runTests({
163
152
});
164
153
165
154
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
-
171
155
// 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;
174
159
});
175
160
});
176
161
},
@@ -306,20 +291,16 @@ void runTests({
306
291
]),
307
292
),
308
293
);
309
- final mainDone = waitForMainToExecute ();
294
+ // Main is re-invoked which shouldn't clear the state.
295
+ final logExpectation = expectLogs (['$originalString $newString ' ]);
310
296
final hotRestart = context.getRegisteredServiceExtension ('hotRestart' );
311
297
expect (
312
298
await fakeClient.callServiceExtension (hotRestart! ),
313
299
const TypeMatcher <Success >(),
314
300
);
315
301
316
302
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;
323
304
});
324
305
325
306
test ('can send events before and after hot restart' , () async {
@@ -352,7 +333,8 @@ void runTests({
352
333
);
353
334
354
335
await recompile ();
355
- final mainDone = waitForMainToExecute ();
336
+ // Main is re-invoked which shouldn't clear the state.
337
+ final logExpectation = expectLogs (['$originalString $originalString ' ]);
356
338
final hotRestart = context.getRegisteredServiceExtension ('hotRestart' );
357
339
expect (
358
340
await fakeClient.callServiceExtension (hotRestart! ),
@@ -371,11 +353,7 @@ void runTests({
371
353
);
372
354
373
355
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;
379
357
});
380
358
381
359
test ('can refresh the page via the fullReload service extension' , () async {
@@ -393,20 +371,16 @@ void runTests({
393
371
]),
394
372
),
395
373
);
396
- final mainDone = waitForMainToExecute ();
374
+ // Should see only the new text.
375
+ final logExpectation = expectLogs ([newString]);
397
376
final fullReload = context.getRegisteredServiceExtension ('fullReload' );
398
377
expect (
399
378
await fakeClient.callServiceExtension (fullReload! ),
400
379
isA <Success >(),
401
380
);
402
381
403
382
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;
410
384
});
411
385
412
386
test ('can hot restart while paused' , () async {
@@ -430,17 +404,11 @@ void runTests({
430
404
);
431
405
432
406
await makeEditAndRecompile ();
433
- final mainDone = waitForMainToExecute ();
407
+ // Main is re-invoked which shouldn't clear the state.
408
+ final logExpectation = expectLogs (['$originalString $newString ' ]);
434
409
final hotRestart = context.getRegisteredServiceExtension ('hotRestart' );
435
410
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;
444
412
445
413
vm = await client.getVM ();
446
414
isolateId = vm.isolates! .first.id! ;
@@ -477,37 +445,27 @@ void runTests({
477
445
test ('can hot restart with no changes, hot restart with changes, and '
478
446
'hot restart again with no changes' , () async {
479
447
// Empty hot restart.
480
- var mainDone = waitForMainToExecute ( );
448
+ var logExpectation = expectLogs ([ '$ originalString $ originalString ' ] );
481
449
await recompile ();
482
450
final hotRestart = context.getRegisteredServiceExtension ('hotRestart' );
483
451
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;
489
453
490
454
// Hot restart.
491
- mainDone = waitForMainToExecute ();
455
+ logExpectation = expectLogs ([
456
+ '$originalString $originalString $newString ' ,
457
+ ]);
492
458
await makeEditAndRecompile ();
493
459
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;
500
461
501
462
// Empty hot restart.
502
- mainDone = waitForMainToExecute ();
463
+ logExpectation = expectLogs ([
464
+ '$originalString $originalString $newString $newString ' ,
465
+ ]);
503
466
await recompile ();
504
467
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;
511
469
});
512
470
}, timeout: Timeout .factor (2 ));
513
471
@@ -532,19 +490,15 @@ void runTests({
532
490
});
533
491
534
492
test ('can hot restart changes ' , () async {
535
- final mainDone = waitForMainToExecute ();
536
- await makeEditAndRecompile ();
537
- await mainDone;
538
- final source = await context.webDriver.pageSource;
539
-
540
493
// 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;
548
502
});
549
503
550
504
test (
@@ -592,19 +546,15 @@ void runTests({
592
546
});
593
547
594
548
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
-
600
549
// 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;
608
558
});
609
559
});
610
560
},
@@ -653,7 +603,8 @@ void runTests({
653
603
),
654
604
);
655
605
656
- final mainDone = waitForMainToExecute ();
606
+ // Main is re-invoked which shouldn't clear the state.
607
+ final logExpectation = expectLogs (['$originalString $newString ' ]);
657
608
final hotRestart = context.getRegisteredServiceExtension ('hotRestart' );
658
609
expect (
659
610
await fakeClient.callServiceExtension (hotRestart! ),
@@ -662,24 +613,18 @@ void runTests({
662
613
663
614
await eventsDone;
664
615
665
- final sourceBeforeResume = await context.webDriver.pageSource;
666
- expect (sourceBeforeResume.contains (newString), isFalse);
667
-
668
616
final vm = await client.getVM ();
669
617
final isolateId = vm.isolates! .first.id! ;
670
618
await client.resume (isolateId);
671
619
672
- await mainDone;
673
-
674
- final sourceAfterResume = await context.webDriver.pageSource;
675
- expect (sourceAfterResume.contains (newString), isTrue);
620
+ await logExpectation;
676
621
},
677
622
);
678
623
679
624
test (
680
625
'after page refresh, does not run app until there is a resume event' ,
681
626
() async {
682
- final mainDone = waitForMainToExecute ( );
627
+ final logExpectation = expectLogs ([newString] );
683
628
await makeEditAndRecompile ();
684
629
await context.webDriver.driver.refresh ();
685
630
@@ -696,17 +641,11 @@ void runTests({
696
641
697
642
await eventsDone;
698
643
699
- final sourceBeforeResume = await context.webDriver.pageSource;
700
- expect (sourceBeforeResume.contains (newString), isFalse);
701
-
702
644
final vm = await client.getVM ();
703
645
final isolateId = vm.isolates! .first.id! ;
704
646
await client.resume (isolateId);
705
647
706
- await mainDone;
707
-
708
- final sourceAfterResume = await context.webDriver.pageSource;
709
- expect (sourceAfterResume.contains (newString), isTrue);
648
+ await logExpectation;
710
649
},
711
650
);
712
651
});
0 commit comments