summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationConnection_onclose_receiving-ua.html
blob: 7194c6f4d96882ee44ff464922b06ad219029713 (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
<!DOCTYPE html>

<meta charset="utf-8">
<title>Closing a PresentationConnection</title>
<link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs">
<link rel="help" href="https://w3c.github.io/presentation-api/#closing-a-presentationconnection">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../common.js"></script>
<script src="stash.js"></script>

<script>
  const stash = new Stash(stashIds.toReceiver, stashIds.toController);

  add_completion_callback((tests, status) => {
    const log = document.getElementById('log');
    stash.send(JSON.stringify({ type: 'result', tests: tests, status: status, log: log.innerHTML }));
  });

  const checkCloseEvent = (evt, connection, reason) => {
    assert_true(evt instanceof PresentationConnectionCloseEvent, 'An event using PresentationConnectionCloseEvent is fired.');
    assert_true(evt.isTrusted, 'The event is a trusted event.');
    assert_false(evt.bubbles, 'The event does not bubbles.');
    assert_false(evt.cancelable, 'The event is not cancelable.');
    assert_equals(evt.type, 'close', 'The event name is "close".');
    assert_equals(evt.target, connection, 'event.target is the presentation connection.');
    assert_equals(connection.state, 'closed', 'State of the presentation connection is "closed".');
    assert_equals(evt.reason, reason, 'The reason for closing the presentation connection is "' + reason + '".');
  };

  const watchEvent = (obj, watcher, type) => {
    const watchHandler = new Promise(resolve => {
      obj['on' + type] = evt => { resolve(evt); };
    });
    return Promise.all([ watchHandler, watcher.wait_for(type) ]).then(results => {
      assert_equals(results[0], results[1], 'Both on' + type + ' and addEventListener pass the same event object.');
      return results[0];
    });
  };

  promise_test(t => {
    return navigator.presentation.receiver.connectionList.then(list => {
      let connection = list.connections[0];
      assert_equals(list.connections.length, 1, 'A presentation connection list is populated with a first presentation connection.');

      // Step 1: close the presentation connection in "connected" state
      connection.close();
      let watchClose = new EventWatcher(t, connection, 'close');
      const watchNewConnection = new EventWatcher(t, list, 'connectionavailable');
      return watchEvent(connection, watchClose, 'close').then(evt => {
        checkCloseEvent(evt, connection, 'closed');

        // Step 2: check a connection closed by the controlling user agent
        stash.send(JSON.stringify({ type: 'ok' }));
        return watchNewConnection.wait_for('connectionavailable');
      }).then(evt => {
        connection = evt.connection;
        watchClose = new EventWatcher(t, connection, 'close');
        return watchEvent(connection, watchClose, 'close');
      }).then(evt => {
        checkCloseEvent(evt, connection, 'closed');

        // Step 3: try to close the presentation connection in "closed" state (nothing should happen)
        connection.close();
        return Promise.race([
          new Promise(resolve => { t.step_timeout(resolve, 1000); }),
          watchEvent(connection, watchClose, 'close').then(() => {
            assert_unreached('Invoking PresentationConnection.close() in the "closed" state causes nothing.'); })
        ]);
      }).then(() => {

        // Step 4: check a connection closed due to aborting the nested browsing context
        stash.send(JSON.stringify({ type: 'ok' }));
        return watchNewConnection.wait_for('connectionavailable');
      }).then(evt => {
        connection = evt.connection;
        stash.send(JSON.stringify({ type: 'ok' }));
        watchClose = new EventWatcher(t, connection, 'close');
        return Promise.race([
          watchEvent(connection, watchClose, 'close'),
          new Promise(resolve => { t.step_timeout(() => { resolve(null); }, 3000); })
        ]);
      }).then(evt => {
        assert_true(!!evt, 'A presentation connection is closed when its controller\'s browsing context is discarded.');
        checkCloseEvent(evt, connection, 'wentaway');
      });
    });
  });
</script>