11cb0ef41Sopenharmony_ci"use strict" 21cb0ef41Sopenharmony_ci// https://w3c.github.io/hr-time/#time-origin 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ciasync_test(function(test) { 51cb0ef41Sopenharmony_ci // Cache global time before starting worker 61cb0ef41Sopenharmony_ci const globalTimeOrigin = performance.timeOrigin; 71cb0ef41Sopenharmony_ci const globalNowBeforeWorkerStart = performance.now(); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci // Start worker and retrieve time 101cb0ef41Sopenharmony_ci const workerScript = "postMessage({timeOrigin: performance.timeOrigin, now: performance.now()})"; 111cb0ef41Sopenharmony_ci const blob = new Blob([workerScript]); 121cb0ef41Sopenharmony_ci let worker = new Worker(URL.createObjectURL(blob)); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci worker.addEventListener("message", test.step_func_done(function(event) { 151cb0ef41Sopenharmony_ci const workerTimeOrigin = event.data.timeOrigin; 161cb0ef41Sopenharmony_ci const workerNow = event.data.now; 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ci assert_not_equals(workerTimeOrigin, 0, "worker timeOrigin must not be 0"); 191cb0ef41Sopenharmony_ci assert_not_equals(performance.timeOrigin, 0, "Document timeOrigin must not be 0"); 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci assert_equals(globalTimeOrigin, performance.timeOrigin, "timeOrigin should not be changed in same document mode"); 221cb0ef41Sopenharmony_ci assert_less_than(globalTimeOrigin, workerTimeOrigin, "Document timeOrigin must be earlier than worker timeOrigin"); 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci // Document and worker's now() start from their respective timeOrigins. 251cb0ef41Sopenharmony_ci const timeDiff = workerTimeOrigin - globalTimeOrigin; // convert worker's time to Document time. 261cb0ef41Sopenharmony_ci assert_less_than(globalTimeOrigin + globalNowBeforeWorkerStart, globalTimeOrigin + timeDiff + workerNow, "Document old now is earlier than worker now."); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci // Comparing timing between Document and worker threads could be delicate as it relies on the thread implementation and could be subject to race conditions. 291cb0ef41Sopenharmony_ci })); 301cb0ef41Sopenharmony_ci}, 'timeOrigin and now() should be correctly ordered between window and worker'); 31