11cb0ef41Sopenharmony_ciconst syncDelay = ms => {
21cb0ef41Sopenharmony_ci  const start = performance.now();
31cb0ef41Sopenharmony_ci  let elapsedTime;
41cb0ef41Sopenharmony_ci  do {
51cb0ef41Sopenharmony_ci    elapsedTime = performance.now() - start;
61cb0ef41Sopenharmony_ci  } while (elapsedTime < ms);
71cb0ef41Sopenharmony_ci};
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst markTime = (docName, lifecycleEventName) => {
101cb0ef41Sopenharmony_ci  // Calculating these values before the below `mark` invocation ensures that delays in
111cb0ef41Sopenharmony_ci  // reaching across to the other window object doesn't interfere with the correctness
121cb0ef41Sopenharmony_ci  // of the test.
131cb0ef41Sopenharmony_ci  const dateNow = Date.now();
141cb0ef41Sopenharmony_ci  const performanceNow = performance.now();
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  window.opener.mark({
171cb0ef41Sopenharmony_ci    docName,
181cb0ef41Sopenharmony_ci    lifecycleEventName,
191cb0ef41Sopenharmony_ci    performanceNow: performanceNow,
201cb0ef41Sopenharmony_ci    dateNow: dateNow
211cb0ef41Sopenharmony_ci  });
221cb0ef41Sopenharmony_ci};
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciconst setupUnloadPrompt = (docName, msg) => {
251cb0ef41Sopenharmony_ci  window.addEventListener("beforeunload", ev => {
261cb0ef41Sopenharmony_ci    markTime(docName, "beforeunload");
271cb0ef41Sopenharmony_ci    return ev.returnValue = msg || "Click OK to continue test."
281cb0ef41Sopenharmony_ci  });
291cb0ef41Sopenharmony_ci};
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciconst setupListeners = (docName, nextDocument) => {
321cb0ef41Sopenharmony_ci  window.addEventListener("load", () => {
331cb0ef41Sopenharmony_ci    markTime(docName, "load");
341cb0ef41Sopenharmony_ci    document.getElementById("proceed").addEventListener("click", ev => {
351cb0ef41Sopenharmony_ci      ev.preventDefault();
361cb0ef41Sopenharmony_ci      if (nextDocument) {
371cb0ef41Sopenharmony_ci        document.location = nextDocument;
381cb0ef41Sopenharmony_ci      } else {
391cb0ef41Sopenharmony_ci        window.close();
401cb0ef41Sopenharmony_ci      }
411cb0ef41Sopenharmony_ci    })
421cb0ef41Sopenharmony_ci  });
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  setupUnloadPrompt(docName);
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  window.addEventListener("unload", () => {
471cb0ef41Sopenharmony_ci    markTime(docName, "unload");
481cb0ef41Sopenharmony_ci    if (docName !== "c") { syncDelay(1000); }
491cb0ef41Sopenharmony_ci  });
501cb0ef41Sopenharmony_ci};
511cb0ef41Sopenharmony_ci
52