summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/general/performance_timeline_main_test.html
blob: f8094998eb8f08d7ffedea863bd9e594ddbb4d99 (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
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
	<link rel="stylesheet" href="/tests/SimpleTest/test.css?performance-timeline-main-test"/>
	<script type="application/javascript">

function ok(cond, message) {
  window.opener.ok(cond, message)
}

function is(received, expected, message) {
  window.opener.is(received, expected, message);
}

function isnot(received, notExpected, message) {
  window.opener.isnot(received, notExpected, message);
}

var receivedBufferFullEvents = 0;
window.performance.onresourcetimingbufferfull = () => {
  receivedBufferFullEvents++;
}

window.onload = () => {
  // Here, we should have 4 entries (1 css, 3 png) since the image was loaded.
  var nEntries = window.performance.getEntries().length;
  ok(nEntries >= 4, "Performance.getEntries() returned wrong number of entries.");

  window.performance.setResourceTimingBufferSize(5);
  window.performance.mark("test-start");
  window.performance.mark("test-end");

  // The URI should be the address of a resource will be loaded later to be used on getEntriesByName.
  window.performance.measure("http://mochi.test:8888/tests/dom/tests/mochitest/general/test-data2.json",
                             "test-start", "test-end");

  is(window.performance.getEntries().length, nEntries + 3, "User Timing APIs should never be affected by setResourceTimingBufferSize.");
  is(window.performance.getEntriesByType("resource").length, 4, "The number of PerformanceResourceTiming should be 4.");
  is(window.performance.getEntriesByType("mark").length, 2, "The number of PerformanceMark entries should be 2.");
  is(window.performance.getEntriesByType("measure").length, 1, "The number of PerformanceMeasure entries should be 1.");

  is(receivedBufferFullEvents, 0, "onresourcetimingbufferfull should never be called.");

  makeXhr("test-data2.json", firstCheck);
}

function makeXhr(aUrl, aCallback) {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onload = aCallback;
  xmlhttp.open("get", aUrl, true);
  xmlhttp.send();
}

function firstCheck() {
  SpecialPowers.executeSoon(() => {
    is(window.performance.getEntriesByType("resource").length, 5,
      "The number of PerformanceResourceTiming should be 5.");
    is(receivedBufferFullEvents, 0,
      "onresourcetimingbufferfull should not have been called yet.");
    makeXhr("test-data2.json", secondCheck);
	}, window);
}

function secondCheck() {
  SpecialPowers.executeSoon(() => {
    is(window.performance.getEntriesByType("resource").length, 5, "The number of PerformanceResourceTiming should be 5.");
    is(receivedBufferFullEvents, 1, "onresourcetimingbufferfull should have been called since the last call.");
    checkOrder(window.performance.getEntries(), "All PerformanceEntry");
    checkOrder(window.performance.getEntriesByType("resource"), "PerformanceResourceTiming");
    checkOrder(window.performance.getEntriesByType("mark"), "PerformanceMark");
    checkOrder(window.performance.getEntriesByType("measure"), "PerformanceMeasure");

    is(window.performance.getEntriesByName("http://mochi.test:8888/tests/dom/tests/mochitest/general/test-data2.json").length, 2, "Both PerformanceMeasure and XMLHttpRequest resource should be included.");
    checkOrder(window.performance.getEntriesByName("http://mochi.test:8888/tests/dom/tests/mochitest/general/test-data2.json"), "Entry with performance.getEntrieByName()");
    window.opener.finishTests();
  }, window);
}

function checkOrder(entries, name) {
  for (var i = 0; i < entries.length - 1; i++) {
    ok(entries[i].startTime <= entries[i + 1].startTime, name + " should be sorted by startTime.");
  }
}

</script>
</head>
<body>
  <a target="_blank"
     href="https://bugzilla.mozilla.org/show_bug.cgi?id=1158731"
     title="Buffer for Performance APIs (Resource Timing, User Timing) should be separeted">
    Bug #1158731 -  Buffer for Performance APIs (Resource Timing, User Timing) should be separeted
  </a>
  <p id="display"></p>
  <div id="content">
    <img src="http://mochi.test:8888/tests/image/test/mochitest/over.png">
    <object data="http://mochi.test:8888/tests/image/test/mochitest/clear.png" type="image/png"></object>
    <embed src="http://mochi.test:8888/tests/image/test/mochitest/green.png" type="image/png"/>
  </div>
</body>
</html>