tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_net_json-numbers.js (4967B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /**
      7 * Tests if JSON responses with numbers that can't be accurately represented in JS are correctly rendered.
      8 */
      9 add_task(async function () {
     10  const { tab, monitor } = await initNetMonitor(
     11    JSON_BASIC_URL + "?name=numbers",
     12    { requestCount: 1 }
     13  );
     14  info("Starting test... ");
     15 
     16  const { document, store, windowRequire } = monitor.panelWin;
     17  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
     18 
     19  store.dispatch(Actions.batchEnable(false));
     20 
     21  await performRequests(monitor, tab, 1);
     22 
     23  const onResponsePanelReady = waitForDOM(
     24    document,
     25    "#response-panel .data-header"
     26  );
     27 
     28  const onPropsViewReady = waitForDOM(
     29    document,
     30    "#response-panel .properties-view",
     31    1
     32  );
     33 
     34  store.dispatch(Actions.toggleNetworkDetails());
     35  clickOnSidebarTab(document, "response");
     36  await Promise.all([onResponsePanelReady, onPropsViewReady]);
     37 
     38  const tabpanel = document.querySelector("#response-panel");
     39  const labels = tabpanel.querySelectorAll("tr .treeLabelCell .treeLabel");
     40  const values = tabpanel.querySelectorAll("tr .treeValueCell .objectBox");
     41 
     42  info("Checking that regular numbers aren't rendered with JsonNumber");
     43  is(
     44    labels[0].textContent,
     45    "small",
     46    "The first json property name is correct."
     47  );
     48  is(values[0].textContent, "12", "The first json property value is correct.");
     49  ok(
     50    !values[0].classList.contains("objectBox-json-number") &&
     51      !values[0].querySelector(".source-value"),
     52    "Regular number does not get the json number class"
     53  );
     54 
     55  info("Checking that negative numbers aren't rendered with JsonNumber");
     56  is(
     57    labels[1].textContent,
     58    "negzero",
     59    "The second json property name is correct."
     60  );
     61  ok(
     62    !values[1].classList.contains("objectBox-json-number") &&
     63      !values[1].querySelector(".source-value"),
     64    "-0 does not get the json number class"
     65  );
     66 
     67  info("Check numbers bigger than Number.MAX_SAFE_INTEGER");
     68  is(labels[2].textContent, "big", "The third json property name is correct.");
     69  ok(
     70    values[2].classList.contains("objectBox-json-number"),
     71    "Big number get the json number class"
     72  );
     73  is(
     74    values[2].querySelector(".source-value").textContent,
     75    "1516340399466235648",
     76    "Big number has expected source text"
     77  );
     78  is(
     79    values[2].querySelector(".parsed-value").textContent,
     80    "JS:1516340399466235600",
     81    "Big number has expected parsed value text"
     82  );
     83  is(
     84    values[2].querySelector(".parsed-value").getAttribute("title"),
     85    "JavaScript parsed value",
     86    "Big number parsed value label has expected title attribute"
     87  );
     88 
     89  info("Check numbers with higher precision than what's possible in JS");
     90  is(labels[3].textContent, "precise", "Got expected fourth item");
     91  ok(
     92    values[3].classList.contains("objectBox-json-number"),
     93    "High precision number get the json number class"
     94  );
     95  is(
     96    values[3].querySelector(".source-value").textContent,
     97    "3.141592653589793238462643383279",
     98    "High precision number has expected source text"
     99  );
    100  is(
    101    values[3].querySelector(".parsed-value").textContent,
    102    "JS:3.141592653589793",
    103    "High precision number has expected parsed value text"
    104  );
    105  ok(
    106    values[3].querySelector(".parsed-value").getAttribute("title"),
    107    "High precision number parsed value label has a title attribute"
    108  );
    109 
    110  info("Checking that exponential numbers source is displayed");
    111  is(labels[4].textContent, "exp", "Got expected fourth item");
    112  ok(
    113    values[4].classList.contains("objectBox-json-number"),
    114    "Exponential number get the json number class"
    115  );
    116  is(
    117    values[4].querySelector(".source-value").textContent,
    118    "1e2",
    119    "Exponential number has expected source text"
    120  );
    121  is(
    122    values[4].querySelector(".parsed-value").textContent,
    123    "JS:100",
    124    "Exponential number has expected parsed value text"
    125  );
    126  ok(
    127    values[4].querySelector(".parsed-value").getAttribute("title"),
    128    "Exponential number parsed value label has a title attribute"
    129  );
    130 
    131  await teardown(monitor);
    132 });
    133 
    134 add_task(async function testLargeRootInteger() {
    135  const { tab, monitor } = await initNetMonitor(
    136    JSON_BASIC_URL + "?name=large-root-integer",
    137    { requestCount: 1 }
    138  );
    139 
    140  const { document, store, windowRequire } = monitor.panelWin;
    141  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
    142 
    143  store.dispatch(Actions.batchEnable(false));
    144 
    145  await performRequests(monitor, tab, 1);
    146 
    147  const onCodeMirrorReady = waitForDOM(document, "#response-panel .cm-content");
    148 
    149  store.dispatch(Actions.toggleNetworkDetails());
    150  clickOnSidebarTab(document, "response");
    151  const [codeMirrorCodeEl] = await onCodeMirrorReady;
    152  is(
    153    codeMirrorCodeEl.querySelector(".cm-line").textContent,
    154    "1516340399466235648",
    155    "Large number is displayed in a CodeMirror editor"
    156  );
    157 
    158  await teardown(monitor);
    159 });