11cb0ef41Sopenharmony_ciasync_test(t => { 21cb0ef41Sopenharmony_ci let c1 = new BroadcastChannel('eventType'); 31cb0ef41Sopenharmony_ci let c2 = new BroadcastChannel('eventType'); 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci c2.onmessage = t.step_func(e => { 61cb0ef41Sopenharmony_ci assert_true(e instanceof MessageEvent); 71cb0ef41Sopenharmony_ci assert_equals(e.target, c2); 81cb0ef41Sopenharmony_ci assert_equals(e.type, 'message'); 91cb0ef41Sopenharmony_ci assert_equals(e.origin, location.origin, 'origin'); 101cb0ef41Sopenharmony_ci assert_equals(e.data, 'hello world'); 111cb0ef41Sopenharmony_ci assert_equals(e.source, null, 'source'); 121cb0ef41Sopenharmony_ci t.done(); 131cb0ef41Sopenharmony_ci }); 141cb0ef41Sopenharmony_ci c1.postMessage('hello world'); 151cb0ef41Sopenharmony_ci }, 'postMessage results in correct event'); 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ciasync_test(t => { 181cb0ef41Sopenharmony_ci let c1 = new BroadcastChannel('order'); 191cb0ef41Sopenharmony_ci let c2 = new BroadcastChannel('order'); 201cb0ef41Sopenharmony_ci let c3 = new BroadcastChannel('order'); 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci let events = []; 231cb0ef41Sopenharmony_ci let doneCount = 0; 241cb0ef41Sopenharmony_ci let handler = t.step_func(e => { 251cb0ef41Sopenharmony_ci events.push(e); 261cb0ef41Sopenharmony_ci if (e.data == 'done') { 271cb0ef41Sopenharmony_ci doneCount++; 281cb0ef41Sopenharmony_ci if (doneCount == 2) { 291cb0ef41Sopenharmony_ci assert_equals(events.length, 6); 301cb0ef41Sopenharmony_ci assert_equals(events[0].target, c2, 'target for event 0'); 311cb0ef41Sopenharmony_ci assert_equals(events[0].data, 'from c1'); 321cb0ef41Sopenharmony_ci assert_equals(events[1].target, c3, 'target for event 1'); 331cb0ef41Sopenharmony_ci assert_equals(events[1].data, 'from c1'); 341cb0ef41Sopenharmony_ci assert_equals(events[2].target, c1, 'target for event 2'); 351cb0ef41Sopenharmony_ci assert_equals(events[2].data, 'from c3'); 361cb0ef41Sopenharmony_ci assert_equals(events[3].target, c2, 'target for event 3'); 371cb0ef41Sopenharmony_ci assert_equals(events[3].data, 'from c3'); 381cb0ef41Sopenharmony_ci assert_equals(events[4].target, c1, 'target for event 4'); 391cb0ef41Sopenharmony_ci assert_equals(events[4].data, 'done'); 401cb0ef41Sopenharmony_ci assert_equals(events[5].target, c3, 'target for event 5'); 411cb0ef41Sopenharmony_ci assert_equals(events[5].data, 'done'); 421cb0ef41Sopenharmony_ci t.done(); 431cb0ef41Sopenharmony_ci } 441cb0ef41Sopenharmony_ci } 451cb0ef41Sopenharmony_ci }); 461cb0ef41Sopenharmony_ci c1.onmessage = handler; 471cb0ef41Sopenharmony_ci c2.onmessage = handler; 481cb0ef41Sopenharmony_ci c3.onmessage = handler; 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci c1.postMessage('from c1'); 511cb0ef41Sopenharmony_ci c3.postMessage('from c3'); 521cb0ef41Sopenharmony_ci c2.postMessage('done'); 531cb0ef41Sopenharmony_ci }, 'messages are delivered in port creation order'); 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ciasync_test(t => { 561cb0ef41Sopenharmony_ci let c1 = new BroadcastChannel('closed'); 571cb0ef41Sopenharmony_ci let c2 = new BroadcastChannel('closed'); 581cb0ef41Sopenharmony_ci let c3 = new BroadcastChannel('closed'); 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci c2.onmessage = t.unreached_func(); 611cb0ef41Sopenharmony_ci c2.close(); 621cb0ef41Sopenharmony_ci c3.onmessage = t.step_func(() => t.done()); 631cb0ef41Sopenharmony_ci c1.postMessage('test'); 641cb0ef41Sopenharmony_ci }, 'messages aren\'t delivered to a closed port'); 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci async_test(t => { 671cb0ef41Sopenharmony_ci let c1 = new BroadcastChannel('closed'); 681cb0ef41Sopenharmony_ci let c2 = new BroadcastChannel('closed'); 691cb0ef41Sopenharmony_ci let c3 = new BroadcastChannel('closed'); 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci c2.onmessage = t.unreached_func(); 721cb0ef41Sopenharmony_ci c3.onmessage = t.step_func(() => t.done()); 731cb0ef41Sopenharmony_ci c1.postMessage('test'); 741cb0ef41Sopenharmony_ci c2.close(); 751cb0ef41Sopenharmony_ci}, 'messages aren\'t delivered to a port closed after calling postMessage.'); 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ciasync_test(t => { 781cb0ef41Sopenharmony_ci let c1 = new BroadcastChannel('create-in-onmessage'); 791cb0ef41Sopenharmony_ci let c2 = new BroadcastChannel('create-in-onmessage'); 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci c2.onmessage = t.step_func(e => { 821cb0ef41Sopenharmony_ci assert_equals(e.data, 'first'); 831cb0ef41Sopenharmony_ci c2.close(); 841cb0ef41Sopenharmony_ci let c3 = new BroadcastChannel('create-in-onmessage'); 851cb0ef41Sopenharmony_ci c3.onmessage = t.step_func(event => { 861cb0ef41Sopenharmony_ci assert_equals(event.data, 'done'); 871cb0ef41Sopenharmony_ci t.done(); 881cb0ef41Sopenharmony_ci }); 891cb0ef41Sopenharmony_ci c1.postMessage('done'); 901cb0ef41Sopenharmony_ci }); 911cb0ef41Sopenharmony_ci c1.postMessage('first'); 921cb0ef41Sopenharmony_ci c2.postMessage('second'); 931cb0ef41Sopenharmony_ci }, 'closing and creating channels during message delivery works correctly'); 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ciasync_test(t => { 961cb0ef41Sopenharmony_ci let c1 = new BroadcastChannel('close-in-onmessage'); 971cb0ef41Sopenharmony_ci let c2 = new BroadcastChannel('close-in-onmessage'); 981cb0ef41Sopenharmony_ci let c3 = new BroadcastChannel('close-in-onmessage'); 991cb0ef41Sopenharmony_ci let events = []; 1001cb0ef41Sopenharmony_ci c1.onmessage = e => events.push('c1: ' + e.data); 1011cb0ef41Sopenharmony_ci c2.onmessage = e => events.push('c2: ' + e.data); 1021cb0ef41Sopenharmony_ci c3.onmessage = e => events.push('c3: ' + e.data); 1031cb0ef41Sopenharmony_ci 1041cb0ef41Sopenharmony_ci // c2 closes itself when it receives the first message 1051cb0ef41Sopenharmony_ci c2.addEventListener('message', e => { 1061cb0ef41Sopenharmony_ci c2.close(); 1071cb0ef41Sopenharmony_ci }); 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ci c3.addEventListener('message', t.step_func(e => { 1101cb0ef41Sopenharmony_ci if (e.data == 'done') { 1111cb0ef41Sopenharmony_ci assert_array_equals(events, [ 1121cb0ef41Sopenharmony_ci 'c2: first', 1131cb0ef41Sopenharmony_ci 'c3: first', 1141cb0ef41Sopenharmony_ci 'c3: done']); 1151cb0ef41Sopenharmony_ci t.done(); 1161cb0ef41Sopenharmony_ci } 1171cb0ef41Sopenharmony_ci })); 1181cb0ef41Sopenharmony_ci c1.postMessage('first'); 1191cb0ef41Sopenharmony_ci c1.postMessage('done'); 1201cb0ef41Sopenharmony_ci }, 'Closing a channel in onmessage prevents already queued tasks from firing onmessage events'); 121