Allow removal of white backgrounds
[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 <Foundation/Foundation.h>
35 #import <WebKit/WKContextConfigurationRef.h>
36 #import <WebKit/WKCookieManager.h>
37 #import <WebKit/WKPreferencesRefPrivate.h>
38 #import <WebKit/WKProcessPoolPrivate.h>
39 #import <WebKit/WKStringCF.h>
40 #import <WebKit/WKUserContentControllerPrivate.h>
41 #import <WebKit/WKWebView.h>
42 #import <WebKit/WKWebViewConfiguration.h>
43 #import <WebKit/WKWebViewConfigurationPrivate.h>
44 #import <WebKit/WKWebViewPrivate.h>
45 #import <WebKit/WKWebsiteDataRecordPrivate.h>
46 #import <WebKit/WKWebsiteDataStorePrivate.h>
47 #import <WebKit/WKWebsiteDataStoreRef.h>
48 #import <WebKit/_WKApplicationManifest.h>
49 #import <WebKit/_WKUserContentExtensionStore.h>
50 #import <WebKit/_WKUserContentExtensionStorePrivate.h>
51 #import <wtf/MainThread.h>
52
53 namespace WTR {
54
55 static WKWebViewConfiguration *globalWebViewConfiguration;
56
57 void initializeWebViewConfiguration(const char* libraryPath, WKStringRef injectedBundlePath, WKContextRef context, WKContextConfigurationRef contextConfiguration)
58 {
59 #if WK_API_ENABLED
60     [globalWebViewConfiguration release];
61     globalWebViewConfiguration = [[WKWebViewConfiguration alloc] init];
62
63     globalWebViewConfiguration.processPool = (__bridge WKProcessPool *)context;
64     globalWebViewConfiguration.websiteDataStore = (__bridge WKWebsiteDataStore *)WKContextGetWebsiteDataStore(context);
65     globalWebViewConfiguration._allowUniversalAccessFromFileURLs = YES;
66     globalWebViewConfiguration._applePayEnabled = YES;
67
68 #if (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300) || PLATFORM(IOS)
69     WKCookieManagerSetCookieStoragePartitioningEnabled(WKContextGetCookieManager(context), true);
70     WKCookieManagerSetStorageAccessAPIEnabled(WKContextGetCookieManager(context), true);
71 #endif
72
73     WKWebsiteDataStore* poolWebsiteDataStore = (__bridge WKWebsiteDataStore *)WKContextGetWebsiteDataStore((__bridge WKContextRef)globalWebViewConfiguration.processPool);
74     [poolWebsiteDataStore _setCacheStoragePerOriginQuota: 400 * 1024];
75     if (libraryPath) {
76         String cacheStorageDirectory = String(libraryPath) + '/' + "CacheStorage";
77         [poolWebsiteDataStore _setCacheStorageDirectory: cacheStorageDirectory];
78
79         String serviceWorkerRegistrationDirectory = String(libraryPath) + '/' + "ServiceWorkers";
80         [poolWebsiteDataStore _setServiceWorkerRegistrationDirectory: serviceWorkerRegistrationDirectory];
81     }
82
83     [globalWebViewConfiguration.websiteDataStore _setResourceLoadStatisticsEnabled:YES];
84     [globalWebViewConfiguration.websiteDataStore _resourceLoadStatisticsSetShouldSubmitTelemetry:NO];
85
86 #if PLATFORM(IOS)
87     globalWebViewConfiguration.allowsInlineMediaPlayback = YES;
88     globalWebViewConfiguration._inlineMediaPlaybackRequiresPlaysInlineAttribute = NO;
89     globalWebViewConfiguration._invisibleAutoplayNotPermitted = NO;
90     globalWebViewConfiguration._mediaDataLoadsAutomatically = YES;
91     globalWebViewConfiguration.requiresUserActionForMediaPlayback = NO;
92 #endif
93     globalWebViewConfiguration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
94 #endif
95
96 #if USE(SYSTEM_PREVIEW)
97     globalWebViewConfiguration._systemPreviewEnabled = YES;
98 #endif
99 }
100
101 void TestController::cocoaPlatformInitialize()
102 {
103     const char* dumpRenderTreeTemp = libraryPathForTesting();
104     if (!dumpRenderTreeTemp)
105         return;
106
107     String resourceLoadStatisticsFolder = String(dumpRenderTreeTemp) + '/' + "ResourceLoadStatistics";
108     [[NSFileManager defaultManager] createDirectoryAtPath:resourceLoadStatisticsFolder withIntermediateDirectories:YES attributes:nil error: nil];
109     String fullBrowsingSessionResourceLog = resourceLoadStatisticsFolder + '/' + "full_browsing_session_resourceLog.plist";
110     NSDictionary *resourceLogPlist = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt:1], @"version", nil];
111     if (![resourceLogPlist writeToFile:fullBrowsingSessionResourceLog atomically:YES])
112         WTFCrash();
113     [resourceLogPlist release];
114 }
115
116 WKContextRef TestController::platformContext()
117 {
118 #if WK_API_ENABLED
119     return (__bridge WKContextRef)globalWebViewConfiguration.processPool;
120 #else
121     return nullptr;
122 #endif
123 }
124
125 WKPreferencesRef TestController::platformPreferences()
126 {
127 #if WK_API_ENABLED
128     return (__bridge WKPreferencesRef)globalWebViewConfiguration.preferences;
129 #else
130     return nullptr;
131 #endif
132 }
133
134 void TestController::platformAddTestOptions(TestOptions& options) const
135 {
136     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EnableProcessSwapOnNavigation"])
137         options.enableProcessSwapOnNavigation = true;
138     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EnableProcessSwapOnWindowOpen"])
139         options.enableProcessSwapOnWindowOpen = true;
140 }
141
142 void TestController::platformCreateWebView(WKPageConfigurationRef, const TestOptions& options)
143 {
144 #if WK_API_ENABLED
145     RetainPtr<WKWebViewConfiguration> copiedConfiguration = adoptNS([globalWebViewConfiguration copy]);
146
147 #if PLATFORM(IOS)
148     if (options.useDataDetection)
149         [copiedConfiguration setDataDetectorTypes:WKDataDetectorTypeAll];
150     if (options.ignoresViewportScaleLimits)
151         [copiedConfiguration setIgnoresViewportScaleLimits:YES];
152     if (options.useCharacterSelectionGranularity)
153         [copiedConfiguration setSelectionGranularity:WKSelectionGranularityCharacter];
154     if (options.useCharacterSelectionGranularity)
155         [copiedConfiguration setSelectionGranularity:WKSelectionGranularityCharacter];
156 #endif
157
158     if (options.enableAttachmentElement)
159         [copiedConfiguration _setAttachmentElementEnabled: YES];
160
161     if (options.enableColorFilter)
162         [copiedConfiguration _setColorFilterEnabled: YES];
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
172     if (options.punchOutWhiteBackgroundsInDarkMode)
173         m_mainWebView->setDrawsBackground(false);
174 #else
175     m_mainWebView = std::make_unique<PlatformWebView>(globalWebViewConfiguration, options);
176 #endif
177 }
178
179 PlatformWebView* TestController::platformCreateOtherPage(PlatformWebView* parentView, WKPageConfigurationRef, const TestOptions& options)
180 {
181 #if WK_API_ENABLED
182     WKWebViewConfiguration *newConfiguration = [[globalWebViewConfiguration copy] autorelease];
183     newConfiguration._relatedWebView = static_cast<WKWebView*>(parentView->platformView());
184     return new PlatformWebView(newConfiguration, options);
185 #else
186     return nullptr;
187 #endif
188 }
189
190 WKContextRef TestController::platformAdjustContext(WKContextRef context, WKContextConfigurationRef contextConfiguration)
191 {
192 #if WK_API_ENABLED
193     initializeWebViewConfiguration(libraryPathForTesting(), injectedBundlePath(), context, contextConfiguration);
194     return (__bridge WKContextRef)globalWebViewConfiguration.processPool;
195 #else
196     return nullptr;
197 #endif
198 }
199
200 void TestController::platformRunUntil(bool& done, double timeout)
201 {
202     NSDate *endDate = (timeout > 0) ? [NSDate dateWithTimeIntervalSinceNow:timeout] : [NSDate distantFuture];
203
204     while (!done && [endDate compare:[NSDate date]] == NSOrderedDescending)
205         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:endDate];
206 }
207
208 void TestController::cocoaResetStateToConsistentValues()
209 {
210 #if WK_API_ENABLED
211     __block bool doneRemoving = false;
212     [[_WKUserContentExtensionStore defaultStore] removeContentExtensionForIdentifier:@"TestContentExtensions" completionHandler:^(NSError *error) {
213         doneRemoving = true;
214     }];
215     platformRunUntil(doneRemoving, noTimeout);
216     [[_WKUserContentExtensionStore defaultStore] _removeAllContentExtensions];
217
218     if (PlatformWebView* webView = mainWebView())
219         [webView->platformView().configuration.userContentController _removeAllUserContentFilters];
220 #endif
221 }
222
223 void TestController::platformWillRunTest(const TestInvocation& testInvocation)
224 {
225     setCrashReportApplicationSpecificInformationToURL(testInvocation.url());
226 }
227
228 static NSString * const WebArchivePboardType = @"Apple Web Archive pasteboard type";
229 static NSString * const WebSubresourcesKey = @"WebSubresources";
230 static NSString * const WebSubframeArchivesKey = @"WebResourceMIMEType like 'image*'";
231
232 unsigned TestController::imageCountInGeneralPasteboard() const
233 {
234 #if PLATFORM(MAC)
235     NSData *data = [[NSPasteboard generalPasteboard] dataForType:WebArchivePboardType];
236 #elif PLATFORM(IOS)
237     NSData *data = [[UIPasteboard generalPasteboard] valueForPasteboardType:WebArchivePboardType];
238 #endif
239     if (!data)
240         return 0;
241     
242     NSError *error = nil;
243     id webArchive = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:&error];
244     if (error) {
245         NSLog(@"Encountered error while serializing Web Archive pasteboard data: %@", error);
246         return 0;
247     }
248     
249     NSArray *subItems = [NSArray arrayWithArray:[webArchive objectForKey:WebSubresourcesKey]];
250     NSPredicate *predicate = [NSPredicate predicateWithFormat:WebSubframeArchivesKey];
251     NSArray *imagesArray = [subItems filteredArrayUsingPredicate:predicate];
252     
253     if (!imagesArray)
254         return 0;
255     
256     return imagesArray.count;
257 }
258
259 void TestController::removeAllSessionCredentials()
260 {
261 #if WK_API_ENABLED
262     auto types = adoptNS([[NSSet alloc] initWithObjects:_WKWebsiteDataTypeCredentials, nil]);
263     [globalWebViewConfiguration.websiteDataStore removeDataOfTypes:types.get() modifiedSince:[NSDate distantPast] completionHandler:^() {
264         m_currentInvocation->didRemoveAllSessionCredentials();
265     }];
266 #endif
267 }
268
269 void TestController::getAllStorageAccessEntries()
270 {
271 #if WK_API_ENABLED
272     auto* parentView = mainWebView();
273     if (!parentView)
274         return;
275
276     [globalWebViewConfiguration.websiteDataStore _getAllStorageAccessEntriesFor:parentView->platformView() completionHandler:^(NSArray<NSString *> *nsDomains) {
277         Vector<String> domains;
278         domains.reserveInitialCapacity(nsDomains.count);
279         for (NSString *domain : nsDomains)
280             domains.uncheckedAppend(domain);
281         m_currentInvocation->didReceiveAllStorageAccessEntries(domains);
282     }];
283 #endif
284 }
285
286 void TestController::injectUserScript(WKStringRef script)
287 {
288 #if WK_API_ENABLED
289     auto userScript = adoptNS([[WKUserScript alloc] initWithSource: toWTFString(script) injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO]);
290
291     [[globalWebViewConfiguration userContentController] addUserScript: userScript.get()];
292 #endif
293 }
294
295 } // namespace WTR