summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser_webconsole_output_dom_elements_04.js
blob: 0158267ae4abff1b13a23e50fa3f2d2758be7dcf (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Test that inspector links in the webconsole output for DOM Nodes do not try
// to highlight or select nodes once they have been detached

"use strict";

const TEST_URI = "http://example.com/browser/devtools/client/webconsole/" +
                 "test/test-console-output-dom-elements.html";

const TEST_DATA = [
  {
    // The first test shouldn't be returning the body element as this is the
    // default selected node, so re-selecting it won't fire the
    // inspector-updated event
    input: "testNode()",
    output: '<p some-attribute="some-value">'
  },
  {
    input: "testSvgNode()",
    output: '<clipPath>'
  },
  {
    input: "testBodyNode()",
    output: '<body class="body-class" id="body-id">'
  },
  {
    input: "testNodeInIframe()",
    output: "<p>"
  },
  {
    input: "testDocumentElement()",
    output: '<html dir="ltr" lang="en-US">'
  }
];

const PREF = "devtools.webconsole.persistlog";

function test() {
  Services.prefs.setBoolPref(PREF, true);
  registerCleanupFunction(() => Services.prefs.clearUserPref(PREF));

  Task.spawn(function* () {
    let {tab} = yield loadTab(TEST_URI);
    let hud = yield openConsole(tab);
    let toolbox = gDevTools.getToolbox(hud.target);

    info("Executing the test data");
    let widgets = [];
    for (let data of TEST_DATA) {
      let [result] = yield jsEval(data.input, hud, {text: data.output});
      let {widget} = yield getWidgetAndMessage(result);
      widgets.push(widget);
    }

    info("Reloading the page");
    yield reloadPage();

    info("Iterating over the ElementNode widgets");
    for (let widget of widgets) {
      // Verify that openNodeInInspector rejects since the associated dom node
      // doesn't exist anymore
      yield widget.openNodeInInspector().then(() => {
        ok(false, "The openNodeInInspector promise resolved");
      }, () => {
        ok(true, "The openNodeInInspector promise rejected as expected");
      });
      yield toolbox.selectTool("webconsole");

      // Verify that highlightDomNode rejects too, for the same reason
      yield widget.highlightDomNode().then(() => {
        ok(false, "The highlightDomNode promise resolved");
      }, () => {
        ok(true, "The highlightDomNode promise rejected as expected");
      });
    }
  }).then(finishTest);
}

function jsEval(input, hud, message) {
  info("Executing '" + input + "' in the web console");
  hud.jsterm.execute(input);
  return waitForMessages({
    webconsole: hud,
    messages: [message]
  });
}

function* getWidgetAndMessage(result) {
  info("Getting the output ElementNode widget");

  let msg = [...result.matched][0];
  let widget = [...msg._messageObject.widgets][0];
  ok(widget, "ElementNode widget found in the output");

  info("Waiting for the ElementNode widget to be linked to the inspector");
  yield widget.linkToInspector();

  return {widget: widget, msg: msg};
}

function reloadPage() {
  let def = promise.defer();
  gBrowser.selectedBrowser.addEventListener("load", function onload() {
    gBrowser.selectedBrowser.removeEventListener("load", onload, true);
    def.resolve();
  }, true);
  content.location.reload();
  return def.promise;
}