Correct '--show-webview' option for Tiled Drawing tests
[WebKit-https.git] / Tools / WebKitTestRunner / mac / TestControllerMac.mm
1 /*
2  * Copyright (C) 2010 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 "PoseAsClass.h"
32 #import "TestInvocation.h"
33 #import "WebKitTestRunnerPasteboard.h"
34 #import <WebKit/WKContextPrivate.h>
35 #import <WebKit/WKPageGroup.h>
36 #import <WebKit/WKStringCF.h>
37 #import <WebKit/WKURLCF.h>
38 #import <WebKit/_WKUserContentExtensionStore.h>
39 #import <WebKit/_WKUserContentExtensionStorePrivate.h>
40 #import <mach-o/dyld.h>
41
42 @interface NSSound (Details)
43 + (void)_setAlertType:(NSUInteger)alertType;
44 @end
45
46 namespace WTR {
47
48 void TestController::notifyDone()
49 {
50 }
51
52 void TestController::platformInitialize()
53 {
54     poseAsClass("WebKitTestRunnerPasteboard", "NSPasteboard");
55     poseAsClass("WebKitTestRunnerEvent", "NSEvent");
56
57     [NSSound _setAlertType:0];
58 }
59
60 void TestController::platformDestroy()
61 {
62     [WebKitTestRunnerPasteboard releaseLocalPasteboards];
63 }
64
65 void TestController::initializeInjectedBundlePath()
66 {
67     NSString *nsBundlePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"WebKitTestRunnerInjectedBundle.bundle"];
68     m_injectedBundlePath.adopt(WKStringCreateWithCFString((CFStringRef)nsBundlePath));
69 }
70
71 void TestController::initializeTestPluginDirectory()
72 {
73     m_testPluginDirectory.adopt(WKStringCreateWithCFString((CFStringRef)[[NSBundle mainBundle] bundlePath]));
74 }
75
76 void TestController::platformWillRunTest(const TestInvocation& testInvocation)
77 {
78     setCrashReportApplicationSpecificInformationToURL(testInvocation.url());
79 }
80
81 static bool shouldUseThreadedScrolling(const TestInvocation& test)
82 {
83     return test.urlContains("tiled-drawing/");
84 }
85
86 void TestController::platformResetPreferencesToConsistentValues()
87 {
88 #if WK_API_ENABLED
89     __block bool doneRemoving = false;
90     [[_WKUserContentExtensionStore defaultStore] removeContentExtensionForIdentifier:@"TestContentExtensions" completionHandler:^(NSError *error)
91     {
92         doneRemoving = true;
93     }];
94     platformRunUntil(doneRemoving, 0);
95     [[_WKUserContentExtensionStore defaultStore] _removeAllContentExtensions];
96 #endif
97 }
98
99 void TestController::platformConfigureViewForTest(const TestInvocation& test)
100 {
101     auto viewOptions = adoptWK(WKMutableDictionaryCreate());
102     auto useThreadedScrollingKey = adoptWK(WKStringCreateWithUTF8CString("ThreadedScrolling"));
103     auto useThreadedScrollingValue = adoptWK(WKBooleanCreate(shouldUseThreadedScrolling(test)));
104     WKDictionarySetItem(viewOptions.get(), useThreadedScrollingKey.get(), useThreadedScrollingValue.get());
105
106     auto useRemoteLayerTreeKey = adoptWK(WKStringCreateWithUTF8CString("RemoteLayerTree"));
107     auto useRemoteLayerTreeValue = adoptWK(WKBooleanCreate(shouldUseRemoteLayerTree()));
108     WKDictionarySetItem(viewOptions.get(), useRemoteLayerTreeKey.get(), useRemoteLayerTreeValue.get());
109
110     auto shouldShowWebViewKey = adoptWK(WKStringCreateWithUTF8CString("ShouldShowWebView"));
111     auto shouldShowWebViewValue = adoptWK(WKBooleanCreate(shouldShowWebView()));
112     WKDictionarySetItem(viewOptions.get(), shouldShowWebViewKey.get(), shouldShowWebViewValue.get());
113
114     ensureViewSupportsOptions(viewOptions.get());
115
116     if (!test.urlContains("contentextensions/"))
117         return;
118
119     RetainPtr<CFURLRef> testURL = adoptCF(WKURLCopyCFURL(kCFAllocatorDefault, test.url()));
120     NSURL *filterURL = [(NSURL *)testURL.get() URLByAppendingPathExtension:@"json"];
121
122     NSStringEncoding encoding;
123     NSString *contentExtensionString = [NSString stringWithContentsOfURL:filterURL usedEncoding:&encoding error:NULL];
124     if (!contentExtensionString)
125         return;
126     
127 #if WK_API_ENABLED
128     __block bool doneCompiling = false;
129     [[_WKUserContentExtensionStore defaultStore] compileContentExtensionForIdentifier:@"TestContentExtensions" encodedContentExtension:contentExtensionString completionHandler:^(_WKUserContentFilter *filter, NSError *error)
130     {
131         if (!error)
132             WKPageGroupAddUserContentFilter(WKPageGetPageGroup(TestController::singleton().mainWebView()->page()), (__bridge WKUserContentFilterRef)filter);
133         else
134             NSLog(@"%@", [error helpAnchor]);
135         doneCompiling = true;
136     }];
137     platformRunUntil(doneCompiling, 0);
138 #endif
139 }
140
141 void TestController::platformRunUntil(bool& done, double timeout)
142 {
143     NSDate *endDate = (timeout > 0) ? [NSDate dateWithTimeIntervalSinceNow:timeout] : [NSDate distantFuture];
144
145     while (!done && [endDate compare:[NSDate date]] == NSOrderedDescending)
146         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:endDate];
147 }
148
149 #if ENABLE(PLATFORM_FONT_LOOKUP)
150 static NSSet *allowedFontFamilySet()
151 {
152     static NSSet *fontFamilySet = [[NSSet setWithObjects:
153         @"Ahem",
154         @"Al Bayan",
155         @"American Typewriter",
156         @"Andale Mono",
157         @"Apple Braille",
158         @"Apple Color Emoji",
159         @"Apple Chancery",
160         @"Apple Garamond BT",
161         @"Apple LiGothic",
162         @"Apple LiSung",
163         @"Apple Symbols",
164         @"AppleGothic",
165         @"AppleMyungjo",
166         @"Arial Black",
167         @"Arial Hebrew",
168         @"Arial Narrow",
169         @"Arial Rounded MT Bold",
170         @"Arial Unicode MS",
171         @"Arial",
172         @"Ayuthaya",
173         @"Baghdad",
174         @"Baskerville",
175         @"BiauKai",
176         @"Big Caslon",
177         @"Brush Script MT",
178         @"Chalkboard",
179         @"Chalkduster",
180         @"Charcoal CY",
181         @"Cochin",
182         @"Comic Sans MS",
183         @"Copperplate",
184         @"Corsiva Hebrew",
185         @"Courier New",
186         @"Courier",
187         @"DecoType Naskh",
188         @"Devanagari MT",
189         @"Didot",
190         @"Euphemia UCAS",
191         @"Futura",
192         @"GB18030 Bitmap",
193         @"Geeza Pro",
194         @"Geneva CY",
195         @"Geneva",
196         @"Georgia",
197         @"Gill Sans",
198         @"Gujarati MT",
199         @"GungSeo",
200         @"Gurmukhi MT",
201         @"HeadLineA",
202         @"Hei",
203         @"Heiti SC",
204         @"Heiti TC",
205         @"Helvetica CY",
206         @"Helvetica Neue",
207         @"Helvetica",
208         @"Herculanum",
209         @"Hiragino Kaku Gothic Pro",
210         @"Hiragino Kaku Gothic ProN",
211         @"Hiragino Kaku Gothic Std",
212         @"Hiragino Kaku Gothic StdN",
213         @"Hiragino Maru Gothic Monospaced",
214         @"Hiragino Maru Gothic Pro",
215         @"Hiragino Maru Gothic ProN",
216         @"Hiragino Mincho Pro",
217         @"Hiragino Mincho ProN",
218         @"Hiragino Sans GB",
219         @"Hoefler Text",
220         @"Impact",
221         @"InaiMathi",
222         @"Kai",
223         @"Kailasa",
224         @"Kokonor",
225         @"Krungthep",
226         @"KufiStandardGK",
227         @"LiHei Pro",
228         @"LiSong Pro",
229         @"Lucida Grande",
230         @"Marker Felt",
231         @"Menlo",
232         @"Microsoft Sans Serif",
233         @"Monaco",
234         @"Mshtakan",
235         @"Nadeem",
236         @"New Peninim MT",
237         @"Optima",
238         @"Osaka",
239         @"Papyrus",
240         @"PCMyungjo",
241         @"PilGi",
242         @"Plantagenet Cherokee",
243         @"Raanana",
244         @"Sathu",
245         @"Silom",
246         @"Skia",
247         @"Songti SC",
248         @"Songti TC",
249         @"STFangsong",
250         @"STHeiti",
251         @"STIXGeneral",
252         @"STIXSizeOneSym",
253         @"STKaiti",
254         @"STSong",
255         @"Symbol",
256         @"System Font",
257         @"Tahoma",
258         @"Thonburi",
259         @"Times New Roman",
260         @"Times",
261         @"Trebuchet MS",
262         @"Verdana",
263         @"Webdings",
264         @"WebKit WeightWatcher",
265         @"Wingdings 2",
266         @"Wingdings 3",
267         @"Wingdings",
268         @"Zapf Dingbats",
269         @"Zapfino",
270         nil] retain];
271
272     return fontFamilySet;
273 }
274
275 static NSSet *systemHiddenFontFamilySet()
276 {
277     static NSSet *fontFamilySet = [[NSSet setWithObjects:
278         @".LucidaGrandeUI",
279         nil] retain];
280
281     return fontFamilySet;
282 }
283
284 static WKRetainPtr<WKArrayRef> generateWhitelist()
285 {
286     WKMutableArrayRef result = WKMutableArrayCreate();
287     for (NSString *fontFamily in allowedFontFamilySet()) {
288         NSArray *fontsForFamily = [[NSFontManager sharedFontManager] availableMembersOfFontFamily:fontFamily];
289         WKArrayAppendItem(result, WKStringCreateWithUTF8CString([fontFamily UTF8String]));
290         for (NSArray *fontInfo in fontsForFamily) {
291             // Font name is the first entry in the array.
292             WKArrayAppendItem(result, WKStringCreateWithUTF8CString([[fontInfo objectAtIndex:0] UTF8String]));
293         }
294     }
295
296     for (NSString *hiddenFontFamily in systemHiddenFontFamilySet())
297         WKArrayAppendItem(result, WKStringCreateWithUTF8CString([hiddenFontFamily UTF8String]));
298
299     return adoptWK(result);
300 }
301 #endif
302
303 void TestController::platformInitializeContext()
304 {
305     // Testing uses a private session, which is memory only. However creating one instantiates a shared NSURLCache,
306     // and if we haven't created one yet, the default one will be created on disk.
307     // Making the shared cache memory-only avoids touching the file system.
308     RetainPtr<NSURLCache> sharedCache =
309         adoptNS([[NSURLCache alloc] initWithMemoryCapacity:1024 * 1024
310                                       diskCapacity:0
311                                           diskPath:nil]);
312     [NSURLCache setSharedURLCache:sharedCache.get()];
313
314 #if ENABLE(PLATFORM_FONT_LOOKUP)
315     WKContextSetFontWhitelist(m_context.get(), generateWhitelist().get());
316 #endif
317 }
318
319 void TestController::setHidden(bool hidden)
320 {
321     NSWindow *window = [mainWebView()->platformView() window];
322     if (!window)
323         return;
324
325     if (hidden)
326         [window orderOut:nil];
327     else
328         [window makeKeyAndOrderFront:nil];
329 }
330
331 void TestController::runModal(PlatformWebView* view)
332 {
333     NSWindow *window = [view->platformView() window];
334     if (!window)
335         return;
336     [NSApp runModalForWindow:window];
337 }
338
339 const char* TestController::platformLibraryPathForTesting()
340 {
341     return [[@"~/Library/Application Support/DumpRenderTree" stringByExpandingTildeInPath] UTF8String];
342 }
343
344 } // namespace WTR