browser_jsonview_chunked_json.js (3009B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const TEST_JSON_URL = URL_ROOT_SSL + "chunked_json.sjs"; 7 8 add_task(async function () { 9 info("Test chunked JSON started"); 10 11 await addJsonViewTab(TEST_JSON_URL, { 12 appReadyState: "interactive", 13 docReadyState: "loading", 14 }); 15 16 is( 17 await getElementCount(".rawdata.is-active"), 18 1, 19 "The Raw Data tab is selected." 20 ); 21 22 // Write some text and check that it is displayed. 23 await write("["); 24 await checkText(); 25 26 // Repeat just in case. 27 await write("1,"); 28 await checkText(); 29 30 is( 31 await getElementCount("button.prettyprint"), 32 0, 33 "There is no pretty print button during load" 34 ); 35 36 await selectJsonViewContentTab("json"); 37 is( 38 await getElementText(".jsonPanelBox > .panelContent"), 39 "", 40 "There is no JSON tree" 41 ); 42 43 await selectJsonViewContentTab("headers"); 44 ok( 45 await getElementText(".headersPanelBox .netInfoHeadersTable"), 46 "The headers table has been filled." 47 ); 48 49 // Write some text without being in Raw Data, then switch tab and check. 50 await write("2"); 51 await selectJsonViewContentTab("rawdata"); 52 await checkText(); 53 54 // Add an array, when counting rows we will ensure it has been expanded automatically. 55 await write(",[3]]"); 56 await checkText(); 57 58 // Close the connection. 59 60 // When the ready state of the JSON View app changes, it triggers the 61 // custom event "AppReadyStateChange". 62 const appReady = BrowserTestUtils.waitForContentEvent( 63 gBrowser.selectedBrowser, 64 "AppReadyStateChange", 65 true, 66 null, 67 true 68 ); 69 await server("close"); 70 await appReady; 71 72 is(await getElementCount(".json.is-active"), 1, "The JSON tab is selected."); 73 74 is( 75 await getElementCount(".jsonPanelBox .treeTable .treeRow"), 76 4, 77 "There is a tree with 4 rows." 78 ); 79 80 await selectJsonViewContentTab("rawdata"); 81 await checkText(); 82 83 is( 84 await getElementCount("button.prettyprint"), 85 1, 86 "There is a pretty print button." 87 ); 88 await clickJsonNode("button.prettyprint"); 89 await checkText(JSON.stringify(JSON.parse(data), null, 2)); 90 }); 91 92 let data = " "; 93 async function write(text) { 94 data += text; 95 const onJsonViewUpdated = SpecialPowers.spawn( 96 gBrowser.selectedBrowser, 97 [], 98 () => { 99 return new Promise(resolve => { 100 const observer = new content.MutationObserver(() => resolve()); 101 observer.observe(content.wrappedJSObject.JSONView.json, { 102 characterData: true, 103 }); 104 }); 105 } 106 ); 107 await server("write", text); 108 await onJsonViewUpdated; 109 } 110 async function checkText(text = data) { 111 is(await getElementText(".textPanelBox .data"), text, "Got the right text."); 112 } 113 114 function server(action, value) { 115 return new Promise(resolve => { 116 const xhr = new XMLHttpRequest(); 117 xhr.open("GET", TEST_JSON_URL + "?" + action + "=" + value); 118 xhr.addEventListener("load", resolve, { once: true }); 119 xhr.send(); 120 }); 121 }