summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/test/browser_inspector_delete-selected-node-02.js
blob: ff0d66d9b7d195fc03a077f574cab3ddb704b478 (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
/* 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/. */
"use strict";

// Test that when nodes are being deleted in the page, the current selection
// and therefore the markup view, css rule view, computed view, font view,
// box model view, and breadcrumbs, reset accordingly to show the right node

const TEST_PAGE = URL_ROOT + "doc_inspector_delete-selected-node-02.html";

add_task(async function() {
  const { inspector } = await openInspectorForURL(TEST_PAGE);

  await testManuallyDeleteSelectedNode();
  await testAutomaticallyDeleteSelectedNode();
  await testDeleteSelectedNodeContainerFrame();
  await testDeleteWithNonElementNode();

  async function testManuallyDeleteSelectedNode() {
    info(
      "Selecting a node, deleting it via context menu and checking that " +
        "its parent node is selected and breadcrumbs are updated."
    );

    await deleteNodeWithContextMenu("#deleteManually");

    info("Performing checks.");
    await assertNodeSelectedAndPanelsUpdated(
      "#selectedAfterDelete",
      "li#selectedAfterDelete"
    );
  }

  async function testAutomaticallyDeleteSelectedNode() {
    info(
      "Selecting a node, deleting it via javascript and checking that " +
        "its parent node is selected and breadcrumbs are updated."
    );

    const div = await getNodeFront("#deleteAutomatically", inspector);
    await selectNode(div, inspector);

    info("Deleting selected node via javascript.");
    await inspector.walker.removeNode(div);

    info("Waiting for inspector to update.");
    await inspector.once("inspector-updated");

    info("Inspector updated, performing checks.");
    await assertNodeSelectedAndPanelsUpdated(
      "#deleteChildren",
      "ul#deleteChildren"
    );
  }

  async function testDeleteSelectedNodeContainerFrame() {
    info(
      "Selecting a node inside iframe, deleting the iframe via javascript " +
        "and checking the parent node of the iframe is selected and " +
        "breadcrumbs are updated."
    );

    info("Selecting an element inside iframe.");
    const iframe = await getNodeFront("#deleteIframe", inspector);
    const div = await getNodeFrontInFrame("#deleteInIframe", iframe, inspector);
    await selectNode(div, inspector);

    info("Deleting selected node via javascript.");
    await inspector.walker.removeNode(iframe);

    info("Waiting for inspector to update.");
    await inspector.once("inspector-updated");

    info("Inspector updated, performing checks.");
    await assertNodeSelectedAndPanelsUpdated("body", "body");
  }

  async function testDeleteWithNonElementNode() {
    info(
      "Selecting a node, deleting it via context menu and checking that " +
        "its parent node is selected and breadcrumbs are updated " +
        "when the node is followed by a non-element node"
    );

    await deleteNodeWithContextMenu("#deleteWithNonElement");

    let expectedCrumbs = ["html", "body", "div#deleteToMakeSingleTextNode"];
    await assertNodeSelectedAndCrumbsUpdated(expectedCrumbs, Node.TEXT_NODE);

    // Delete node with key, as cannot delete text node with
    // context menu at this time.
    inspector.markup._frame.focus();
    EventUtils.synthesizeKey("KEY_Delete");
    await inspector.once("inspector-updated");

    expectedCrumbs = ["html", "body", "div#deleteToMakeSingleTextNode"];
    await assertNodeSelectedAndCrumbsUpdated(expectedCrumbs, Node.ELEMENT_NODE);
  }

  async function deleteNodeWithContextMenu(selector) {
    await selectNode(selector, inspector);
    const nodeToBeDeleted = inspector.selection.nodeFront;

    info("Getting the node container in the markup view.");
    const container = await getContainerForSelector(selector, inspector);

    const allMenuItems = openContextMenuAndGetAllItems(inspector, {
      target: container.tagLine,
    });
    const menuItem = allMenuItems.find(item => item.id === "node-menu-delete");

    info("Clicking 'Delete Node' in the context menu.");
    is(menuItem.disabled, false, "delete menu item is enabled");
    menuItem.click();

    // close the open context menu
    EventUtils.synthesizeKey("KEY_Escape");

    info("Waiting for inspector to update.");
    await inspector.once("inspector-updated");

    // Since the mutations are sent asynchronously from the server, the
    // inspector-updated event triggered by the deletion might happen before
    // the mutation is received and the element is removed from the
    // breadcrumbs. See bug 1284125.
    if (inspector.breadcrumbs.indexOf(nodeToBeDeleted) > -1) {
      info("Crumbs haven't seen deletion. Waiting for breadcrumbs-updated.");
      await inspector.once("breadcrumbs-updated");
    }

    return menuItem;
  }

  function assertNodeSelectedAndCrumbsUpdated(
    expectedCrumbs,
    expectedNodeType
  ) {
    info("Performing checks");
    const actualNodeType = inspector.selection.nodeFront.nodeType;
    is(actualNodeType, expectedNodeType, "The node has the right type");

    const breadcrumbs = inspector.panelDoc.querySelectorAll(
      "#inspector-breadcrumbs .html-arrowscrollbox-inner > *"
    );
    is(
      breadcrumbs.length,
      expectedCrumbs.length,
      "Have the correct number of breadcrumbs"
    );
    for (let i = 0; i < breadcrumbs.length; i++) {
      is(
        breadcrumbs[i].textContent,
        expectedCrumbs[i],
        "Text content for button " + i + " is correct"
      );
    }
  }

  async function assertNodeSelectedAndPanelsUpdated(selector, crumbLabel) {
    const nodeFront = await getNodeFront(selector, inspector);
    is(inspector.selection.nodeFront, nodeFront, "The right node is selected");

    const breadcrumbs = inspector.panelDoc.querySelector(
      "#inspector-breadcrumbs .html-arrowscrollbox-inner"
    );
    is(
      breadcrumbs.querySelector("button[checked=true]").textContent,
      crumbLabel,
      "The right breadcrumb is selected"
    );
  }
});