2 * Copyright (C) 2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #import "TestRunnerWKWebView.h"
29 #import "WebKitTestRunnerDraggingInfo.h"
30 #import <wtf/Assertions.h>
31 #import <wtf/RetainPtr.h>
34 @interface WKWebView ()
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;
46 @interface TestRunnerWKWebView ()
47 @property (nonatomic, copy) void (^zoomToScaleCompletionHandler)(void);
48 @property (nonatomic, copy) void (^showKeyboardCompletionHandler)(void);
49 @property (nonatomic) BOOL isShowingKeyboard;
52 @implementation TestRunnerWKWebView
55 - (void)dragImage:(NSImage *)anImage at:(NSPoint)viewLocation offset:(NSSize)initialOffset event:(NSEvent *)event pasteboard:(NSPasteboard *)pboard source:(id)sourceObj slideBack:(BOOL)slideFlag
57 RetainPtr<WebKitTestRunnerDraggingInfo> draggingInfo = adoptNS([[WebKitTestRunnerDraggingInfo alloc] initWithImage:anImage offset:initialOffset pasteboard:pboard source:sourceObj]);
58 [self draggingUpdated:draggingInfo.get()];
63 - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration
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];
75 [[NSNotificationCenter defaultCenter] removeObserver:self];
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;
87 self.zoomToScaleCompletionHandler = nil;
88 self.showKeyboardCompletionHandler = nil;
93 - (void)didStartFormControlInteraction
95 if (self.didStartFormControlInteractionCallback)
96 self.didStartFormControlInteractionCallback();
99 - (void)didEndFormControlInteraction
101 if (self.didEndFormControlInteractionCallback)
102 self.didEndFormControlInteractionCallback();
105 - (void)_didShowForcePressPreview
107 if (self.didShowForcePressPreviewCallback)
108 self.didShowForcePressPreviewCallback();
111 - (void)_didDismissForcePressPreview
113 if (self.didDismissForcePressPreviewCallback)
114 self.didDismissForcePressPreviewCallback();
117 - (void)zoomToScale:(double)scale animated:(BOOL)animated completionHandler:(void (^)(void))completionHandler
119 ASSERT(!self.zoomToScaleCompletionHandler);
120 self.zoomToScaleCompletionHandler = completionHandler;
122 [self.scrollView setZoomScale:scale animated:animated];
125 - (void)_keyboardDidShow:(NSNotification *)notification
127 if (self.isShowingKeyboard)
130 self.isShowingKeyboard = YES;
131 if (self.didShowKeyboardCallback)
132 self.didShowKeyboardCallback();
135 - (void)_keyboardDidHide:(NSNotification *)notification
137 if (!self.isShowingKeyboard)
140 self.isShowingKeyboard = NO;
141 if (self.didHideKeyboardCallback)
142 self.didHideKeyboardCallback();
145 - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
147 [super scrollViewWillBeginZooming:scrollView withView:view];
149 if (self.willBeginZoomingCallback)
150 self.willBeginZoomingCallback();
153 - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
155 [super scrollViewDidEndZooming:scrollView withView:view atScale:scale];
157 if (self.didEndZoomingCallback)
158 self.didEndZoomingCallback();
160 if (self.zoomToScaleCompletionHandler) {
161 self.zoomToScaleCompletionHandler();
162 self.zoomToScaleCompletionHandler = nullptr;
166 - (void)_didFinishScrolling
168 [super _didFinishScrolling];
170 if (self.didEndScrollingCallback)
171 self.didEndScrollingCallback();
177 #endif // WK_API_ENABLED