c159ef2de700c2094e6fc55839f5996e0429fa75
[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 <JavaScriptCore/JavaScriptCore.h>
38 #import <JavaScriptCore/OpaqueJSString.h>
39 #import <UIKit/UIKit.h>
40 #import <WebCore/FloatRect.h>
41 #import <WebKit/WKWebViewPrivate.h>
42 #import <WebKit/WebKit.h>
43
44 namespace WTR {
45
46 void UIScriptController::doAsyncTask(JSValueRef callback)
47 {
48     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
49
50     dispatch_async(dispatch_get_main_queue(), ^{
51         if (!m_context)
52             return;
53         m_context->asyncTaskComplete(callbackID);
54     });
55 }
56
57 void UIScriptController::zoomToScale(double scale, JSValueRef callback)
58 {
59     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
60
61     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
62
63     [webView zoomToScale:scale animated:YES completionHandler:^{
64         if (!m_context)
65             return;
66         m_context->asyncTaskComplete(callbackID);
67     }];
68 }
69
70 double UIScriptController::zoomScale() const
71 {
72     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
73     return webView.scrollView.zoomScale;
74 }
75
76 static CGPoint globalToContentCoordinates(TestRunnerWKWebView *webView, long x, long y)
77 {
78     CGPoint point = CGPointMake(x, y);
79     point = [webView _convertPointFromContentsToView:point];
80     point = [webView convertPoint:point toView:nil];
81     point = [webView.window convertPoint:point toWindow:nil];
82     return point;
83 }
84
85 void UIScriptController::touchDownAtPoint(long x, long y, long touchCount, JSValueRef callback)
86 {
87     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
88
89     auto location = globalToContentCoordinates(TestController::singleton().mainWebView()->platformView(), x, y);
90     [[HIDEventGenerator sharedHIDEventGenerator] touchDown:location touchCount:touchCount completionBlock:^{
91         if (!m_context)
92             return;
93         m_context->asyncTaskComplete(callbackID);
94     }];
95 }
96
97 void UIScriptController::liftUpAtPoint(long x, long y, long touchCount, JSValueRef callback)
98 {
99     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
100     
101     auto location = globalToContentCoordinates(TestController::singleton().mainWebView()->platformView(), x, y);
102     [[HIDEventGenerator sharedHIDEventGenerator] liftUp:location touchCount:touchCount completionBlock:^{
103         if (!m_context)
104             return;
105         m_context->asyncTaskComplete(callbackID);
106     }];
107 }
108
109 void UIScriptController::singleTapAtPoint(long x, long y, JSValueRef callback)
110 {
111     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
112
113     [[HIDEventGenerator sharedHIDEventGenerator] tap:globalToContentCoordinates(TestController::singleton().mainWebView()->platformView(), x, y) completionBlock:^{
114         if (!m_context)
115             return;
116         m_context->asyncTaskComplete(callbackID);
117     }];
118 }
119
120 void UIScriptController::doubleTapAtPoint(long x, long y, JSValueRef callback)
121 {
122     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
123
124     [[HIDEventGenerator sharedHIDEventGenerator] doubleTap:globalToContentCoordinates(TestController::singleton().mainWebView()->platformView(), x, y) completionBlock:^{
125         if (!m_context)
126             return;
127         m_context->asyncTaskComplete(callbackID);
128     }];
129 }
130
131 void UIScriptController::stylusDownAtPoint(long x, long y, float azimuthAngle, float altitudeAngle, float pressure, JSValueRef callback)
132 {
133     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
134
135     auto location = globalToContentCoordinates(TestController::singleton().mainWebView()->platformView(), x, y);
136     [[HIDEventGenerator sharedHIDEventGenerator] stylusDownAtPoint:location azimuthAngle:azimuthAngle altitudeAngle:altitudeAngle pressure:pressure completionBlock:^{
137         if (!m_context)
138             return;
139         m_context->asyncTaskComplete(callbackID);
140     }];
141 }
142
143 void UIScriptController::stylusMoveToPoint(long x, long y, float azimuthAngle, float altitudeAngle, float pressure, JSValueRef callback)
144 {
145     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
146
147     auto location = globalToContentCoordinates(TestController::singleton().mainWebView()->platformView(), x, y);
148     [[HIDEventGenerator sharedHIDEventGenerator] stylusMoveToPoint:location azimuthAngle:azimuthAngle altitudeAngle:altitudeAngle pressure:pressure completionBlock:^{
149         if (!m_context)
150             return;
151         m_context->asyncTaskComplete(callbackID);
152     }];
153 }
154
155 void UIScriptController::stylusUpAtPoint(long x, long y, JSValueRef callback)
156 {
157     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
158
159     auto location = globalToContentCoordinates(TestController::singleton().mainWebView()->platformView(), x, y);
160     [[HIDEventGenerator sharedHIDEventGenerator] stylusUpAtPoint:location completionBlock:^{
161         if (!m_context)
162             return;
163         m_context->asyncTaskComplete(callbackID);
164     }];
165 }
166
167 void UIScriptController::stylusTapAtPoint(long x, long y, float azimuthAngle, float altitudeAngle, float pressure, JSValueRef callback)
168 {
169     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
170
171     auto location = globalToContentCoordinates(TestController::singleton().mainWebView()->platformView(), x, y);
172     [[HIDEventGenerator sharedHIDEventGenerator] stylusTapAtPoint:location azimuthAngle:azimuthAngle altitudeAngle:altitudeAngle pressure:pressure completionBlock:^{
173         if (!m_context)
174             return;
175         m_context->asyncTaskComplete(callbackID);
176     }];
177 }
178
179 void UIScriptController::sendEventStream(JSStringRef eventsJSON, JSValueRef callback)
180 {
181     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
182
183     String jsonString = eventsJSON->string();
184     auto eventInfo = dynamic_objc_cast<NSDictionary>([NSJSONSerialization JSONObjectWithData:[(NSString *)jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]);
185     if (!eventInfo || ![eventInfo isKindOfClass:[NSDictionary class]]) {
186         WTFLogAlways("JSON is not convertible to a dictionary");
187         return;
188     }
189     
190     [[HIDEventGenerator sharedHIDEventGenerator] sendEventStream:eventInfo completionBlock:^{
191         if (!m_context)
192             return;
193         m_context->asyncTaskComplete(callbackID);
194     }];
195 }
196
197 void UIScriptController::dragFromPointToPoint(long startX, long startY, long endX, long endY, double durationSeconds, JSValueRef callback)
198 {
199     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
200
201     CGPoint startPoint = globalToContentCoordinates(TestController::singleton().mainWebView()->platformView(), startX, startY);
202     CGPoint endPoint = globalToContentCoordinates(TestController::singleton().mainWebView()->platformView(), endX, endY);
203     
204     [[HIDEventGenerator sharedHIDEventGenerator] dragWithStartPoint:startPoint endPoint:endPoint duration:durationSeconds completionBlock:^{
205         if (!m_context)
206             return;
207         m_context->asyncTaskComplete(callbackID);
208     }];
209 }
210     
211 void UIScriptController::longPressAtPoint(long x, long y, JSValueRef callback)
212 {
213     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
214     
215     [[HIDEventGenerator sharedHIDEventGenerator] longPress:globalToContentCoordinates(TestController::singleton().mainWebView()->platformView(), x, y) completionBlock:^{
216         if (!m_context)
217             return;
218         m_context->asyncTaskComplete(callbackID);
219     }];
220 }
221
222 void UIScriptController::typeCharacterUsingHardwareKeyboard(JSStringRef character, JSValueRef callback)
223 {
224     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
225
226     // Assumes that the keyboard is already shown.
227     [[HIDEventGenerator sharedHIDEventGenerator] keyPress:toWTFString(toWK(character)) completionBlock:^{
228         if (!m_context)
229             return;
230         m_context->asyncTaskComplete(callbackID);
231     }];
232 }
233
234 void UIScriptController::keyDownUsingHardwareKeyboard(JSStringRef character, JSValueRef callback)
235 {
236     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
237
238     // Assumes that the keyboard is already shown.
239     [[HIDEventGenerator sharedHIDEventGenerator] keyDown:toWTFString(toWK(character)) completionBlock:^{
240         if (!m_context)
241             return;
242         m_context->asyncTaskComplete(callbackID);
243     }];
244 }
245
246 void UIScriptController::keyUpUsingHardwareKeyboard(JSStringRef character, JSValueRef callback)
247 {
248     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
249
250     // Assumes that the keyboard is already shown.
251     [[HIDEventGenerator sharedHIDEventGenerator] keyUp:toWTFString(toWK(character)) completionBlock:^{
252         if (!m_context)
253             return;
254         m_context->asyncTaskComplete(callbackID);
255     }];
256 }
257
258 void UIScriptController::selectTextCandidateAtIndex(long index, JSValueRef callback)
259 {
260 #if USE(APPLE_INTERNAL_SDK)
261     static const float textPredictionsPollingInterval = 0.1;
262     unsigned callbackID = m_context->prepareForAsyncTask(callback, CallbackTypeNonPersistent);
263     waitForTextPredictionsViewAndSelectCandidateAtIndex(index, callbackID, textPredictionsPollingInterval);
264 #else
265     // FIXME: This is a no-op on non-internal builds due to UIKeyboardPredictionView being unavailable. Ideally, there should be a better way to
266     // retrieve information and interact with the predictive text view that will be compatible with OpenSource.
267     UNUSED_PARAM(index);
268     UNUSED_PARAM(callback);
269 #endif
270 }
271
272 void UIScriptController::waitForTextPredictionsViewAndSelectCandidateAtIndex(long index, unsigned callbackID, float interval)
273 {
274     id UIKeyboardPredictionViewClass = NSClassFromString(@"UIKeyboardPredictionView");
275     if (!UIKeyboardPredictionViewClass)
276         return;
277
278 #if USE(APPLE_INTERNAL_SDK)
279     if (![[UIKeyboardPredictionViewClass activeInstance] hasPredictions]) {
280         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, interval * NSEC_PER_SEC), dispatch_get_main_queue(), ^() {
281             waitForTextPredictionsViewAndSelectCandidateAtIndex(index, callbackID, interval);
282         });
283         return;
284     }
285
286     PlatformWKView webView = TestController::singleton().mainWebView()->platformView();
287     CGRect predictionViewFrame = [[UIKeyboardPredictionViewClass activeInstance] frame];
288     // This assumes there are 3 predicted text cells of equal width, which is the case on iOS.
289     float offsetX = (index * 2 + 1) * CGRectGetWidth(predictionViewFrame) / 6;
290     float offsetY = CGRectGetHeight(webView.window.frame) - CGRectGetHeight([[[UIKeyboardPredictionViewClass activeInstance] superview] frame]) + CGRectGetHeight(predictionViewFrame) / 2;
291     [[HIDEventGenerator sharedHIDEventGenerator] tap:CGPointMake(offsetX, offsetY) completionBlock:^{
292         if (m_context)
293             m_context->asyncTaskComplete(callbackID);
294     }];
295 #else
296     UNUSED_PARAM(index);
297     UNUSED_PARAM(callbackID);
298     UNUSED_PARAM(interval);
299 #endif
300 }
301
302 void UIScriptController::dismissFormAccessoryView()
303 {
304     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
305     [webView dismissFormAccessoryView];
306 }
307
308 void UIScriptController::selectFormAccessoryPickerRow(long rowIndex)
309 {
310     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
311     [webView selectFormAccessoryPickerRow:rowIndex];
312 }
313     
314 JSObjectRef UIScriptController::contentsOfUserInterfaceItem(JSStringRef interfaceItem) const
315 {
316     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
317     NSDictionary *contentDictionary = [webView _contentsOfUserInterfaceItem:toWTFString(toWK(interfaceItem))];
318     return JSValueToObject(m_context->jsContext(), [JSValue valueWithObject:contentDictionary inContext:[JSContext contextWithJSGlobalContextRef:m_context->jsContext()]].JSValueRef, nullptr);
319 }
320
321 static CGPoint contentOffsetBoundedInValidRange(UIScrollView *scrollView, CGPoint contentOffset)
322 {
323     UIEdgeInsets contentInsets = scrollView.contentInset;
324     CGSize contentSize = scrollView.contentSize;
325     CGSize scrollViewSize = scrollView.bounds.size;
326
327     CGFloat maxHorizontalOffset = contentSize.width + contentInsets.right - scrollViewSize.width;
328     contentOffset.x = std::min(maxHorizontalOffset, contentOffset.x);
329     contentOffset.x = std::max(-contentInsets.left, contentOffset.x);
330
331     CGFloat maxVerticalOffset = contentSize.height + contentInsets.bottom - scrollViewSize.height;
332     contentOffset.y = std::min(maxVerticalOffset, contentOffset.y);
333     contentOffset.y = std::max(-contentInsets.top, contentOffset.y);
334     return contentOffset;
335 }
336
337 void UIScriptController::scrollToOffset(long x, long y)
338 {
339     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
340     [webView.scrollView setContentOffset:contentOffsetBoundedInValidRange(webView.scrollView, CGPointMake(x, y)) animated:YES];
341 }
342
343 void UIScriptController::keyboardAccessoryBarNext()
344 {
345     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
346     [webView keyboardAccessoryBarNext];
347 }
348
349 void UIScriptController::keyboardAccessoryBarPrevious()
350 {
351     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
352     [webView keyboardAccessoryBarPrevious];
353 }
354
355 double UIScriptController::minimumZoomScale() const
356 {
357     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
358     return webView.scrollView.minimumZoomScale;
359 }
360
361 double UIScriptController::maximumZoomScale() const
362 {
363     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
364     return webView.scrollView.maximumZoomScale;
365 }
366
367 JSObjectRef UIScriptController::contentVisibleRect() const
368 {
369     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
370
371     CGRect contentVisibleRect = webView._contentVisibleRect;
372     
373     WebCore::FloatRect rect(contentVisibleRect.origin.x, contentVisibleRect.origin.y, contentVisibleRect.size.width, contentVisibleRect.size.height);
374     return m_context->objectFromRect(rect);
375 }
376
377 JSObjectRef UIScriptController::selectionRangeViewRects() const
378 {
379     NSMutableArray *selectionRects = [[NSMutableArray alloc] init];
380     for (UIView *rectView in TestController::singleton().mainWebView()->platformView()._uiTextSelectionRectViews) {
381         if (rectView.hidden)
382             continue;
383
384         CGRect frame = rectView.frame;
385         [selectionRects addObject:@{
386             @"left": @(frame.origin.x),
387             @"top": @(frame.origin.y),
388             @"width": @(frame.size.width),
389             @"height": @(frame.size.height),
390         }];
391     }
392     return JSValueToObject(m_context->jsContext(), [JSValue valueWithObject:selectionRects inContext:[JSContext contextWithJSGlobalContextRef:m_context->jsContext()]].JSValueRef, nullptr);
393 }
394
395 void UIScriptController::platformSetDidStartFormControlInteractionCallback()
396 {
397     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
398     webView.didStartFormControlInteractionCallback = ^{
399         if (!m_context)
400             return;
401         m_context->fireCallback(CallbackTypeDidStartFormControlInteraction);
402     };
403 }
404
405 void UIScriptController::platformSetDidEndFormControlInteractionCallback()
406 {
407     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
408     webView.didEndFormControlInteractionCallback = ^{
409         if (!m_context)
410             return;
411         m_context->fireCallback(CallbackTypeDidEndFormControlInteraction);
412     };
413 }
414     
415 void UIScriptController::platformSetDidShowForcePressPreviewCallback()
416 {
417     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
418     webView.didShowForcePressPreviewCallback = ^ {
419         if (!m_context)
420             return;
421         m_context->fireCallback(CallbackTypeDidShowForcePressPreview);
422     };
423 }
424
425 void UIScriptController::platformSetDidDismissForcePressPreviewCallback()
426 {
427     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
428     webView.didDismissForcePressPreviewCallback = ^ {
429         if (!m_context)
430             return;
431         m_context->fireCallback(CallbackTypeDidEndFormControlInteraction);
432     };
433 }
434
435 void UIScriptController::platformSetWillBeginZoomingCallback()
436 {
437     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
438     webView.willBeginZoomingCallback = ^{
439         if (!m_context)
440             return;
441         m_context->fireCallback(CallbackTypeWillBeginZooming);
442     };
443 }
444
445 void UIScriptController::platformSetDidEndZoomingCallback()
446 {
447     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
448     webView.didEndZoomingCallback = ^{
449         if (!m_context)
450             return;
451         m_context->fireCallback(CallbackTypeDidEndZooming);
452     };
453 }
454
455 void UIScriptController::platformSetDidShowKeyboardCallback()
456 {
457     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
458     webView.didShowKeyboardCallback = ^{
459         if (!m_context)
460             return;
461         m_context->fireCallback(CallbackTypeDidShowKeyboard);
462     };
463 }
464
465 void UIScriptController::platformSetDidHideKeyboardCallback()
466 {
467     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
468     webView.didHideKeyboardCallback = ^{
469         if (!m_context)
470             return;
471         m_context->fireCallback(CallbackTypeDidHideKeyboard);
472     };
473 }
474
475 void UIScriptController::platformSetDidEndScrollingCallback()
476 {
477     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
478     webView.didEndScrollingCallback = ^{
479         if (!m_context)
480             return;
481         m_context->fireCallback(CallbackTypeDidEndScrolling);
482     };
483 }
484
485 void UIScriptController::platformClearAllCallbacks()
486 {
487     TestRunnerWKWebView *webView = TestController::singleton().mainWebView()->platformView();
488     webView.didEndZoomingCallback = nil;
489     webView.willBeginZoomingCallback = nil;
490     webView.didHideKeyboardCallback = nil;
491     webView.didShowKeyboardCallback = nil;
492     webView.didEndScrollingCallback = nil;
493 }
494
495 }
496
497 #endif // PLATFORM(IOS)