browser_jsonview_numbers.js (7028B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 add_task(async function () { 7 await addJsonViewTab( 8 `data:application/json,{ 9 "small": 12, 10 "negzero": -0, 11 "big": 1516340399466235648, 12 "precise": 3.141592653589793238462643383279, 13 "exp": 1e2 14 }`.replaceAll("\n", "") // ensure equality with actually loaded URI 15 ); 16 17 await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => { 18 const rows = content.document.querySelectorAll( 19 ".jsonPanelBox .treeTable .treeRow" 20 ); 21 is(rows.length, 5, "There is 5 properties"); 22 23 rows.forEach((row, i) => { 24 ok( 25 !row.querySelector(".theme-twisty"), 26 `item #${i} doesn't have an expand button` 27 ); 28 }); 29 30 info("Checking that regular numbers aren't rendered with JsonNumber"); 31 is( 32 rows[0].querySelector(".treeLabel").textContent, 33 "small", 34 "Got expected first item" 35 ); 36 const smallValueEl = rows[0].querySelector(".objectBox-number"); 37 is(smallValueEl.textContent, "12", "First item has expected text"); 38 ok( 39 !smallValueEl.classList.contains("objectBox-json-number") && 40 !smallValueEl.querySelector(".source-value"), 41 "Regular number does not get the lossy class" 42 ); 43 44 info("Checking that negative numbers aren't rendered with JsonNumber"); 45 is( 46 rows[1].querySelector(".treeLabel").textContent, 47 "negzero", 48 "Got expected second item" 49 ); 50 const negZeroValueEl = rows[1].querySelector(".objectBox-number"); 51 is(negZeroValueEl.textContent, "-0", "-0 has expected text"); 52 ok( 53 !negZeroValueEl.classList.contains("objectBox-json-number"), 54 "-0 does not get the lossy class" 55 ); 56 57 info("Checkt numbers bigger than Number.MAX_SAFE_INTEGER"); 58 is( 59 rows[2].querySelector(".treeLabel").textContent, 60 "big", 61 "Got expected third item" 62 ); 63 const bigValueEl = rows[2].querySelector(".objectBox-number"); 64 ok( 65 bigValueEl.classList.contains("objectBox-json-number"), 66 "Big number get the lossy class" 67 ); 68 is( 69 bigValueEl.querySelector(".source-value").textContent, 70 "1516340399466235648", 71 "Big number has expected source text" 72 ); 73 is( 74 bigValueEl.querySelector(".parsed-value").textContent, 75 "JS:1516340399466235600", 76 "Big number has expected parsed value text" 77 ); 78 ok( 79 bigValueEl.querySelector(".parsed-value").getAttribute("title"), 80 "Big number parsed value label has a title attribute" 81 ); 82 83 info("Check numbers with higher precision than what's possible in JS"); 84 is( 85 rows[3].querySelector(".treeLabel").textContent, 86 "precise", 87 "Got expected fourth item" 88 ); 89 const preciseValueEl = rows[3].querySelector(".objectBox-number"); 90 ok( 91 preciseValueEl.classList.contains("objectBox-json-number"), 92 "High precision number get the lossy class" 93 ); 94 is( 95 preciseValueEl.querySelector(".source-value").textContent, 96 "3.141592653589793238462643383279", 97 "High precision number has expected source text" 98 ); 99 is( 100 preciseValueEl.querySelector(".parsed-value").textContent, 101 "JS:3.141592653589793", 102 "High precision number has expected parsed value text" 103 ); 104 ok( 105 preciseValueEl.querySelector(".parsed-value").getAttribute("title"), 106 "High precision number parsed value label has a title attribute" 107 ); 108 109 info("Checking that exponential numbers source is displayed"); 110 is( 111 rows[4].querySelector(".treeLabel").textContent, 112 "exp", 113 "Got expected fourth item" 114 ); 115 const expValueEl = rows[4].querySelector(".objectBox-number"); 116 ok( 117 expValueEl.classList.contains("objectBox-json-number"), 118 "Exponential number get the lossy class" 119 ); 120 is( 121 expValueEl.querySelector(".source-value").textContent, 122 "1e2", 123 "Exponential number has expected source text" 124 ); 125 is( 126 expValueEl.querySelector(".parsed-value").textContent, 127 "JS:100", 128 "Exponential number has expected parsed value text" 129 ); 130 ok( 131 expValueEl.querySelector(".parsed-value").getAttribute("title"), 132 "Exponential number parsed value label has a title attribute" 133 ); 134 }); 135 136 info("Select the RawData tab"); 137 await selectJsonViewContentTab("rawdata"); 138 139 const text = await getElementText(".textPanelBox .data"); 140 is( 141 text, 142 `{ 143 "small": 12, 144 "negzero": -0, 145 "big": 1516340399466235648, 146 "precise": 3.141592653589793238462643383279, 147 "exp": 1e2 148 }`.replaceAll("\n", ""), 149 "Proper JSON must be displayed in DOM" 150 ); 151 152 info("Click 'Pretty Print' button"); 153 await BrowserTestUtils.synthesizeMouseAtCenter( 154 ".textPanelBox .toolbar button.prettyprint", 155 {}, 156 gBrowser.selectedBrowser 157 ); 158 159 let prettyText = await getElementText(".textPanelBox .data"); 160 prettyText = normalizeNewLines(prettyText); 161 is( 162 prettyText, 163 `{ 164 "small": 12, 165 "negzero": -0, 166 "big": 1516340399466235648, 167 "precise": 3.141592653589793238462643383279, 168 "exp": 1e2 169 }`, 170 "Pretty printed JSON must be displayed" 171 ); 172 }); 173 174 add_task(async function testBigNumberNotNested() { 175 await addJsonViewTab(`data:application/json,9999999999999999`); 176 177 await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => { 178 const values = content.document.querySelectorAll( 179 ".jsonPanelBox .jsonPrimitiveValue" 180 ); 181 is(values.length, 1, "There is 1 JSON primitive value"); 182 183 ok( 184 !values[0].querySelector(".theme-twisty"), 185 `top level big number doesn't have an expand button` 186 ); 187 188 info("Check a root number bigger than Number.MAX_SAFE_INTEGER"); 189 const rootBigNumberValueEl = values[0].querySelector(".objectBox-number"); 190 ok( 191 rootBigNumberValueEl.classList.contains("objectBox-json-number"), 192 "Big number get the lossy class" 193 ); 194 is( 195 rootBigNumberValueEl.querySelector(".source-value").textContent, 196 "9999999999999999", 197 "Big number has expected source text" 198 ); 199 is( 200 rootBigNumberValueEl.querySelector(".parsed-value").textContent, 201 "JS:10000000000000000", 202 "Big number has expected parsed value text" 203 ); 204 ok( 205 rootBigNumberValueEl.querySelector(".parsed-value").getAttribute("title"), 206 "Big number parsed value label has a title attribute" 207 ); 208 }); 209 210 info("Select the RawData tab"); 211 await selectJsonViewContentTab("rawdata"); 212 213 const text = await getElementText(".textPanelBox .data"); 214 is(text, "9999999999999999", "Proper JSON must be displayed in DOM"); 215 216 info("Click 'Pretty Print' button"); 217 await BrowserTestUtils.synthesizeMouseAtCenter( 218 ".textPanelBox .toolbar button.prettyprint", 219 {}, 220 gBrowser.selectedBrowser 221 ); 222 223 let prettyText = await getElementText(".textPanelBox .data"); 224 prettyText = normalizeNewLines(prettyText); 225 is(prettyText, "9999999999999999", "Pretty printed JSON must be displayed"); 226 });