tor-browser

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

test_performance_observer.html (4571B)


      1 <!--
      2  Any copyright is dedicated to the Public Domain.
      3  http://creativecommons.org/publicdomain/zero/1.0/
      4 -->
      5 <!DOCTYPE html>
      6 <html>
      7 <head>
      8 <meta charset=utf-8>
      9 <title>Test for performance observer</title>
     10 <script src="/tests/SimpleTest/SimpleTest.js"></script>
     11 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
     12 </head>
     13 <body>
     14 <div id="log"></div>
     15 <script>
     16 SimpleTest.requestFlakyTimeout("For testing when observer callbacks should not be called.");
     17 SimpleTest.waitForExplicitFinish();
     18 
     19 let _tests = [];
     20 
     21 let test = promise_test = fn => {
     22  let cleanups = [];
     23  _tests.push(async () => {
     24    try {
     25      await fn({
     26        add_cleanup: f => { cleanups.push(f); },
     27        step_timeout(f, timeout) {
     28          var test_this = this;
     29          var args = Array.prototype.slice.call(arguments, 2);
     30          return setTimeout(() => {
     31            return f.apply(test_this, args);
     32          }, timeout);
     33        }
     34      });
     35    } catch(e) {
     36      ok(false, `got unexpected exception ${e}`);
     37    }
     38    try {
     39      for (const f of cleanups) {
     40        f();
     41      }
     42      runNextTest();
     43    } catch (e) {
     44      ok(false, `got unexpected exception during cleanup ${e}`);
     45    }
     46  });
     47 }
     48 
     49 function runNextTest() {
     50  if (!_tests.length) {
     51    SimpleTest.finish()
     52    return;
     53  }
     54  _tests.shift()();
     55 }
     56 
     57 function assert_equals(actual, expected, description) {
     58  ok(typeof actual == typeof expected,
     59     `${description} expected (${typeof expected}) ${expected} but got (${typeof actual}) ${actual}`);
     60  ok(Object.is(actual, expected),
     61     `${description} expected ${expected} but got ${actual}`);
     62 }
     63 
     64 function assert_array_equals(actual, expected, description) {
     65  ok(actual.length === expected.length,
     66     `${description} lengths differ, expected ${expected.length} but got ${actual.length}`);
     67  for (var i = 0; i < actual.length; i++) {
     68    ok(actual.hasOwnProperty(i) === expected.hasOwnProperty(i),
     69       `${description} property expected to be ${expected[i]} but got ${actual[i]}`);
     70  }
     71 }
     72 
     73 function assert_throws(expected_exc, func, desc) {
     74  try {
     75    func.call(this);
     76  } catch(e) {
     77    var actual = e.name || e.type;
     78    var expected = expected_exc.name || expected_exc.type;
     79    ok(actual == expected,
     80       `Expected '${expected}', got '${actual}'.`);
     81    return;
     82  }
     83  ok(false, "Expected exception, but none was thrown");
     84 }
     85 
     86 function assert_unreached(description) {
     87  ok(false, `${description} reached unreachable code`);
     88 }
     89 </script>
     90 <script src="test_performance_observer.js"></script>
     91 <script>
     92 function makeXHR(aUrl) {
     93  var xmlhttp = new XMLHttpRequest();
     94  xmlhttp.open("get", aUrl, true);
     95  xmlhttp.send();
     96 }
     97 
     98 let waitForConsole = new Promise(resolve => {
     99  SimpleTest.monitorConsole(resolve, [{
    100    message: /JavaScript Warning: "Ignoring unsupported entryTypes: invalid."/,
    101  }]);
    102 });
    103 
    104 promise_test(t => {
    105  var promise = new Promise(resolve => {
    106    performance.clearResourceTimings();
    107 
    108    var observer = new PerformanceObserver(list => resolve(list));
    109    observer.observe({entryTypes: ['resource']});
    110    t.add_cleanup(() => observer.disconnect());
    111  });
    112 
    113  makeXHR("test-data.json");
    114 
    115  return promise.then(async list => {
    116    assert_equals(list.getEntries().length, 1);
    117    assert_array_equals(list.getEntries(),
    118                        performance.getEntriesByType("resource"),
    119                        "Observed 'resource' entries should equal to entries obtained by getEntriesByType.");
    120 
    121    // getEntries filtering tests
    122    assert_array_equals(list.getEntries({name: "http://mochi.test:8888/tests/dom/base/test/test-data.json"}),
    123                        performance.getEntriesByName("http://mochi.test:8888/tests/dom/base/test/test-data.json"),
    124                        "getEntries with name filter should return correct results.");
    125    assert_array_equals(list.getEntries({entryType: "resource"}),
    126                        performance.getEntriesByType("resource"),
    127                        "getEntries with entryType filter should return correct results.");
    128    assert_array_equals(list.getEntries({initiatorType: "xmlhttprequest"}),
    129                        performance.getEntriesByType("resource"),
    130                        "getEntries with initiatorType filter should return correct results.");
    131    assert_array_equals(list.getEntries({initiatorType: "link"}),
    132                        [],
    133                        "getEntries with non-existent initiatorType filter should return an empty array.");
    134 
    135    SimpleTest.endMonitorConsole();
    136    await waitForConsole;
    137  });
    138 }, "resource-timing test");
    139 
    140 runNextTest();
    141 </script>
    142 </body>