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 "UIScriptController.h"
29 #import "PlatformWebView.h"
30 #import "StringFunctions.h"
31 #import "TestController.h"
32 #import "TestRunnerWKWebView.h"
33 #import "UIScriptContext.h"
34 #import <JavaScriptCore/JSContext.h>
35 #import <JavaScriptCore/JSStringRefCF.h>
36 #import <JavaScriptCore/JSValue.h>
37 #import <JavaScriptCore/JavaScriptCore.h>
38 #import <JavaScriptCore/OpaqueJSString.h>
39 #import <WebKit/WKWebViewPrivate.h>
43 NSString *nsString(JSStringRef string)
45 return (NSString *)adoptCF(JSStringCopyCFString(kCFAllocatorDefault, string)).autorelease();
48 void UIScriptController::doAsyncTask(JSValueRef callback)
50 unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
52 dispatch_async(dispatch_get_main_queue(), ^{
55 m_context->asyncTaskComplete(callbackID);
59 void UIScriptController::doAfterPresentationUpdate(JSValueRef callback)
61 return doAsyncTask(callback);
64 void UIScriptController::doAfterNextStablePresentationUpdate(JSValueRef callback)
66 doAsyncTask(callback);
69 void UIScriptController::insertText(JSStringRef text, int location, int length)
73 location = NSNotFound;
75 auto* webView = TestController::singleton().mainWebView()->platformView();
76 [webView _insertText:nsString(text) replacementRange:NSMakeRange(location, length)];
79 UNUSED_PARAM(location);
84 void UIScriptController::zoomToScale(double scale, JSValueRef callback)
87 unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
89 auto* webView = TestController::singleton().mainWebView()->platformView();
90 [webView _setPageScale:scale withOrigin:CGPointZero];
92 [webView _doAfterNextPresentationUpdate: ^ {
95 m_context->asyncTaskComplete(callbackID);
99 UNUSED_PARAM(callback);
103 void UIScriptController::simulateAccessibilitySettingsChangeNotification(JSValueRef callback)
106 unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
108 auto* webView = TestController::singleton().mainWebView()->platformView();
109 NSNotificationCenter *center = [[NSWorkspace sharedWorkspace] notificationCenter];
110 [center postNotificationName:NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification object:webView];
112 [webView _doAfterNextPresentationUpdate: ^{
115 m_context->asyncTaskComplete(callbackID);
118 UNUSED_PARAM(callback);
122 JSObjectRef UIScriptController::contentsOfUserInterfaceItem(JSStringRef interfaceItem) const
125 TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
126 NSDictionary *contentDictionary = [webView _contentsOfUserInterfaceItem:toWTFString(toWK(interfaceItem))];
127 return JSValueToObject(m_context->jsContext(), [JSValue valueWithObject:contentDictionary inContext:[JSContext contextWithJSGlobalContextRef:m_context->jsContext()]].JSValueRef, nullptr);
129 UNUSED_PARAM(interfaceItem);
134 void UIScriptController::removeViewFromWindow(JSValueRef callback)
137 unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
139 auto* mainWebView = TestController::singleton().mainWebView();
140 mainWebView->removeFromWindow();
142 [mainWebView->platformView() _doAfterNextPresentationUpdate: ^ {
145 m_context->asyncTaskComplete(callbackID);
148 UNUSED_PARAM(callback);
152 void UIScriptController::addViewToWindow(JSValueRef callback)
155 unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
157 auto* mainWebView = TestController::singleton().mainWebView();
158 mainWebView->addToWindow();
160 [mainWebView->platformView() _doAfterNextPresentationUpdate: ^ {
163 m_context->asyncTaskComplete(callbackID);
166 UNUSED_PARAM(callback);