summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/mochitest/test_framerate_01.html
blob: 0282d50a267b734eb2a143abd3eb4e0984b8d7ac (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
<!DOCTYPE HTML>
<html>
<!--
Bug 1007200 - Create a framerate actor
-->
<head>
  <meta charset="utf-8">
  <title>Framerate actor test</title>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
</head>
<body>
<pre id="test">
<script>

window.onload = function() {
  var Cu = Components.utils;
  var Cc = Components.classes;
  var Ci = Components.interfaces;

  var { require } = Cu.import("resource://devtools/shared/Loader.jsm", {});
  var Services = require("Services");
  var { DebuggerClient } = require("devtools/shared/client/main");
  var { DebuggerServer } = require("devtools/server/main");

  // Always log packets when running tests.
  Services.prefs.setBoolPref("devtools.debugger.log", true);
  SimpleTest.registerCleanupFunction(function() {
    Services.prefs.clearUserPref("devtools.debugger.log");
  });

  SimpleTest.waitForExplicitFinish();

  var {FramerateFront} = require("devtools/shared/fronts/framerate");

  function plotFPS(ticks, interval = 100, clamp = 60) {
    var timeline = [];
    var totalTicks = ticks.length;

    // If the refresh driver didn't get a chance to tick before the
    // recording was stopped, assume framerate was 0.
    if (totalTicks == 0) {
      timeline.push({ delta: 0, value: 0 });
      timeline.push({ delta: interval, value: 0 });
      return timeline;
    }

    var frameCount = 0;
    var prevTime = ticks[0];

    for (var i = 1; i < totalTicks; i++) {
      var currTime = ticks[i];
      frameCount++;

      var elapsedTime = currTime - prevTime;
      if (elapsedTime < interval) {
        continue;
      }

      var framerate = Math.min(1000 / (elapsedTime / frameCount), clamp);
      timeline.push({ delta: prevTime, value: framerate });
      timeline.push({ delta: currTime, value: framerate });

      frameCount = 0;
      prevTime = currTime;
    }

    return timeline;
  };

  if (!DebuggerServer.initialized) {
    DebuggerServer.init();
    DebuggerServer.addBrowserActors();
  }

  var client = new DebuggerClient(DebuggerServer.connectPipe());
  client.connect().then(function onConnect() {
    client.listTabs(function onListTabs(aResponse) {
      var form = aResponse.tabs[aResponse.selected];
      var front = FramerateFront(client, form);

      window.setTimeout(() => {
        front.startRecording().then(() => {
          window.setTimeout(() => {
            front.stopRecording().then(rawData => {
              onRecordingStopped(front, rawData);
            });
          }, 1000);
        });
      }, 1000);
    });
  });

  function onRecordingStopped(front, rawData) {
    ok(rawData, "There should be a recording available.");

    var timeline = plotFPS(rawData);
    ok(timeline.length >= 2,
      "There should be at least one measurement available, with two entries.");

    var prevTimeStart = timeline[0].delta;

    for (var i = 0; i < timeline.length; i += 2) {
      var currTimeStart = timeline[i].delta;
      var currTimeEnd = timeline[i + 1].delta;
      info("Testing delta: " + currTimeStart + " vs. " + currTimeEnd);

      ok(currTimeStart < currTimeEnd,
        "The start and end time deltas should be consecutive.");
      is(currTimeStart, prevTimeStart,
        "There should be two time deltas for each framerate value.");

      prevTimeStart = currTimeEnd;
    }

    var prevFramerateValue = -1;

    for (var i = 0; i < timeline.length; i += 2) {
      var currFramerateStart = timeline[i].value;
      var currFramerateEnd = timeline[i + 1].value;
      info("Testing framerate: " + currFramerateStart);

      is(currFramerateStart, currFramerateEnd,
        "The start and end framerate values should be equal.");

      is(typeof currFramerateStart, "number", "All values should be numbers.");
      ok(currFramerateStart <= 60, "All values were correctly clamped.")

      prevFramerateValue = currFramerateStart;
    }

    client.close().then(() => {
      DebuggerServer.destroy();
      SimpleTest.finish()
    });
  }
}
</script>
</pre>
</body>
</html>