@@ -21,9 +21,11 @@ final String _testAppWebDirectory = path.join(_testAppDirectory, 'web');
2121final String _appBuildDirectory = path.join (_testAppDirectory, 'build' , 'web' );
2222final String _target = path.join ('lib' , 'service_worker_test.dart' );
2323final String _targetWithCachedResources = path.join ('lib' , 'service_worker_test_cached_resources.dart' );
24+ final String _targetWithBlockedServiceWorkers = path.join ('lib' , 'service_worker_test_blocked_service_workers.dart' );
2425final String _targetPath = path.join (_testAppDirectory, _target);
2526
2627enum ServiceWorkerTestType {
28+ blockedServiceWorkers,
2729 withoutFlutterJs,
2830 withFlutterJs,
2931 withFlutterJsShort,
@@ -37,6 +39,7 @@ Future<void> main() async {
3739 await runWebServiceWorkerTestWithCachingResources (headless: false , testType: ServiceWorkerTestType .withoutFlutterJs);
3840 await runWebServiceWorkerTestWithCachingResources (headless: false , testType: ServiceWorkerTestType .withFlutterJs);
3941 await runWebServiceWorkerTestWithCachingResources (headless: false , testType: ServiceWorkerTestType .withFlutterJsShort);
42+ await runWebServiceWorkerTestWithBlockedServiceWorkers (headless: false );
4043}
4144
4245Future <void > _setAppVersion (int version) async {
@@ -52,6 +55,9 @@ Future<void> _setAppVersion(int version) async {
5255String _testTypeToIndexFile (ServiceWorkerTestType type) {
5356 late String indexFile;
5457 switch (type) {
58+ case ServiceWorkerTestType .blockedServiceWorkers:
59+ indexFile = 'index_with_blocked_service_workers.html' ;
60+ break ;
5561 case ServiceWorkerTestType .withFlutterJs:
5662 indexFile = 'index_with_flutterjs.html' ;
5763 break ;
@@ -562,3 +568,89 @@ Future<void> runWebServiceWorkerTestWithCachingResources({
562568
563569 print ('END runWebServiceWorkerTestWithCachingResources(headless: $headless , testType: $testType )\n ' );
564570}
571+
572+ Future <void > runWebServiceWorkerTestWithBlockedServiceWorkers ({
573+ required bool headless
574+ }) async {
575+ final Map <String , int > requestedPathCounts = < String , int > {};
576+ void expectRequestCounts (Map <String , int > expectedCounts) =>
577+ _expectRequestCounts (expectedCounts, requestedPathCounts);
578+
579+ AppServer ? server;
580+ Future <void > waitForAppToLoad (Map <String , int > waitForCounts) async =>
581+ _waitForAppToLoad (waitForCounts, requestedPathCounts, server);
582+
583+ Future <void > startAppServer ({
584+ required String cacheControl,
585+ }) async {
586+ final int serverPort = await findAvailablePort ();
587+ final int browserDebugPort = await findAvailablePort ();
588+ server = await AppServer .start (
589+ headless: headless,
590+ cacheControl: cacheControl,
591+ // TODO(yjbanov): use a better port disambiguation strategy than trying
592+ // to guess what ports other tests use.
593+ appUrl: 'http://localhost:$serverPort /index.html' ,
594+ serverPort: serverPort,
595+ browserDebugPort: browserDebugPort,
596+ appDirectory: _appBuildDirectory,
597+ additionalRequestHandlers: < Handler > [
598+ (Request request) {
599+ final String requestedPath = request.url.path;
600+ requestedPathCounts.putIfAbsent (requestedPath, () => 0 );
601+ requestedPathCounts[requestedPath] = requestedPathCounts[requestedPath]! + 1 ;
602+ if (requestedPath == 'CLOSE' ) {
603+ return Response .ok ('OK' );
604+ }
605+ return Response .notFound ('' );
606+ },
607+ ],
608+ );
609+ }
610+
611+ // Preserve old index.html as index_og.html so we can restore it later for other tests
612+ await runCommand (
613+ 'mv' ,
614+ < String > [
615+ 'index.html' ,
616+ 'index_og.html' ,
617+ ],
618+ workingDirectory: _testAppWebDirectory,
619+ );
620+
621+ print ('BEGIN runWebServiceWorkerTestWithBlockedServiceWorkers(headless: $headless )\n ' );
622+ try {
623+ await _rebuildApp (version: 1 , testType: ServiceWorkerTestType .blockedServiceWorkers, target: _targetWithBlockedServiceWorkers);
624+
625+ print ('Ensure app starts (when service workers are blocked)' );
626+ await startAppServer (cacheControl: 'max-age=3600' );
627+ await waitForAppToLoad (< String , int > {
628+ 'CLOSE' : 1 ,
629+ });
630+ expectRequestCounts (< String , int > {
631+ 'index.html' : 1 ,
632+ 'flutter.js' : 1 ,
633+ 'main.dart.js' : 1 ,
634+ 'assets/FontManifest.json' : 1 ,
635+ 'assets/fonts/MaterialIcons-Regular.otf' : 1 ,
636+ 'CLOSE' : 1 ,
637+ // In headless mode Chrome does not load 'manifest.json' and 'favicon.ico'.
638+ if (! headless)
639+ ...< String , int > {
640+ 'manifest.json' : 1 ,
641+ 'favicon.ico' : 1 ,
642+ },
643+ });
644+ } finally {
645+ await runCommand (
646+ 'mv' ,
647+ < String > [
648+ 'index_og.html' ,
649+ 'index.html' ,
650+ ],
651+ workingDirectory: _testAppWebDirectory,
652+ );
653+ await server? .stop ();
654+ }
655+ print ('END runWebServiceWorkerTestWithBlockedServiceWorkers(headless: $headless )\n ' );
656+ }
0 commit comments