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

"use strict";

// Fix blank inspector bug when opening DevTools on a webpage with a document
// where document.write is called without ever calling document.close.
// Such a document remains forever in the "loading" readyState. See Bug 1765760.

const TEST_URL =
  `data:text/html;charset=utf-8,` +
  encodeURIComponent(`
  <!DOCTYPE html>
  <html lang="en">
    <body>
      <div id=outsideframe></div>
      <script type="text/javascript">
        const iframe = document.createElement("iframe");
        iframe.src = "about:blank";
        iframe.addEventListener('load', () => {
          iframe.contentDocument.write('<div id=inframe>inframe</div>');
        }, true);
        document.body.appendChild(iframe);
      </script>
    </body>
  </html>
`);

add_task(async function testSlowLoadingFrame() {
  const loadingTab = BrowserTestUtils.addTab(gBrowser, TEST_URL);
  gBrowser.selectedTab = loadingTab;

  // Note: we cannot use `await addTab` here because the iframe never finishes
  // loading. But we still want to wait for the frame to reach the loading state
  // and to display a test element so that we can reproduce the initial issue
  // fixed by Bug 1765760.
  info("Wait for the loading iframe to be ready to test");
  await TestUtils.waitForCondition(async () => {
    try {
      return await ContentTask.spawn(gBrowser.selectedBrowser, {}, () => {
        const iframe = content.document.querySelector("iframe");
        return (
          iframe?.contentDocument.readyState === "loading" &&
          iframe.contentDocument.getElementById("inframe")
        );
      });
    } catch (e) {
      return false;
    }
  });

  const { inspector } = await openInspector();

  info("Check the markup view displays the loading iframe successfully");
  await assertMarkupViewAsTree(
    `
    body
      div id="outsideframe"
      script!ignore-children
      iframe
        #document
          html
            head
            body
              div id="inframe"`,
    "body",
    inspector
  );
});

add_task(async function testSlowLoadingDocument() {
  info("Create a test server serving a slow document");
  const httpServer = createTestHTTPServer();
  httpServer.registerContentType("html", "text/html");

  // This promise allows to block serving the complete document from the test.
  let unblockRequest;
  const onRequestUnblocked = new Promise(r => (unblockRequest = r));

  httpServer.registerPathHandler(`/`, async function (request, response) {
    response.processAsync();
    response.setStatusLine(request.httpVersion, 200, "OK");

    // Split the page content in 2 parts:
    // - opening body tag and the "#start" div will be returned immediately
    // - "#end" div and closing body tag are blocked on a promise.
    const page_start = "<body><div id='start'>start</div>";
    const page_end = "<div id='end'>end</div></body>";
    const page = page_start + page_end;

    response.setHeader("Content-Type", "text/html; charset=utf-8", false);
    response.setHeader("Content-Length", page.length + "", false);
    response.write(page_start);

    await onRequestUnblocked;

    response.write(page_end);
    response.finish();
  });

  const port = httpServer.identity.primaryPort;
  const TEST_URL_2 = `http://localhost:${port}/`;

  // Same as in the other task, we cannot wait for the full load.
  info("Open a new tab on TEST_URL_2 and select it");
  const loadingTab = BrowserTestUtils.addTab(gBrowser, TEST_URL_2);
  gBrowser.selectedTab = loadingTab;

  info("Wait for the #start div to be available in the document");
  await TestUtils.waitForCondition(async () => {
    try {
      return await ContentTask.spawn(gBrowser.selectedBrowser, {}, () =>
        content.document.getElementById("start")
      );
    } catch (e) {
      return false;
    }
  });

  const { inspector } = await openInspector();

  info("Check that the inspector is not blank and only shows the #start div");
  await assertMarkupViewAsTree(
    `
    body
      div id="start"`,
    "body",
    inspector
  );

  // Navigate to about:blank to clean the state.
  await navigateTo("about:blank");

  const markuploaded = inspector.once("markuploaded");
  const onNewRoot = inspector.once("new-root");
  const onUpdated = inspector.once("inspector-updated");

  await navigateTo(TEST_URL_2, { waitForLoad: false });
  info("Wait for the #start div to be available as a markupview container");
  await TestUtils.waitForCondition(async () => {
    const nodeFront = await getNodeFront("#start", inspector);
    return nodeFront && getContainerForNodeFront(nodeFront, inspector);
  });

  info("Check that the inspector is not blank and only shows the #start div");
  await assertMarkupViewAsTree(
    `
    body
      div id="start"`,
    "body",
    inspector
  );

  info("Unblock the document request");
  unblockRequest();

  info("Wait for the #end div to be available as a markupview container");
  await TestUtils.waitForCondition(async () => {
    const nodeFront = await getNodeFront("#end", inspector);
    return nodeFront && getContainerForNodeFront(nodeFront, inspector);
  });

  info("Check that the inspector will ultimately show the #end div");
  await assertMarkupViewAsTree(
    `
    body
      div id="start"
      div id="end"`,
    "body",
    inspector
  );

  info(
    "Waiting for inspector to update after having released the document load"
  );
  await markuploaded;
  await onNewRoot;
  await onUpdated;
});