d096cf09c55cdf1c6244662d31adab16b278ec98
[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 @end
315
316 #if PLATFORM(IOS)
317
318 @implementation TestWKWebView (IOSOnly)
319
320 - (UIView <UITextInput> *)textInputContentView
321 {
322     return (UIView <UITextInput> *)[self valueForKey:@"_currentContentView"];
323 }
324
325 - (RetainPtr<NSArray>)selectionRectsAfterPresentationUpdate
326 {
327     RetainPtr<TestWKWebView> retainedSelf = self;
328
329     __block bool isDone = false;
330     __block RetainPtr<NSArray> selectionRects;
331     [self _doAfterNextPresentationUpdate:^() {
332         selectionRects = adoptNS([[retainedSelf _uiTextSelectionRects] retain]);
333         isDone = true;
334     }];
335
336     TestWebKitAPI::Util::run(&isDone);
337     return selectionRects;
338 }
339
340 - (CGRect)caretViewRectInContentCoordinates
341 {
342     UIView *selectionView = [self.textInputContentView valueForKeyPath:@"interactionAssistant.selectionView"];
343     CGRect caretFrame = [[selectionView valueForKeyPath:@"caretView.frame"] CGRectValue];
344     return [selectionView convertRect:caretFrame toView:self.textInputContentView];
345 }
346
347 - (NSArray<NSValue *> *)selectionViewRectsInContentCoordinates
348 {
349     NSMutableArray *selectionRects = [NSMutableArray array];
350     NSArray<UITextSelectionRect *> *rects = [self.textInputContentView valueForKeyPath:@"interactionAssistant.selectionView.rangeView.rects"];
351     for (UITextSelectionRect *rect in rects)
352         [selectionRects addObject:[NSValue valueWithCGRect:rect.rect]];
353     return selectionRects;
354 }
355
356 - (_WKActivatedElementInfo *)activatedElementAtPosition:(CGPoint)position
357 {
358     __block RetainPtr<_WKActivatedElementInfo> info;
359     __block bool finished = false;
360     [self _requestActivatedElementAtPosition:position completionBlock:^(_WKActivatedElementInfo *elementInfo) {
361         info = elementInfo;
362         finished = true;
363     }];
364
365     TestWebKitAPI::Util::run(&finished);
366     return info.autorelease();
367 }
368
369 @end
370
371 #endif
372
373 #if PLATFORM(MAC)
374 @implementation TestWKWebView (MacOnly)
375 - (void)mouseDownAtPoint:(NSPoint)pointInWindow simulatePressure:(BOOL)simulatePressure
376 {
377     [_hostWindow _mouseDownAtPoint:pointInWindow simulatePressure:simulatePressure clickCount:1];
378 }
379
380 - (void)mouseUpAtPoint:(NSPoint)pointInWindow
381 {
382     [_hostWindow _mouseUpAtPoint:pointInWindow clickCount:1];
383 }
384
385 - (void)mouseMoveToPoint:(NSPoint)pointInWindow withFlags:(NSEventModifierFlags)flags
386 {
387     [self mouseMoved:[self _mouseEventWithType:NSEventTypeMouseMoved atLocation:pointInWindow flags:flags timestamp:GetCurrentEventTime() clickCount:0]];
388 }
389
390 - (void)sendClicksAtPoint:(NSPoint)pointInWindow numberOfClicks:(NSUInteger)numberOfClicks
391 {
392     for (NSUInteger clickCount = 1; clickCount <= numberOfClicks; ++clickCount) {
393         [_hostWindow _mouseDownAtPoint:pointInWindow simulatePressure:NO clickCount:clickCount];
394         [_hostWindow _mouseUpAtPoint:pointInWindow clickCount:clickCount];
395     }
396 }
397
398 - (void)mouseEnterAtPoint:(NSPoint)pointInWindow
399 {
400     [self mouseEntered:[self _mouseEventWithType:NSEventTypeMouseEntered atLocation:pointInWindow]];
401 }
402
403 - (void)mouseDragToPoint:(NSPoint)pointInWindow
404 {
405     [self mouseDragged:[self _mouseEventWithType:NSEventTypeLeftMouseDragged atLocation:pointInWindow]];
406 }
407
408 - (NSEvent *)_mouseEventWithType:(NSEventType)type atLocation:(NSPoint)pointInWindow
409 {
410     return [self _mouseEventWithType:type atLocation:pointInWindow flags:0 timestamp:GetCurrentEventTime() clickCount:0];
411 }
412
413 - (NSEvent *)_mouseEventWithType:(NSEventType)type atLocation:(NSPoint)locationInWindow flags:(NSEventModifierFlags)flags timestamp:(NSTimeInterval)timestamp clickCount:(NSUInteger)clickCount
414 {
415     switch (type) {
416     case NSEventTypeMouseEntered:
417     case NSEventTypeMouseExited:
418         return [NSEvent enterExitEventWithType:type location:locationInWindow modifierFlags:flags timestamp:timestamp windowNumber:[_hostWindow windowNumber] context:[NSGraphicsContext currentContext] eventNumber:++gEventNumber trackingNumber:1 userData:nil];
419     default:
420         return [NSEvent mouseEventWithType:type location:locationInWindow modifierFlags:flags timestamp:timestamp windowNumber:[_hostWindow windowNumber] context:[NSGraphicsContext currentContext] eventNumber:++gEventNumber clickCount:clickCount pressure:0];
421     }
422 }
423
424 - (NSWindow *)hostWindow
425 {
426     return _hostWindow.get();
427 }
428
429 - (void)typeCharacter:(char)character
430 {
431     NSString *characterAsString = [NSString stringWithFormat:@"%c" , character];
432     NSEventType keyDownEventType = NSEventTypeKeyDown;
433     NSEventType keyUpEventType = NSEventTypeKeyUp;
434     [self keyDown:[NSEvent keyEventWithType:keyDownEventType location:NSZeroPoint modifierFlags:0 timestamp:GetCurrentEventTime() windowNumber:[_hostWindow windowNumber] context:nil characters:characterAsString charactersIgnoringModifiers:characterAsString isARepeat:NO keyCode:character]];
435     [self keyUp:[NSEvent keyEventWithType:keyUpEventType location:NSZeroPoint modifierFlags:0 timestamp:GetCurrentEventTime() windowNumber:[_hostWindow windowNumber] context:nil characters:characterAsString charactersIgnoringModifiers:characterAsString isARepeat:NO keyCode:character]];
436 }
437
438 @end
439 #endif
440
441 #endif // WK_API_ENABLED