summaryrefslogtreecommitdiffstats
path: root/dom/events/test/test_eventTimeStamp.html
blob: 5ca5cd47df4ce75bf13d12dfa0a6f9cdeec3cc35 (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
<!doctype html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=77992
-->
<head>
  <meta charset="utf-8">
  <title>Test for Event.timeStamp (Bug 77992)</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank"
  href="https://bugzilla.mozilla.org/show_bug.cgi?id=77992">Mozilla Bug 77992</a>
<p id="display"></p>
<pre id="test">
<script type="text/js-worker" id="worker-src">
  // Simply returns the event timestamp
  onmessage = function(evt) {
    postMessage(evt.timeStamp + performance.timeOrigin);
  }
</script>
<script type="text/js-worker" id="shared-worker-src">
  // Simply returns the event timestamp
  onconnect = function(evt) {
    var port = evt.ports[0];
    port.onmessage = function(messageEvt) {
      port.postMessage(messageEvt.timeStamp + performance.timeOrigin);
    };
  };
</script>
<script type="application/javascript">
"use strict";

SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("untriaged");

testRegularEvents();

// Event.timeStamp should be relative to the time origin which is:
//
//   Non-worker context: navigation start
//   Dedicated worker: navigation start of the document that created the worker
//   Shared worker: creation time of the shared worker
//
// See: https://w3c.github.io/web-performance/specs/HighResolutionTime2/Overview.html#sec-time-origin

function testRegularEvents() {
  if (document.readyState === "complete") {
    ok(false, "Onload event has already fired");
    finishTests();
    return;
  }
  var timeBeforeEvent = performance.now();
  addEventListener("load", function(evt) {
    var timeAfterEvent = performance.now();
    ok(evt.timeStamp >= timeBeforeEvent &&
       evt.timeStamp <= timeAfterEvent,
       "Event timestamp (" + evt.timeStamp + ") is in expected range: (" +
         timeBeforeEvent + ", " + timeAfterEvent + ")");
    testWorkerEvents();
  });
}

function testWorkerEvents() {
  var blob = new Blob([ document.getElementById("worker-src").textContent ],
                      { type: "text/javascript" });
  var worker = new Worker(URL.createObjectURL(blob));
  worker.onmessage = function(evt) {
    var timeAfterEvent = performance.now() + performance.timeOrigin;
    ok(evt.data >= timeBeforeEvent &&
       evt.data <= timeAfterEvent,
    //   "Event timestamp in dedicated worker (" + evt.data +
    //     ") is in expected range: (" +
    //     timeBeforeEvent + ", " + timeAfterEvent + ")");
    worker.terminate();
    testSharedWorkerEvents();
  };
  var timeBeforeEvent = performance.now() + performance.timeOrigin;
  worker.postMessage("");
}

function testSharedWorkerEvents() {
  var blob =
    new Blob([ document.getElementById("shared-worker-src").textContent ],
             { type: "text/javascript" });
  // Delay creation of worker slightly so it is easier to distinguish
  // shared worker creation time from this document's navigation start
  setTimeout(function() {
  var timeBeforeEvent = performance.now() + performance.timeOrigin;
    var worker = new SharedWorker(URL.createObjectURL(blob));
    worker.port.onmessage = function(evt) {
      var timeAfterEvent = performance.now() + performance.timeOrigin;
      ok(evt.data >= timeBeforeEvent &&
         evt.data <= timeAfterEvent,
      //   "Event timestamp in shared worker (" + evt.data +
      //     ") is in expected range: (0, " +
      //     timeBeforeEvent + ", " + timeAfterEvent + ")");
      worker.port.close();
      finishTests();
    };
    worker.port.start();
    worker.port.postMessage("");
  }, 500);
}

var finishTests = function() {
  SpecialPowers.setBoolPref(kPrefName, prevPrefValue);
  SimpleTest.finish();
};

</script>
</pre>
</body>
</html>