summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/performance-timeline/po-observe.html
blob: a48f0f3764bda4ceb88b2a8be4d65f168362627a (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
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>PerformanceObservers: PerformanceObserverInit.buffered</title>
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="performanceobservers.js"></script>
<h1>PerformanceObservers: PerformanceObserverInit.buffered</h1>
<p>
PerformanceObserverInit.buffered should retrieve previously buffered entries
</p>
<div id="log"></div>
<script>
  async_test(function (t) {
    function initTest() {
      new PerformanceObserver(function (entryList, observer) {
        entryList.getEntries().forEach(function(entry) {
          if (shouldExclude(entry)) {
            return;
          }

          observedEntries.push(entry);
          if (observedEntries.length === entryTypes.length) {
            observer.disconnect();
            runTest();
          }
        });
      }).observe({entryTypes});

      // creates a `resource` entry
      var img = document.createElement("img");
      img.src = "./resources/square.png";
      document.body.appendChild(img);

      performance.mark("markName"); // creates a `mark` entry
      performance.measure("measureName"); // creates a `measure` entry
    }
    function shouldExclude(entry) {
      // exclude all `resource` entries that aren't for "square.png"
      return entry.entryType === "resource" &&
             entry.name.indexOf("square.png") === -1;
    }
    function runTest() {
      // this PerformanceObserver is a nop because we've already been notified about all of our `entryTypes`
      var po_nop = new PerformanceObserver(function (entryList, observer) {
        if (entryList.getEntries().find(function(entry) {
          return !shouldExclude(entry);
        })) {
          assert_unreached("this PerformanceObserver callback should never be called");
        }
      });
      po_nop.observe({
        entryTypes,
      });

      // this PerformanceObserver should be notified about the previously
      // buffered mark entry only
      const bufferedEntries = [];
      new PerformanceObserver(function (entryList, observer) {
        entryList.getEntries().forEach(function(entry) {
          if (shouldExclude(entry)) {
            return;
          }

          bufferedEntries.push(entry);
          if (bufferedEntries.length === 1) {
            observer.disconnect();
            po_nop.disconnect();
            for (i = 0; i < bufferedEntries.length; i++) {
              assert_equals(bufferedEntries[i].entryType, "mark")
            }
            t.done();
          }
        });
      }).observe({
        type: "mark",
        buffered: true
      });
    }

    const entryTypes = ["navigation", "resource", "mark", "measure"];
    const observedEntries = [];
    initTest();
  }, "PerformanceObserverInit.buffered should retrieve previously buffered entries");

</script>