2950910ae404dd0362df606b9f1db7b71729bab2
[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.willBeginZoomingCallback = nil;
77     self.didEndZoomingCallback = nil;
78     self.didShowKeyboardCallback = nil;
79     self.didHideKeyboardCallback = nil;
80     self.didEndScrollingCallback = nil;
81
82     self.zoomToScaleCompletionHandler = nil;
83     self.showKeyboardCompletionHandler = nil;
84
85     [super dealloc];
86 }
87
88 - (void)zoomToScale:(double)scale animated:(BOOL)animated completionHandler:(void (^)(void))completionHandler
89 {
90     ASSERT(!self.zoomToScaleCompletionHandler);
91     self.zoomToScaleCompletionHandler = completionHandler;
92
93     [self.scrollView setZoomScale:scale animated:animated];
94 }
95
96 - (void)_keyboardDidShow:(NSNotification *)notification
97 {
98     if (self.didShowKeyboardCallback)
99         self.didShowKeyboardCallback();
100 }
101
102 - (void)_keyboardDidHide:(NSNotification *)notification
103 {
104     if (self.didHideKeyboardCallback)
105         self.didHideKeyboardCallback();
106 }
107
108 - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
109 {
110     [super scrollViewWillBeginZooming:scrollView withView:view];
111
112     if (self.willBeginZoomingCallback)
113         self.willBeginZoomingCallback();
114 }
115
116 - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
117 {
118     [super scrollViewDidEndZooming:scrollView withView:view atScale:scale];
119     
120     if (self.didEndZoomingCallback)
121         self.didEndZoomingCallback();
122
123     if (self.zoomToScaleCompletionHandler) {
124         self.zoomToScaleCompletionHandler();
125         self.zoomToScaleCompletionHandler = nullptr;
126     }
127 }
128
129 - (void)_didFinishScrolling
130 {
131     [super _didFinishScrolling];
132
133     if (self.didEndScrollingCallback)
134         self.didEndScrollingCallback();
135 }
136 #endif
137
138 @end
139
140 #endif // WK_API_ENABLED