forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestWKWebView.mm
965 lines (795 loc) · 31.5 KB
/
TestWKWebView.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
/*
* Copyright (C) 2016-2020 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "config.h"
#import "TestWKWebView.h"
#import "ClassMethodSwizzler.h"
#import "InstanceMethodSwizzler.h"
#import "TestNavigationDelegate.h"
#import "Utilities.h"
#import <WebKit/WKContentWorld.h>
#import <WebKit/WKUIDelegate.h>
#import <WebKit/WKWebViewConfigurationPrivate.h>
#import <WebKit/WKWebViewPrivateForTesting.h>
#import <WebKit/WebKitPrivate.h>
#import <WebKit/_WKActivatedElementInfo.h>
#import <WebKit/_WKProcessPoolConfiguration.h>
#import <WebKit/_WKTextInputContext.h>
#import <objc/runtime.h>
#import <wtf/BlockPtr.h>
#import <wtf/RetainPtr.h>
#if PLATFORM(MAC)
#import <AppKit/AppKit.h>
#import <Carbon/Carbon.h>
#endif
#if PLATFORM(IOS_FAMILY)
#import "UIKitSPI.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import <wtf/SoftLinking.h>
SOFT_LINK_FRAMEWORK(UIKit)
SOFT_LINK_CLASS(UIKit, UIWindow)
static NSString *overrideBundleIdentifier(id, SEL)
{
return @"com.apple.TestWebKitAPI";
}
#endif // PLATFORM(IOS_FAMILY)
@implementation WKWebView (TestWebKitAPI)
- (void)loadTestPageNamed:(NSString *)pageName
{
NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:pageName withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
[self loadRequest:request];
}
- (void)synchronouslyGoBack
{
[self goBack];
[self _test_waitForDidFinishNavigation];
}
- (void)synchronouslyGoForward
{
[self goForward];
[self _test_waitForDidFinishNavigation];
}
- (void)synchronouslyLoadRequest:(NSURLRequest *)request
{
[self loadRequest:request];
[self _test_waitForDidFinishNavigation];
}
- (void)synchronouslyLoadHTMLString:(NSString *)html baseURL:(NSURL *)url
{
[self loadHTMLString:html baseURL:url];
[self _test_waitForDidFinishNavigation];
}
- (void)synchronouslyLoadHTMLString:(NSString *)html
{
[self synchronouslyLoadHTMLString:html baseURL:[[[NSBundle mainBundle] bundleURL] URLByAppendingPathComponent:@"TestWebKitAPI.resources"]];
}
- (void)synchronouslyLoadHTMLString:(NSString *)html preferences:(WKWebpagePreferences *)preferences
{
[self loadHTMLString:html baseURL:[[[NSBundle mainBundle] bundleURL] URLByAppendingPathComponent:@"TestWebKitAPI.resources"]];
[self _test_waitForDidFinishNavigationWithPreferences:preferences];
}
- (void)synchronouslyLoadTestPageNamed:(NSString *)pageName
{
[self loadTestPageNamed:pageName];
[self _test_waitForDidFinishNavigation];
}
- (BOOL)_synchronouslyExecuteEditCommand:(NSString *)command argument:(NSString *)argument
{
__block bool done = false;
__block bool success;
[self _executeEditCommand:command argument:argument completion:^(BOOL completionSuccess) {
done = true;
success = completionSuccess;
}];
TestWebKitAPI::Util::run(&done);
return success;
}
#if PLATFORM(IOS_FAMILY)
- (UIView <UITextInputPrivate, UITextInputMultiDocument> *)textInputContentView
{
return (UIView <UITextInputPrivate, UITextInputMultiDocument> *)[self valueForKey:@"_currentContentView"];
}
- (NSArray<_WKTextInputContext *> *)synchronouslyRequestTextInputContextsInRect:(CGRect)rect
{
__block bool finished = false;
__block RetainPtr<NSArray<_WKTextInputContext *>> result;
[self _requestTextInputContextsInRect:rect completionHandler:^(NSArray<_WKTextInputContext *> *contexts) {
result = contexts;
finished = true;
}];
TestWebKitAPI::Util::run(&finished);
return result.autorelease();
}
#endif // PLATFORM(IOS_FAMILY)
- (NSUInteger)gpuToWebProcessConnectionCount
{
__block bool done = false;
__block NSUInteger count = 0;
[self _gpuToWebProcessConnectionCountForTesting:^(NSUInteger result) {
done = true;
count = result;
}];
TestWebKitAPI::Util::run(&done);
return count;
}
- (NSString *)contentsAsString
{
__block bool done = false;
__block RetainPtr<NSString> result;
[self _getContentsAsStringWithCompletionHandler:^(NSString *contents, NSError *error) {
result = contents;
done = true;
}];
TestWebKitAPI::Util::run(&done);
return result.autorelease();
}
- (NSArray<NSString *> *)tagsInBody
{
return [self objectByEvaluatingJavaScript:@"Array.from(document.body.getElementsByTagName('*')).map(e => e.tagName)"];
}
- (NSString *)selectedText
{
return [self stringByEvaluatingJavaScript:@"getSelection().toString()"];
}
- (void)expectElementTagsInOrder:(NSArray<NSString *> *)tagNames
{
auto remainingTags = adoptNS([tagNames mutableCopy]);
NSArray<NSString *> *tagsInBody = self.tagsInBody;
for (NSString *tag in tagsInBody.reverseObjectEnumerator) {
if ([tag isEqualToString:[remainingTags lastObject]])
[remainingTags removeLastObject];
if (![remainingTags count])
break;
}
EXPECT_EQ([remainingTags count], 0U);
if ([remainingTags count])
NSLog(@"Expected to find ordered tags: %@ in: %@", tagNames, tagsInBody);
}
- (void)expectElementCount:(NSInteger)count querySelector:(NSString *)querySelector
{
NSString *script = [NSString stringWithFormat:@"document.querySelectorAll('%@').length", querySelector];
EXPECT_EQ(count, [self stringByEvaluatingJavaScript:script].integerValue);
}
- (void)expectElementTag:(NSString *)tagName toComeBefore:(NSString *)otherTagName
{
[self expectElementTagsInOrder:@[tagName, otherTagName]];
}
- (BOOL)evaluateMediaQuery:(NSString *)query
{
return [[self objectByEvaluatingJavaScript:[NSString stringWithFormat:@"window.matchMedia(\"(%@)\").matches", query]] boolValue];
}
- (id)objectByEvaluatingJavaScript:(NSString *)script
{
bool callbackComplete = false;
RetainPtr<id> evalResult;
[self _evaluateJavaScriptWithoutUserGesture:script completionHandler:[&] (id result, NSError *error) {
evalResult = result;
callbackComplete = true;
EXPECT_TRUE(!error);
if (error)
NSLog(@"Encountered error: %@ while evaluating script: %@", error, script);
}];
TestWebKitAPI::Util::run(&callbackComplete);
return evalResult.autorelease();
}
- (id)objectByEvaluatingJavaScriptWithUserGesture:(NSString *)script
{
bool callbackComplete = false;
RetainPtr<id> evalResult;
[self evaluateJavaScript:script completionHandler:[&] (id result, NSError *error) {
evalResult = result;
callbackComplete = true;
EXPECT_TRUE(!error);
if (error)
NSLog(@"Encountered error: %@ while evaluating script: %@", error, script);
}];
TestWebKitAPI::Util::run(&callbackComplete);
return evalResult.autorelease();
}
- (id)objectByCallingAsyncFunction:(NSString *)script withArguments:(NSDictionary *)arguments error:(NSError **)errorOut
{
bool callbackComplete = false;
if (errorOut)
*errorOut = nil;
RetainPtr<id> evalResult;
RetainPtr<NSError> strongError;
[self callAsyncJavaScript:script arguments:arguments inFrame:nil inContentWorld:WKContentWorld.pageWorld completionHandler:[&] (id result, NSError *error) {
evalResult = result;
strongError = error;
callbackComplete = true;
}];
TestWebKitAPI::Util::run(&callbackComplete);
if (errorOut)
*errorOut = strongError.autorelease();
return evalResult.autorelease();
}
- (NSString *)stringByEvaluatingJavaScript:(NSString *)script
{
return [NSString stringWithFormat:@"%@", [self objectByEvaluatingJavaScript:script]];
}
- (unsigned)waitUntilClientWidthIs:(unsigned)expectedClientWidth
{
int timeout = 10;
unsigned clientWidth = 0;
do {
if (timeout != 10)
TestWebKitAPI::Util::runFor(0.1_s);
id result = [self objectByEvaluatingJavaScript:@"function ___forceLayoutAndGetClientWidth___() { document.body.offsetTop; return document.body.clientWidth; }; ___forceLayoutAndGetClientWidth___();"];
clientWidth = [result integerValue];
--timeout;
} while (clientWidth != expectedClientWidth && timeout >= 0);
return clientWidth;
}
@end
@implementation TestMessageHandler {
NSMutableDictionary<NSString *, dispatch_block_t> *_messageHandlers;
BlockPtr<void(NSString *)> _wildcardMessageHandler;
}
- (void)addMessage:(NSString *)message withHandler:(dispatch_block_t)handler
{
if (!_messageHandlers)
_messageHandlers = [NSMutableDictionary dictionary];
_messageHandlers[message] = [handler copy];
}
- (void)setWildcardMessageHandler:(void (^)(NSString *))handler
{
_wildcardMessageHandler = handler;
}
- (void)removeMessage:(NSString *)message
{
[_messageHandlers removeObjectForKey:message];
}
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
dispatch_block_t handler = _messageHandlers[message.body];
if (handler)
handler();
if (_wildcardMessageHandler)
_wildcardMessageHandler(message.body);
}
@end
#if PLATFORM(MAC)
@interface TestWKWebViewHostWindow : NSWindow
#else
@interface TestWKWebViewHostWindow : UIWindow
#endif // PLATFORM(MAC)
@end
@implementation TestWKWebViewHostWindow {
BOOL _forceKeyWindow;
__weak TestWKWebView *_webView;
}
#if PLATFORM(MAC)
static int gEventNumber = 1;
NSEventMask __simulated_forceClickAssociatedEventsMask(id self, SEL _cmd)
{
return NSEventMaskPressure | NSEventMaskLeftMouseDown | NSEventMaskLeftMouseUp | NSEventMaskLeftMouseDragged;
}
- (instancetype)initWithWebView:(TestWKWebView *)webView contentRect:(NSRect)contentRect styleMask:(NSWindowStyleMask)style backing:(NSBackingStoreType)backingStoreType defer:(BOOL)flag
{
if (self = [super initWithContentRect:contentRect styleMask:style backing:backingStoreType defer:flag])
_webView = webView;
return self;
}
- (void)_mouseDownAtPoint:(NSPoint)point simulatePressure:(BOOL)simulatePressure clickCount:(NSUInteger)clickCount modifierFlags:(NSEventModifierFlags)modifierFlags mouseEventType:(NSEventType)mouseEventType
{
if (simulatePressure)
modifierFlags |= NSEventMaskPressure;
NSEvent *event = [NSEvent mouseEventWithType:mouseEventType location:point modifierFlags:modifierFlags timestamp:_webView.eventTimestamp windowNumber:self.windowNumber context:[NSGraphicsContext currentContext] eventNumber:++gEventNumber clickCount:clickCount pressure:simulatePressure];
if (!simulatePressure) {
[self sendEvent:event];
return;
}
IMP simulatedAssociatedEventsMaskImpl = (IMP)__simulated_forceClickAssociatedEventsMask;
Method associatedEventsMaskMethod = class_getInstanceMethod([NSEvent class], @selector(associatedEventsMask));
IMP originalAssociatedEventsMaskImpl = method_setImplementation(associatedEventsMaskMethod, simulatedAssociatedEventsMaskImpl);
@try {
[self sendEvent:event];
} @finally {
// In the case where event sending raises an exception, we still want to restore the original implementation
// to prevent subsequent event sending tests from being affected.
method_setImplementation(associatedEventsMaskMethod, originalAssociatedEventsMaskImpl);
}
}
- (void)_mouseUpAtPoint:(NSPoint)point clickCount:(NSUInteger)clickCount modifierFlags:(NSEventModifierFlags)modifierFlags eventType:(NSEventType)eventType
{
[self sendEvent:[NSEvent mouseEventWithType:eventType location:point modifierFlags:modifierFlags timestamp:_webView.eventTimestamp windowNumber:self.windowNumber context:[NSGraphicsContext currentContext] eventNumber:++gEventNumber clickCount:clickCount pressure:0]];
}
- (BOOL)canBecomeKeyWindow
{
return _webView.forceWindowToBecomeKey || super.canBecomeKeyWindow;
}
#endif
- (BOOL)isKeyWindow
{
return _forceKeyWindow || [super isKeyWindow];
}
#if PLATFORM(IOS_FAMILY)
static NeverDestroyed<RetainPtr<UIWindow>> gOverriddenApplicationKeyWindow;
static NeverDestroyed<std::unique_ptr<InstanceMethodSwizzler>> gApplicationKeyWindowSwizzler;
static NeverDestroyed<std::unique_ptr<InstanceMethodSwizzler>> gSharedApplicationSwizzler;
static void setOverriddenApplicationKeyWindow(UIWindow *window)
{
if (gOverriddenApplicationKeyWindow.get() == window)
return;
if (!UIApplication.sharedApplication) {
InstanceMethodSwizzler bundleIdentifierSwizzler(NSBundle.class, @selector(bundleIdentifier), reinterpret_cast<IMP>(overrideBundleIdentifier));
UIApplicationInitialize();
UIApplicationInstantiateSingleton(UIApplication.class);
}
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
gApplicationKeyWindowSwizzler.get() = makeUnique<InstanceMethodSwizzler>(UIApplication.class, @selector(keyWindow), reinterpret_cast<IMP>(applicationKeyWindowOverride));
});
gOverriddenApplicationKeyWindow.get() = window;
}
static UIWindow *applicationKeyWindowOverride(id, SEL)
{
return gOverriddenApplicationKeyWindow.get().get();
}
- (instancetype)initWithWebView:(TestWKWebView *)webView frame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
_webView = webView;
return self;
}
#endif
- (void)makeKeyWindow
{
if (_forceKeyWindow)
return;
_forceKeyWindow = YES;
#if PLATFORM(MAC)
[[NSNotificationCenter defaultCenter] postNotificationName:NSWindowDidBecomeKeyNotification object:self];
#else
setOverriddenApplicationKeyWindow(self);
[[NSNotificationCenter defaultCenter] postNotificationName:UIWindowDidBecomeKeyNotification object:self];
#endif
}
- (void)resignKeyWindow
{
_forceKeyWindow = NO;
[super resignKeyWindow];
}
@end
#if PLATFORM(IOS_FAMILY)
using InputSessionChangeCount = NSUInteger;
static InputSessionChangeCount nextInputSessionChangeCount()
{
static InputSessionChangeCount gInputSessionChangeCount = 0;
return ++gInputSessionChangeCount;
}
#endif
@implementation TestWKWebView {
RetainPtr<TestWKWebViewHostWindow> _hostWindow;
RetainPtr<TestMessageHandler> _testHandler;
RetainPtr<WKUserScript> _onloadScript;
#if PLATFORM(IOS_FAMILY)
std::unique_ptr<ClassMethodSwizzler> _sharedCalloutBarSwizzler;
InputSessionChangeCount _inputSessionChangeCount;
#endif
#if PLATFORM(MAC)
BOOL _forceWindowToBecomeKey;
NSTimeInterval _eventTimestampOffset;
#endif
}
- (instancetype)initWithFrame:(CGRect)frame
{
auto defaultConfiguration = adoptNS([[WKWebViewConfiguration alloc] init]);
return [self initWithFrame:frame configuration:defaultConfiguration.get()];
}
- (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration
{
return [self initWithFrame:frame configuration:configuration addToWindow:YES];
}
#if PLATFORM(IOS_FAMILY)
static UICalloutBar *suppressUICalloutBar()
{
return nil;
}
#endif
- (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration addToWindow:(BOOL)addToWindow
{
self = [super initWithFrame:frame configuration:configuration];
if (!self)
return nil;
if (addToWindow)
[self _setUpTestWindow:frame];
#if PLATFORM(IOS_FAMILY)
// FIXME: Remove this workaround once <https://webkit.org/b/175204> is fixed.
_sharedCalloutBarSwizzler = makeUnique<ClassMethodSwizzler>([UICalloutBar class], @selector(sharedCalloutBar), reinterpret_cast<IMP>(suppressUICalloutBar));
_inputSessionChangeCount = 0;
#endif
return self;
}
- (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration processPoolConfiguration:(_WKProcessPoolConfiguration *)processPoolConfiguration
{
[configuration setProcessPool:adoptNS([[WKProcessPool alloc] _initWithConfiguration:processPoolConfiguration]).get()];
return [self initWithFrame:frame configuration:configuration];
}
- (void)_setUpTestWindow:(NSRect)frame
{
#if PLATFORM(MAC)
_hostWindow = adoptNS([[TestWKWebViewHostWindow alloc] initWithWebView:self contentRect:frame styleMask:(NSWindowStyleMaskBorderless | NSWindowStyleMaskMiniaturizable) backing:NSBackingStoreBuffered defer:NO]);
[_hostWindow setFrameOrigin:frame.origin];
[_hostWindow setIsVisible:YES];
[_hostWindow contentView].wantsLayer = YES;
[[_hostWindow contentView] addSubview:self];
[_hostWindow makeKeyAndOrderFront:self];
#else
_hostWindow = adoptNS([[TestWKWebViewHostWindow alloc] initWithWebView:self frame:frame]);
[_hostWindow setHidden:NO];
[_hostWindow addSubview:self];
#endif
}
- (void)addToTestWindow
{
if (!_hostWindow) {
[self _setUpTestWindow:self.frame];
return;
}
#if PLATFORM(MAC)
[[_hostWindow contentView] addSubview:self];
#else
[_hostWindow addSubview:self];
#endif
}
- (void)clearMessageHandlers:(NSArray *)messageNames
{
for (NSString *messageName in messageNames)
[_testHandler removeMessage:messageName];
}
- (void)performAfterReceivingMessage:(NSString *)message action:(dispatch_block_t)action
{
if (!_testHandler) {
_testHandler = adoptNS([[TestMessageHandler alloc] init]);
[[[self configuration] userContentController] addScriptMessageHandler:_testHandler.get() name:@"testHandler"];
}
[_testHandler addMessage:message withHandler:action];
}
- (void)performAfterReceivingAnyMessage:(void (^)(NSString *))action
{
if (!_testHandler) {
_testHandler = adoptNS([[TestMessageHandler alloc] init]);
[[[self configuration] userContentController] addScriptMessageHandler:_testHandler.get() name:@"testHandler"];
}
[_testHandler setWildcardMessageHandler:action];
}
- (void)synchronouslyLoadHTMLStringAndWaitUntilAllImmediateChildFramesPaint:(NSString *)html
{
bool didFireDOMLoadEvent = false;
[self performAfterLoading:[&] { didFireDOMLoadEvent = true; }];
[self loadHTMLString:html baseURL:[NSBundle.mainBundle.bundleURL URLByAppendingPathComponent:@"TestWebKitAPI.resources"]];
TestWebKitAPI::Util::run(&didFireDOMLoadEvent);
[self waitForNextPresentationUpdate];
}
- (void)waitForMessage:(NSString *)message
{
__block bool isDoneWaiting = false;
[self performAfterReceivingMessage:message action:^()
{
isDoneWaiting = true;
}];
TestWebKitAPI::Util::run(&isDoneWaiting);
}
- (void)performAfterLoading:(dispatch_block_t)actions
{
NSString *const viewDidLoadMessage = @"TestWKWebViewDidLoad";
if (!_onloadScript) {
NSString *onloadScript = [NSString stringWithFormat:@"window.addEventListener('load', () => window.webkit.messageHandlers.testHandler.postMessage('%@'), true /* useCapture */)", viewDidLoadMessage];
_onloadScript = adoptNS([[WKUserScript alloc] initWithSource:onloadScript injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES]);
[self.configuration.userContentController addUserScript:_onloadScript.get()];
}
[self performAfterReceivingMessage:viewDidLoadMessage action:actions];
}
- (void)waitForNextPresentationUpdate
{
__block bool done = false;
[self _doAfterNextPresentationUpdate:^() {
done = true;
}];
TestWebKitAPI::Util::run(&done);
}
- (void)waitUntilActivityStateUpdateDone
{
__block bool done = false;
[self _doAfterActivityStateUpdate:^() {
done = true;
}];
TestWebKitAPI::Util::run(&done);
}
- (void)forceDarkMode
{
#if HAVE(OS_DARK_MODE_SUPPORT)
#if USE(APPKIT)
[self setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameDarkAqua]];
#else
[self setOverrideUserInterfaceStyle:UIUserInterfaceStyleDark];
#endif
#endif
}
- (NSString *)stylePropertyAtSelectionStart:(NSString *)propertyName
{
NSString *script = [NSString stringWithFormat:@"getComputedStyle(getSelection().getRangeAt(0).startContainer.parentElement)['%@']", propertyName];
return [self stringByEvaluatingJavaScript:script];
}
- (NSString *)stylePropertyAtSelectionEnd:(NSString *)propertyName
{
NSString *script = [NSString stringWithFormat:@"getComputedStyle(getSelection().getRangeAt(0).endContainer.parentElement)['%@']", propertyName];
return [self stringByEvaluatingJavaScript:script];
}
- (void)collapseToStart
{
[self evaluateJavaScript:@"getSelection().collapseToStart()" completionHandler:nil];
}
- (void)collapseToEnd
{
[self evaluateJavaScript:@"getSelection().collapseToEnd()" completionHandler:nil];
}
- (BOOL)selectionRangeHasStartOffset:(int)start endOffset:(int)end
{
__block bool isDone = false;
__block bool matches = true;
[self evaluateJavaScript:@"window.getSelection().getRangeAt(0).startOffset" completionHandler:^(id result, NSError *error) {
if ([(NSNumber *)result intValue] != start)
matches = false;
}];
[self evaluateJavaScript:@"window.getSelection().getRangeAt(0).endOffset" completionHandler:^(id result, NSError *error) {
if ([(NSNumber *)result intValue] != end)
matches = false;
isDone = true;
}];
TestWebKitAPI::Util::run(&isDone);
return matches;
}
- (void)clickOnElementID:(NSString *)elementID
{
[self evaluateJavaScript:[NSString stringWithFormat:@"document.getElementById('%@').click();", elementID] completionHandler:nil];
}
- (void)waitForPendingMouseEvents
{
__block bool doneProcessingMouseEvents = false;
[self _doAfterProcessingAllPendingMouseEvents:^{
doneProcessingMouseEvents = true;
}];
TestWebKitAPI::Util::run(&doneProcessingMouseEvents);
}
- (void)focus
{
#if PLATFORM(MAC)
[_hostWindow makeFirstResponder:self];
#else
[super becomeFirstResponder];
#endif
}
#if PLATFORM(IOS_FAMILY)
- (void)didStartFormControlInteraction
{
_inputSessionChangeCount = nextInputSessionChangeCount();
}
- (void)didEndFormControlInteraction
{
_inputSessionChangeCount = 0;
}
#endif // PLATFORM(IOS_FAMILY)
@end
#if PLATFORM(IOS_FAMILY)
@implementation TestWKWebView (IOSOnly)
- (void)evaluateJavaScriptAndWaitForInputSessionToChange:(NSString *)script
{
auto initialChangeCount = _inputSessionChangeCount;
BOOL hasEmittedWarning = NO;
NSTimeInterval secondsToWaitUntilWarning = 2;
NSTimeInterval startTime = [NSDate timeIntervalSinceReferenceDate];
[self objectByEvaluatingJavaScript:script];
while ([[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]]) {
if (_inputSessionChangeCount != initialChangeCount)
break;
if (hasEmittedWarning || startTime + secondsToWaitUntilWarning >= [NSDate timeIntervalSinceReferenceDate])
continue;
NSLog(@"Warning: expecting input session change count to differ from %lu", static_cast<unsigned long>(initialChangeCount));
hasEmittedWarning = YES;
}
}
- (RetainPtr<NSArray>)selectionRectsAfterPresentationUpdate
{
RetainPtr<TestWKWebView> retainedSelf = self;
__block bool isDone = false;
__block RetainPtr<NSArray> selectionRects;
[self _doAfterNextPresentationUpdate:^() {
selectionRects = [retainedSelf _uiTextSelectionRects];
isDone = true;
}];
TestWebKitAPI::Util::run(&isDone);
return selectionRects;
}
- (CGRect)caretViewRectInContentCoordinates
{
UIView *selectionView = [self.textInputContentView valueForKeyPath:@"interactionAssistant.selectionView"];
CGRect caretFrame = [[selectionView valueForKeyPath:@"caretView.frame"] CGRectValue];
return [selectionView convertRect:caretFrame toView:self.textInputContentView];
}
- (NSArray<NSValue *> *)selectionViewRectsInContentCoordinates
{
NSMutableArray *selectionRects = [NSMutableArray array];
NSArray<UITextSelectionRect *> *rects = [self.textInputContentView valueForKeyPath:@"interactionAssistant.selectionView.rangeView.rects"];
for (UITextSelectionRect *rect in rects)
[selectionRects addObject:[NSValue valueWithCGRect:rect.rect]];
return selectionRects;
}
- (_WKActivatedElementInfo *)activatedElementAtPosition:(CGPoint)position
{
__block RetainPtr<_WKActivatedElementInfo> info;
__block bool finished = false;
[self _requestActivatedElementAtPosition:position completionBlock:^(_WKActivatedElementInfo *elementInfo) {
info = elementInfo;
finished = true;
}];
TestWebKitAPI::Util::run(&finished);
return info.autorelease();
}
static WKContentView *recursiveFindWKContentView(UIView *view)
{
if ([view isKindOfClass:NSClassFromString(@"WKContentView")])
return (WKContentView *)view;
for (UIView *subview in view.subviews) {
WKContentView *contentView = recursiveFindWKContentView(subview);
if (contentView)
return contentView;
}
return nil;
}
- (WKContentView *)wkContentView
{
return recursiveFindWKContentView(self);
}
@end
#endif
#if PLATFORM(MAC)
@implementation TestWKWebView (MacOnly)
- (void)setEventTimestampOffset:(NSTimeInterval)offset
{
_eventTimestampOffset += offset;
}
- (NSTimeInterval)eventTimestamp
{
return GetCurrentEventTime() + _eventTimestampOffset;
}
- (BOOL)forceWindowToBecomeKey
{
return _forceWindowToBecomeKey;
}
- (void)setForceWindowToBecomeKey:(BOOL)forceWindowToBecomeKey
{
_forceWindowToBecomeKey = forceWindowToBecomeKey;
}
- (void)mouseDownAtPoint:(NSPoint)pointInWindow simulatePressure:(BOOL)simulatePressure
{
[self mouseDownAtPoint:pointInWindow simulatePressure:simulatePressure withFlags:0 eventType:NSEventTypeLeftMouseDown];
}
- (void)mouseDownAtPoint:(NSPoint)pointInWindow simulatePressure:(BOOL)simulatePressure withFlags:(NSEventModifierFlags)flags eventType:(NSEventType)eventType
{
[_hostWindow _mouseDownAtPoint:pointInWindow simulatePressure:simulatePressure clickCount:1 modifierFlags:flags mouseEventType:eventType];
}
- (void)mouseUpAtPoint:(NSPoint)pointInWindow
{
[self mouseUpAtPoint:pointInWindow withFlags:0 eventType:NSEventTypeLeftMouseUp];
}
- (void)mouseUpAtPoint:(NSPoint)pointInWindow withFlags:(NSEventModifierFlags)flags eventType:(NSEventType)eventType
{
[_hostWindow _mouseUpAtPoint:pointInWindow clickCount:1 modifierFlags:flags eventType:eventType];
}
- (void)mouseMoveToPoint:(NSPoint)pointInWindow withFlags:(NSEventModifierFlags)flags
{
[self _simulateMouseMove:[self _mouseEventWithType:NSEventTypeMouseMoved atLocation:pointInWindow flags:flags timestamp:self.eventTimestamp clickCount:0]];
}
- (void)sendClicksAtPoint:(NSPoint)pointInWindow numberOfClicks:(NSUInteger)numberOfClicks
{
for (NSUInteger clickCount = 1; clickCount <= numberOfClicks; ++clickCount) {
[_hostWindow _mouseDownAtPoint:pointInWindow simulatePressure:NO clickCount:clickCount modifierFlags:0 mouseEventType:NSEventTypeLeftMouseDown];
[_hostWindow _mouseUpAtPoint:pointInWindow clickCount:clickCount modifierFlags:0 eventType:NSEventTypeLeftMouseUp];
}
}
- (void)sendClickAtPoint:(NSPoint)pointInWindow
{
[self sendClicksAtPoint:pointInWindow numberOfClicks:1];
}
- (BOOL)acceptsFirstMouseAtPoint:(NSPoint)pointInWindow
{
return [self acceptsFirstMouse:[self _mouseEventWithType:NSEventTypeLeftMouseDown atLocation:pointInWindow]];
}
- (void)mouseEnterAtPoint:(NSPoint)pointInWindow
{
[self mouseEntered:[self _mouseEventWithType:NSEventTypeMouseEntered atLocation:pointInWindow]];
}
- (void)mouseDragToPoint:(NSPoint)pointInWindow
{
[self mouseDragged:[self _mouseEventWithType:NSEventTypeLeftMouseDragged atLocation:pointInWindow]];
}
- (NSEvent *)_mouseEventWithType:(NSEventType)type atLocation:(NSPoint)pointInWindow
{
return [self _mouseEventWithType:type atLocation:pointInWindow flags:0 timestamp:self.eventTimestamp clickCount:0];
}
- (NSEvent *)_mouseEventWithType:(NSEventType)type atLocation:(NSPoint)locationInWindow flags:(NSEventModifierFlags)flags timestamp:(NSTimeInterval)timestamp clickCount:(NSUInteger)clickCount
{
switch (type) {
case NSEventTypeMouseEntered:
case NSEventTypeMouseExited:
return [NSEvent enterExitEventWithType:type location:locationInWindow modifierFlags:flags timestamp:timestamp windowNumber:[_hostWindow windowNumber] context:[NSGraphicsContext currentContext] eventNumber:++gEventNumber trackingNumber:1 userData:nil];
default:
return [NSEvent mouseEventWithType:type location:locationInWindow modifierFlags:flags timestamp:timestamp windowNumber:[_hostWindow windowNumber] context:[NSGraphicsContext currentContext] eventNumber:++gEventNumber clickCount:clickCount pressure:0];
}
}
- (void)wheelEventAtPoint:(CGPoint)pointInWindow wheelDelta:(CGSize)delta
{
RetainPtr<CGEventRef> cgScrollEvent = adoptCF(CGEventCreateScrollWheelEvent(nullptr, kCGScrollEventUnitPixel, 2, delta.height, delta.width, 0));
CGPoint locationInGlobalScreenCoordinates = [[self window] convertPointToScreen:pointInWindow];
locationInGlobalScreenCoordinates.y = [[[NSScreen screens] objectAtIndex:0] frame].size.height - locationInGlobalScreenCoordinates.y;
CGEventSetLocation(cgScrollEvent.get(), locationInGlobalScreenCoordinates);
NSEvent* event = [NSEvent eventWithCGEvent:cgScrollEvent.get()];
[self scrollWheel:event];
}
- (NSWindow *)hostWindow
{
return _hostWindow.get();
}
- (void)typeCharacter:(char)character
{
[self typeCharacter:character modifiers:0];
}
- (void)typeCharacter:(char)character modifiers:(NSEventModifierFlags)modifiers
{
NSString *characterAsString = [NSString stringWithFormat:@"%c" , character];
NSEventType keyDownEventType = NSEventTypeKeyDown;
NSEventType keyUpEventType = NSEventTypeKeyUp;
[self keyDown:[NSEvent keyEventWithType:keyDownEventType location:NSZeroPoint modifierFlags:modifiers timestamp:self.eventTimestamp windowNumber:[_hostWindow windowNumber] context:nil characters:characterAsString charactersIgnoringModifiers:characterAsString isARepeat:NO keyCode:character]];
[self keyUp:[NSEvent keyEventWithType:keyUpEventType location:NSZeroPoint modifierFlags:modifiers timestamp:self.eventTimestamp windowNumber:[_hostWindow windowNumber] context:nil characters:characterAsString charactersIgnoringModifiers:characterAsString isARepeat:NO keyCode:character]];
}
@end
#endif // PLATFORM(MAC)
#if PLATFORM(IOS_FAMILY)
@implementation UIView (WKTestingUIViewUtilities)
- (UIView *)wkFirstSubviewWithClass:(Class)targetClass
{
for (UIView *view in self.subviews) {
if ([view isKindOfClass:targetClass])
return view;
UIView *foundSubview = [view wkFirstSubviewWithClass:targetClass];
if (foundSubview)
return foundSubview;
}
return nil;
}
- (UIView *)wkFirstSubviewWithBoundsSize:(CGSize)size
{
for (UIView *view in self.subviews) {
if (CGSizeEqualToSize([view bounds].size, size))
return view;
UIView *foundSubview = [view wkFirstSubviewWithBoundsSize:size];
if (foundSubview)
return foundSubview;
}
return nil;
}
@end
#endif // PLATFORM(IOS_FAMILY)