forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsyncFunction.mm
324 lines (267 loc) · 13.9 KB
/
AsyncFunction.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
/*
* Copyright (C) 2019 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 "PlatformUtilities.h"
#import "Test.h"
#import "TestUIDelegate.h"
#import "TestWKWebView.h"
#import <WebKit/WKContentWorld.h>
#import <WebKit/WKFrameInfo.h>
#import <WebKit/WKProcessPoolPrivate.h>
#import <WebKit/WKWebViewPrivate.h>
namespace TestWebKitAPI {
TEST(AsyncFunction, Basic)
{
auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
NSError *error;
// Function with no return value.
// Returns JavaScript undefined which translates to nil.
id result = [webView objectByCallingAsyncFunction:@"1" withArguments:nil error:&error];
EXPECT_NULL(error);
EXPECT_EQ(result, nil);
// Function returns explicit null.
// Returns JavaScript null which translates to NSNull.
result = [webView objectByCallingAsyncFunction:@"return null" withArguments:nil error:&error];
EXPECT_NULL(error);
EXPECT_TRUE([result isKindOfClass:[NSNull class]]);
// Function returns a number.
result = [webView objectByCallingAsyncFunction:@"return 1" withArguments:nil error:&error];
EXPECT_NULL(error);
EXPECT_TRUE([result isKindOfClass:[NSNumber class]]);
EXPECT_TRUE([result isEqualToNumber:@1]);
// Function returns a string.
result = [webView objectByCallingAsyncFunction:@"return '1'" withArguments:nil error:&error];
EXPECT_NULL(error);
EXPECT_TRUE([result isKindOfClass:[NSString class]]);
EXPECT_TRUE([result isEqualToString:@"1"]);
// Takes multiple arguments.
result = [webView objectByCallingAsyncFunction:@"return a + b" withArguments:@{ @"a" : @40, @"b" : @2 } error:&error];
EXPECT_NULL(error);
EXPECT_TRUE([result isKindOfClass:[NSNumber class]]);
EXPECT_TRUE([result isEqualToNumber:@42]);
// Takes multiple arguments of different types, follows javascripts "type appending" rules.
result = [webView objectByCallingAsyncFunction:@"return foo + bar" withArguments:@{ @"foo" : @"foo", @"bar" : @42 } error:&error];
EXPECT_NULL(error);
EXPECT_TRUE([result isKindOfClass:[NSString class]]);
EXPECT_TRUE([result isEqualToString:@"foo42"]);
// Invalid JavaScript, should return an error.
result = [webView objectByCallingAsyncFunction:@"retunr null" withArguments:nil error:&error];
EXPECT_FALSE(error == nil);
EXPECT_NULL(result);
}
TEST(AsyncFunction, InvalidArguments)
{
auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
NSError *error;
// Values can only be NSString, NSNumber, NSDate, NSNull, NS(Mutable)Array, NS(Mutable)Dictionary, NSNull, and may only contain those 6 types.
id result = [webView objectByCallingAsyncFunction:@"return 1" withArguments:@{ @"a" : [NSData data] } error:&error];
EXPECT_NULL(result);
EXPECT_FALSE(error == nil);
result = [webView objectByCallingAsyncFunction:@"return 1" withArguments:@{ @"a" : @[ @1, [NSData data] ] } error:&error];
EXPECT_NULL(result);
EXPECT_FALSE(error == nil);
// References an argument that was not provided.
result = [webView objectByCallingAsyncFunction:@"return a + b" withArguments:@{ @"a" : @40 } error:&error];
EXPECT_NULL(result);
EXPECT_FALSE(error == nil);
}
TEST(AsyncFunction, RoundTrip)
{
auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
NSError *error;
// Tests round tripping a whole bunch of argument inputs and verifying the result.
id value = @42;
id arguments = @{ @"a" : value };
id result = [webView objectByCallingAsyncFunction:@"return a" withArguments:arguments error:&error];
EXPECT_NULL(error);
EXPECT_TRUE([value isEqual:result]);
value = [NSNull null];
arguments = @{ @"a" : value };
result = [webView objectByCallingAsyncFunction:@"return a" withArguments:arguments error:&error];
EXPECT_NULL(error);
EXPECT_TRUE([value isEqual:result]);
value = @"Foo";
arguments = @{ @"a" : value };
result = [webView objectByCallingAsyncFunction:@"return a" withArguments:arguments error:&error];
EXPECT_NULL(error);
EXPECT_TRUE([value isEqual:result]);
value = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
arguments = @{ @"a" : value };
result = [webView objectByCallingAsyncFunction:@"return a" withArguments:arguments error:&error];
EXPECT_NULL(error);
EXPECT_TRUE([value isEqual:result]);
value = @[ @1, @2 ];
arguments = @{ @"a" : value };
result = [webView objectByCallingAsyncFunction:@"return a" withArguments:arguments error:&error];
EXPECT_NULL(error);
EXPECT_TRUE([value isEqual:result]);
auto mutableArray = adoptNS([[NSMutableArray alloc] init]);
[mutableArray addObject:value];
arguments = @{ @"a" : mutableArray.get() };
result = [webView objectByCallingAsyncFunction:@"return a" withArguments:arguments error:&error];
EXPECT_NULL(error);
EXPECT_TRUE([mutableArray isEqual:result]);
value = @[ @"foo", [NSDate dateWithTimeIntervalSinceReferenceDate:0] ];
arguments = @{ @"a" : value };
result = [webView objectByCallingAsyncFunction:@"return a" withArguments:arguments error:&error];
EXPECT_NULL(error);
EXPECT_TRUE([value isEqual:result]);
value = @{ @"a" : @1, @"b" : @2 };
arguments = @{ @"a" : value };
result = [webView objectByCallingAsyncFunction:@"return a" withArguments:arguments error:&error];
EXPECT_NULL(error);
EXPECT_TRUE([value isEqual:result]);
auto mutableDictionary = adoptNS((NSMutableDictionary<NSString *, id> *)[[NSMutableDictionary alloc] init]);
mutableDictionary.get()[@"foo"] = value;
arguments = @{ @"a" : mutableDictionary.get() };
result = [webView objectByCallingAsyncFunction:@"return a" withArguments:arguments error:&error];
EXPECT_NULL(error);
EXPECT_TRUE([mutableDictionary isEqual:result]);
value = @{ @"a" : @"foo", @"b" : [NSDate dateWithTimeIntervalSinceReferenceDate:0] };
arguments = @{ @"a" : value };
result = [webView objectByCallingAsyncFunction:@"return a" withArguments:arguments error:&error];
EXPECT_NULL(error);
EXPECT_TRUE([value isEqual:result]);
value = @{ @"a" : @{ @"a" : @1 } };
arguments = @{ @"a" : value };
result = [webView objectByCallingAsyncFunction:@"return a" withArguments:arguments error:&error];
EXPECT_NULL(error);
EXPECT_TRUE([value isEqual:result]);
}
TEST(AsyncFunction, Promise)
{
auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
[webView callAsyncJavaScript:@"shouldn't crash" arguments:@{ @"invalidparameter" : webView.get() } inFrame:nil inContentWorld:WKContentWorld.pageWorld completionHandler:nil];
NSString *functionBody = @"return new Promise(function(resolve, reject) { setTimeout(function(){ resolve(42) }, 0); })";
bool done = false;
[webView callAsyncJavaScript:functionBody arguments:nil inFrame:nil inContentWorld:WKContentWorld.pageWorld completionHandler:[&] (id result, NSError *error) {
EXPECT_NULL(error);
EXPECT_TRUE([result isKindOfClass:[NSNumber class]]);
EXPECT_TRUE([result isEqualToNumber:@42]);
done = true;
}];
TestWebKitAPI::Util::run(&done);
done = false;
functionBody = @"return new Promise(function(resolve, reject) { setTimeout(function(){ reject('Rejected!') }, 0); })";
done = false;
[webView callAsyncJavaScript:functionBody arguments:nil inFrame:nil inContentWorld:WKContentWorld.pageWorld completionHandler:[&] (id result, NSError *error) {
EXPECT_NULL(result);
EXPECT_TRUE(error != nil);
EXPECT_TRUE([[error description] containsString:@"Rejected!"]);
done = true;
}];
TestWebKitAPI::Util::run(&done);
functionBody = @"let p = new Proxy(function(resolve, reject) { setTimeout(function() { resolve(42); }, 0); }, { }); return { then: p };";
done = false;
[webView callAsyncJavaScript:functionBody arguments:nil inFrame:nil inContentWorld:WKContentWorld.pageWorld completionHandler:[&] (id result, NSError *error) {
EXPECT_NULL(error);
EXPECT_TRUE([result isKindOfClass:[NSNumber class]]);
EXPECT_TRUE([result isEqualToNumber:@42]);
done = true;
}];
TestWebKitAPI::Util::run(&done);
functionBody = @"let p = new Proxy(function(resolve, reject) { setTimeout(function() { reject('Rejected!'); }, 0); }, { }); return { then: p };";
done = false;
[webView callAsyncJavaScript:functionBody arguments:nil inFrame:nil inContentWorld:WKContentWorld.pageWorld completionHandler:[&] (id result, NSError *error) {
EXPECT_NULL(result);
EXPECT_TRUE(error != nil);
EXPECT_TRUE([[error description] containsString:@"Rejected!"]);
done = true;
}];
TestWebKitAPI::Util::run(&done);
// Verify we can await for a promise to be resolved before returning.
functionBody = @"var r = 0; var p = new Promise(function(fulfill, reject) { setTimeout(function(){ r = 42; fulfill(); }, 5);}); await p; return r;";
done = false;
[webView callAsyncJavaScript:functionBody arguments:nil inFrame:nil inContentWorld:WKContentWorld.pageWorld completionHandler:[&] (id result, NSError *error) {
EXPECT_NULL(error);
EXPECT_TRUE([result isKindOfClass:[NSNumber class]]);
EXPECT_TRUE([result isEqualToNumber:@42]);
done = true;
}];
TestWebKitAPI::Util::run(&done);
// Returning an already resolved promise gives the value it was resolved with.
functionBody = @"var p = new Promise(function(fulfill, reject) { setTimeout(function(){ fulfill('Fulfilled!') }, 5);}); await p; return p;";
done = false;
[webView callAsyncJavaScript:functionBody arguments:nil inFrame:nil inContentWorld:WKContentWorld.pageWorld completionHandler:[&] (id result, NSError *error) {
EXPECT_NULL(error);
EXPECT_TRUE([result isKindOfClass:[NSString class]]);
EXPECT_TRUE([result isEqualToString:@"Fulfilled!"]);
done = true;
}];
TestWebKitAPI::Util::run(&done);
// Chaining thenables should work.
functionBody = @"var p = new Promise(function (r) { r(new Promise(function (r) { r(42); })); }); await p; return 'Done';";
done = false;
[webView callAsyncJavaScript:functionBody arguments:nil inFrame:nil inContentWorld:WKContentWorld.pageWorld completionHandler:[&] (id result, NSError *error) {
EXPECT_NULL(error);
EXPECT_TRUE([result isKindOfClass:[NSString class]]);
EXPECT_TRUE([result isEqualToString:@"Done"]);
done = true;
}];
TestWebKitAPI::Util::run(&done);
// Promises known to become unreachable (e.g. via garbage collection) should call back with an error.
done = false;
functionBody = @"return new Promise(function(resolve, reject) { })";
for (int i = 0; i < 500; ++i) {
[webView callAsyncJavaScript:functionBody arguments:nil inFrame:nil inContentWorld:WKContentWorld.pageWorld completionHandler:[&] (id result, NSError *error) {
EXPECT_NULL(result);
EXPECT_TRUE(error != nil);
EXPECT_TRUE([[error description] containsString:@"no longer reachable"]);
done = true;
}];
}
[webView.get().configuration.processPool _garbageCollectJavaScriptObjectsForTesting];
TestWebKitAPI::Util::run(&done);
}
TEST(AsyncFunction, PromiseDetachedFrame)
{
auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
auto uiDelegate = adoptNS([[TestUIDelegate alloc] init]);
[webView setUIDelegate:uiDelegate.get()];
__block RetainPtr<WKFrameInfo> subframe;
__block bool doneGettingSubframe = false;
uiDelegate.get().runJavaScriptAlertPanelWithMessage = ^(WKWebView *, NSString *, WKFrameInfo * frame, void (^completionHandler)(void)) {
subframe = frame;
doneGettingSubframe = true;
completionHandler();
};
[webView synchronouslyLoadHTMLString:@"<iframe src='javascript:alert()'></iframe>"];
TestWebKitAPI::Util::run(&doneGettingSubframe);
ASSERT_TRUE(!!subframe);
EXPECT_FALSE([subframe isMainFrame]);
auto pid = [webView _webProcessIdentifier];
EXPECT_NE(pid, 0);
NSString *functionBody = @"return new Promise(function(resolve, reject) { setTimeout(function(){ top.setTimeout(function() { resolve(42); }, 100); top.document.querySelector('iframe').remove(); }, 0); })";
bool done = false;
[webView callAsyncJavaScript:functionBody arguments:nil inFrame:subframe.get() inContentWorld:WKContentWorld.pageWorld completionHandler:[&] (id result, NSError *error) {
EXPECT_NULL(error);
EXPECT_TRUE([result isKindOfClass:[NSNumber class]]);
EXPECT_TRUE([result isEqualToNumber:@42]);
done = true;
}];
TestWebKitAPI::Util::run(&done);
EXPECT_EQ([webView _webProcessIdentifier], pid);
}
}