Loosen audio playback restrictions when the volume is 0.
[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     NSURLRequest *requestWithoutVolume = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"autoplay-zero-volume-check" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
218     
219     [delegate setAutoplayPolicyForURL:^(NSURL *) {
220         return _WKWebsiteAutoplayPolicyAllowWithoutSound;
221     }];
222     [webView loadRequest:requestWithAudio];
223     [webView waitForMessage:@"did-not-play"];
224
225     [webView loadRequest:requestWithoutAudio];
226     [webView waitForMessage:@"autoplayed"];
227
228     [webView loadRequest:requestWithoutVolume];
229     [webView waitForMessage:@"autoplayed"];
230
231     [delegate setAutoplayPolicyForURL:^(NSURL *) {
232         return _WKWebsiteAutoplayPolicyDeny;
233     }];
234     [webView loadRequest:requestWithoutVolume];
235     [webView waitForMessage:@"did-not-play"];
236
237     [webView loadRequest:requestWithAudio];
238     [webView waitForMessage:@"did-not-play"];
239
240     // Test updating website policies.
241     _WKWebsitePolicies *websitePolicies = [[[_WKWebsitePolicies alloc] init] autorelease];
242     websitePolicies.autoplayPolicy = _WKWebsiteAutoplayPolicyAllow;
243     [webView _updateWebsitePolicies:websitePolicies];
244     [webView stringByEvaluatingJavaScript:@"playVideo()"];
245     [webView waitForMessage:@"autoplayed"];
246
247     [webView loadRequest:requestWithoutAudio];
248     [webView waitForMessage:@"did-not-play"];
249
250     [delegate setAutoplayPolicyForURL:^(NSURL *) {
251         return _WKWebsiteAutoplayPolicyAllow;
252     }];
253     [webView loadRequest:requestWithAudio];
254     [webView waitForMessage:@"autoplayed"];
255
256     [webView loadRequest:requestWithoutAudio];
257     [webView waitForMessage:@"autoplayed"];
258
259     [webView loadRequest:requestWithoutVolume];
260     [webView waitForMessage:@"autoplayed"];
261
262     NSURLRequest *requestWithAudioInIFrame = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"autoplay-check-in-iframe" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
263
264     // If the top-level document allows autoplay, any iframes should also autoplay.
265     [delegate setAutoplayPolicyForURL:^(NSURL *url) {
266         if ([url.lastPathComponent isEqualToString:@"autoplay-check-in-iframe.html"])
267             return _WKWebsiteAutoplayPolicyAllow;
268         return _WKWebsiteAutoplayPolicyDeny;
269     }];
270
271     [webView loadRequest:requestWithAudioInIFrame];
272     [webView waitForMessage:@"autoplayed"];
273
274     // If the top-level document disallows autoplay, any iframes should also not autoplay.
275     [delegate setAutoplayPolicyForURL:^(NSURL *url) {
276         if ([url.lastPathComponent isEqualToString:@"autoplay-check-in-iframe.html"])
277             return _WKWebsiteAutoplayPolicyDeny;
278         return _WKWebsiteAutoplayPolicyAllow;
279     }];
280
281     [webView loadRequest:requestWithAudioInIFrame];
282     [webView waitForMessage:@"did-not-play"];
283 }
284
285 #if PLATFORM(MAC)
286 static void handleAutoplayEvent(WKPageRef page, WKAutoplayEvent event, const void* clientInfo)
287 {
288     receivedAutoplayEvent = event;
289 }
290
291 static void runUntilReceivesAutoplayEvent(WKAutoplayEvent event)
292 {
293     while (!receivedAutoplayEvent || *receivedAutoplayEvent != event)
294         CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, true);
295 }
296
297 TEST(WebKit2, WebsitePoliciesPlayAfterPreventedAutoplay)
298 {
299     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
300     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 336, 276) configuration:configuration.get()]);
301
302     auto delegate = adoptNS([[AutoplayPoliciesDelegate alloc] init]);
303     [delegate setAutoplayPolicyForURL:^(NSURL *) {
304         return _WKWebsiteAutoplayPolicyDeny;
305     }];
306     [webView setNavigationDelegate:delegate.get()];
307
308     WKPageUIClientV9 uiClient;
309     memset(&uiClient, 0, sizeof(uiClient));
310
311     uiClient.base.version = 9;
312     uiClient.handleAutoplayEvent = handleAutoplayEvent;
313
314     WKPageSetPageUIClient([webView _pageForTesting], &uiClient.base);
315     NSPoint playButtonClickPoint = NSMakePoint(20, 256);
316
317     receivedAutoplayEvent = std::nullopt;
318     NSURLRequest *jsPlayRequest = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"js-play-with-controls" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
319     [webView loadRequest:jsPlayRequest];
320     [webView waitForMessage:@"loaded"];
321     runUntilReceivesAutoplayEvent(kWKAutoplayEventDidPreventFromAutoplaying);
322
323     [webView mouseDownAtPoint:playButtonClickPoint simulatePressure:NO];
324     [webView mouseUpAtPoint:playButtonClickPoint];
325     runUntilReceivesAutoplayEvent(kWKAutoplayEventDidPlayMediaPreventedFromAutoplaying);
326
327     receivedAutoplayEvent = std::nullopt;
328     [webView loadHTMLString:@"" baseURL:nil];
329
330     NSURLRequest *autoplayRequest = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"autoplay-with-controls" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
331     [webView loadRequest:autoplayRequest];
332     [webView waitForMessage:@"loaded"];
333     runUntilReceivesAutoplayEvent(kWKAutoplayEventDidPreventFromAutoplaying);
334
335     [webView mouseDownAtPoint:playButtonClickPoint simulatePressure:NO];
336     [webView mouseUpAtPoint:playButtonClickPoint];
337     runUntilReceivesAutoplayEvent(kWKAutoplayEventDidPlayMediaPreventedFromAutoplaying);
338
339     receivedAutoplayEvent = std::nullopt;
340     [webView loadHTMLString:@"" baseURL:nil];
341
342     NSURLRequest *noAutoplayRequest = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"no-autoplay-with-controls" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
343     [webView loadRequest:noAutoplayRequest];
344     [webView waitForMessage:@"loaded"];
345     [webView mouseDownAtPoint:playButtonClickPoint simulatePressure:NO];
346     [webView mouseUpAtPoint:playButtonClickPoint];
347     [webView waitForMessage:@"played"];
348     ASSERT_TRUE(receivedAutoplayEvent == std::nullopt);
349 }
350
351 TEST(WebKit2, WebsitePoliciesPlayingWithoutInterference)
352 {
353     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
354     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 336, 276) configuration:configuration.get()]);
355
356     auto delegate = adoptNS([[AutoplayPoliciesDelegate alloc] init]);
357     [delegate setAutoplayPolicyForURL:^(NSURL *) {
358         return _WKWebsiteAutoplayPolicyAllow;
359     }];
360     [webView setNavigationDelegate:delegate.get()];
361
362     WKPageUIClientV9 uiClient;
363     memset(&uiClient, 0, sizeof(uiClient));
364
365     uiClient.base.version = 9;
366     uiClient.handleAutoplayEvent = handleAutoplayEvent;
367
368     WKPageSetPageUIClient([webView _pageForTesting], &uiClient.base);
369
370     receivedAutoplayEvent = std::nullopt;
371     NSURLRequest *jsPlayRequest = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"js-play-with-controls" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
372     [webView loadRequest:jsPlayRequest];
373     [webView waitForMessage:@"playing"];
374
375     ASSERT_TRUE(receivedAutoplayEvent == std::nullopt);
376
377     [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
378     runUntilReceivesAutoplayEvent(kWKAutoplayEventDidEndMediaPlaybackWithoutUserInterference);
379
380     receivedAutoplayEvent = std::nullopt;
381     [webView loadRequest:jsPlayRequest];
382     [webView waitForMessage:@"ended"];
383     runUntilReceivesAutoplayEvent(kWKAutoplayEventDidEndMediaPlaybackWithoutUserInterference);
384 }
385
386 TEST(WebKit2, WebsitePoliciesUserInterferenceWithPlaying)
387 {
388     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
389     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 336, 276) configuration:configuration.get()]);
390
391     auto delegate = adoptNS([[AutoplayPoliciesDelegate alloc] init]);
392     [delegate setAutoplayPolicyForURL:^(NSURL *) {
393         return _WKWebsiteAutoplayPolicyAllow;
394     }];
395     [webView setNavigationDelegate:delegate.get()];
396
397     WKPageUIClientV9 uiClient;
398     memset(&uiClient, 0, sizeof(uiClient));
399
400     uiClient.base.version = 9;
401     uiClient.handleAutoplayEvent = handleAutoplayEvent;
402
403     WKPageSetPageUIClient([webView _pageForTesting], &uiClient.base);
404
405     receivedAutoplayEvent = std::nullopt;
406     NSURLRequest *jsPlayRequest = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"js-play-with-controls" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
407     [webView loadRequest:jsPlayRequest];
408     [webView waitForMessage:@"playing"];
409     ASSERT_TRUE(receivedAutoplayEvent == std::nullopt);
410
411     WKPageSetMuted([webView _pageForTesting], kWKMediaAudioMuted);
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 muteButtonClickPoint = NSMakePoint(80, 256);
420     [webView mouseDownAtPoint:muteButtonClickPoint simulatePressure:NO];
421     [webView mouseUpAtPoint:muteButtonClickPoint];
422     runUntilReceivesAutoplayEvent(kWKAutoplayEventUserDidInterfereWithPlayback);
423
424     receivedAutoplayEvent = std::nullopt;
425     [webView loadRequest:jsPlayRequest];
426     [webView waitForMessage:@"playing"];
427     ASSERT_TRUE(receivedAutoplayEvent == std::nullopt);
428
429     const NSPoint playButtonClickPoint = NSMakePoint(20, 256);
430     [webView mouseDownAtPoint:playButtonClickPoint simulatePressure:NO];
431     [webView mouseUpAtPoint:playButtonClickPoint];
432     runUntilReceivesAutoplayEvent(kWKAutoplayEventUserDidInterfereWithPlayback);
433 }
434
435 TEST(WebKit2, WebsitePoliciesUserNeverPlayedMediaPreventedFromPlaying)
436 {
437     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
438     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 336, 276) configuration:configuration.get()]);
439
440     auto delegate = adoptNS([[AutoplayPoliciesDelegate alloc] init]);
441     [delegate setAutoplayPolicyForURL:^(NSURL *) {
442         return _WKWebsiteAutoplayPolicyDeny;
443     }];
444     [webView setNavigationDelegate:delegate.get()];
445
446     WKPageUIClientV9 uiClient;
447     memset(&uiClient, 0, sizeof(uiClient));
448
449     uiClient.base.version = 9;
450     uiClient.handleAutoplayEvent = handleAutoplayEvent;
451
452     WKPageSetPageUIClient([webView _pageForTesting], &uiClient.base);
453
454     receivedAutoplayEvent = std::nullopt;
455     NSURLRequest *jsPlayRequest = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"js-play-with-controls" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
456     [webView loadRequest:jsPlayRequest];
457     [webView waitForMessage:@"loaded"];
458     runUntilReceivesAutoplayEvent(kWKAutoplayEventDidPreventFromAutoplaying);
459
460     [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
461     runUntilReceivesAutoplayEvent(kWKAutoplayEventUserNeverPlayedMediaPreventedFromPlaying);
462 }
463
464 TEST(WebKit2, WebsitePoliciesAutoplayQuirks)
465 {
466     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
467     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
468
469     auto delegate = adoptNS([[AutoplayPoliciesDelegate alloc] init]);
470     [webView setNavigationDelegate:delegate.get()];
471
472     WKRetainPtr<WKPreferencesRef> preferences(AdoptWK, WKPreferencesCreate());
473     WKPreferencesSetNeedsSiteSpecificQuirks(preferences.get(), true);
474     WKPageGroupSetPreferences(WKPageGetPageGroup([webView _pageForTesting]), preferences.get());
475
476     NSURLRequest *requestWithAudio = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"autoplay-check" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
477
478     [delegate setAllowsAutoplayQuirks:YES];
479     [delegate setAutoplayPolicyForURL:^(NSURL *) {
480         return _WKWebsiteAutoplayPolicyDeny;
481     }];
482     [webView loadRequest:requestWithAudio];
483     [webView waitForMessage:@"did-not-play"];
484     [webView waitForMessage:@"on-pause"];
485 }
486 #endif
487
488 #endif