summaryrefslogtreecommitdiffstats
path: root/dom/console/tests/test_timer.html
blob: fa3ca7baec2777ea9b7b9c168a9af5be3db5c5d4 (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for timeStart/timeLog/timeEnd in console</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="head.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
  <script type="application/javascript">

SimpleTest.waitForExplicitFinish();

var reduceTimePrecisionPrevPrefValue = SpecialPowers.getBoolPref("privacy.reduceTimerPrecision");
SpecialPowers.setBoolPref("privacy.reduceTimerPrecision", false);

function ConsoleListener() {
  addConsoleStorageListener(this);
}

ConsoleListener.prototype = {
  observe(aSubject) {
    let obj = aSubject.wrappedJSObject;
    if (obj.arguments[0] != "test_bug1463614") {
      return;
    }

    if (!this._cb || !this._cb(obj)) {
      return;
    }

    this._cb = null;
    this._resolve();
  },

  shutdown() {
    removeConsoleStorageListener(this);
  },

  waitFor(cb) {
    return new Promise(resolve => {
      this._cb = SpecialPowers.wrapCallback(cb);
      this._resolve = resolve;
    });
  },
};

let listener = new ConsoleListener();

// Timer creation:
async function runTest() {
  let cl = listener.waitFor(obj => {
    return ("timer" in obj) &&
           ("name" in obj.timer) &&
           obj.timer.name == "test_bug1463614";
  });

  console.time("test_bug1463614");
  await cl;
  ok(true, "Console.time received!");

  // Timer check:
  cl = listener.waitFor(obj => {
    return ("timer" in obj) &&
           ("name" in obj.timer) &&
           obj.timer.name == "test_bug1463614" &&
           ("duration" in obj.timer) &&
           obj.timer.duration >= 0 &&
           obj.arguments[1] == 1 &&
           obj.arguments[2] == 2 &&
           obj.arguments[3] == 3 &&
           obj.arguments[4] == 4;
  });
  console.timeLog("test_bug1463614", 1, 2, 3, 4);
  await cl;
  ok(true, "Console.timeLog received!");

  // Time deleted:
  cl = listener.waitFor(obj => {
    return ("timer" in obj) &&
           ("name" in obj.timer) &&
           obj.timer.name == "test_bug1463614" &&
           ("duration" in obj.timer) &&
           obj.timer.duration >= 0;
  });
  console.timeEnd("test_bug1463614");
  await cl;
  ok(true, "Console.timeEnd received!");

  // Here an error:
  cl = listener.waitFor(obj => {
    return ("timer" in obj) &&
           ("name" in obj.timer) &&
           obj.timer.name == "test_bug1463614" &&
           ("error" in obj.timer);
  });
  console.timeLog("test_bug1463614");
  await cl;
  ok(true, "Console.time with error received!");
}

runTest().then(() => {
  listener.shutdown();

  SpecialPowers.setBoolPref("privacy.reduceTimerPrecision", reduceTimePrecisionPrevPrefValue);
  SimpleTest.finish();
});

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