summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_webconsole_object_inspector_array_getters.js
blob: 40081ef3099692ec97a0bbd9dd492de1b1f58656 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Check evaluating and expanding getters on an array in the console.
const TEST_URI =
  "data:text/html;charset=utf8,<!DOCTYPE html><h1>Object Inspector on Getters on an Array</h1>";

add_task(async function () {
  const hud = await openNewTabAndConsole(TEST_URI);

  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    const array = [];
    Object.defineProperty(array, 0, {
      get: () => "elem0",
    });
    Object.defineProperty(array, 1, {
      set: () => {},
    });
    Object.defineProperty(array, 2, {
      get: () => "elem2",
      set: () => {},
    });
    content.wrappedJSObject.console.log("oi-array-test", array);
  });

  const node = await waitFor(() => findConsoleAPIMessage(hud, "oi-array-test"));
  const oi = node.querySelector(".tree");

  const arrayText = oi.querySelector(".objectBox-array");

  is(
    arrayText.textContent,
    "Array(3) [ Getter, Setter, Getter & Setter ]",
    "Elements with getter/setter should be shown correctly"
  );

  expandObjectInspectorNode(oi);
  await waitFor(() => getObjectInspectorNodes(oi).length > 1);

  await testGetter(oi, "0");
  await testSetterOnly(oi, "1");
  await testGetter(oi, "2");
});

async function testGetter(oi, index) {
  let node = findObjectInspectorNode(oi, index);
  is(
    isObjectInspectorNodeExpandable(node),
    false,
    `The ${index} node can't be expanded`
  );
  const invokeButton = getObjectInspectorInvokeGetterButton(node);
  ok(invokeButton, `There is an invoke button for ${index} as expected`);

  invokeButton.click();
  await waitFor(
    () =>
      !getObjectInspectorInvokeGetterButton(findObjectInspectorNode(oi, index))
  );

  node = findObjectInspectorNode(oi, index);
  ok(
    node.textContent.includes(`${index}: "elem${index}"`),
    "Element ${index} now has the expected text content"
  );
  is(
    isObjectInspectorNodeExpandable(node),
    false,
    `The ${index} node can't be expanded`
  );
}

async function testSetterOnly(oi, index) {
  const node = findObjectInspectorNode(oi, index);
  is(
    isObjectInspectorNodeExpandable(node),
    false,
    `The ${index} node can't be expanded`
  );
  const invokeButton = getObjectInspectorInvokeGetterButton(node);
  ok(!invokeButton, `There is no invoke button for ${index}`);

  ok(
    node.textContent.includes(`${index}: Setter`),
    "Element ${index} now has the expected text content"
  );
  is(
    isObjectInspectorNodeExpandable(node),
    false,
    `The ${index} node can't be expanded`
  );
}