tor-browser

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

test_worker_performance_entries.js (2829B)


      1 function ok(a, msg) {
      2  postMessage({ type: "check", status: !!a, msg });
      3 }
      4 
      5 function is(a, b, msg) {
      6  ok(a === b, msg);
      7 }
      8 
      9 function finish(a, msg) {
     10  postMessage({ type: "finish" });
     11 }
     12 
     13 async function wait_for_performance_entries() {
     14  let promise = new Promise(resolve => {
     15    new PerformanceObserver(list => {
     16      resolve(list.getEntries());
     17    }).observe({ entryTypes: ["resource"] });
     18  });
     19  entries = await promise;
     20  return entries;
     21 }
     22 
     23 async function check(resource, initiatorType, protocol) {
     24  let entries = performance.getEntries();
     25  if (!entries.length) {
     26    entries = await wait_for_performance_entries();
     27  }
     28  ok(entries.length == 1, "We have an entry");
     29 
     30  ok(entries[0] instanceof PerformanceEntry, "The entry is a PerformanceEntry");
     31  ok(entries[0].name.endsWith(resource), "The entry has been found!");
     32 
     33  is(entries[0].entryType, "resource", "Correct EntryType");
     34  ok(entries[0].startTime > 0, "We have a startTime");
     35  ok(entries[0].duration > 0, "We have a duration");
     36 
     37  ok(
     38    entries[0] instanceof PerformanceResourceTiming,
     39    "The entry is a PerformanceResourceTiming"
     40  );
     41 
     42  is(entries[0].initiatorType, initiatorType, "Correct initiatorType");
     43  is(entries[0].nextHopProtocol, protocol, "Correct protocol");
     44 
     45  performance.clearResourceTimings();
     46 }
     47 
     48 function simple_checks() {
     49  ok("performance" in self, "We have self.performance");
     50  performance.clearResourceTimings();
     51  next();
     52 }
     53 
     54 function fetch_request() {
     55  fetch("test_worker_performance_entries.sjs")
     56    .then(r => r.blob())
     57    .then(blob => {
     58      check("test_worker_performance_entries.sjs", "fetch", "http/1.1");
     59    })
     60    .then(next);
     61 }
     62 
     63 function xhr_request() {
     64  let xhr = new XMLHttpRequest();
     65  xhr.open("GET", "test_worker_performance_entries.sjs");
     66  xhr.send();
     67  xhr.onload = () => {
     68    check("test_worker_performance_entries.sjs", "xmlhttprequest", "http/1.1");
     69    next();
     70  };
     71 }
     72 
     73 function sync_xhr_request() {
     74  let xhr = new XMLHttpRequest();
     75  xhr.open("GET", "test_worker_performance_entries.sjs", false);
     76  xhr.send();
     77  check("test_worker_performance_entries.sjs", "xmlhttprequest", "http/1.1");
     78  next();
     79 }
     80 
     81 function import_script() {
     82  importScripts(["empty.js"]);
     83  check("empty.js", "other", "http/1.1");
     84  next();
     85 }
     86 
     87 function redirect() {
     88  fetch("test_worker_performance_entries.sjs?redirect")
     89    .then(r => r.text())
     90    .then(async text => {
     91      is(text, "Hello world \\o/", "The redirect worked correctly");
     92      await check(
     93        "test_worker_performance_entries.sjs?redirect",
     94        "fetch",
     95        "http/1.1"
     96      );
     97    })
     98    .then(next);
     99 }
    100 
    101 let tests = [
    102  simple_checks,
    103  fetch_request,
    104  xhr_request,
    105  sync_xhr_request,
    106  import_script,
    107  redirect,
    108 ];
    109 
    110 function next() {
    111  if (!tests.length) {
    112    finish();
    113    return;
    114  }
    115 
    116  let test = tests.shift();
    117  test();
    118 }
    119 
    120 next();