summaryrefslogtreecommitdiffstats
path: root/remote/shared/test/xpcshell/test_Stack.js
blob: c41c5f0240fb92470d5b726234de886f2d55a5a5 (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
/* 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/. */

const { getFramesFromStack, isChromeFrame } = ChromeUtils.importESModule(
  "chrome://remote/content/shared/Stack.sys.mjs"
);

const sourceFrames = [
  {
    column: 1,
    functionDisplayName: "foo",
    line: 2,
    source: "cheese",
    sourceId: 1,
  },
  {
    column: 3,
    functionDisplayName: null,
    line: 4,
    source: "cake",
    sourceId: 2,
  },
  {
    column: 5,
    functionDisplayName: "chrome",
    line: 6,
    source: "chrome://foo",
    sourceId: 3,
  },
];

const targetFrames = [
  {
    columnNumber: 1,
    functionName: "foo",
    lineNumber: 2,
    filename: "cheese",
    sourceId: 1,
  },
  {
    columnNumber: 3,
    functionName: "",
    lineNumber: 4,
    filename: "cake",
    sourceId: 2,
  },
  {
    columnNumber: 5,
    functionName: "chrome",
    lineNumber: 6,
    filename: "chrome://foo",
    sourceId: 3,
  },
];

add_task(async function test_getFramesFromStack() {
  const stack = buildStack(sourceFrames);
  const frames = getFramesFromStack(stack, { includeChrome: false });

  ok(Array.isArray(frames), "frames is of expected type Array");
  equal(frames.length, 3, "Got expected amount of frames");
  checkFrame(frames.at(0), targetFrames.at(0));
  checkFrame(frames.at(1), targetFrames.at(1));
  checkFrame(frames.at(2), targetFrames.at(2));
});

add_task(async function test_getFramesFromStack_asyncStack() {
  const stack = buildStack(sourceFrames, true);
  const frames = getFramesFromStack(stack);

  ok(Array.isArray(frames), "frames is of expected type Array");
  equal(frames.length, 3, "Got expected amount of frames");
  checkFrame(frames.at(0), targetFrames.at(0));
  checkFrame(frames.at(1), targetFrames.at(1));
  checkFrame(frames.at(2), targetFrames.at(2));
});

add_task(async function test_isChromeFrame() {
  for (const filename of ["chrome://foo/bar", "resource://foo/bar"]) {
    ok(isChromeFrame({ filename }), "Frame is of expected chrome scope");
  }

  for (const filename of ["http://foo.bar", "about:blank"]) {
    ok(!isChromeFrame({ filename }), "Frame is of expected content scope");
  }
});

function buildStack(frames, async = false) {
  const parent = async ? "asyncParent" : "parent";

  let currentFrame, stack;
  for (const frame of frames) {
    if (currentFrame) {
      currentFrame[parent] = Object.assign({}, frame);
      currentFrame = currentFrame[parent];
    } else {
      stack = Object.assign({}, frame);
      currentFrame = stack;
    }
  }

  return stack;
}

function checkFrame(frame, expectedFrame) {
  equal(
    frame.columnNumber,
    expectedFrame.columnNumber,
    "Got expected column number"
  );
  equal(
    frame.functionName,
    expectedFrame.functionName,
    "Got expected function name"
  );
  equal(frame.lineNumber, expectedFrame.lineNumber, "Got expected line number");
  equal(frame.filename, expectedFrame.filename, "Got expected filename");
  equal(frame.sourceId, expectedFrame.sourceId, "Got expected source id");
}