summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/window-management/resources/helpers.js
blob: ee77fd8d30d713eb5e764db51c0edd1429e0f1e8 (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
// Appends a list item with `innerHTML` to the document's 'list' element.
function log(innerHTML) {
  const li = document.createElement('li');
  li.innerHTML = innerHTML;
  document.getElementById('list').appendChild(li);
}

// Returns a string with the label and bounds of screen `s` for logging.
function screenLog(s) {
  return `'${s.label}': (${s.left},${s.top} ${s.width}x${s.height})`;
}

// Returns a string with the bounds of window `w` for logging.
function windowLog(w) {
  return `(${w.screenLeft},${w.screenTop} ${w.outerWidth}x${w.outerHeight})`;
}

// Appends a button with `innerHTML` to the document's `list` element.
// Waits for a test driver or manual click, and disables the button afterwards.
async function buttonClick(test, innerHTML) {
  const button = document.createElement('button');
  button.innerHTML = innerHTML;
  const li = document.createElement('li');
  li.appendChild(button)
  document.getElementById('list').appendChild(li);
  const click = new EventWatcher(test, button, ['click']).wait_for('click');
  try {  // Support manual testing where test_driver is not running.
    await test_driver.click(button);
  } catch {
  }
  await click;
  button.disabled = true;
}

// Grants `window-management` permission and caches `window.screenDetails`.
async function setUpWindowManagement(test) {
  assert_implements(
    'getScreenDetails' in self && 'isExtended' in screen,
    `API not supported; use Chrome or Chromium (not content_shell)`);
  if (!screen.isExtended)
    log(`WARNING: Use multiple screens for full test coverage`);
  if (window.location.href.startsWith('file'))
    log(`WARNING: Run via 'wpt serve'; file URLs lack permission support`);

  try {  // Support manual testing where test_driver is not running.
    await test_driver.set_permission({ name: 'window-management' }, 'granted');
  } catch {
  }
  await buttonClick(test, 'Request screen details');
  window.screenDetails = await window.getScreenDetails();
  assert_true(!!window.screenDetails, 'Error getting screen details');
}

// Polls until `condition` is true, with the given `interval` and `duration`.
// Returns a promise that will be resolved on success or timeout.
async function poll(condition, interval = 100, duration = 3000) {
  const timeout = Date.now() + duration;
  const loop = (resolve) => {
    if (condition() || Date.now() > timeout)
      resolve();
    else
      step_timeout(loop, interval, resolve);
  }
  return new Promise(loop);
}

// Open and return a popup on `screen`, optionally asserting placement.
async function openPopupOnScreen(screen, assertPlacement = true, fullscreen = false) {
  const left = screen.availLeft + Math.floor(screen.availWidth / 2) - 150;
  const top = screen.availTop + Math.floor(screen.availHeight / 2) - 50;
  let features = `left=${left},top=${top},width=300,height=100`;
  if (fullscreen) {
    features += ",fullscreen";
  }
  log(`Opening a popup with features '${features}' on ${screenLog(screen)}`);
  // Window.open() synchronously returns a Window with estimated screenLeft|Top,
  // which may be clamped to the opener's screen or incompletely initialized.
  let popup = window.open('/resources/blank.html', '', features);

  if (assertPlacement) {
    // Assert the popup is eventually placed at the expected location.
    // This may occur after window load, document ready and visible, etc.
    const initialBounds = windowLog(popup);
    log(`<div style='margin-left: 40px'>Initial: ${initialBounds}</div>`);
    await poll(() => { return popup.screenLeft == left &&
                              popup.screenTop == top });
    popup.document.write(`Requested: (${left},${top} 300x100) <br> \
        Initial: ${initialBounds} <br> \
        Resolved: ${windowLog(popup)}`);
    log(`<div style='margin-left: 40px'>Resolved: ${windowLog(popup)}</div>`);
    const context = `popup: ${windowLog(popup)}, ${screenLog(screen)}`;
    assert_equals(popup.screenLeft, left, context);
    assert_equals(popup.screenTop, top, context);
  }

  return popup;
}

// Returns true if window `w` bounds are on screen `s` with threshold `t`.
function isWindowOnScreen(w, s, t = 100) {
  return (w.screenLeft >= s.left - t) && (w.screenTop >= s.top - t) &&
          (w.screenLeft + w.outerWidth <= s.left + s.width + t) &&
          (w.screenTop + w.outerHeight <= s.top + s.height + t);
}

// Asserts window `w` currentScreen matches screen `s`. Awaits pending changes,
// e.g. fullscreen promises may resolve before screen change: crbug.com/1330724.
async function assertWindowHasCurrentScreen(w, s) {
  log(`assertWindowHasCurrentScreen w: ${windowLog(w)} s: ${screenLog(s)}`);
  await poll(() => { return s === w.screenDetails.currentScreen; });
  assert_equals(screenLog(s), screenLog(w.screenDetails.currentScreen));
}

// Asserts window `w` bounds roughly match screen `s`. Awaits pending changes,
// e.g. fullscreen promises may resolve before bounds change: crbug.com/1330724.
async function assertWindowBoundsOnScreen(w, s) {
  log(`assertWindowBoundsOnScreen w: ${windowLog(w)} s: ${screenLog(s)}`);
  await poll(() => { return isWindowOnScreen(w, s); });
  assert_true(isWindowOnScreen(w, s), `${windowLog(w)} on ${screenLog(s)}`);
}

// Asserts window `w` bounds and currentScreen match screen `s`.
async function assertWindowOnScreen(w, s) {
  await assertWindowHasCurrentScreen(w, s);
  await assertWindowBoundsOnScreen(w, s);
}