summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/presentation-api/controlling-ua/PresentationConnection_onclose-manual.https.html
blob: f63806f82b09990d97acf685c0ea44c7140c973d (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
<!DOCTYPE html>

<meta charset="utf-8">
<title>Closing a PresentationConnection</title>
<link rel="author" title="Intel" href="http://www.intel.com">
<link rel="author" title="He Yue" href="mailto:yue.he@intel.com">
<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="support/stash.js"></script>
<h2>Description</h2>
<p>
  This test validates that after connection close,<br/>
  the connection state is set closed,<br/>
  the onclose EventHandler is triggered.
</p>
<br/>
<p>Click the button below to start the test.</p>
<button id="presentBtn" >Start Presentation Test</button>

<script>
  setup({explicit_timeout: true});

  const presentBtn = document.getElementById('presentBtn');

  promise_test(t => {
    const clickWatcher = new EventWatcher(t, presentBtn, 'click');
    const request = new PresentationRequest(presentationUrls);
    const stash = new Stash(stashIds.toController, stashIds.toReceiver);
    let connection, eventWatcher;

    t.add_cleanup(() => {
      if (connection) {
        connection.onconnect = () => { connection.terminate(); };
        if (connection.state === 'closed')
          request.reconnect(connection.id);
        else
          connection.terminate();
      }
      stash.stop();
    });

    const checkCloseEvent = evt => {
      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, 'closed', 'The reason for closing the presentation connection is "closed".');
    };

    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];
      });
    };

    const waitForEvent = (obj, watcher, type) => {
      const watchHandler = new Promise(resolve => {
        obj['on' + type] = evt => { resolve(evt); };
      });
      return Promise.race([ watchHandler, watcher.wait_for(type) ]);
    };

    return Promise.all([
      clickWatcher.wait_for('click'),
      stash.init()
    ]).then(() => {
      presentBtn.disabled = true;
      return request.start();
    }).then(c => {
      // Enable timeout again, cause no user action is needed from here.
      t.step_timeout(() => {
          t.force_timeout();
          t.done();
      }, 10000);

      connection = c;
      eventWatcher = new EventWatcher(t, connection, ['connect', 'close', 'terminate']);

      // Step 1: close the presentation connection in "connecting" state
      connection.close();
      return Promise.race([
        new Promise((_, reject) => {
          t.step_timeout(() => { reject('The presentation connection in "connecting" state was not closed successfully.'); }, 3000);
        }),
        watchEvent(connection, eventWatcher, 'close')
      ]);
    }).then(evt => {
      checkCloseEvent(evt);

      // Step 2: close the presentation connection in "connected" state
      return request.reconnect(connection.id);
    }).then(() => {
      return eventWatcher.wait_for('connect');
    }).then(() => {
      connection.close();
      return watchEvent(connection, eventWatcher, 'close');
    }).then(evt => {
      checkCloseEvent(evt);

      // Step 3: check a connection closed by the receiving user agent
      return request.reconnect(connection.id);
    }).then(() => {
      return eventWatcher.wait_for('connect');
    }).then(() => {
      return Promise.all([ stash.send('close'), watchEvent(connection, eventWatcher, 'close') ]);
    }).then(results => {
      checkCloseEvent(results[1]);

      // Step 4: close the presentation connection in "closed" state (nothing should happen)
      const closeWatcher = new EventWatcher(t, connection, 'close');
      connection.close();
      return Promise.race([
        new Promise(resolve => { t.step_timeout(resolve, 1000); }),
        waitForEvent(connection, closeWatcher, 'close').then(() => {
          assert_unreached('Invoking PresentationConnection.close() in the "closed" state causes nothing.'); })
      ]);
    }).then(() => {
      // Step 5: close the presentation connection in "terminated" state (nothing should happen)
      return request.reconnect(connection.id);
    }).then(() => {
      return eventWatcher.wait_for('connect');
    }).then(() => {
      connection.terminate();
      return eventWatcher.wait_for('terminate');
    }).then(() => {
      const closeWatcher = new EventWatcher(t, connection, 'close');
      connection.close();
      return Promise.race([
        new Promise(resolve => { t.step_timeout(resolve, 1000); }),
        waitForEvent(connection, closeWatcher, 'close').then(() => {
          assert_unreached('Invoking PresentationConnection.close() in the "terminated" state causes nothing.'); })
      ]);
    });
  });
</script>