2 * Copyright (C) 2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #import "TestController.h"
29 #import "CrashReporterInfo.h"
30 #import "PlatformWebView.h"
31 #import "TestInvocation.h"
32 #import <Foundation/Foundation.h>
33 #import <WebKit/WKContextConfigurationRef.h>
34 #import <WebKit/WKPreferencesRefPrivate.h>
35 #import <WebKit/WKProcessPoolPrivate.h>
36 #import <WebKit/WKStringCF.h>
37 #import <WebKit/WKUserContentControllerPrivate.h>
38 #import <WebKit/WKWebView.h>
39 #import <WebKit/WKWebViewConfiguration.h>
40 #import <WebKit/WKWebViewConfigurationPrivate.h>
41 #import <WebKit/_WKProcessPoolConfiguration.h>
42 #import <WebKit/_WKUserContentExtensionStore.h>
43 #import <WebKit/_WKUserContentExtensionStorePrivate.h>
44 #import <wtf/MainThread.h>
48 static WKWebViewConfiguration *globalWebViewConfiguration;
50 void initializeWebViewConfiguration(const char* libraryPath, WKStringRef injectedBundlePath, WKContextRef context, WKContextConfigurationRef contextConfiguration)
53 ASSERT(!globalWebViewConfiguration);
54 globalWebViewConfiguration = [[WKWebViewConfiguration alloc] init];
56 globalWebViewConfiguration.processPool = [[WKProcessPool alloc] _initWithConfiguration:(_WKProcessPoolConfiguration *)contextConfiguration];
57 globalWebViewConfiguration.websiteDataStore = (WKWebsiteDataStore *)WKContextGetWebsiteDataStore(context);
60 globalWebViewConfiguration.allowsInlineMediaPlayback = YES;
61 globalWebViewConfiguration._inlineMediaPlaybackRequiresPlaysInlineAttribute = NO;
62 globalWebViewConfiguration._mediaDataLoadsAutomatically = YES;
63 globalWebViewConfiguration.requiresUserActionForMediaPlayback = NO;
68 WKPreferencesRef TestController::platformPreferences()
71 return (WKPreferencesRef)globalWebViewConfiguration.preferences;
77 void TestController::platformCreateWebView(WKPageConfigurationRef, const ViewOptions& options)
79 m_mainWebView = std::make_unique<PlatformWebView>(globalWebViewConfiguration, options);
82 PlatformWebView* TestController::platformCreateOtherPage(PlatformWebView* parentView, WKPageConfigurationRef, const ViewOptions& options)
85 WKWebViewConfiguration *newConfiguration = [[globalWebViewConfiguration copy] autorelease];
86 newConfiguration._relatedWebView = static_cast<WKWebView*>(parentView->platformView());
87 return new PlatformWebView(newConfiguration, options);
93 WKContextRef TestController::platformAdjustContext(WKContextRef context, WKContextConfigurationRef contextConfiguration)
96 initializeWebViewConfiguration(libraryPathForTesting(), injectedBundlePath(), context, contextConfiguration);
97 return (WKContextRef)globalWebViewConfiguration.processPool;
103 void TestController::platformRunUntil(bool& done, double timeout)
105 NSDate *endDate = (timeout > 0) ? [NSDate dateWithTimeIntervalSinceNow:timeout] : [NSDate distantFuture];
107 while (!done && [endDate compare:[NSDate date]] == NSOrderedDescending)
108 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:endDate];
111 void TestController::cocoaResetStateToConsistentValues()
114 __block bool doneRemoving = false;
115 [[_WKUserContentExtensionStore defaultStore] removeContentExtensionForIdentifier:@"TestContentExtensions" completionHandler:^(NSError *error) {
118 platformRunUntil(doneRemoving, 0);
119 [[_WKUserContentExtensionStore defaultStore] _removeAllContentExtensions];
121 if (PlatformWebView* webView = mainWebView())
122 [webView->platformView().configuration.userContentController _removeAllUserContentFilters];
126 void TestController::platformWillRunTest(const TestInvocation& testInvocation)
128 setCrashReportApplicationSpecificInformationToURL(testInvocation.url());