summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/shadow-dom/focus-navigation/focus-navigation-web-component-radio.html
blob: 9304cc2ce199b0ad08c1385ef759e08518428dcc (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
62
63
64
65
66
67
68
69
70
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="resources/shadow-dom.js"></script>
<script src="resources/focus-utils.js"></script>

<template id="custom-radio">
  <span aria-role="radio" tabindex="0">&#x1F518;</span>
  <slot></slot>
</template>

<div tabindex="0" id="start">OUT</div>
<form>
  <custom-radio name="radio" id="A">A</x-radio>
  <custom-radio name="radio" id="B">B</x-radio>
</form>
<form>
  <custom-radio name="radio" id="C">C</x-radio>
  <custom-radio name="radio" id="D">D</x-radio>
</form>
<div tabindex="0" id="end">OUT</div>

<script>
const template = document.querySelector('#custom-radio');

class CustomRadio extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: 'open', delegatesFocus: true }).appendChild(
        template.content.cloneNode(true),
    );
  }
}
customElements.define('custom-radio', CustomRadio);

async function assert_web_component_focus_navigation_forward(elements) {
  let start = document.getElementById(elements[0]);
  start.focus();
  for (let i = 1; i < elements.length; i++) {
    await navigateFocusForward();
    assert_equals(document.activeElement.id, elements[i]);
  }
}

async function assert_web_component_focus_navigation_backward(elements) {
  let end = document.getElementById(elements[elements.length - 1]);
  end.focus();
  for (let i = elements.length - 2; i >= 0; i--) {
    await navigateFocusBackward();
    assert_equals(document.activeElement.id, elements[i]);
  }
}

promise_test(async () => {
  let elements = [
    'start',
    'A',
    'B',
    'C',
    'D',
    'end'
  ];

  await assert_web_component_focus_navigation_forward(elements);
  await assert_web_component_focus_navigation_backward(elements);
}, 'Focus for web component input type elements should be bound by <form> inside shadow DOM');
</script>