e19979fffbbb13bdad5005ce7b85bcb19519ee8d
[WebKit-https.git] / Tools / WebKitTestRunner / ios / PlatformWebViewIOS.mm
1 /*
2  * Copyright (C) 2014 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 #include "config.h"
27 #include "PlatformWebView.h"
28 #include "TestController.h"
29
30 #import <WebKit/WKImageCG.h>
31 #import <WebKit/WKViewPrivate.h>
32 #import <WebKit/WKPreferencesPrivate.h>
33 #import <wtf/RetainPtr.h>
34
35 @interface WebKitTestRunnerWindow : UIWindow {
36     WTR::PlatformWebView* _platformWebView;
37     CGPoint _fakeOrigin;
38 }
39 @property (nonatomic, assign) WTR::PlatformWebView* platformWebView;
40 @end
41
42 @interface TestRunnerWKView : WKView {
43     BOOL _useTiledDrawing;
44 }
45
46 - (id)initWithFrame:(CGRect)frame contextRef:(WKContextRef)contextRef pageGroupRef:(WKPageGroupRef)pageGroupRef relatedToPage:(WKPageRef)relatedPage useTiledDrawing:(BOOL)useTiledDrawing;
47
48 @property (nonatomic, assign) BOOL useTiledDrawing;
49 @end
50
51 @implementation TestRunnerWKView
52
53 @synthesize useTiledDrawing = _useTiledDrawing;
54
55 - (id)initWithFrame:(CGRect)frame contextRef:(WKContextRef)contextRef pageGroupRef:(WKPageGroupRef)pageGroupRef relatedToPage:(WKPageRef)relatedPage useTiledDrawing:(BOOL)useTiledDrawing
56 {
57     _useTiledDrawing = useTiledDrawing;
58     return [super initWithFrame:frame contextRef:contextRef pageGroupRef:pageGroupRef];
59 }
60
61 - (BOOL)_shouldUseTiledDrawingArea
62 {
63     return _useTiledDrawing;
64 }
65
66 @end
67
68 @implementation WebKitTestRunnerWindow
69 @synthesize platformWebView = _platformWebView;
70
71 - (BOOL)isKeyWindow
72 {
73     return _platformWebView ? _platformWebView->windowIsKey() : YES;
74 }
75
76 - (void)setFrameOrigin:(CGPoint)point
77 {
78     _fakeOrigin = point;
79 }
80
81 // FIXME: these frame gyrations cause the window to go half offscreen.
82 - (void)setFrame:(CGRect)windowFrame
83 {
84     CGRect currentFrame = [super frame];
85
86     _fakeOrigin = windowFrame.origin;
87
88     [super setFrame:CGRectMake(currentFrame.origin.x, currentFrame.origin.y, windowFrame.size.width, windowFrame.size.height)];
89 }
90
91 - (CGRect)frameRespectingFakeOrigin
92 {
93     CGRect currentFrame = [self frame];
94     return CGRectMake(_fakeOrigin.x, _fakeOrigin.y, currentFrame.size.width, currentFrame.size.height);
95 }
96
97 - (CGFloat)backingScaleFactor
98 {
99     return 1;
100 }
101
102 @end
103
104 @interface UIWindow (Details)
105
106 - (void)_setWindowResolution:(CGFloat)resolution displayIfChanged:(BOOL)displayIfChanged;
107
108 @end
109
110 namespace WTR {
111
112 PlatformWebView::PlatformWebView(WKContextRef contextRef, WKPageGroupRef pageGroupRef, WKPageRef relatedPage, WKDictionaryRef options)
113     : m_windowIsKey(true)
114     , m_options(options)
115 {
116     WKRetainPtr<WKStringRef> useTiledDrawingKey(AdoptWK, WKStringCreateWithUTF8CString("TiledDrawing"));
117     WKTypeRef useTiledDrawingValue = options ? WKDictionaryGetItemForKey(options, useTiledDrawingKey.get()) : NULL;
118     bool useTiledDrawing = useTiledDrawingValue && WKBooleanGetValue(static_cast<WKBooleanRef>(useTiledDrawingValue));
119
120     CGRect rect = CGRectMake(0, 0, TestController::viewWidth, TestController::viewHeight);
121     m_view = [[TestRunnerWKView alloc] initWithFrame:rect contextRef:contextRef pageGroupRef:pageGroupRef relatedToPage:relatedPage useTiledDrawing:useTiledDrawing];
122
123     WKPreferencesSetCompositingBordersVisible(WKPageGroupGetPreferences(pageGroupRef), YES);
124     WKPreferencesSetCompositingRepaintCountersVisible(WKPageGroupGetPreferences(pageGroupRef), YES);
125
126     CGRect windowRect = rect;
127     m_window = [[WebKitTestRunnerWindow alloc] initWithFrame:windowRect];
128     m_window.platformWebView = this;
129
130     [m_window addSubview:m_view];
131     [m_window makeKeyAndVisible];
132 }
133
134 void PlatformWebView::resizeTo(unsigned width, unsigned height)
135 {
136     WKRect frame = windowFrame();
137     frame.size.width = width;
138     frame.size.height = height;
139     setWindowFrame(frame);
140 }
141
142 PlatformWebView::~PlatformWebView()
143 {
144     m_window.platformWebView = 0;
145 //    [m_window close];
146     [m_view release];
147     [m_window release];
148 }
149
150 WKPageRef PlatformWebView::page()
151 {
152     return [m_view pageRef];
153 }
154
155 void PlatformWebView::focus()
156 {
157 //    [m_window makeFirstResponder:m_view]; // FIXME: iOS equivalent?
158     setWindowIsKey(true);
159 }
160
161 WKRect PlatformWebView::windowFrame()
162 {
163     CGRect frame = [m_window frameRespectingFakeOrigin];
164
165     WKRect wkFrame;
166     wkFrame.origin.x = frame.origin.x;
167     wkFrame.origin.y = frame.origin.y;
168     wkFrame.size.width = frame.size.width;
169     wkFrame.size.height = frame.size.height;
170     return wkFrame;
171 }
172
173 void PlatformWebView::setWindowFrame(WKRect frame)
174 {
175     [m_window setFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height)];
176     [m_view setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
177 }
178
179 void PlatformWebView::didInitializeClients()
180 {
181     // Set a temporary 1x1 window frame to force a WindowAndViewFramesChanged notification. <rdar://problem/13380145>
182     WKRect wkFrame = windowFrame();
183     [m_window setFrame:CGRectMake(0, 0, 1, 1)];
184     setWindowFrame(wkFrame);
185 }
186
187 void PlatformWebView::addChromeInputField()
188 {
189     UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
190     textField.tag = 1;
191     [m_window addSubview:textField];
192     [textField release];
193 }
194
195 void PlatformWebView::removeChromeInputField()
196 {
197     UITextField* textField = (UITextField*)[m_window viewWithTag:1];
198     if (textField) {
199         [textField removeFromSuperview];
200         makeWebViewFirstResponder();
201         [textField release];
202     }
203 }
204
205 void PlatformWebView::makeWebViewFirstResponder()
206 {
207 //    [m_window makeFirstResponder:m_view];
208 }
209
210 void PlatformWebView::changeWindowScaleIfNeeded(float)
211 {
212     // Retina only surface.
213 }
214
215 WKRetainPtr<WKImageRef> PlatformWebView::windowSnapshotImage()
216 {
217     return 0; // FIXME for iOS?
218 }
219
220 bool PlatformWebView::viewSupportsOptions(WKDictionaryRef options) const
221 {
222     WKRetainPtr<WKStringRef> useTiledDrawingKey(AdoptWK, WKStringCreateWithUTF8CString("TiledDrawing"));
223     WKTypeRef useTiledDrawingValue = WKDictionaryGetItemForKey(options, useTiledDrawingKey.get());
224     bool useTiledDrawing = useTiledDrawingValue && WKBooleanGetValue(static_cast<WKBooleanRef>(useTiledDrawingValue));
225
226     return useTiledDrawing == [(TestRunnerWKView *)m_view useTiledDrawing];
227 }
228
229 } // namespace WTR