@@ -852,6 +852,103 @@ function frameLoaded() {
852
852
Util::run (&saved);
853
853
}
854
854
855
+ static const char * htmlDataBytesForIframesWithSameURL = R"TESTRESOURCE(
856
+ <script>
857
+ count = 0;
858
+ messageCount = 0;
859
+ function frameLoaded() {
860
+ if (++count == 3) {
861
+ const iframes = document.getElementsByTagName("iframe");
862
+ for (let i = 0; i < iframes.length; ++i)
863
+ iframes[i].contentWindow.postMessage("thisisiframe" + (i + 1), "*");
864
+ }
865
+ }
866
+ window.addEventListener("message", function(event) {
867
+ if (++messageCount == 3)
868
+ window.webkit.messageHandlers.testHandler.postMessage("done");
869
+ });
870
+ </script>
871
+ <iframe onload="frameLoaded();" src="iframe.html"></iframe>
872
+ <iframe onload="frameLoaded();" src="iframe.html"></iframe>
873
+ <iframe onload="frameLoaded();" src="iframe.html"></iframe>
874
+ )TESTRESOURCE" ;
875
+ static const char * iframeHTMLDataBytesForIframesWithSameURL = R"TESTRESOURCE(
876
+ <head>
877
+ <script>
878
+ window.addEventListener("message", function(event) {
879
+ if (!document.body.innerHTML)
880
+ document.body.innerHTML = "<p>" + event.data + "</p>";
881
+ window.parent.postMessage("done", "*");
882
+ });
883
+ </script>
884
+ </head>
885
+ )TESTRESOURCE" ;
886
+
887
+ TEST (WebArchive , SaveResourcesIframesWithSameURL)
888
+ {
889
+ RetainPtr<NSURL > directoryURL = [NSURL fileURLWithPath: [NSTemporaryDirectory () stringByAppendingPathComponent: @" SaveResourcesTest" ] isDirectory: YES ];
890
+ NSFileManager *fileManager = [NSFileManager defaultManager ];
891
+ [fileManager removeItemAtURL: directoryURL.get () error: nil ];
892
+
893
+ auto configuration = adoptNS ([[WKWebViewConfiguration alloc ] init ]);
894
+ auto schemeHandler = adoptNS ([[TestURLSchemeHandler alloc ] init ]);
895
+ [configuration setURLSchemeHandler: schemeHandler.get () forURLScheme: @" webarchivetest" ];
896
+ NSData *htmlData = [NSData dataWithBytes: htmlDataBytesForIframesWithSameURL length: strlen (htmlDataBytesForIframesWithSameURL)];
897
+ NSData *iframeHTMLData = [NSData dataWithBytes: iframeHTMLDataBytesForIframesWithSameURL length: strlen (iframeHTMLDataBytesForIframesWithSameURL)];
898
+ [schemeHandler setStartURLSchemeTaskHandler: ^(WKWebView *, id <WKURLSchemeTask > task) {
899
+ NSData *data = nil ;
900
+ NSString *mimeType = nil ;
901
+ if ([task.request.URL.absoluteString isEqualToString: @" webarchivetest://host/main.html" ]) {
902
+ mimeType = @" text/html" ;
903
+ data = htmlData;
904
+ } else if ([task.request.URL.absoluteString isEqualToString: @" webarchivetest://host/iframe.html" ]) {
905
+ mimeType = @" text/html" ;
906
+ data = iframeHTMLData;
907
+ } else
908
+ FAIL ();
909
+
910
+ auto response = adoptNS ([[NSURLResponse alloc ] initWithURL: task.request.URL MIMEType: mimeType expectedContentLength: data.length textEncodingName: nil ]);
911
+ [task didReceiveResponse: response.get ()];
912
+ [task didReceiveData: data];
913
+ [task didFinish ];
914
+ }];
915
+
916
+ auto webView = adoptNS ([[TestWKWebView alloc ] initWithFrame: NSMakeRect (0 , 0 , 800 , 600 ) configuration: configuration.get ()]);
917
+ static bool messageReceived = false ;
918
+ [webView performAfterReceivingMessage: @" done" action: [&] {
919
+ messageReceived = true ;
920
+ }];
921
+ [webView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: @" webarchivetest://host/main.html" ]]];
922
+ Util::run (&messageReceived);
923
+
924
+ static bool saved = false ;
925
+ [webView _saveResources: directoryURL.get () suggestedFileName: @" host" completionHandler: ^(NSError *error) {
926
+ EXPECT_NULL (error);
927
+ NSString *mainResourcePath = [directoryURL URLByAppendingPathComponent: @" host.html" ].path ;
928
+ EXPECT_TRUE ([fileManager fileExistsAtPath: mainResourcePath]);
929
+
930
+ NSString *resourceDirectoryName = @" host_files" ;
931
+ NSString *resourceDirectoryPath = [directoryURL URLByAppendingPathComponent: resourceDirectoryName].path ;
932
+ NSArray *resourceFileNames = [fileManager contentsOfDirectoryAtPath: resourceDirectoryPath error: 0 ];
933
+ EXPECT_EQ (3llu, resourceFileNames.count );
934
+
935
+ NSMutableSet *frameResourceContentsToFind = [NSMutableSet set ];
936
+ [frameResourceContentsToFind addObjectsFromArray: [NSArray arrayWithObjects: @" thisisiframe1" , @" thisisiframe2" , @" thisisiframe3" , nil ]];
937
+ for (NSString *fileName in resourceFileNames) {
938
+ EXPECT_TRUE ([fileName containsString: @" frame_" ]);
939
+ NSString *resourceFilePath = [resourceDirectoryPath stringByAppendingPathComponent: fileName];
940
+ NSString * savedSubframeResource = [[NSString alloc ] initWithData: [NSData dataWithContentsOfFile: resourceFilePath] encoding: NSUTF8StringEncoding];
941
+ NSRange range = [savedSubframeResource rangeOfString: @" thisisiframe" ];
942
+ EXPECT_NE (NSNotFound , (long )range.location );
943
+ NSString *iframeContent = [savedSubframeResource substringWithRange: NSMakeRange (range.location, range.length + 1 )];
944
+ [frameResourceContentsToFind removeObject: iframeContent];
945
+ }
946
+ EXPECT_EQ (0u , frameResourceContentsToFind.count );
947
+ saved = true ;
948
+ }];
949
+ Util::run (&saved);
950
+ }
951
+
855
952
} // namespace TestWebKitAPI
856
953
857
954
#endif // PLATFORM(MAC) || PLATFORM(IOS_FAMILY)
0 commit comments