Correctly set the "inWindow" flag for TileControllers that aren't the page tiles...
[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::removeViewFromWindow(JSValueRef callback)
135 {
136 #if WK_API_ENABLED
137     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
138
139     auto* mainWebView = TestController::singleton().mainWebView();
140     mainWebView->removeFromWindow();
141
142     [mainWebView->platformView() _doAfterNextPresentationUpdate: ^ {
143         if (!m_context)
144             return;
145         m_context->asyncTaskComplete(callbackID);
146     }];
147 #else
148     UNUSED_PARAM(callback);
149 #endif
150 }
151
152 void UIScriptController::addViewToWindow(JSValueRef callback)
153 {
154 #if WK_API_ENABLED
155     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
156
157     auto* mainWebView = TestController::singleton().mainWebView();
158     mainWebView->addToWindow();
159
160     [mainWebView->platformView() _doAfterNextPresentationUpdate: ^ {
161         if (!m_context)
162             return;
163         m_context->asyncTaskComplete(callbackID);
164     }];
165 #else
166     UNUSED_PARAM(callback);
167 #endif
168 }
169
170 } // namespace WTR