tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test-initiator.js (1247B)


      1 const with_timeout_message = async (promise, message, timeout = 1000) => {
      2  return Promise.race([
      3    promise,
      4    new Promise((resolve, reject) => {
      5      step_timeout(() => {
      6        reject(new Error(message));
      7      }, timeout);
      8    }),
      9  ]);
     10 }
     11 
     12 const observe_entry_no_timeout = entry_name => {
     13  const entry = new Promise(resolve => {
     14    new PerformanceObserver((entry_list, observer) => {
     15      for (const entry of entry_list.getEntries()) {
     16        if (entry.name.endsWith(entry_name)) {
     17          resolve(entry);
     18          observer.disconnect();
     19          return;
     20        }
     21      }
     22    }).observe({"type": "resource", "buffered": true});
     23  });
     24  return entry;
     25 };
     26 
     27 // Asserts that, for the given name, there is/will-be a
     28 // PerformanceResourceTiming entry that has the given 'initiatorUrl'. The test
     29 // is labeled according to the given descriptor.
     30 const initiator_url_test = (entry_name, expected_url, descriptor, timeout_msg) => {
     31  promise_test(async () => {
     32    const promise = observe_entry_no_timeout(entry_name);
     33    const entry = await with_timeout_message(promise, timeout_msg, /* timeout = */ 2000);
     34    assert_equals(entry.initiatorUrl, expected_url);
     35  }, `The initiator Url for ${descriptor} must be '${expected_url}'`);
     36 };