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