2 * Copyright (C) 2014 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "PlatformWebView.h"
28 #include "TestController.h"
30 #import <WebKit/WKImageCG.h>
31 #import <WebKit/WKPreferencesPrivate.h>
32 #import <WebKit/WKWebViewConfiguration.h>
33 #import <WebKit/WKWebViewPrivate.h>
34 #import <wtf/RetainPtr.h>
36 @interface WKWebView (Details)
37 - (WKPageRef)_pageForTesting;
40 @interface WebKitTestRunnerWindow : UIWindow {
41 WTR::PlatformWebView* _platformWebView;
45 @property (nonatomic, assign) WTR::PlatformWebView* platformWebView;
48 @implementation WebKitTestRunnerWindow
49 @synthesize platformWebView = _platformWebView;
51 - (id)initWithFrame:(CGRect)frame
53 if ((self = [super initWithFrame:frame]))
60 return _platformWebView ? _platformWebView->windowIsKey() : YES;
63 - (void)setFrameOrigin:(CGPoint)point
68 - (void)setFrame:(CGRect)windowFrame
71 [super setFrame:windowFrame];
75 CGRect currentFrame = [super frame];
77 _fakeOrigin = windowFrame.origin;
79 [super setFrame:CGRectMake(currentFrame.origin.x, currentFrame.origin.y, windowFrame.size.width, windowFrame.size.height)];
82 - (CGRect)frameRespectingFakeOrigin
84 CGRect currentFrame = [self frame];
85 return CGRectMake(_fakeOrigin.x, _fakeOrigin.y, currentFrame.size.width, currentFrame.size.height);
88 - (CGFloat)backingScaleFactor
95 @interface UIWindow ()
97 - (void)_setWindowResolution:(CGFloat)resolution displayIfChanged:(BOOL)displayIfChanged;
103 PlatformWebView::PlatformWebView(WKWebViewConfiguration* configuration, const TestOptions& options)
104 : m_windowIsKey(true)
107 CGRect rect = CGRectMake(0, 0, TestController::viewWidth, TestController::viewHeight);
108 m_view = [[WKWebView alloc] initWithFrame:rect configuration:configuration];
110 CGRect windowRect = rect;
111 m_window = [[WebKitTestRunnerWindow alloc] initWithFrame:windowRect];
112 m_window.platformWebView = this;
114 [m_window addSubview:m_view];
115 [m_window makeKeyAndVisible];
118 void PlatformWebView::resizeTo(unsigned width, unsigned height)
120 WKRect frame = windowFrame();
121 frame.size.width = width;
122 frame.size.height = height;
123 setWindowFrame(frame);
126 PlatformWebView::~PlatformWebView()
128 m_window.platformWebView = 0;
133 WKPageRef PlatformWebView::page()
135 return [m_view _pageForTesting];
138 void PlatformWebView::focus()
140 makeWebViewFirstResponder();
141 setWindowIsKey(true);
144 WKRect PlatformWebView::windowFrame()
146 CGRect frame = [m_window frameRespectingFakeOrigin];
149 wkFrame.origin.x = frame.origin.x;
150 wkFrame.origin.y = frame.origin.y;
151 wkFrame.size.width = frame.size.width;
152 wkFrame.size.height = frame.size.height;
156 void PlatformWebView::setWindowFrame(WKRect frame)
158 [m_window setFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height)];
159 [platformView() setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
162 void PlatformWebView::didInitializeClients()
164 // Set a temporary 1x1 window frame to force a WindowAndViewFramesChanged notification. <rdar://problem/13380145>
165 WKRect wkFrame = windowFrame();
166 [m_window setFrame:CGRectMake(0, 0, 1, 1)];
167 setWindowFrame(wkFrame);
170 void PlatformWebView::addChromeInputField()
172 UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
174 [m_window addSubview:textField];
178 void PlatformWebView::removeChromeInputField()
180 UITextField* textField = (UITextField*)[m_window viewWithTag:1];
182 [textField removeFromSuperview];
183 makeWebViewFirstResponder();
188 void PlatformWebView::makeWebViewFirstResponder()
190 // FIXME: iOS equivalent?
191 // [m_window makeFirstResponder:m_view];
194 void PlatformWebView::changeWindowScaleIfNeeded(float)
196 // Retina only surface.
199 WKRetainPtr<WKImageRef> PlatformWebView::windowSnapshotImage()
201 // FIXME: Need an implementation of this, or we're depending on software paints!
205 bool PlatformWebView::viewSupportsOptions(const TestOptions& options) const
210 void PlatformWebView::setNavigationGesturesEnabled(bool enabled)