summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/portals/portals-focus.sub.html
blob: 54b4312be0ada5c7e8e1bba50184d421de10a420 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<!DOCTYPE html>
<meta name="timeout" content="long">
<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/open-blank-host.js"></script>
<body>
<script>
  async function createPortal(doc, url) {
    assert_implements("HTMLPortalElement" in self);
    let portal = doc.createElement("portal");
    portal.src = url;
    doc.body.appendChild(portal);
    await new Promise(r => portal.onload = r);
    return portal;
  }

  promise_test(async t => {
    let portal = await createPortal(document, new URL("resources/focus-page-with-button.html", location.href));
    try {
      portal.onmessage = t.step_func(e => {
        assert_unreached("button inside portal should not be focused");
      });
      portal.postMessage("focus");
      await new Promise(r => t.step_timeout(r, 500));
    } finally {
      document.body.removeChild(portal);
    }
  }, "test that an element inside a portal cannot steal focus");

  promise_test(async () => {
    let portal = await createPortal(document, new URL("resources/focus-page-with-button.html", location.href));
    try {
      let activeElementUpdated = new Promise(r => {
        portal.onmessage = e => r(e.data.activeElementUpdated)
      });
      portal.postMessage('focus-update-active-element');
      assert_true(await activeElementUpdated);
    } finally {
      document.body.removeChild(portal);
    }
  }, "test that activeElement inside a portal is updated after focus() is called");

  promise_test(async t => {
    let portal = await createPortal(document, new URL("resources/focus-page-with-x-origin-iframe.sub.html", location.href));
    try {
      portal.onmessage = t.step_func(e => {
        assert_unreached("button inside portal should not be focused");
      });
      portal.postMessage("focus");
      await new Promise(r => t.step_timeout(r, 500));
    } finally {
      document.body.removeChild(portal);
    }
  }, "test that an element inside a portal's x-origin subframe cannot steal focus");

  promise_test(async () => {
    let portal = await createPortal(document, new URL("resources/focus-page-with-x-origin-iframe.sub.html", location.href));
    try {
      portal.postMessage("focus-update-active-element");
      let {activeElementUpdated} = await new Promise(r => {
        portal.onmessage = e => r(e.data);
      });
      assert_true(activeElementUpdated);
    } finally {
      document.body.removeChild(portal);
    }
  }, "test that a portal's x-origin subframe becomes active element on focus");

  promise_test(async t => {
    let win = await openBlankPortalHost();
    let doc = win.document;
    try {
      let portal = await createPortal(doc, new URL("resources/simple-portal-adopts-predecessor.html", location.href));
      let button = doc.createElement("button");
      doc.body.appendChild(button);

      await portal.activate();
      doc.body.removeChild(portal);

      button.onfocus = t.step_func(() => {
        assert_unreached("button inside adopted portal should not be focused");
      });
      button.focus();
      await new Promise(r => t.step_timeout(r, 500));
    } finally {
      win.close();
    }
  }, "test that an element inside an adopted portal cannot steal focus");

  promise_test(async t => {
    let win = await openBlankPortalHost();
    let doc = win.document;
    try {
      let portal = await createPortal(doc, new URL("resources/simple-portal-adopts-predecessor.html", location.href));
      let iframe = doc.createElement("iframe");
      iframe.src = new URL("resources/focus-page-with-button.html",
                           "http://{{hosts[alt][www]}}:{{ports[http][0]}}/portals/");
      doc.body.appendChild(iframe);
      await new Promise(r => iframe.onload = r);

      await portal.activate();
      doc.body.removeChild(portal);

      iframe.contentWindow.postMessage("focus", "*");
      window.onmessage = t.step_func(() => {
        assert_unreached("button inside x-origin iframe inside a portal should not be focused");
      });
      await new Promise(r => t.step_timeout(r, 500));
    } finally {
      win.close();
    }
  }, "test that a x-origin iframe inside an adopted portal cannot steal focus");

  promise_test(async () => {
    let win = await openBlankPortalHost();
    let doc = win.document;
    try {
      let portal = await createPortal(doc, new URL("resources/focus-page-with-autofocus.html", location.href));
      portal.postMessage('check-active-element');
      let result = await new Promise(r => {
        portal.onmessage = e => r(e.data);
      });
      assert_true(result, "autofocused element is active element");

      await portal.activate();
      win.portalHost.postMessage('check-active-element');
      result = await new Promise(r => {
        win.portalHost.onmessage = e => r(e.data)
      });
      assert_true(result, "autofocused element is still active element");
    } finally {
      win.close();
    }
  }, "test that autofocus inside a portal works");

  const TAB = "\ue004"; // https://w3c.github.io/webdriver/#keyboard-actions
  const SPACE = " "
  const RETURN = "\r";

  promise_test(async t => {
    let portal = await createPortal(document, "resources/focus-page-with-button.html");
    try {
      await test_driver.send_keys(document.body, TAB);
      portal.onmessage = t.unreached_func("button inside portal should not be focused");
      await new Promise(r => t.step_timeout(r, 500));
    } finally {
      document.body.removeChild(portal);
    }
  }, "test that a portal is keyboard focusable");

  promise_test(async t => {
    let portal = await createPortal(document, "resources/focus-page-with-button.html");
    try {
      let portalFocusPromise = new Promise(r => portal.onfocus = r);
      portal.onmessage = t.unreached_func("button inside portal should not be focused");
      await test_driver.send_keys(document.body, TAB);
      await portalFocusPromise;
      await test_driver.send_keys(document.body, TAB);
      await new Promise(r => t.step_timeout(r, 500));
    } finally {
      document.body.removeChild(portal);
    }
  }, "test that we cannot tab into a portal's contents");

  promise_test(async t => {
    let portal = await createPortal(document, "resources/simple-portal.html");
    try {
      portal.focus();
      for (let key of [SPACE, RETURN]) {
        let clickPromise = new Promise((resolve) => {
          portal.onclick = e => { e.preventDefault(); resolve(); };
        });
        await test_driver.send_keys(document.body, key);
        await clickPromise;
      }
    } finally {
      document.body.removeChild(portal);
    }
  }, "test that a portal is keyboard activatable");

</script>
</body>