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

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

<p id="notice">
  Click the button below and select the available presentation display, to start the manual test. The test passes if a "PASS" result appears.<br>
  <button id="presentBtn">Start Presentation Test</button>
</p>

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

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

    const message1 = '1st';
    const message2 = '2nd';
    const message3 = new Uint8Array([51, 114, 100]);      // "3rd"
    const message4 = new Uint8Array([52, 116, 104]);      // "4th"
    const message5 = new Uint8Array([108, 97, 115, 116]); // "last"

    const toUint8Array = buf => {
        return buf instanceof ArrayBuffer ? new Uint8Array(buf) : buf;
    }

    // convert ArrayBuffer or Uint8Array into string
    const toText = buf => {
        const arr = toUint8Array(buf);
        return !buf ? null : arr.reduce((result, item) => {
            return result + String.fromCharCode(item);
        }, '');
    }

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

        t.add_cleanup(() => {
            if (connection) {
                connection.onconnect = () => { connection.terminate(); };
                if (connection.state === 'closed')
                    request.reconnect(connection.id);
                else
                    connection.terminate();
            }
            const notice = document.getElementById('notice');
            notice.parentNode.removeChild(notice);
            stash.stop();
        });

        return clickWatcher.wait_for('click').then(() => {
            presentBtn.disabled = true;

            return request.start();
        }).then(c => {
            connection = c;

            // send data in "connecting" state (throws an exception)
            assert_equals(connection.state, 'connecting', 'the initial state of the presentation connection is "connecting"');
            assert_throws_dom('InvalidStateError', () => {
                connection.send('');
            }, 'an InvalidStateError is thrown if the state is "connecting"');

            // enable timeout again, cause no user action is needed from here.
            t.step_timeout(() => {
                t.force_timeout();
                t.done();
            }, 10000);

            watcher = new EventWatcher(t, connection, ['connect', 'close', 'terminate']);
            return watcher.wait_for('connect');
        }).then(() => {
            return stash.init();
        }).then(() => {
            return Promise.all([ stash.send('send'), stash.receive() ]);
        }).then(results => {
            // send messages
            connection.send(message1);              // string
            connection.send(message2);              // string
            connection.send(new Blob([message3]));  // Blob
            connection.send(message4.buffer);       // ArrayBuffer
            connection.send(message5);              // ArrayBufferView
            return stash.receive();
        }).then(stash => {
            // verify messages
            const results = JSON.parse(stash);
            assert_true(!!results[0] && results[0].type === 'text'   && results[0].data === message1,          'send a string correctly');
            assert_true(!!results[1] && results[1].type === 'text'   && results[1].data === message2,          'send a string correctly');
            assert_true(!!results[2] && results[2].type === 'binary' && results[2].data === toText(message3), 'send a Blob correctly');
            assert_true(!!results[3] && results[3].type === 'binary' && results[3].data === toText(message4), 'send a ArrayBuffer correctly');
            assert_true(!!results[4] && results[4].type === 'binary' && results[4].data === toText(message5), 'send a ArrayBufferView correctly');

            // send data in "closed" state (throws an exception)
            connection.close();
            return watcher.wait_for('close');
        }).then(() => {
            assert_equals(connection.state, 'closed', 'the state is set to "closed" when the presentation connection is closed');
            assert_throws_dom('InvalidStateError', () => {
                connection.send('');
            }, 'an InvalidStateError is thrown if the state is "closed"');

            // reconnect and terminate the connection
            return request.reconnect(connection.id);
        }).then(() => {
            return watcher.wait_for('connect');
        }).then(() => {
            // send data in "terminated" state (throws an exception)
            connection.terminate();
            return watcher.wait_for('terminate');
        }).then(() => {
            assert_equals(connection.state, 'terminated', 'the state is set to "terminated" when the presentation connection is terminated');
            assert_throws_dom('InvalidStateError', () => {
                connection.send('');
            }, 'an InvalidStateError is thrown if the state is "terminated"');
        });
    });
</script>