9699747b79f92a8745a55da2a7804aee473f75b5
[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 - (void)_didFinishScrolling;
40
41 @end
42 #endif
43
44 #if WK_API_ENABLED
45
46 @interface TestRunnerWKWebView ()
47 @property (nonatomic, copy) void (^zoomToScaleCompletionHandler)(void);
48 @property (nonatomic, copy) void (^showKeyboardCompletionHandler)(void);
49 @property (nonatomic) BOOL isShowingKeyboard;
50 @end
51
52 @implementation TestRunnerWKWebView
53
54 #if PLATFORM(MAC)
55 - (void)dragImage:(NSImage *)anImage at:(NSPoint)viewLocation offset:(NSSize)initialOffset event:(NSEvent *)event pasteboard:(NSPasteboard *)pboard source:(id)sourceObj slideBack:(BOOL)slideFlag
56 {
57     RetainPtr<WebKitTestRunnerDraggingInfo> draggingInfo = adoptNS([[WebKitTestRunnerDraggingInfo alloc] initWithImage:anImage offset:initialOffset pasteboard:pboard source:sourceObj]);
58     [self draggingUpdated:draggingInfo.get()];
59 }
60 #endif
61
62 #if PLATFORM(IOS)
63 - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration
64 {
65     if (self = [super initWithFrame:frame configuration:configuration]) {
66         NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
67         [center addObserver:self selector:@selector(_keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
68         [center addObserver:self selector:@selector(_keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
69     }
70     return self;
71 }
72
73 - (void)dealloc
74 {
75     [[NSNotificationCenter defaultCenter] removeObserver:self];
76
77     self.didStartFormControlInteractionCallback = nil;
78     self.didEndFormControlInteractionCallback = nil;
79     self.didShowForcePressPreviewCallback = nil;
80     self.didDismissForcePressPreviewCallback = nil;
81     self.willBeginZoomingCallback = nil;
82     self.didEndZoomingCallback = nil;
83     self.didShowKeyboardCallback = nil;
84     self.didHideKeyboardCallback = nil;
85     self.didEndScrollingCallback = nil;
86
87     self.zoomToScaleCompletionHandler = nil;
88     self.showKeyboardCompletionHandler = nil;
89
90     [super dealloc];
91 }
92
93 - (void)didStartFormControlInteraction
94 {
95     if (self.didStartFormControlInteractionCallback)
96         self.didStartFormControlInteractionCallback();
97 }
98
99 - (void)didEndFormControlInteraction
100 {
101     if (self.didEndFormControlInteractionCallback)
102         self.didEndFormControlInteractionCallback();
103 }
104
105 - (void)_didShowForcePressPreview
106 {
107     if (self.didShowForcePressPreviewCallback)
108         self.didShowForcePressPreviewCallback();
109 }
110
111 - (void)_didDismissForcePressPreview
112 {
113     if (self.didDismissForcePressPreviewCallback)
114         self.didDismissForcePressPreviewCallback();
115 }
116
117 - (void)zoomToScale:(double)scale animated:(BOOL)animated completionHandler:(void (^)(void))completionHandler
118 {
119     ASSERT(!self.zoomToScaleCompletionHandler);
120     self.zoomToScaleCompletionHandler = completionHandler;
121
122     [self.scrollView setZoomScale:scale animated:animated];
123 }
124
125 - (void)_keyboardDidShow:(NSNotification *)notification
126 {
127     if (self.isShowingKeyboard)
128         return;
129
130     self.isShowingKeyboard = YES;
131     if (self.didShowKeyboardCallback)
132         self.didShowKeyboardCallback();
133 }
134
135 - (void)_keyboardDidHide:(NSNotification *)notification
136 {
137     if (!self.isShowingKeyboard)
138         return;
139
140     self.isShowingKeyboard = NO;
141     if (self.didHideKeyboardCallback)
142         self.didHideKeyboardCallback();
143 }
144
145 - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
146 {
147     [super scrollViewWillBeginZooming:scrollView withView:view];
148
149     if (self.willBeginZoomingCallback)
150         self.willBeginZoomingCallback();
151 }
152
153 - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
154 {
155     [super scrollViewDidEndZooming:scrollView withView:view atScale:scale];
156     
157     if (self.didEndZoomingCallback)
158         self.didEndZoomingCallback();
159
160     if (self.zoomToScaleCompletionHandler) {
161         self.zoomToScaleCompletionHandler();
162         self.zoomToScaleCompletionHandler = nullptr;
163     }
164 }
165
166 - (void)_didFinishScrolling
167 {
168     [super _didFinishScrolling];
169
170     if (self.didEndScrollingCallback)
171         self.didEndScrollingCallback();
172 }
173 #endif
174
175 @end
176
177 #endif // WK_API_ENABLED