tor-browser

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

browser_jsonview_row_selection.js (7686B)


      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  info("Test 1 JSON row selection started");
      8 
      9  // Create a tall JSON so that there is a scrollbar.
     10  // Use 10,000 elements which creates 100 buckets (bucket size = 100)
     11  const numElements = 1e4;
     12  const json = JSON.stringify(
     13    Array(numElements)
     14      .fill()
     15      .map((_, i) => i)
     16  );
     17  const tab = await addJsonViewTab("data:application/json," + json);
     18 
     19  // Array with 10,000 elements creates buckets (100 buckets of 100 elements each)
     20  // The root array expands to show 100 bucket rows
     21  const numRows = 100;
     22  is(
     23    await getElementCount(".treeRow"),
     24    numRows,
     25    "Got the expected number of rows."
     26  );
     27  await assertRowSelected(null);
     28 
     29  // Focus the tree and select first row.
     30  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
     31    const tree = content.document.querySelector(".treeTable");
     32    tree.focus();
     33    is(tree, content.document.activeElement, "Tree should be focused");
     34    content.document.querySelector(".treeRow:nth-child(1)").click();
     35  });
     36  await assertRowSelected(1);
     37 
     38  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
     39    const scroller = content.document.querySelector(
     40      ".jsonPanelBox .panelContent"
     41    );
     42    Assert.less(
     43      scroller.clientHeight,
     44      scroller.scrollHeight,
     45      "There is a scrollbar."
     46    );
     47    is(scroller.scrollTop, 0, "Initially scrolled to the top.");
     48  });
     49 
     50  // Select last row.
     51  await BrowserTestUtils.synthesizeKey("VK_END", {}, tab.linkedBrowser);
     52  await assertRowSelected(numRows);
     53 
     54  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
     55    const scroller = content.document.querySelector(
     56      ".jsonPanelBox .panelContent"
     57    );
     58    is(
     59      scroller.scrollTop + scroller.clientHeight,
     60      scroller.scrollHeight,
     61      "Scrolled to the bottom."
     62    );
     63    // Click to select 2nd row.
     64    content.document.querySelector(".treeRow:nth-child(2)").click();
     65  });
     66  await assertRowSelected(2);
     67 
     68  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
     69    const scroller = content.document.querySelector(
     70      ".jsonPanelBox .panelContent"
     71    );
     72    Assert.greater(scroller.scrollTop, 0, "Not scrolled to the top.");
     73    // Synthesize up arrow key to select first row.
     74    content.document.querySelector(".treeTable").focus();
     75  });
     76  await BrowserTestUtils.synthesizeKey("VK_UP", {}, tab.linkedBrowser);
     77  await assertRowSelected(1);
     78  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
     79    const scroller = content.document.querySelector(
     80      ".jsonPanelBox .panelContent"
     81    );
     82    is(scroller.scrollTop, 0, "Scrolled to the top.");
     83  });
     84 });
     85 
     86 add_task(async function () {
     87  info("Test 2 JSON row selection started");
     88 
     89  const numRows = 4;
     90  const tab = await addJsonViewTab("data:application/json,[0,1,2,3]");
     91 
     92  is(
     93    await getElementCount(".treeRow"),
     94    numRows,
     95    "Got the expected number of rows."
     96  );
     97  await assertRowSelected(null);
     98 
     99  // Click to select first row.
    100  await clickJsonNode(".treeRow:first-child");
    101  await assertRowSelected(1);
    102 
    103  // Synthesize multiple down arrow keydowns to select following rows.
    104  await SpecialPowers.spawn(tab.linkedBrowser, [], function () {
    105    content.document.querySelector(".treeTable").focus();
    106  });
    107  for (let i = 2; i < numRows; ++i) {
    108    await BrowserTestUtils.synthesizeKey(
    109      "VK_DOWN",
    110      { type: "keydown" },
    111      tab.linkedBrowser
    112    );
    113    await assertRowSelected(i);
    114  }
    115 
    116  // Now synthesize the keyup, this shouldn't change selected row.
    117  await BrowserTestUtils.synthesizeKey(
    118    "VK_DOWN",
    119    { type: "keyup" },
    120    tab.linkedBrowser
    121  );
    122  await wait(500);
    123  await assertRowSelected(numRows - 1);
    124 
    125  // Finally, synthesize keydown with a modifier, this also shouldn't change selected row.
    126  await BrowserTestUtils.synthesizeKey(
    127    "VK_DOWN",
    128    { type: "keydown", shiftKey: true },
    129    tab.linkedBrowser
    130  );
    131  await wait(500);
    132  await assertRowSelected(numRows - 1);
    133 });
    134 
    135 add_task(async function () {
    136  info("Test 3 JSON row selection started");
    137 
    138  // Create a JSON with a row taller than the panel.
    139  const json = JSON.stringify([0, "a ".repeat(1e4), 1]);
    140  const tab = await addJsonViewTab("data:application/json," + encodeURI(json));
    141 
    142  is(await getElementCount(".treeRow"), 3, "Got the expected number of rows.");
    143  await assertRowSelected(null);
    144  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    145    const scroller = content.document.querySelector(
    146      ".jsonPanelBox .panelContent"
    147    );
    148    const row = content.document.querySelector(".treeRow:nth-child(2)");
    149    Assert.less(
    150      scroller.clientHeight,
    151      row.clientHeight,
    152      "The row is taller than the scroller."
    153    );
    154    is(scroller.scrollTop, 0, "Initially scrolled to the top.");
    155 
    156    // Select the tall row.
    157    content.document.querySelector(".treeTable").focus();
    158    row.click();
    159  });
    160  await assertRowSelected(2);
    161  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    162    const scroller = content.document.querySelector(
    163      ".jsonPanelBox .panelContent"
    164    );
    165    is(
    166      scroller.scrollTop,
    167      0,
    168      "When the row is visible, do not scroll on click."
    169    );
    170  });
    171 
    172  // Select the last row.
    173  await BrowserTestUtils.synthesizeKey("VK_DOWN", {}, tab.linkedBrowser);
    174  await assertRowSelected(3);
    175  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    176    const scroller = content.document.querySelector(
    177      ".jsonPanelBox .panelContent"
    178    );
    179    is(
    180      scroller.scrollTop + scroller.offsetHeight,
    181      scroller.scrollHeight,
    182      "Scrolled to the bottom."
    183    );
    184 
    185    // Select the tall row.
    186    const row = content.document.querySelector(".treeRow:nth-child(2)");
    187    row.click();
    188  });
    189 
    190  await assertRowSelected(2);
    191  const scroll = await SpecialPowers.spawn(
    192    gBrowser.selectedBrowser,
    193    [],
    194    function () {
    195      const scroller = content.document.querySelector(
    196        ".jsonPanelBox .panelContent"
    197      );
    198      const row = content.document.querySelector(".treeRow:nth-child(2)");
    199      is(
    200        scroller.scrollTop + scroller.offsetHeight,
    201        scroller.scrollHeight,
    202        "Scrolled to the bottom. When the row is visible, do not scroll on click."
    203      );
    204 
    205      // Scroll up a bit, so that both the top and bottom of the row are not visible.
    206      const scrollPos = (scroller.scrollTop = Math.ceil(
    207        (scroller.scrollTop + row.offsetTop) / 2
    208      ));
    209      Assert.greater(
    210        scroller.scrollTop,
    211        row.offsetTop,
    212        "The top of the row is not visible."
    213      );
    214      Assert.less(
    215        scroller.scrollTop + scroller.offsetHeight,
    216        row.offsetTop + row.offsetHeight,
    217        "The bottom of the row is not visible."
    218      );
    219 
    220      // Select the tall row.
    221      row.click();
    222      return scrollPos;
    223    }
    224  );
    225 
    226  await assertRowSelected(2);
    227  await SpecialPowers.spawn(
    228    gBrowser.selectedBrowser,
    229    [scroll],
    230    function (scrollPos) {
    231      const scroller = content.document.querySelector(
    232        ".jsonPanelBox .panelContent"
    233      );
    234      is(scroller.scrollTop, scrollPos, "Scroll did not change");
    235    }
    236  );
    237 });
    238 
    239 async function assertRowSelected(rowNum) {
    240  const idx = await SpecialPowers.spawn(
    241    gBrowser.selectedBrowser,
    242    [],
    243    function () {
    244      return [].indexOf.call(
    245        content.document.querySelectorAll(".treeRow"),
    246        content.document.querySelector(".treeRow.selected")
    247      );
    248    }
    249  );
    250  is(
    251    idx + 1,
    252    +rowNum,
    253    `${rowNum ? "The row #" + rowNum : "No row"} is selected.`
    254  );
    255 }