summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/navigation-api/focus-reset/focus-reset-timing.html
blob: df9f03afe9f3699cc9feb9a44e9e69dce25c7558 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<script>
promise_test(async t => {
  navigation.addEventListener("navigate", e => {
    e.intercept({ focusReset: "after-transition" });
  }, { once: true });

  const button = document.body.appendChild(document.createElement("button"));
  const button2 = document.body.appendChild(document.createElement("button"));
  button2.tabIndex = 0;
  t.add_cleanup(() => {
    button.remove();
    button2.remove();
  });

  assert_equals(document.activeElement, document.body, "Start on body");
  button.focus();
  assert_equals(document.activeElement, button, "focus() worked");

  let navigatesuccess_called = false;
  navigation.onnavigatesuccess = t.step_func(() => {
    navigatesuccess_called = true;
    assert_equals(document.activeElement, document.body, "Focus must be reset before navigatesuccess");
  });

  await navigation.navigate("#1").finished;
  assert_true(navigatesuccess_called);
}, "Focus should be reset before navigatesuccess");

promise_test(async t => {
  navigation.addEventListener("navigate", e => {
    e.intercept({ handler: () => Promise.reject(),
                  focusReset: "after-transition" });
  }, { once: true });

  const button = document.body.appendChild(document.createElement("button"));
  const button2 = document.body.appendChild(document.createElement("button"));
  button2.tabIndex = 0;
  t.add_cleanup(() => {
    button.remove();
    button2.remove();
  });

  assert_equals(document.activeElement, document.body, "Start on body");
  button.focus();
  assert_equals(document.activeElement, button, "focus() worked");

  let navigateerror_called = false;
  navigation.onnavigateerror = t.step_func(() => {
    navigateerror_called = true;
    assert_equals(document.activeElement, document.body, "Focus must be reset before navigateerror");
  });

  await promise_rejects_exactly(t, undefined, navigation.navigate("#2").finished);
  assert_true(navigateerror_called);
}, "Focus should be reset before navigateerror");
</script>
</body>