Skip to content

Commit

Permalink
Add promise rejection tracking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Nov 30, 2015
1 parent 7dda9a1 commit f86b4b7
Show file tree
Hide file tree
Showing 8 changed files with 1,083 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/cors/support.js?pipe=sub"></script>
<link rel="help" href="https://html.spec.whatwg.org/#unhandled-promise-rejections">
<link rel="help" href="https://html.spec.whatwg.org/#muted-errors">

<body>
<script>
'use strict';
async_test(function(t) {
addEventListener('unhandledrejection', t.step_func(function(e) {
assert_equals(e.reason, 42, 'reason should be the one given by the script');
t.done();
}));
}, 'Promise rejection event should be received for the cross-origin CORS script');

(function() {
var scriptEl = document.createElement('script');
scriptEl.src = CROSSDOMAIN + '/support/promise-access-control.py?allow=true';
document.body.appendChild(scriptEl);
}());
</script>
</body>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/cors/support.js?pipe=sub"></script>
<link rel="help" href="https://html.spec.whatwg.org/#unhandled-promise-rejections">
<link rel="help" href="https://html.spec.whatwg.org/#muted-errors">

<body>
<script>
'use strict';

(function() {
var resolveLoaded;
var loadedPromise = new Promise(function(resolve) { resolveLoaded = resolve; });

async_test(function(t) {
addEventListener('unhandledrejection', t.unreached_func('unhandledrejection event should never be triggered'));
addEventListener('rejectionhandled', t.unreached_func('rejectionhandled event should never be triggered'));

setTimeout(function() {
loadedPromise.then(t.step_func(function() {
t.done();
}));
}, 1000);
}, 'Promise rejection event should be muted the cross-origin non-CORS script');

var scriptEl = document.createElement('script');
scriptEl.src = CROSSDOMAIN + '/support/promise-access-control.py?allow=false';
scriptEl.onload = resolveLoaded;
document.body.appendChild(scriptEl);
}());
</script>
</body>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="help" href="https://html.spec.whatwg.org/#the-promiserejectionevent-interface">
<script>
'use strict';

test(function() {
var p = new Promise(function(resolve, reject) {});

// No custom options are passed (besides required promise).
assert_equals(new PromiseRejectionEvent('eventType', { promise: p }).bubbles, false);
assert_equals(new PromiseRejectionEvent('eventType', { promise: p }).cancelable, false);
assert_equals(new PromiseRejectionEvent('eventType', { promise: p }).promise, p);
assert_equals(new PromiseRejectionEvent('eventType', { promise: p }).reason, undefined);

// No promise is passed.
assert_throws(new TypeError(),
function() {
new PromiseRejectionEvent('eventType', { bubbles: false });
},
'Cannot construct PromiseRejectionEventInit without promise');

// bubbles is passed.
assert_equals(new PromiseRejectionEvent('eventType', { bubbles: false, promise: p }).bubbles, false);
assert_equals(new PromiseRejectionEvent('eventType', { bubbles: true, promise: p }).bubbles, true);

// cancelable is passed.
assert_equals(new PromiseRejectionEvent('eventType', { cancelable: false, promise: p }).cancelable, false);
assert_equals(new PromiseRejectionEvent('eventType', { cancelable: true, promise: p }).cancelable, true);

// reason is passed.
var r = new Error();
assert_equals(new PromiseRejectionEvent('eventType', { promise: p, reason: r }).reason, r);


// All initializers are passed.
assert_equals(new PromiseRejectionEvent('eventType', { bubbles: true, cancelable: true, promise: p, reason: r }).bubbles, true);
assert_equals(new PromiseRejectionEvent('eventType', { bubbles: true, cancelable: true, promise: p, reason: r }).cancelable, true);
assert_equals(new PromiseRejectionEvent('eventType', { bubbles: true, cancelable: true, promise: p, reason: r }).promise, p);
assert_equals(new PromiseRejectionEvent('eventType', { bubbles: true, cancelable: true, promise: p, reason: r }).reason, r);
}, "This tests the constructor for the PromiseRejectionEvent DOM class.");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="help" href="https://html.spec.whatwg.org/#unhandled-promise-rejections">
<script>
'use strict';
async_test(function(t) {
var e = new Error('e');
var p = Promise.reject(e);

window.onunhandledrejection = function(evt) {
t.step(function() {
assert_equals(evt.promise, p);
assert_equals(evt.reason, e);
});
var unreached = t.unreached_func('promise should not be fulfilled');
p.then(unreached, function(reason) {
t.step(function() {
assert_equals(reason, e);
});
setTimeout(function() { t.done(); }, 10);
});
};

window.onrejectionhandled = t.unreached_func('rejectionhandled event should not be invoked');
}, 'Attaching a handler in unhandledrejection should not trigger rejectionhandled.');
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="help" href="https://html.spec.whatwg.org/#unhandled-promise-rejections">
<script>
'use strict';
setup({
allow_uncaught_exception: true
});
async_test(function(t) {
var e = new Error('e');
var e2 = new Error('e2');
window.onerror = function (msg, url, line, col, error) {
t.step(function() {
assert_equals(msg, 'Uncaught Error: e2');
assert_equals(error, e2);
});
t.done();
};
window.onrejectionhandled = function() {
throw e2;
};
var p = Promise.reject(e);
setTimeout(function() {
try {
p.catch(function() {});
} catch (e) {
assert_unreached('attaching a handler should not throw an error');
}
}, 1);
}, 'Throwing inside an unhandledrejection handler invokes the error handler.');
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/service-workers/service-workers/resources/test-helpers.js"></script>
<link rel="help" href="https://html.spec.whatwg.org/#unhandled-promise-rejections">

<script src="support/promise-rejection-events.js"></script>
<script>
fetch_tests_from_worker(new Worker('support/promise-rejection-events.js'));
fetch_tests_from_worker(new SharedWorker('support/promise-rejection-events.js'));
service_worker_test('support/promise-rejection-events.js');
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def main(request, response):
allow = request.GET.first("allow", "false")

headers = [("Content-Type", "application/javascript")]
if allow != "false":
headers.append(("Access-Control-Allow-Origin", "*"))

body = "new Promise(function(resolve, reject) { reject(42); })"

return headers, body
Loading

0 comments on commit f86b4b7

Please sign in to comment.