summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/speculation-rules/prerender/resources/window-move.html
blob: 0c5888c957e28e49452134ae37b3239821d3b040 (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
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="utils.js"></script>
<script>

const params = new URLSearchParams(location.search);

// The main test page (restriction-window-move.html) loads the
// initiator page, then the initiator page will prerender itself with the
// `prerendering` parameter.
const isPrerendering = params.has('prerendering');

function tryRun(func) {
  try {
    func();
  } catch (e) {
    const testChannel = new PrerenderChannel('test-channel');
    testChannel.postMessage({status: 'FAIL: ' + e});
  }
}

if (!isPrerendering) {
  // Ensure that the primary page can move this window.
  tryRun(() => {
    const expectedPosition = {x: screen.availLeft + 1, y: screen.availTop + 1};
    window.moveTo(expectedPosition.x, expectedPosition.y);
    assert_equals(window.screenX, expectedPosition.x, 'x position for primary');
    assert_equals(window.screenY, expectedPosition.y, 'y position for primary');
  });
  // Start prerendering a page which tries to move this window.
  loadInitiatorPage();
} else {
  const prevPosition = {x: window.screenX, y: window.screenY};
  tryRun(
      () => {
        // Try to move this window, and should not succeed.
        const moveToOrMoveBy = params.get('move');
        switch (moveToOrMoveBy) {
          case 'moveTo':
            window.moveTo(screen.availLeft + 10, screen.availTop + 10);
            break;
          case 'moveBy':
            window.moveBy(screen.availLeft + 10 - window.screenX,
                          screen.availTop + 10 - window.screenY);
            break;
          default:
            assert_unreached(`wrong parameter: ${moveToOrMoveBy}`);
        }
      }
  );

  const bc = new PrerenderChannel('test-channel');
  bc.postMessage({
    'status': 'PASS',
    'prevPosition': prevPosition,
    'newPosition': {x: window.screenX, y: window.screenY}
  });
  bc.close();
}

</script>