818a02dd73244b55bd0ba3ca80f72c2bcf018ddc
[WebKit-https.git] / Tools / TestWebKitAPI / cocoa / TestWKWebView.mm
1 /*
2  * Copyright (C) 2016 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 "TestWKWebView.h"
28
29 #if WK_API_ENABLED
30
31 #import "TestNavigationDelegate.h"
32 #import "Utilities.h"
33
34 #import <WebKit/WKWebViewConfigurationPrivate.h>
35 #import <WebKit/WebKitPrivate.h>
36 #import <WebKit/_WKActivatedElementInfo.h>
37 #import <WebKit/_WKProcessPoolConfiguration.h>
38 #import <objc/runtime.h>
39 #import <wtf/RetainPtr.h>
40
41 #if PLATFORM(MAC)
42 #import <AppKit/AppKit.h>
43 #import <Carbon/Carbon.h>
44 #import <wtf/mac/AppKitCompatibilityDeclarations.h>
45 #endif
46
47 #if PLATFORM(IOS)
48 #import <UIKit/UIKit.h>
49 #import <wtf/SoftLinking.h>
50 SOFT_LINK_FRAMEWORK(UIKit)
51 SOFT_LINK_CLASS(UIKit, UIWindow)
52
53 @implementation WKWebView (WKWebViewTestingQuicks)
54
55 // TestWebKitAPI is currently not a UIApplication so we are unable to track if it is in
56 // the background or not (https://bugs.webkit.org/show_bug.cgi?id=175204). This can
57 // cause our processes to get suspended on iOS. We work around this by having
58 // WKWebView._isBackground always return NO in the context of API tests.
59 - (BOOL)_isBackground
60 {
61     return NO;
62 }
63 @end
64 #endif
65
66 @implementation TestMessageHandler {
67     NSMutableDictionary<NSString *, dispatch_block_t> *_messageHandlers;
68 }
69
70 - (void)addMessage:(NSString *)message withHandler:(dispatch_block_t)handler
71 {
72     if (!_messageHandlers)
73         _messageHandlers = [NSMutableDictionary dictionary];
74
75     _messageHandlers[message] = [handler copy];
76 }
77
78 - (void)removeMessage:(NSString *)message
79 {
80     [_messageHandlers removeObjectForKey:message];
81 }
82
83 - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
84 {
85     dispatch_block_t handler = _messageHandlers[message.body];
86     if (handler)
87         handler();
88 }
89
90 @end
91
92 #if PLATFORM(MAC)
93 @interface TestWKWebViewHostWindow : NSWindow
94 #else
95 @interface TestWKWebViewHostWindow : UIWindow
96 #endif // PLATFORM(MAC)
97 @end
98
99 @implementation TestWKWebViewHostWindow {
100     BOOL _forceKeyWindow;
101 }
102
103 #if PLATFORM(MAC)
104 static int gEventNumber = 1;
105
106 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101003
107 NSEventMask __simulated_forceClickAssociatedEventsMask(id self, SEL _cmd)
108 {
109     return NSEventMaskPressure | NSEventMaskLeftMouseDown | NSEventMaskLeftMouseUp | NSEventMaskLeftMouseDragged;
110 }
111 #endif
112
113 - (void)_mouseDownAtPoint:(NSPoint)point simulatePressure:(BOOL)simulatePressure clickCount:(NSUInteger)clickCount
114 {
115     NSEventType mouseEventType = NSEventTypeLeftMouseDown;
116
117     NSEventMask modifierFlags = 0;
118 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101003
119     if (simulatePressure)
120         modifierFlags |= NSEventMaskPressure;
121 #else
122     simulatePressure = NO;
123 #endif
124
125     NSEvent *event = [NSEvent mouseEventWithType:mouseEventType location:point modifierFlags:modifierFlags timestamp:GetCurrentEventTime() windowNumber:self.windowNumber context:[NSGraphicsContext currentContext] eventNumber:++gEventNumber clickCount:clickCount pressure:simulatePressure];
126     if (!simulatePressure) {
127         [self sendEvent:event];
128         return;
129     }
130
131 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101003
132     IMP simulatedAssociatedEventsMaskImpl = (IMP)__simulated_forceClickAssociatedEventsMask;
133     Method associatedEventsMaskMethod = class_getInstanceMethod([NSEvent class], @selector(associatedEventsMask));
134     IMP originalAssociatedEventsMaskImpl = method_setImplementation(associatedEventsMaskMethod, simulatedAssociatedEventsMaskImpl);
135     @try {
136         [self sendEvent:event];
137     } @finally {
138         // In the case where event sending raises an exception, we still want to restore the original implementation
139         // to prevent subsequent event sending tests from being affected.
140         method_setImplementation(associatedEventsMaskMethod, originalAssociatedEventsMaskImpl);
141     }
142 #endif
143 }
144
145 - (void)_mouseUpAtPoint:(NSPoint)point clickCount:(NSUInteger)clickCount
146 {
147     [self sendEvent:[NSEvent mouseEventWithType:NSEventTypeLeftMouseUp location:point modifierFlags:0 timestamp:GetCurrentEventTime() windowNumber:self.windowNumber context:[NSGraphicsContext currentContext] eventNumber:++gEventNumber clickCount:clickCount pressure:0]];
148 }
149 #endif // PLATFORM(MAC)
150
151 - (BOOL)isKeyWindow
152 {
153     return _forceKeyWindow || [super isKeyWindow];
154 }
155
156 - (void)makeKeyWindow
157 {
158     if (_forceKeyWindow)
159         return;
160
161     _forceKeyWindow = YES;
162 #if PLATFORM(MAC)
163     [[NSNotificationCenter defaultCenter] postNotificationName:NSWindowDidBecomeKeyNotification object:self];
164 #else
165     [[NSNotificationCenter defaultCenter] postNotificationName:UIWindowDidBecomeKeyNotification object:self];
166 #endif
167 }
168
169 - (void)resignKeyWindow
170 {
171     _forceKeyWindow = NO;
172     [super resignKeyWindow];
173 }
174
175 @end
176
177 @implementation TestWKWebView {
178     RetainPtr<TestWKWebViewHostWindow> _hostWindow;
179     RetainPtr<TestMessageHandler> _testHandler;
180 }
181
182 - (instancetype)initWithFrame:(CGRect)frame
183 {
184     WKWebViewConfiguration *defaultConfiguration = [[[WKWebViewConfiguration alloc] init] autorelease];
185     return [self initWithFrame:frame configuration:defaultConfiguration];
186 }
187
188 - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration
189 {
190     return [self initWithFrame:frame configuration:configuration addToWindow:YES];
191 }
192
193 - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration addToWindow:(BOOL)addToWindow
194 {
195     self = [super initWithFrame:frame configuration:configuration];
196     if (!self)
197         return nil;
198
199     if (addToWindow)
200         [self _setUpTestWindow:frame];
201
202     return self;
203 }
204
205 - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration processPoolConfiguration:(_WKProcessPoolConfiguration *)processPoolConfiguration
206 {
207     [configuration setProcessPool:[[[WKProcessPool alloc] _initWithConfiguration:processPoolConfiguration] autorelease]];
208     return [self initWithFrame:frame configuration:configuration];
209 }
210
211 - (void)_setUpTestWindow:(NSRect)frame
212 {
213 #if PLATFORM(MAC)
214     _hostWindow = adoptNS([[TestWKWebViewHostWindow alloc] initWithContentRect:frame styleMask:NSWindowStyleMaskBorderless backing:NSBackingStoreBuffered defer:NO]);
215     [_hostWindow setFrameOrigin:NSMakePoint(0, 0)];
216     [_hostWindow setIsVisible:YES];
217     [_hostWindow contentView].wantsLayer = YES;
218     [[_hostWindow contentView] addSubview:self];
219     [_hostWindow makeKeyAndOrderFront:self];
220 #else
221     _hostWindow = adoptNS([[TestWKWebViewHostWindow alloc] initWithFrame:frame]);
222     [_hostWindow setHidden:NO];
223     [_hostWindow addSubview:self];
224 #endif
225 }
226
227 - (void)clearMessageHandlers:(NSArray *)messageNames
228 {
229     for (NSString *messageName in messageNames)
230         [_testHandler removeMessage:messageName];
231 }
232
233 - (void)performAfterReceivingMessage:(NSString *)message action:(dispatch_block_t)action
234 {
235     if (!_testHandler) {
236         _testHandler = adoptNS([[TestMessageHandler alloc] init]);
237         [[[self configuration] userContentController] addScriptMessageHandler:_testHandler.get() name:@"testHandler"];
238     }
239
240     [_testHandler addMessage:message withHandler:action];
241 }
242
243 - (void)loadTestPageNamed:(NSString *)pageName
244 {
245     NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:pageName withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
246     [self loadRequest:request];
247 }
248
249 - (void)synchronouslyLoadHTMLString:(NSString *)html
250 {
251     NSURL *testResourceURL = [[[NSBundle mainBundle] bundleURL] URLByAppendingPathComponent:@"TestWebKitAPI.resources"];
252     [self loadHTMLString:html baseURL:testResourceURL];
253     [self _test_waitForDidFinishNavigation];
254 }
255
256 - (void)synchronouslyLoadTestPageNamed:(NSString *)pageName
257 {
258     [self loadTestPageNamed:pageName];
259     [self _test_waitForDidFinishNavigation];
260 }
261
262 - (id)objectByEvaluatingJavaScript:(NSString *)script
263 {
264     bool isWaitingForJavaScript = false;
265     RetainPtr<id> evalResult;
266     [self _evaluateJavaScriptWithoutUserGesture:script completionHandler:[&] (id result, NSError *error) {
267         evalResult = result;
268         isWaitingForJavaScript = true;
269         EXPECT_TRUE(!error);
270         if (error)
271             NSLog(@"Encountered error: %@ while evaluating script: %@", error, script);
272     }];
273     TestWebKitAPI::Util::run(&isWaitingForJavaScript);
274     return evalResult.autorelease();
275 }
276
277 - (NSString *)stringByEvaluatingJavaScript:(NSString *)script
278 {
279     return [NSString stringWithFormat:@"%@", [self objectByEvaluatingJavaScript:script]];
280 }
281
282 - (void)waitForMessage:(NSString *)message
283 {
284     __block bool isDoneWaiting = false;
285     [self performAfterReceivingMessage:message action:^()
286     {
287         isDoneWaiting = true;
288     }];
289     TestWebKitAPI::Util::run(&isDoneWaiting);
290 }
291
292 - (void)performAfterLoading:(dispatch_block_t)actions {
293     TestMessageHandler *handler = [[TestMessageHandler alloc] init];
294     [handler addMessage:@"loaded" withHandler:actions];
295
296     NSString *onloadScript = @"window.onload = () => window.webkit.messageHandlers.onloadHandler.postMessage('loaded')";
297     WKUserScript *script = [[WKUserScript alloc] initWithSource:onloadScript injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
298
299     WKUserContentController* contentController = [[self configuration] userContentController];
300     [contentController addUserScript:script];
301     [contentController addScriptMessageHandler:handler name:@"onloadHandler"];
302 }
303
304 - (void)waitForNextPresentationUpdate
305 {
306     __block bool done = false;
307     [self _doAfterNextPresentationUpdate:^() {
308         done = true;
309     }];
310
311     TestWebKitAPI::Util::run(&done);
312 }
313
314 - (void)typeCharacter:(char)character {
315     NSString *characterAsString = [NSString stringWithFormat:@"%c" , character];
316     NSEventType keyDownEventType = NSEventTypeKeyDown;
317     NSEventType keyUpEventType = NSEventTypeKeyUp;
318     [self keyDown:[NSEvent keyEventWithType:keyDownEventType location:NSZeroPoint modifierFlags:0 timestamp:GetCurrentEventTime() windowNumber:[_hostWindow windowNumber] context:nil characters:characterAsString charactersIgnoringModifiers:characterAsString isARepeat:NO keyCode:character]];
319     [self keyUp:[NSEvent keyEventWithType:keyUpEventType location:NSZeroPoint modifierFlags:0 timestamp:GetCurrentEventTime() windowNumber:[_hostWindow windowNumber] context:nil characters:characterAsString charactersIgnoringModifiers:characterAsString isARepeat:NO keyCode:character]];
320 }
321
322 @end
323
324 #if PLATFORM(IOS)
325
326 @implementation TestWKWebView (IOSOnly)
327
328 - (UIView <UITextInput> *)textInputContentView
329 {
330     return (UIView <UITextInput> *)[self valueForKey:@"_currentContentView"];
331 }
332
333 - (RetainPtr<NSArray>)selectionRectsAfterPresentationUpdate
334 {
335     RetainPtr<TestWKWebView> retainedSelf = self;
336
337     __block bool isDone = false;
338     __block RetainPtr<NSArray> selectionRects;
339     [self _doAfterNextPresentationUpdate:^() {
340         selectionRects = adoptNS([[retainedSelf _uiTextSelectionRects] retain]);
341         isDone = true;
342     }];
343
344     TestWebKitAPI::Util::run(&isDone);
345     return selectionRects;
346 }
347
348 - (_WKActivatedElementInfo *)activatedElementAtPosition:(CGPoint)position
349 {
350     __block RetainPtr<_WKActivatedElementInfo> info;
351     __block bool finished = false;
352     [self _requestActivatedElementAtPosition:position completionBlock:^(_WKActivatedElementInfo *elementInfo) {
353         info = elementInfo;
354         finished = true;
355     }];
356
357     TestWebKitAPI::Util::run(&finished);
358     return info.autorelease();
359 }
360
361 @end
362
363 #endif
364
365 #if PLATFORM(MAC)
366 @implementation TestWKWebView (MacOnly)
367 - (void)mouseDownAtPoint:(NSPoint)point simulatePressure:(BOOL)simulatePressure
368 {
369     [_hostWindow _mouseDownAtPoint:point simulatePressure:simulatePressure clickCount:1];
370 }
371
372 - (void)mouseUpAtPoint:(NSPoint)point
373 {
374     [_hostWindow _mouseUpAtPoint:point clickCount:1];
375 }
376
377 - (void)mouseMoveToPoint:(NSPoint)point withFlags:(NSEventModifierFlags)flags
378 {
379     [self mouseMoved:[NSEvent mouseEventWithType:NSEventTypeMouseMoved location:point modifierFlags:flags timestamp:GetCurrentEventTime() windowNumber:[_hostWindow windowNumber] context:[NSGraphicsContext currentContext] eventNumber:++gEventNumber clickCount:0 pressure:0]];
380 }
381
382 - (void)sendClicksAtPoint:(NSPoint)point numberOfClicks:(NSUInteger)numberOfClicks
383 {
384     for (NSUInteger clickCount = 1; clickCount <= numberOfClicks; ++clickCount) {
385         [_hostWindow _mouseDownAtPoint:point simulatePressure:NO clickCount:clickCount];
386         [_hostWindow _mouseUpAtPoint:point clickCount:clickCount];
387     }
388 }
389
390 - (NSWindow *)hostWindow
391 {
392     return _hostWindow.get();
393 }
394 @end
395 #endif
396
397 #endif // WK_API_ENABLED