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