summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/close-watcher/resources/helpers.js
blob: 97a62309cd5b39c14ab9c31b9c1305b160ba5c7e (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
window.createRecordingCloseWatcher = (t, events, name, type, parentWatcher) => {
  let watcher = null;
  if (type === 'dialog') {
    watcher = document.createElement('dialog');
    watcher.textContent = 'hello world';
    t.add_cleanup(() => watcher.remove());
    if (parentWatcher?.appendChild) {
      parentWatcher.appendChild(watcher);
    } else {
      document.body.appendChild(watcher);
    }
    watcher.showModal();
  } else if (type === 'popover') {
    watcher = document.createElement('div');
    watcher.setAttribute('popover', 'auto');
    watcher.textContent = 'hello world';
    t.add_cleanup(() => watcher.remove());
    if (parentWatcher?.appendChild) {
      parentWatcher.appendChild(watcher);
    } else {
      document.body.appendChild(watcher);
    }
    watcher.showPopover();
  } else {
    watcher = new CloseWatcher();
    t.add_cleanup(() => watcher.destroy());
  }

  const prefix = name === undefined ? "" : name + " ";
  watcher.addEventListener('cancel', () => events.push(prefix + "cancel"));
  watcher.addEventListener('close', () => events.push(prefix + "close"));

  return watcher;
};

window.createBlessedRecordingCloseWatcher = async (t, events, name, type, parentWatcher) => {
  await maybeTopLayerBless(parentWatcher);
  return createRecordingCloseWatcher(t, events, name, type, parentWatcher);
};

window.sendEscKey = () => {
  // Esc is \uE00C, *not* \uu001B; see https://w3c.github.io/webdriver/#keyboard-actions.
  //
  // It's important to target document.body, and not any element that might stop receiving events
  // if a popover or dialog is making that element inert.
  return test_driver.send_keys(document.body, '\uE00C');
};

// For now, we always use the Esc keypress as our close request. In
// theory, in the future, we could add a WebDriver command or similar
// for the close request, which would allow different tests on platforms
// with different close requests. In that case, we'd update this
// function, but not update the sendEscKey function above.
window.sendCloseRequest = window.sendEscKey;

window.maybeTopLayerBless = (watcher) => {
  if (watcher instanceof HTMLElement) {
    return blessTopLayer(watcher);
  }
  return test_driver.bless();
};