summaryrefslogtreecommitdiffstats
path: root/testing/mochitest/BrowserTestUtils/content/content-task.js
blob: ea2d530c5088446ea7194771b2c37c21a17a7a2b (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* eslint-env mozilla/frame-script */

"use strict";

ChromeUtils.import("resource://testing-common/Task.jsm", this);
ChromeUtils.import("resource://testing-common/ContentTaskUtils.jsm", this);
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const AssertCls = ChromeUtils.import(
  "resource://testing-common/Assert.jsm",
  null
).Assert;

// Injects EventUtils into ContentTask scope. To avoid leaks, this does not hold on
// to the window global. This means you **need** to pass the window as an argument to
// the individual EventUtils functions.
// See SimpleTest/EventUtils.js for documentation.
var EventUtils = {};

EventUtils.window = {};
EventUtils.parent = EventUtils.window;
EventUtils._EU_Ci = Ci;
EventUtils._EU_Cc = Cc;
EventUtils.KeyboardEvent = content.KeyboardEvent;
EventUtils.navigator = content.navigator;

EventUtils.synthesizeClick = element =>
  new Promise(resolve => {
    element.addEventListener(
      "click",
      function() {
        resolve();
      },
      { once: true }
    );

    EventUtils.synthesizeMouseAtCenter(
      element,
      { type: "mousedown", isSynthesized: false },
      content
    );
    EventUtils.synthesizeMouseAtCenter(
      element,
      { type: "mouseup", isSynthesized: false },
      content
    );
  });

try {
  Services.scriptloader.loadSubScript(
    "chrome://mochikit/content/tests/SimpleTest/EventUtils.js",
    EventUtils
  );
} catch (e) {
  // There are some xpcshell tests which may use ContentTask.
  // Just ignore if loading EventUtils fails. Tests that need it
  // will fail anyway.
  EventUtils = null;
}

addMessageListener("content-task:spawn", function(msg) {
  let id = msg.data.id;
  let source = msg.data.runnable || "()=>{}";

  function getStack(aStack) {
    let frames = [];
    for (let frame = aStack; frame; frame = frame.caller) {
      frames.push(frame.filename + ":" + frame.name + ":" + frame.lineNumber);
    }
    return frames.join("\n");
  }

  var Assert = new AssertCls((err, message, stack) => {
    sendAsyncMessage("content-task:test-result", {
      id,
      condition: !err,
      name: err ? err.message : message,
      stack: getStack(err ? err.stack : stack),
    });
  });

  /* eslint-disable no-unused-vars */
  var ok = Assert.ok.bind(Assert);
  var is = Assert.equal.bind(Assert);
  var isnot = Assert.notEqual.bind(Assert);

  function todo(expr, name) {
    sendAsyncMessage("content-task:test-todo", { id, expr, name });
  }

  function todo_is(a, b, name) {
    sendAsyncMessage("content-task:test-todo_is", { id, a, b, name });
  }

  function info(name) {
    sendAsyncMessage("content-task:test-info", { id, name });
  }
  /* eslint-enable no-unused-vars */

  try {
    let runnablestr = `
      (() => {
        return (${source});
      })();`;

    // eslint-disable-next-line no-eval
    let runnable = eval(runnablestr);
    let iterator = runnable.call(this, msg.data.arg);
    Task.spawn(iterator).then(
      val => {
        sendAsyncMessage("content-task:complete", {
          id,
          result: val,
        });
      },
      e => {
        sendAsyncMessage("content-task:complete", {
          id,
          error: e.toString(),
        });
      }
    );
  } catch (e) {
    sendAsyncMessage("content-task:complete", {
      id,
      error: e.toString(),
    });
  }
});