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

"use strict";

const TEST_JSON_URL = URL_ROOT_SSL + "chunked_json.sjs";

add_task(async function () {
  info("Test chunked JSON started");

  await addJsonViewTab(TEST_JSON_URL, {
    appReadyState: "interactive",
    docReadyState: "loading",
  });

  is(
    await getElementCount(".rawdata.is-active"),
    1,
    "The Raw Data tab is selected."
  );

  // Write some text and check that it is displayed.
  await write("[");
  await checkText();

  // Repeat just in case.
  await write("1,");
  await checkText();

  is(
    await getElementCount("button.prettyprint"),
    0,
    "There is no pretty print button during load"
  );

  await selectJsonViewContentTab("json");
  is(
    await getElementText(".jsonPanelBox > .panelContent"),
    "",
    "There is no JSON tree"
  );

  await selectJsonViewContentTab("headers");
  ok(
    await getElementText(".headersPanelBox .netInfoHeadersTable"),
    "The headers table has been filled."
  );

  // Write some text without being in Raw Data, then switch tab and check.
  await write("2");
  await selectJsonViewContentTab("rawdata");
  await checkText();

  // Add an array, when counting rows we will ensure it has been expanded automatically.
  await write(",[3]]");
  await checkText();

  // Close the connection.

  // When the ready state of the JSON View app changes, it triggers the
  // custom event "AppReadyStateChange".
  const appReady = BrowserTestUtils.waitForContentEvent(
    gBrowser.selectedBrowser,
    "AppReadyStateChange",
    true,
    null,
    true
  );
  await server("close");
  await appReady;

  is(await getElementCount(".json.is-active"), 1, "The JSON tab is selected.");

  is(
    await getElementCount(".jsonPanelBox .treeTable .treeRow"),
    4,
    "There is a tree with 4 rows."
  );

  await selectJsonViewContentTab("rawdata");
  await checkText();

  is(
    await getElementCount("button.prettyprint"),
    1,
    "There is a pretty print button."
  );
  await clickJsonNode("button.prettyprint");
  await checkText(JSON.stringify(JSON.parse(data), null, 2));
});

let data = " ";
async function write(text) {
  data += text;
  const onJsonViewUpdated = SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [],
    () => {
      return new Promise(resolve => {
        const observer = new content.MutationObserver(() => resolve());
        observer.observe(content.wrappedJSObject.JSONView.json, {
          characterData: true,
        });
      });
    }
  );
  await server("write", text);
  await onJsonViewUpdated;
}
async function checkText(text = data) {
  is(await getElementText(".textPanelBox .data"), text, "Got the right text.");
}

function server(action, value) {
  return new Promise(resolve => {
    const xhr = new XMLHttpRequest();
    xhr.open("GET", TEST_JSON_URL + "?" + action + "=" + value);
    xhr.addEventListener("load", resolve, { once: true });
    xhr.send();
  });
}