| 1 |
<!doctype html> |
| 2 |
<title>enumerateDevices() in background tab with focus in chrome</title> |
| 3 |
<script src=/resources/testharness.js></script> |
| 4 |
<script src=/resources/testharnessreport.js></script> |
| 5 |
<script src="/resources/testdriver.js"></script> |
| 6 |
<script src="/resources/testdriver-vendor.js"></script> |
| 7 |
<body></body> |
| 8 |
<script> |
| 9 |
'use strict'; |
| 10 |
// This test is not in cross-browser wpt because it uses Gecko-specific API |
| 11 |
// for focusing browser chrome and it assumes a specific tab browser design. |
| 12 |
// It assumes that |
| 13 |
// * browser chrome widget focus is associated with a single current document |
| 14 |
// presentation (tab), |
| 15 |
// https://github.com/w3c/mediacapture-main/issues/752#issuecomment-742036800 |
| 16 |
// * window.open() focuses the new tab and makes the current tab hidden, and |
| 17 |
// * the opener tab becomes the current and visible tab again when the new tab |
| 18 |
// is closed. |
| 19 |
const blank_url = '/common/blank.html'; |
| 20 |
|
| 21 |
function promise_event(target, name) { |
| 22 |
return new Promise(resolve => target[`on${name}`] = resolve); |
| 23 |
} |
| 24 |
|
| 25 |
promise_test(async t => { |
| 26 |
// Open a new tab, which is expected to receive focus and hide the first tab. |
| 27 |
await test_driver.bless('window.open()'); |
| 28 |
assert_true(document.hasFocus(), 'This test needs focus on the browser.'); |
| 29 |
const promise_hidden = promise_event(document, 'visibilitychange'); |
| 30 |
const proxy = window.open(blank_url); |
| 31 |
t.add_cleanup(() => proxy.close()); |
| 32 |
await Promise.all([ |
| 33 |
promise_hidden, |
| 34 |
promise_event(proxy, 'focus'), |
| 35 |
promise_event(proxy, 'load'), |
| 36 |
]); |
| 37 |
assert_true(proxy.document.hasFocus(), 'proxy.document.hasFocus()'); |
| 38 |
|
| 39 |
await Promise.all([ |
| 40 |
promise_event(proxy, 'blur'), |
| 41 |
SpecialPowers.spawnChrome([], function focus_url_bar() { |
| 42 |
this.browsingContext.topChromeWindow.gURLBar.focus(); |
| 43 |
}), |
| 44 |
]); |
| 45 |
assert_false(proxy.document.hasFocus(), 'proxy.document.hasFocus()'); |
| 46 |
assert_false(document.hasFocus(), 'document.hasFocus()'); |
| 47 |
assert_equals(document.visibilityState, 'hidden', 'visibilityState'); |
| 48 |
|
| 49 |
// Enumeration should remain pending while the first tab is background. |
| 50 |
const promise_enumerate = navigator.mediaDevices.enumerateDevices(); |
| 51 |
// Enumerate in the foreground tab to confirm that URL bar focus is |
| 52 |
// sufficient, and to provide enough time to check that the Promise from the |
| 53 |
// background tab does not settle. |
| 54 |
await proxy.navigator.mediaDevices.enumerateDevices(); |
| 55 |
// Race a settled Promise to check that the enumeration in the background tab |
| 56 |
// has not settled. |
| 57 |
const result = await Promise.race([promise_enumerate, 'pending']); |
| 58 |
assert_equals(result, 'pending', 'pending Promise while background.'); |
| 59 |
|
| 60 |
// The enumeration Promise should resolve after the first tab returns to the |
| 61 |
// foreground. |
| 62 |
proxy.close(); |
| 63 |
await promise_event(document, 'visibilitychange'); |
| 64 |
assert_equals(document.visibilityState, 'visible', 'visibilityState'); |
| 65 |
await promise_enumerate; |
| 66 |
}, 'enumerateDevices in background'); |
| 67 |
</script> |