f2e3a66c7880103c8084a9d8945ff408fdb2e316
[WebKit-https.git] / Tools / WebKitTestRunner / cocoa / TestControllerCocoa.mm
1 /*
2  * Copyright (C) 2015-2020 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 #import "config.h"
27 #import "TestController.h"
28
29 #import "CrashReporterInfo.h"
30 #import "PlatformWebView.h"
31 #import "StringFunctions.h"
32 #import "TestInvocation.h"
33 #import "TestRunnerWKWebView.h"
34 #import "TestWebsiteDataStoreDelegate.h"
35 #import <Foundation/Foundation.h>
36 #import <Security/SecItem.h>
37 #import <WebKit/WKContextConfigurationRef.h>
38 #import <WebKit/WKContextPrivate.h>
39 #import <WebKit/WKPreferencesRefPrivate.h>
40 #import <WebKit/WKProcessPoolPrivate.h>
41 #import <WebKit/WKStringCF.h>
42 #import <WebKit/WKUserContentControllerPrivate.h>
43 #import <WebKit/WKWebView.h>
44 #import <WebKit/WKWebViewConfiguration.h>
45 #import <WebKit/WKWebViewConfigurationPrivate.h>
46 #import <WebKit/WKWebViewPrivate.h>
47 #import <WebKit/WKWebViewPrivateForTesting.h>
48 #import <WebKit/WKWebsiteDataRecordPrivate.h>
49 #import <WebKit/WKWebsiteDataStorePrivate.h>
50 #import <WebKit/WKWebsiteDataStoreRef.h>
51 #import <WebKit/_WKApplicationManifest.h>
52 #import <WebKit/_WKUserContentExtensionStore.h>
53 #import <WebKit/_WKUserContentExtensionStorePrivate.h>
54 #import <wtf/MainThread.h>
55 #import <wtf/spi/cocoa/SecuritySPI.h>
56
57 namespace WTR {
58
59 static WKWebViewConfiguration *globalWebViewConfiguration;
60 static TestWebsiteDataStoreDelegate *globalWebsiteDataStoreDelegateClient;
61
62 void initializeWebViewConfiguration(const char* libraryPath, WKStringRef injectedBundlePath, WKContextRef context, WKContextConfigurationRef contextConfiguration)
63 {
64     [globalWebViewConfiguration release];
65     globalWebViewConfiguration = [[WKWebViewConfiguration alloc] init];
66
67     globalWebViewConfiguration.processPool = (__bridge WKProcessPool *)context;
68     globalWebViewConfiguration.websiteDataStore = (__bridge WKWebsiteDataStore *)TestController::defaultWebsiteDataStore();
69     globalWebViewConfiguration._allowUniversalAccessFromFileURLs = YES;
70     globalWebViewConfiguration._allowTopNavigationToDataURLs = YES;
71     globalWebViewConfiguration._applePayEnabled = YES;
72
73     WKContextSetStorageAccessAPIEnabled(context, true);
74
75     [globalWebViewConfiguration.websiteDataStore _setResourceLoadStatisticsEnabled:YES];
76
77     [globalWebsiteDataStoreDelegateClient release];
78     globalWebsiteDataStoreDelegateClient = [[TestWebsiteDataStoreDelegate alloc] init];
79     [globalWebViewConfiguration.websiteDataStore set_delegate:globalWebsiteDataStoreDelegateClient];
80
81 #if PLATFORM(IOS_FAMILY)
82     globalWebViewConfiguration.allowsInlineMediaPlayback = YES;
83     globalWebViewConfiguration._inlineMediaPlaybackRequiresPlaysInlineAttribute = NO;
84     globalWebViewConfiguration._invisibleAutoplayNotPermitted = NO;
85     globalWebViewConfiguration._mediaDataLoadsAutomatically = YES;
86     globalWebViewConfiguration.requiresUserActionForMediaPlayback = NO;
87 #endif
88     globalWebViewConfiguration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
89
90 #if USE(SYSTEM_PREVIEW)
91     globalWebViewConfiguration._systemPreviewEnabled = YES;
92 #endif
93 }
94
95 void TestController::cocoaPlatformInitialize()
96 {
97     const char* dumpRenderTreeTemp = libraryPathForTesting();
98     if (!dumpRenderTreeTemp)
99         return;
100
101     String resourceLoadStatisticsFolder = String(dumpRenderTreeTemp) + '/' + "ResourceLoadStatistics";
102     [[NSFileManager defaultManager] createDirectoryAtPath:resourceLoadStatisticsFolder withIntermediateDirectories:YES attributes:nil error: nil];
103     String fullBrowsingSessionResourceLog = resourceLoadStatisticsFolder + '/' + "full_browsing_session_resourceLog.plist";
104     NSDictionary *resourceLogPlist = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt:1], @"version", nil];
105     if (![resourceLogPlist writeToFile:fullBrowsingSessionResourceLog atomically:YES])
106         WTFCrash();
107     [resourceLogPlist release];
108 }
109
110 WKContextRef TestController::platformContext()
111 {
112     return (__bridge WKContextRef)globalWebViewConfiguration.processPool;
113 }
114
115 WKPreferencesRef TestController::platformPreferences()
116 {
117     return (__bridge WKPreferencesRef)globalWebViewConfiguration.preferences;
118 }
119
120 void TestController::platformAddTestOptions(TestOptions& options) const
121 {
122     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EnableProcessSwapOnNavigation"])
123         options.contextOptions.enableProcessSwapOnNavigation = true;
124     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EnableProcessSwapOnWindowOpen"])
125         options.contextOptions.enableProcessSwapOnWindowOpen = true;
126 }
127
128 void TestController::platformCreateWebView(WKPageConfigurationRef, const TestOptions& options)
129 {
130     RetainPtr<WKWebViewConfiguration> copiedConfiguration = adoptNS([globalWebViewConfiguration copy]);
131
132 #if PLATFORM(IOS_FAMILY)
133     if (options.useDataDetection)
134         [copiedConfiguration setDataDetectorTypes:WKDataDetectorTypeAll];
135     if (options.ignoresViewportScaleLimits)
136         [copiedConfiguration setIgnoresViewportScaleLimits:YES];
137     if (options.useCharacterSelectionGranularity)
138         [copiedConfiguration setSelectionGranularity:WKSelectionGranularityCharacter];
139 #else
140     [copiedConfiguration _setServiceControlsEnabled:options.enableServiceControls];
141 #endif
142
143     if (options.enableAttachmentElement)
144         [copiedConfiguration _setAttachmentElementEnabled:YES];
145
146     if (options.enableColorFilter)
147         [copiedConfiguration _setColorFilterEnabled:YES];
148
149     if (options.enableEditableImages)
150         [copiedConfiguration _setEditableImagesEnabled:YES];
151
152     if (options.enableUndoManagerAPI)
153         [copiedConfiguration _setUndoManagerAPIEnabled:YES];
154
155     if (options.useEphemeralSession) {
156         auto ephemeralWebsiteDataStore = [WKWebsiteDataStore nonPersistentDataStore];
157         [ephemeralWebsiteDataStore _setResourceLoadStatisticsEnabled:YES];
158         [copiedConfiguration setWebsiteDataStore:ephemeralWebsiteDataStore];
159     }
160
161     [copiedConfiguration _setAllowTopNavigationToDataURLs:options.allowTopNavigationToDataURLs];
162
163     configureContentMode(copiedConfiguration.get(), options);
164
165     if (options.applicationManifest.length()) {
166         auto manifestPath = [NSString stringWithUTF8String:options.applicationManifest.c_str()];
167         NSString *text = [NSString stringWithContentsOfFile:manifestPath usedEncoding:nullptr error:nullptr];
168         [copiedConfiguration _setApplicationManifest:[_WKApplicationManifest applicationManifestFromJSON:text manifestURL:nil documentURL:nil]];
169     }
170
171     m_mainWebView = makeUnique<PlatformWebView>(copiedConfiguration.get(), options);
172     finishCreatingPlatformWebView(m_mainWebView.get(), options);
173
174     if (options.punchOutWhiteBackgroundsInDarkMode)
175         m_mainWebView->setDrawsBackground(false);
176
177     if (options.editable)
178         m_mainWebView->setEditable(true);
179
180     m_mainWebView->platformView().allowsLinkPreview = options.allowsLinkPreview;
181 }
182
183 PlatformWebView* TestController::platformCreateOtherPage(PlatformWebView* parentView, WKPageConfigurationRef, const TestOptions& options)
184 {
185     WKWebViewConfiguration *newConfiguration = [[globalWebViewConfiguration copy] autorelease];
186     newConfiguration._relatedWebView = static_cast<WKWebView*>(parentView->platformView());
187     PlatformWebView* view = new PlatformWebView(newConfiguration, options);
188     finishCreatingPlatformWebView(view, options);
189     return view;
190 }
191
192 // Code that needs to run after TestController::m_mainWebView is initialized goes into this function.
193 void TestController::finishCreatingPlatformWebView(PlatformWebView* view, const TestOptions& options)
194 {
195 #if PLATFORM(MAC)
196     if (options.shouldShowWebView)
197         [view->platformWindow() orderFront:nil];
198     else
199         [view->platformWindow() orderBack:nil];
200 #endif
201 }
202
203 WKContextRef TestController::platformAdjustContext(WKContextRef context, WKContextConfigurationRef contextConfiguration)
204 {
205     initializeWebViewConfiguration(libraryPathForTesting(), injectedBundlePath(), context, contextConfiguration);
206     return (__bridge WKContextRef)globalWebViewConfiguration.processPool;
207 }
208
209 void TestController::platformRunUntil(bool& done, WTF::Seconds timeout)
210 {
211     NSDate *endDate = (timeout > 0_s) ? [NSDate dateWithTimeIntervalSinceNow:timeout.seconds()] : [NSDate distantFuture];
212
213     while (!done && [endDate compare:[NSDate date]] == NSOrderedDescending)
214         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:endDate];
215 }
216
217 static NSCalendar *swizzledCalendar()
218 {
219     NSCalendar *calendar = [NSCalendar calendarWithIdentifier:TestController::singleton().overriddenCalendarIdentifier()];
220     calendar.locale = [NSLocale localeWithLocaleIdentifier:TestController::singleton().overriddenCalendarLocaleIdentifier()];
221     return calendar;
222 }
223
224 NSString *TestController::overriddenCalendarIdentifier() const
225 {
226     return m_overriddenCalendarAndLocaleIdentifiers.first.get();
227 }
228
229 NSString *TestController::overriddenCalendarLocaleIdentifier() const
230 {
231     return m_overriddenCalendarAndLocaleIdentifiers.second.get();
232 }
233
234 void TestController::setDefaultCalendarType(NSString *identifier, NSString *localeIdentifier)
235 {
236     m_overriddenCalendarAndLocaleIdentifiers = { identifier, localeIdentifier };
237     if (!m_calendarSwizzler)
238         m_calendarSwizzler = makeUnique<ClassMethodSwizzler>([NSCalendar class], @selector(currentCalendar), reinterpret_cast<IMP>(swizzledCalendar));
239 }
240
241 void TestController::resetContentExtensions()
242 {
243     __block bool doneRemoving = false;
244     [[_WKUserContentExtensionStore defaultStore] removeContentExtensionForIdentifier:@"TestContentExtensions" completionHandler:^(NSError *error) {
245         doneRemoving = true;
246     }];
247     platformRunUntil(doneRemoving, noTimeout);
248     [[_WKUserContentExtensionStore defaultStore] _removeAllContentExtensions];
249
250     if (auto* webView = mainWebView()) {
251         TestRunnerWKWebView *platformView = webView->platformView();
252         [platformView.configuration.userContentController _removeAllUserContentFilters];
253     }
254 }
255
256 void TestController::setApplicationBundleIdentifier(const String& bundleIdentifier)
257 {
258     if (bundleIdentifier.isEmpty())
259         return;
260     
261     [TestRunnerWKWebView _setApplicationBundleIdentifier:(NSString *)bundleIdentifier.createCFString().get()];
262 }
263
264 void TestController::clearApplicationBundleIdentifierTestingOverride()
265 {
266     [TestRunnerWKWebView _clearApplicationBundleIdentifierTestingOverride];
267     m_hasSetApplicationBundleIdentifier = false;
268 }
269
270 void TestController::cocoaResetStateToConsistentValues(const TestOptions& options)
271 {
272     m_calendarSwizzler = nullptr;
273     m_overriddenCalendarAndLocaleIdentifiers = { nil, nil };
274     
275     if (auto* webView = mainWebView()) {
276         TestRunnerWKWebView *platformView = webView->platformView();
277         platformView._viewScale = 1;
278         platformView._minimumEffectiveDeviceWidth = 0;
279         [platformView _setContinuousSpellCheckingEnabledForTesting:options.shouldShowSpellCheckingDots];
280         [platformView resetInteractionCallbacks];
281     }
282
283     [globalWebsiteDataStoreDelegateClient setAllowRaisingQuota:YES];
284 }
285
286 void TestController::platformWillRunTest(const TestInvocation& testInvocation)
287 {
288     setCrashReportApplicationSpecificInformationToURL(testInvocation.url());
289 }
290
291 static NSString * const WebArchivePboardType = @"Apple Web Archive pasteboard type";
292 static NSString * const WebSubresourcesKey = @"WebSubresources";
293 static NSString * const WebSubframeArchivesKey = @"WebResourceMIMEType like 'image*'";
294
295 unsigned TestController::imageCountInGeneralPasteboard() const
296 {
297 #if PLATFORM(MAC)
298     NSData *data = [[NSPasteboard generalPasteboard] dataForType:WebArchivePboardType];
299 #elif PLATFORM(IOS_FAMILY)
300     NSData *data = [[UIPasteboard generalPasteboard] valueForPasteboardType:WebArchivePboardType];
301 #endif
302     if (!data)
303         return 0;
304     
305     NSError *error = nil;
306     id webArchive = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:&error];
307     if (error) {
308         NSLog(@"Encountered error while serializing Web Archive pasteboard data: %@", error);
309         return 0;
310     }
311     
312     NSArray *subItems = [NSArray arrayWithArray:[webArchive objectForKey:WebSubresourcesKey]];
313     NSPredicate *predicate = [NSPredicate predicateWithFormat:WebSubframeArchivesKey];
314     NSArray *imagesArray = [subItems filteredArrayUsingPredicate:predicate];
315     
316     if (!imagesArray)
317         return 0;
318     
319     return imagesArray.count;
320 }
321
322 void TestController::removeAllSessionCredentials()
323 {
324     auto types = adoptNS([[NSSet alloc] initWithObjects:_WKWebsiteDataTypeCredentials, nil]);
325     [globalWebViewConfiguration.websiteDataStore removeDataOfTypes:types.get() modifiedSince:[NSDate distantPast] completionHandler:^() {
326         m_currentInvocation->didRemoveAllSessionCredentials();
327     }];
328 }
329
330 void TestController::getAllStorageAccessEntries()
331 {
332     auto* parentView = mainWebView();
333     if (!parentView)
334         return;
335
336     [globalWebViewConfiguration.websiteDataStore _getAllStorageAccessEntriesFor:parentView->platformView() completionHandler:^(NSArray<NSString *> *nsDomains) {
337         Vector<String> domains;
338         domains.reserveInitialCapacity(nsDomains.count);
339         for (NSString *domain : nsDomains)
340             domains.uncheckedAppend(domain);
341         m_currentInvocation->didReceiveAllStorageAccessEntries(domains);
342     }];
343 }
344
345 void TestController::getPrevalentDomains()
346 {
347     auto* parentView = mainWebView();
348     if (!parentView)
349         return;
350     
351     [globalWebViewConfiguration.websiteDataStore _getPrevalentDomainsFor:parentView->platformView() completionHandler:^(NSArray<NSString *> *nsDomains) {
352         Vector<String> domains;
353         domains.reserveInitialCapacity(nsDomains.count);
354         for (NSString *domain : nsDomains)
355             domains.uncheckedAppend(domain);
356         m_currentInvocation->didReceivePrevalentDomains(WTFMove(domains));
357     }];
358 }
359
360 void TestController::clearPrevalentDomains()
361 {
362     auto* parentView = mainWebView();
363     if (!parentView)
364         return;
365
366     [globalWebViewConfiguration.websiteDataStore _clearPrevalentDomainsFor:parentView->platformView()];
367 }
368
369 void TestController::getWebViewCategory()
370 {
371     auto* parentView = mainWebView();
372     if (!parentView)
373         return;
374
375     [globalWebViewConfiguration.websiteDataStore _getWebViewCategoryFor:parentView->platformView() completionHandler:^(_WKWebViewCategory webViewCategory) {
376         String category;
377         switch (webViewCategory) {
378         case _WKWebViewCategoryAppBoundDomain:
379             category = "AppBoundDomain";
380             break;
381         case _WKWebViewCategoryHybridApp:
382             category = "HybridApp";
383             break;
384         case _WKWebViewCategoryInAppBrowser:
385             category = "InAppBrowser";
386             break;
387         case _WKWebViewCategoryWebBrowser:
388             category = "WebBrowser";
389             break;
390         }
391         m_currentInvocation->didReceiveWebViewCategory(category);
392     }];
393 }
394
395 void TestController::injectUserScript(WKStringRef script)
396 {
397     auto userScript = adoptNS([[WKUserScript alloc] initWithSource: toWTFString(script) injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO]);
398
399     [[globalWebViewConfiguration userContentController] addUserScript: userScript.get()];
400 }
401
402 void TestController::addTestKeyToKeychain(const String& privateKeyBase64, const String& attrLabel, const String& applicationTagBase64)
403 {
404     NSDictionary* options = @{
405         (id)kSecAttrKeyType: (id)kSecAttrKeyTypeECSECPrimeRandom,
406         (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPrivate,
407         (id)kSecAttrKeySizeInBits: @256
408     };
409     CFErrorRef errorRef = nullptr;
410     auto key = adoptCF(SecKeyCreateWithData(
411         (__bridge CFDataRef)adoptNS([[NSData alloc] initWithBase64EncodedString:privateKeyBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters]).get(),
412         (__bridge CFDictionaryRef)options,
413         &errorRef
414     ));
415     ASSERT(!errorRef);
416
417     NSDictionary* addQuery = @{
418         (id)kSecValueRef: (id)key.get(),
419         (id)kSecClass: (id)kSecClassKey,
420         (id)kSecAttrLabel: attrLabel,
421         (id)kSecAttrApplicationTag: adoptNS([[NSData alloc] initWithBase64EncodedString:applicationTagBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters]).get(),
422         (id)kSecAttrAccessible: (id)kSecAttrAccessibleAfterFirstUnlock,
423 #if HAVE(DATA_PROTECTION_KEYCHAIN)
424         (id)kSecUseDataProtectionKeychain: @YES
425 #else
426         (id)kSecAttrNoLegacy: @YES
427 #endif
428     };
429     OSStatus status = SecItemAdd((__bridge CFDictionaryRef)addQuery, NULL);
430     ASSERT_UNUSED(status, !status);
431 }
432
433 void TestController::cleanUpKeychain(const String& attrLabel, const String& applicationTagBase64)
434 {
435     auto deleteQuery = adoptNS([[NSMutableDictionary alloc] init]);
436     [deleteQuery setObject:(id)kSecClassKey forKey:(id)kSecClass];
437     [deleteQuery setObject:attrLabel forKey:(id)kSecAttrLabel];
438 #if HAVE(DATA_PROTECTION_KEYCHAIN)
439     [deleteQuery setObject:@YES forKey:(id)kSecUseDataProtectionKeychain];
440 #else
441     [deleteQuery setObject:@YES forKey:(id)kSecAttrNoLegacy];
442 #endif
443     if (!!applicationTagBase64)
444         [deleteQuery setObject:adoptNS([[NSData alloc] initWithBase64EncodedString:applicationTagBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters]).get() forKey:(id)kSecAttrApplicationTag];
445
446     SecItemDelete((__bridge CFDictionaryRef)deleteQuery.get());
447 }
448
449 bool TestController::keyExistsInKeychain(const String& attrLabel, const String& applicationTagBase64)
450 {
451     NSDictionary *query = @{
452         (id)kSecClass: (id)kSecClassKey,
453         (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPrivate,
454         (id)kSecAttrLabel: attrLabel,
455         (id)kSecAttrApplicationTag: adoptNS([[NSData alloc] initWithBase64EncodedString:applicationTagBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters]).get(),
456 #if HAVE(DATA_PROTECTION_KEYCHAIN)
457         (id)kSecUseDataProtectionKeychain: @YES
458 #else
459         (id)kSecAttrNoLegacy: @YES
460 #endif
461     };
462     OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, NULL);
463     if (!status)
464         return true;
465     ASSERT(status == errSecItemNotFound);
466     return false;
467 }
468
469 void TestController::setAllowStorageQuotaIncrease(bool value)
470 {
471     [globalWebsiteDataStoreDelegateClient setAllowRaisingQuota: value];
472 }
473
474 void TestController::setAllowsAnySSLCertificate(bool allows)
475 {
476     m_allowsAnySSLCertificate = allows;
477     WKContextSetAllowsAnySSLCertificateForWebSocketTesting(platformContext(), allows);
478     [globalWebsiteDataStoreDelegateClient setAllowAnySSLCertificate: allows];
479 }
480
481 void TestController::installCustomMenuAction(const String& name, bool dismissesAutomatically)
482 {
483 #if PLATFORM(IOS_FAMILY)
484     auto* invocation = m_currentInvocation.get();
485     [m_mainWebView->platformView() installCustomMenuAction:name dismissesAutomatically:dismissesAutomatically callback:[invocation] {
486         if (TestController::singleton().isCurrentInvocation(invocation))
487             invocation->performCustomMenuAction();
488     }];
489 #else
490     UNUSED_PARAM(name);
491     UNUSED_PARAM(dismissesAutomatically);
492 #endif
493 }
494
495 void TestController::setAllowedMenuActions(const Vector<String>& actions)
496 {
497 #if PLATFORM(IOS_FAMILY)
498     auto actionNames = adoptNS([[NSMutableArray<NSString *> alloc] initWithCapacity:actions.size()]);
499     for (auto& action : actions)
500         [actionNames addObject:action];
501     [m_mainWebView->platformView() setAllowedMenuActions:actionNames.get()];
502 #else
503     UNUSED_PARAM(actions);
504 #endif
505 }
506
507 bool TestController::isDoingMediaCapture() const
508 {
509     return m_mainWebView->platformView()._mediaCaptureState != _WKMediaCaptureStateNone;
510 }
511
512 #if PLATFORM(IOS_FAMILY)
513
514 static WKContentMode contentMode(const TestOptions& options)
515 {
516     if (options.contentMode == "desktop"_s)
517         return WKContentModeDesktop;
518
519     if (options.contentMode == "mobile"_s)
520         return WKContentModeMobile;
521
522     return WKContentModeRecommended;
523 }
524
525 #endif // PLATFORM(IOS_FAMILY)
526
527 void TestController::configureContentMode(WKWebViewConfiguration *configuration, const TestOptions& options)
528 {
529     auto webpagePreferences = adoptNS([[WKWebpagePreferences alloc] init]);
530 #if PLATFORM(IOS_FAMILY)
531     [webpagePreferences setPreferredContentMode:contentMode(options)];
532 #else
533     UNUSED_PARAM(options);
534 #endif
535     configuration.defaultWebpagePreferences = webpagePreferences.get();
536 }
537
538 } // namespace WTR