summaryrefslogtreecommitdiffstats
path: root/testing/mochitest/tests/Harness_sanity/test_SpecialPowersSpawn.js
blob: 06c8dc3c59282b8ec6b24342a5769ac1d5ae8a31 (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
"use strict";

const { XPCShellContentUtils } = ChromeUtils.import(
  "resource://testing-common/XPCShellContentUtils.jsm"
);

XPCShellContentUtils.init(this);

const HTML = String.raw`<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
  <span id="span">Hello there.</span>
</body>
</html>`;

const server = XPCShellContentUtils.createHttpServer({
  hosts: ["example.com"],
});

server.registerPathHandler("/", (request, response) => {
  response.setHeader("Content-Type", "text/html");
  response.write(HTML);
});

add_task(async function () {
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  let page = await XPCShellContentUtils.loadContentPage("http://example.com/", {
    remote: true,
    remoteSubframes: true,
  });

  let { SpecialPowers, browsingContext } = page;

  let result = await SpecialPowers.spawn(
    browsingContext,
    ["#span"],
    selector => {
      let elem = content.document.querySelector(selector);
      return elem.textContent;
    }
  );

  equal(result, "Hello there.", "Got correct element text from frame");

  let line = Components.stack.lineNumber + 1;
  let callback = () => {
    let e = new Error("Hello.");
    return { filename: e.fileName, lineNumber: e.lineNumber };
  };

  let loc = await SpecialPowers.spawn(browsingContext, [], callback);
  equal(
    loc.filename,
    Components.stack.filename,
    "Error should have correct script filename"
  );
  equal(
    loc.lineNumber,
    line + 2,
    "Error should have correct script line number"
  );

  await page.close();
});