summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/streams/transferable/resources/helpers.js
blob: 12504537f91eab42f8822226eda80513698f477d (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
'use strict';

(() => {
  // Create a ReadableStream that will pass the tests in
  // testTransferredReadableStream(), below.
  function createOriginalReadableStream() {
    return new ReadableStream({
      start(controller) {
        controller.enqueue('a');
        controller.close();
      }
    });
  }

  // Common tests to roughly determine that |rs| is a correctly transferred
  // version of a stream created by createOriginalReadableStream().
  function testTransferredReadableStream(rs) {
    assert_equals(rs.constructor, ReadableStream,
                  'rs should be a ReadableStream in this realm');
    assert_true(rs instanceof ReadableStream,
                'instanceof check should pass');

    // Perform a brand-check on |rs| in the process of calling getReader().
    const reader = ReadableStream.prototype.getReader.call(rs);

    return reader.read().then(({value, done}) => {
      assert_false(done, 'done should be false');
      assert_equals(value, 'a', 'value should be "a"');
      return reader.read();
    }).then(({done}) => {
      assert_true(done, 'done should be true');
    });
  }

  function testMessage(msg) {
    assert_array_equals(msg.ports, [], 'there should be no ports in the event');
    return testTransferredReadableStream(msg.data);
  }

  function testMessageEvent(target) {
    return new Promise((resolve, reject) => {
      target.addEventListener('message', ev => {
        try {
          resolve(testMessage(ev));
        } catch (e) {
          reject(e);
        }
      }, {once: true});
    });
  }

  function testMessageEventOrErrorMessage(target) {
    return new Promise((resolve, reject) => {
      target.addEventListener('message', ev => {
        if (typeof ev.data === 'string') {
          // Assume it's an error message and reject with it.
          reject(ev.data);
          return;
        }

        try {
          resolve(testMessage(ev));
        } catch (e) {
          reject(e);
        }
      }, {once: true});
    });
  }

  function checkTestResults(target) {
    return new Promise((resolve, reject) => {
      target.onmessage = msg => {
        // testharness.js sends us objects which we need to ignore.
        if (typeof msg.data !== 'string')
        return;

        if (msg.data === 'OK') {
          resolve();
        } else {
          reject(msg.data);
        }
      };
    });
  }

  // These tests assume that a transferred ReadableStream will behave the same
  // regardless of how it was transferred. This enables us to simply transfer the
  // stream to ourselves.
  function createTransferredReadableStream(underlyingSource) {
    const original = new ReadableStream(underlyingSource);
    const promise = new Promise((resolve, reject) => {
      addEventListener('message', msg => {
        const rs = msg.data;
        if (rs instanceof ReadableStream) {
          resolve(rs);
        } else {
          reject(new Error(`what is this thing: "${rs}"?`));
        }
      }, {once: true});
    });
    postMessage(original, '*', [original]);
    return promise;
  }

  function recordingTransferredReadableStream(underlyingSource, strategy) {
    const original = recordingReadableStream(underlyingSource, strategy);
    const promise = new Promise((resolve, reject) => {
      addEventListener('message', msg => {
        const rs = msg.data;
        if (rs instanceof ReadableStream) {
          rs.events = original.events;
          rs.eventsWithoutPulls = original.eventsWithoutPulls;
          rs.controller = original.controller;
          resolve(rs);
        } else {
          reject(new Error(`what is this thing: "${rs}"?`));
        }
      }, {once: true});
    });
    postMessage(original, '*', [original]);
    return promise;
  }

  self.createOriginalReadableStream = createOriginalReadableStream;
  self.testMessage = testMessage;
  self.testMessageEvent = testMessageEvent;
  self.testMessageEventOrErrorMessage = testMessageEventOrErrorMessage;
  self.checkTestResults = checkTestResults;
  self.createTransferredReadableStream = createTransferredReadableStream;
  self.recordingTransferredReadableStream = recordingTransferredReadableStream;

})();