1<!DOCTYPE html>
2<meta charset=utf-8>
3<script src="/resources/testharness.js"></script>
4<script src="/resources/testharnessreport.js"></script>
5<script>
6async_test(t => {
7    const c1 = new BroadcastChannel('blob');
8    const c2 = new BroadcastChannel('blob');
9    const c3 = new BroadcastChannel('blob');
10
11    let readCount = 0;
12    c2.onmessage = t.step_func(e => {
13        // check blob
14        assert_true('blob' in e.data);
15        assert_true(e.data.blob instanceof Blob);
16        assert_equals(e.data.blob.size, 6);
17        const reader = new FileReader();
18        reader.onerror = t.unreached_func();
19        reader.onload = t.step_func(() => {
20            assert_equals(reader.result, 'foobar');
21            if (++readCount == 2)
22              t.done();
23          });
24        reader.readAsText(e.data.blob);
25      });
26    c3.onmessage = c2.onmessage;
27    (() => {
28      c1.postMessage({blob: new Blob(['foo', 'bar'])});
29    })();
30    // TODO(https://github.com/web-platform-tests/wpt/issues/7899): Change to
31    // some sort of cross-browser GC trigger.
32    if (self.gc) self.gc();
33  }, 'Blobs work on BroadcastChannel');
34
35async_test(t => {
36    const c1 = new BroadcastChannel('blobworker');
37    const c2 = new BroadcastChannel('blobworker');
38    const events = [];
39
40    const verifyEvents = function() {
41        assert_equals(events.length, 5);
42        assert_equals(events[0], 'from worker');
43        assert_equals(events[1], 'from worker');
44        assert_true(events[2].blob instanceof Blob);
45        assert_equals(events[2].blob.size, 11);
46        assert_true(events[3].blob instanceof Blob);
47        assert_equals(events[3].blob.size, 11);
48        assert_equals(events[4], 'done');
49        const reader = new FileReader();
50        reader.onerror = t.unreached_func();
51        reader.onload = t.step_func(() => {
52            assert_equals(reader.result, 'hello-world');
53            t.done();
54          });
55        reader.readAsText(events[3].blob);
56    };
57
58    let receivedDone = false;
59    let receivedWorkerDone = false;
60
61    c1.onmessage = e => events.push(e.data);
62    c2.onmessage = e => events.push(e.data);
63    c2.addEventListener('message', t.step_func(e => {
64        if (e.data.blob)
65          c1.postMessage('done');
66        if (e.data === 'done')
67          receivedDone = true;
68        if (receivedDone && receivedWorkerDone)
69          verifyEvents();
70      }));
71
72    const worker = new Worker('resources/worker.js');
73    worker.onmessage = t.step_func(e => {
74        receivedWorkerDone = true;
75        if (receivedDone && receivedWorkerDone)
76          verifyEvents();
77      });
78    worker.postMessage({channel: 'blobworker'});
79    worker.postMessage({blob: ['hello-world']});
80
81  }, 'Blobs work with workers on BroadcastChannel');
82
83</script>
84