summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/forms/the-input-element/focus-dynamic-type-change-on-blur.html
blob: 23292d3a83a67448cbbb160b8ee56c1d20b09dbf (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>
<meta charset="utf-8">
<title>Input type switch on blur event should clean up properly</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<script type=module>
import inputTypes from "./input-types.js";

function tick() {
  return new Promise(resolve => {
    requestAnimationFrame(() => requestAnimationFrame(resolve));
  });
}

function test_from_to(fromType, toType, capture) {
  if (fromType == toType) {
    return;
  }
  promise_test(async function(t) {
    const input = document.createElement("input");
    input.type = fromType;
    document.body.appendChild(input);
    input.focus();
    assert_equals(document.activeElement, input, `${fromType} input should be focused`);
    function onFocus() {
      t.assert_unreached("shouldn't be getting spurious focus events");
    }
    function onBlur() {
      input.type = toType;
    }
    input.addEventListener("focus", onFocus);
    input.addEventListener("blur", onBlur, capture);
    await tick();

    assert_equals(document.activeElement, input, `${fromType} input should still be focused after tick`);
    assert_true(input.matches(":focus"), `${fromType} input should match :focus`);
    assert_true(input.matches(":focus-visible"), `${fromType} input should match :focus-visible`);

    input.blur();

    assert_equals(document.activeElement, document.body, `${fromType} input should not remain focused after blur`);
    assert_false(input.matches(":focus"), `${fromType} input should not match :focus`);
    assert_false(input.matches(":focus-visible"), `${fromType} input should not match :focus-visible`);
    assert_equals(input.type, toType, `${fromType} input should have changed to ${toType}`);
    input.removeEventListener("focus", onFocus);
    input.removeEventListener("blur", onBlur);
  }, `${fromType} -> ${toType} ${capture}`);
}

for (let type of inputTypes) {
  if (type == "hidden") {
    continue; // hidden inputs are not focusable
  }
  for (let capture of [true, false]) {
    test_from_to(type, "text", capture);
    test_from_to("text", type, capture);
  }
}
</script>
</body>