9b041d4c318f149c05d0cd284d1cab641c99895b
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WebKit2Cocoa / WebsitePolicies.mm
1 /*
2  * Copyright (C) 2016 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27
28 #import "PlatformUtilities.h"
29 #import "TestWKWebView.h"
30 #import <WebKit/WKPagePrivate.h>
31 #import <WebKit/WKPreferencesRefPrivate.h>
32 #import <WebKit/WKUserContentControllerPrivate.h>
33 #import <WebKit/WKWebViewPrivate.h>
34 #import <WebKit/_WKUserContentExtensionStorePrivate.h>
35 #import <WebKit/_WKWebsitePolicies.h>
36 #import <wtf/MainThread.h>
37 #import <wtf/RetainPtr.h>
38
39 #if PLATFORM(IOS)
40 #import <WebKit/WKWebViewConfigurationPrivate.h>
41 #endif
42
43 #if WK_API_ENABLED
44
45 @interface WKWebView ()
46 - (WKPageRef)_pageForTesting;
47 @end
48
49 static bool doneCompiling;
50 static bool receivedAlert;
51
52 #if PLATFORM(MAC)
53 static std::optional<WKAutoplayEvent> receivedAutoplayEvent;
54 #endif
55
56 static size_t alertCount;
57
58 @interface ContentBlockingWebsitePoliciesDelegate : NSObject <WKNavigationDelegate, WKUIDelegate>
59 @end
60
61 @implementation ContentBlockingWebsitePoliciesDelegate
62
63 - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
64 {
65     // _webView:decidePolicyForNavigationAction:decisionHandler: should be called instead if it is implemented.
66     EXPECT_TRUE(false);
67     decisionHandler(WKNavigationActionPolicyAllow);
68 }
69
70 - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
71 {
72     switch (alertCount++) {
73     case 0:
74         // FIXME: The first content blocker should be enabled here.
75         // ContentExtensionsBackend::addContentExtension isn't being called in the WebProcess
76         // until after the first main resource starts loading, so we need to send a message to the
77         // WebProcess before loading if we have installed content blockers.
78         // See rdar://problem/27788755
79         EXPECT_STREQ("content blockers disabled", message.UTF8String);
80         break;
81     case 1:
82         // Default behavior.
83         EXPECT_STREQ("content blockers enabled", message.UTF8String);
84         break;
85     case 2:
86         // After having set websitePolicies.contentBlockersEnabled to false.
87         EXPECT_STREQ("content blockers disabled", message.UTF8String);
88         break;
89     case 3:
90         // After having reloaded without content blockers.
91         EXPECT_STREQ("content blockers disabled", message.UTF8String);
92         break;
93     default:
94         EXPECT_TRUE(false);
95     }
96     receivedAlert = true;
97     completionHandler();
98 }
99
100 - (void)_webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy, _WKWebsitePolicies *))decisionHandler
101 {
102     _WKWebsitePolicies *websitePolicies = [[[_WKWebsitePolicies alloc] init] autorelease];
103     switch (alertCount) {
104     case 0:
105         // Verify an existing bug the first time a page is loaded in a new WebProcess.
106         break;
107     case 1:
108         // Verify the content blockers behave correctly with the default behavior.
109         break;
110     case 2:
111         {
112             // Verify disabling content blockers works correctly.
113             websitePolicies.contentBlockersEnabled = false;
114             
115             // Verify calling the decisionHandler asynchronously works correctly.
116             auto decisionHandlerCopy = Block_copy(decisionHandler);
117             callOnMainThread([decisionHandlerCopy, websitePolicies = RetainPtr<_WKWebsitePolicies>(websitePolicies)] {
118                 decisionHandlerCopy(WKNavigationActionPolicyAllow, websitePolicies.get());
119                 Block_release(decisionHandlerCopy);
120             });
121         }
122         return;
123     case 3:
124         // Verify enabling content blockers has no effect when reloading without content blockers.
125         websitePolicies.contentBlockersEnabled = true;
126         break;
127     default:
128         EXPECT_TRUE(false);
129     }
130     decisionHandler(WKNavigationActionPolicyAllow, websitePolicies);
131 }
132
133 @end
134
135 TEST(WebKit2, WebsitePoliciesContentBlockersEnabled)
136 {
137     [[_WKUserContentExtensionStore defaultStore] _removeAllContentExtensions];
138
139     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
140
141     doneCompiling = false;
142     NSString* contentBlocker = @"[{\"action\":{\"type\":\"css-display-none\",\"selector\":\".hidden\"},\"trigger\":{\"url-filter\":\".*\"}}]";
143     [[_WKUserContentExtensionStore defaultStore] compileContentExtensionForIdentifier:@"WebsitePoliciesTest" encodedContentExtension:contentBlocker completionHandler:^(_WKUserContentFilter *filter, NSError *error) {
144         EXPECT_TRUE(error == nil);
145         [[configuration userContentController] _addUserContentFilter:filter];
146         doneCompiling = true;
147     }];
148     TestWebKitAPI::Util::run(&doneCompiling);
149
150     auto webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
151
152     auto delegate = adoptNS([[ContentBlockingWebsitePoliciesDelegate alloc] init]);
153     [webView setNavigationDelegate:delegate.get()];
154     [webView setUIDelegate:delegate.get()];
155
156     NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"contentBlockerCheck" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
157     alertCount = 0;
158     receivedAlert = false;
159     [webView loadRequest:request];
160     TestWebKitAPI::Util::run(&receivedAlert);
161
162     receivedAlert = false;
163     [webView reload];
164     TestWebKitAPI::Util::run(&receivedAlert);
165
166     receivedAlert = false;
167     [webView reload];
168     TestWebKitAPI::Util::run(&receivedAlert);
169
170     receivedAlert = false;
171     [webView _reloadWithoutContentBlockers];
172     TestWebKitAPI::Util::run(&receivedAlert);
173
174     [[_WKUserContentExtensionStore defaultStore] _removeAllContentExtensions];
175 }
176
177 @interface AutoplayPoliciesDelegate : NSObject <WKNavigationDelegate, WKUIDelegate>
178 @property (nonatomic, copy) _WKWebsiteAutoplayPolicy(^autoplayPolicyForURL)(NSURL *);
179 @property (nonatomic) BOOL allowsAutoplayQuirks;
180 @end
181
182 @implementation AutoplayPoliciesDelegate
183
184 - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
185 {
186     // _webView:decidePolicyForNavigationAction:decisionHandler: should be called instead if it is implemented.
187     EXPECT_TRUE(false);
188     decisionHandler(WKNavigationActionPolicyAllow);
189 }
190
191 - (void)_webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy, _WKWebsitePolicies *))decisionHandler
192 {
193     _WKWebsitePolicies *websitePolicies = [[[_WKWebsitePolicies alloc] init] autorelease];
194     websitePolicies.allowsAutoplayQuirks = _allowsAutoplayQuirks;
195     if (_autoplayPolicyForURL)
196         websitePolicies.autoplayPolicy = _autoplayPolicyForURL(navigationAction.request.URL);
197     decisionHandler(WKNavigationActionPolicyAllow, websitePolicies);
198 }
199
200 @end
201
202 TEST(WebKit2, WebsitePoliciesAutoplayEnabled)
203 {
204     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
205
206 #if PLATFORM(IOS)
207     [configuration setAllowsInlineMediaPlayback:YES];
208 #endif
209
210     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
211
212     auto delegate = adoptNS([[AutoplayPoliciesDelegate alloc] init]);
213     [webView setNavigationDelegate:delegate.get()];
214
215     NSURLRequest *requestWithAudio = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"autoplay-check" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
216     NSURLRequest *requestWithoutAudio = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"autoplay-no-audio-check" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
217     
218     [delegate setAutoplayPolicyForURL:^(NSURL *) {
219         return _WKWebsiteAutoplayPolicyAllowWithoutSound;
220     }];
221     [webView loadRequest:requestWithAudio];
222     [webView waitForMessage:@"did-not-play"];
223
224     [webView loadRequest:requestWithoutAudio];
225     [webView waitForMessage:@"autoplayed"];
226
227     [delegate setAutoplayPolicyForURL:^(NSURL *) {
228         return _WKWebsiteAutoplayPolicyDeny;
229     }];
230     [webView loadRequest:requestWithAudio];
231     [webView waitForMessage:@"did-not-play"];
232
233     // Test updating website policies.
234     _WKWebsitePolicies *websitePolicies = [[[_WKWebsitePolicies alloc] init] autorelease];
235     websitePolicies.autoplayPolicy = _WKWebsiteAutoplayPolicyAllow;
236     [webView _updateWebsitePolicies:websitePolicies];
237     [webView stringByEvaluatingJavaScript:@"playVideo()"];
238     [webView waitForMessage:@"autoplayed"];
239
240     [webView loadRequest:requestWithoutAudio];
241     [webView waitForMessage:@"did-not-play"];
242
243     [delegate setAutoplayPolicyForURL:^(NSURL *) {
244         return _WKWebsiteAutoplayPolicyAllow;
245     }];
246     [webView loadRequest:requestWithAudio];
247     [webView waitForMessage:@"autoplayed"];
248
249     [webView loadRequest:requestWithoutAudio];
250     [webView waitForMessage:@"autoplayed"];
251
252     NSURLRequest *requestWithAudioInIFrame = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"autoplay-check-in-iframe" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
253
254     // If the top-level document allows autoplay, any iframes should also autoplay.
255     [delegate setAutoplayPolicyForURL:^(NSURL *url) {
256         if ([url.lastPathComponent isEqualToString:@"autoplay-check-in-iframe.html"])
257             return _WKWebsiteAutoplayPolicyAllow;
258         return _WKWebsiteAutoplayPolicyDeny;
259     }];
260
261     [webView loadRequest:requestWithAudioInIFrame];
262     [webView waitForMessage:@"autoplayed"];
263
264     // If the top-level document disallows autoplay, any iframes should also not autoplay.
265     [delegate setAutoplayPolicyForURL:^(NSURL *url) {
266         if ([url.lastPathComponent isEqualToString:@"autoplay-check-in-iframe.html"])
267             return _WKWebsiteAutoplayPolicyDeny;
268         return _WKWebsiteAutoplayPolicyAllow;
269     }];
270
271     [webView loadRequest:requestWithAudioInIFrame];
272     [webView waitForMessage:@"did-not-play"];
273 }
274
275 #if PLATFORM(MAC)
276 static void handleAutoplayEvent(WKPageRef page, WKAutoplayEvent event, const void* clientInfo)
277 {
278     receivedAutoplayEvent = event;
279 }
280
281 static void runUntilReceivesAutoplayEvent(WKAutoplayEvent event)
282 {
283     while (!receivedAutoplayEvent || *receivedAutoplayEvent != event)
284         CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, true);
285 }
286
287 TEST(WebKit2, WebsitePoliciesPlayAfterPreventedAutoplay)
288 {
289     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
290     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 336, 276) configuration:configuration.get()]);
291
292     auto delegate = adoptNS([[AutoplayPoliciesDelegate alloc] init]);
293     [delegate setAutoplayPolicyForURL:^(NSURL *) {
294         return _WKWebsiteAutoplayPolicyDeny;
295     }];
296     [webView setNavigationDelegate:delegate.get()];
297
298     WKPageUIClientV9 uiClient;
299     memset(&uiClient, 0, sizeof(uiClient));
300
301     uiClient.base.version = 9;
302     uiClient.handleAutoplayEvent = handleAutoplayEvent;
303
304     WKPageSetPageUIClient([webView _pageForTesting], &uiClient.base);
305     NSPoint playButtonClickPoint = NSMakePoint(20, 256);
306
307     receivedAutoplayEvent = std::nullopt;
308     NSURLRequest *jsPlayRequest = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"js-play-with-controls" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
309     [webView loadRequest:jsPlayRequest];
310     [webView waitForMessage:@"loaded"];
311     runUntilReceivesAutoplayEvent(kWKAutoplayEventDidPreventFromAutoplaying);
312
313     [webView mouseDownAtPoint:playButtonClickPoint simulatePressure:NO];
314     [webView mouseUpAtPoint:playButtonClickPoint];
315     runUntilReceivesAutoplayEvent(kWKAutoplayEventDidPlayMediaPreventedFromAutoplaying);
316
317     receivedAutoplayEvent = std::nullopt;
318     [webView loadHTMLString:@"" baseURL:nil];
319
320     NSURLRequest *autoplayRequest = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"autoplay-with-controls" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
321     [webView loadRequest:autoplayRequest];
322     [webView waitForMessage:@"loaded"];
323     runUntilReceivesAutoplayEvent(kWKAutoplayEventDidPreventFromAutoplaying);
324
325     [webView mouseDownAtPoint:playButtonClickPoint simulatePressure:NO];
326     [webView mouseUpAtPoint:playButtonClickPoint];
327     runUntilReceivesAutoplayEvent(kWKAutoplayEventDidPlayMediaPreventedFromAutoplaying);
328
329     receivedAutoplayEvent = std::nullopt;
330     [webView loadHTMLString:@"" baseURL:nil];
331
332     NSURLRequest *noAutoplayRequest = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"no-autoplay-with-controls" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
333     [webView loadRequest:noAutoplayRequest];
334     [webView waitForMessage:@"loaded"];
335     [webView mouseDownAtPoint:playButtonClickPoint simulatePressure:NO];
336     [webView mouseUpAtPoint:playButtonClickPoint];
337     [webView waitForMessage:@"played"];
338     ASSERT_TRUE(receivedAutoplayEvent == std::nullopt);
339 }
340
341 TEST(WebKit2, WebsitePoliciesPlayingWithoutInterference)
342 {
343     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
344     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 336, 276) configuration:configuration.get()]);
345
346     auto delegate = adoptNS([[AutoplayPoliciesDelegate alloc] init]);
347     [delegate setAutoplayPolicyForURL:^(NSURL *) {
348         return _WKWebsiteAutoplayPolicyAllow;
349     }];
350     [webView setNavigationDelegate:delegate.get()];
351
352     WKPageUIClientV9 uiClient;
353     memset(&uiClient, 0, sizeof(uiClient));
354
355     uiClient.base.version = 9;
356     uiClient.handleAutoplayEvent = handleAutoplayEvent;
357
358     WKPageSetPageUIClient([webView _pageForTesting], &uiClient.base);
359
360     receivedAutoplayEvent = std::nullopt;
361     NSURLRequest *jsPlayRequest = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"js-play-with-controls" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
362     [webView loadRequest:jsPlayRequest];
363     [webView waitForMessage:@"playing"];
364
365     ASSERT_TRUE(receivedAutoplayEvent == std::nullopt);
366
367     [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
368     runUntilReceivesAutoplayEvent(kWKAutoplayEventDidEndMediaPlaybackWithoutUserInterference);
369
370     receivedAutoplayEvent = std::nullopt;
371     [webView loadRequest:jsPlayRequest];
372     [webView waitForMessage:@"ended"];
373     runUntilReceivesAutoplayEvent(kWKAutoplayEventDidEndMediaPlaybackWithoutUserInterference);
374 }
375
376 TEST(WebKit2, WebsitePoliciesUserInterferenceWithPlaying)
377 {
378     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
379     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 336, 276) configuration:configuration.get()]);
380
381     auto delegate = adoptNS([[AutoplayPoliciesDelegate alloc] init]);
382     [delegate setAutoplayPolicyForURL:^(NSURL *) {
383         return _WKWebsiteAutoplayPolicyAllow;
384     }];
385     [webView setNavigationDelegate:delegate.get()];
386
387     WKPageUIClientV9 uiClient;
388     memset(&uiClient, 0, sizeof(uiClient));
389
390     uiClient.base.version = 9;
391     uiClient.handleAutoplayEvent = handleAutoplayEvent;
392
393     WKPageSetPageUIClient([webView _pageForTesting], &uiClient.base);
394
395     receivedAutoplayEvent = std::nullopt;
396     NSURLRequest *jsPlayRequest = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"js-play-with-controls" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
397     [webView loadRequest:jsPlayRequest];
398     [webView waitForMessage:@"playing"];
399     ASSERT_TRUE(receivedAutoplayEvent == std::nullopt);
400
401     WKPageSetMuted([webView _pageForTesting], kWKMediaAudioMuted);
402     runUntilReceivesAutoplayEvent(kWKAutoplayEventUserDidInterfereWithPlayback);
403
404     receivedAutoplayEvent = std::nullopt;
405     [webView loadRequest:jsPlayRequest];
406     [webView waitForMessage:@"playing"];
407     ASSERT_TRUE(receivedAutoplayEvent == std::nullopt);
408
409     const NSPoint muteButtonClickPoint = NSMakePoint(80, 256);
410     [webView mouseDownAtPoint:muteButtonClickPoint simulatePressure:NO];
411     [webView mouseUpAtPoint:muteButtonClickPoint];
412     runUntilReceivesAutoplayEvent(kWKAutoplayEventUserDidInterfereWithPlayback);
413
414     receivedAutoplayEvent = std::nullopt;
415     [webView loadRequest:jsPlayRequest];
416     [webView waitForMessage:@"playing"];
417     ASSERT_TRUE(receivedAutoplayEvent == std::nullopt);
418
419     const NSPoint playButtonClickPoint = NSMakePoint(20, 256);
420     [webView mouseDownAtPoint:playButtonClickPoint simulatePressure:NO];
421     [webView mouseUpAtPoint:playButtonClickPoint];
422     runUntilReceivesAutoplayEvent(kWKAutoplayEventUserDidInterfereWithPlayback);
423 }
424
425 TEST(WebKit2, WebsitePoliciesUserNeverPlayedMediaPreventedFromPlaying)
426 {
427     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
428     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 336, 276) configuration:configuration.get()]);
429
430     auto delegate = adoptNS([[AutoplayPoliciesDelegate alloc] init]);
431     [delegate setAutoplayPolicyForURL:^(NSURL *) {
432         return _WKWebsiteAutoplayPolicyDeny;
433     }];
434     [webView setNavigationDelegate:delegate.get()];
435
436     WKPageUIClientV9 uiClient;
437     memset(&uiClient, 0, sizeof(uiClient));
438
439     uiClient.base.version = 9;
440     uiClient.handleAutoplayEvent = handleAutoplayEvent;
441
442     WKPageSetPageUIClient([webView _pageForTesting], &uiClient.base);
443
444     receivedAutoplayEvent = std::nullopt;
445     NSURLRequest *jsPlayRequest = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"js-play-with-controls" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
446     [webView loadRequest:jsPlayRequest];
447     [webView waitForMessage:@"loaded"];
448     runUntilReceivesAutoplayEvent(kWKAutoplayEventDidPreventFromAutoplaying);
449
450     [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
451     runUntilReceivesAutoplayEvent(kWKAutoplayEventUserNeverPlayedMediaPreventedFromPlaying);
452 }
453
454 TEST(WebKit2, WebsitePoliciesAutoplayQuirks)
455 {
456     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
457     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
458
459     auto delegate = adoptNS([[AutoplayPoliciesDelegate alloc] init]);
460     [webView setNavigationDelegate:delegate.get()];
461
462     WKRetainPtr<WKPreferencesRef> preferences(AdoptWK, WKPreferencesCreate());
463     WKPreferencesSetNeedsSiteSpecificQuirks(preferences.get(), true);
464     WKPageGroupSetPreferences(WKPageGetPageGroup([webView _pageForTesting]), preferences.get());
465
466     NSURLRequest *requestWithAudio = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"autoplay-check" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
467
468     [delegate setAllowsAutoplayQuirks:YES];
469     [delegate setAutoplayPolicyForURL:^(NSURL *) {
470         return _WKWebsiteAutoplayPolicyDeny;
471     }];
472     [webView loadRequest:requestWithAudio];
473     [webView waitForMessage:@"did-not-play"];
474     [webView waitForMessage:@"on-pause"];
475 }
476 #endif
477
478 #endif