summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/interaction/focus/document-level-focus-apis/document-has-system-focus.html
blob: 26f0bbbd09feb7972946ed06a1153425e6fe319e (plain)
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
<!DOCTYPE html>
<meta charset="utf-8">
<title>HTML Test: focus - document has system focus</title>
<link rel="help" href="https://html.spec.whatwg.org/#has-focus-steps">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<input id="input">
<script>

promise_test(async t => {
  await new Promise(r => window.onload = r);
  // This test requires the document to have focus as a starting condition.
  // Whether a newly loaded page receives focus or not, seems to be somewhat
  // browser-dependent and situation-dependent. For instance, Firefox appears to
  // focus the page immediately if the page was loaded with the refresh button,
  // but not if it was loaded from pressing ENTER in the URL bar. To ensure a
  // reliable starting condition for this test, we give an extra push for focus.
  if (!document.hasFocus()) {
    const input = document.getElementById("input");
    input.focus();
    await new Promise(r => input.onfocus = r);
  }
  assert_true(document.hasFocus(), "Document has focus as starting condition.");

  let gotBlur = false;
  window.onblur = () => gotBlur = true;
  const popup = window.open("support/popup.html", "otherwindow", "resizable");
  assert_not_equals(popup, null, "Test requires popup be opened");
  t.add_cleanup(() => popup.close());
  const msg = await new Promise(r => window.onmessage = ({data}) => r(data));
  assert_equals(msg, "focus = true",
                "Test requires popups be focused (may require harness flags)");
  assert_true(gotBlur, "Document received blur event when popup opened");
  assert_false(document.hasFocus(), "Document lost focus when popup opened");

  const p = new Promise(r => window.onfocus = r);
  popup.close();
  await p;
  assert_true(true, "Document received focus event when popup closed");
  assert_true(document.hasFocus(), "Document regained focus when popup closed");
}, "Top-level document receives blur/focus events and loses system focus " +
   "during opening/closing of a popup");

</script>