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

"use strict";

// Check expanding/collapsing object with symbol properties in the console.
const TEST_URI = "data:text/html;charset=utf8,<!DOCTYPE html>";

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

  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    class MyClass {
      constructor(isParent = true) {
        this.publicProperty = "public property";
        // A public property can start with a # character. Here we're
        // adding a public property that looks like an existing private property
        // to check we do render both.
        this["#privateProperty"] = {
          content: "actually this is a public property",
        };
        this[Symbol()] = "first Symbol";
        this[Symbol()] = "second Symbol";

        if (isParent) {
          this.#privateProperty = new MyClass(false);
        } else {
          this.#privateProperty = null;
        }
      }

      #privateProperty;
      #privateMethod() {
        return Math.random();
      }
      get #privateGetter() {
        return 42;
      }
      getPrivateProperty() {
        return this.#privateProperty;
      }
    }

    content.wrappedJSObject.console.log(
      "private-properties-test",
      new MyClass(true)
    );
  });

  const node = await waitFor(() =>
    findConsoleAPIMessage(hud, "private-properties-test")
  );
  const objectInspectors = [...node.querySelectorAll(".tree")];
  is(
    objectInspectors.length,
    1,
    "There is the expected number of object inspectors"
  );

  const [oi] = objectInspectors;

  info("Expanding the Object");
  const onMapOiMutation = waitForNodeMutation(oi, {
    childList: true,
  });

  oi.querySelector(".arrow").click();
  await onMapOiMutation;

  ok(
    oi.querySelector(".arrow").classList.contains("expanded"),
    "The arrow of the node has the expected class after clicking on it"
  );

  const oiNodes = getObjectInspectorNodes(oi);
  // The object inspector should look like this:
  /*
   * ▼ { … }
   * |  ▶︎ "#privateProperty": Object { content: "actually this is a public property" }
   * |    publicProperty: "public property"
   * |    Symbol(): "first Symbol",
   * |    Symbol(): "second Symbol",
   * |  ▶︎ #privateGetter: undefined
   * |  ▶︎ #privateProperty: Object { publicProperty: "public property", "#privateProperty": { … }, #privateProperty: null, Symbol(): "first Symbol", … }
   * |  ▶︎ <prototype>
   */
  is(oiNodes.length, 8, "There is the expected number of nodes in the tree");

  const [
    publicDisguisedAsPrivateNodeEl,
    publicNodeEl,
    firstSymbolNodeEl,
    secondSymbolNodeEl,
    // FIXME: This shouldn't appear here (See Bug 1759823)
    privateGetterNodeEl,
    privatePropertyNodeEl,
  ] = Array.from(oiNodes).slice(1);

  checkOiNodeText(
    publicDisguisedAsPrivateNodeEl,
    `"#privateProperty": Object { content: "actually this is a public property" }`,
    `"fake" private property has expected text`
  );
  checkOiNodeText(
    publicNodeEl,
    `publicProperty: "public property"`,
    "public property has expected text"
  );
  checkOiNodeText(
    firstSymbolNodeEl,
    `Symbol(): "first Symbol"`,
    "first symbol has expected text"
  );
  checkOiNodeText(
    secondSymbolNodeEl,
    `Symbol(): "second Symbol"`,
    "second symbol has expected text"
  );
  checkOiNodeText(
    privateGetterNodeEl,
    `#privateGetter: undefined`,
    "private getter is rendered (at the wrong place with the wrong content, see Bug 1759823)"
  );
  checkOiNodeText(
    privatePropertyNodeEl,
    `#privateProperty: Object { publicProperty: "public property", "#privateProperty": {…}, #privateGetter: undefined, Symbol(): "first Symbol", … }`,
    "private property is rendered as expected"
  );

  info("Expand public property disguised as private property");
  expandObjectInspectorNode(publicDisguisedAsPrivateNodeEl);
  const publicPropChildren = await waitFor(() => {
    const children = getObjectInspectorChildrenNodes(
      publicDisguisedAsPrivateNodeEl
    );
    if (children.length === 0) {
      return null;
    }
    return children;
  });
  ok(true, "public property was expanded");

  /*
   * ObjectInspector now should look like:
   *
   * ▼ { … }
   * |  ▼ "#privateProperty": { … }
   * |  |    content: "actually this is a public property"
   * |  |  ▶︎ <prototype>
   * |    publicProperty: "public property"
   * |    Symbol(): "first Symbol",
   * |    Symbol(): "second Symbol",
   * |  ▶︎ #privateProperty: Object { publicProperty: "public property", "#privateProperty": { … }, #privateProperty: null, Symbol(): "first Symbol", … }
   * |  ▶︎ <prototype>
   */
  checkOiNodeText(
    publicPropChildren[0],
    `content: "actually this is a public property"`,
    "public property child has expected text"
  );

  info("Expand private property");
  expandObjectInspectorNode(privatePropertyNodeEl);
  const privatePropChildren = await waitFor(() => {
    const children = getObjectInspectorChildrenNodes(privatePropertyNodeEl);
    if (children.length === 0) {
      return null;
    }
    return children;
  });
  ok(true, "private property was expanded");

  /*
   * ObjectInspector now should look like:
   *
   * ▼ { … }
   * |  ▼ "#privateProperty": { … }
   * |  |    content: "actually this is a public property"
   * |  |  ▶︎ <prototype>
   * |    publicProperty: "public property"
   * |    Symbol(): "first Symbol",
   * |    Symbol(): "second Symbol",
   * |  ▶︎ #privateGetter: undefined
   * |  ▼ #privateProperty: { … }
   * |  |  ▶︎ "#privateProperty": Object { content: "actually this is a public property" }
   * |  |    publicProperty: "public property"
   * |  |    Symbol(): "first Symbol"
   * |  |    Symbol(): "second Symbol"
   * |  |  ▶︎ #privateGetter: undefined
   * |  |    #privateProperty: null
   * |  |  ▶︎ <prototype>
   * |  ▶︎ <prototype>
   */
  checkOiNodeText(
    privatePropChildren[0],
    `"#privateProperty": Object { content: "actually this is a public property" }`,
    "child private property has expected text "
  );
  checkOiNodeText(
    privatePropChildren[1],
    `publicProperty: "public property"`,
    "public property of private property object has expected text"
  );
  checkOiNodeText(
    privatePropChildren[2],
    `Symbol(): "first Symbol"`,
    "first symbol of private property object has expected text"
  );
  checkOiNodeText(
    privatePropChildren[3],
    `Symbol(): "second Symbol"`,
    "second symbol of private property object has expected text"
  );
  checkOiNodeText(
    privatePropChildren[4],
    `#privateGetter: undefined`,
    "private getter of private property object is displayed (even though it shouldn't, see Bug 1759823)"
  );
  checkOiNodeText(
    privatePropChildren[5],
    `#privateProperty: null`,
    "private property of private property object has expected text"
  );
  const privatePropertyPrototypeEl = privatePropChildren[6];
  checkOiNodeText(
    privatePropertyPrototypeEl,
    `<prototype>: Object { … }`,
    "prototype of private property object has expected text"
  );

  info("Expand private property prototype");
  expandObjectInspectorNode(privatePropertyPrototypeEl);
  const privatePropertyPrototypeChildren = await waitFor(() => {
    const children = getObjectInspectorChildrenNodes(
      privatePropertyPrototypeEl
    );
    if (children.length === 0) {
      return null;
    }
    return children;
  });
  ok(true, "private property prototype was expanded");

  /*
   * ObjectInspector now should look like:
   *
   * ▼ { … }
   * |  ▼ "#privateProperty": { … }
   * |  |    content: "actually this is a public property"
   * |  |  ▶︎ <prototype>
   * |    publicProperty: "public property"
   * |    Symbol(): "first Symbol",
   * |    Symbol(): "second Symbol",
   * |  ▶︎ #privateGetter: undefined
   * |  ▼ #privateProperty: { … }
   * |  |  ▶︎ "#privateProperty": Object { content: "actually this is a public property" }
   * |  |    publicProperty: "public property"
   * |  |    Symbol(): "first Symbol"
   * |  |    Symbol(): "second Symbol"
   * |  |  ▶︎ #privateGetter: undefined
   * |  |    #privateProperty: null
   * |  |  ▼ <prototype>
   * |  |  |   constructor: function MyClass()
   * |  |  |   getPrivateProperty: function getPrivateProperty()
   * |  ▶︎ <prototype>
   */
  checkOiNodeText(
    privatePropertyPrototypeChildren[0],
    `constructor: function MyClass()`,
    "constructor is displayed as expected"
  );

  checkOiNodeText(
    privatePropertyPrototypeChildren[1],
    `getPrivateProperty: function getPrivateProperty()`,
    "private method is displayed as expected"
  );

  // TODO: #privateMethod should be displayed (See Bug 1759826)
  // checkOiNodeText(
  //   privatePropertyPrototypeChildren[2],
  //   `#privateMethod: function #privateMethod()`,
  //   "private method is displayed as expected"
  // );

  // TODO: #privateGetter should be displayed here
  // checkOiNodeText(
  //   privatePropertyPrototypeChildren[3],
  //   `#privateGetter: ">>"`,
  //   "private getter value is displayed as expected"
  // );
  // checkOiNodeText(
  //   privatePropertyPrototypeChildren[4],
  //   `<get #privateGetter>: "function"`,
  //   "private getter function is displayed as expected"
  // );
});

function checkOiNodeText(oiNode, expectedText, assertionName) {
  // strip out unwanted character before the label
  is(
    oiNode.querySelector(".node").textContent.trim(),
    expectedText,
    assertionName
  );
}