HTML Form Validation bubble should take minimum font size setting into consideration
[WebKit-https.git] / Tools / WebKitTestRunner / mac / UIScriptControllerMac.mm
1 /*
2  * Copyright (C) 2015 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 "UIScriptController.h"
28
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>
40
41 namespace WTR {
42
43 NSString *nsString(JSStringRef string)
44 {
45     return (NSString *)adoptCF(JSStringCopyCFString(kCFAllocatorDefault, string)).autorelease();
46 }
47
48 void UIScriptController::doAsyncTask(JSValueRef callback)
49 {
50     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
51
52     dispatch_async(dispatch_get_main_queue(), ^{
53         if (!m_context)
54             return;
55         m_context->asyncTaskComplete(callbackID);
56     });
57 }
58
59 void UIScriptController::doAfterPresentationUpdate(JSValueRef callback)
60 {
61     return doAsyncTask(callback);
62 }
63
64 void UIScriptController::doAfterNextStablePresentationUpdate(JSValueRef callback)
65 {
66     doAsyncTask(callback);
67 }
68
69 void UIScriptController::insertText(JSStringRef text, int location, int length)
70 {
71 #if WK_API_ENABLED
72     if (location == -1)
73         location = NSNotFound;
74
75     auto* webView = TestController::singleton().mainWebView()->platformView();
76     [webView _insertText:nsString(text) replacementRange:NSMakeRange(location, length)];
77 #else
78     UNUSED_PARAM(text);
79     UNUSED_PARAM(location);
80     UNUSED_PARAM(length);
81 #endif
82 }
83
84 void UIScriptController::zoomToScale(double scale, JSValueRef callback)
85 {
86 #if WK_API_ENABLED
87     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
88
89     auto* webView = TestController::singleton().mainWebView()->platformView();
90     [webView _setPageScale:scale withOrigin:CGPointZero];
91
92     [webView _doAfterNextPresentationUpdate: ^ {
93         if (!m_context)
94             return;
95         m_context->asyncTaskComplete(callbackID);
96     }];
97 #else
98     UNUSED_PARAM(scale);
99     UNUSED_PARAM(callback);
100 #endif
101 }
102
103 void UIScriptController::simulateAccessibilitySettingsChangeNotification(JSValueRef callback)
104 {
105 #if WK_API_ENABLED
106     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
107
108     auto* webView = TestController::singleton().mainWebView()->platformView();
109     NSNotificationCenter *center = [[NSWorkspace sharedWorkspace] notificationCenter];
110     [center postNotificationName:NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification object:webView];
111
112     [webView _doAfterNextPresentationUpdate: ^{
113         if (!m_context)
114             return;
115         m_context->asyncTaskComplete(callbackID);
116     }];
117 #else
118     UNUSED_PARAM(callback);
119 #endif
120 }
121
122 JSObjectRef UIScriptController::contentsOfUserInterfaceItem(JSStringRef interfaceItem) const
123 {
124 #if WK_API_ENABLED
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);
128 #else
129     UNUSED_PARAM(interfaceItem);
130     return nullptr;
131 #endif
132 }
133
134 void UIScriptController::overridePreference(JSStringRef preferenceRef, JSStringRef valueRef)
135 {
136 #if WK_API_ENABLED
137     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
138     WKPreferences *preferences = webView.configuration.preferences;
139
140     String preference = toWTFString(toWK(preferenceRef));
141     String value = toWTFString(toWK(valueRef));
142     if (preference == "WebKitMinimumFontSize")
143         preferences.minimumFontSize = value.toDouble();
144 #else
145     UNUSED_PARAM(preferenceRef);
146     UNUSED_PARAM(valueRef);
147 #endif
148 }
149
150 void UIScriptController::removeViewFromWindow(JSValueRef callback)
151 {
152 #if WK_API_ENABLED
153     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
154
155     auto* mainWebView = TestController::singleton().mainWebView();
156     mainWebView->removeFromWindow();
157
158     [mainWebView->platformView() _doAfterNextPresentationUpdate: ^ {
159         if (!m_context)
160             return;
161         m_context->asyncTaskComplete(callbackID);
162     }];
163 #else
164     UNUSED_PARAM(callback);
165 #endif
166 }
167
168 void UIScriptController::addViewToWindow(JSValueRef callback)
169 {
170 #if WK_API_ENABLED
171     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
172
173     auto* mainWebView = TestController::singleton().mainWebView();
174     mainWebView->addToWindow();
175
176     [mainWebView->platformView() _doAfterNextPresentationUpdate: ^ {
177         if (!m_context)
178             return;
179         m_context->asyncTaskComplete(callbackID);
180     }];
181 #else
182     UNUSED_PARAM(callback);
183 #endif
184 }
185
186 } // namespace WTR