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