summaryrefslogtreecommitdiffstats
path: root/devtools/client/performance/test/helpers/wait-utils.js
blob: be654b7d8c48b810a534bc3045971f77cfe67bd6 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

/* globals dump */

const { CC } = require("chrome");
const { Task } = require("devtools/shared/task");
const DevToolsUtils = require("devtools/shared/DevToolsUtils");
const { once, observeOnce } = require("devtools/client/performance/test/helpers/event-utils");

/**
 * Blocks the main thread for the specified amount of time.
 */
exports.busyWait = function (time) {
  dump(`Busy waiting for: ${time} milliseconds.\n`);
  let start = Date.now();
  /* eslint-disable no-unused-vars */
  let stack;
  while (Date.now() - start < time) {
    stack = CC.stack;
  }
  /* eslint-enable no-unused-vars */
};

/**
 * Idly waits for the specified amount of time.
 */
exports.idleWait = function (time) {
  dump(`Idly waiting for: ${time} milliseconds.\n`);
  return DevToolsUtils.waitForTime(time);
};

/**
 * Waits until a predicate returns true.
 */
exports.waitUntil = function* (predicate, interval = 100, tries = 100) {
  for (let i = 1; i <= tries; i++) {
    if (yield Task.spawn(predicate)) {
      dump(`Predicate returned true after ${i} tries.\n`);
      return;
    }
    yield exports.idleWait(interval);
  }
  throw new Error(`Predicate returned false after ${tries} tries, aborting.\n`);
};

/**
 * Waits for a `MozAfterPaint` event to be fired on the specified window.
 */
exports.waitForMozAfterPaint = function (window) {
  return once(window, "MozAfterPaint");
};

/**
 * Waits for the `browser-delayed-startup-finished` observer notification
 * to be fired on the specified window.
 */
exports.waitForDelayedStartupFinished = function (window) {
  return observeOnce("browser-delayed-startup-finished", { expectedSubject: window });
};