-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
testharness.js
4971 lines (4510 loc) · 182 KB
/
testharness.js
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
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*global self*/
/*jshint latedef: nofunc*/
/* Documentation: https://web-platform-tests.org/writing-tests/testharness-api.html
* (../docs/_writing-tests/testharness-api.md) */
(function (global_scope)
{
// default timeout is 10 seconds, test can override if needed
var settings = {
output:true,
harness_timeout:{
"normal":10000,
"long":60000
},
test_timeout:null,
message_events: ["start", "test_state", "result", "completion"],
debug: false,
};
var xhtml_ns = "http://www.w3.org/1999/xhtml";
/*
* TestEnvironment is an abstraction for the environment in which the test
* harness is used. Each implementation of a test environment has to provide
* the following interface:
*
* interface TestEnvironment {
* // Invoked after the global 'tests' object has been created and it's
* // safe to call add_*_callback() to register event handlers.
* void on_tests_ready();
*
* // Invoked after setup() has been called to notify the test environment
* // of changes to the test harness properties.
* void on_new_harness_properties(object properties);
*
* // Should return a new unique default test name.
* DOMString next_default_test_name();
*
* // Should return the test harness timeout duration in milliseconds.
* float test_timeout();
* };
*/
/*
* A test environment with a DOM. The global object is 'window'. By default
* test results are displayed in a table. Any parent windows receive
* callbacks or messages via postMessage() when test events occur. See
* apisample11.html and apisample12.html.
*/
function WindowTestEnvironment() {
this.name_counter = 0;
this.window_cache = null;
this.output_handler = null;
this.all_loaded = false;
var this_obj = this;
this.message_events = [];
this.dispatched_messages = [];
this.message_functions = {
start: [add_start_callback, remove_start_callback,
function (properties) {
this_obj._dispatch("start_callback", [properties],
{type: "start", properties: properties});
}],
test_state: [add_test_state_callback, remove_test_state_callback,
function(test) {
this_obj._dispatch("test_state_callback", [test],
{type: "test_state",
test: test.structured_clone()});
}],
result: [add_result_callback, remove_result_callback,
function (test) {
this_obj.output_handler.show_status();
this_obj._dispatch("result_callback", [test],
{type: "result",
test: test.structured_clone()});
}],
completion: [add_completion_callback, remove_completion_callback,
function (tests, harness_status, asserts) {
var cloned_tests = map(tests, function(test) {
return test.structured_clone();
});
this_obj._dispatch("completion_callback", [tests, harness_status],
{type: "complete",
tests: cloned_tests,
status: harness_status.structured_clone(),
asserts: asserts.map(assert => assert.structured_clone())});
}]
}
on_event(window, 'load', function() {
setTimeout(() => {
this_obj.all_loaded = true;
if (tests.all_done()) {
tests.complete();
}
},0);
});
on_event(window, 'message', function(event) {
if (event.data && event.data.type === "getmessages" && event.source) {
// A window can post "getmessages" to receive a duplicate of every
// message posted by this environment so far. This allows subscribers
// from fetch_tests_from_window to 'catch up' to the current state of
// this environment.
for (var i = 0; i < this_obj.dispatched_messages.length; ++i)
{
event.source.postMessage(this_obj.dispatched_messages[i], "*");
}
}
});
}
WindowTestEnvironment.prototype._dispatch = function(selector, callback_args, message_arg) {
this.dispatched_messages.push(message_arg);
this._forEach_windows(
function(w, same_origin) {
if (same_origin) {
try {
var has_selector = selector in w;
} catch(e) {
// If document.domain was set at some point same_origin can be
// wrong and the above will fail.
has_selector = false;
}
if (has_selector) {
try {
w[selector].apply(undefined, callback_args);
} catch (e) {}
}
}
if (w !== self) {
w.postMessage(message_arg, "*");
}
});
};
WindowTestEnvironment.prototype._forEach_windows = function(callback) {
// Iterate over the windows [self ... top, opener]. The callback is passed
// two objects, the first one is the window object itself, the second one
// is a boolean indicating whether or not it's on the same origin as the
// current window.
var cache = this.window_cache;
if (!cache) {
cache = [[self, true]];
var w = self;
var i = 0;
var so;
while (w != w.parent) {
w = w.parent;
so = is_same_origin(w);
cache.push([w, so]);
i++;
}
w = window.opener;
if (w) {
cache.push([w, is_same_origin(w)]);
}
this.window_cache = cache;
}
forEach(cache,
function(a) {
callback.apply(null, a);
});
};
WindowTestEnvironment.prototype.on_tests_ready = function() {
var output = new Output();
this.output_handler = output;
var this_obj = this;
add_start_callback(function (properties) {
this_obj.output_handler.init(properties);
});
add_test_state_callback(function(test) {
this_obj.output_handler.show_status();
});
add_result_callback(function (test) {
this_obj.output_handler.show_status();
});
add_completion_callback(function (tests, harness_status, asserts_run) {
this_obj.output_handler.show_results(tests, harness_status, asserts_run);
});
this.setup_messages(settings.message_events);
};
WindowTestEnvironment.prototype.setup_messages = function(new_events) {
var this_obj = this;
forEach(settings.message_events, function(x) {
var current_dispatch = this_obj.message_events.indexOf(x) !== -1;
var new_dispatch = new_events.indexOf(x) !== -1;
if (!current_dispatch && new_dispatch) {
this_obj.message_functions[x][0](this_obj.message_functions[x][2]);
} else if (current_dispatch && !new_dispatch) {
this_obj.message_functions[x][1](this_obj.message_functions[x][2]);
}
});
this.message_events = new_events;
}
WindowTestEnvironment.prototype.next_default_test_name = function() {
var suffix = this.name_counter > 0 ? " " + this.name_counter : "";
this.name_counter++;
return get_title() + suffix;
};
WindowTestEnvironment.prototype.on_new_harness_properties = function(properties) {
this.output_handler.setup(properties);
if (properties.hasOwnProperty("message_events")) {
this.setup_messages(properties.message_events);
}
};
WindowTestEnvironment.prototype.add_on_loaded_callback = function(callback) {
on_event(window, 'load', callback);
};
WindowTestEnvironment.prototype.test_timeout = function() {
var metas = document.getElementsByTagName("meta");
for (var i = 0; i < metas.length; i++) {
if (metas[i].name == "timeout") {
if (metas[i].content == "long") {
return settings.harness_timeout.long;
}
break;
}
}
return settings.harness_timeout.normal;
};
/*
* Base TestEnvironment implementation for a generic web worker.
*
* Workers accumulate test results. One or more clients can connect and
* retrieve results from a worker at any time.
*
* WorkerTestEnvironment supports communicating with a client via a
* MessagePort. The mechanism for determining the appropriate MessagePort
* for communicating with a client depends on the type of worker and is
* implemented by the various specializations of WorkerTestEnvironment
* below.
*
* A client document using testharness can use fetch_tests_from_worker() to
* retrieve results from a worker. See apisample16.html.
*/
function WorkerTestEnvironment() {
this.name_counter = 0;
this.all_loaded = true;
this.message_list = [];
this.message_ports = [];
}
WorkerTestEnvironment.prototype._dispatch = function(message) {
this.message_list.push(message);
for (var i = 0; i < this.message_ports.length; ++i)
{
this.message_ports[i].postMessage(message);
}
};
// The only requirement is that port has a postMessage() method. It doesn't
// have to be an instance of a MessagePort, and often isn't.
WorkerTestEnvironment.prototype._add_message_port = function(port) {
this.message_ports.push(port);
for (var i = 0; i < this.message_list.length; ++i)
{
port.postMessage(this.message_list[i]);
}
};
WorkerTestEnvironment.prototype.next_default_test_name = function() {
var suffix = this.name_counter > 0 ? " " + this.name_counter : "";
this.name_counter++;
return get_title() + suffix;
};
WorkerTestEnvironment.prototype.on_new_harness_properties = function() {};
WorkerTestEnvironment.prototype.on_tests_ready = function() {
var this_obj = this;
add_start_callback(
function(properties) {
this_obj._dispatch({
type: "start",
properties: properties,
});
});
add_test_state_callback(
function(test) {
this_obj._dispatch({
type: "test_state",
test: test.structured_clone()
});
});
add_result_callback(
function(test) {
this_obj._dispatch({
type: "result",
test: test.structured_clone()
});
});
add_completion_callback(
function(tests, harness_status, asserts) {
this_obj._dispatch({
type: "complete",
tests: map(tests,
function(test) {
return test.structured_clone();
}),
status: harness_status.structured_clone(),
asserts: asserts.map(assert => assert.structured_clone()),
});
});
};
WorkerTestEnvironment.prototype.add_on_loaded_callback = function() {};
WorkerTestEnvironment.prototype.test_timeout = function() {
// Tests running in a worker don't have a default timeout. I.e. all
// worker tests behave as if settings.explicit_timeout is true.
return null;
};
/*
* Dedicated web workers.
* https://html.spec.whatwg.org/multipage/workers.html#dedicatedworkerglobalscope
*
* This class is used as the test_environment when testharness is running
* inside a dedicated worker.
*/
function DedicatedWorkerTestEnvironment() {
WorkerTestEnvironment.call(this);
// self is an instance of DedicatedWorkerGlobalScope which exposes
// a postMessage() method for communicating via the message channel
// established when the worker is created.
this._add_message_port(self);
}
DedicatedWorkerTestEnvironment.prototype = Object.create(WorkerTestEnvironment.prototype);
DedicatedWorkerTestEnvironment.prototype.on_tests_ready = function() {
WorkerTestEnvironment.prototype.on_tests_ready.call(this);
// In the absence of an onload notification, we a require dedicated
// workers to explicitly signal when the tests are done.
tests.wait_for_finish = true;
};
/*
* Shared web workers.
* https://html.spec.whatwg.org/multipage/workers.html#sharedworkerglobalscope
*
* This class is used as the test_environment when testharness is running
* inside a shared web worker.
*/
function SharedWorkerTestEnvironment() {
WorkerTestEnvironment.call(this);
var this_obj = this;
// Shared workers receive message ports via the 'onconnect' event for
// each connection.
self.addEventListener("connect",
function(message_event) {
this_obj._add_message_port(message_event.source);
}, false);
}
SharedWorkerTestEnvironment.prototype = Object.create(WorkerTestEnvironment.prototype);
SharedWorkerTestEnvironment.prototype.on_tests_ready = function() {
WorkerTestEnvironment.prototype.on_tests_ready.call(this);
// In the absence of an onload notification, we a require shared
// workers to explicitly signal when the tests are done.
tests.wait_for_finish = true;
};
/*
* Service workers.
* http://www.w3.org/TR/service-workers/
*
* This class is used as the test_environment when testharness is running
* inside a service worker.
*/
function ServiceWorkerTestEnvironment() {
WorkerTestEnvironment.call(this);
this.all_loaded = false;
this.on_loaded_callback = null;
var this_obj = this;
self.addEventListener("message",
function(event) {
if (event.data && event.data.type && event.data.type === "connect") {
this_obj._add_message_port(event.source);
}
}, false);
// The oninstall event is received after the service worker script and
// all imported scripts have been fetched and executed. It's the
// equivalent of an onload event for a document. All tests should have
// been added by the time this event is received, thus it's not
// necessary to wait until the onactivate event. However, tests for
// installed service workers need another event which is equivalent to
// the onload event because oninstall is fired only on installation. The
// onmessage event is used for that purpose since tests using
// testharness.js should ask the result to its service worker by
// PostMessage. If the onmessage event is triggered on the service
// worker's context, that means the worker's script has been evaluated.
on_event(self, "install", on_all_loaded);
on_event(self, "message", on_all_loaded);
function on_all_loaded() {
if (this_obj.all_loaded)
return;
this_obj.all_loaded = true;
if (this_obj.on_loaded_callback) {
this_obj.on_loaded_callback();
}
}
}
ServiceWorkerTestEnvironment.prototype = Object.create(WorkerTestEnvironment.prototype);
ServiceWorkerTestEnvironment.prototype.add_on_loaded_callback = function(callback) {
if (this.all_loaded) {
callback();
} else {
this.on_loaded_callback = callback;
}
};
/*
* Shadow realms.
* https://github.com/tc39/proposal-shadowrealm
*
* This class is used as the test_environment when testharness is running
* inside a shadow realm.
*/
function ShadowRealmTestEnvironment() {
WorkerTestEnvironment.call(this);
this.all_loaded = false;
this.on_loaded_callback = null;
}
ShadowRealmTestEnvironment.prototype = Object.create(WorkerTestEnvironment.prototype);
/**
* Signal to the test environment that the tests are ready and the on-loaded
* callback should be run.
*
* Shadow realms are not *really* a DOM context: they have no `onload` or similar
* event for us to use to set up the test environment; so, instead, this method
* is manually triggered from the incubating realm
*
* @param {Function} message_destination - a function that receives JSON-serializable
* data to send to the incubating realm, in the same format as used by RemoteContext
*/
ShadowRealmTestEnvironment.prototype.begin = function(message_destination) {
if (this.all_loaded) {
throw new Error("Tried to start a shadow realm test environment after it has already started");
}
var fakeMessagePort = {};
fakeMessagePort.postMessage = message_destination;
this._add_message_port(fakeMessagePort);
this.all_loaded = true;
if (this.on_loaded_callback) {
this.on_loaded_callback();
}
};
ShadowRealmTestEnvironment.prototype.add_on_loaded_callback = function(callback) {
if (this.all_loaded) {
callback();
} else {
this.on_loaded_callback = callback;
}
};
/*
* JavaScript shells.
*
* This class is used as the test_environment when testharness is running
* inside a JavaScript shell.
*/
function ShellTestEnvironment() {
this.name_counter = 0;
this.all_loaded = false;
this.on_loaded_callback = null;
Promise.resolve().then(function() {
this.all_loaded = true
if (this.on_loaded_callback) {
this.on_loaded_callback();
}
}.bind(this));
this.message_list = [];
this.message_ports = [];
}
ShellTestEnvironment.prototype.next_default_test_name = function() {
var suffix = this.name_counter > 0 ? " " + this.name_counter : "";
this.name_counter++;
return get_title() + suffix;
};
ShellTestEnvironment.prototype.on_new_harness_properties = function() {};
ShellTestEnvironment.prototype.on_tests_ready = function() {};
ShellTestEnvironment.prototype.add_on_loaded_callback = function(callback) {
if (this.all_loaded) {
callback();
} else {
this.on_loaded_callback = callback;
}
};
ShellTestEnvironment.prototype.test_timeout = function() {
// Tests running in a shell don't have a default timeout, so behave as
// if settings.explicit_timeout is true.
return null;
};
function create_test_environment() {
if ('document' in global_scope) {
return new WindowTestEnvironment();
}
if ('DedicatedWorkerGlobalScope' in global_scope &&
global_scope instanceof DedicatedWorkerGlobalScope) {
return new DedicatedWorkerTestEnvironment();
}
if ('SharedWorkerGlobalScope' in global_scope &&
global_scope instanceof SharedWorkerGlobalScope) {
return new SharedWorkerTestEnvironment();
}
if ('ServiceWorkerGlobalScope' in global_scope &&
global_scope instanceof ServiceWorkerGlobalScope) {
return new ServiceWorkerTestEnvironment();
}
if ('WorkerGlobalScope' in global_scope &&
global_scope instanceof WorkerGlobalScope) {
return new DedicatedWorkerTestEnvironment();
}
/* Shadow realm global objects are _ordinary_ objects (i.e. their prototype is
* Object) so we don't have a nice `instanceof` test to use; instead, we
* check if the there is a GLOBAL.isShadowRealm() property
* on the global object. that was set by the test harness when it
* created the ShadowRealm.
*/
if (global_scope.GLOBAL && global_scope.GLOBAL.isShadowRealm()) {
return new ShadowRealmTestEnvironment();
}
return new ShellTestEnvironment();
}
var test_environment = create_test_environment();
function is_shared_worker(worker) {
return 'SharedWorker' in global_scope && worker instanceof SharedWorker;
}
function is_service_worker(worker) {
// The worker object may be from another execution context,
// so do not use instanceof here.
return 'ServiceWorker' in global_scope &&
Object.prototype.toString.call(worker) == '[object ServiceWorker]';
}
var seen_func_name = Object.create(null);
function get_test_name(func, name)
{
if (name) {
return name;
}
if (func) {
var func_code = func.toString();
// Try and match with brackets, but fallback to matching without
var arrow = func_code.match(/^\(\)\s*=>\s*(?:{(.*)}\s*|(.*))$/);
// Check for JS line separators
if (arrow !== null && !/[\u000A\u000D\u2028\u2029]/.test(func_code)) {
var trimmed = (arrow[1] !== undefined ? arrow[1] : arrow[2]).trim();
// drop trailing ; if there's no earlier ones
trimmed = trimmed.replace(/^([^;]*)(;\s*)+$/, "$1");
if (trimmed) {
let name = trimmed;
if (seen_func_name[trimmed]) {
// This subtest name already exists, so add a suffix.
name += " " + seen_func_name[trimmed];
} else {
seen_func_name[trimmed] = 0;
}
seen_func_name[trimmed] += 1;
return name;
}
}
}
return test_environment.next_default_test_name();
}
/**
* @callback TestFunction
* @param {Test} test - The test currnetly being run.
* @param {Any[]} args - Additional args to pass to function.
*
*/
/**
* Create a synchronous test
*
* @param {TestFunction} func - Test function. This is executed
* immediately. If it returns without error, the test status is
* set to ``PASS``. If it throws an :js:class:`AssertionError`, or
* any other exception, the test status is set to ``FAIL``
* (typically from an `assert` function).
* @param {String} name - Test name. This must be unique in a
* given file and must be invariant between runs.
*/
function test(func, name, properties)
{
if (tests.promise_setup_called) {
tests.status.status = tests.status.ERROR;
tests.status.message = '`test` invoked after `promise_setup`';
tests.complete();
}
var test_name = get_test_name(func, name);
var test_obj = new Test(test_name, properties);
var value = test_obj.step(func, test_obj, test_obj);
if (value !== undefined) {
var msg = 'Test named "' + test_name +
'" passed a function to `test` that returned a value.';
try {
if (value && typeof value.then === 'function') {
msg += ' Consider using `promise_test` instead when ' +
'using Promises or async/await.';
}
} catch (err) {}
tests.status.status = tests.status.ERROR;
tests.status.message = msg;
}
if (test_obj.phase === test_obj.phases.STARTED) {
test_obj.done();
}
}
/**
* Create an asynchronous test
*
* @param {TestFunction|string} funcOrName - Initial step function
* to call immediately with the test name as an argument (if any),
* or name of the test.
* @param {String} name - Test name (if a test function was
* provided). This must be unique in a given file and must be
* invariant between runs.
* @returns {Test} An object representing the ongoing test.
*/
function async_test(func, name, properties)
{
if (tests.promise_setup_called) {
tests.status.status = tests.status.ERROR;
tests.status.message = '`async_test` invoked after `promise_setup`';
tests.complete();
}
if (typeof func !== "function") {
properties = name;
name = func;
func = null;
}
var test_name = get_test_name(func, name);
var test_obj = new Test(test_name, properties);
if (func) {
var value = test_obj.step(func, test_obj, test_obj);
// Test authors sometimes return values to async_test, expecting us
// to handle the value somehow. Make doing so a harness error to be
// clear this is invalid, and point authors to promise_test if it
// may be appropriate.
//
// Note that we only perform this check on the initial function
// passed to async_test, not on any later steps - we haven't seen a
// consistent problem with those (and it's harder to check).
if (value !== undefined) {
var msg = 'Test named "' + test_name +
'" passed a function to `async_test` that returned a value.';
try {
if (value && typeof value.then === 'function') {
msg += ' Consider using `promise_test` instead when ' +
'using Promises or async/await.';
}
} catch (err) {}
tests.set_status(tests.status.ERROR, msg);
tests.complete();
}
}
return test_obj;
}
/**
* Create a promise test.
*
* Promise tests are tests which are represented by a promise
* object. If the promise is fulfilled the test passes, if it's
* rejected the test fails, otherwise the test passes.
*
* @param {TestFunction} func - Test function. This must return a
* promise. The test is automatically marked as complete once the
* promise settles.
* @param {String} name - Test name. This must be unique in a
* given file and must be invariant between runs.
*/
function promise_test(func, name, properties) {
if (typeof func !== "function") {
properties = name;
name = func;
func = null;
}
var test_name = get_test_name(func, name);
var test = new Test(test_name, properties);
test._is_promise_test = true;
// If there is no promise tests queue make one.
if (!tests.promise_tests) {
tests.promise_tests = Promise.resolve();
}
tests.promise_tests = tests.promise_tests.then(function() {
return new Promise(function(resolve) {
var promise = test.step(func, test, test);
test.step(function() {
assert(!!promise, "promise_test", null,
"test body must return a 'thenable' object (received ${value})",
{value:promise});
assert(typeof promise.then === "function", "promise_test", null,
"test body must return a 'thenable' object (received an object with no `then` method)",
null);
});
// Test authors may use the `step` method within a
// `promise_test` even though this reflects a mixture of
// asynchronous control flow paradigms. The "done" callback
// should be registered prior to the resolution of the
// user-provided Promise to avoid timeouts in cases where the
// Promise does not settle but a `step` function has thrown an
// error.
add_test_done_callback(test, resolve);
Promise.resolve(promise)
.catch(test.step_func(
function(value) {
if (value instanceof AssertionError) {
throw value;
}
assert(false, "promise_test", null,
"Unhandled rejection with value: ${value}", {value:value});
}))
.then(function() {
test.done();
});
});
});
}
/**
* Make a copy of a Promise in the current realm.
*
* @param {Promise} promise the given promise that may be from a different
* realm
* @returns {Promise}
*
* An arbitrary promise provided by the caller may have originated
* in another frame that have since navigated away, rendering the
* frame's document inactive. Such a promise cannot be used with
* `await` or Promise.resolve(), as microtasks associated with it
* may be prevented from being run. See `issue
* 5319<https://github.com/whatwg/html/issues/5319>`_ for a
* particular case.
*
* In functions we define here, there is an expectation from the caller
* that the promise is from the current realm, that can always be used with
* `await`, etc. We therefore create a new promise in this realm that
* inherit the value and status from the given promise.
*/
function bring_promise_to_current_realm(promise) {
return new Promise(promise.then.bind(promise));
}
/**
* Assert that a Promise is rejected with the right ECMAScript exception.
*
* @param {Test} test - the `Test` to use for the assertion.
* @param {Function} constructor - The expected exception constructor.
* @param {Promise} promise - The promise that's expected to
* reject with the given exception.
* @param {string} [description] Error message to add to assert in case of
* failure.
*/
function promise_rejects_js(test, constructor, promise, description) {
return bring_promise_to_current_realm(promise)
.then(test.unreached_func("Should have rejected: " + description))
.catch(function(e) {
assert_throws_js_impl(constructor, function() { throw e },
description, "promise_rejects_js");
});
}
/**
* Assert that a Promise is rejected with the right DOMException.
*
* For the remaining arguments, there are two ways of calling
* promise_rejects_dom:
*
* 1) If the DOMException is expected to come from the current global, the
* third argument should be the promise expected to reject, and a fourth,
* optional, argument is the assertion description.
*
* 2) If the DOMException is expected to come from some other global, the
* third argument should be the DOMException constructor from that global,
* the fourth argument the promise expected to reject, and the fifth,
* optional, argument the assertion description.
*
* @param {Test} test - the `Test` to use for the assertion.
* @param {number|string} type - See documentation for
* `assert_throws_dom <#assert_throws_dom>`_.
* @param {Function} promiseOrConstructor - Either the constructor
* for the expected exception (if the exception comes from another
* global), or the promise that's expected to reject (if the
* exception comes from the current global).
* @param {Function|string} descriptionOrPromise - Either the
* promise that's expected to reject (if the exception comes from
* another global), or the optional description of the condition
* being tested (if the exception comes from the current global).
* @param {string} [description] - Description of the condition
* being tested (if the exception comes from another global).
*
*/
function promise_rejects_dom(test, type, promiseOrConstructor, descriptionOrPromise, maybeDescription) {
let constructor, promise, description;
if (typeof promiseOrConstructor === "function" &&
promiseOrConstructor.name === "DOMException") {
constructor = promiseOrConstructor;
promise = descriptionOrPromise;
description = maybeDescription;
} else {
constructor = self.DOMException;
promise = promiseOrConstructor;
description = descriptionOrPromise;
assert(maybeDescription === undefined,
"Too many args passed to no-constructor version of promise_rejects_dom, or accidentally explicitly passed undefined");
}
return bring_promise_to_current_realm(promise)
.then(test.unreached_func("Should have rejected: " + description))
.catch(function(e) {
assert_throws_dom_impl(type, function() { throw e }, description,
"promise_rejects_dom", constructor);
});
}
/**
* Assert that a Promise is rejected with the provided value.
*
* @param {Test} test - the `Test` to use for the assertion.
* @param {Any} exception - The expected value of the rejected promise.
* @param {Promise} promise - The promise that's expected to
* reject.
* @param {string} [description] Error message to add to assert in case of
* failure.
*/
function promise_rejects_exactly(test, exception, promise, description) {
return bring_promise_to_current_realm(promise)
.then(test.unreached_func("Should have rejected: " + description))
.catch(function(e) {
assert_throws_exactly_impl(exception, function() { throw e },
description, "promise_rejects_exactly");
});
}
/**
* Allow DOM events to be handled using Promises.
*
* This can make it a lot easier to test a very specific series of events,
* including ensuring that unexpected events are not fired at any point.
*
* `EventWatcher` will assert if an event occurs while there is no `wait_for`
* created Promise waiting to be fulfilled, or if the event is of a different type
* to the type currently expected. This ensures that only the events that are
* expected occur, in the correct order, and with the correct timing.
*
* @constructor
* @param {Test} test - The `Test` to use for the assertion.
* @param {EventTarget} watchedNode - The target expected to receive the events.
* @param {string[]} eventTypes - List of events to watch for.
* @param {Promise} timeoutPromise - Promise that will cause the
* test to be set to `TIMEOUT` once fulfilled.
*
*/
function EventWatcher(test, watchedNode, eventTypes, timeoutPromise)
{
if (typeof eventTypes == 'string') {
eventTypes = [eventTypes];
}
var waitingFor = null;
// This is null unless we are recording all events, in which case it
// will be an Array object.
var recordedEvents = null;
var eventHandler = test.step_func(function(evt) {
assert_true(!!waitingFor,
'Not expecting event, but got ' + evt.type + ' event');
assert_equals(evt.type, waitingFor.types[0],
'Expected ' + waitingFor.types[0] + ' event, but got ' +
evt.type + ' event instead');
if (Array.isArray(recordedEvents)) {
recordedEvents.push(evt);
}
if (waitingFor.types.length > 1) {
// Pop first event from array
waitingFor.types.shift();
return;
}
// We need to null out waitingFor before calling the resolve function
// since the Promise's resolve handlers may call wait_for() which will
// need to set waitingFor.
var resolveFunc = waitingFor.resolve;
waitingFor = null;
// Likewise, we should reset the state of recordedEvents.
var result = recordedEvents || evt;
recordedEvents = null;
resolveFunc(result);
});
for (var i = 0; i < eventTypes.length; i++) {
watchedNode.addEventListener(eventTypes[i], eventHandler, false);
}
/**
* Returns a Promise that will resolve after the specified event or
* series of events has occurred.
*
* @param {Object} options An optional options object. If the 'record' property
* on this object has the value 'all', when the Promise
* returned by this function is resolved, *all* Event
* objects that were waited for will be returned as an
* array.
*
* @example
* const watcher = new EventWatcher(t, div, [ 'animationstart',
* 'animationiteration',
* 'animationend' ]);
* return watcher.wait_for([ 'animationstart', 'animationend' ],
* { record: 'all' }).then(evts => {
* assert_equals(evts[0].elapsedTime, 0.0);
* assert_equals(evts[1].elapsedTime, 2.0);
* });
*/
this.wait_for = function(types, options) {
if (waitingFor) {
return Promise.reject('Already waiting for an event or events');
}
if (typeof types == 'string') {
types = [types];
}
if (options && options.record && options.record === 'all') {
recordedEvents = [];
}
return new Promise(function(resolve, reject) {
var timeout = test.step_func(function() {
// If the timeout fires after the events have been received
// or during a subsequent call to wait_for, ignore it.
if (!waitingFor || waitingFor.resolve !== resolve)
return;
// This should always fail, otherwise we should have
// resolved the promise.
assert_true(waitingFor.types.length == 0,
'Timed out waiting for ' + waitingFor.types.join(', '));
var result = recordedEvents;
recordedEvents = null;
var resolveFunc = waitingFor.resolve;
waitingFor = null;
resolveFunc(result);
});
if (timeoutPromise) {
timeoutPromise().then(timeout);