|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | +<title>document.modelContext.waitForToolsUntil()</title> |
| 5 | +<script src="/resources/testharness.js"></script> |
| 6 | +<script src="/resources/testharnessreport.js"></script> |
| 7 | +<script src="/common/get-host-info.sub.js"></script> |
| 8 | +</head> |
| 9 | +<body> |
| 10 | +<script> |
| 11 | +// Covers the web-observable contract of `waitForToolsUntil()`; whether |
| 12 | +// consumers of the tools actually wait is up to the user agent. |
| 13 | + |
| 14 | +test(() => { |
| 15 | + // The argument is a `Promise<any>`, so non-promise values are wrapped. |
| 16 | + document.modelContext.waitForToolsUntil(42); |
| 17 | + document.modelContext.waitForToolsUntil(undefined); |
| 18 | +}, 'waitForToolsUntil() accepts non-promise values'); |
| 19 | + |
| 20 | +test(() => { |
| 21 | + assert_throws_js(TypeError, () => document.modelContext.waitForToolsUntil()); |
| 22 | +}, 'waitForToolsUntil() requires an argument'); |
| 23 | + |
| 24 | +test(() => { |
| 25 | + const doc = document.implementation.createHTMLDocument(); |
| 26 | + assert_throws_dom('InvalidStateError', |
| 27 | + () => doc.modelContext.waitForToolsUntil(Promise.resolve())); |
| 28 | +}, 'waitForToolsUntil() throws InvalidStateError in a document without a frame'); |
| 29 | + |
| 30 | +promise_test(async t => { |
| 31 | + const iframe = document.createElement('iframe'); |
| 32 | + iframe.src = get_host_info().HTTPS_REMOTE_ORIGIN + |
| 33 | + '/webmcp/imperative/resources/iframe-wait-for-tools-until.html'; |
| 34 | + // No `allow` attribute, so the `tools` permissions policy is disabled in |
| 35 | + // this cross-origin iframe. |
| 36 | + |
| 37 | + const loaded = new Promise(resolve => iframe.onload = resolve); |
| 38 | + document.body.appendChild(iframe); |
| 39 | + t.add_cleanup(() => iframe.remove()); |
| 40 | + await loaded; |
| 41 | + |
| 42 | + const response = new Promise(resolve => { |
| 43 | + window.addEventListener('message', e => resolve(e.data), {once: true}); |
| 44 | + }); |
| 45 | + iframe.contentWindow.postMessage('waitForToolsUntil', '*'); |
| 46 | + assert_equals(await response, 'NotAllowedError'); |
| 47 | +}, 'waitForToolsUntil() throws NotAllowedError when the tools permissions policy is disabled'); |
| 48 | +</script> |
| 49 | +</body> |
| 50 | +</html> |
0 commit comments