summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/components/test/chrome/head.js
blob: e8260cb774cb847a44339737e6a7546ef4d7d22f (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* 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 no-unused-vars: [2, {"vars": "local"}] */

/* global _snapshots */

"use strict";

var { require } = ChromeUtils.importESModule(
  "resource://devtools/shared/loader/Loader.sys.mjs"
);
var { Assert } = ChromeUtils.importESModule(
  "resource://testing-common/Assert.sys.mjs"
);
var { gDevTools } = require("resource://devtools/client/framework/devtools.js");
var { BrowserLoader } = ChromeUtils.importESModule(
  "resource://devtools/shared/loader/browser-loader.sys.mjs"
);
var {
  DevToolsServer,
} = require("resource://devtools/server/devtools-server.js");
var {
  DevToolsClient,
} = require("resource://devtools/client/devtools-client.js");
var DevToolsUtils = require("resource://devtools/shared/DevToolsUtils.js");
var { Toolbox } = require("resource://devtools/client/framework/toolbox.js");

var { require: browserRequire } = BrowserLoader({
  baseURI: "resource://devtools/client/shared/",
  window,
});

const React = browserRequire("devtools/client/shared/vendor/react");
const ReactDOM = browserRequire("devtools/client/shared/vendor/react-dom");
const dom = browserRequire("devtools/client/shared/vendor/react-dom-factories");
const TestUtils = browserRequire(
  "devtools/client/shared/vendor/react-dom-test-utils"
);

const ShallowRenderer = browserRequire(
  "devtools/client/shared/vendor/react-test-renderer-shallow"
);
const TestRenderer = browserRequire(
  "devtools/client/shared/vendor/react-test-renderer"
);

var EXAMPLE_URL = "https://example.com/browser/browser/devtools/shared/test/";

SimpleTest.registerCleanupFunction(() => {
  window._snapshots = null;
});

function forceRender(comp) {
  return setState(comp, {}).then(() => setState(comp, {}));
}

// All tests are asynchronous.
SimpleTest.waitForExplicitFinish();

function onNextAnimationFrame(fn) {
  return () => requestAnimationFrame(() => requestAnimationFrame(fn));
}

function setState(component, newState) {
  return new Promise(resolve => {
    component.setState(newState, onNextAnimationFrame(resolve));
  });
}

function dumpn(msg) {
  dump(`SHARED-COMPONENTS-TEST: ${msg}\n`);
}

/**
 * Tree View
 */

const TEST_TREE_VIEW = {
  A: { label: "A", value: "A" },
  B: { label: "B", value: "B" },
  C: { label: "C", value: "C" },
  D: { label: "D", value: "D" },
  E: { label: "E", value: "E" },
  F: { label: "F", value: "F" },
  G: { label: "G", value: "G" },
  H: { label: "H", value: "H" },
  I: { label: "I", value: "I" },
  J: { label: "J", value: "J" },
  K: { label: "K", value: "K" },
  L: { label: "L", value: "L" },
};

TEST_TREE_VIEW.children = {
  A: [TEST_TREE_VIEW.B, TEST_TREE_VIEW.C, TEST_TREE_VIEW.D],
  B: [TEST_TREE_VIEW.E, TEST_TREE_VIEW.F, TEST_TREE_VIEW.G],
  C: [TEST_TREE_VIEW.H, TEST_TREE_VIEW.I],
  D: [TEST_TREE_VIEW.J],
  E: [TEST_TREE_VIEW.K, TEST_TREE_VIEW.L],
  F: [],
  G: [],
  H: [],
  I: [],
  J: [],
  K: [],
  L: [],
};

const TEST_TREE_VIEW_INTERFACE = {
  provider: {
    getChildren: x => TEST_TREE_VIEW.children[x.label],
    hasChildren: x => !!TEST_TREE_VIEW.children[x.label].length,
    getLabel: x => x.label,
    getValue: x => x.value,
    getKey: x => x.label,
    getType: () => "string",
  },
  object: TEST_TREE_VIEW.A,
  columns: [{ id: "default" }, { id: "value" }],
};

/**
 * Tree
 */

var TEST_TREE_INTERFACE = {
  getParent: x => TEST_TREE.parent[x],
  getChildren: x => TEST_TREE.children[x],
  renderItem: (x, depth, focused) =>
    "-".repeat(depth) + x + ":" + focused + "\n",
  getRoots: () => ["A", "M"],
  getKey: x => "key-" + x,
  itemHeight: 1,
  onExpand: x => TEST_TREE.expanded.add(x),
  onCollapse: x => TEST_TREE.expanded.delete(x),
  isExpanded: x => TEST_TREE.expanded.has(x),
};

function isRenderedTree(actual, expectedDescription, msg) {
  const expected = expectedDescription.map(x => x + "\n").join("");
  dumpn(`Expected tree:\n${expected}`);
  dumpn(`Actual tree:\n${actual}`);
  is(actual, expected, msg);
}

function isAccessibleTree(tree, options = {}) {
  const treeNode = tree.refs.tree;
  is(treeNode.getAttribute("tabindex"), "0", "Tab index is set");
  is(treeNode.getAttribute("role"), "tree", "Tree semantics is present");
  if (options.hasActiveDescendant) {
    ok(
      treeNode.hasAttribute("aria-activedescendant"),
      "Tree has an active descendant set"
    );
  }

  const treeNodes = [...treeNode.querySelectorAll(".tree-node")];
  for (const node of treeNodes) {
    ok(node.id, "TreeNode has an id");
    is(node.getAttribute("role"), "treeitem", "Tree item semantics is present");
    is(
      parseInt(node.getAttribute("aria-level"), 10),
      parseInt(node.getAttribute("data-depth"), 10) + 1,
      "Aria level attribute is set correctly"
    );
  }
}

// Encoding of the following tree/forest:
//
// A
// |-- B
// |   |-- E
// |   |   |-- K
// |   |   `-- L
// |   |-- F
// |   `-- G
// |-- C
// |   |-- H
// |   `-- I
// `-- D
//     `-- J
// M
// `-- N
//     `-- O
var TEST_TREE = {
  children: {
    A: ["B", "C", "D"],
    B: ["E", "F", "G"],
    C: ["H", "I"],
    D: ["J"],
    E: ["K", "L"],
    F: [],
    G: [],
    H: [],
    I: [],
    J: [],
    K: [],
    L: [],
    M: ["N"],
    N: ["O"],
    O: [],
  },
  parent: {
    A: null,
    B: "A",
    C: "A",
    D: "A",
    E: "B",
    F: "B",
    G: "B",
    H: "C",
    I: "C",
    J: "D",
    K: "E",
    L: "E",
    M: null,
    N: "M",
    O: "N",
  },
  expanded: new Set(),
};

/**
 * Frame
 */
function checkFrameString({
  el,
  file,
  line,
  column,
  source,
  functionName,
  shouldLink,
  tooltip,
  locationPrefix,
}) {
  const $ = selector => el.querySelector(selector);

  const $func = $(".frame-link-function-display-name");
  const $source = $(".frame-link-source");
  const $locationPrefix = $(".frame-link-prefix");
  const $filename = $(".frame-link-filename");
  const $line = $(".frame-link-line");

  is($filename.textContent, file, "Correct filename");
  is(
    el.getAttribute("data-line"),
    line ? `${line}` : null,
    "Expected `data-line` found"
  );
  is(
    el.getAttribute("data-column"),
    column ? `${column}` : null,
    "Expected `data-column` found"
  );
  is($source.getAttribute("title"), tooltip, "Correct tooltip");
  is($source.tagName, shouldLink ? "A" : "SPAN", "Correct linkable status");
  if (shouldLink) {
    is($source.getAttribute("href"), source, "Correct source");
  }

  if (line != null) {
    let lineText = `:${line}`;
    if (column != null) {
      lineText += `:${column}`;
    }

    is($line.textContent, lineText, "Correct line number");
  } else {
    ok(!$line, "Should not have an element for `line`");
  }

  if (functionName != null) {
    is($func.textContent, functionName, "Correct function name");
  } else {
    ok(!$func, "Should not have an element for `functionName`");
  }

  if (locationPrefix != null) {
    is($locationPrefix.textContent, locationPrefix, "Correct prefix");
  } else {
    ok(!$locationPrefix, "Should not have an element for `locationPrefix`");
  }
}

function checkSmartFrameString({ el, location, functionName, tooltip }) {
  const $ = selector => el.querySelector(selector);

  const $func = $(".title");
  const $location = $(".location");

  is($location.textContent, location, "Correct filename");
  is(el.getAttribute("title"), tooltip, "Correct tooltip");
  if (functionName != null) {
    is($func.textContent, functionName, "Correct function name");
  } else {
    ok(!$func, "Should not have an element for `functionName`");
  }
}

function renderComponent(component, props) {
  const el = React.createElement(component, props, {});
  // By default, renderIntoDocument() won't work for stateless components, but
  // it will work if the stateless component is wrapped in a stateful one.
  // See https://github.com/facebook/react/issues/4839
  const wrappedEl = dom.span({}, [el]);
  const renderedComponent = TestUtils.renderIntoDocument(wrappedEl);
  return ReactDOM.findDOMNode(renderedComponent).children[0];
}

function shallowRenderComponent(component, props) {
  const el = React.createElement(component, props);
  const renderer = new ShallowRenderer();
  renderer.render(el, {});
  return renderer.getRenderOutput();
}

/**
 * Creates a React Component for testing
 *
 * @param {string} factory - factory object of the component to be created
 * @param {object} props - React props for the component
 * @returns {object} - container Node, Object with React component
 * and querySelector function with $ as name.
 */
async function createComponentTest(factory, props) {
  const container = document.createElement("div");
  document.body.appendChild(container);

  const component = ReactDOM.render(factory(props), container);
  await forceRender(component);

  return {
    container,
    component,
    $: s => container.querySelector(s),
  };
}

async function waitFor(condition = () => true, delay = 50) {
  do {
    const res = condition();
    if (res) {
      return res;
    }
    await new Promise(resolve => setTimeout(resolve, delay));
  } while (true);
}

/**
 * Matches a component tree rendererd using TestRenderer to a given expected JSON
 * snapshot.
 * @param  {String} name
 *         Name of the function derived from a test [step] name.
 * @param  {Object} el
 *         React element to be rendered using TestRenderer.
 */
function matchSnapshot(name, el) {
  if (!_snapshots) {
    is(false, "No snapshots were loaded into test.");
  }

  const snapshot = _snapshots[name];
  if (snapshot === undefined) {
    is(false, `Snapshot for "${name}" not found.`);
  }

  const renderer = TestRenderer.create(el, {});
  const tree = renderer.toJSON();

  is(
    JSON.stringify(tree, (key, value) =>
      typeof value === "function" ? value.toString() : value
    ),
    JSON.stringify(snapshot),
    name
  );
}