| 1 |
<!doctype html> |
| 2 |
<title>enumerateDevices() without focus</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 |
const blank_url = '/common/blank.html'; |
| 11 |
|
| 12 |
function promise_event(target, name) { |
| 13 |
return new Promise(resolve => target[`on${name}`] = resolve); |
| 14 |
} |
| 15 |
// When testdriver.js supports switch-to-window, it can replace this function |
| 16 |
// and this test can be upstreamed. |
| 17 |
// https://github.com/web-platform-tests/wpt/issues/10666 |
| 18 |
function switch_toplevel_focus_for_window(win) { |
| 19 |
return win.SpecialPowers.spawnChrome([], function activate_browser_window() { |
| 20 |
this.browsingContext.topChromeWindow.focus(); |
| 21 |
}); |
| 22 |
} |
| 23 |
|
| 24 |
promise_test(async t => { |
| 25 |
await test_driver.bless('window.open()'); |
| 26 |
assert_true(document.hasFocus(), 'This test needs focus on the document.'); |
| 27 |
const promise_blur = promise_event(window, 'blur'); |
| 28 |
// 'resizable' is requested for a separate OS window on relevant platforms |
| 29 |
// so that this test tests OS focus changes rather than document visibility. |
| 30 |
const proxy = window.open(blank_url, '', 'resizable'); |
| 31 |
t.add_cleanup(() => proxy.close()); |
| 32 |
await Promise.all([ |
| 33 |
promise_blur, |
| 34 |
switch_toplevel_focus_for_window(proxy), |
| 35 |
promise_event(proxy, 'load'), |
| 36 |
]); |
| 37 |
assert_false(document.hasFocus(), 'document.hasFocus() after blur'); |
| 38 |
|
| 39 |
// Enumeration should remain pending without focus. |
| 40 |
const promise_enumerate = navigator.mediaDevices.enumerateDevices(); |
| 41 |
// Enumerate in the focused window to provide enough time to check that |
| 42 |
// the Promise from the unfocused window does not settle. |
| 43 |
await proxy.navigator.mediaDevices.enumerateDevices(); |
| 44 |
// Race a settled Promise to check that the enumeration in the first window |
| 45 |
// has not settled. |
| 46 |
const result = await Promise.race([promise_enumerate, 'pending']); |
| 47 |
assert_equals(result, 'pending', 'pending Promise without focus.'); |
| 48 |
|
| 49 |
// The enumeration Promise should resolve after focus returns to the window. |
| 50 |
proxy.close(); |
| 51 |
await Promise.all([ |
| 52 |
promise_event(window, 'focus'), |
| 53 |
switch_toplevel_focus_for_window(window), |
| 54 |
]); |
| 55 |
assert_true(document.hasFocus(), 'document.hasFocus() after focus'); |
| 56 |
await promise_enumerate; |
| 57 |
}, 'enumerateDevices without focus'); |
| 58 |
</script> |