1async_test(t => { 2 let c1 = new BroadcastChannel('eventType'); 3 let c2 = new BroadcastChannel('eventType'); 4 5 c2.onmessage = t.step_func(e => { 6 assert_true(e instanceof MessageEvent); 7 assert_equals(e.target, c2); 8 assert_equals(e.type, 'message'); 9 assert_equals(e.origin, location.origin, 'origin'); 10 assert_equals(e.data, 'hello world'); 11 assert_equals(e.source, null, 'source'); 12 t.done(); 13 }); 14 c1.postMessage('hello world'); 15 }, 'postMessage results in correct event'); 16 17async_test(t => { 18 let c1 = new BroadcastChannel('order'); 19 let c2 = new BroadcastChannel('order'); 20 let c3 = new BroadcastChannel('order'); 21 22 let events = []; 23 let doneCount = 0; 24 let handler = t.step_func(e => { 25 events.push(e); 26 if (e.data == 'done') { 27 doneCount++; 28 if (doneCount == 2) { 29 assert_equals(events.length, 6); 30 assert_equals(events[0].target, c2, 'target for event 0'); 31 assert_equals(events[0].data, 'from c1'); 32 assert_equals(events[1].target, c3, 'target for event 1'); 33 assert_equals(events[1].data, 'from c1'); 34 assert_equals(events[2].target, c1, 'target for event 2'); 35 assert_equals(events[2].data, 'from c3'); 36 assert_equals(events[3].target, c2, 'target for event 3'); 37 assert_equals(events[3].data, 'from c3'); 38 assert_equals(events[4].target, c1, 'target for event 4'); 39 assert_equals(events[4].data, 'done'); 40 assert_equals(events[5].target, c3, 'target for event 5'); 41 assert_equals(events[5].data, 'done'); 42 t.done(); 43 } 44 } 45 }); 46 c1.onmessage = handler; 47 c2.onmessage = handler; 48 c3.onmessage = handler; 49 50 c1.postMessage('from c1'); 51 c3.postMessage('from c3'); 52 c2.postMessage('done'); 53 }, 'messages are delivered in port creation order'); 54 55async_test(t => { 56 let c1 = new BroadcastChannel('closed'); 57 let c2 = new BroadcastChannel('closed'); 58 let c3 = new BroadcastChannel('closed'); 59 60 c2.onmessage = t.unreached_func(); 61 c2.close(); 62 c3.onmessage = t.step_func(() => t.done()); 63 c1.postMessage('test'); 64 }, 'messages aren\'t delivered to a closed port'); 65 66 async_test(t => { 67 let c1 = new BroadcastChannel('closed'); 68 let c2 = new BroadcastChannel('closed'); 69 let c3 = new BroadcastChannel('closed'); 70 71 c2.onmessage = t.unreached_func(); 72 c3.onmessage = t.step_func(() => t.done()); 73 c1.postMessage('test'); 74 c2.close(); 75}, 'messages aren\'t delivered to a port closed after calling postMessage.'); 76 77async_test(t => { 78 let c1 = new BroadcastChannel('create-in-onmessage'); 79 let c2 = new BroadcastChannel('create-in-onmessage'); 80 81 c2.onmessage = t.step_func(e => { 82 assert_equals(e.data, 'first'); 83 c2.close(); 84 let c3 = new BroadcastChannel('create-in-onmessage'); 85 c3.onmessage = t.step_func(event => { 86 assert_equals(event.data, 'done'); 87 t.done(); 88 }); 89 c1.postMessage('done'); 90 }); 91 c1.postMessage('first'); 92 c2.postMessage('second'); 93 }, 'closing and creating channels during message delivery works correctly'); 94 95async_test(t => { 96 let c1 = new BroadcastChannel('close-in-onmessage'); 97 let c2 = new BroadcastChannel('close-in-onmessage'); 98 let c3 = new BroadcastChannel('close-in-onmessage'); 99 let events = []; 100 c1.onmessage = e => events.push('c1: ' + e.data); 101 c2.onmessage = e => events.push('c2: ' + e.data); 102 c3.onmessage = e => events.push('c3: ' + e.data); 103 104 // c2 closes itself when it receives the first message 105 c2.addEventListener('message', e => { 106 c2.close(); 107 }); 108 109 c3.addEventListener('message', t.step_func(e => { 110 if (e.data == 'done') { 111 assert_array_equals(events, [ 112 'c2: first', 113 'c3: first', 114 'c3: done']); 115 t.done(); 116 } 117 })); 118 c1.postMessage('first'); 119 c1.postMessage('done'); 120 }, 'Closing a channel in onmessage prevents already queued tasks from firing onmessage events'); 121