fd28758de3df64634acdced79ce5e14fe5a644b3
[WebKit-https.git] / Tools / WebKitTestRunner / cocoa / TestControllerCocoa.mm
1 /*
2  * Copyright (C) 2015-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 #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/WKCookieManager.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/WKWebsiteDataRecordPrivate.h>
48 #import <WebKit/WKWebsiteDataStorePrivate.h>
49 #import <WebKit/WKWebsiteDataStoreRef.h>
50 #import <WebKit/_WKApplicationManifest.h>
51 #import <WebKit/_WKUserContentExtensionStore.h>
52 #import <WebKit/_WKUserContentExtensionStorePrivate.h>
53 #import <wtf/MainThread.h>
54
55 namespace WTR {
56
57 static WKWebViewConfiguration *globalWebViewConfiguration;
58 static TestWebsiteDataStoreDelegate *globalWebsiteDataStoreDelegateClient;
59
60 void initializeWebViewConfiguration(const char* libraryPath, WKStringRef injectedBundlePath, WKContextRef context, WKContextConfigurationRef contextConfiguration)
61 {
62     [globalWebViewConfiguration release];
63     globalWebViewConfiguration = [[WKWebViewConfiguration alloc] init];
64
65     globalWebViewConfiguration.processPool = (__bridge WKProcessPool *)context;
66     globalWebViewConfiguration.websiteDataStore = (__bridge WKWebsiteDataStore *)WKContextGetWebsiteDataStore(context);
67     globalWebViewConfiguration._allowUniversalAccessFromFileURLs = YES;
68     globalWebViewConfiguration._applePayEnabled = YES;
69
70     WKCookieManagerSetStorageAccessAPIEnabled(WKContextGetCookieManager(context), true);
71
72     WKWebsiteDataStore* poolWebsiteDataStore = (__bridge WKWebsiteDataStore *)WKContextGetWebsiteDataStore((__bridge WKContextRef)globalWebViewConfiguration.processPool);
73     if (libraryPath) {
74         String cacheStorageDirectory = String(libraryPath) + '/' + "CacheStorage";
75         [poolWebsiteDataStore _setCacheStorageDirectory: cacheStorageDirectory];
76
77         String serviceWorkerRegistrationDirectory = String(libraryPath) + '/' + "ServiceWorkers";
78         [poolWebsiteDataStore _setServiceWorkerRegistrationDirectory: serviceWorkerRegistrationDirectory];
79     }
80
81     [globalWebViewConfiguration.websiteDataStore _setResourceLoadStatisticsEnabled:YES];
82     [globalWebViewConfiguration.websiteDataStore _resourceLoadStatisticsSetShouldSubmitTelemetry:NO];
83
84     [globalWebsiteDataStoreDelegateClient release];
85     globalWebsiteDataStoreDelegateClient = [[TestWebsiteDataStoreDelegate alloc] init];
86     [globalWebViewConfiguration.websiteDataStore set_delegate:globalWebsiteDataStoreDelegateClient];
87
88 #if PLATFORM(IOS_FAMILY)
89     globalWebViewConfiguration.allowsInlineMediaPlayback = YES;
90     globalWebViewConfiguration._inlineMediaPlaybackRequiresPlaysInlineAttribute = NO;
91     globalWebViewConfiguration._invisibleAutoplayNotPermitted = NO;
92     globalWebViewConfiguration._mediaDataLoadsAutomatically = YES;
93     globalWebViewConfiguration.requiresUserActionForMediaPlayback = NO;
94 #endif
95     globalWebViewConfiguration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
96
97 #if USE(SYSTEM_PREVIEW)
98     globalWebViewConfiguration._systemPreviewEnabled = YES;
99 #endif
100 }
101
102 void TestController::cocoaPlatformInitialize()
103 {
104     const char* dumpRenderTreeTemp = libraryPathForTesting();
105     if (!dumpRenderTreeTemp)
106         return;
107
108     String resourceLoadStatisticsFolder = String(dumpRenderTreeTemp) + '/' + "ResourceLoadStatistics";
109     [[NSFileManager defaultManager] createDirectoryAtPath:resourceLoadStatisticsFolder withIntermediateDirectories:YES attributes:nil error: nil];
110     String fullBrowsingSessionResourceLog = resourceLoadStatisticsFolder + '/' + "full_browsing_session_resourceLog.plist";
111     NSDictionary *resourceLogPlist = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt:1], @"version", nil];
112     if (![resourceLogPlist writeToFile:fullBrowsingSessionResourceLog atomically:YES])
113         WTFCrash();
114     [resourceLogPlist release];
115 }
116
117 WKContextRef TestController::platformContext()
118 {
119     return (__bridge WKContextRef)globalWebViewConfiguration.processPool;
120 }
121
122 WKPreferencesRef TestController::platformPreferences()
123 {
124     return (__bridge WKPreferencesRef)globalWebViewConfiguration.preferences;
125 }
126
127 void TestController::platformAddTestOptions(TestOptions& options) const
128 {
129     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EnableProcessSwapOnNavigation"])
130         options.contextOptions.enableProcessSwapOnNavigation = true;
131     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EnableProcessSwapOnWindowOpen"])
132         options.contextOptions.enableProcessSwapOnWindowOpen = true;
133 }
134
135 void TestController::platformCreateWebView(WKPageConfigurationRef, const TestOptions& options)
136 {
137     RetainPtr<WKWebViewConfiguration> copiedConfiguration = adoptNS([globalWebViewConfiguration copy]);
138
139 #if PLATFORM(IOS_FAMILY)
140     if (options.useDataDetection)
141         [copiedConfiguration setDataDetectorTypes:WKDataDetectorTypeAll];
142     if (options.ignoresViewportScaleLimits)
143         [copiedConfiguration setIgnoresViewportScaleLimits:YES];
144     if (options.useCharacterSelectionGranularity)
145         [copiedConfiguration setSelectionGranularity:WKSelectionGranularityCharacter];
146     if (options.useCharacterSelectionGranularity)
147         [copiedConfiguration setSelectionGranularity:WKSelectionGranularityCharacter];
148 #endif
149
150     if (options.enableAttachmentElement)
151         [copiedConfiguration _setAttachmentElementEnabled:YES];
152
153     if (options.enableColorFilter)
154         [copiedConfiguration _setColorFilterEnabled:YES];
155
156     if (options.enableEditableImages)
157         [copiedConfiguration _setEditableImagesEnabled:YES];
158
159     if (options.enableUndoManagerAPI)
160         [copiedConfiguration _setUndoManagerAPIEnabled:YES];
161
162     configureContentMode(copiedConfiguration.get(), options);
163
164     if (options.applicationManifest.length()) {
165         auto manifestPath = [NSString stringWithUTF8String:options.applicationManifest.c_str()];
166         NSString *text = [NSString stringWithContentsOfFile:manifestPath usedEncoding:nullptr error:nullptr];
167         [copiedConfiguration _setApplicationManifest:[_WKApplicationManifest applicationManifestFromJSON:text manifestURL:nil documentURL:nil]];
168     }
169
170     m_mainWebView = std::make_unique<PlatformWebView>(copiedConfiguration.get(), options);
171     finishCreatingPlatformWebView(m_mainWebView.get(), options);
172
173     if (options.punchOutWhiteBackgroundsInDarkMode)
174         m_mainWebView->setDrawsBackground(false);
175
176     if (options.editable)
177         m_mainWebView->setEditable(true);
178 }
179
180 PlatformWebView* TestController::platformCreateOtherPage(PlatformWebView* parentView, WKPageConfigurationRef, const TestOptions& options)
181 {
182     WKWebViewConfiguration *newConfiguration = [[globalWebViewConfiguration copy] autorelease];
183     newConfiguration._relatedWebView = static_cast<WKWebView*>(parentView->platformView());
184     PlatformWebView* view = new PlatformWebView(newConfiguration, options);
185     finishCreatingPlatformWebView(view, options);
186     return view;
187 }
188
189 // Code that needs to run after TestController::m_mainWebView is initialized goes into this function.
190 void TestController::finishCreatingPlatformWebView(PlatformWebView* view, const TestOptions& options)
191 {
192 #if PLATFORM(MAC)
193     if (options.shouldShowWebView)
194         [view->platformWindow() orderFront:nil];
195     else
196         [view->platformWindow() orderBack:nil];
197 #endif
198 }
199
200 WKContextRef TestController::platformAdjustContext(WKContextRef context, WKContextConfigurationRef contextConfiguration)
201 {
202     initializeWebViewConfiguration(libraryPathForTesting(), injectedBundlePath(), context, contextConfiguration);
203     return (__bridge WKContextRef)globalWebViewConfiguration.processPool;
204 }
205
206 void TestController::platformRunUntil(bool& done, WTF::Seconds timeout)
207 {
208     NSDate *endDate = (timeout > 0_s) ? [NSDate dateWithTimeIntervalSinceNow:timeout.seconds()] : [NSDate distantFuture];
209
210     while (!done && [endDate compare:[NSDate date]] == NSOrderedDescending)
211         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:endDate];
212 }
213
214 static NSCalendar *swizzledCalendar()
215 {
216     return [NSCalendar calendarWithIdentifier:TestController::singleton().getOverriddenCalendarIdentifier().get()];
217 }
218     
219 RetainPtr<NSString> TestController::getOverriddenCalendarIdentifier() const
220 {
221     return m_overriddenCalendarIdentifier;
222 }
223
224 void TestController::setDefaultCalendarType(NSString *identifier)
225 {
226     m_overriddenCalendarIdentifier = identifier;
227     if (!m_calendarSwizzler)
228         m_calendarSwizzler = std::make_unique<ClassMethodSwizzler>([NSCalendar class], @selector(currentCalendar), reinterpret_cast<IMP>(swizzledCalendar));
229 }
230
231 void TestController::resetContentExtensions()
232 {
233     __block bool doneRemoving = false;
234     [[_WKUserContentExtensionStore defaultStore] removeContentExtensionForIdentifier:@"TestContentExtensions" completionHandler:^(NSError *error) {
235         doneRemoving = true;
236     }];
237     platformRunUntil(doneRemoving, noTimeout);
238     [[_WKUserContentExtensionStore defaultStore] _removeAllContentExtensions];
239
240     if (auto* webView = mainWebView()) {
241         TestRunnerWKWebView *platformView = webView->platformView();
242         [platformView.configuration.userContentController _removeAllUserContentFilters];
243     }
244 }
245
246 void TestController::cocoaResetStateToConsistentValues(const TestOptions& options)
247 {
248     m_calendarSwizzler = nullptr;
249     m_overriddenCalendarIdentifier = nil;
250     
251     if (auto* webView = mainWebView()) {
252         TestRunnerWKWebView *platformView = webView->platformView();
253         platformView._viewScale = 1;
254         platformView._minimumEffectiveDeviceWidth = 0;
255
256         // Toggle on before the test, and toggle off after the test.
257         if (options.shouldShowSpellCheckingDots)
258             [platformView toggleContinuousSpellChecking:nil];
259     }
260
261     [globalWebsiteDataStoreDelegateClient setAllowRaisingQuota: true];
262 }
263
264 void TestController::platformWillRunTest(const TestInvocation& testInvocation)
265 {
266     setCrashReportApplicationSpecificInformationToURL(testInvocation.url());
267 }
268
269 static NSString * const WebArchivePboardType = @"Apple Web Archive pasteboard type";
270 static NSString * const WebSubresourcesKey = @"WebSubresources";
271 static NSString * const WebSubframeArchivesKey = @"WebResourceMIMEType like 'image*'";
272
273 unsigned TestController::imageCountInGeneralPasteboard() const
274 {
275 #if PLATFORM(MAC)
276     NSData *data = [[NSPasteboard generalPasteboard] dataForType:WebArchivePboardType];
277 #elif PLATFORM(IOS_FAMILY)
278     NSData *data = [[UIPasteboard generalPasteboard] valueForPasteboardType:WebArchivePboardType];
279 #endif
280     if (!data)
281         return 0;
282     
283     NSError *error = nil;
284     id webArchive = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:&error];
285     if (error) {
286         NSLog(@"Encountered error while serializing Web Archive pasteboard data: %@", error);
287         return 0;
288     }
289     
290     NSArray *subItems = [NSArray arrayWithArray:[webArchive objectForKey:WebSubresourcesKey]];
291     NSPredicate *predicate = [NSPredicate predicateWithFormat:WebSubframeArchivesKey];
292     NSArray *imagesArray = [subItems filteredArrayUsingPredicate:predicate];
293     
294     if (!imagesArray)
295         return 0;
296     
297     return imagesArray.count;
298 }
299
300 void TestController::removeAllSessionCredentials()
301 {
302     auto types = adoptNS([[NSSet alloc] initWithObjects:_WKWebsiteDataTypeCredentials, nil]);
303     [globalWebViewConfiguration.websiteDataStore removeDataOfTypes:types.get() modifiedSince:[NSDate distantPast] completionHandler:^() {
304         m_currentInvocation->didRemoveAllSessionCredentials();
305     }];
306 }
307
308 void TestController::getAllStorageAccessEntries()
309 {
310     auto* parentView = mainWebView();
311     if (!parentView)
312         return;
313
314     [globalWebViewConfiguration.websiteDataStore _getAllStorageAccessEntriesFor:parentView->platformView() completionHandler:^(NSArray<NSString *> *nsDomains) {
315         Vector<String> domains;
316         domains.reserveInitialCapacity(nsDomains.count);
317         for (NSString *domain : nsDomains)
318             domains.uncheckedAppend(domain);
319         m_currentInvocation->didReceiveAllStorageAccessEntries(domains);
320     }];
321 }
322
323 void TestController::injectUserScript(WKStringRef script)
324 {
325     auto userScript = adoptNS([[WKUserScript alloc] initWithSource: toWTFString(script) injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO]);
326
327     [[globalWebViewConfiguration userContentController] addUserScript: userScript.get()];
328 }
329
330 void TestController::addTestKeyToKeychain(const String& privateKeyBase64, const String& attrLabel, const String& applicationTagBase64)
331 {
332     // FIXME(182772)
333 #if PLATFORM(IOS_FAMILY)
334     NSDictionary* options = @{
335         (id)kSecAttrKeyType: (id)kSecAttrKeyTypeECSECPrimeRandom,
336         (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPrivate,
337         (id)kSecAttrKeySizeInBits: @256
338     };
339     CFErrorRef errorRef = nullptr;
340     auto key = adoptCF(SecKeyCreateWithData(
341         (__bridge CFDataRef)adoptNS([[NSData alloc] initWithBase64EncodedString:privateKeyBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters]).get(),
342         (__bridge CFDictionaryRef)options,
343         &errorRef
344     ));
345     ASSERT(!errorRef);
346
347     NSDictionary* addQuery = @{
348         (id)kSecValueRef: (id)key.get(),
349         (id)kSecClass: (id)kSecClassKey,
350         (id)kSecAttrLabel: attrLabel,
351         (id)kSecAttrApplicationTag: adoptNS([[NSData alloc] initWithBase64EncodedString:applicationTagBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters]).get()
352     };
353     OSStatus status = SecItemAdd((__bridge CFDictionaryRef)addQuery, NULL);
354     ASSERT_UNUSED(status, !status);
355 #endif
356 }
357
358 void TestController::cleanUpKeychain(const String& attrLabel)
359 {
360     // FIXME(182772)
361 #if PLATFORM(IOS_FAMILY)
362     NSDictionary* deleteQuery = @{
363         (id)kSecClass: (id)kSecClassKey,
364         (id)kSecAttrLabel: attrLabel
365     };
366     SecItemDelete((__bridge CFDictionaryRef)deleteQuery);
367 #endif
368 }
369
370 bool TestController::keyExistsInKeychain(const String& attrLabel, const String& applicationTagBase64)
371 {
372     // FIXME(182772)
373 #if PLATFORM(IOS_FAMILY)
374     NSDictionary *query = @{
375         (id)kSecClass: (id)kSecClassKey,
376         (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPrivate,
377         (id)kSecAttrLabel: attrLabel,
378         (id)kSecAttrApplicationTag: adoptNS([[NSData alloc] initWithBase64EncodedString:applicationTagBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters]).get(),
379     };
380     OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, NULL);
381     if (!status)
382         return true;
383     ASSERT(status == errSecItemNotFound);
384 #endif
385     return false;
386 }
387
388 void TestController::setAllowStorageQuotaIncrease(bool value)
389 {
390     [globalWebsiteDataStoreDelegateClient setAllowRaisingQuota: value];
391 }
392
393 bool TestController::canDoServerTrustEvaluationInNetworkProcess() const
394 {
395 #if HAVE(CFNETWORK_NSURLSESSION_STRICTRUSTEVALUATE)
396     return true;
397 #else
398     return false;
399 #endif
400 }
401
402 void TestController::installCustomMenuAction(const String& name, bool dismissesAutomatically)
403 {
404 #if PLATFORM(IOS_FAMILY)
405     auto* invocation = m_currentInvocation.get();
406     [m_mainWebView->platformView() installCustomMenuAction:name dismissesAutomatically:dismissesAutomatically callback:[invocation] {
407         if (TestController::singleton().isCurrentInvocation(invocation))
408             invocation->performCustomMenuAction();
409     }];
410 #else
411     UNUSED_PARAM(name);
412     UNUSED_PARAM(dismissesAutomatically);
413 #endif
414 }
415
416 void TestController::setAllowedMenuActions(const Vector<String>& actions)
417 {
418 #if PLATFORM(IOS_FAMILY)
419     auto actionNames = adoptNS([[NSMutableArray<NSString *> alloc] initWithCapacity:actions.size()]);
420     for (auto action : actions)
421         [actionNames addObject:action];
422     [m_mainWebView->platformView() setAllowedMenuActions:actionNames.get()];
423 #else
424     UNUSED_PARAM(actions);
425 #endif
426 }
427
428 bool TestController::isDoingMediaCapture() const
429 {
430     return m_mainWebView->platformView()._mediaCaptureState != _WKMediaCaptureStateNone;
431 }
432
433 #if PLATFORM(IOS_FAMILY)
434
435 static WKContentMode contentMode(const TestOptions& options)
436 {
437     if (options.contentMode == "desktop"_s)
438         return WKContentModeDesktop;
439
440     if (options.contentMode == "mobile"_s)
441         return WKContentModeMobile;
442
443     return WKContentModeRecommended;
444 }
445
446 #endif // PLATFORM(IOS_FAMILY)
447
448 void TestController::configureContentMode(WKWebViewConfiguration *configuration, const TestOptions& options)
449 {
450     auto webpagePreferences = adoptNS([[WKWebpagePreferences alloc] init]);
451 #if PLATFORM(IOS_FAMILY)
452     [webpagePreferences setPreferredContentMode:contentMode(options)];
453 #else
454     UNUSED_PARAM(options);
455 #endif
456     configuration.defaultWebpagePreferences = webpagePreferences.get();
457 }
458
459 } // namespace WTR