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

// Test the "Copy object" menu item of the webconsole is enabled only when
// clicking on messages that are associated with an object actor.

"use strict";

const TEST_URI = `data:text/html;charset=utf-8,<!DOCTYPE html><script>
  window.bar = { baz: 1 };
  console.log("foo");
  console.log("foo", window.bar);
  console.log(["foo", window.bar, 2]);
  console.group("group");
  console.groupCollapsed("collapsed");
  console.groupEnd();
  console.log(532);
  console.log(true);
  console.log(false);
  console.log(undefined);
  console.log(null);
  /* Verify that the conflicting binding on user code doesn't break the
   * functionality. */
  function copy() { alert("user-defined function is called"); }
</script>`;
const copyObjectMenuItemId = "#console-menu-copy-object";

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

  const [msgWithText, msgWithObj, msgNested] = await waitFor(() =>
    findConsoleAPIMessages(hud, "foo")
  );
  ok(
    msgWithText && msgWithObj && msgNested,
    "Three messages should have appeared"
  );

  const [groupMsgObj] = await waitFor(() =>
    findMessagePartsByType(hud, {
      text: "group",
      typeSelector: ".console-api",
      partSelector: ".message-body",
    })
  );
  const [collapsedGroupMsgObj] = await waitFor(() =>
    findMessagePartsByType(hud, {
      text: "collapsed",
      typeSelector: ".console-api",
      partSelector: ".message-body",
    })
  );
  const [numberMsgObj] = await waitFor(() =>
    findMessagePartsByType(hud, {
      text: `532`,
      typeSelector: ".console-api",
      partSelector: ".message-body",
    })
  );
  const [trueMsgObj] = await waitFor(() =>
    findMessagePartsByType(hud, {
      text: `true`,
      typeSelector: ".console-api",
      partSelector: ".message-body",
    })
  );
  const [falseMsgObj] = await waitFor(() =>
    findMessagePartsByType(hud, {
      text: `false`,
      typeSelector: ".console-api",
      partSelector: ".message-body",
    })
  );
  const [undefinedMsgObj] = await waitFor(() =>
    findMessagePartsByType(hud, {
      text: `undefined`,
      typeSelector: ".console-api",
      partSelector: ".message-body",
    })
  );
  const [nullMsgObj] = await waitFor(() =>
    findMessagePartsByType(hud, {
      text: `null`,
      typeSelector: ".console-api",
      partSelector: ".message-body",
    })
  );
  ok(nullMsgObj, "One message with null value should have appeared");

  const text = msgWithText.querySelector(".objectBox-string");
  const objInMsgWithObj = msgWithObj.querySelector(".objectBox-object");
  const textInMsgWithObj = msgWithObj.querySelector(".objectBox-string");

  // The third message has an object nested in an array, the array is therefore the top
  // object, the object is the nested object.
  const topObjInMsg = msgNested.querySelector(".objectBox-array");
  const nestedObjInMsg = msgNested.querySelector(".objectBox-object");

  const consoleMessages = await waitFor(() =>
    findMessagePartsByType(hud, {
      text: 'console.log("foo");',
      typeSelector: ".console-api",
      partSelector: ".message-location",
    })
  );
  await testCopyObjectMenuItemDisabled(hud, consoleMessages[0]);

  info(`Check "Copy object" is enabled for text only messages
    thus copying the text`);
  await testCopyObject(hud, text, `foo`, false);

  info(`Check "Copy object" is enabled for text in complex messages
   thus copying the text`);
  await testCopyObject(hud, textInMsgWithObj, `foo`, false);

  info("Check `Copy object` is enabled for objects in complex messages");
  await testCopyObject(hud, objInMsgWithObj, `{"baz":1}`, true);

  info("Check `Copy object` is enabled for top object in nested messages");
  await testCopyObject(hud, topObjInMsg, `["foo",{"baz":1},2]`, true);

  info("Check `Copy object` is enabled for nested object in nested messages");
  await testCopyObject(hud, nestedObjInMsg, `{"baz":1}`, true);

  info("Check `Copy object` is disabled on `console.group('group')` messages");
  await testCopyObjectMenuItemDisabled(hud, groupMsgObj);

  info(`Check "Copy object" is disabled in "console.groupCollapsed('collapsed')"
    messages`);
  await testCopyObjectMenuItemDisabled(hud, collapsedGroupMsgObj);

  // Check for primitive objects
  info("Check `Copy object` is enabled for numbers");
  await testCopyObject(hud, numberMsgObj, `532`, false);

  info("Check `Copy object` is enabled for booleans");
  await testCopyObject(hud, trueMsgObj, `true`, false);
  await testCopyObject(hud, falseMsgObj, `false`, false);

  info("Check `Copy object` is enabled for undefined and null");
  await testCopyObject(hud, undefinedMsgObj, `undefined`, false);
  await testCopyObject(hud, nullMsgObj, `null`, false);
});

async function testCopyObject(hud, element, expectedMessage, objectInput) {
  info("Check `Copy object` is enabled");
  const menuPopup = await openContextMenu(hud, element);
  const copyObjectMenuItem = menuPopup.querySelector(copyObjectMenuItemId);
  ok(
    !copyObjectMenuItem.disabled,
    "`Copy object` is enabled for object in complex message"
  );
  is(
    copyObjectMenuItem.getAttribute("accesskey"),
    "o",
    "`Copy object` has the right accesskey"
  );

  const validatorFn = data => {
    const prettifiedMessage = prettyPrintMessage(expectedMessage, objectInput);
    return data === prettifiedMessage;
  };

  info("Activate item `Copy object`");
  await waitForClipboardPromise(
    () => menuPopup.activateItem(copyObjectMenuItem),
    validatorFn
  );
}

async function testCopyObjectMenuItemDisabled(hud, element) {
  const menuPopup = await openContextMenu(hud, element);
  const copyObjectMenuItem = menuPopup.querySelector(copyObjectMenuItemId);
  ok(
    copyObjectMenuItem.disabled,
    `"Copy object" is disabled for messages
    with no variables/objects`
  );
  await hideContextMenu(hud);
}

function prettyPrintMessage(message, isObject) {
  return isObject ? JSON.stringify(JSON.parse(message), null, 2) : message;
}