Add a test for iOS arrow-key repeat
[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] keyPress:toWTFString(toWK(character)) completionBlock:^{
110         if (!m_context)
111             return;
112         m_context->asyncTaskComplete(callbackID);
113     }];
114 }
115
116 void UIScriptController::keyDownUsingHardwareKeyboard(JSStringRef character, JSValueRef callback)
117 {
118     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
119
120     // Assumes that the keyboard is already shown.
121     [[HIDEventGenerator sharedHIDEventGenerator] keyDown:toWTFString(toWK(character)) completionBlock:^{
122         if (!m_context)
123             return;
124         m_context->asyncTaskComplete(callbackID);
125     }];
126 }
127
128 void UIScriptController::keyUpUsingHardwareKeyboard(JSStringRef character, JSValueRef callback)
129 {
130     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
131
132     // Assumes that the keyboard is already shown.
133     [[HIDEventGenerator sharedHIDEventGenerator] keyUp:toWTFString(toWK(character)) completionBlock:^{
134         if (!m_context)
135             return;
136         m_context->asyncTaskComplete(callbackID);
137     }];
138 }
139
140 double UIScriptController::minimumZoomScale() const
141 {
142     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
143     return webView.scrollView.minimumZoomScale;
144 }
145
146 double UIScriptController::maximumZoomScale() const
147 {
148     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
149     return webView.scrollView.maximumZoomScale;
150 }
151
152 JSObjectRef UIScriptController::contentVisibleRect() const
153 {
154     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
155
156     CGRect contentVisibleRect = webView._contentVisibleRect;
157     
158     WKRect wkRect = WKRectMake(contentVisibleRect.origin.x, contentVisibleRect.origin.y, contentVisibleRect.size.width, contentVisibleRect.size.height);
159     return m_context->objectFromRect(wkRect);
160 }
161
162 void UIScriptController::platformSetWillBeginZoomingCallback()
163 {
164     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
165     webView.willBeginZoomingCallback = ^{
166         if (!m_context)
167             return;
168         m_context->fireCallback(CallbackTypeWillBeginZooming);
169     };
170 }
171
172 void UIScriptController::platformSetDidEndZoomingCallback()
173 {
174     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
175     webView.didEndZoomingCallback = ^{
176         if (!m_context)
177             return;
178         m_context->fireCallback(CallbackTypeDidEndZooming);
179     };
180 }
181
182 void UIScriptController::platformSetDidShowKeyboardCallback()
183 {
184     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
185     webView.didShowKeyboardCallback = ^{
186         if (!m_context)
187             return;
188         m_context->fireCallback(CallbackTypeDidShowKeyboard);
189     };
190 }
191
192 void UIScriptController::platformSetDidHideKeyboardCallback()
193 {
194     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
195     webView.didHideKeyboardCallback = ^{
196         if (!m_context)
197             return;
198         m_context->fireCallback(CallbackTypeDidHideKeyboard);
199     };
200 }
201
202 void UIScriptController::platformSetDidEndScrollingCallback()
203 {
204     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
205     webView.didEndScrollingCallback = ^{
206         if (!m_context)
207             return;
208         m_context->fireCallback(CallbackTypeDidEndScrolling);
209     };
210 }
211
212 void UIScriptController::platformClearAllCallbacks()
213 {
214     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
215     webView.didEndZoomingCallback = nil;
216     webView.willBeginZoomingCallback = nil;
217     webView.didHideKeyboardCallback = nil;
218     webView.didShowKeyboardCallback = nil;
219     webView.didEndScrollingCallback = nil;
220 }
221
222 }
223
224 #endif // PLATFORM(IOS)