@@ -37,6 +37,8 @@ enum ServiceWorkerTestType {
37
37
withFlutterJsEntrypointLoadedEvent,
38
38
// Same as withFlutterJsEntrypointLoadedEvent, but with TrustedTypes enabled.
39
39
withFlutterJsTrustedTypesOn,
40
+ // Uses custom serviceWorkerVersion.
41
+ withFlutterJsCustomServiceWorkerVersion,
40
42
// Entrypoint generated by `flutter create`.
41
43
generatedEntrypoint,
42
44
}
@@ -58,6 +60,7 @@ Future<void> main() async {
58
60
await runWebServiceWorkerTestWithCachingResources (headless: false , testType: ServiceWorkerTestType .withFlutterJsTrustedTypesOn);
59
61
await runWebServiceWorkerTestWithGeneratedEntrypoint (headless: false );
60
62
await runWebServiceWorkerTestWithBlockedServiceWorkers (headless: false );
63
+ await runWebServiceWorkerTestWithCustomServiceWorkerVersion (headless: false );
61
64
62
65
if (hasError) {
63
66
reportErrorsAndExit ('${bold }One or more tests failed.$reset ' );
@@ -117,6 +120,8 @@ String _testTypeToIndexFile(ServiceWorkerTestType type) {
117
120
indexFile = 'index_with_flutterjs_entrypoint_loaded.html' ;
118
121
case ServiceWorkerTestType .withFlutterJsTrustedTypesOn:
119
122
indexFile = 'index_with_flutterjs_el_tt_on.html' ;
123
+ case ServiceWorkerTestType .withFlutterJsCustomServiceWorkerVersion:
124
+ indexFile = 'index_with_flutterjs_custom_sw_version.html' ;
120
125
case ServiceWorkerTestType .generatedEntrypoint:
121
126
indexFile = 'generated_entrypoint.html' ;
122
127
}
@@ -703,3 +708,137 @@ Future<void> runWebServiceWorkerTestWithBlockedServiceWorkers({
703
708
}
704
709
print ('END runWebServiceWorkerTestWithBlockedServiceWorkers(headless: $headless )' );
705
710
}
711
+
712
+ /// Regression test for https://github.com/flutter/flutter/issues/130212.
713
+ Future <void > runWebServiceWorkerTestWithCustomServiceWorkerVersion ({
714
+ required bool headless,
715
+ }) async {
716
+ final Map <String , int > requestedPathCounts = < String , int > {};
717
+ void expectRequestCounts (Map <String , int > expectedCounts) =>
718
+ _expectRequestCounts (expectedCounts, requestedPathCounts);
719
+
720
+ AppServer ? server;
721
+ Future <void > waitForAppToLoad (Map <String , int > waitForCounts) async =>
722
+ _waitForAppToLoad (waitForCounts, requestedPathCounts, server);
723
+
724
+ Future <void > startAppServer ({
725
+ required String cacheControl,
726
+ }) async {
727
+ final int serverPort = await findAvailablePortAndPossiblyCauseFlakyTests ();
728
+ final int browserDebugPort = await findAvailablePortAndPossiblyCauseFlakyTests ();
729
+ server = await AppServer .start (
730
+ headless: headless,
731
+ cacheControl: cacheControl,
732
+ // TODO(yjbanov): use a better port disambiguation strategy than trying
733
+ // to guess what ports other tests use.
734
+ appUrl: 'http://localhost:$serverPort /index.html' ,
735
+ serverPort: serverPort,
736
+ browserDebugPort: browserDebugPort,
737
+ appDirectory: _appBuildDirectory,
738
+ additionalRequestHandlers: < Handler > [
739
+ (Request request) {
740
+ final String requestedPath = request.url.path;
741
+ requestedPathCounts.putIfAbsent (requestedPath, () => 0 );
742
+ requestedPathCounts[requestedPath] = requestedPathCounts[requestedPath]! + 1 ;
743
+ if (requestedPath == 'CLOSE' ) {
744
+ return Response .ok ('OK' );
745
+ }
746
+ return Response .notFound ('' );
747
+ },
748
+ ],
749
+ );
750
+ }
751
+
752
+ // Preserve old index.html as index_og.html so we can restore it later for other tests
753
+ await runCommand (
754
+ 'mv' ,
755
+ < String > [
756
+ 'index.html' ,
757
+ 'index_og.html' ,
758
+ ],
759
+ workingDirectory: _testAppWebDirectory,
760
+ );
761
+
762
+ print ('BEGIN runWebServiceWorkerTestWithCustomServiceWorkerVersion(headless: $headless )' );
763
+ try {
764
+ await _rebuildApp (version: 1 , testType: ServiceWorkerTestType .withFlutterJsCustomServiceWorkerVersion, target: _target);
765
+
766
+ print ('Test page load' );
767
+ await startAppServer (cacheControl: 'max-age=0' );
768
+ await waitForAppToLoad (< String , int > {
769
+ 'CLOSE' : 1 ,
770
+ 'flutter_service_worker.js' : 1 ,
771
+ 'assets/fonts/MaterialIcons-Regular.otf' : 1 ,
772
+ });
773
+ expectRequestCounts (< String , int > {
774
+ 'index.html' : 2 ,
775
+ 'flutter.js' : 1 ,
776
+ 'main.dart.js' : 1 ,
777
+ 'CLOSE' : 1 ,
778
+ 'flutter_service_worker.js' : 1 ,
779
+ 'assets/FontManifest.json' : 1 ,
780
+ 'assets/AssetManifest.json' : 1 ,
781
+ 'assets/fonts/MaterialIcons-Regular.otf' : 1 ,
782
+ // In headless mode Chrome does not load 'manifest.json' and 'favicon.ico'.
783
+ if (! headless)
784
+ ...< String , int > {
785
+ 'manifest.json' : 1 ,
786
+ 'favicon.ico' : 1 ,
787
+ },
788
+ });
789
+
790
+ print ('Test page reload, ensure service worker is not reloaded' );
791
+ await server! .chrome.reloadPage (ignoreCache: true );
792
+ await waitForAppToLoad (< String , int > {
793
+ 'CLOSE' : 1 ,
794
+ 'flutter.js' : 1 ,
795
+ });
796
+ expectRequestCounts (< String , int > {
797
+ 'index.html' : 1 ,
798
+ 'flutter.js' : 1 ,
799
+ 'main.dart.js' : 1 ,
800
+ 'assets/FontManifest.json' : 1 ,
801
+ 'assets/fonts/MaterialIcons-Regular.otf' : 1 ,
802
+ 'CLOSE' : 1 ,
803
+ // In headless mode Chrome does not load 'manifest.json' and 'favicon.ico'.
804
+ if (! headless)
805
+ ...< String , int > {
806
+ 'manifest.json' : 1 ,
807
+ 'favicon.ico' : 1 ,
808
+ },
809
+ });
810
+
811
+ print ('Test page reload after rebuild, ensure service worker is not reloaded' );
812
+ await _rebuildApp (version: 1 , testType: ServiceWorkerTestType .withFlutterJsCustomServiceWorkerVersion, target: _target);
813
+ await server! .chrome.reloadPage (ignoreCache: true );
814
+ await waitForAppToLoad (< String , int > {
815
+ 'CLOSE' : 1 ,
816
+ 'flutter.js' : 1 ,
817
+ });
818
+ expectRequestCounts (< String , int > {
819
+ 'index.html' : 1 ,
820
+ 'flutter.js' : 1 ,
821
+ 'main.dart.js' : 1 ,
822
+ 'assets/FontManifest.json' : 1 ,
823
+ 'assets/fonts/MaterialIcons-Regular.otf' : 1 ,
824
+ 'CLOSE' : 1 ,
825
+ // In headless mode Chrome does not load 'manifest.json' and 'favicon.ico'.
826
+ if (! headless)
827
+ ...< String , int > {
828
+ 'manifest.json' : 1 ,
829
+ 'favicon.ico' : 1 ,
830
+ },
831
+ });
832
+ } finally {
833
+ await runCommand (
834
+ 'mv' ,
835
+ < String > [
836
+ 'index_og.html' ,
837
+ 'index.html' ,
838
+ ],
839
+ workingDirectory: _testAppWebDirectory,
840
+ );
841
+ await server? .stop ();
842
+ }
843
+ print ('END runWebServiceWorkerTestWithCustomServiceWorkerVersion(headless: $headless )' );
844
+ }
0 commit comments