|
Lines 153-158
static String retrievedString;
a/Tools/TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm_sec1
|
| 153 |
|
153 |
|
| 154 |
@end |
154 |
@end |
| 155 |
|
155 |
|
|
|
156 |
static bool shouldAccept = true; |
| 157 |
static bool navigationComplete = false; |
| 158 |
static bool navigationFailed = false; |
| 159 |
|
| 160 |
@interface TestSWAsyncNavigationDelegate : NSObject <WKNavigationDelegate, WKUIDelegate> |
| 161 |
@end |
| 162 |
|
| 163 |
@implementation TestSWAsyncNavigationDelegate |
| 164 |
|
| 165 |
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation |
| 166 |
{ |
| 167 |
navigationComplete = true; |
| 168 |
} |
| 169 |
|
| 170 |
- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error |
| 171 |
{ |
| 172 |
navigationFailed = true; |
| 173 |
navigationComplete = true; |
| 174 |
} |
| 175 |
|
| 176 |
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error |
| 177 |
{ |
| 178 |
navigationFailed = true; |
| 179 |
navigationComplete = true; |
| 180 |
} |
| 181 |
|
| 182 |
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler |
| 183 |
{ |
| 184 |
decisionHandler(WKNavigationActionPolicyAllow); |
| 185 |
} |
| 186 |
|
| 187 |
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler |
| 188 |
{ |
| 189 |
int64_t deferredWaitTime = 100 * NSEC_PER_MSEC; |
| 190 |
dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, deferredWaitTime); |
| 191 |
dispatch_after(when, dispatch_get_main_queue(), ^{ |
| 192 |
decisionHandler(shouldAccept ? WKNavigationResponsePolicyAllow : WKNavigationResponsePolicyCancel); |
| 193 |
}); |
| 194 |
} |
| 195 |
@end |
| 196 |
|
| 197 |
|
| 156 |
static const char* mainBytes = R"SWRESOURCE( |
198 |
static const char* mainBytes = R"SWRESOURCE( |
| 157 |
<script> |
199 |
<script> |
| 158 |
|
200 |
|
|
Lines 618-623
TEST(ServiceWorkers, InterceptFirstLoadAfterRestoreFromDisk)
a/Tools/TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm_sec2
|
| 618 |
done = false; |
660 |
done = false; |
| 619 |
} |
661 |
} |
| 620 |
|
662 |
|
|
|
663 |
TEST(ServiceWorkers, WaitForPolicyDelegate) |
| 664 |
{ |
| 665 |
[WKWebsiteDataStore _allowWebsiteDataRecordsForAllOrigins]; |
| 666 |
|
| 667 |
// Start with a clean slate data store |
| 668 |
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^() { |
| 669 |
done = true; |
| 670 |
}]; |
| 671 |
TestWebKitAPI::Util::run(&done); |
| 672 |
done = false; |
| 673 |
|
| 674 |
RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]); |
| 675 |
|
| 676 |
RetainPtr<SWMessageHandlerWithExpectedMessage> messageHandler = adoptNS([[SWMessageHandlerWithExpectedMessage alloc] init]); |
| 677 |
[[configuration userContentController] addScriptMessageHandler:messageHandler.get() name:@"sw"]; |
| 678 |
|
| 679 |
RetainPtr<SWSchemes> handler = adoptNS([[SWSchemes alloc] init]); |
| 680 |
handler->resources.set("sw://host/main.html", ResourceInfo { @"text/html", mainForFirstLoadInterceptTestBytes }); |
| 681 |
handler->resources.set("sw://host/sw.js", ResourceInfo { @"application/javascript", scriptInterceptingFirstLoadBytes }); |
| 682 |
[configuration setURLSchemeHandler:handler.get() forURLScheme:@"SW"]; |
| 683 |
|
| 684 |
RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]); |
| 685 |
[webView.get().configuration.processPool _registerURLSchemeServiceWorkersCanHandle:@"sw"]; |
| 686 |
|
| 687 |
// Register a service worker and activate it. |
| 688 |
expectedMessage = "Service Worker activated"; |
| 689 |
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/main.html"]]; |
| 690 |
[webView loadRequest:request]; |
| 691 |
|
| 692 |
TestWebKitAPI::Util::run(&done); |
| 693 |
|
| 694 |
webView = nullptr; |
| 695 |
configuration = nullptr; |
| 696 |
messageHandler = nullptr; |
| 697 |
handler = nullptr; |
| 698 |
|
| 699 |
done = false; |
| 700 |
|
| 701 |
configuration = adoptNS([[WKWebViewConfiguration alloc] init]); |
| 702 |
messageHandler = adoptNS([[SWMessageHandlerWithExpectedMessage alloc] init]); |
| 703 |
[[configuration userContentController] addScriptMessageHandler:messageHandler.get() name:@"sw"]; |
| 704 |
|
| 705 |
handler = adoptNS([[SWSchemes alloc] init]); |
| 706 |
handler->resources.set("sw://host/main.html", ResourceInfo { @"text/html", mainForFirstLoadInterceptTestBytes }); |
| 707 |
handler->resources.set("sw://host/sw.js", ResourceInfo { @"application/javascript", scriptInterceptingFirstLoadBytes }); |
| 708 |
[configuration setURLSchemeHandler:handler.get() forURLScheme:@"SW"]; |
| 709 |
|
| 710 |
webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]); |
| 711 |
[webView.get().configuration.processPool _registerURLSchemeServiceWorkersCanHandle:@"sw"]; |
| 712 |
|
| 713 |
// Verify service worker is intercepting load. |
| 714 |
expectedMessage = "Intercepted by worker"; |
| 715 |
request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/main.html"]]; |
| 716 |
[webView loadRequest:request]; |
| 717 |
|
| 718 |
TestWebKitAPI::Util::run(&done); |
| 719 |
done = false; |
| 720 |
|
| 721 |
webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]); |
| 722 |
[webView.get().configuration.processPool _registerURLSchemeServiceWorkersCanHandle:@"sw"]; |
| 723 |
auto delegate = adoptNS([[TestSWAsyncNavigationDelegate alloc] init]); |
| 724 |
[webView setNavigationDelegate:delegate.get()]; |
| 725 |
[webView setUIDelegate:delegate.get()]; |
| 726 |
|
| 727 |
shouldAccept = true; |
| 728 |
navigationFailed = false; |
| 729 |
navigationComplete = false; |
| 730 |
|
| 731 |
// Verify service worker load goes well when policy delegate is ok. |
| 732 |
request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/main.html"]]; |
| 733 |
[webView loadRequest:request]; |
| 734 |
TestWebKitAPI::Util::run(&navigationComplete); |
| 735 |
|
| 736 |
EXPECT_FALSE(navigationFailed); |
| 737 |
|
| 738 |
webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]); |
| 739 |
[webView.get().configuration.processPool _registerURLSchemeServiceWorkersCanHandle:@"sw"]; |
| 740 |
[webView setNavigationDelegate:delegate.get()]; |
| 741 |
[webView setUIDelegate:delegate.get()]; |
| 742 |
|
| 743 |
shouldAccept = false; |
| 744 |
navigationFailed = false; |
| 745 |
navigationComplete = false; |
| 746 |
|
| 747 |
// Verify service worker load fails well when policy delegate is not ok. |
| 748 |
request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/main.html"]]; |
| 749 |
[webView loadRequest:request]; |
| 750 |
TestWebKitAPI::Util::run(&navigationComplete); |
| 751 |
|
| 752 |
EXPECT_TRUE(navigationFailed); |
| 753 |
} |
| 621 |
#if WK_HAVE_C_SPI |
754 |
#if WK_HAVE_C_SPI |
| 622 |
|
755 |
|
| 623 |
void setConfigurationInjectedBundlePath(WKWebViewConfiguration* configuration) |
756 |
void setConfigurationInjectedBundlePath(WKWebViewConfiguration* configuration) |