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