summaryrefslogtreecommitdiffstats
path: root/dom/performance/tests/test_performance_observer.html
blob: 86c780c56ca009b3ef9729c35e299f36aadbf9af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>Test for performance observer</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<div id="log"></div>
<script>
SimpleTest.requestFlakyTimeout("For testing when observer callbacks should not be called.");
SimpleTest.waitForExplicitFinish();

let _tests = [];

let test = promise_test = fn => {
  let cleanups = [];
  _tests.push(async () => {
    try {
      await fn({
        add_cleanup: f => { cleanups.push(f); },
        step_timeout(f, timeout) {
          var test_this = this;
          var args = Array.prototype.slice.call(arguments, 2);
          return setTimeout(() => {
            return f.apply(test_this, args);
          }, timeout);
        }
      });
    } catch(e) {
      ok(false, `got unexpected exception ${e}`);
    }
    try {
      for (const f of cleanups) {
        f();
      }
      runNextTest();
    } catch (e) {
      ok(false, `got unexpected exception during cleanup ${e}`);
    }
  });
}

function runNextTest() {
  if (!_tests.length) {
    SimpleTest.finish()
    return;
  }
  _tests.shift()();
}

function assert_equals(actual, expected, description) {
  ok(typeof actual == typeof expected,
     `${description} expected (${typeof expected}) ${expected} but got (${typeof actual}) ${actual}`);
  ok(Object.is(actual, expected),
     `${description} expected ${expected} but got ${actual}`);
}

function assert_array_equals(actual, expected, description) {
  ok(actual.length === expected.length,
     `${description} lengths differ, expected ${expected.length} but got ${actual.length}`);
  for (var i = 0; i < actual.length; i++) {
    ok(actual.hasOwnProperty(i) === expected.hasOwnProperty(i),
       `${description} property expected to be ${expected[i]} but got ${actual[i]}`);
  }
}

function assert_throws(expected_exc, func, desc) {
  try {
    func.call(this);
  } catch(e) {
    var actual = e.name || e.type;
    var expected = expected_exc.name || expected_exc.type;
    ok(actual == expected,
       `Expected '${expected}', got '${actual}'.`);
    return;
  }
  ok(false, "Expected exception, but none was thrown");
}

function assert_unreached(description) {
  ok(false, `${description} reached unreachable code`);
}
</script>
<script src="test_performance_observer.js"></script>
<script>
function makeXHR(aUrl) {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.open("get", aUrl, true);
  xmlhttp.send();
}

let waitForConsole = new Promise(resolve => {
  SimpleTest.monitorConsole(resolve, [{
    message: /JavaScript Warning: "Ignoring unsupported entryTypes: invalid."/,
  }]);
});

promise_test(t => {
  var promise = new Promise(resolve => {
    performance.clearResourceTimings();

    var observer = new PerformanceObserver(list => resolve(list));
    observer.observe({entryTypes: ['resource']});
    t.add_cleanup(() => observer.disconnect());
  });

  makeXHR("test-data.json");

  return promise.then(async list => {
    assert_equals(list.getEntries().length, 1);
    assert_array_equals(list.getEntries(),
                        performance.getEntriesByType("resource"),
                        "Observed 'resource' entries should equal to entries obtained by getEntriesByType.");

    // getEntries filtering tests
    assert_array_equals(list.getEntries({name: "http://mochi.test:8888/tests/dom/base/test/test-data.json"}),
                        performance.getEntriesByName("http://mochi.test:8888/tests/dom/base/test/test-data.json"),
                        "getEntries with name filter should return correct results.");
    assert_array_equals(list.getEntries({entryType: "resource"}),
                        performance.getEntriesByType("resource"),
                        "getEntries with entryType filter should return correct results.");
    assert_array_equals(list.getEntries({initiatorType: "xmlhttprequest"}),
                        performance.getEntriesByType("resource"),
                        "getEntries with initiatorType filter should return correct results.");
    assert_array_equals(list.getEntries({initiatorType: "link"}),
                        [],
                        "getEntries with non-existent initiatorType filter should return an empty array.");

    SimpleTest.endMonitorConsole();
    await waitForConsole;
  });
}, "resource-timing test");

runNextTest();
</script>
</body>