005d0249ad45245df644758d9146e922a7b6850d
[WebKit-https.git] / Tools / WebKitTestRunner / ios / UIScriptControllerIOS.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 #if PLATFORM(IOS)
30
31 #import "HIDEventGenerator.h"
32 #import "PlatformWebView.h"
33 #import "StringFunctions.h"
34 #import "TestController.h"
35 #import "TestRunnerWKWebView.h"
36 #import "UIScriptContext.h"
37 #import <UIKit/UIKit.h>
38 #import <WebKit/WKWebViewPrivate.h>
39 #import <WebKit/WebKit.h>
40
41 namespace WTR {
42
43 void UIScriptController::doAsyncTask(JSValueRef callback)
44 {
45     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
46
47     dispatch_async(dispatch_get_main_queue(), ^{
48         if (!m_context)
49             return;
50         m_context->asyncTaskComplete(callbackID);
51     });
52 }
53
54 void UIScriptController::zoomToScale(double scale, JSValueRef callback)
55 {
56     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
57
58     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
59
60     [webView zoomToScale:scale animated:YES completionHandler:^{
61         if (!m_context)
62             return;
63         m_context->asyncTaskComplete(callbackID);
64     }];
65 }
66
67 double UIScriptController::zoomScale() const
68 {
69     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
70     return webView.scrollView.zoomScale;
71 }
72
73 static CGPoint globalToContentCoordinates(TestRunnerWKWebView *webView, long x, long y)
74 {
75     CGPoint point = CGPointMake(x, y);
76     point = [webView _convertPointFromContentsToView:point];
77     point = [webView convertPoint:point toView:nil];
78     point = [webView.window convertPoint:point toWindow:nil];
79     return point;
80 }
81
82 void UIScriptController::singleTapAtPoint(long x, long y, JSValueRef callback)
83 {
84     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
85
86     [[HIDEventGenerator sharedHIDEventGenerator] tap:globalToContentCoordinates(TestController::singleton().mainWebView()->platformView(), x, y) completionBlock:^{
87         if (!m_context)
88             return;
89         m_context->asyncTaskComplete(callbackID);
90     }];
91 }
92
93 void UIScriptController::doubleTapAtPoint(long x, long y, JSValueRef callback)
94 {
95     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
96
97     [[HIDEventGenerator sharedHIDEventGenerator] doubleTap:globalToContentCoordinates(TestController::singleton().mainWebView()->platformView(), x, y) completionBlock:^{
98         if (!m_context)
99             return;
100         m_context->asyncTaskComplete(callbackID);
101     }];
102 }
103
104 void UIScriptController::typeCharacterUsingHardwareKeyboard(JSStringRef character, JSValueRef callback)
105 {
106     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
107
108     // Assumes that the keyboard is already shown.
109     [[HIDEventGenerator sharedHIDEventGenerator] keyDown:toWTFString(toWK(character)) completionBlock:^{
110         if (!m_context)
111             return;
112         m_context->asyncTaskComplete(callbackID);
113     }];
114 }
115
116 double UIScriptController::minimumZoomScale() const
117 {
118     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
119     return webView.scrollView.minimumZoomScale;
120 }
121
122 double UIScriptController::maximumZoomScale() const
123 {
124     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
125     return webView.scrollView.maximumZoomScale;
126 }
127
128 JSObjectRef UIScriptController::contentVisibleRect() const
129 {
130     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
131
132     CGRect contentVisibleRect = webView._contentVisibleRect;
133     
134     WKRect wkRect = WKRectMake(contentVisibleRect.origin.x, contentVisibleRect.origin.y, contentVisibleRect.size.width, contentVisibleRect.size.height);
135     return m_context->objectFromRect(wkRect);
136 }
137
138 void UIScriptController::platformSetWillBeginZoomingCallback()
139 {
140     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
141     webView.willBeginZoomingCallback = ^{
142         if (!m_context)
143             return;
144         m_context->fireCallback(CallbackTypeWillBeginZooming);
145     };
146 }
147
148 void UIScriptController::platformSetDidEndZoomingCallback()
149 {
150     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
151     webView.didEndZoomingCallback = ^{
152         if (!m_context)
153             return;
154         m_context->fireCallback(CallbackTypeDidEndZooming);
155     };
156 }
157
158 void UIScriptController::platformSetDidShowKeyboardCallback()
159 {
160     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
161     webView.didShowKeyboardCallback = ^{
162         if (!m_context)
163             return;
164         m_context->fireCallback(CallbackTypeDidShowKeyboard);
165     };
166 }
167
168 void UIScriptController::platformSetDidHideKeyboardCallback()
169 {
170     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
171     webView.didHideKeyboardCallback = ^{
172         if (!m_context)
173             return;
174         m_context->fireCallback(CallbackTypeDidHideKeyboard);
175     };
176 }
177
178 void UIScriptController::platformSetDidEndScrollingCallback()
179 {
180     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
181     webView.didEndScrollingCallback = ^{
182         if (!m_context)
183             return;
184         m_context->fireCallback(CallbackTypeDidEndScrolling);
185     };
186 }
187
188 void UIScriptController::platformClearAllCallbacks()
189 {
190     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
191     webView.didEndZoomingCallback = nil;
192     webView.willBeginZoomingCallback = nil;
193     webView.didHideKeyboardCallback = nil;
194     webView.didShowKeyboardCallback = nil;
195     webView.didEndScrollingCallback = nil;
196 }
197
198 }
199
200 #endif // PLATFORM(IOS)