Implement keyboard event sending for iOS in WebKitTestRunner
[WebKit-https.git] / Tools / WebKitTestRunner / cocoa / TestRunnerWKWebView.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 "TestRunnerWKWebView.h"
28
29 #import "WebKitTestRunnerDraggingInfo.h"
30 #import <wtf/Assertions.h>
31 #import <wtf/RetainPtr.h>
32
33 #if PLATFORM(IOS)
34 @interface WKWebView ()
35
36 // FIXME: move these to WKWebView_Private.h
37 - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view;
38 - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale;
39
40 @end
41 #endif
42
43 #if WK_API_ENABLED
44
45 @interface TestRunnerWKWebView ()
46 @property (nonatomic, copy) void (^zoomToScaleCompletionHandler)(void);
47 @property (nonatomic, copy) void (^showKeyboardCompletionHandler)(void);
48 @end
49
50 @implementation TestRunnerWKWebView
51
52 #if PLATFORM(MAC)
53 - (void)dragImage:(NSImage *)anImage at:(NSPoint)viewLocation offset:(NSSize)initialOffset event:(NSEvent *)event pasteboard:(NSPasteboard *)pboard source:(id)sourceObj slideBack:(BOOL)slideFlag
54 {
55     RetainPtr<WebKitTestRunnerDraggingInfo> draggingInfo = adoptNS([[WebKitTestRunnerDraggingInfo alloc] initWithImage:anImage offset:initialOffset pasteboard:pboard source:sourceObj]);
56     [self draggingUpdated:draggingInfo.get()];
57 }
58 #endif
59
60 #if PLATFORM(IOS)
61 - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration
62 {
63     if (self = [super initWithFrame:frame configuration:configuration]) {
64         NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
65         [center addObserver:self selector:@selector(_keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
66         [center addObserver:self selector:@selector(_keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
67     }
68     return self;
69 }
70
71 - (void)dealloc
72 {
73     [super dealloc];
74     [[NSNotificationCenter defaultCenter] removeObserver:self];
75 }
76
77 - (void)zoomToScale:(double)scale animated:(BOOL)animated completionHandler:(void (^)(void))completionHandler
78 {
79     ASSERT(!self.zoomToScaleCompletionHandler);
80     self.zoomToScaleCompletionHandler = completionHandler;
81
82     [self.scrollView setZoomScale:scale animated:animated];
83 }
84
85 - (void)_keyboardDidShow:(NSNotification *)notification
86 {
87     if (self.didShowKeyboardCallback)
88         self.didShowKeyboardCallback();
89 }
90
91 - (void)_keyboardDidHide:(NSNotification *)notification
92 {
93     if (self.didHideKeyboardCallback)
94         self.didHideKeyboardCallback();
95 }
96
97 - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view
98 {
99     [super scrollViewWillBeginZooming:scrollView withView:view];
100
101     if (self.willBeginZoomingCallback)
102         self.willBeginZoomingCallback();
103 }
104
105 - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
106 {
107     [super scrollViewDidEndZooming:scrollView withView:view atScale:scale];
108     
109     if (self.didEndZoomingCallback)
110         self.didEndZoomingCallback();
111
112     if (self.zoomToScaleCompletionHandler) {
113         self.zoomToScaleCompletionHandler();
114         self.zoomToScaleCompletionHandler = nullptr;
115     }
116 }
117 #endif
118
119 @end
120
121 #endif // WK_API_ENABLED