11cb0ef41Sopenharmony_ci<!DOCTYPE html> 21cb0ef41Sopenharmony_ci<meta charset=utf-8> 31cb0ef41Sopenharmony_ci<script src="/resources/testharness.js"></script> 41cb0ef41Sopenharmony_ci<script src="/resources/testharnessreport.js"></script> 51cb0ef41Sopenharmony_ci<script> 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciconst SERVICE_WORKER_TEST_CHANNEL_NAME = 'service worker'; 81cb0ef41Sopenharmony_ciconst events = []; 91cb0ef41Sopenharmony_ciconst c1 = new BroadcastChannel(SERVICE_WORKER_TEST_CHANNEL_NAME); 101cb0ef41Sopenharmony_ciconst c2 = new BroadcastChannel(SERVICE_WORKER_TEST_CHANNEL_NAME); 111cb0ef41Sopenharmony_cic1.onmessage = e => events.push(e); 121cb0ef41Sopenharmony_cic2.onmessage = e => events.push(e); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_cifunction testCompletion(t) { 151cb0ef41Sopenharmony_ci return new Promise((resolve) => { 161cb0ef41Sopenharmony_ci c2.addEventListener("message", t.step_func(e => { 171cb0ef41Sopenharmony_ci if (e.data == 'from worker') { 181cb0ef41Sopenharmony_ci c2.postMessage('from c2'); 191cb0ef41Sopenharmony_ci } else if (e.data == 'done') { 201cb0ef41Sopenharmony_ci assert_equals(events.length, 5); 211cb0ef41Sopenharmony_ci assert_equals(events[0].data, 'from worker'); 221cb0ef41Sopenharmony_ci assert_equals(events[0].target, c1); 231cb0ef41Sopenharmony_ci assert_equals(events[1].data, 'from worker'); 241cb0ef41Sopenharmony_ci assert_equals(events[1].target, c2); 251cb0ef41Sopenharmony_ci assert_equals(events[2].data, 'from c2'); 261cb0ef41Sopenharmony_ci assert_equals(events[3].data, 'done'); 271cb0ef41Sopenharmony_ci assert_equals(events[3].target, c1); 281cb0ef41Sopenharmony_ci assert_equals(events[4].data, 'done'); 291cb0ef41Sopenharmony_ci assert_equals(events[4].target, c2); 301cb0ef41Sopenharmony_ci resolve(); 311cb0ef41Sopenharmony_ci } 321cb0ef41Sopenharmony_ci })); 331cb0ef41Sopenharmony_ci }); 341cb0ef41Sopenharmony_ci} 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_cipromise_test(async t => { 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci const testResults = testCompletion(t); 391cb0ef41Sopenharmony_ci const SCRIPT = "resources/service-worker.js"; 401cb0ef41Sopenharmony_ci const SCOPE = "/webmessaging/broadcastchannel/resources/not-used/"; 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci const reg = await navigator.serviceWorker.register(SCRIPT, {'scope': SCOPE}); 431cb0ef41Sopenharmony_ci t.add_cleanup(() => reg.unregister()); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci return testResults; 461cb0ef41Sopenharmony_ci }, 'BroadcastChannel works in service workers'); 471cb0ef41Sopenharmony_ci</script> 48